doptimusprime
doptimusprime

Reputation: 9415

Message boxes while clicking the cancel button

I tried my first Hello World GUI application in Cocoa without using Interface Builder (as I need to do it in this way only). In this, I write following code.

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
 NSWindow * vWindow;     ///< Window under delegation.
 //BOOL       vQuit;       ///< Flag to inidicate wheather to quit or not.
}
@property (assign) IBOutlet NSWindow *window;
- (id) init;
- (void) CreateWindow;
@end

//Implementation.
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = vWindow;

-(id)init
{
     if(self = [super init]) {
       //my allocation if required.
      }

      return self;
}

- (void)dealloc
{
     [super dealloc];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
     // Insert code here to initialize your application
     [vWindow setTitle:@"Hello World Application"];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
    [self dealloc];
}

-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
     return 1;
}

/**
 This is called when the last window is closed.
 */
 -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
 {    
         int answer;

    answer  = NSRunAlertPanel(@"Quit Confirmation", @"Do you want it to close?", @"Yes", @"No", @"Cancel");
    if(NSAlertDefaultReturn == answer)
        return YES;

    //Recreate the Windows.
    [self CreateWindow];
    return NO;
 }


/**
 * It creates the Window and assign it to the current Window.
*/
-(void)CreateWindow
{
    //Adding the menu bar.
    id      menubar;        ///< Menu bar.

//Create new menubar.
menubar     = [[[NSMenu alloc] initWithTitle:@"Menu"] autorelease];

    //Add a menu item to the menubar.
    //[menubar addItem:menuitem];

    id      quitmenu;   ///< quit menu.

quitmenu    = [[[NSMenuItem alloc] initWithTitle:@"Quit"
                                          action:@selector(terminate) keyEquivalent:@"q"]
               autorelease];

[menubar addItem:quitmenu];


//Set the main menu
[NSApp setMainMenu:menubar];

//Add close menu.
    id      window;     ///< Window.

window  = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200)
                                       styleMask:NSResizableWindowMask|NSTitledWindowMask|NSClosableWindowMask
                                         backing:NSBackingStoreBuffered defer:NO] autorelease];

//where to mention my delegate.
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window setTitle:@"Hello World App"];
[window makeKeyAndOrderFront:nil];

//Let us print "Hello Mac OS X" on this Windows.
    NSString *    strmessage;

strmessage  = [[NSString alloc] initWithUTF8String:"Hello Mac OS X"];

[strmessage drawAtPoint:NSMakePoint(30, 30) withAttributes:nil];

//Let us add few control (in assignment 2)

[self setWindow:window];

//make this window a key window
[window makeKeyAndOrderFront:nil];
}
@end

int main(int argc, char ** argv)
{
    NSApplication * application;    ///< application.
    AppDelegate *   appdelegate;    ///< Application delegate.

  //setup new auto release pool.
  [NSAutoreleasePool new];

  //Instantiate the instance of application.
  application     = [NSApplication sharedApplication];

  //Set the delegate here.
  appdelegate     = [[AppDelegate alloc] init];
  [application setDelegate:appdelegate];

  //Create Window on delegate.
  [appdelegate CreateWindow];


  //Start of the message loop.
  [NSApp run];


  //Any cleanup after this.

  [appdelegate release];
  [application release];

  return 0;
}

I am facing following problem: 1. On clicking the close button, it shows a message box. On clicking Yes, it exits. However, on clicking No or Cancel, it shows the message box again and again unless I click Yes. 2. Nothing is shown on menu bar.

Kindly let me know what I am missing here. If there is any good link to learn Cocoa application without nib or xib file, please post. I also need to support other features such as Command-W and Command-Q behaviour.

Upvotes: 0

Views: 243

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Once you have clicked on "x" button, your app is sent a notification to get close.

After this you can not roll back to not to close.

So this is the problem here.

However you can do this kind of work quite comfortably with document based application.

There must be something but that will be considered as hack to stop the notification sent to application and OS

Upvotes: 1

Related Questions