1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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/window.h"
24 #include "wx/toplevel.h"
25 #include "wx/menuitem.h"
31 #include "wx/cocoa/autorelease.h"
32 #include "wx/cocoa/string.h"
34 #import <AppKit/NSView.h>
35 #import <AppKit/NSWindow.h>
36 #import <AppKit/NSPanel.h>
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // list of all frames and modeless dialogs
42 wxWindowList wxModelessWindows;
44 // ============================================================================
45 // wxTopLevelWindowCocoa implementation
46 // ============================================================================
48 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
50 // ----------------------------------------------------------------------------
51 // wxTopLevelWindowCocoa creation
52 // ----------------------------------------------------------------------------
53 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
54 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
57 void wxTopLevelWindowCocoa::Init()
64 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
66 unsigned int styleMask = 0;
68 styleMask |= NSTitledWindowMask;
69 if(style & wxMINIMIZE_BOX)
70 styleMask |= NSMiniaturizableWindowMask;
72 if(style & wxMAXIMIZE_BOX)
73 styleMask |= NSWindowMask;
75 if(style & wxCLOSE_BOX)
76 styleMask |= NSClosableWindowMask;
77 if(style & wxRESIZE_BORDER)
78 styleMask |= NSResizableWindowMask;
79 if(style & wxSIMPLE_BORDER)
80 styleMask |= NSBorderlessWindowMask;
84 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
86 const wxString& title,
92 wxAutoNSAutoreleasePool pool;
93 wxTopLevelWindows.Append(this);
95 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
99 parent->AddChild(this);
101 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
102 if(style & wxFRAME_TOOL_WINDOW)
103 cocoaStyle |= NSUtilityWindowMask;
105 wxPoint realpos = pos;
106 wxSize realsize = size;
107 // FIXME: this is lame
116 // NOTE: y-origin needs to be flipped.
117 NSRect cocoaRect = [NSWindow contentRectForFrameRect:NSMakeRect(realpos.x,realpos.y,realsize.x,realsize.y) styleMask:cocoaStyle];
119 m_cocoaNSWindow = NULL;
120 m_cocoaNSView = NULL;
121 if(style & wxFRAME_TOOL_WINDOW)
122 SetNSWindow([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
124 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
125 // NOTE: SetNSWindow has retained the Cocoa object for this object.
126 // Because we do not release on close, the following release matches the
127 // above alloc and thus the retain count will be 1.
128 [m_cocoaNSWindow release];
130 if(style & wxFRAME_NO_TASKBAR)
131 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
132 if(style & wxSTAY_ON_TOP)
133 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
134 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
138 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
140 wxASSERT(sm_cocoaDeactivateWindow!=this);
141 wxAutoNSAutoreleasePool pool;
148 bool wxTopLevelWindowCocoa::Destroy()
150 if(sm_cocoaDeactivateWindow==this)
152 sm_cocoaDeactivateWindow = NULL;
153 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
155 return wxTopLevelWindowBase::Destroy();
158 // ----------------------------------------------------------------------------
159 // wxTopLevelWindowCocoa Cocoa Specifics
160 // ----------------------------------------------------------------------------
162 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
164 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
166 return parent->GetAppMenuBar(win);
170 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
172 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
173 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
174 DisassociateNSWindow(m_cocoaNSWindow);
175 [cocoaNSWindow retain];
176 [m_cocoaNSWindow release];
177 m_cocoaNSWindow = cocoaNSWindow;
179 SetNSView([m_cocoaNSWindow contentView]);
182 AssociateNSWindow(m_cocoaNSWindow);
183 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
186 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
188 if([m_cocoaNSWindow contentView] == (id)oldView)
189 [m_cocoaNSWindow setContentView:newView];
192 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
194 if(sm_cocoaDeactivateWindow)
195 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
196 sm_cocoaDeactivateWindow = NULL;
199 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
201 DeactivatePendingWindow();
202 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
203 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, GetId());
204 event.SetEventObject(this);
205 GetEventHandler()->ProcessEvent(event);
208 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
210 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
211 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
212 event.SetEventObject(this);
213 GetEventHandler()->ProcessEvent(event);
216 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
218 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
221 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
223 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
226 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
232 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
234 return wxWindowBase::Close(false);
237 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
241 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
246 // ----------------------------------------------------------------------------
247 // wxTopLevelWindowCocoa maximize/minimize
248 // ----------------------------------------------------------------------------
250 void wxTopLevelWindowCocoa::Maximize(bool maximize)
254 bool wxTopLevelWindowCocoa::IsMaximized() const
259 void wxTopLevelWindowCocoa::Iconize(bool iconize)
263 bool wxTopLevelWindowCocoa::IsIconized() const
268 void wxTopLevelWindowCocoa::Restore()
272 bool wxTopLevelWindowCocoa::Show(bool show)
274 if(m_isShown == show)
276 wxAutoNSAutoreleasePool pool;
279 // Send the window a size event because wxWidgets apps expect it
280 // NOTE: This should really only be done the first time a window
281 // is shown. I doubt this will cause any problems though.
282 wxSizeEvent event(GetSize(), GetId());
283 event.SetEventObject(this);
284 GetEventHandler()->ProcessEvent(event);
286 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
289 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
294 bool wxTopLevelWindowCocoa::Close(bool force)
297 return wxWindowBase::Close(force);
298 // performClose will fake the user clicking the close button which
299 // will invoke windowShouldClose which will call the base class version
300 // of Close() which will NOT Destroy() the window (see below) but
301 // if closing is not stopped, then performClose will go ahead and
302 // close the window which will send the close notifications setting
303 // m_closed to true and Destroy()ing the window.
304 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
308 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
310 // If the event was forced, close the window which will Destroy() it
312 [m_cocoaNSWindow close];
313 // if the event was not forced, it's probably because the user clicked
314 // the close button, or Close(false) was called which (see above) is
315 // redirected to performClose and thus Cocoa itself will close the window
318 // ----------------------------------------------------------------------------
319 // wxTopLevelWindowCocoa misc
320 // ----------------------------------------------------------------------------
322 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
327 bool wxTopLevelWindowCocoa::IsFullScreen() const
332 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
334 // Set the NSView size by setting the frame size to enclose it
335 unsigned int styleMask = [m_cocoaNSWindow styleMask];
336 NSRect frameRect = [m_cocoaNSWindow frame];
337 NSRect contentRect = [NSWindow
338 contentRectForFrameRect: frameRect
339 styleMask: styleMask];
340 contentRect.size.width = width;
341 contentRect.size.height = height;
342 frameRect = [NSWindow
343 frameRectForContentRect: contentRect
344 styleMask: styleMask];
345 [m_cocoaNSWindow setFrame: frameRect display: NO];
348 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
350 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
352 NSRect cocoaRect = NSMakeRect(x,y,width,height);
353 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
356 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
358 NSRect cocoaRect = [m_cocoaNSWindow frame];
360 *w=(int)cocoaRect.size.width;
362 *h=(int)cocoaRect.size.height;
363 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
366 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
368 NSRect cocoaRect = [m_cocoaNSWindow frame];
370 *x=(int)cocoaRect.origin.x;
372 *y=(int)cocoaRect.origin.y;
373 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);