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
,
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 ( 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();
219 void wxFrame::HandleResized( double timestampsec
)
221 // according to the other ports we handle this within the OS level
222 // resize event, not within a wxSizeEvent
226 wxNonOwnedWindow::HandleResized( timestampsec
);
230 void wxFrame::DetachMenuBar()
232 wxFrameBase::DetachMenuBar();
235 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
238 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
240 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
242 bool makeCurrent
= false;
244 // if this is already the current menubar or we are the frontmost window
245 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
247 // or there is an app-level menubar like MDI
248 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
251 wxFrameBase::AttachMenuBar( menuBar
);
256 m_frameMenuBar
->MacInstallMenuBar();
261 void wxFrame::DoGetClientSize(int *x
, int *y
) const
263 wxTopLevelWindow::DoGetClientSize( x
, y
);
266 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
267 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
271 wxToolBar
*toolbar
= GetToolBar();
272 if ( toolbar
&& toolbar
->IsShown() )
275 toolbar
->GetSize(&w
, &h
);
277 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
284 #if !wxOSX_USE_NATIVE_TOOLBAR
293 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
296 if ( child
== GetStatusBar() )
301 if ( child
== GetToolBar() )
305 return wxFrameBase::MacIsChildOfClientArea( child
) ;
308 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
310 int currentclientwidth
, currentclientheight
;
311 int currentwidth
, currentheight
;
313 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
314 if ( clientwidth
== -1 )
315 clientwidth
= currentclientwidth
;
316 if ( clientheight
== -1 )
317 clientheight
= currentclientheight
;
318 GetSize( ¤twidth
, ¤theight
) ;
320 // find the current client size
322 // Find the difference between the entire window (title bar and all) and
323 // the client area; add this to the new client size to move the window
324 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
325 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
329 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
331 if ( m_frameToolBar
== toolbar
)
334 #ifndef __WXOSX_IPHONE__
335 #if wxOSX_USE_NATIVE_TOOLBAR
336 if ( m_frameToolBar
)
337 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
340 m_frameToolBar
= toolbar
;
342 #ifndef __WXOSX_IPHONE__
343 #if wxOSX_USE_NATIVE_TOOLBAR
345 toolbar
->MacInstallNativeToolbar( true ) ;
350 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
352 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
355 return m_frameToolBar
;
358 void wxFrame::PositionToolBar()
362 GetSize( &cw
, &ch
) ;
368 if (GetStatusBar() && GetStatusBar()->IsShown())
370 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
375 #ifdef __WXOSX_IPHONE__
376 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
377 // but the toolbar is
386 GetToolBar()->GetSize(&tw
, &th
);
387 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
389 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
390 // means, pretend we don't have toolbar/status bar, so we
391 // have the original client size.
392 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
394 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM
)
396 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
397 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
398 //doesn't extend it's width to the width of the frame.
400 ty
= ch
- (th
+ statusY
);
401 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
405 #if !wxOSX_USE_NATIVE_TOOLBAR
406 // Use the 'real' position
407 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
412 #endif // wxUSE_TOOLBAR
414 void wxFrame::PositionBars()