]>
Commit | Line | Data |
---|---|---|
fb896a32 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1c4e8f38 | 2 | // Name: src/cocoa/NSWindow.mm |
fb896a32 DE |
3 | // Purpose: wxCocoaNSWindow |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2003/03/16 | |
1c4e8f38 | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2003 David Elliott |
065e208e | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
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 | ||
fb45bb1f | 26 | #include "wx/cocoa/ObjcPose.h" |
fb896a32 DE |
27 | #include "wx/cocoa/NSWindow.h" |
28 | ||
720e01c3 DE |
29 | #import <Foundation/NSNotification.h> |
30 | #import <Foundation/NSString.h> | |
ad3628fa | 31 | #include "wx/cocoa/objc/NSWindow.h" |
720e01c3 DE |
32 | |
33 | // ============================================================================ | |
78c67cb6 | 34 | // @class wxNSWindowDelegate |
720e01c3 | 35 | // ============================================================================ |
78c67cb6 | 36 | @interface wxNSWindowDelegate : NSObject |
720e01c3 | 37 | { |
c6f73d05 | 38 | wxCocoaNSWindow *m_wxCocoaInterface; |
720e01c3 DE |
39 | } |
40 | ||
c6f73d05 DE |
41 | - (id)init; |
42 | - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface; | |
43 | - (wxCocoaNSWindow *)wxCocoaInterface; | |
44 | ||
45 | // Delegate message handlers | |
78c67cb6 DE |
46 | - (void)windowDidBecomeKey: (NSNotification *)notification; |
47 | - (void)windowDidResignKey: (NSNotification *)notification; | |
8fc821cc DE |
48 | - (void)windowDidBecomeMain: (NSNotification *)notification; |
49 | - (void)windowDidResignMain: (NSNotification *)notification; | |
eb3426e7 | 50 | - (BOOL)windowShouldClose: (id)sender; |
9692f42b | 51 | - (void)windowWillClose: (NSNotification *)notification; |
86adc758 DE |
52 | |
53 | // Menu item handlers | |
7dd8b1ea DE |
54 | - (void)wxMenuItemAction: (NSMenuItem *)menuItem; |
55 | - (BOOL)validateMenuItem: (NSMenuItem *)menuItem; | |
78c67cb6 | 56 | @end //interface wxNSWindowDelegate |
720e01c3 | 57 | |
78c67cb6 | 58 | @implementation wxNSWindowDelegate : NSObject |
720e01c3 | 59 | |
c6f73d05 DE |
60 | - (id)init |
61 | { | |
62 | m_wxCocoaInterface = NULL; | |
63 | return [super init]; | |
64 | } | |
65 | ||
66 | - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface | |
67 | { | |
68 | m_wxCocoaInterface = wxCocoaInterface; | |
69 | } | |
70 | ||
71 | - (wxCocoaNSWindow *)wxCocoaInterface | |
72 | { | |
73 | return m_wxCocoaInterface; | |
74 | } | |
75 | ||
86adc758 | 76 | // Delegate message handlers |
78c67cb6 | 77 | - (void)windowDidBecomeKey: (NSNotification *)notification |
720e01c3 DE |
78 | { |
79 | wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); | |
c6f73d05 | 80 | wxASSERT(win==m_wxCocoaInterface); |
2b030203 | 81 | wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists")); |
aa992c59 | 82 | win->CocoaDelegate_windowDidBecomeKey(); |
720e01c3 DE |
83 | } |
84 | ||
78c67cb6 | 85 | - (void)windowDidResignKey: (NSNotification *)notification |
720e01c3 DE |
86 | { |
87 | wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); | |
c6f73d05 | 88 | wxASSERT(win==m_wxCocoaInterface); |
2b030203 | 89 | wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists")); |
aa992c59 | 90 | win->CocoaDelegate_windowDidResignKey(); |
720e01c3 DE |
91 | } |
92 | ||
8fc821cc DE |
93 | - (void)windowDidBecomeMain: (NSNotification *)notification |
94 | { | |
95 | wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); | |
c6f73d05 | 96 | wxASSERT(win==m_wxCocoaInterface); |
2b030203 | 97 | wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists")); |
8fc821cc DE |
98 | win->CocoaDelegate_windowDidBecomeMain(); |
99 | } | |
100 | ||
101 | - (void)windowDidResignMain: (NSNotification *)notification | |
102 | { | |
103 | wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); | |
c6f73d05 | 104 | wxASSERT(win==m_wxCocoaInterface); |
2b030203 | 105 | wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists")); |
8fc821cc DE |
106 | win->CocoaDelegate_windowDidResignMain(); |
107 | } | |
108 | ||
eb3426e7 DE |
109 | - (BOOL)windowShouldClose: (id)sender |
110 | { | |
48580976 | 111 | wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose")); |
eb3426e7 | 112 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender); |
c6f73d05 | 113 | wxASSERT(tlw==m_wxCocoaInterface); |
aa992c59 | 114 | if(tlw && !tlw->CocoaDelegate_windowShouldClose()) |
eb3426e7 | 115 | { |
48580976 | 116 | wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed")); |
eb3426e7 DE |
117 | return NO; |
118 | } | |
48580976 | 119 | wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed")); |
eb3426e7 DE |
120 | return YES; |
121 | } | |
122 | ||
9692f42b DE |
123 | - (void)windowWillClose: (NSNotification *)notification |
124 | { | |
125 | wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); | |
c6f73d05 | 126 | wxASSERT(win==m_wxCocoaInterface); |
2b030203 | 127 | wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists")); |
9692f42b DE |
128 | win->CocoaDelegate_windowWillClose(); |
129 | } | |
130 | ||
86adc758 | 131 | // Menu item handlers |
7dd8b1ea | 132 | - (void)wxMenuItemAction: (NSMenuItem *)sender |
86adc758 DE |
133 | { |
134 | wxASSERT(m_wxCocoaInterface); | |
135 | m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender); | |
136 | } | |
137 | ||
7dd8b1ea | 138 | - (BOOL)validateMenuItem: (NSMenuItem *)sender |
86adc758 DE |
139 | { |
140 | wxASSERT(m_wxCocoaInterface); | |
141 | return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender); | |
142 | } | |
143 | ||
78c67cb6 | 144 | @end //implementation wxNSWindowDelegate |
720e01c3 DE |
145 | |
146 | // ============================================================================ | |
147 | // class wxCocoaNSWindow | |
148 | // ============================================================================ | |
fb896a32 | 149 | |
fb896a32 DE |
150 | WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow) |
151 | ||
0092ef0d DE |
152 | wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw) |
153 | : m_wxTopLevelWindowCocoa(tlw) | |
c6f73d05 DE |
154 | { |
155 | m_cocoaDelegate = [[wxNSWindowDelegate alloc] init]; | |
156 | [m_cocoaDelegate setWxCocoaInterface: this]; | |
157 | } | |
158 | ||
159 | wxCocoaNSWindow::~wxCocoaNSWindow() | |
160 | { | |
161 | [m_cocoaDelegate setWxCocoaInterface: NULL]; | |
162 | [m_cocoaDelegate release]; | |
163 | } | |
720e01c3 | 164 | |
fb896a32 DE |
165 | void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow) |
166 | { | |
bac6f234 DE |
167 | if(cocoaNSWindow) |
168 | { | |
169 | [cocoaNSWindow setReleasedWhenClosed: NO]; | |
170 | sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this)); | |
c6f73d05 | 171 | [cocoaNSWindow setDelegate: m_cocoaDelegate]; |
720e01c3 DE |
172 | } |
173 | } | |
174 | ||
175 | void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow) | |
176 | { | |
177 | if(cocoaNSWindow) | |
178 | { | |
78c67cb6 | 179 | [cocoaNSWindow setDelegate: nil]; |
720e01c3 | 180 | sm_cocoaHash.erase(cocoaNSWindow); |
bac6f234 | 181 | } |
fb896a32 DE |
182 | } |
183 | ||
8ded703d | 184 | wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win) |
360be3c0 DE |
185 | { |
186 | return NULL; | |
187 | } | |
188 | ||
fb896a32 | 189 | // ============================================================================ |
829a2e95 | 190 | // @class WXNSWindow |
fb896a32 | 191 | // ============================================================================ |
829a2e95 | 192 | @implementation WXNSWindow : NSWindow |
fb896a32 | 193 | |
3d012d1b DE |
194 | - (BOOL)canBecomeKeyWindow |
195 | { | |
196 | bool canBecome = false; | |
197 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self); | |
198 | if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome)) | |
199 | canBecome = [super canBecomeKeyWindow]; | |
200 | return canBecome; | |
201 | } | |
202 | ||
8fc821cc DE |
203 | - (BOOL)canBecomeMainWindow |
204 | { | |
205 | bool canBecome = false; | |
206 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self); | |
207 | if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome)) | |
208 | canBecome = [super canBecomeMainWindow]; | |
209 | return canBecome; | |
210 | } | |
211 | ||
829a2e95 DE |
212 | @end // implementation WXNSWindow |
213 | ||
214 | // ============================================================================ | |
215 | // @class WXNSPanel | |
216 | // ============================================================================ | |
217 | @implementation WXNSPanel : NSPanel | |
218 | ||
219 | - (BOOL)canBecomeKeyWindow | |
220 | { | |
221 | bool canBecome = false; | |
222 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self); | |
223 | if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome)) | |
224 | canBecome = [super canBecomeKeyWindow]; | |
225 | return canBecome; | |
226 | } | |
227 | ||
228 | - (BOOL)canBecomeMainWindow | |
229 | { | |
230 | bool canBecome = false; | |
231 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self); | |
232 | if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome)) | |
233 | canBecome = [super canBecomeMainWindow]; | |
234 | return canBecome; | |
235 | } | |
236 | ||
237 | @end // implementation WXNSPanel |