1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
23 #include "wx/menuitem.h"
26 #include "wx/cocoa/NSWindow.h"
28 #include "wx/cocoa/objc/objc_uniquifying.h"
30 #import <Foundation/NSNotification.h>
31 #import <Foundation/NSString.h>
32 #include "wx/cocoa/objc/NSWindow.h"
34 // ============================================================================
35 // @class wxNSWindowDelegate
36 // ============================================================================
37 @interface wxNSWindowDelegate : NSObject
39 wxCocoaNSWindow *m_wxCocoaInterface;
43 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
44 - (wxCocoaNSWindow *)wxCocoaInterface;
46 // Delegate message handlers
47 - (void)windowDidBecomeKey: (NSNotification *)notification;
48 - (void)windowDidResignKey: (NSNotification *)notification;
49 - (void)windowDidBecomeMain: (NSNotification *)notification;
50 - (void)windowDidResignMain: (NSNotification *)notification;
51 - (BOOL)windowShouldClose: (id)sender;
52 - (void)windowWillClose: (NSNotification *)notification;
55 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
56 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
57 @end //interface wxNSWindowDelegate
58 WX_DECLARE_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
60 @implementation wxNSWindowDelegate : NSObject
64 m_wxCocoaInterface = NULL;
68 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
70 m_wxCocoaInterface = wxCocoaInterface;
73 - (wxCocoaNSWindow *)wxCocoaInterface
75 return m_wxCocoaInterface;
78 // Delegate message handlers
79 - (void)windowDidBecomeKey: (NSNotification *)notification
81 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
82 wxASSERT(win==m_wxCocoaInterface);
83 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
84 win->CocoaDelegate_windowDidBecomeKey();
87 - (void)windowDidResignKey: (NSNotification *)notification
89 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
90 wxASSERT(win==m_wxCocoaInterface);
91 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
92 win->CocoaDelegate_windowDidResignKey();
95 - (void)windowDidBecomeMain: (NSNotification *)notification
97 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
98 wxASSERT(win==m_wxCocoaInterface);
99 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
100 win->CocoaDelegate_windowDidBecomeMain();
103 - (void)windowDidResignMain: (NSNotification *)notification
105 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
106 wxASSERT(win==m_wxCocoaInterface);
107 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
108 win->CocoaDelegate_windowDidResignMain();
111 - (BOOL)windowShouldClose: (id)sender
113 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
114 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
115 wxASSERT(tlw==m_wxCocoaInterface);
116 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
118 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
121 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
125 - (void)windowWillClose: (NSNotification *)notification
127 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
128 wxASSERT(win==m_wxCocoaInterface);
129 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
130 win->CocoaDelegate_windowWillClose();
133 // Menu item handlers
134 - (void)wxMenuItemAction: (NSMenuItem *)sender
136 wxASSERT(m_wxCocoaInterface);
137 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
140 - (BOOL)validateMenuItem: (NSMenuItem *)sender
142 wxASSERT(m_wxCocoaInterface);
143 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
146 @end //implementation wxNSWindowDelegate
147 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
149 // ============================================================================
150 // class wxCocoaNSWindow
151 // ============================================================================
153 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
155 wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
156 : m_wxTopLevelWindowCocoa(tlw)
158 m_cocoaDelegate = [[WX_GET_OBJC_CLASS(wxNSWindowDelegate) alloc] init];
159 [m_cocoaDelegate setWxCocoaInterface: this];
162 wxCocoaNSWindow::~wxCocoaNSWindow()
164 [m_cocoaDelegate setWxCocoaInterface: NULL];
165 [m_cocoaDelegate release];
168 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
172 [cocoaNSWindow setReleasedWhenClosed: NO];
173 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
174 [cocoaNSWindow setDelegate: m_cocoaDelegate];
178 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
182 [cocoaNSWindow setDelegate: nil];
183 sm_cocoaHash.erase(cocoaNSWindow);
187 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
192 // ============================================================================
194 // ============================================================================
195 @implementation WXNSWindow : NSWindow
197 - (BOOL)canBecomeKeyWindow
199 bool canBecome = false;
200 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
201 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
202 canBecome = [super canBecomeKeyWindow];
206 - (BOOL)canBecomeMainWindow
208 bool canBecome = false;
209 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
210 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
211 canBecome = [super canBecomeMainWindow];
215 @end // implementation WXNSWindow
216 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSWindow,NSWindow)
218 // ============================================================================
220 // ============================================================================
221 @implementation WXNSPanel : NSPanel
223 - (BOOL)canBecomeKeyWindow
225 bool canBecome = false;
226 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
227 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
228 canBecome = [super canBecomeKeyWindow];
232 - (BOOL)canBecomeMainWindow
234 bool canBecome = false;
235 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
236 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
237 canBecome = [super canBecomeMainWindow];
241 @end // implementation WXNSPanel
242 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSPanel,NSPanel)