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
) )
69 // get the origin of the client area in the client coordinates
70 wxPoint
wxFrame::GetClientAreaOrigin() const
72 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
74 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
75 wxToolBar
*toolbar
= GetToolBar();
76 if ( toolbar
&& toolbar
->IsShown() )
79 toolbar
->GetSize(&w
, &h
);
81 if ( toolbar
->HasFlag(wxTB_LEFT
) )
85 else if ( HasFlag(wxTB_TOP
) )
87 #if !wxOSX_USE_NATIVE_TOOLBAR
97 bool wxFrame::Enable(bool enable
)
99 if ( !wxWindow::Enable(enable
) )
103 // we should always enable/disable the menubar, even if we are not current, otherwise
104 // we might miss some state change later (happened eg in the docview sample after PrintPreview)
105 if ( m_frameMenuBar
/*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/)
107 int iMaxMenu
= m_frameMenuBar
->GetMenuCount();
108 for ( int i
= 0 ; i
< iMaxMenu
; ++ i
)
110 m_frameMenuBar
->EnableTop( i
, enable
) ;
118 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
119 const wxString
& name
)
121 wxStatusBar
*statusBar
;
123 statusBar
= new wxStatusBar(this, id
, style
, name
);
124 statusBar
->SetSize(100, WX_MAC_STATUSBAR_HEIGHT
);
125 statusBar
->SetFieldsCount(number
);
130 void wxFrame::PositionStatusBar()
132 if (m_frameStatusBar
&& m_frameStatusBar
->IsShown() )
135 GetClientSize(&w
, &h
);
137 // Since we wish the status bar to be directly under the client area,
138 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
139 m_frameStatusBar
->SetSize(0, h
, w
, WX_MAC_STATUSBAR_HEIGHT
);
142 #endif // wxUSE_STATUSBAR
144 // Responds to colour changes, and passes event on to children.
145 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
147 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
151 if ( m_frameStatusBar
)
153 wxSysColourChangedEvent event2
;
155 event2
.SetEventObject( m_frameStatusBar
);
156 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
158 #endif // wxUSE_STATUSBAR
160 // Propagate the event to the non-top-level children
161 wxWindow::OnSysColourChanged(event
);
164 // Default activation behaviour - set the focus for the first child
166 void wxFrame::OnActivate(wxActivateEvent
& event
)
168 if ( !event
.GetActive() )
170 // remember the last focused child if it is our child
171 m_winLastFocused
= FindFocus();
173 // so we NULL it out if it's a child from some other frame
174 wxWindow
*win
= m_winLastFocused
;
177 if ( win
->IsTopLevel() )
180 m_winLastFocused
= NULL
;
185 win
= win
->GetParent();
192 // restore focus to the child which was last focused
193 wxWindow
*parent
= m_winLastFocused
194 ? m_winLastFocused
->GetParent()
200 wxSetFocusToChild(parent
, &m_winLastFocused
);
203 if (m_frameMenuBar
!= NULL
)
205 m_frameMenuBar
->MacInstallMenuBar();
209 wxFrame
*tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
212 // Trying top-level frame membar
213 if (tlf
->GetMenuBar())
214 tlf
->GetMenuBar()->MacInstallMenuBar();
221 void wxFrame::HandleResized( double timestampsec
)
223 // according to the other ports we handle this within the OS level
224 // resize event, not within a wxSizeEvent
228 wxNonOwnedWindow::HandleResized( timestampsec
);
232 void wxFrame::DetachMenuBar()
234 wxFrameBase::DetachMenuBar();
237 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
240 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
242 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
244 bool makeCurrent
= false;
246 // if this is already the current menubar or we are the frontmost window
247 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
249 // or there is an app-level menubar like MDI
250 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
253 wxFrameBase::AttachMenuBar( menuBar
);
258 m_frameMenuBar
->MacInstallMenuBar();
263 void wxFrame::DoGetClientSize(int *x
, int *y
) const
265 wxTopLevelWindow::DoGetClientSize( x
, y
);
268 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
269 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
273 wxToolBar
*toolbar
= GetToolBar();
274 if ( toolbar
&& toolbar
->IsShown() )
277 toolbar
->GetSize(&w
, &h
);
279 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
286 #if !wxOSX_USE_NATIVE_TOOLBAR
295 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
298 if ( child
== GetStatusBar() )
303 if ( child
== GetToolBar() )
307 return wxFrameBase::MacIsChildOfClientArea( child
) ;
310 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
312 int currentclientwidth
, currentclientheight
;
313 int currentwidth
, currentheight
;
315 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
316 if ( clientwidth
== -1 )
317 clientwidth
= currentclientwidth
;
318 if ( clientheight
== -1 )
319 clientheight
= currentclientheight
;
320 GetSize( ¤twidth
, ¤theight
) ;
322 // find the current client size
324 // Find the difference between the entire window (title bar and all) and
325 // the client area; add this to the new client size to move the window
326 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
327 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
331 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
333 if ( m_frameToolBar
== toolbar
)
336 #ifndef __WXOSX_IPHONE__
337 #if wxOSX_USE_NATIVE_TOOLBAR
338 if ( m_frameToolBar
)
339 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
342 m_frameToolBar
= toolbar
;
344 #ifndef __WXOSX_IPHONE__
345 #if wxOSX_USE_NATIVE_TOOLBAR
347 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
);
377 #ifdef __WXOSX_IPHONE__
378 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
379 // but the toolbar is
388 GetToolBar()->GetSize(&tw
, &th
);
389 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
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.
394 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
396 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM
)
398 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
399 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
400 //doesn't extend it's width to the width of the frame.
402 ty
= ch
- (th
+ statusY
);
403 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
407 #if !wxOSX_USE_NATIVE_TOOLBAR
408 // Use the 'real' position
409 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
414 #endif // wxUSE_TOOLBAR
416 void wxFrame::PositionBars()