1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
22 #include "wx/menuitem.h"
25 #include "wx/cocoa/NSWindow.h"
27 #include "wx/cocoa/objc/objc_uniquifying.h"
29 #import <Foundation/NSNotification.h>
30 #import <Foundation/NSString.h>
31 #include "wx/cocoa/objc/NSWindow.h"
33 // ============================================================================
34 // @class wxNSWindowDelegate
35 // ============================================================================
36 @interface wxNSWindowDelegate : NSObject
38 wxCocoaNSWindow *m_wxCocoaInterface;
42 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
43 - (wxCocoaNSWindow *)wxCocoaInterface;
45 // Delegate message handlers
46 - (void)windowDidBecomeKey: (NSNotification *)notification;
47 - (void)windowDidResignKey: (NSNotification *)notification;
48 - (void)windowDidBecomeMain: (NSNotification *)notification;
49 - (void)windowDidResignMain: (NSNotification *)notification;
50 - (BOOL)windowShouldClose: (id)sender;
51 - (void)windowWillClose: (NSNotification *)notification;
54 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
55 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
56 @end //interface wxNSWindowDelegate
57 WX_DECLARE_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
59 @implementation wxNSWindowDelegate : NSObject
63 m_wxCocoaInterface = NULL;
67 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
69 m_wxCocoaInterface = wxCocoaInterface;
72 - (wxCocoaNSWindow *)wxCocoaInterface
74 return m_wxCocoaInterface;
77 // Delegate message handlers
78 - (void)windowDidBecomeKey: (NSNotification *)notification
80 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
81 wxASSERT(win==m_wxCocoaInterface);
82 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
83 win->CocoaDelegate_windowDidBecomeKey();
86 - (void)windowDidResignKey: (NSNotification *)notification
88 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
89 wxASSERT(win==m_wxCocoaInterface);
90 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
91 win->CocoaDelegate_windowDidResignKey();
94 - (void)windowDidBecomeMain: (NSNotification *)notification
96 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
97 wxASSERT(win==m_wxCocoaInterface);
98 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
99 win->CocoaDelegate_windowDidBecomeMain();
102 - (void)windowDidResignMain: (NSNotification *)notification
104 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
105 wxASSERT(win==m_wxCocoaInterface);
106 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
107 win->CocoaDelegate_windowDidResignMain();
110 - (BOOL)windowShouldClose: (id)sender
112 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
113 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
114 wxASSERT(tlw==m_wxCocoaInterface);
115 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
117 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
120 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
124 - (void)windowWillClose: (NSNotification *)notification
126 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
127 wxASSERT(win==m_wxCocoaInterface);
128 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
129 win->CocoaDelegate_windowWillClose();
132 // Menu item handlers
133 - (void)wxMenuItemAction: (NSMenuItem *)sender
135 wxASSERT(m_wxCocoaInterface);
136 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
139 - (BOOL)validateMenuItem: (NSMenuItem *)sender
141 wxASSERT(m_wxCocoaInterface);
142 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
145 @end //implementation wxNSWindowDelegate
146 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
148 // ============================================================================
149 // class wxCocoaNSWindow
150 // ============================================================================
152 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
154 wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
155 : m_wxTopLevelWindowCocoa(tlw)
157 m_cocoaDelegate = [[WX_GET_OBJC_CLASS(wxNSWindowDelegate) alloc] init];
158 [m_cocoaDelegate setWxCocoaInterface: this];
161 wxCocoaNSWindow::~wxCocoaNSWindow()
163 [m_cocoaDelegate setWxCocoaInterface: NULL];
164 [m_cocoaDelegate release];
167 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
171 [cocoaNSWindow setReleasedWhenClosed: NO];
172 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
173 [cocoaNSWindow setDelegate: m_cocoaDelegate];
177 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
181 [cocoaNSWindow setDelegate: nil];
182 sm_cocoaHash.erase(cocoaNSWindow);
186 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
191 // ============================================================================
193 // ============================================================================
194 @implementation WXNSWindow : NSWindow
196 - (BOOL)canBecomeKeyWindow
198 bool canBecome = false;
199 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
200 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
201 canBecome = [super canBecomeKeyWindow];
205 - (BOOL)canBecomeMainWindow
207 bool canBecome = false;
208 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
209 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
210 canBecome = [super canBecomeMainWindow];
214 @end // implementation WXNSWindow
215 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSWindow,NSWindow)
217 // ============================================================================
219 // ============================================================================
220 @implementation WXNSPanel : NSPanel
222 - (BOOL)canBecomeKeyWindow
224 bool canBecome = false;
225 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
226 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
227 canBecome = [super canBecomeKeyWindow];
231 - (BOOL)canBecomeMainWindow
233 bool canBecome = false;
234 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
235 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
236 canBecome = [super canBecomeMainWindow];
240 @end // implementation WXNSPanel
241 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSPanel,NSPanel)