]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/frame.cpp
Create an autorelease pool to catch objects created during several special situations...
[wxWidgets.git] / src / osx / carbon / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/frame.cpp
3 // Purpose: wxFrame
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/frame.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/dcclient.h"
19 #include "wx/menu.h"
20 #include "wx/dialog.h"
21 #include "wx/settings.h"
22 #include "wx/toolbar.h"
23 #include "wx/statusbr.h"
24 #include "wx/menuitem.h"
25 #endif // WX_PRECOMP
26
27 #include "wx/osx/private.h"
28
29 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
30 EVT_ACTIVATE(wxFrame::OnActivate)
31 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
32 END_EVENT_TABLE()
33
34 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
35
36 #define WX_MAC_STATUSBAR_HEIGHT 18
37
38 // ----------------------------------------------------------------------------
39 // creation/destruction
40 // ----------------------------------------------------------------------------
41
42 void wxFrame::Init()
43 {
44 m_winLastFocused = NULL;
45 }
46
47 bool wxFrame::Create(wxWindow *parent,
48 wxWindowID id,
49 const wxString& title,
50 const wxPoint& pos,
51 const wxSize& size,
52 long style,
53 const wxString& name)
54 {
55
56 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
57 return false;
58
59 return true;
60 }
61
62 wxFrame::~wxFrame()
63 {
64 SendDestroyEvent();
65
66 DeleteAllBars();
67 }
68
69 // get the origin of the client area in the client coordinates
70 wxPoint wxFrame::GetClientAreaOrigin() const
71 {
72 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
73
74 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
75 wxToolBar *toolbar = GetToolBar();
76 if ( toolbar && toolbar->IsShown() )
77 {
78 int w, h;
79 toolbar->GetSize(&w, &h);
80
81 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
82 {
83 pt.x += w;
84 }
85 else
86 {
87 #if !wxOSX_USE_NATIVE_TOOLBAR
88 pt.y += h;
89 #endif
90 }
91 }
92 #endif
93
94 return pt;
95 }
96
97 bool wxFrame::Enable(bool enable)
98 {
99 if ( !wxWindow::Enable(enable) )
100 return false;
101
102 #if wxUSE_MENUS
103 // we should always enable/disable the menubar, even if we are not current, otherwise
104 // we might miss some state change later (happened eg in the docview sample after PrintPreview)
105 if ( m_frameMenuBar /*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/)
106 {
107 int iMaxMenu = m_frameMenuBar->GetMenuCount();
108 for ( int i = 0 ; i < iMaxMenu ; ++ i )
109 {
110 m_frameMenuBar->EnableTop( i , enable ) ;
111 }
112 }
113 #endif
114 return true;
115 }
116
117 #if wxUSE_STATUSBAR
118 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
119 const wxString& name)
120 {
121 wxStatusBar *statusBar;
122
123 statusBar = new wxStatusBar(this, id, style, name);
124 statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT);
125 statusBar->SetFieldsCount(number);
126
127 return statusBar;
128 }
129
130 void wxFrame::PositionStatusBar()
131 {
132 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
133 {
134 int w, h;
135 GetClientSize(&w, &h);
136
137 // Since we wish the status bar to be directly under the client area,
138 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
139 m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT);
140 }
141 }
142 #endif // wxUSE_STATUSBAR
143
144 // Responds to colour changes, and passes event on to children.
145 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
146 {
147 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
148 Refresh();
149
150 #if wxUSE_STATUSBAR
151 if ( m_frameStatusBar )
152 {
153 wxSysColourChangedEvent event2;
154
155 event2.SetEventObject( m_frameStatusBar );
156 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
157 }
158 #endif // wxUSE_STATUSBAR
159
160 // Propagate the event to the non-top-level children
161 wxWindow::OnSysColourChanged(event);
162 }
163
164 // Default activation behaviour - set the focus for the first child
165 // subwindow found.
166 void wxFrame::OnActivate(wxActivateEvent& event)
167 {
168 if ( !event.GetActive() )
169 {
170 // remember the last focused child if it is our child
171 m_winLastFocused = FindFocus();
172
173 // so we NULL it out if it's a child from some other frame
174 wxWindow *win = m_winLastFocused;
175 while ( win )
176 {
177 if ( win->IsTopLevel() )
178 {
179 if ( win != this )
180 m_winLastFocused = NULL;
181
182 break;
183 }
184
185 win = win->GetParent();
186 }
187
188 event.Skip();
189 }
190 else
191 {
192 // restore focus to the child which was last focused
193 wxWindow *parent = m_winLastFocused
194 ? m_winLastFocused->GetParent()
195 : NULL;
196
197 if (parent == NULL)
198 parent = this;
199
200 wxSetFocusToChild(parent, &m_winLastFocused);
201
202 #if wxUSE_MENUS
203 if (m_frameMenuBar != NULL)
204 {
205 m_frameMenuBar->MacInstallMenuBar();
206 }
207 else
208 {
209 wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame );
210 if (tlf != NULL)
211 {
212 // Trying top-level frame membar
213 if (tlf->GetMenuBar())
214 tlf->GetMenuBar()->MacInstallMenuBar();
215 }
216 }
217 #endif
218 }
219 }
220
221 void wxFrame::HandleResized( double timestampsec )
222 {
223 // according to the other ports we handle this within the OS level
224 // resize event, not within a wxSizeEvent
225
226 PositionBars();
227
228 wxNonOwnedWindow::HandleResized( timestampsec );
229 }
230
231 #if wxUSE_MENUS
232 void wxFrame::DetachMenuBar()
233 {
234 if ( m_frameMenuBar )
235 m_frameMenuBar->UnsetInvokingWindow();
236
237 wxFrameBase::DetachMenuBar();
238 }
239
240 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
241 {
242 #if wxOSX_USE_CARBON
243 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame );
244 #else
245 wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow();
246 #endif
247 bool makeCurrent = false;
248
249 // if this is already the current menubar or we are the frontmost window
250 if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) )
251 makeCurrent = true;
252 // or there is an app-level menubar like MDI
253 else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) )
254 makeCurrent = true;
255
256 wxFrameBase::AttachMenuBar( menuBar );
257
258 if (m_frameMenuBar)
259 {
260 m_frameMenuBar->SetInvokingWindow( this );
261 if (makeCurrent)
262 m_frameMenuBar->MacInstallMenuBar();
263 }
264 }
265 #endif
266
267 void wxFrame::DoGetClientSize(int *x, int *y) const
268 {
269 wxTopLevelWindow::DoGetClientSize( x , y );
270
271 #if wxUSE_STATUSBAR
272 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
273 *y -= WX_MAC_STATUSBAR_HEIGHT;
274 #endif
275
276 #if wxUSE_TOOLBAR
277 wxToolBar *toolbar = GetToolBar();
278 if ( toolbar && toolbar->IsShown() )
279 {
280 int w, h;
281 toolbar->GetSize(&w, &h);
282
283 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
284 {
285 if ( x )
286 *x -= w;
287 }
288 else
289 {
290 #if !wxOSX_USE_NATIVE_TOOLBAR
291 if ( y )
292 *y -= h;
293 #endif
294 }
295 }
296 #endif
297 }
298
299 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
300 {
301 #if wxUSE_STATUSBAR
302 if ( child == GetStatusBar() )
303 return false ;
304 #endif
305
306 #if wxUSE_TOOLBAR
307 if ( child == GetToolBar() )
308 return false ;
309 #endif
310
311 return wxFrameBase::MacIsChildOfClientArea( child ) ;
312 }
313
314 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
315 {
316 int currentclientwidth , currentclientheight ;
317 int currentwidth , currentheight ;
318
319 GetClientSize( &currentclientwidth , &currentclientheight ) ;
320 if ( clientwidth == -1 )
321 clientwidth = currentclientwidth ;
322 if ( clientheight == -1 )
323 clientheight = currentclientheight ;
324 GetSize( &currentwidth , &currentheight ) ;
325
326 // find the current client size
327
328 // Find the difference between the entire window (title bar and all) and
329 // the client area; add this to the new client size to move the window
330 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
331 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
332 }
333
334 #if wxUSE_TOOLBAR
335 void wxFrame::SetToolBar(wxToolBar *toolbar)
336 {
337 if ( m_frameToolBar == toolbar )
338 return ;
339
340 #if wxOSX_USE_NATIVE_TOOLBAR
341 if ( m_frameToolBar )
342 m_frameToolBar->MacInstallNativeToolbar( false ) ;
343 #endif
344
345 m_frameToolBar = toolbar ;
346
347 #if wxOSX_USE_NATIVE_TOOLBAR
348 if ( toolbar )
349 toolbar->MacInstallNativeToolbar( true ) ;
350 #endif
351 }
352
353 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
354 {
355 if ( wxFrameBase::CreateToolBar(style, id, name) )
356 PositionToolBar();
357
358 return m_frameToolBar;
359 }
360
361 void wxFrame::PositionToolBar()
362 {
363 int cw, ch;
364
365 GetSize( &cw , &ch ) ;
366
367 int statusX = 0 ;
368 int statusY = 0 ;
369
370 #if wxUSE_STATUSBAR
371 if (GetStatusBar() && GetStatusBar()->IsShown())
372 {
373 GetStatusBar()->GetClientSize(&statusX, &statusY);
374 ch -= statusY;
375 }
376 #endif
377
378 if (GetToolBar())
379 {
380 int tx, ty, tw, th;
381
382 tx = ty = 0 ;
383 GetToolBar()->GetSize(&tw, &th);
384 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
385 {
386 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
387 // means, pretend we don't have toolbar/status bar, so we
388 // have the original client size.
389 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
390 }
391 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM)
392 {
393 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
394 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
395 //doesn't extend it's width to the width of the frame.
396 tx = 0;
397 ty = ch - (th + statusY);
398 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
399 }
400 else
401 {
402 #if !wxOSX_USE_NATIVE_TOOLBAR
403 // Use the 'real' position
404 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
405 #endif
406 }
407 }
408 }
409 #endif // wxUSE_TOOLBAR
410
411 void wxFrame::PositionBars()
412 {
413 #if wxUSE_STATUSBAR
414 PositionStatusBar();
415 #endif
416 #if wxUSE_TOOLBAR
417 PositionToolBar();
418 #endif
419 }
420
421