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 #import <AppKit/NSWindow.h>
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // list of all frames and modeless dialogs
37 wxWindowList wxModelessWindows;
39 // ============================================================================
40 // wxTopLevelWindowCocoa implementation
41 // ============================================================================
43 // ----------------------------------------------------------------------------
44 // wxTopLevelWindowCocoa creation
45 // ----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
47 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
50 void wxTopLevelWindowCocoa::Init()
57 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
59 const wxString& title,
65 wxTopLevelWindows.Append(this);
67 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
71 parent->AddChild(this);
73 // TODO: get rect from given position/size
74 NSRect cocoaRect = NSMakeRect(100,100,200,200);
76 // TODO: Set flags given wxWindows style
77 unsigned int cocoaStyle = 0;
78 cocoaStyle |= NSTitledWindowMask;
79 cocoaStyle |= NSClosableWindowMask;
80 cocoaStyle |= NSMiniaturizableWindowMask;
81 cocoaStyle |= NSResizableWindowMask;
83 m_cocoaNSWindow = NULL;
85 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
86 // NOTE: SetNSWindow has retained the Cocoa object for this object.
87 // Because we do not release on close, the following release matches the
88 // above alloc and thus the retain count will be 1.
89 [m_cocoaNSWindow release];
94 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
99 // ----------------------------------------------------------------------------
100 // wxTopLevelWindowCocoa Cocoa Specifics
101 // ----------------------------------------------------------------------------
103 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
105 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
106 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d",this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
108 DisassociateNSWindow(m_cocoaNSWindow);
109 [cocoaNSWindow retain];
110 [m_cocoaNSWindow release];
111 m_cocoaNSWindow = cocoaNSWindow;
113 SetNSView([m_cocoaNSWindow contentView]);
117 AssociateNSWindow(m_cocoaNSWindow);
118 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
121 void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item)
125 void wxTopLevelWindowCocoa::Cocoa_close(void)
129 /* Be SURE that idle events get ran. If the window was not active when
130 it was closed, then there will be no more events to trigger this and
131 therefore it must be done here */
132 wxTheApp->CocoaInstallRequestedIdleHandler();
135 bool wxTopLevelWindowCocoa::Cocoa_windowShouldClose()
137 return wxWindowBase::Close(false);
140 // ----------------------------------------------------------------------------
141 // wxTopLevelWindowCocoa maximize/minimize
142 // ----------------------------------------------------------------------------
144 void wxTopLevelWindowCocoa::Maximize(bool maximize)
148 bool wxTopLevelWindowCocoa::IsMaximized() const
153 void wxTopLevelWindowCocoa::Iconize(bool iconize)
157 bool wxTopLevelWindowCocoa::IsIconized() const
162 void wxTopLevelWindowCocoa::Restore()
166 bool wxTopLevelWindowCocoa::Show(bool show)
169 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
171 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
175 bool wxTopLevelWindowCocoa::Close(bool force)
178 return wxWindowBase::Close(force);
179 // performClose will fake the user clicking the close button which
180 // will invoke windowShouldClose which will call the base class version
181 // of Close() which will NOT Destroy() the window (see below) but
182 // if closing is not stopped, then performClose will go ahead and
183 // close the window which will invoke Cocoa_close() setting m_closed
184 // to true and Destroy()ing the window.
185 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
189 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
191 // If the event was forced, close the window which will Destroy() it
193 [m_cocoaNSWindow close];
194 // if the event was not forced, it's probably because the user clicked
195 // the close button, or Close(false) was called which (see above) is
196 // redirected to performClose and thus Cocoa itself will close the window
199 // ----------------------------------------------------------------------------
200 // wxTopLevelWindowCocoa misc
201 // ----------------------------------------------------------------------------
203 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
208 bool wxTopLevelWindowCocoa::IsFullScreen() const
213 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
215 wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
217 NSRect cocoaRect = NSMakeRect(x,y,width,height);
218 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
221 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
223 NSRect cocoaRect = [m_cocoaNSWindow frame];
225 *w=(int)cocoaRect.size.width;
227 *h=(int)cocoaRect.size.height;
228 wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
231 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
233 NSRect cocoaRect = [m_cocoaNSWindow frame];
235 *x=(int)cocoaRect.origin.x;
237 *y=(int)cocoaRect.origin.y;
238 wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);