1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/toplevel.mm
3 // Purpose: implements wxTopLevelWindow for Cocoa
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include "wx/toplevel.h"
26 #include "wx/window.h"
27 #include "wx/menuitem.h"
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
36 #include "wx/cocoa/objc/NSView.h"
37 #include "wx/cocoa/objc/NSWindow.h"
38 #import <AppKit/NSPanel.h>
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // list of all frames and modeless dialogs
44 wxWindowList wxModelessWindows;
46 // ============================================================================
47 // wxTopLevelWindowCocoa implementation
48 // ============================================================================
50 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
52 // ----------------------------------------------------------------------------
53 // wxTopLevelWindowCocoa creation
54 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
56 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
59 void wxTopLevelWindowCocoa::Init()
66 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
68 unsigned int styleMask = 0;
70 styleMask |= NSTitledWindowMask;
71 if(style & wxMINIMIZE_BOX)
72 styleMask |= NSMiniaturizableWindowMask;
74 if(style & wxMAXIMIZE_BOX)
75 styleMask |= NSWindowMask;
77 if(style & wxCLOSE_BOX)
78 styleMask |= NSClosableWindowMask;
79 if(style & wxRESIZE_BORDER)
80 styleMask |= NSResizableWindowMask;
81 if(style & wxSIMPLE_BORDER)
82 styleMask |= NSBorderlessWindowMask;
86 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
88 const wxString& title,
94 wxAutoNSAutoreleasePool pool;
95 wxTopLevelWindows.Append(this);
97 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
101 parent->AddChild(this);
103 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
104 if(style & wxFRAME_TOOL_WINDOW)
105 cocoaStyle |= NSUtilityWindowMask;
107 // Create frame and check and handle default position and size
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
124 int realw = WidthDefault(size.x);
125 int realh = HeightDefault(size.y);
127 // NOTE: y-origin needs to be flipped.
128 NSRect cocoaRect = [NSWindow
129 contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh)
130 styleMask:cocoaStyle];
132 m_cocoaNSWindow = NULL;
133 m_cocoaNSView = NULL;
134 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
136 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
137 if(style & wxFRAME_TOOL_WINDOW)
138 newWindow = [[WXNSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
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);
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];
151 if(style & wxFRAME_NO_TASKBAR)
152 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
153 if(style & wxSTAY_ON_TOP)
154 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
155 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
159 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
161 wxASSERT(sm_cocoaDeactivateWindow!=this);
162 wxAutoNSAutoreleasePool pool;
169 bool wxTopLevelWindowCocoa::Destroy()
171 if(sm_cocoaDeactivateWindow==this)
173 sm_cocoaDeactivateWindow = NULL;
174 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
176 return wxTopLevelWindowBase::Destroy();
179 // ----------------------------------------------------------------------------
180 // wxTopLevelWindowCocoa Cocoa Specifics
181 // ----------------------------------------------------------------------------
183 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
185 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
187 return parent->GetAppMenuBar(win);
191 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
193 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
194 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
195 DisassociateNSWindow(m_cocoaNSWindow);
196 [cocoaNSWindow retain];
197 [m_cocoaNSWindow release];
198 m_cocoaNSWindow = cocoaNSWindow;
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.
202 SetNSView([m_cocoaNSWindow contentView]);
205 AssociateNSWindow(m_cocoaNSWindow);
206 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
209 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
211 if([m_cocoaNSWindow contentView] == (id)oldView)
212 [m_cocoaNSWindow setContentView:newView];
215 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
217 if(sm_cocoaDeactivateWindow)
218 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
219 sm_cocoaDeactivateWindow = NULL;
222 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
224 DeactivatePendingWindow();
225 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
226 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
227 event.SetEventObject(this);
228 GetEventHandler()->ProcessEvent(event);
231 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
233 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
234 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
235 event.SetEventObject(this);
236 GetEventHandler()->ProcessEvent(event);
239 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
241 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
244 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
246 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
249 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
255 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
257 return wxWindowBase::Close(false);
260 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
264 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
269 // ----------------------------------------------------------------------------
270 // wxTopLevelWindowCocoa maximize/minimize
271 // ----------------------------------------------------------------------------
273 void wxTopLevelWindowCocoa::Maximize(bool maximize)
277 bool wxTopLevelWindowCocoa::IsMaximized() const
282 void wxTopLevelWindowCocoa::Iconize(bool iconize)
286 bool wxTopLevelWindowCocoa::IsIconized() const
291 void wxTopLevelWindowCocoa::Restore()
295 bool wxTopLevelWindowCocoa::Show(bool show)
297 if(m_isShown == show)
299 wxAutoNSAutoreleasePool pool;
302 // Send the window a size event because wxWidgets apps expect it
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);
309 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
312 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
317 bool wxTopLevelWindowCocoa::Close(bool 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
325 // close the window which will send the close notifications setting
326 // m_closed to true and Destroy()ing the window.
327 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
331 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
333 // If the event was forced, close the window which will Destroy() it
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
341 // ----------------------------------------------------------------------------
342 // wxTopLevelWindowCocoa misc
343 // ----------------------------------------------------------------------------
345 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
347 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
350 wxString wxTopLevelWindowCocoa::GetTitle() const
352 return wxStringWithNSString([m_cocoaNSWindow title]);
355 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
360 bool wxTopLevelWindowCocoa::IsFullScreen() const
365 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
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];
381 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
383 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
385 NSRect cocoaRect = NSMakeRect(x,y,width,height);
386 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
389 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
391 NSRect cocoaRect = [m_cocoaNSWindow frame];
393 *w=(int)cocoaRect.size.width;
395 *h=(int)cocoaRect.size.height;
396 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
399 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
401 NSRect cocoaRect = [m_cocoaNSWindow frame];
403 *x=(int)cocoaRect.origin.x;
405 *y=(int)cocoaRect.origin.y;
406 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);