]>
Commit | Line | Data |
---|---|---|
fb896a32 | 1 | /////////////////////////////////////////////////////////////////////////////// |
8d8d3633 | 2 | // Name: src/cocoa/toplevel.mm |
fb896a32 | 3 | // Purpose: implements wxTopLevelWindow for Cocoa |
8d8d3633 | 4 | // Author: David Elliott |
fb896a32 DE |
5 | // Modified by: |
6 | // Created: 2002/11/27 | |
46cdffaf | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2002 David Elliott |
065e208e | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
1832043f WS |
22 | |
23 | #include "wx/toplevel.h" | |
24 | ||
fb896a32 DE |
25 | #ifndef WX_PRECOMP |
26 | #include "wx/window.h" | |
fb896a32 DE |
27 | #include "wx/menuitem.h" |
28 | #include "wx/frame.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/app.h" | |
31 | #endif //WX_PRECOMP | |
32 | ||
7fc77f30 | 33 | #include "wx/cocoa/autorelease.h" |
da026811 | 34 | #include "wx/cocoa/string.h" |
7fc77f30 | 35 | |
829a2e95 DE |
36 | #include "wx/cocoa/objc/NSView.h" |
37 | #include "wx/cocoa/objc/NSWindow.h" | |
080c7d56 | 38 | #import <AppKit/NSPanel.h> |
a24aa427 | 39 | |
fb896a32 DE |
40 | // ---------------------------------------------------------------------------- |
41 | // globals | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | // list of all frames and modeless dialogs | |
45 | wxWindowList wxModelessWindows; | |
46 | ||
47 | // ============================================================================ | |
48 | // wxTopLevelWindowCocoa implementation | |
49 | // ============================================================================ | |
50 | ||
39050120 DE |
51 | wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL; |
52 | ||
fb896a32 DE |
53 | // ---------------------------------------------------------------------------- |
54 | // wxTopLevelWindowCocoa creation | |
55 | // ---------------------------------------------------------------------------- | |
fb896a32 DE |
56 | BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase) |
57 | EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow) | |
58 | END_EVENT_TABLE() | |
59 | ||
60 | void wxTopLevelWindowCocoa::Init() | |
61 | { | |
62 | m_iconized = | |
63 | m_maximizeOnShow = | |
64 | m_closed = false; | |
65 | } | |
66 | ||
080c7d56 DE |
67 | unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style) |
68 | { | |
69 | unsigned int styleMask = 0; | |
70 | if(style & wxCAPTION) | |
71 | styleMask |= NSTitledWindowMask; | |
72 | if(style & wxMINIMIZE_BOX) | |
73 | styleMask |= NSMiniaturizableWindowMask; | |
74 | #if 0 | |
75 | if(style & wxMAXIMIZE_BOX) | |
76 | styleMask |= NSWindowMask; | |
77 | #endif | |
78 | if(style & wxCLOSE_BOX) | |
79 | styleMask |= NSClosableWindowMask; | |
80 | if(style & wxRESIZE_BORDER) | |
81 | styleMask |= NSResizableWindowMask; | |
82 | if(style & wxSIMPLE_BORDER) | |
83 | styleMask |= NSBorderlessWindowMask; | |
84 | return styleMask; | |
85 | } | |
86 | ||
fb896a32 DE |
87 | bool wxTopLevelWindowCocoa::Create(wxWindow *parent, |
88 | wxWindowID winid, | |
89 | const wxString& title, | |
90 | const wxPoint& pos, | |
91 | const wxSize& size, | |
92 | long style, | |
93 | const wxString& name) | |
94 | { | |
7fc77f30 | 95 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
96 | wxTopLevelWindows.Append(this); |
97 | ||
98 | if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name)) | |
8d8d3633 | 99 | return false; |
fb896a32 DE |
100 | |
101 | if ( parent ) | |
102 | parent->AddChild(this); | |
103 | ||
080c7d56 DE |
104 | unsigned int cocoaStyle = NSWindowStyleForWxStyle(style); |
105 | if(style & wxFRAME_TOOL_WINDOW) | |
106 | cocoaStyle |= NSUtilityWindowMask; | |
fb896a32 | 107 | |
d2518be4 RN |
108 | // Create frame and check and handle default position and size |
109 | int realx, | |
110 | realy; | |
8d8d3633 | 111 | |
d2518be4 RN |
112 | // WX has no set default position - the carbon port caps the low |
113 | // end at 20, 50. Here we do the same, except instead of setting | |
114 | // it to 20 and 50, we set it to 100 and 100 if the values are too low | |
115 | if (pos.x < 20) | |
116 | realx = 100; | |
117 | else | |
118 | realx = pos.x; | |
8d8d3633 | 119 | |
d2518be4 RN |
120 | if (pos.y < 50) |
121 | realy = 100; | |
122 | else | |
123 | realy = pos.y; | |
124 | ||
125 | int realw = WidthDefault(size.x); | |
126 | int realh = HeightDefault(size.y); | |
127 | ||
080c7d56 | 128 | // NOTE: y-origin needs to be flipped. |
d2518be4 | 129 | NSRect cocoaRect = [NSWindow |
8d8d3633 | 130 | contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh) |
d2518be4 | 131 | styleMask:cocoaStyle]; |
fb896a32 DE |
132 | |
133 | m_cocoaNSWindow = NULL; | |
134 | m_cocoaNSView = NULL; | |
829a2e95 DE |
135 | // NOTE: We may need to deal with the contentView becoming a wx NSView as well. |
136 | NSWindow *newWindow; | |
137 | // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired. | |
080c7d56 | 138 | if(style & wxFRAME_TOOL_WINDOW) |
829a2e95 | 139 | newWindow = [[WXNSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]; |
080c7d56 | 140 | else |
829a2e95 DE |
141 | newWindow = [[WXNSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]; |
142 | // Make sure the default content view is a WXNSView | |
a24aa427 | 143 | [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]]; |
829a2e95 DE |
144 | // Associate the window and view |
145 | SetNSWindow(newWindow); | |
146 | ||
fb896a32 DE |
147 | // NOTE: SetNSWindow has retained the Cocoa object for this object. |
148 | // Because we do not release on close, the following release matches the | |
149 | // above alloc and thus the retain count will be 1. | |
150 | [m_cocoaNSWindow release]; | |
151 | ||
080c7d56 DE |
152 | if(style & wxFRAME_NO_TASKBAR) |
153 | [m_cocoaNSWindow setExcludedFromWindowsMenu: YES]; | |
154 | if(style & wxSTAY_ON_TOP) | |
155 | [m_cocoaNSWindow setLevel:NSFloatingWindowLevel]; | |
03cebc22 | 156 | [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)]; |
8d8d3633 | 157 | return true; |
fb896a32 DE |
158 | } |
159 | ||
160 | wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa() | |
161 | { | |
39050120 | 162 | wxASSERT(sm_cocoaDeactivateWindow!=this); |
7fc77f30 | 163 | wxAutoNSAutoreleasePool pool; |
0b659b0a | 164 | DestroyChildren(); |
9c85202a DE |
165 | if(m_cocoaNSView) |
166 | SendDestroyEvent(); | |
fb896a32 DE |
167 | SetNSWindow(NULL); |
168 | } | |
169 | ||
39050120 DE |
170 | bool wxTopLevelWindowCocoa::Destroy() |
171 | { | |
172 | if(sm_cocoaDeactivateWindow==this) | |
173 | { | |
174 | sm_cocoaDeactivateWindow = NULL; | |
175 | wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(); | |
176 | } | |
177 | return wxTopLevelWindowBase::Destroy(); | |
178 | } | |
179 | ||
fb896a32 DE |
180 | // ---------------------------------------------------------------------------- |
181 | // wxTopLevelWindowCocoa Cocoa Specifics | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
8ded703d | 184 | wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win) |
3e84f98f DE |
185 | { |
186 | wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow); | |
187 | if(parent) | |
8ded703d | 188 | return parent->GetAppMenuBar(win); |
3e84f98f DE |
189 | return NULL; |
190 | } | |
191 | ||
fb896a32 DE |
192 | void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow) |
193 | { | |
194 | bool need_debug = cocoaNSWindow || m_cocoaNSWindow; | |
48580976 | 195 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]); |
fe169919 | 196 | DisassociateNSWindow(m_cocoaNSWindow); |
fb896a32 DE |
197 | [cocoaNSWindow retain]; |
198 | [m_cocoaNSWindow release]; | |
199 | m_cocoaNSWindow = cocoaNSWindow; | |
829a2e95 DE |
200 | // NOTE: We are no longer using posing so we won't get events on the |
201 | // window's view unless it was explicitly created as the wx view class. | |
fb896a32 DE |
202 | if(m_cocoaNSWindow) |
203 | SetNSView([m_cocoaNSWindow contentView]); | |
204 | else | |
205 | SetNSView(NULL); | |
fe169919 | 206 | AssociateNSWindow(m_cocoaNSWindow); |
48580976 | 207 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]); |
fb896a32 DE |
208 | } |
209 | ||
448cbf1d DE |
210 | void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView) |
211 | { | |
911e17c6 | 212 | if([m_cocoaNSWindow contentView] == (id)oldView) |
448cbf1d DE |
213 | [m_cocoaNSWindow setContentView:newView]; |
214 | } | |
215 | ||
39050120 DE |
216 | /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow() |
217 | { | |
218 | if(sm_cocoaDeactivateWindow) | |
219 | sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(); | |
220 | sm_cocoaDeactivateWindow = NULL; | |
221 | } | |
222 | ||
aa992c59 | 223 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void) |
5aa417d5 | 224 | { |
39050120 | 225 | DeactivatePendingWindow(); |
48580976 | 226 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this); |
8d8d3633 | 227 | wxActivateEvent event(wxEVT_ACTIVATE, true, GetId()); |
5aa417d5 DE |
228 | event.SetEventObject(this); |
229 | GetEventHandler()->ProcessEvent(event); | |
230 | } | |
231 | ||
aa992c59 | 232 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void) |
5aa417d5 | 233 | { |
48580976 | 234 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this); |
8d8d3633 | 235 | wxActivateEvent event(wxEVT_ACTIVATE, false, GetId()); |
5aa417d5 DE |
236 | event.SetEventObject(this); |
237 | GetEventHandler()->ProcessEvent(event); | |
33faea0a DE |
238 | } |
239 | ||
240 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void) | |
241 | { | |
48580976 | 242 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this); |
33faea0a DE |
243 | } |
244 | ||
245 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void) | |
246 | { | |
48580976 | 247 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this); |
5aa417d5 DE |
248 | } |
249 | ||
9692f42b | 250 | void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void) |
fb896a32 DE |
251 | { |
252 | m_closed = true; | |
253 | Destroy(); | |
fb896a32 DE |
254 | } |
255 | ||
aa992c59 | 256 | bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose() |
fb896a32 DE |
257 | { |
258 | return wxWindowBase::Close(false); | |
259 | } | |
260 | ||
7dd8b1ea | 261 | void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem) |
86adc758 DE |
262 | { |
263 | } | |
264 | ||
7dd8b1ea | 265 | bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem) |
86adc758 DE |
266 | { |
267 | return false; | |
268 | } | |
269 | ||
fb896a32 DE |
270 | // ---------------------------------------------------------------------------- |
271 | // wxTopLevelWindowCocoa maximize/minimize | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | void wxTopLevelWindowCocoa::Maximize(bool maximize) | |
275 | { | |
276 | } | |
277 | ||
278 | bool wxTopLevelWindowCocoa::IsMaximized() const | |
279 | { | |
8d8d3633 | 280 | return false ; |
fb896a32 DE |
281 | } |
282 | ||
283 | void wxTopLevelWindowCocoa::Iconize(bool iconize) | |
284 | { | |
285 | } | |
286 | ||
287 | bool wxTopLevelWindowCocoa::IsIconized() const | |
288 | { | |
8d8d3633 | 289 | return false; |
fb896a32 DE |
290 | } |
291 | ||
292 | void wxTopLevelWindowCocoa::Restore() | |
293 | { | |
294 | } | |
295 | ||
296 | bool wxTopLevelWindowCocoa::Show(bool show) | |
297 | { | |
cbb2499e DE |
298 | if(m_isShown == show) |
299 | return false; | |
7fc77f30 | 300 | wxAutoNSAutoreleasePool pool; |
fb896a32 | 301 | if(show) |
275341c0 | 302 | { |
065e208e | 303 | // Send the window a size event because wxWidgets apps expect it |
275341c0 DE |
304 | // NOTE: This should really only be done the first time a window |
305 | // is shown. I doubt this will cause any problems though. | |
306 | wxSizeEvent event(GetSize(), GetId()); | |
307 | event.SetEventObject(this); | |
308 | GetEventHandler()->ProcessEvent(event); | |
309 | ||
fb896a32 | 310 | [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow]; |
275341c0 | 311 | } |
fb896a32 DE |
312 | else |
313 | [m_cocoaNSWindow orderOut:m_cocoaNSWindow]; | |
cbb2499e | 314 | m_isShown = show; |
fb896a32 DE |
315 | return true; |
316 | } | |
317 | ||
318 | bool wxTopLevelWindowCocoa::Close(bool force) | |
319 | { | |
320 | if(force) | |
321 | return wxWindowBase::Close(force); | |
322 | // performClose will fake the user clicking the close button which | |
323 | // will invoke windowShouldClose which will call the base class version | |
324 | // of Close() which will NOT Destroy() the window (see below) but | |
325 | // if closing is not stopped, then performClose will go ahead and | |
9692f42b DE |
326 | // close the window which will send the close notifications setting |
327 | // m_closed to true and Destroy()ing the window. | |
fb896a32 DE |
328 | [m_cocoaNSWindow performClose:m_cocoaNSWindow]; |
329 | return m_closed; | |
330 | } | |
331 | ||
332 | void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event) | |
333 | { | |
334 | // If the event was forced, close the window which will Destroy() it | |
335 | if(!event.CanVeto()) | |
336 | [m_cocoaNSWindow close]; | |
337 | // if the event was not forced, it's probably because the user clicked | |
338 | // the close button, or Close(false) was called which (see above) is | |
339 | // redirected to performClose and thus Cocoa itself will close the window | |
340 | } | |
341 | ||
342 | // ---------------------------------------------------------------------------- | |
343 | // wxTopLevelWindowCocoa misc | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
8b6fd08a | 346 | void wxTopLevelWindowCocoa::SetTitle(const wxString& title) |
8d8d3633 | 347 | { |
65aeaf19 | 348 | [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)]; |
8d8d3633 WS |
349 | } |
350 | ||
351 | wxString wxTopLevelWindowCocoa::GetTitle() const | |
352 | { | |
65aeaf19 | 353 | return wxStringWithNSString([m_cocoaNSWindow title]); |
8d8d3633 WS |
354 | } |
355 | ||
fb896a32 DE |
356 | bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style) |
357 | { | |
8d8d3633 | 358 | return false; |
fb896a32 DE |
359 | } |
360 | ||
361 | bool wxTopLevelWindowCocoa::IsFullScreen() const | |
362 | { | |
8d8d3633 | 363 | return false; |
fb896a32 DE |
364 | } |
365 | ||
e08efb8d DE |
366 | void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height) |
367 | { | |
368 | // Set the NSView size by setting the frame size to enclose it | |
369 | unsigned int styleMask = [m_cocoaNSWindow styleMask]; | |
370 | NSRect frameRect = [m_cocoaNSWindow frame]; | |
371 | NSRect contentRect = [NSWindow | |
372 | contentRectForFrameRect: frameRect | |
373 | styleMask: styleMask]; | |
374 | contentRect.size.width = width; | |
375 | contentRect.size.height = height; | |
376 | frameRect = [NSWindow | |
377 | frameRectForContentRect: contentRect | |
378 | styleMask: styleMask]; | |
379 | [m_cocoaNSWindow setFrame: frameRect display: NO]; | |
380 | } | |
381 | ||
fb896a32 DE |
382 | void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height) |
383 | { | |
9879fa84 | 384 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height); |
fb896a32 DE |
385 | |
386 | NSRect cocoaRect = NSMakeRect(x,y,width,height); | |
387 | [m_cocoaNSWindow setFrame: cocoaRect display:NO]; | |
388 | } | |
389 | ||
390 | void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const | |
391 | { | |
392 | NSRect cocoaRect = [m_cocoaNSWindow frame]; | |
393 | if(w) | |
394 | *w=(int)cocoaRect.size.width; | |
395 | if(h) | |
396 | *h=(int)cocoaRect.size.height; | |
9879fa84 | 397 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); |
fb896a32 DE |
398 | } |
399 | ||
400 | void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const | |
401 | { | |
402 | NSRect cocoaRect = [m_cocoaNSWindow frame]; | |
403 | if(x) | |
404 | *x=(int)cocoaRect.origin.x; | |
405 | if(y) | |
406 | *y=(int)cocoaRect.origin.y; | |
9879fa84 | 407 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); |
fb896a32 | 408 | } |