Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / September 2005



Tip: Looking for answers? Try searching our database.

how do I get notifications?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Santa Claus - 25 Sep 2005 06:12 GMT
I need to get notifications for 2 events for a NSwindow.
How do I set up the notification manager to get "application became active" and "window was resized"?

the source for the window declaration is as follows:

-(void)initReqsWindow;
{
 reqswindow = [[NSWindow alloc] initWithContentRect: reqswindowlocation styleMask: (NSTitledWindowMask | NSResizableWindowMask)  backing: NSBackingStoreBuffered defer: NO screen: [myScreens objectAtIndex: reqswindowscreen]];
 [reqswindow setTitle: @"Reqs"];
 reqsdata = [[NSTextView alloc] initWithFrame: NSMakeRect(20, 20, reqswindowlocation.size.width-40, reqswindowlocation.size.height-40)];

 NSView *reqswindowview  = [reqswindow contentView];
 [reqswindowview addSubview: reqsdata];
 [reqsdata setString: @"Reqs data here!"];
 [reqsdata setSelectable: YES];
 [reqsdata setEditable: NO];
 [reqsdata setRichText: NO];
 [reqsdata setBackgroundColor: [NSColor whiteColor]];
 [reqsdata setTextColor: [NSColor blackColor]];
 [reqsdata setFont: [NSFont userFixedPitchFontOfSize: windowFontSize]];
 [reqsdata sizeToFit];
 
 [reqswindow setViewsNeedDisplay: YES];
 [reqswindow makeKeyAndOrderFront: self];
 [reqswindow display];
 [reqswindowview display];
 [reqsdata display];
 [openAndCloseReqsWindowItem setState: NSOnState];
 
};
Allen Brunson - 25 Sep 2005 06:38 GMT
> I need to get notifications for 2 events for a NSwindow.
> How do I set up the notification manager to get "application became active"
> and "window was resized"?

you don't really "set up the notification manager."  here's a method i added
to a view that gets the key status change notifications:

-(void)observeWindowKeyChangeStart
 {
   SEL                    csel = @selector(windowKeyChange:);
   NSNotificationCenter*  nsnc = [NSNotificationCenter defaultCenter];
   NSWindow*              wind = [self window];

   [nsnc addObserver:self selector:csel
    name:NSWindowDidBecomeKeyNotification object:wind];

   [nsnc addObserver:self selector:csel
    name:NSWindowDidResignKeyNotification object:wind];
 }

now this view's windowKeyChange: method will get called every time the window
changes key status.

it's important to stop observing before the object is deallocated, or you're
setting yourself up for a crash later, and it will be very difficult to debug,
because it would seem to come out of nowhere.  so do this before destruction
time:

-(void)observeWindowKeyChangeStop
 {
   NSNotificationCenter*  nsnc = [NSNotificationCenter defaultCenter];
   NSWindow*              wind = [self window];

   [nsnc removeObserver:self
    name:NSWindowDidBecomeKeyNotification object:wind];

   [nsnc removeObserver:self
    name:NSWindowDidResignKeyNotification object:wind];
 }

you can change this to fit your scenario by changing the names of the
notifications.  you'd want NSWindowDidResizeNotification and
NSApplicationDidBecomeActiveNotification.  for that second one, you wouldn't
need to use anything for the "object:" parameter, you could leave it nil.
Santa Claus - 25 Sep 2005 19:21 GMT
> > I need to get notifications for 2 events for a NSwindow.
> > How do I set up the notification manager to get "application became active"
[quoted text clipped - 40 lines]
> NSApplicationDidBecomeActiveNotification.  for that second one, you wouldn't
> need to use anything for the "object:" parameter, you could leave it nil.

it's still not working...

in initialize routine I have:
   myNotificationCenter = [NSNotificationCenter defaultCenter];

