]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/toplevel.mm
preliminary implementation of wxEVT_MENU_OPEN/CLOSE for Mac
[wxWidgets.git] / src / cocoa / toplevel.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/toplevel.mm
3 // Purpose: implements wxTopLevelWindow for Cocoa
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/11/27
7 // RCS-ID: $Id:
8 // Copyright: (c) 2002 David Elliott
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22 #ifndef WX_PRECOMP
23 #include "wx/window.h"
24 #include "wx/toplevel.h"
25 #include "wx/menuitem.h"
26 #include "wx/frame.h"
27 #include "wx/log.h"
28 #include "wx/app.h"
29 #endif //WX_PRECOMP
30
31 #import <AppKit/NSWindow.h>
32 // ----------------------------------------------------------------------------
33 // globals
34 // ----------------------------------------------------------------------------
35
36 // list of all frames and modeless dialogs
37 wxWindowList wxModelessWindows;
38
39 // ============================================================================
40 // wxTopLevelWindowCocoa implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // wxTopLevelWindowCocoa creation
45 // ----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
47 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
48 END_EVENT_TABLE()
49
50 void wxTopLevelWindowCocoa::Init()
51 {
52 m_iconized =
53 m_maximizeOnShow =
54 m_closed = false;
55 }
56
57 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
58 wxWindowID winid,
59 const wxString& title,
60 const wxPoint& pos,
61 const wxSize& size,
62 long style,
63 const wxString& name)
64 {
65 wxTopLevelWindows.Append(this);
66
67 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
68 return FALSE;
69
70 if ( parent )
71 parent->AddChild(this);
72
73 // TODO: get rect from given position/size
74 NSRect cocoaRect = NSMakeRect(100,100,200,200);
75
76 // TODO: Set flags given wxWindows style
77 unsigned int cocoaStyle = 0;
78 cocoaStyle |= NSTitledWindowMask;
79 cocoaStyle |= NSClosableWindowMask;
80 cocoaStyle |= NSMiniaturizableWindowMask;
81 cocoaStyle |= NSResizableWindowMask;
82
83 m_cocoaNSWindow = NULL;
84 m_cocoaNSView = 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];
90
91 return TRUE;
92 }
93
94 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
95 {
96 SetNSWindow(NULL);
97 }
98
99 // ----------------------------------------------------------------------------
100 // wxTopLevelWindowCocoa Cocoa Specifics
101 // ----------------------------------------------------------------------------
102
103 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
104 {
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]);
107 if(m_cocoaNSWindow)
108 DisassociateNSWindow(m_cocoaNSWindow);
109 [cocoaNSWindow retain];
110 [m_cocoaNSWindow release];
111 m_cocoaNSWindow = cocoaNSWindow;
112 if(m_cocoaNSWindow)
113 SetNSView([m_cocoaNSWindow contentView]);
114 else
115 SetNSView(NULL);
116 if(m_cocoaNSWindow)
117 AssociateNSWindow(m_cocoaNSWindow);
118 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
119 }
120
121 void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item)
122 {
123 }
124
125 void wxTopLevelWindowCocoa::Cocoa_close(void)
126 {
127 m_closed = true;
128 Destroy();
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();
133 }
134
135 bool wxTopLevelWindowCocoa::Cocoa_windowShouldClose()
136 {
137 return wxWindowBase::Close(false);
138 }
139
140 // ----------------------------------------------------------------------------
141 // wxTopLevelWindowCocoa maximize/minimize
142 // ----------------------------------------------------------------------------
143
144 void wxTopLevelWindowCocoa::Maximize(bool maximize)
145 {
146 }
147
148 bool wxTopLevelWindowCocoa::IsMaximized() const
149 {
150 return false ;
151 }
152
153 void wxTopLevelWindowCocoa::Iconize(bool iconize)
154 {
155 }
156
157 bool wxTopLevelWindowCocoa::IsIconized() const
158 {
159 return FALSE;
160 }
161
162 void wxTopLevelWindowCocoa::Restore()
163 {
164 }
165
166 bool wxTopLevelWindowCocoa::Show(bool show)
167 {
168 if(show)
169 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
170 else
171 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
172 return true;
173 }
174
175 bool wxTopLevelWindowCocoa::Close(bool force)
176 {
177 if(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];
186 return m_closed;
187 }
188
189 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
190 {
191 // If the event was forced, close the window which will Destroy() it
192 if(!event.CanVeto())
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
197 }
198
199 // ----------------------------------------------------------------------------
200 // wxTopLevelWindowCocoa misc
201 // ----------------------------------------------------------------------------
202
203 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
204 {
205 return FALSE;
206 }
207
208 bool wxTopLevelWindowCocoa::IsFullScreen() const
209 {
210 return FALSE;
211 }
212
213 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
214 {
215 wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
216
217 NSRect cocoaRect = NSMakeRect(x,y,width,height);
218 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
219 }
220
221 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
222 {
223 NSRect cocoaRect = [m_cocoaNSWindow frame];
224 if(w)
225 *w=(int)cocoaRect.size.width;
226 if(h)
227 *h=(int)cocoaRect.size.height;
228 wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
229 }
230
231 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
232 {
233 NSRect cocoaRect = [m_cocoaNSWindow frame];
234 if(x)
235 *x=(int)cocoaRect.origin.x;
236 if(y)
237 *y=(int)cocoaRect.origin.y;
238 wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
239 }
240