1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/frame.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/dcclient.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"
27 #include "wx/osx/private.h"
29 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
30 EVT_ACTIVATE(wxFrame::OnActivate
)
31 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
32 EVT_SIZE(wxFrame::OnSize
)
35 #define WX_MAC_STATUSBAR_HEIGHT 18
37 // ----------------------------------------------------------------------------
38 // creation/destruction
39 // ----------------------------------------------------------------------------
43 m_winLastFocused
= NULL
;
46 bool wxFrame::Create(wxWindow
*parent
,
48 const wxString
& title
,
55 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
68 // get the origin of the client area in the client coordinates
69 wxPoint
wxFrame::GetClientAreaOrigin() const
71 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
73 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
74 wxToolBar
*toolbar
= GetToolBar();
75 if ( toolbar
&& toolbar
->IsShown() )
78 toolbar
->GetSize(&w
, &h
);
80 if ( toolbar
->HasFlag(wxTB_LEFT
) )
84 else if ( toolbar
->HasFlag(wxTB_TOP
) )
86 #if !wxOSX_USE_NATIVE_TOOLBAR
96 bool wxFrame::Enable(bool enable
)
98 if ( !wxWindow::Enable(enable
) )
102 // we should always enable/disable the menubar, even if we are not current, otherwise
103 // we might miss some state change later (happened eg in the docview sample after PrintPreview)
104 if ( m_frameMenuBar
/*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/)
106 int iMaxMenu
= m_frameMenuBar
->GetMenuCount();
107 for ( int i
= 0 ; i
< iMaxMenu
; ++ i
)
109 m_frameMenuBar
->EnableTop( i
, enable
) ;
117 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
118 const wxString
& name
)
120 wxStatusBar
*statusBar
;
122 statusBar
= new wxStatusBar(this, id
, style
, name
);
123 statusBar
->SetSize(100, WX_MAC_STATUSBAR_HEIGHT
);
124 statusBar
->SetFieldsCount(number
);
129 void wxFrame::PositionStatusBar()
131 if (m_frameStatusBar
&& m_frameStatusBar
->IsShown() )
134 GetClientSize(&w
, &h
);
136 // Since we wish the status bar to be directly under the client area,
137 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
138 m_frameStatusBar
->SetSize(0, h
, w
, WX_MAC_STATUSBAR_HEIGHT
);
141 #endif // wxUSE_STATUSBAR
143 // Responds to colour changes, and passes event on to children.
144 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
146 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
150 if ( m_frameStatusBar
)
152 wxSysColourChangedEvent event2
;
154 event2
.SetEventObject( m_frameStatusBar
);
155 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
157 #endif // wxUSE_STATUSBAR
159 // Propagate the event to the non-top-level children
160 wxWindow::OnSysColourChanged(event
);
163 // Default activation behaviour - set the focus for the first child
165 void wxFrame::OnActivate(wxActivateEvent
& event
)
167 if ( !event
.GetActive() )
169 // remember the last focused child if it is our child
170 m_winLastFocused
= FindFocus();
172 // so we NULL it out if it's a child from some other frame
173 wxWindow
*win
= m_winLastFocused
;
176 if ( win
->IsTopLevel() )
179 m_winLastFocused
= NULL
;
184 win
= win
->GetParent();
191 // restore focus to the child which was last focused
192 wxWindow
*parent
= m_winLastFocused
193 ? m_winLastFocused
->GetParent()
199 wxSetFocusToChild(parent
, &m_winLastFocused
);
202 if (m_frameMenuBar
!= NULL
)
204 m_frameMenuBar
->MacInstallMenuBar();
208 wxFrame
*tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
211 // Trying top-level frame membar
212 if (tlf
->GetMenuBar())
213 tlf
->GetMenuBar()->MacInstallMenuBar();
221 void wxFrame::OnSize(wxSizeEvent
& event
)
229 void wxFrame::DetachMenuBar()
231 wxFrameBase::DetachMenuBar();
234 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
237 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
239 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
241 bool makeCurrent
= false;
243 // if this is already the current menubar or we are the frontmost window
244 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
246 // or there is an app-level menubar like MDI
247 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
250 wxFrameBase::AttachMenuBar( menuBar
);
255 m_frameMenuBar
->MacInstallMenuBar();
260 void wxFrame::DoGetClientSize(int *x
, int *y
) const
262 wxTopLevelWindow::DoGetClientSize( x
, y
);
265 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
266 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
270 wxToolBar
*toolbar
= GetToolBar();
271 if ( toolbar
&& toolbar
->IsShown() )
274 toolbar
->GetSize(&w
, &h
);
276 if ( toolbar
->IsVertical() )
281 else if ( toolbar
->HasFlag( wxTB_BOTTOM
) )
288 #if !wxOSX_USE_NATIVE_TOOLBAR
297 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
300 if ( child
== GetStatusBar() )
305 if ( child
== GetToolBar() )
309 return wxFrameBase::MacIsChildOfClientArea( child
) ;
312 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
314 int currentclientwidth
, currentclientheight
;
315 int currentwidth
, currentheight
;
317 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
318 if ( clientwidth
== -1 )
319 clientwidth
= currentclientwidth
;
320 if ( clientheight
== -1 )
321 clientheight
= currentclientheight
;
322 GetSize( ¤twidth
, ¤theight
) ;
324 // find the current client size
326 // Find the difference between the entire window (title bar and all) and
327 // the client area; add this to the new client size to move the window
328 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
329 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
333 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
335 if ( m_frameToolBar
== toolbar
)
338 #ifndef __WXOSX_IPHONE__
339 #if wxOSX_USE_NATIVE_TOOLBAR
340 if ( m_frameToolBar
)
341 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
344 m_frameToolBar
= toolbar
;
346 #ifndef __WXOSX_IPHONE__
347 #if wxOSX_USE_NATIVE_TOOLBAR
349 toolbar
->MacInstallNativeToolbar( true ) ;
354 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
356 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
359 return m_frameToolBar
;
362 void wxFrame::PositionToolBar()
366 wxTopLevelWindow::DoGetClientSize( &cw
, &ch
);
372 if (GetStatusBar() && GetStatusBar()->IsShown())
374 GetStatusBar()->GetSize(&statusX
, &statusY
);
379 #ifdef __WXOSX_IPHONE__
380 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
381 // but the toolbar is
390 GetToolBar()->GetSize(&tw
, &th
);
391 if (GetToolBar()->HasFlag(wxTB_LEFT
))
393 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
394 // means, pretend we don't have toolbar/status bar, so we
395 // have the original client size.
396 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
398 else if (GetToolBar()->HasFlag(wxTB_RIGHT
))
400 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
401 // means, pretend we don't have toolbar/status bar, so we
402 // have the original client size.
404 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
406 else if (GetToolBar()->HasFlag(wxTB_BOTTOM
))
410 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
414 #if !wxOSX_USE_NATIVE_TOOLBAR
415 // Use the 'real' position
416 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
421 #endif // wxUSE_TOOLBAR
423 void wxFrame::PositionBars()