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>
39 #import <AppKit/NSButtonCell.h>
40 #import <AppKit/NSControl.h>
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // list of all frames and modeless dialogs
47 wxWindowList wxModelessWindows;
49 // ============================================================================
50 // wxTopLevelWindowCocoa implementation
51 // ============================================================================
53 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
55 // ----------------------------------------------------------------------------
56 // wxTopLevelWindowCocoa creation
57 // ----------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
59 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
62 void wxTopLevelWindowCocoa::Init()
69 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
71 unsigned int styleMask = 0;
73 styleMask |= NSTitledWindowMask;
74 if(style & wxMINIMIZE_BOX)
75 styleMask |= NSMiniaturizableWindowMask;
77 if(style & wxMAXIMIZE_BOX)
78 styleMask |= NSWindowMask;
80 if(style & wxCLOSE_BOX)
81 styleMask |= NSClosableWindowMask;
82 if(style & wxRESIZE_BORDER)
83 styleMask |= NSResizableWindowMask;
84 if(style & wxSIMPLE_BORDER)
85 styleMask |= NSBorderlessWindowMask;
89 NSRect wxTopLevelWindowCocoa::MakeInitialNSWindowContentRect(const wxPoint& pos, const wxSize& size, unsigned int cocoaStyleMask)
91 // Arbitrarily use (100,100) as the origin when default coords are given.
92 wxCoord x = pos.x!=wxDefaultCoord ? pos.x : 100;
93 wxCoord y = pos.y!=wxDefaultCoord ? pos.y : 100;
95 wxCoord w = WidthDefault(size.x);
96 wxCoord h = HeightDefault(size.y);
98 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,w,h,true);
101 contentRectForFrameRect:NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,w,h)
102 styleMask:cocoaStyleMask];
106 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
108 const wxString& title,
112 const wxString& name)
114 wxAutoNSAutoreleasePool pool;
115 wxTopLevelWindows.Append(this);
117 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
121 parent->AddChild(this);
123 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
124 if(style & wxFRAME_TOOL_WINDOW)
125 cocoaStyle |= NSUtilityWindowMask;
127 NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
129 m_cocoaNSWindow = NULL;
130 m_cocoaNSView = NULL;
131 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
133 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
134 if(style & wxFRAME_TOOL_WINDOW)
135 newWindow = [[WX_GET_OBJC_CLASS(WXNSPanel) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
137 newWindow = [[WX_GET_OBJC_CLASS(WXNSWindow) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
138 // Make sure the default content view is a WXNSView
139 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
140 // Associate the window and view
141 SetNSWindow(newWindow);
143 // NOTE: SetNSWindow has retained the Cocoa object for this object.
144 // Because we do not release on close, the following release matches the
145 // above alloc and thus the retain count will be 1.
146 [m_cocoaNSWindow release];
148 if(style & wxFRAME_NO_TASKBAR)
149 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
150 if(style & wxSTAY_ON_TOP)
151 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
152 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
156 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
158 wxASSERT(sm_cocoaDeactivateWindow!=this);
159 wxAutoNSAutoreleasePool pool;
166 bool wxTopLevelWindowCocoa::Destroy()
168 if(sm_cocoaDeactivateWindow==this)
170 sm_cocoaDeactivateWindow = NULL;
171 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
173 return wxTopLevelWindowBase::Destroy();
176 // ----------------------------------------------------------------------------
177 // wxTopLevelWindowCocoa Cocoa Specifics
178 // ----------------------------------------------------------------------------
180 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
182 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
184 return parent->GetAppMenuBar(win);
188 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
190 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
191 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
192 DisassociateNSWindow(m_cocoaNSWindow);
193 [cocoaNSWindow retain];
194 [m_cocoaNSWindow release];
195 m_cocoaNSWindow = cocoaNSWindow;
196 // NOTE: We are no longer using posing so we won't get events on the
197 // window's view unless it was explicitly created as the wx view class.
199 SetNSView([m_cocoaNSWindow contentView]);
202 AssociateNSWindow(m_cocoaNSWindow);
203 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
206 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
208 if([m_cocoaNSWindow contentView] == (id)oldView)
209 [m_cocoaNSWindow setContentView:newView];
212 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
214 if(sm_cocoaDeactivateWindow)
215 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
216 sm_cocoaDeactivateWindow = NULL;
219 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
221 DeactivatePendingWindow();
222 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
223 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
224 event.SetEventObject(this);
225 HandleWindowEvent(event);
228 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
230 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
231 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
232 event.SetEventObject(this);
233 HandleWindowEvent(event);
236 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
238 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
241 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
243 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
246 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
252 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
254 return wxWindowBase::Close(false);
257 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
261 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
266 // ----------------------------------------------------------------------------
267 // wxTopLevelWindowCocoa maximize/minimize
268 // ----------------------------------------------------------------------------
270 void wxTopLevelWindowCocoa::Maximize(bool maximize)
274 bool wxTopLevelWindowCocoa::IsMaximized() const
279 void wxTopLevelWindowCocoa::Iconize(bool iconize)
283 bool wxTopLevelWindowCocoa::IsIconized() const
288 void wxTopLevelWindowCocoa::Restore()
292 bool wxTopLevelWindowCocoa::Show(bool show)
294 if(m_isShown == show)
296 wxAutoNSAutoreleasePool pool;
299 // Send the window a size event because wxWidgets apps expect it
300 // NOTE: This should really only be done the first time a window
301 // is shown. I doubt this will cause any problems though.
302 wxSizeEvent event(GetSize(), GetId());
303 event.SetEventObject(this);
304 HandleWindowEvent(event);
306 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
309 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
314 bool wxTopLevelWindowCocoa::Close(bool force)
317 return wxWindowBase::Close(force);
318 // performClose will fake the user clicking the close button which
319 // will invoke windowShouldClose which will call the base class version
320 // of Close() which will NOT Destroy() the window (see below) but
321 // if closing is not stopped, then performClose will go ahead and
322 // close the window which will send the close notifications setting
323 // m_closed to true and Destroy()ing the window.
324 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
328 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
330 // If the event was forced, close the window which will Destroy() it
332 [m_cocoaNSWindow close];
333 // if the event was not forced, it's probably because the user clicked
334 // the close button, or Close(false) was called which (see above) is
335 // redirected to performClose and thus Cocoa itself will close the window
338 // ----------------------------------------------------------------------------
339 // wxTopLevelWindowCocoa misc
340 // ----------------------------------------------------------------------------
342 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
344 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
347 wxString wxTopLevelWindowCocoa::GetTitle() const
349 return wxStringWithNSString([m_cocoaNSWindow title]);
352 wxWindow* wxTopLevelWindowCocoa::SetDefaultItem(wxWindow *win)
354 wxWindow *old = wxTopLevelWindowBase::SetDefaultItem(win);
355 NSView *newView = win->GetNSView();
358 // newView does not have to be an NSControl, we only cast to NSControl*
359 // to silence the warning about cell not being implemented.
360 if(newView != nil && [newView respondsToSelector:@selector(cell)])
361 newCell = [(NSControl*)newView cell];
365 if(newCell != nil && ![newCell isKindOfClass:[NSButtonCell class]])
366 { // It's not an NSButtonCell, set the default to nil.
370 [GetNSWindow() setDefaultButtonCell:(NSButtonCell*)newCell];
374 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
379 bool wxTopLevelWindowCocoa::IsFullScreen() const
384 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
386 // Set the NSView size by setting the frame size to enclose it
387 unsigned int styleMask = [m_cocoaNSWindow styleMask];
388 NSRect oldFrameRect = [m_cocoaNSWindow frame];
389 NSRect contentRect = [NSWindow
390 contentRectForFrameRect: oldFrameRect
391 styleMask: styleMask];
392 contentRect.size.width = width;
393 contentRect.size.height = height;
394 NSRect newFrameRect = [NSWindow
395 frameRectForContentRect: contentRect
396 styleMask: styleMask];
398 // Cocoa uses +y is up but wxWidgets uses +y is down. We want an increase/decrease in height
399 // to not effect where the top of the window is placed so we set the new y origin relative the
400 // old one taking the height change into account.
401 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
403 [m_cocoaNSWindow setFrame: newFrameRect display: NO];
406 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
408 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
410 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,width,height,false);
412 NSRect cocoaRect = NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,width,height);
413 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
416 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
418 NSRect cocoaRect = [m_cocoaNSWindow frame];
420 *w=(int)cocoaRect.size.width;
422 *h=(int)cocoaRect.size.height;
423 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
426 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
428 NSRect windowFrame = [m_cocoaNSWindow frame];
430 wxPoint theWxOrigin = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(windowFrame);
437 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)theWxOrigin.x,(int)theWxOrigin.y);