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 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
)
36 #define WX_MAC_STATUSBAR_HEIGHT 18
38 // ----------------------------------------------------------------------------
39 // creation/destruction
40 // ----------------------------------------------------------------------------
44 m_winLastFocused
= NULL
;
47 bool wxFrame::Create(wxWindow
*parent
,
49 const wxString
& title
,
56 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
64 m_isBeingDeleted
= true;
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
->GetWindowStyleFlag() & wxTB_VERTICAL
)
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
->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();
220 void wxFrame::HandleResized( double timestampsec
)
222 // according to the other ports we handle this within the OS level
223 // resize event, not within a wxSizeEvent
227 wxNonOwnedWindow::HandleResized( timestampsec
);
231 void wxFrame::DetachMenuBar()
233 if ( m_frameMenuBar
)
234 m_frameMenuBar
->UnsetInvokingWindow();
236 wxFrameBase::DetachMenuBar();
239 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
242 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
244 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
246 bool makeCurrent
= false;
248 // if this is already the current menubar or we are the frontmost window
249 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
251 // or there is an app-level menubar like MDI
252 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
255 wxFrameBase::AttachMenuBar( menuBar
);
259 m_frameMenuBar
->SetInvokingWindow( this );
261 m_frameMenuBar
->MacInstallMenuBar();
266 void wxFrame::DoGetClientSize(int *x
, int *y
) const
268 wxTopLevelWindow::DoGetClientSize( x
, y
);
271 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
272 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
276 wxToolBar
*toolbar
= GetToolBar();
277 if ( toolbar
&& toolbar
->IsShown() )
280 toolbar
->GetSize(&w
, &h
);
282 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
289 #if !wxOSX_USE_NATIVE_TOOLBAR
298 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
301 if ( child
== GetStatusBar() )
306 if ( child
== GetToolBar() )
310 return wxFrameBase::MacIsChildOfClientArea( child
) ;
313 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
315 int currentclientwidth
, currentclientheight
;
316 int currentwidth
, currentheight
;
318 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
319 if ( clientwidth
== -1 )
320 clientwidth
= currentclientwidth
;
321 if ( clientheight
== -1 )
322 clientheight
= currentclientheight
;
323 GetSize( ¤twidth
, ¤theight
) ;
325 // find the current client size
327 // Find the difference between the entire window (title bar and all) and
328 // the client area; add this to the new client size to move the window
329 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
330 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
334 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
336 if ( m_frameToolBar
== toolbar
)
339 #if wxOSX_USE_NATIVE_TOOLBAR
340 if ( m_frameToolBar
)
341 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
344 m_frameToolBar
= toolbar
;
346 #if wxOSX_USE_NATIVE_TOOLBAR
348 toolbar
->MacInstallNativeToolbar( true ) ;
352 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
354 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
357 return m_frameToolBar
;
360 void wxFrame::PositionToolBar()
364 GetSize( &cw
, &ch
) ;
370 if (GetStatusBar() && GetStatusBar()->IsShown())
372 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
382 GetToolBar()->GetSize(&tw
, &th
);
383 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
385 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
386 // means, pretend we don't have toolbar/status bar, so we
387 // have the original client size.
388 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
390 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM
)
392 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
393 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
394 //doesn't extend it's width to the width of the frame.
396 ty
= ch
- (th
+ statusY
);
397 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
401 #if !wxOSX_USE_NATIVE_TOOLBAR
402 // Use the 'real' position
403 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
408 #endif // wxUSE_TOOLBAR
410 void wxFrame::PositionBars()