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