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
,
54 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
67 // get the origin of the client area in the client coordinates
68 wxPoint
wxFrame::GetClientAreaOrigin() const
70 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
72 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
73 wxToolBar
*toolbar
= GetToolBar();
74 if ( toolbar
&& toolbar
->IsShown() )
77 toolbar
->GetSize(&w
, &h
);
79 if ( toolbar
->HasFlag(wxTB_LEFT
) )
83 else if ( toolbar
->HasFlag(wxTB_TOP
) )
85 #if !wxOSX_USE_NATIVE_TOOLBAR
95 bool wxFrame::Enable(bool enable
)
97 if ( !wxWindow::Enable(enable
) )
101 // we should always enable/disable the menubar, even if we are not current, otherwise
102 // we might miss some state change later (happened eg in the docview sample after PrintPreview)
103 if ( m_frameMenuBar
/*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/)
105 int iMaxMenu
= m_frameMenuBar
->GetMenuCount();
106 for ( int i
= 0 ; i
< iMaxMenu
; ++ i
)
108 m_frameMenuBar
->EnableTop( i
, enable
) ;
116 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
117 const wxString
& name
)
119 wxStatusBar
*statusBar
;
121 statusBar
= new wxStatusBar(this, id
, style
, name
);
122 statusBar
->SetSize(100, WX_MAC_STATUSBAR_HEIGHT
);
123 statusBar
->SetFieldsCount(number
);
128 void wxFrame::PositionStatusBar()
130 if (m_frameStatusBar
&& m_frameStatusBar
->IsShown() )
133 GetClientSize(&w
, &h
);
135 // Since we wish the status bar to be directly under the client area,
136 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
137 m_frameStatusBar
->SetSize(0, h
, w
, WX_MAC_STATUSBAR_HEIGHT
);
140 #endif // wxUSE_STATUSBAR
142 // Responds to colour changes, and passes event on to children.
143 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
145 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
149 if ( m_frameStatusBar
)
151 wxSysColourChangedEvent event2
;
153 event2
.SetEventObject( m_frameStatusBar
);
154 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
156 #endif // wxUSE_STATUSBAR
158 // Propagate the event to the non-top-level children
159 wxWindow::OnSysColourChanged(event
);
162 // Default activation behaviour - set the focus for the first child
164 void wxFrame::OnActivate(wxActivateEvent
& event
)
166 if ( !event
.GetActive() )
168 // remember the last focused child if it is our child
169 m_winLastFocused
= FindFocus();
171 // so we NULL it out if it's a child from some other frame
172 wxWindow
*win
= m_winLastFocused
;
175 if ( win
->IsTopLevel() )
178 m_winLastFocused
= NULL
;
183 win
= win
->GetParent();
190 // restore focus to the child which was last focused
191 wxWindow
*parent
= m_winLastFocused
192 ? m_winLastFocused
->GetParent()
198 wxSetFocusToChild(parent
, &m_winLastFocused
);
201 if (m_frameMenuBar
!= NULL
)
203 m_frameMenuBar
->MacInstallMenuBar();
207 wxFrame
*tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
210 // Trying top-level frame membar
211 if (tlf
->GetMenuBar())
212 tlf
->GetMenuBar()->MacInstallMenuBar();
220 void wxFrame::OnSize(wxSizeEvent
& event
)
228 void wxFrame::DetachMenuBar()
230 wxFrameBase::DetachMenuBar();
233 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
236 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
238 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
240 bool makeCurrent
= false;
242 // if this is already the current menubar or we are the frontmost window
243 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
245 // or there is an app-level menubar like MDI
246 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
249 wxFrameBase::AttachMenuBar( menuBar
);
254 m_frameMenuBar
->MacInstallMenuBar();
259 void wxFrame::DoGetClientSize(int *x
, int *y
) const
261 wxTopLevelWindow::DoGetClientSize( x
, y
);
264 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
265 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
269 wxToolBar
*toolbar
= GetToolBar();
270 if ( toolbar
&& toolbar
->IsShown() )
273 toolbar
->GetSize(&w
, &h
);
275 if ( toolbar
->IsVertical() )
280 else if ( toolbar
->HasFlag( wxTB_BOTTOM
) )
287 #if !wxOSX_USE_NATIVE_TOOLBAR
296 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
299 if ( child
== GetStatusBar() )
304 if ( child
== GetToolBar() )
308 return wxFrameBase::MacIsChildOfClientArea( child
) ;
311 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
313 int currentclientwidth
, currentclientheight
;
314 int currentwidth
, currentheight
;
316 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
317 if ( clientwidth
== -1 )
318 clientwidth
= currentclientwidth
;
319 if ( clientheight
== -1 )
320 clientheight
= currentclientheight
;
321 GetSize( ¤twidth
, ¤theight
) ;
323 // find the current client size
325 // Find the difference between the entire window (title bar and all) and
326 // the client area; add this to the new client size to move the window
327 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
328 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
332 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
334 if ( m_frameToolBar
== toolbar
)
337 #ifndef __WXOSX_IPHONE__
338 #if wxOSX_USE_NATIVE_TOOLBAR
339 if ( m_frameToolBar
)
340 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
343 m_frameToolBar
= toolbar
;
345 #ifndef __WXOSX_IPHONE__
346 #if wxOSX_USE_NATIVE_TOOLBAR
348 toolbar
->MacInstallNativeToolbar( true ) ;
353 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
355 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
358 return m_frameToolBar
;
361 void wxFrame::PositionToolBar()
365 wxTopLevelWindow::DoGetClientSize( &cw
, &ch
);
371 if (GetStatusBar() && GetStatusBar()->IsShown())
373 GetStatusBar()->GetSize(&statusX
, &statusY
);
378 #ifdef __WXOSX_IPHONE__
379 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
380 // but the toolbar is
389 GetToolBar()->GetSize(&tw
, &th
);
390 if (GetToolBar()->HasFlag(wxTB_LEFT
))
392 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
393 // means, pretend we don't have toolbar/status bar, so we
394 // have the original client size.
395 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
397 else if (GetToolBar()->HasFlag(wxTB_RIGHT
))
399 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
400 // means, pretend we don't have toolbar/status bar, so we
401 // have the original client size.
403 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
405 else if (GetToolBar()->HasFlag(wxTB_BOTTOM
))
409 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
413 #if !wxOSX_USE_NATIVE_TOOLBAR
414 // Use the 'real' position
415 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
420 #endif // wxUSE_TOOLBAR
422 void wxFrame::PositionBars()