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