the routines are:
-(void)initReqsWindow;
{
 reqswindow = [[NSWindow alloc] initWithContentRect: reqswindowlocation styleMask: (NSTitledWindowMask | NSResizableWindowMask)  backing: NSBackingStoreBuffered defer: NO screen: [myScreens objectAtIndex: reqswindowscreen]];
 [reqswindow setTitle: @"Reqs"];
 reqsdata = [[NSTextView alloc] initWithFrame: NSMakeRect(20, 20, reqswindowlocation.size.width-40, reqswindowlocation.size.height-40)];

 NSView *reqswindowview  = [reqswindow contentView];
 [reqswindowview addSubview: reqsdata];
 [reqsdata setString: @"Reqs data here!"];
 [reqsdata setSelectable: YES];
 [reqsdata setEditable: NO];
 [reqsdata setRichText: NO];
 [reqsdata setBackgroundColor: [NSColor whiteColor]];
 [reqsdata setTextColor: [NSColor blackColor]];
 [reqsdata setFont: [NSFont userFixedPitchFontOfSize: windowFontSize]];
 [reqsdata sizeToFit];
 
 [reqswindow setViewsNeedDisplay: YES];
 [reqswindow makeKeyAndOrderFront: self];
 [reqswindow display];
 [reqswindowview display];
 [reqsdata display];
 [openAndCloseReqsWindowItem setState: NSOnState];
 [myNotificationCenter addObserver: self selector: @selector(activateReqsWindow:) name: NSApplicationWillBecomeActiveNotification object: nil];
 [myNotificationCenter addObserver: self selector: @selector(activateReqsWindow:) name: NSWindowDidResizeNotification object: reqswindow];
 
};

-(void)activateReqsWindow: (id) myobject;
{
 [reqsdata setFrame: NSMakeRect(20, 20, reqswindowlocation.size.width-40, reqswindowlocation.size.height-40)];
 [reqswindow display];
 [reqsdata display];
};

-(void)openAndCloseReqsWindow: (id) menu;
{
 NSMenuItem *myLocalMenuItem = menu;
 if (reqswindowvisible)
   {
   reqswindowvisible = NO;
   reqswindowlocation = [reqswindow frame];
   reqswindowlocation.size.height = reqswindowlocation.size.height-22;
   [myNotificationCenter removeObserver: self name: NSApplicationWillBecomeActiveNotification object: reqswindow];
   [myNotificationCenter removeObserver: self name: NSWindowDidResizeNotification object: reqswindow];
   [reqswindow close];
   }
 else
   {
   reqswindowvisible = YES;
   [self initReqsWindow];
   }
 if (reqswindowvisible)
   {
   [myLocalMenuItem setState: NSOnState];
   }
 else
   {
   [myLocalMenuItem setState: NSOffState];
   }
 [self saveprefs];
};
Allen Brunson - 26 Sep 2005 02:37 GMT
> it's still not working...

i don't see anything obviously wrong in the code, except that there's no need
to cache a pointer to the default NSNotificationCenter.  global and instance
variables are just one more thing to keep track of, and any that can be
eliminated, should be.

also, this is wrong:

[myNotificationCenter removeObserver: self name:
 NSApplicationWillBecomeActiveNotification object: reqswindow];

because you used 'nil' for "object:" when you were setting up the call.
that's an unexplained crash waiting to happen.

could be a problem in something you're not showing.  myNotificationCenter
might not be getting set before it's used, or you're clobbering its value
somewhere.  yet another reason not to cache a pointer to it.
Santa Claus - 26 Sep 2005 11:37 GMT
> > it's still not working...
>
[quoted text clipped - 14 lines]
> might not be getting set before it's used, or you're clobbering its value
> somewhere.  yet another reason not to cache a pointer to it.

you're suggesting it can move all of it's own?
google@whooley.utvinternet.com - 26 Sep 2005 15:45 GMT
Wouldn't the easiest way to do this be via delegate methods?

In your application's delegate object, implement the
applicationDidBecomeActive: method. In the window's delegate, implement
windowDidResize:.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.