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 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
91 const wxString& title,
97 wxAutoNSAutoreleasePool pool;
98 wxTopLevelWindows.Append(this);
100 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
104 parent->AddChild(this);
106 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
107 if(style & wxFRAME_TOOL_WINDOW)
108 cocoaStyle |= NSUtilityWindowMask;
110 // Create frame and check and handle default position and size
114 // WX has no set default position - the carbon port caps the low
115 // end at 20, 50. Here we do the same, except instead of setting
116 // it to 20 and 50, we set it to 100 and 100 if the values are too low
127 int realw = WidthDefault(size.x);
128 int realh = HeightDefault(size.y);
130 // NOTE: y-origin needs to be flipped.
131 NSRect cocoaRect = [NSWindow
132 contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh)
133 styleMask:cocoaStyle];
135 m_cocoaNSWindow = NULL;
136 m_cocoaNSView = NULL;
137 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
139 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
140 if(style & wxFRAME_TOOL_WINDOW)
141 newWindow = [[WXNSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
143 newWindow = [[WXNSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
144 // Make sure the default content view is a WXNSView
145 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
146 // Associate the window and view
147 SetNSWindow(newWindow);
149 // NOTE: SetNSWindow has retained the Cocoa object for this object.
150 // Because we do not release on close, the following release matches the
151 // above alloc and thus the retain count will be 1.
152 [m_cocoaNSWindow release];
154 if(style & wxFRAME_NO_TASKBAR)
155 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
156 if(style & wxSTAY_ON_TOP)
157 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
158 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
162 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
164 wxASSERT(sm_cocoaDeactivateWindow!=this);
165 wxAutoNSAutoreleasePool pool;
172 bool wxTopLevelWindowCocoa::Destroy()
174 if(sm_cocoaDeactivateWindow==this)
176 sm_cocoaDeactivateWindow = NULL;
177 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
179 return wxTopLevelWindowBase::Destroy();
182 // ----------------------------------------------------------------------------
183 // wxTopLevelWindowCocoa Cocoa Specifics
184 // ----------------------------------------------------------------------------
186 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
188 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
190 return parent->GetAppMenuBar(win);
194 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
196 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
197 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
198 DisassociateNSWindow(m_cocoaNSWindow);
199 [cocoaNSWindow retain];
200 [m_cocoaNSWindow release];
201 m_cocoaNSWindow = cocoaNSWindow;
202 // NOTE: We are no longer using posing so we won't get events on the
203 // window's view unless it was explicitly created as the wx view class.
205 SetNSView([m_cocoaNSWindow contentView]);
208 AssociateNSWindow(m_cocoaNSWindow);
209 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
212 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
214 if([m_cocoaNSWindow contentView] == (id)oldView)
215 [m_cocoaNSWindow setContentView:newView];
218 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
220 if(sm_cocoaDeactivateWindow)
221 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
222 sm_cocoaDeactivateWindow = NULL;
225 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
227 DeactivatePendingWindow();
228 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
229 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
230 event.SetEventObject(this);
231 GetEventHandler()->ProcessEvent(event);
234 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
236 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
237 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
238 event.SetEventObject(this);
239 GetEventHandler()->ProcessEvent(event);
242 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
244 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
247 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
249 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
252 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
258 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
260 return wxWindowBase::Close(false);
263 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
267 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
272 // ----------------------------------------------------------------------------
273 // wxTopLevelWindowCocoa maximize/minimize
274 // ----------------------------------------------------------------------------
276 void wxTopLevelWindowCocoa::Maximize(bool maximize)
280 bool wxTopLevelWindowCocoa::IsMaximized() const
285 void wxTopLevelWindowCocoa::Iconize(bool iconize)
289 bool wxTopLevelWindowCocoa::IsIconized() const
294 void wxTopLevelWindowCocoa::Restore()
298 bool wxTopLevelWindowCocoa::Show(bool show)
300 if(m_isShown == show)
302 wxAutoNSAutoreleasePool pool;
305 // Send the window a size event because wxWidgets apps expect it
306 // NOTE: This should really only be done the first time a window
307 // is shown. I doubt this will cause any problems though.
308 wxSizeEvent event(GetSize(), GetId());
309 event.SetEventObject(this);
310 GetEventHandler()->ProcessEvent(event);
312 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
315 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
320 bool wxTopLevelWindowCocoa::Close(bool force)
323 return wxWindowBase::Close(force);
324 // performClose will fake the user clicking the close button which
325 // will invoke windowShouldClose which will call the base class version
326 // of Close() which will NOT Destroy() the window (see below) but
327 // if closing is not stopped, then performClose will go ahead and
328 // close the window which will send the close notifications setting
329 // m_closed to true and Destroy()ing the window.
330 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
334 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
336 // If the event was forced, close the window which will Destroy() it
338 [m_cocoaNSWindow close];
339 // if the event was not forced, it's probably because the user clicked
340 // the close button, or Close(false) was called which (see above) is
341 // redirected to performClose and thus Cocoa itself will close the window
344 // ----------------------------------------------------------------------------
345 // wxTopLevelWindowCocoa misc
346 // ----------------------------------------------------------------------------
348 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
350 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
353 wxString wxTopLevelWindowCocoa::GetTitle() const
355 return wxStringWithNSString([m_cocoaNSWindow title]);
358 wxWindow* wxTopLevelWindowCocoa::SetDefaultItem(wxWindow *win)
360 wxWindow *old = wxTopLevelWindowBase::SetDefaultItem(win);
361 NSView *newView = win->GetNSView();
364 // newView does not have to be an NSControl, we only cast to NSControl*
365 // to silence the warning about cell not being implemented.
366 if(newView != nil && [newView respondsToSelector:@selector(cell)])
367 newCell = [(NSControl*)newView cell];
371 if(newCell != nil && ![newCell isKindOfClass:[NSButtonCell class]])
372 { // It's not an NSButtonCell, set the default to nil.
376 [GetNSWindow() setDefaultButtonCell:(NSButtonCell*)newCell];
380 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
385 bool wxTopLevelWindowCocoa::IsFullScreen() const
390 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
392 // Set the NSView size by setting the frame size to enclose it
393 unsigned int styleMask = [m_cocoaNSWindow styleMask];
394 NSRect frameRect = [m_cocoaNSWindow frame];
395 NSRect contentRect = [NSWindow
396 contentRectForFrameRect: frameRect
397 styleMask: styleMask];
398 contentRect.size.width = width;
399 contentRect.size.height = height;
400 frameRect = [NSWindow
401 frameRectForContentRect: contentRect
402 styleMask: styleMask];
403 [m_cocoaNSWindow setFrame: frameRect 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 NSRect cocoaRect = NSMakeRect(x,y,width,height);
411 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
414 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
416 NSRect cocoaRect = [m_cocoaNSWindow frame];
418 *w=(int)cocoaRect.size.width;
420 *h=(int)cocoaRect.size.height;
421 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
424 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
426 NSRect cocoaRect = [m_cocoaNSWindow frame];
428 *x=(int)cocoaRect.origin.x;
430 *y=(int)cocoaRect.origin.y;
431 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);