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>
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // list of all frames and modeless dialogs
45 wxWindowList wxModelessWindows;
47 // ============================================================================
48 // wxTopLevelWindowCocoa implementation
49 // ============================================================================
51 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
53 // ----------------------------------------------------------------------------
54 // wxTopLevelWindowCocoa creation
55 // ----------------------------------------------------------------------------
56 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
57 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
60 void wxTopLevelWindowCocoa::Init()
67 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
69 unsigned int styleMask = 0;
71 styleMask |= NSTitledWindowMask;
72 if(style & wxMINIMIZE_BOX)
73 styleMask |= NSMiniaturizableWindowMask;
75 if(style & wxMAXIMIZE_BOX)
76 styleMask |= NSWindowMask;
78 if(style & wxCLOSE_BOX)
79 styleMask |= NSClosableWindowMask;
80 if(style & wxRESIZE_BORDER)
81 styleMask |= NSResizableWindowMask;
82 if(style & wxSIMPLE_BORDER)
83 styleMask |= NSBorderlessWindowMask;
87 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
89 const wxString& title,
95 wxAutoNSAutoreleasePool pool;
96 wxTopLevelWindows.Append(this);
98 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
102 parent->AddChild(this);
104 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
105 if(style & wxFRAME_TOOL_WINDOW)
106 cocoaStyle |= NSUtilityWindowMask;
108 // Create frame and check and handle default position and size
112 // WX has no set default position - the carbon port caps the low
113 // end at 20, 50. Here we do the same, except instead of setting
114 // it to 20 and 50, we set it to 100 and 100 if the values are too low
125 int realw = WidthDefault(size.x);
126 int realh = HeightDefault(size.y);
128 // NOTE: y-origin needs to be flipped.
129 NSRect cocoaRect = [NSWindow
130 contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh)
131 styleMask:cocoaStyle];
133 m_cocoaNSWindow = NULL;
134 m_cocoaNSView = NULL;
135 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
137 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
138 if(style & wxFRAME_TOOL_WINDOW)
139 newWindow = [[WXNSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
141 newWindow = [[WXNSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
142 // Make sure the default content view is a WXNSView
143 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
144 // Associate the window and view
145 SetNSWindow(newWindow);
147 // NOTE: SetNSWindow has retained the Cocoa object for this object.
148 // Because we do not release on close, the following release matches the
149 // above alloc and thus the retain count will be 1.
150 [m_cocoaNSWindow release];
152 if(style & wxFRAME_NO_TASKBAR)
153 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
154 if(style & wxSTAY_ON_TOP)
155 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
156 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
160 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
162 wxASSERT(sm_cocoaDeactivateWindow!=this);
163 wxAutoNSAutoreleasePool pool;
170 bool wxTopLevelWindowCocoa::Destroy()
172 if(sm_cocoaDeactivateWindow==this)
174 sm_cocoaDeactivateWindow = NULL;
175 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
177 return wxTopLevelWindowBase::Destroy();
180 // ----------------------------------------------------------------------------
181 // wxTopLevelWindowCocoa Cocoa Specifics
182 // ----------------------------------------------------------------------------
184 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
186 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
188 return parent->GetAppMenuBar(win);
192 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
194 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
195 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
196 DisassociateNSWindow(m_cocoaNSWindow);
197 [cocoaNSWindow retain];
198 [m_cocoaNSWindow release];
199 m_cocoaNSWindow = cocoaNSWindow;
200 // NOTE: We are no longer using posing so we won't get events on the
201 // window's view unless it was explicitly created as the wx view class.
203 SetNSView([m_cocoaNSWindow contentView]);
206 AssociateNSWindow(m_cocoaNSWindow);
207 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
210 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
212 if([m_cocoaNSWindow contentView] == (id)oldView)
213 [m_cocoaNSWindow setContentView:newView];
216 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
218 if(sm_cocoaDeactivateWindow)
219 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
220 sm_cocoaDeactivateWindow = NULL;
223 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
225 DeactivatePendingWindow();
226 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
227 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
228 event.SetEventObject(this);
229 GetEventHandler()->ProcessEvent(event);
232 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
234 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
235 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
236 event.SetEventObject(this);
237 GetEventHandler()->ProcessEvent(event);
240 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
242 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
245 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
247 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
250 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
256 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
258 return wxWindowBase::Close(false);
261 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
265 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
270 // ----------------------------------------------------------------------------
271 // wxTopLevelWindowCocoa maximize/minimize
272 // ----------------------------------------------------------------------------
274 void wxTopLevelWindowCocoa::Maximize(bool maximize)
278 bool wxTopLevelWindowCocoa::IsMaximized() const
283 void wxTopLevelWindowCocoa::Iconize(bool iconize)
287 bool wxTopLevelWindowCocoa::IsIconized() const
292 void wxTopLevelWindowCocoa::Restore()
296 bool wxTopLevelWindowCocoa::Show(bool show)
298 if(m_isShown == show)
300 wxAutoNSAutoreleasePool pool;
303 // Send the window a size event because wxWidgets apps expect it
304 // NOTE: This should really only be done the first time a window
305 // is shown. I doubt this will cause any problems though.
306 wxSizeEvent event(GetSize(), GetId());
307 event.SetEventObject(this);
308 GetEventHandler()->ProcessEvent(event);
310 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
313 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
318 bool wxTopLevelWindowCocoa::Close(bool force)
321 return wxWindowBase::Close(force);
322 // performClose will fake the user clicking the close button which
323 // will invoke windowShouldClose which will call the base class version
324 // of Close() which will NOT Destroy() the window (see below) but
325 // if closing is not stopped, then performClose will go ahead and
326 // close the window which will send the close notifications setting
327 // m_closed to true and Destroy()ing the window.
328 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
332 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
334 // If the event was forced, close the window which will Destroy() it
336 [m_cocoaNSWindow close];
337 // if the event was not forced, it's probably because the user clicked
338 // the close button, or Close(false) was called which (see above) is
339 // redirected to performClose and thus Cocoa itself will close the window
342 // ----------------------------------------------------------------------------
343 // wxTopLevelWindowCocoa misc
344 // ----------------------------------------------------------------------------
346 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
348 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
351 wxString wxTopLevelWindowCocoa::GetTitle() const
353 return wxStringWithNSString([m_cocoaNSWindow title]);
356 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
361 bool wxTopLevelWindowCocoa::IsFullScreen() const
366 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
368 // Set the NSView size by setting the frame size to enclose it
369 unsigned int styleMask = [m_cocoaNSWindow styleMask];
370 NSRect frameRect = [m_cocoaNSWindow frame];
371 NSRect contentRect = [NSWindow
372 contentRectForFrameRect: frameRect
373 styleMask: styleMask];
374 contentRect.size.width = width;
375 contentRect.size.height = height;
376 frameRect = [NSWindow
377 frameRectForContentRect: contentRect
378 styleMask: styleMask];
379 [m_cocoaNSWindow setFrame: frameRect display: NO];
382 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
384 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
386 NSRect cocoaRect = NSMakeRect(x,y,width,height);
387 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
390 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
392 NSRect cocoaRect = [m_cocoaNSWindow frame];
394 *w=(int)cocoaRect.size.width;
396 *h=(int)cocoaRect.size.height;
397 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
400 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
402 NSRect cocoaRect = [m_cocoaNSWindow frame];
404 *x=(int)cocoaRect.origin.x;
406 *y=(int)cocoaRect.origin.y;
407 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);