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