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 - (void)windowWillClose: (NSNotification *)notification;
46 @end //interface wxNSWindowDelegate
48 @implementation wxNSWindowDelegate : NSObject
50 - (void)windowDidBecomeKey: (NSNotification *)notification
52 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
53 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
54 win->CocoaDelegate_windowDidBecomeKey();
57 - (void)windowDidResignKey: (NSNotification *)notification
59 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
60 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
61 win->CocoaDelegate_windowDidResignKey();
64 - (void)windowDidBecomeMain: (NSNotification *)notification
66 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
67 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
68 win->CocoaDelegate_windowDidBecomeMain();
71 - (void)windowDidResignMain: (NSNotification *)notification
73 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
74 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
75 win->CocoaDelegate_windowDidResignMain();
78 - (BOOL)windowShouldClose: (id)sender
80 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
81 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
82 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
84 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
87 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
91 - (void)windowWillClose: (NSNotification *)notification
93 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
94 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
95 win->CocoaDelegate_windowWillClose();
98 @end //implementation wxNSWindowDelegate
100 // ============================================================================
101 // class wxCocoaNSWindow
102 // ============================================================================
104 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
106 struct objc_object *wxCocoaNSWindow::sm_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
108 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
112 [cocoaNSWindow setReleasedWhenClosed: NO];
113 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
114 [cocoaNSWindow setDelegate: sm_cocoaDelegate];
118 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
122 [cocoaNSWindow setDelegate: nil];
123 sm_cocoaHash.erase(cocoaNSWindow);
127 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
132 // ============================================================================
133 // @class wxPoserNSWindow
134 // ============================================================================
135 @interface wxPoserNSWindow : NSWindow
139 - (BOOL)canBecomeMainWindow;
140 @end // wxPoserNSwindow
142 WX_IMPLEMENT_POSER(wxPoserNSWindow);
143 @implementation wxPoserNSWindow : NSWindow
145 - (BOOL)canBecomeMainWindow
147 bool canBecome = false;
148 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
149 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
150 canBecome = [super canBecomeMainWindow];
154 @end // implementation wxPoserNSWindow