1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
23 #include "wx/menuitem.h"
26 #include "wx/cocoa/ObjcPose.h"
27 #include "wx/cocoa/NSWindow.h"
29 #import <Appkit/NSWindow.h>
30 #import <Foundation/NSNotification.h>
31 #import <Foundation/NSString.h>
33 // ============================================================================
34 // @class wxNSWindowNotificationObserver
35 // ============================================================================
36 @interface wxNSWindowNotificationObserver : NSObject
40 - (void)notificationDidBecomeKey: (NSNotification *)notification;
41 - (void)notificationDidResignKey: (NSNotification *)notification;
42 @end //interface wxNSWindowNotificationObserver
44 @implementation wxNSWindowNotificationObserver : NSObject
46 - (void)notificationDidBecomeKey: (NSNotification *)notification
48 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
49 wxCHECK_RET(win,"notificationDidBecomeKey received but no wxWindow exists");
50 win->CocoaNotification_DidBecomeKey();
53 - (void)notificationDidResignKey: (NSNotification *)notification
55 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
56 wxCHECK_RET(win,"notificationDidResignKey received but no wxWindow exists");
57 win->CocoaNotification_DidResignKey();
60 @end //implementation wxNSWindowNotificationObserver
62 // ============================================================================
63 // class wxCocoaNSWindow
64 // ============================================================================
66 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
68 void *wxCocoaNSWindow::sm_cocoaObserver = [[wxNSWindowNotificationObserver alloc] init];
70 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
74 [cocoaNSWindow setReleasedWhenClosed: NO];
75 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
76 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationDidBecomeKey:) name:@"NSWindowDidBecomeKeyNotification" object:cocoaNSWindow];
77 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationDidResignKey:) name:@"NSWindowDidResignKeyNotification" object:cocoaNSWindow];
81 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
85 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSWindowDidBecomeKeyNotification" object:cocoaNSWindow];
86 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSWindowDidResignKeyNotification" object:cocoaNSWindow];
87 sm_cocoaHash.erase(cocoaNSWindow);
91 // ============================================================================
92 // @class wxPoserNSWindow
93 // ============================================================================
94 @interface wxPoserNSWindow : NSWindow
99 - (BOOL)windowShouldClose: (id)sender;
101 - (BOOL)wxMenuItemAction: (id)sender;
102 @end // wxPoserNSwindow
104 WX_IMPLEMENT_POSER(wxPoserNSWindow);
105 @implementation wxPoserNSWindow : NSWindow
110 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
116 - (BOOL)windowShouldClose: (id)sender
118 wxLogDebug("windowShouldClose");
119 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
120 if(tlw && !tlw->Cocoa_windowShouldClose())
122 wxLogDebug("Window will most likely be CLOSED");
123 if([[wxPoserNSWindow superclass] instancesRespondToSelector:@selector(windowShouldClose:)])
124 return [super windowShouldClose: sender];
128 - (BOOL)wxMenuItemAction: (id)sender
130 wxLogDebug("wxMenuItemAction");
131 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
135 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
137 tlw->Cocoa_wxMenuItemAction(*item);
140 @end // implementation wxPoserNSWindow