]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSWindow.mm
9bd527fb46626720d9466c03f4e5dc409cd2df39
[wxWidgets.git] / src / cocoa / NSWindow.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/menuitem.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/cocoa/NSWindow.h"
27
28 #include "wx/cocoa/objc/objc_uniquifying.h"
29
30 #import <Foundation/NSNotification.h>
31 #import <Foundation/NSString.h>
32 #include "wx/cocoa/objc/NSWindow.h"
33
34 // ============================================================================
35 // @class wxNSWindowDelegate
36 // ============================================================================
37 @interface wxNSWindowDelegate : NSObject
38 {
39 wxCocoaNSWindow *m_wxCocoaInterface;
40 }
41
42 - (id)init;
43 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
44 - (wxCocoaNSWindow *)wxCocoaInterface;
45
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;
53
54 // Menu item handlers
55 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
56 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
57 @end //interface wxNSWindowDelegate
58 WX_DECLARE_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
59
60 @implementation wxNSWindowDelegate : NSObject
61
62 - (id)init
63 {
64 m_wxCocoaInterface = NULL;
65 return [super init];
66 }
67
68 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
69 {
70 m_wxCocoaInterface = wxCocoaInterface;
71 }
72
73 - (wxCocoaNSWindow *)wxCocoaInterface
74 {
75 return m_wxCocoaInterface;
76 }
77
78 // Delegate message handlers
79 - (void)windowDidBecomeKey: (NSNotification *)notification
80 {
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();
85 }
86
87 - (void)windowDidResignKey: (NSNotification *)notification
88 {
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();
93 }
94
95 - (void)windowDidBecomeMain: (NSNotification *)notification
96 {
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();
101 }
102
103 - (void)windowDidResignMain: (NSNotification *)notification
104 {
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();
109 }
110
111 - (BOOL)windowShouldClose: (id)sender
112 {
113 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
114 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
115 wxASSERT(tlw==m_wxCocoaInterface);
116 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
117 {
118 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
119 return NO;
120 }
121 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
122 return YES;
123 }
124
125 - (void)windowWillClose: (NSNotification *)notification
126 {
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();
131 }
132
133 // Menu item handlers
134 - (void)wxMenuItemAction: (NSMenuItem *)sender
135 {
136 wxASSERT(m_wxCocoaInterface);
137 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
138 }
139
140 - (BOOL)validateMenuItem: (NSMenuItem *)sender
141 {
142 wxASSERT(m_wxCocoaInterface);
143 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
144 }
145
146 @end //implementation wxNSWindowDelegate
147 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
148
149 // ============================================================================
150 // class wxCocoaNSWindow
151 // ============================================================================
152
153 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
154
155 wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
156 : m_wxTopLevelWindowCocoa(tlw)
157 {
158 m_cocoaDelegate = [[WX_GET_OBJC_CLASS(wxNSWindowDelegate) alloc] init];
159 [m_cocoaDelegate setWxCocoaInterface: this];
160 }
161
162 wxCocoaNSWindow::~wxCocoaNSWindow()
163 {
164 [m_cocoaDelegate setWxCocoaInterface: NULL];
165 [m_cocoaDelegate release];
166 }
167
168 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
169 {
170 if(cocoaNSWindow)
171 {
172 [cocoaNSWindow setReleasedWhenClosed: NO];
173 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
174 [cocoaNSWindow setDelegate: m_cocoaDelegate];
175 }
176 }
177
178 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
179 {
180 if(cocoaNSWindow)
181 {
182 [cocoaNSWindow setDelegate: nil];
183 sm_cocoaHash.erase(cocoaNSWindow);
184 }
185 }
186
187 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
188 {
189 return NULL;
190 }
191
192 // ============================================================================
193 // @class WXNSWindow
194 // ============================================================================
195 @implementation WXNSWindow : NSWindow
196
197 - (BOOL)canBecomeKeyWindow
198 {
199 bool canBecome = false;
200 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
201 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
202 canBecome = [super canBecomeKeyWindow];
203 return canBecome;
204 }
205
206 - (BOOL)canBecomeMainWindow
207 {
208 bool canBecome = false;
209 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
210 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
211 canBecome = [super canBecomeMainWindow];
212 return canBecome;
213 }
214
215 @end // implementation WXNSWindow
216 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSWindow,NSWindow)
217
218 // ============================================================================
219 // @class WXNSPanel
220 // ============================================================================
221 @implementation WXNSPanel : NSPanel
222
223 - (BOOL)canBecomeKeyWindow
224 {
225 bool canBecome = false;
226 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
227 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
228 canBecome = [super canBecomeKeyWindow];
229 return canBecome;
230 }
231
232 - (BOOL)canBecomeMainWindow
233 {
234 bool canBecome = false;
235 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
236 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
237 canBecome = [super canBecomeMainWindow];
238 return canBecome;
239 }
240
241 @end // implementation WXNSPanel
242 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSPanel,NSPanel)