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 #import <AppKit/NSView.h>
37 #import <AppKit/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 if(style & wxFRAME_TOOL_WINDOW)
135 SetNSWindow([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
137 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
138 // NOTE: SetNSWindow has retained the Cocoa object for this object.
139 // Because we do not release on close, the following release matches the
140 // above alloc and thus the retain count will be 1.
141 [m_cocoaNSWindow release];
143 if(style & wxFRAME_NO_TASKBAR)
144 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
145 if(style & wxSTAY_ON_TOP)
146 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
147 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
151 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
153 wxASSERT(sm_cocoaDeactivateWindow!=this);
154 wxAutoNSAutoreleasePool pool;
161 bool wxTopLevelWindowCocoa::Destroy()
163 if(sm_cocoaDeactivateWindow==this)
165 sm_cocoaDeactivateWindow = NULL;
166 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
168 return wxTopLevelWindowBase::Destroy();
171 // ----------------------------------------------------------------------------
172 // wxTopLevelWindowCocoa Cocoa Specifics
173 // ----------------------------------------------------------------------------
175 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
177 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
179 return parent->GetAppMenuBar(win);
183 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
185 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
186 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
187 DisassociateNSWindow(m_cocoaNSWindow);
188 [cocoaNSWindow retain];
189 [m_cocoaNSWindow release];
190 m_cocoaNSWindow = cocoaNSWindow;
192 SetNSView([m_cocoaNSWindow contentView]);
195 AssociateNSWindow(m_cocoaNSWindow);
196 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
199 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
201 if([m_cocoaNSWindow contentView] == (id)oldView)
202 [m_cocoaNSWindow setContentView:newView];
205 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
207 if(sm_cocoaDeactivateWindow)
208 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
209 sm_cocoaDeactivateWindow = NULL;
212 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
214 DeactivatePendingWindow();
215 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
216 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
217 event.SetEventObject(this);
218 GetEventHandler()->ProcessEvent(event);
221 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
223 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
224 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
225 event.SetEventObject(this);
226 GetEventHandler()->ProcessEvent(event);
229 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
231 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
234 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
236 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
239 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
245 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
247 return wxWindowBase::Close(false);
250 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
254 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
259 // ----------------------------------------------------------------------------
260 // wxTopLevelWindowCocoa maximize/minimize
261 // ----------------------------------------------------------------------------
263 void wxTopLevelWindowCocoa::Maximize(bool maximize)
267 bool wxTopLevelWindowCocoa::IsMaximized() const
272 void wxTopLevelWindowCocoa::Iconize(bool iconize)
276 bool wxTopLevelWindowCocoa::IsIconized() const
281 void wxTopLevelWindowCocoa::Restore()
285 bool wxTopLevelWindowCocoa::Show(bool show)
287 if(m_isShown == show)
289 wxAutoNSAutoreleasePool pool;
292 // Send the window a size event because wxWidgets apps expect it
293 // NOTE: This should really only be done the first time a window
294 // is shown. I doubt this will cause any problems though.
295 wxSizeEvent event(GetSize(), GetId());
296 event.SetEventObject(this);
297 GetEventHandler()->ProcessEvent(event);
299 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
302 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
307 bool wxTopLevelWindowCocoa::Close(bool force)
310 return wxWindowBase::Close(force);
311 // performClose will fake the user clicking the close button which
312 // will invoke windowShouldClose which will call the base class version
313 // of Close() which will NOT Destroy() the window (see below) but
314 // if closing is not stopped, then performClose will go ahead and
315 // close the window which will send the close notifications setting
316 // m_closed to true and Destroy()ing the window.
317 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
321 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
323 // If the event was forced, close the window which will Destroy() it
325 [m_cocoaNSWindow close];
326 // if the event was not forced, it's probably because the user clicked
327 // the close button, or Close(false) was called which (see above) is
328 // redirected to performClose and thus Cocoa itself will close the window
331 // ----------------------------------------------------------------------------
332 // wxTopLevelWindowCocoa misc
333 // ----------------------------------------------------------------------------
335 void wxTopLevelWindowCocoa::SetTitle( const wxString& WXUNUSED(title))
340 wxString wxTopLevelWindowCocoa::GetTitle() const
343 return wxEmptyString;
346 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
351 bool wxTopLevelWindowCocoa::IsFullScreen() const
356 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
358 // Set the NSView size by setting the frame size to enclose it
359 unsigned int styleMask = [m_cocoaNSWindow styleMask];
360 NSRect frameRect = [m_cocoaNSWindow frame];
361 NSRect contentRect = [NSWindow
362 contentRectForFrameRect: frameRect
363 styleMask: styleMask];
364 contentRect.size.width = width;
365 contentRect.size.height = height;
366 frameRect = [NSWindow
367 frameRectForContentRect: contentRect
368 styleMask: styleMask];
369 [m_cocoaNSWindow setFrame: frameRect display: NO];
372 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
374 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
376 NSRect cocoaRect = NSMakeRect(x,y,width,height);
377 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
380 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
382 NSRect cocoaRect = [m_cocoaNSWindow frame];
384 *w=(int)cocoaRect.size.width;
386 *h=(int)cocoaRect.size.height;
387 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
390 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
392 NSRect cocoaRect = [m_cocoaNSWindow frame];
394 *x=(int)cocoaRect.origin.x;
396 *y=(int)cocoaRect.origin.y;
397 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);