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 wxNSWindowDelegate
35 // ============================================================================
36 @interface wxNSWindowDelegate : NSObject
40 - (void)windowDidBecomeKey: (NSNotification *)notification;
41 - (void)windowDidResignKey: (NSNotification *)notification;
42 - (void)windowDidBecomeMain: (NSNotification *)notification;
43 - (void)windowDidResignMain: (NSNotification *)notification;
44 - (BOOL)windowShouldClose: (id)sender;
45 @end //interface wxNSWindowDelegate
47 @implementation wxNSWindowDelegate : NSObject
49 - (void)windowDidBecomeKey: (NSNotification *)notification
51 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
52 wxCHECK_RET(win,"notificationDidBecomeKey received but no wxWindow exists");
53 win->CocoaDelegate_windowDidBecomeKey();
56 - (void)windowDidResignKey: (NSNotification *)notification
58 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
59 wxCHECK_RET(win,"notificationDidResignKey received but no wxWindow exists");
60 win->CocoaDelegate_windowDidResignKey();
63 - (void)windowDidBecomeMain: (NSNotification *)notification
65 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
66 wxCHECK_RET(win,"notificationDidBecomeMain received but no wxWindow exists");
67 win->CocoaDelegate_windowDidBecomeMain();
70 - (void)windowDidResignMain: (NSNotification *)notification
72 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
73 wxCHECK_RET(win,"notificationDidResignMain received but no wxWindow exists");
74 win->CocoaDelegate_windowDidResignMain();
77 - (BOOL)windowShouldClose: (id)sender
79 wxLogDebug("windowShouldClose");
80 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
81 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
83 wxLogDebug("Window will not be closed");
86 wxLogDebug("Window will be closed");
90 @end //implementation wxNSWindowDelegate
92 // ============================================================================
93 // class wxCocoaNSWindow
94 // ============================================================================
96 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
98 struct objc_object *wxCocoaNSWindow::sm_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
100 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
104 [cocoaNSWindow setReleasedWhenClosed: NO];
105 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
106 [cocoaNSWindow setDelegate: sm_cocoaDelegate];
110 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
114 [cocoaNSWindow setDelegate: nil];
115 sm_cocoaHash.erase(cocoaNSWindow);
119 // ============================================================================
120 // @class wxPoserNSWindow
121 // ============================================================================
122 @interface wxPoserNSWindow : NSWindow
127 - (BOOL)canBecomeMainWindow;
128 @end // wxPoserNSwindow
130 WX_IMPLEMENT_POSER(wxPoserNSWindow);
131 @implementation wxPoserNSWindow : NSWindow
136 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
142 - (BOOL)canBecomeMainWindow
144 bool canBecome = false;
145 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
146 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
147 canBecome = [super canBecomeMainWindow];
151 @end // implementation wxPoserNSWindow