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