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