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
)
34 #define WX_MAC_STATUSBAR_HEIGHT 18
36 // ----------------------------------------------------------------------------
37 // creation/destruction
38 // ----------------------------------------------------------------------------
42 m_winLastFocused
= NULL
;
45 bool wxFrame::Create(wxWindow
*parent
,
47 const wxString
& title
,
53 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
66 // get the origin of the client area in the client coordinates
67 wxPoint
wxFrame::GetClientAreaOrigin() const
69 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
71 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
72 wxToolBar
*toolbar
= GetToolBar();
73 if ( toolbar
&& toolbar
->IsShown() )
76 toolbar
->GetSize(&w
, &h
);
78 if ( toolbar
->HasFlag(wxTB_LEFT
) )
82 else if ( toolbar
->HasFlag(wxTB_TOP
) )
84 #if !wxOSX_USE_NATIVE_TOOLBAR
94 bool wxFrame::Enable(bool enable
)
96 if ( !wxWindow::Enable(enable
) )
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()*/)
104 int iMaxMenu
= m_frameMenuBar
->GetMenuCount();
105 for ( int i
= 0 ; i
< iMaxMenu
; ++ i
)
107 m_frameMenuBar
->EnableTop( i
, enable
) ;
115 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
116 const wxString
& name
)
118 wxStatusBar
*statusBar
;
120 statusBar
= new wxStatusBar(this, id
, style
, name
);
121 statusBar
->SetSize(100, WX_MAC_STATUSBAR_HEIGHT
);
122 statusBar
->SetFieldsCount(number
);
127 void wxFrame::PositionStatusBar()
129 if (m_frameStatusBar
&& m_frameStatusBar
->IsShown() )
132 GetClientSize(&w
, &h
);
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
);
139 #endif // wxUSE_STATUSBAR
141 // Responds to colour changes, and passes event on to children.
142 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
144 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
148 if ( m_frameStatusBar
)
150 wxSysColourChangedEvent event2
;
152 event2
.SetEventObject( m_frameStatusBar
);
153 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
155 #endif // wxUSE_STATUSBAR
157 // Propagate the event to the non-top-level children
158 wxWindow::OnSysColourChanged(event
);
161 // Default activation behaviour - set the focus for the first child
163 void wxFrame::OnActivate(wxActivateEvent
& event
)
165 if ( !event
.GetActive() )
167 // remember the last focused child if it is our child
168 m_winLastFocused
= FindFocus();
170 // so we NULL it out if it's a child from some other frame
171 wxWindow
*win
= m_winLastFocused
;
174 if ( win
->IsTopLevel() )
177 m_winLastFocused
= NULL
;
182 win
= win
->GetParent();
189 // restore focus to the child which was last focused
190 wxWindow
*parent
= m_winLastFocused
191 ? m_winLastFocused
->GetParent()
197 wxSetFocusToChild(parent
, &m_winLastFocused
);
200 if (m_frameMenuBar
!= NULL
)
202 m_frameMenuBar
->MacInstallMenuBar();
206 wxFrame
*tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
209 // Trying top-level frame membar
210 if (tlf
->GetMenuBar())
211 tlf
->GetMenuBar()->MacInstallMenuBar();
219 void wxFrame::DetachMenuBar()
221 wxFrameBase::DetachMenuBar();
224 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
227 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
229 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
231 bool makeCurrent
= false;
233 // if this is already the current menubar or we are the frontmost window
234 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
236 // or there is an app-level menubar like MDI
237 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
240 wxFrameBase::AttachMenuBar( menuBar
);
245 m_frameMenuBar
->MacInstallMenuBar();
250 void wxFrame::DoGetClientSize(int *x
, int *y
) const
252 wxTopLevelWindow::DoGetClientSize( x
, y
);
255 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
256 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
260 wxToolBar
*toolbar
= GetToolBar();
261 if ( toolbar
&& toolbar
->IsShown() )
264 toolbar
->GetSize(&w
, &h
);
266 if ( toolbar
->IsVertical() )
271 else if ( toolbar
->HasFlag( wxTB_BOTTOM
) )
278 #if !wxOSX_USE_NATIVE_TOOLBAR
287 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
290 if ( child
== GetStatusBar() )
295 if ( child
== GetToolBar() )
299 return wxFrameBase::MacIsChildOfClientArea( child
) ;
302 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
304 int currentclientwidth
, currentclientheight
;
305 int currentwidth
, currentheight
;
307 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
308 if ( clientwidth
== -1 )
309 clientwidth
= currentclientwidth
;
310 if ( clientheight
== -1 )
311 clientheight
= currentclientheight
;
312 GetSize( ¤twidth
, ¤theight
) ;
314 // find the current client size
316 // Find the difference between the entire window (title bar and all) and
317 // the client area; add this to the new client size to move the window
318 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
319 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
323 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
325 if ( m_frameToolBar
== toolbar
)
328 #ifndef __WXOSX_IPHONE__
329 #if wxOSX_USE_NATIVE_TOOLBAR
330 if ( m_frameToolBar
)
331 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
334 m_frameToolBar
= toolbar
;
336 #ifndef __WXOSX_IPHONE__
337 #if wxOSX_USE_NATIVE_TOOLBAR
339 toolbar
->MacInstallNativeToolbar( true ) ;
344 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
346 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
349 return m_frameToolBar
;
352 void wxFrame::PositionToolBar()
356 wxTopLevelWindow::DoGetClientSize( &cw
, &ch
);
362 if (GetStatusBar() && GetStatusBar()->IsShown())
364 GetStatusBar()->GetSize(&statusX
, &statusY
);
369 #ifdef __WXOSX_IPHONE__
370 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
371 // but the toolbar is
380 GetToolBar()->GetSize(&tw
, &th
);
381 if (GetToolBar()->HasFlag(wxTB_LEFT
))
383 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
384 // means, pretend we don't have toolbar/status bar, so we
385 // have the original client size.
386 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
388 else if (GetToolBar()->HasFlag(wxTB_RIGHT
))
390 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
391 // means, pretend we don't have toolbar/status bar, so we
392 // have the original client size.
394 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
396 else if (GetToolBar()->HasFlag(wxTB_BOTTOM
))
400 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
404 #if !wxOSX_USE_NATIVE_TOOLBAR
405 // Use the 'real' position
406 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
411 #endif // wxUSE_TOOLBAR
413 void wxFrame::PositionBars()