]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: cocoa/toplevel.mm | |
3 | // Purpose: implements wxTopLevelWindow for Cocoa | |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2002/11/27 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2002 David Elliott | |
9 | // Licence: wxWidgets licence | |
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 | ||
31 | #include "wx/cocoa/autorelease.h" | |
32 | #include "wx/cocoa/string.h" | |
33 | ||
34 | #import <AppKit/NSView.h> | |
35 | #import <AppKit/NSWindow.h> | |
36 | #import <AppKit/NSPanel.h> | |
37 | // ---------------------------------------------------------------------------- | |
38 | // globals | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // list of all frames and modeless dialogs | |
42 | wxWindowList wxModelessWindows; | |
43 | ||
44 | // ============================================================================ | |
45 | // wxTopLevelWindowCocoa implementation | |
46 | // ============================================================================ | |
47 | ||
48 | wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL; | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxTopLevelWindowCocoa creation | |
52 | // ---------------------------------------------------------------------------- | |
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 | ||
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 | ||
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 | { | |
92 | wxAutoNSAutoreleasePool pool; | |
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 | ||
101 | unsigned int cocoaStyle = NSWindowStyleForWxStyle(style); | |
102 | if(style & wxFRAME_TOOL_WINDOW) | |
103 | cocoaStyle |= NSUtilityWindowMask; | |
104 | ||
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 | ||
125 | // NOTE: y-origin needs to be flipped. | |
126 | NSRect cocoaRect = [NSWindow | |
127 | contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh) | |
128 | styleMask:cocoaStyle]; | |
129 | ||
130 | m_cocoaNSWindow = NULL; | |
131 | m_cocoaNSView = NULL; | |
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]); | |
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 | ||
141 | if(style & wxFRAME_NO_TASKBAR) | |
142 | [m_cocoaNSWindow setExcludedFromWindowsMenu: YES]; | |
143 | if(style & wxSTAY_ON_TOP) | |
144 | [m_cocoaNSWindow setLevel:NSFloatingWindowLevel]; | |
145 | [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)]; | |
146 | return TRUE; | |
147 | } | |
148 | ||
149 | wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa() | |
150 | { | |
151 | wxASSERT(sm_cocoaDeactivateWindow!=this); | |
152 | wxAutoNSAutoreleasePool pool; | |
153 | DestroyChildren(); | |
154 | if(m_cocoaNSView) | |
155 | SendDestroyEvent(); | |
156 | SetNSWindow(NULL); | |
157 | } | |
158 | ||
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 | ||
169 | // ---------------------------------------------------------------------------- | |
170 | // wxTopLevelWindowCocoa Cocoa Specifics | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
173 | wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win) | |
174 | { | |
175 | wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow); | |
176 | if(parent) | |
177 | return parent->GetAppMenuBar(win); | |
178 | return NULL; | |
179 | } | |
180 | ||
181 | void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow) | |
182 | { | |
183 | bool need_debug = cocoaNSWindow || m_cocoaNSWindow; | |
184 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]); | |
185 | DisassociateNSWindow(m_cocoaNSWindow); | |
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); | |
193 | AssociateNSWindow(m_cocoaNSWindow); | |
194 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]); | |
195 | } | |
196 | ||
197 | void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView) | |
198 | { | |
199 | if([m_cocoaNSWindow contentView] == (id)oldView) | |
200 | [m_cocoaNSWindow setContentView:newView]; | |
201 | } | |
202 | ||
203 | /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow() | |
204 | { | |
205 | if(sm_cocoaDeactivateWindow) | |
206 | sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(); | |
207 | sm_cocoaDeactivateWindow = NULL; | |
208 | } | |
209 | ||
210 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void) | |
211 | { | |
212 | DeactivatePendingWindow(); | |
213 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this); | |
214 | wxActivateEvent event(wxEVT_ACTIVATE, TRUE, GetId()); | |
215 | event.SetEventObject(this); | |
216 | GetEventHandler()->ProcessEvent(event); | |
217 | } | |
218 | ||
219 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void) | |
220 | { | |
221 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this); | |
222 | wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId()); | |
223 | event.SetEventObject(this); | |
224 | GetEventHandler()->ProcessEvent(event); | |
225 | } | |
226 | ||
227 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void) | |
228 | { | |
229 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this); | |
230 | } | |
231 | ||
232 | void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void) | |
233 | { | |
234 | wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this); | |
235 | } | |
236 | ||
237 | void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void) | |
238 | { | |
239 | m_closed = true; | |
240 | Destroy(); | |
241 | } | |
242 | ||
243 | bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose() | |
244 | { | |
245 | return wxWindowBase::Close(false); | |
246 | } | |
247 | ||
248 | void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem) | |
249 | { | |
250 | } | |
251 | ||
252 | bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem) | |
253 | { | |
254 | return false; | |
255 | } | |
256 | ||
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 | { | |
285 | if(m_isShown == show) | |
286 | return false; | |
287 | wxAutoNSAutoreleasePool pool; | |
288 | if(show) | |
289 | { | |
290 | // Send the window a size event because wxWidgets apps expect it | |
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 | ||
297 | [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow]; | |
298 | } | |
299 | else | |
300 | [m_cocoaNSWindow orderOut:m_cocoaNSWindow]; | |
301 | m_isShown = show; | |
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 | |
313 | // close the window which will send the close notifications setting | |
314 | // m_closed to true and Destroy()ing the window. | |
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 | ||
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 | ||
359 | void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height) | |
360 | { | |
361 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height); | |
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; | |
374 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); | |
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; | |
384 | wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); | |
385 | } | |
386 |