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 // 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 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
338 bool wxTopLevelWindowCocoa::IsFullScreen() const
343 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
345 // Set the NSView size by setting the frame size to enclose it
346 unsigned int styleMask = [m_cocoaNSWindow styleMask];
347 NSRect frameRect = [m_cocoaNSWindow frame];
348 NSRect contentRect = [NSWindow
349 contentRectForFrameRect: frameRect
350 styleMask: styleMask];
351 contentRect.size.width = width;
352 contentRect.size.height = height;
353 frameRect = [NSWindow
354 frameRectForContentRect: contentRect
355 styleMask: styleMask];
356 [m_cocoaNSWindow setFrame: frameRect display: NO];
359 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
361 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
363 NSRect cocoaRect = NSMakeRect(x,y,width,height);
364 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
367 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
369 NSRect cocoaRect = [m_cocoaNSWindow frame];
371 *w=(int)cocoaRect.size.width;
373 *h=(int)cocoaRect.size.height;
374 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
377 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
379 NSRect cocoaRect = [m_cocoaNSWindow frame];
381 *x=(int)cocoaRect.origin.x;
383 *y=(int)cocoaRect.origin.y;
384 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);