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