Question

I'm building an OS X cocoa application and I'm not using interface builder (for a variety of reasons). I've got the application to the point of loading a menu, title bar and main window, but I can't seem to figure out how to add the stoplight buttons to the title bar (programmatically).

My AppDelegate.m looks like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MainViewController *mVC = [MainViewController new];
    [mVC showMainViewController];
}

Then code in the MainViewController then creates the menu, window, and loads the application, as follows:

//
//  MainViewController.m
//  TestApp
//

#import "MainViewController.h"

@implementation MainViewController

@synthesize menubar;
@synthesize appMenu;
@synthesize appMenuItem;
@synthesize quitMenuItem;
@synthesize appName;
@synthesize quitTitle;
@synthesize window;
@synthesize homeViewController;
@synthesize resolutionHeight;
@synthesize resolutionWidth;
@synthesize width;
@synthesize height;

- (id)init
{
    self = [super init];
    if (self) {
        appName = @"TestApp";
        [self setupMenubar];
        [self setupMainWindow];
    }
    return self;
}

- (void)setupMenubar
{
    // Set up the main menu
    menubar = [NSMenu new];
    appMenu = [NSMenu new];
    appMenuItem = [NSMenuItem new];
    quitTitle = [NSString stringWithFormat:@"Quit %@", appName];
    quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
                                              action:@selector(terminate:)
                                       keyEquivalent:@"q"];

    [menubar addItem:appMenuItem];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
    [NSApp setMainMenu:menubar];
}

- (void)setupMainWindow
{
    // set the dimensions of the application
    resolutionWidth = [[NSScreen mainScreen] frame].size.width;
    resolutionHeight = [[NSScreen mainScreen] frame].size.height;

    width = resolutionWidth * 0.75;
    height = resolutionHeight * 0.75;
    window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
                                         styleMask:NSTitledWindowMask
                                           backing:NSBackingStoreBuffered defer:NO];
    [window cascadeTopLeftFromPoint:NSMakePoint(10, 10)];

    // add window buttons
    closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSTitledWindowMask];


    // set metadata for the window
    [window setTitle:appName];
    [window makeKeyAndOrderFront:nil];
}

- (void)showMainViewController
{
    // Set app settings
    [NSApplication sharedApplication];
    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    [NSApp activateIgnoringOtherApps:YES];
    [NSApp run];
}

@end

I've looked around and I'm kind of at a loss for how to proceed.

Was it helpful?

Solution

You might try changing your NSWindow init method to the following:

window = [[NSWindow alloc]
             initWithContentRect:NSMakeRect(0, 0, width, height)
                       styleMask:NSTitledWindowMask | NSClosableWindowMask |
                        NSMiniaturizableWindowMask | NSResizableWindowMask

                         backing:NSBackingStoreBuffered defer:NO];

I believe that ORing in the additional masks automatically adds the corresponding buttons to the title bar for you: NSClosableWindowMask adds the close button, NSMiniaturizableWindowMask adds the minimize (center) button, and NSResizableWindowMask adds the zoom (rightmost) button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top