1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
23 #include "wx/menuitem.h"
26 #include "wx/cocoa/NSWindow.h"
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30 #include "wx/cocoa/objc/NSWindow.h"
32 // ============================================================================
33 // @class wxNSWindowDelegate
34 // ============================================================================
35 @interface wxNSWindowDelegate : NSObject
37 wxCocoaNSWindow *m_wxCocoaInterface;
41 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
42 - (wxCocoaNSWindow *)wxCocoaInterface;
44 // Delegate message handlers
45 - (void)windowDidBecomeKey: (NSNotification *)notification;
46 - (void)windowDidResignKey: (NSNotification *)notification;
47 - (void)windowDidBecomeMain: (NSNotification *)notification;
48 - (void)windowDidResignMain: (NSNotification *)notification;
49 - (BOOL)windowShouldClose: (id)sender;
50 - (void)windowWillClose: (NSNotification *)notification;
53 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
54 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
55 @end //interface wxNSWindowDelegate
57 @implementation wxNSWindowDelegate : NSObject
61 m_wxCocoaInterface = NULL;
65 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
67 m_wxCocoaInterface = wxCocoaInterface;
70 - (wxCocoaNSWindow *)wxCocoaInterface
72 return m_wxCocoaInterface;
75 // Delegate message handlers
76 - (void)windowDidBecomeKey: (NSNotification *)notification
78 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
79 wxASSERT(win==m_wxCocoaInterface);
80 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
81 win->CocoaDelegate_windowDidBecomeKey();
84 - (void)windowDidResignKey: (NSNotification *)notification
86 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
87 wxASSERT(win==m_wxCocoaInterface);
88 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
89 win->CocoaDelegate_windowDidResignKey();
92 - (void)windowDidBecomeMain: (NSNotification *)notification
94 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
95 wxASSERT(win==m_wxCocoaInterface);
96 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
97 win->CocoaDelegate_windowDidBecomeMain();
100 - (void)windowDidResignMain: (NSNotification *)notification
102 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
103 wxASSERT(win==m_wxCocoaInterface);
104 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
105 win->CocoaDelegate_windowDidResignMain();
108 - (BOOL)windowShouldClose: (id)sender
110 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
111 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
112 wxASSERT(tlw==m_wxCocoaInterface);
113 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
115 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
118 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
122 - (void)windowWillClose: (NSNotification *)notification
124 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
125 wxASSERT(win==m_wxCocoaInterface);
126 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
127 win->CocoaDelegate_windowWillClose();
130 // Menu item handlers
131 - (void)wxMenuItemAction: (NSMenuItem *)sender
133 wxASSERT(m_wxCocoaInterface);
134 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
137 - (BOOL)validateMenuItem: (NSMenuItem *)sender
139 wxASSERT(m_wxCocoaInterface);
140 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
143 @end //implementation wxNSWindowDelegate
145 // ============================================================================
146 // class wxCocoaNSWindow
147 // ============================================================================
149 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
151 wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
152 : m_wxTopLevelWindowCocoa(tlw)
154 m_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
155 [m_cocoaDelegate setWxCocoaInterface: this];
158 wxCocoaNSWindow::~wxCocoaNSWindow()
160 [m_cocoaDelegate setWxCocoaInterface: NULL];
161 [m_cocoaDelegate release];
164 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
168 [cocoaNSWindow setReleasedWhenClosed: NO];
169 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
170 [cocoaNSWindow setDelegate: m_cocoaDelegate];
174 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
178 [cocoaNSWindow setDelegate: nil];
179 sm_cocoaHash.erase(cocoaNSWindow);
183 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
188 // ============================================================================
190 // ============================================================================
191 @implementation WXNSWindow : NSWindow
193 - (BOOL)canBecomeKeyWindow
195 bool canBecome = false;
196 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
197 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
198 canBecome = [super canBecomeKeyWindow];
202 - (BOOL)canBecomeMainWindow
204 bool canBecome = false;
205 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
206 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
207 canBecome = [super canBecomeMainWindow];
211 @end // implementation WXNSWindow
213 // ============================================================================
215 // ============================================================================
216 @implementation WXNSPanel : NSPanel
218 - (BOOL)canBecomeKeyWindow
220 bool canBecome = false;
221 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
222 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
223 canBecome = [super canBecomeKeyWindow];
227 - (BOOL)canBecomeMainWindow
229 bool canBecome = false;
230 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
231 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
232 canBecome = [super canBecomeMainWindow];
236 @end // implementation WXNSPanel