1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/frame.cpp
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/dcclient.h"
19 #include "wx/dialog.h"
20 #include "wx/settings.h"
21 #include "wx/toolbar.h"
22 #include "wx/statusbr.h"
23 #include "wx/menuitem.h"
26 #include "wx/osx/private.h"
28 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
29 EVT_ACTIVATE(wxFrame::OnActivate
)
30 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
33 #define WX_MAC_STATUSBAR_HEIGHT 18
35 // ----------------------------------------------------------------------------
36 // creation/destruction
37 // ----------------------------------------------------------------------------
41 m_winLastFocused
= NULL
;
44 bool wxFrame::Create(wxWindow
*parent
,
46 const wxString
& title
,
52 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
65 // get the origin of the client area in the client coordinates
66 wxPoint
wxFrame::GetClientAreaOrigin() const
68 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
70 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
71 wxToolBar
*toolbar
= GetToolBar();
72 if ( toolbar
&& toolbar
->IsShown() )
75 toolbar
->GetSize(&w
, &h
);
77 if ( toolbar
->HasFlag(wxTB_LEFT
) )
81 else if ( toolbar
->HasFlag(wxTB_TOP
) )
83 #if !wxOSX_USE_NATIVE_TOOLBAR
93 bool wxFrame::Enable(bool enable
)
95 if ( !wxWindow::Enable(enable
) )
99 // we should always enable/disable the menubar, even if we are not current, otherwise
100 // we might miss some state change later (happened eg in the docview sample after PrintPreview)
101 if ( m_frameMenuBar
/*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/)
103 int iMaxMenu
= m_frameMenuBar
->GetMenuCount();
104 for ( int i
= 0 ; i
< iMaxMenu
; ++ i
)
106 m_frameMenuBar
->EnableTop( i
, enable
) ;
114 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
115 const wxString
& name
)
117 wxStatusBar
*statusBar
;
119 statusBar
= new wxStatusBar(this, id
, style
, name
);
120 statusBar
->SetSize(100, WX_MAC_STATUSBAR_HEIGHT
);
121 statusBar
->SetFieldsCount(number
);
126 void wxFrame::PositionStatusBar()
128 if (m_frameStatusBar
&& m_frameStatusBar
->IsShown() )
131 GetClientSize(&w
, &h
);
133 // Since we wish the status bar to be directly under the client area,
134 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
135 m_frameStatusBar
->SetSize(0, h
, w
, WX_MAC_STATUSBAR_HEIGHT
);
138 #endif // wxUSE_STATUSBAR
140 // Responds to colour changes, and passes event on to children.
141 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
143 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
147 if ( m_frameStatusBar
)
149 wxSysColourChangedEvent event2
;
151 event2
.SetEventObject( m_frameStatusBar
);
152 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
154 #endif // wxUSE_STATUSBAR
156 // Propagate the event to the non-top-level children
157 wxWindow::OnSysColourChanged(event
);
160 // Default activation behaviour - set the focus for the first child
162 void wxFrame::OnActivate(wxActivateEvent
& event
)
164 if ( !event
.GetActive() )
166 // remember the last focused child if it is our child
167 m_winLastFocused
= FindFocus();
169 // so we NULL it out if it's a child from some other frame
170 wxWindow
*win
= m_winLastFocused
;
173 if ( win
->IsTopLevel() )
176 m_winLastFocused
= NULL
;
181 win
= win
->GetParent();
188 // restore focus to the child which was last focused
189 wxWindow
*parent
= m_winLastFocused
190 ? m_winLastFocused
->GetParent()
196 wxSetFocusToChild(parent
, &m_winLastFocused
);
199 if (m_frameMenuBar
!= NULL
)
201 m_frameMenuBar
->MacInstallMenuBar();
205 wxFrame
*tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
208 // Trying top-level frame membar
209 if (tlf
->GetMenuBar())
210 tlf
->GetMenuBar()->MacInstallMenuBar();
218 void wxFrame::DetachMenuBar()
220 wxFrameBase::DetachMenuBar();
223 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
226 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
227 #elif wxOSX_USE_COCOA
228 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( wxOSXGetMainWindow() ) , wxFrame
);
230 wxFrame
* tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
232 bool makeCurrent
= false;
234 // if this is already the current menubar or we are the frontmost window
235 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
237 // or there is an app-level menubar like MDI
238 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
241 wxFrameBase::AttachMenuBar( menuBar
);
246 m_frameMenuBar
->MacInstallMenuBar();
251 void wxFrame::DoGetClientSize(int *x
, int *y
) const
253 wxTopLevelWindow::DoGetClientSize( x
, y
);
256 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
257 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
261 wxToolBar
*toolbar
= GetToolBar();
262 if ( toolbar
&& toolbar
->IsShown() )
265 toolbar
->GetSize(&w
, &h
);
267 if ( toolbar
->IsVertical() )
272 else if ( toolbar
->HasFlag( wxTB_BOTTOM
) )
279 #if !wxOSX_USE_NATIVE_TOOLBAR
288 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
291 if ( child
== GetStatusBar() )
296 if ( child
== GetToolBar() )
300 return wxFrameBase::MacIsChildOfClientArea( child
) ;
303 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
305 int currentclientwidth
, currentclientheight
;
306 int currentwidth
, currentheight
;
308 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
309 if ( clientwidth
== -1 )
310 clientwidth
= currentclientwidth
;
311 if ( clientheight
== -1 )
312 clientheight
= currentclientheight
;
313 GetSize( ¤twidth
, ¤theight
) ;
315 // find the current client size
317 // Find the difference between the entire window (title bar and all) and
318 // the client area; add this to the new client size to move the window
319 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
320 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
324 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
326 if ( m_frameToolBar
== toolbar
)
329 #ifndef __WXOSX_IPHONE__
330 #if wxOSX_USE_NATIVE_TOOLBAR
331 if ( m_frameToolBar
)
332 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
335 m_frameToolBar
= toolbar
;
337 #ifndef __WXOSX_IPHONE__
338 #if wxOSX_USE_NATIVE_TOOLBAR
340 toolbar
->MacInstallNativeToolbar( true ) ;
345 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
347 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
350 return m_frameToolBar
;
353 void wxFrame::PositionToolBar()
357 wxTopLevelWindow::DoGetClientSize( &cw
, &ch
);
363 if (GetStatusBar() && GetStatusBar()->IsShown())
365 GetStatusBar()->GetSize(&statusX
, &statusY
);
370 #ifdef __WXOSX_IPHONE__
371 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
372 // but the toolbar is
381 GetToolBar()->GetSize(&tw
, &th
);
382 if (GetToolBar()->HasFlag(wxTB_LEFT
))
384 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
385 // means, pretend we don't have toolbar/status bar, so we
386 // have the original client size.
387 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
389 else if (GetToolBar()->HasFlag(wxTB_RIGHT
))
391 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
392 // means, pretend we don't have toolbar/status bar, so we
393 // have the original client size.
395 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
397 else if (GetToolBar()->HasFlag(wxTB_BOTTOM
))
401 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
405 #if !wxOSX_USE_NATIVE_TOOLBAR
406 // Use the 'real' position
407 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
412 #endif // wxUSE_TOOLBAR
414 void wxFrame::PositionBars()