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