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
);
228 #elif wxOSX_USE_COCOA
229 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( wxOSXGetMainWindow() ) , wxFrame
);
231 wxFrame
* tlf
= wxDynamicCast( wxTheApp
->GetTopWindow(), wxFrame
);
233 bool makeCurrent
= false;
235 // if this is already the current menubar or we are the frontmost window
236 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
238 // or there is an app-level menubar like MDI
239 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
242 wxFrameBase::AttachMenuBar( menuBar
);
247 m_frameMenuBar
->MacInstallMenuBar();
252 void wxFrame::DoGetClientSize(int *x
, int *y
) const
254 wxTopLevelWindow::DoGetClientSize( x
, y
);
257 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
258 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
262 wxToolBar
*toolbar
= GetToolBar();
263 if ( toolbar
&& toolbar
->IsShown() )
266 toolbar
->GetSize(&w
, &h
);
268 if ( toolbar
->IsVertical() )
273 else if ( toolbar
->HasFlag( wxTB_BOTTOM
) )
280 #if !wxOSX_USE_NATIVE_TOOLBAR
289 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
292 if ( child
== GetStatusBar() )
297 if ( child
== GetToolBar() )
301 return wxFrameBase::MacIsChildOfClientArea( child
) ;
304 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
306 int currentclientwidth
, currentclientheight
;
307 int currentwidth
, currentheight
;
309 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
310 if ( clientwidth
== -1 )
311 clientwidth
= currentclientwidth
;
312 if ( clientheight
== -1 )
313 clientheight
= currentclientheight
;
314 GetSize( ¤twidth
, ¤theight
) ;
316 // find the current client size
318 // Find the difference between the entire window (title bar and all) and
319 // the client area; add this to the new client size to move the window
320 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
321 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
325 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
327 if ( m_frameToolBar
== toolbar
)
330 #ifndef __WXOSX_IPHONE__
331 #if wxOSX_USE_NATIVE_TOOLBAR
332 if ( m_frameToolBar
)
333 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
336 m_frameToolBar
= toolbar
;
338 #ifndef __WXOSX_IPHONE__
339 #if wxOSX_USE_NATIVE_TOOLBAR
341 toolbar
->MacInstallNativeToolbar( true ) ;
346 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
348 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
351 return m_frameToolBar
;
354 void wxFrame::PositionToolBar()
358 wxTopLevelWindow::DoGetClientSize( &cw
, &ch
);
364 if (GetStatusBar() && GetStatusBar()->IsShown())
366 GetStatusBar()->GetSize(&statusX
, &statusY
);
371 #ifdef __WXOSX_IPHONE__
372 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
373 // but the toolbar is
382 GetToolBar()->GetSize(&tw
, &th
);
383 if (GetToolBar()->HasFlag(wxTB_LEFT
))
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()->HasFlag(wxTB_RIGHT
))
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.
396 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
398 else if (GetToolBar()->HasFlag(wxTB_BOTTOM
))
402 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
406 #if !wxOSX_USE_NATIVE_TOOLBAR
407 // Use the 'real' position
408 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
413 #endif // wxUSE_TOOLBAR
415 void wxFrame::PositionBars()