]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/frame.cpp
make sure modality is reflected correctly when rerouted from window-modal to app...
[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
34IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
35
36#define WX_MAC_STATUSBAR_HEIGHT 18
37
38// ----------------------------------------------------------------------------
39// creation/destruction
40// ----------------------------------------------------------------------------
41
42void wxFrame::Init()
43{
44 m_winLastFocused = NULL;
45}
46
47bool 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
62wxFrame::~wxFrame()
63{
c6212a0c
VZ
64 SendDestroyEvent();
65
489468fe
SC
66 DeleteAllBars();
67}
68
69// get the origin of the client area in the client coordinates
70wxPoint 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
85c04b8f 81 if ( toolbar->HasFlag(wxTB_LEFT) )
489468fe
SC
82 {
83 pt.x += w;
84 }
85c04b8f 85 else if ( HasFlag(wxTB_TOP) )
489468fe 86 {
292e5e1f 87#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
88 pt.y += h;
89#endif
90 }
91 }
92#endif
93
94 return pt;
95}
96
97bool wxFrame::Enable(bool enable)
98{
99 if ( !wxWindow::Enable(enable) )
100 return false;
101
524c47aa 102#if wxUSE_MENUS
fc7ba044
SC
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()*/)
489468fe
SC
106 {
107 int iMaxMenu = m_frameMenuBar->GetMenuCount();
108 for ( int i = 0 ; i < iMaxMenu ; ++ i )
109 {
110 m_frameMenuBar->EnableTop( i , enable ) ;
111 }
112 }
524c47aa 113#endif
489468fe
SC
114 return true;
115}
116
117#if wxUSE_STATUSBAR
118wxStatusBar *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
130void 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.
145void 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 );
ac17f728 156 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
489468fe
SC
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.
166void 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
524c47aa 202#if wxUSE_MENUS
489468fe
SC
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 }
524c47aa 217#endif
489468fe
SC
218 }
219}
220
e0e2cbbe 221void wxFrame::HandleResized( double timestampsec )
524c47aa
SC
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
e0e2cbbe 228 wxNonOwnedWindow::HandleResized( timestampsec );
524c47aa
SC
229}
230
231#if wxUSE_MENUS
489468fe
SC
232void wxFrame::DetachMenuBar()
233{
234 if ( m_frameMenuBar )
235 m_frameMenuBar->UnsetInvokingWindow();
236
237 wxFrameBase::DetachMenuBar();
238}
239
240void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
241{
524c47aa 242#if wxOSX_USE_CARBON
b2680ced 243 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame );
524c47aa
SC
244#else
245 wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow();
246#endif
489468fe
SC
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}
524c47aa 265#endif
489468fe
SC
266
267void 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 {
292e5e1f 290#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
291 if ( y )
292 *y -= h;
293#endif
294 }
295 }
296#endif
297}
298
299bool 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
314void 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
335void wxFrame::SetToolBar(wxToolBar *toolbar)
336{
337 if ( m_frameToolBar == toolbar )
338 return ;
339
85c04b8f 340#ifndef __WXOSX_IPHONE__
292e5e1f 341#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
342 if ( m_frameToolBar )
343 m_frameToolBar->MacInstallNativeToolbar( false ) ;
344#endif
85c04b8f 345#endif
489468fe
SC
346 m_frameToolBar = toolbar ;
347
85c04b8f 348#ifndef __WXOSX_IPHONE__
292e5e1f 349#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
350 if ( toolbar )
351 toolbar->MacInstallNativeToolbar( true ) ;
352#endif
85c04b8f 353#endif
489468fe
SC
354}
355
356wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
357{
358 if ( wxFrameBase::CreateToolBar(style, id, name) )
359 PositionToolBar();
360
361 return m_frameToolBar;
362}
363
364void wxFrame::PositionToolBar()
365{
366 int cw, ch;
367
368 GetSize( &cw , &ch ) ;
c6212a0c 369
489468fe
SC
370 int statusX = 0 ;
371 int statusY = 0 ;
372
373#if wxUSE_STATUSBAR
374 if (GetStatusBar() && GetStatusBar()->IsShown())
375 {
376 GetStatusBar()->GetClientSize(&statusX, &statusY);
377 ch -= statusY;
378 }
379#endif
380
85c04b8f
SC
381#ifdef __WXOSX_IPHONE__
382 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
383 // but the toolbar is
384 ch -= 20;
385#endif
386
489468fe
SC
387 if (GetToolBar())
388 {
389 int tx, ty, tw, th;
390
391 tx = ty = 0 ;
392 GetToolBar()->GetSize(&tw, &th);
393 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
394 {
395 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
396 // means, pretend we don't have toolbar/status bar, so we
397 // have the original client size.
398 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
399 }
400 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM)
401 {
402 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
403 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
404 //doesn't extend it's width to the width of the frame.
405 tx = 0;
406 ty = ch - (th + statusY);
407 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
408 }
409 else
410 {
292e5e1f 411#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
412 // Use the 'real' position
413 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
414#endif
415 }
416 }
417}
418#endif // wxUSE_TOOLBAR
419
420void wxFrame::PositionBars()
421{
422#if wxUSE_STATUSBAR
423 PositionStatusBar();
424#endif
425#if wxUSE_TOOLBAR
426 PositionToolBar();
427#endif
428}
429
430