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/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 // Create frame and check and handle default position and size
109 // WX has no set default position - the carbon port caps the low
110 // end at 20, 50. Here we do the same, except instead of setting
111 // it to 20 and 50, we set it to 100 and 100 if the values are too low
122 int realw = WidthDefault(size.x);
123 int realh = HeightDefault(size.y);
125 // NOTE: y-origin needs to be flipped.
126 NSRect cocoaRect = [NSWindow
127 contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh)
128 styleMask:cocoaStyle];
130 m_cocoaNSWindow = NULL;
131 m_cocoaNSView = NULL;
132 if(style & wxFRAME_TOOL_WINDOW)
133 SetNSWindow([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
135 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
136 // NOTE: SetNSWindow has retained the Cocoa object for this object.
137 // Because we do not release on close, the following release matches the
138 // above alloc and thus the retain count will be 1.
139 [m_cocoaNSWindow release];
141 if(style & wxFRAME_NO_TASKBAR)
142 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
143 if(style & wxSTAY_ON_TOP)
144 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
145 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
149 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
151 wxASSERT(sm_cocoaDeactivateWindow!=this);
152 wxAutoNSAutoreleasePool pool;
159 bool wxTopLevelWindowCocoa::Destroy()
161 if(sm_cocoaDeactivateWindow==this)
163 sm_cocoaDeactivateWindow = NULL;
164 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
166 return wxTopLevelWindowBase::Destroy();
169 // ----------------------------------------------------------------------------
170 // wxTopLevelWindowCocoa Cocoa Specifics
171 // ----------------------------------------------------------------------------
173 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
175 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
177 return parent->GetAppMenuBar(win);
181 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
183 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
184 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
185 DisassociateNSWindow(m_cocoaNSWindow);
186 [cocoaNSWindow retain];
187 [m_cocoaNSWindow release];
188 m_cocoaNSWindow = cocoaNSWindow;
190 SetNSView([m_cocoaNSWindow contentView]);
193 AssociateNSWindow(m_cocoaNSWindow);
194 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
197 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
199 if([m_cocoaNSWindow contentView] == (id)oldView)
200 [m_cocoaNSWindow setContentView:newView];
203 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
205 if(sm_cocoaDeactivateWindow)
206 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
207 sm_cocoaDeactivateWindow = NULL;
210 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
212 DeactivatePendingWindow();
213 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
214 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
215 event.SetEventObject(this);
216 GetEventHandler()->ProcessEvent(event);
219 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
221 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
222 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
223 event.SetEventObject(this);
224 GetEventHandler()->ProcessEvent(event);
227 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
229 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
232 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
234 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
237 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
243 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
245 return wxWindowBase::Close(false);
248 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
252 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
257 // ----------------------------------------------------------------------------
258 // wxTopLevelWindowCocoa maximize/minimize
259 // ----------------------------------------------------------------------------
261 void wxTopLevelWindowCocoa::Maximize(bool maximize)
265 bool wxTopLevelWindowCocoa::IsMaximized() const
270 void wxTopLevelWindowCocoa::Iconize(bool iconize)
274 bool wxTopLevelWindowCocoa::IsIconized() const
279 void wxTopLevelWindowCocoa::Restore()
283 bool wxTopLevelWindowCocoa::Show(bool show)
285 if(m_isShown == show)
287 wxAutoNSAutoreleasePool pool;
290 // Send the window a size event because wxWidgets apps expect it
291 // NOTE: This should really only be done the first time a window
292 // is shown. I doubt this will cause any problems though.
293 wxSizeEvent event(GetSize(), GetId());
294 event.SetEventObject(this);
295 GetEventHandler()->ProcessEvent(event);
297 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
300 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
305 bool wxTopLevelWindowCocoa::Close(bool force)
308 return wxWindowBase::Close(force);
309 // performClose will fake the user clicking the close button which
310 // will invoke windowShouldClose which will call the base class version
311 // of Close() which will NOT Destroy() the window (see below) but
312 // if closing is not stopped, then performClose will go ahead and
313 // close the window which will send the close notifications setting
314 // m_closed to true and Destroy()ing the window.
315 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
319 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
321 // If the event was forced, close the window which will Destroy() it
323 [m_cocoaNSWindow close];
324 // if the event was not forced, it's probably because the user clicked
325 // the close button, or Close(false) was called which (see above) is
326 // redirected to performClose and thus Cocoa itself will close the window
329 // ----------------------------------------------------------------------------
330 // wxTopLevelWindowCocoa misc
331 // ----------------------------------------------------------------------------
333 void wxTopLevelWindowCocoa::SetTitle( const wxString& WXUNUSED(title))
338 wxString wxTopLevelWindowCocoa::GetTitle() const
341 return wxEmptyString;
344 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
349 bool wxTopLevelWindowCocoa::IsFullScreen() const
354 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
356 // Set the NSView size by setting the frame size to enclose it
357 unsigned int styleMask = [m_cocoaNSWindow styleMask];
358 NSRect frameRect = [m_cocoaNSWindow frame];
359 NSRect contentRect = [NSWindow
360 contentRectForFrameRect: frameRect
361 styleMask: styleMask];
362 contentRect.size.width = width;
363 contentRect.size.height = height;
364 frameRect = [NSWindow
365 frameRectForContentRect: contentRect
366 styleMask: styleMask];
367 [m_cocoaNSWindow setFrame: frameRect display: NO];
370 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
372 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
374 NSRect cocoaRect = NSMakeRect(x,y,width,height);
375 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
378 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
380 NSRect cocoaRect = [m_cocoaNSWindow frame];
382 *w=(int)cocoaRect.size.width;
384 *h=(int)cocoaRect.size.height;
385 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
388 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
390 NSRect cocoaRect = [m_cocoaNSWindow frame];
392 *x=(int)cocoaRect.origin.x;
394 *y=(int)cocoaRect.origin.y;
395 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);