1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/toplevel.mm
3 // Purpose: implements wxTopLevelWindow for Cocoa
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // License: wxWindows license
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"
33 #import <AppKit/NSView.h>
34 #import <AppKit/NSWindow.h>
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // list of all frames and modeless dialogs
40 wxWindowList wxModelessWindows;
42 // ============================================================================
43 // wxTopLevelWindowCocoa implementation
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxTopLevelWindowCocoa creation
48 // ----------------------------------------------------------------------------
49 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
50 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
53 void wxTopLevelWindowCocoa::Init()
60 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
62 const wxString& title,
68 wxAutoNSAutoreleasePool pool;
69 wxTopLevelWindows.Append(this);
71 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
75 parent->AddChild(this);
77 // TODO: get rect from given position/size
78 NSRect cocoaRect = NSMakeRect(100,100,200,200);
80 // TODO: Set flags given wxWindows style
81 unsigned int cocoaStyle = 0;
82 cocoaStyle |= NSTitledWindowMask;
83 cocoaStyle |= NSClosableWindowMask;
84 cocoaStyle |= NSMiniaturizableWindowMask;
85 cocoaStyle |= NSResizableWindowMask;
87 m_cocoaNSWindow = NULL;
89 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
90 // NOTE: SetNSWindow has retained the Cocoa object for this object.
91 // Because we do not release on close, the following release matches the
92 // above alloc and thus the retain count will be 1.
93 [m_cocoaNSWindow release];
98 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
100 wxAutoNSAutoreleasePool pool;
105 // ----------------------------------------------------------------------------
106 // wxTopLevelWindowCocoa Cocoa Specifics
107 // ----------------------------------------------------------------------------
109 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
111 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
112 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d",this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
113 DisassociateNSWindow(m_cocoaNSWindow);
114 [cocoaNSWindow retain];
115 [m_cocoaNSWindow release];
116 m_cocoaNSWindow = cocoaNSWindow;
118 SetNSView([m_cocoaNSWindow contentView]);
121 AssociateNSWindow(m_cocoaNSWindow);
122 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
125 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
127 if([m_cocoaNSWindow contentView] == (id)oldView)
128 [m_cocoaNSWindow setContentView:newView];
131 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
133 wxLogDebug("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey",this);
134 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, GetId());
135 event.SetEventObject(this);
136 GetEventHandler()->ProcessEvent(event);
139 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
141 wxLogDebug("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey",this);
142 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
143 event.SetEventObject(this);
144 GetEventHandler()->ProcessEvent(event);
147 void wxTopLevelWindowCocoa::Cocoa_close(void)
151 /* Be SURE that idle events get ran. If the window was not active when
152 it was closed, then there will be no more events to trigger this and
153 therefore it must be done here */
154 wxTheApp->CocoaInstallRequestedIdleHandler();
157 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
159 return wxWindowBase::Close(false);
162 // ----------------------------------------------------------------------------
163 // wxTopLevelWindowCocoa maximize/minimize
164 // ----------------------------------------------------------------------------
166 void wxTopLevelWindowCocoa::Maximize(bool maximize)
170 bool wxTopLevelWindowCocoa::IsMaximized() const
175 void wxTopLevelWindowCocoa::Iconize(bool iconize)
179 bool wxTopLevelWindowCocoa::IsIconized() const
184 void wxTopLevelWindowCocoa::Restore()
188 bool wxTopLevelWindowCocoa::Show(bool show)
190 if(m_isShown == show)
192 wxAutoNSAutoreleasePool pool;
195 // Send the window a size event because wxWindows apps expect it
196 // NOTE: This should really only be done the first time a window
197 // is shown. I doubt this will cause any problems though.
198 wxSizeEvent event(GetSize(), GetId());
199 event.SetEventObject(this);
200 GetEventHandler()->ProcessEvent(event);
202 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
205 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
210 bool wxTopLevelWindowCocoa::Close(bool force)
213 return wxWindowBase::Close(force);
214 // performClose will fake the user clicking the close button which
215 // will invoke windowShouldClose which will call the base class version
216 // of Close() which will NOT Destroy() the window (see below) but
217 // if closing is not stopped, then performClose will go ahead and
218 // close the window which will invoke Cocoa_close() setting m_closed
219 // to true and Destroy()ing the window.
220 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
224 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
226 // If the event was forced, close the window which will Destroy() it
228 [m_cocoaNSWindow close];
229 // if the event was not forced, it's probably because the user clicked
230 // the close button, or Close(false) was called which (see above) is
231 // redirected to performClose and thus Cocoa itself will close the window
234 // ----------------------------------------------------------------------------
235 // wxTopLevelWindowCocoa misc
236 // ----------------------------------------------------------------------------
238 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
243 bool wxTopLevelWindowCocoa::IsFullScreen() const
248 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
250 // Set the NSView size by setting the frame size to enclose it
251 unsigned int styleMask = [m_cocoaNSWindow styleMask];
252 NSRect frameRect = [m_cocoaNSWindow frame];
253 NSRect contentRect = [NSWindow
254 contentRectForFrameRect: frameRect
255 styleMask: styleMask];
256 contentRect.size.width = width;
257 contentRect.size.height = height;
258 frameRect = [NSWindow
259 frameRectForContentRect: contentRect
260 styleMask: styleMask];
261 [m_cocoaNSWindow setFrame: frameRect display: NO];
264 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
266 // wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
268 NSRect cocoaRect = NSMakeRect(x,y,width,height);
269 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
272 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
274 NSRect cocoaRect = [m_cocoaNSWindow frame];
276 *w=(int)cocoaRect.size.width;
278 *h=(int)cocoaRect.size.height;
279 // wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
282 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
284 NSRect cocoaRect = [m_cocoaNSWindow frame];
286 *x=(int)cocoaRect.origin.x;
288 *y=(int)cocoaRect.origin.y;
289 // wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);