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 if ( m_frameMenuBar
)
235 m_frameMenuBar
->UnsetInvokingWindow();
237 wxFrameBase::DetachMenuBar();
240 void wxFrame::AttachMenuBar( wxMenuBar
*menuBar
)
243 wxFrame
* tlf
= wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontNonFloatingWindow() ) , wxFrame
);
245 wxFrame
* tlf
= (wxFrame
*) wxTheApp
->GetTopWindow();
247 bool makeCurrent
= false;
249 // if this is already the current menubar or we are the frontmost window
250 if ( (tlf
== this) || (m_frameMenuBar
== wxMenuBar::MacGetInstalledMenuBar()) )
252 // or there is an app-level menubar like MDI
253 else if ( tlf
&& (tlf
->GetMenuBar() == NULL
) && (((wxFrame
*)wxTheApp
->GetTopWindow()) == this) )
256 wxFrameBase::AttachMenuBar( menuBar
);
260 m_frameMenuBar
->SetInvokingWindow( this );
262 m_frameMenuBar
->MacInstallMenuBar();
267 void wxFrame::DoGetClientSize(int *x
, int *y
) const
269 wxTopLevelWindow::DoGetClientSize( x
, y
);
272 if ( GetStatusBar() && GetStatusBar()->IsShown() && y
)
273 *y
-= WX_MAC_STATUSBAR_HEIGHT
;
277 wxToolBar
*toolbar
= GetToolBar();
278 if ( toolbar
&& toolbar
->IsShown() )
281 toolbar
->GetSize(&w
, &h
);
283 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
290 #if !wxOSX_USE_NATIVE_TOOLBAR
299 bool wxFrame::MacIsChildOfClientArea( const wxWindow
* child
) const
302 if ( child
== GetStatusBar() )
307 if ( child
== GetToolBar() )
311 return wxFrameBase::MacIsChildOfClientArea( child
) ;
314 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
316 int currentclientwidth
, currentclientheight
;
317 int currentwidth
, currentheight
;
319 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
320 if ( clientwidth
== -1 )
321 clientwidth
= currentclientwidth
;
322 if ( clientheight
== -1 )
323 clientheight
= currentclientheight
;
324 GetSize( ¤twidth
, ¤theight
) ;
326 // find the current client size
328 // Find the difference between the entire window (title bar and all) and
329 // the client area; add this to the new client size to move the window
330 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
331 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
335 void wxFrame::SetToolBar(wxToolBar
*toolbar
)
337 if ( m_frameToolBar
== toolbar
)
340 #ifndef __WXOSX_IPHONE__
341 #if wxOSX_USE_NATIVE_TOOLBAR
342 if ( m_frameToolBar
)
343 m_frameToolBar
->MacInstallNativeToolbar( false ) ;
346 m_frameToolBar
= toolbar
;
348 #ifndef __WXOSX_IPHONE__
349 #if wxOSX_USE_NATIVE_TOOLBAR
351 toolbar
->MacInstallNativeToolbar( true ) ;
356 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
358 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
361 return m_frameToolBar
;
364 void wxFrame::PositionToolBar()
368 GetSize( &cw
, &ch
) ;
374 if (GetStatusBar() && GetStatusBar()->IsShown())
376 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
381 #ifdef __WXOSX_IPHONE__
382 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
383 // but the toolbar is
392 GetToolBar()->GetSize(&tw
, &th
);
393 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
395 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
396 // means, pretend we don't have toolbar/status bar, so we
397 // have the original client size.
398 GetToolBar()->SetSize(tx
, ty
, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
400 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM
)
402 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
403 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
404 //doesn't extend it's width to the width of the frame.
406 ty
= ch
- (th
+ statusY
);
407 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
411 #if !wxOSX_USE_NATIVE_TOOLBAR
412 // Use the 'real' position
413 GetToolBar()->SetSize(tx
, ty
, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
418 #endif // wxUSE_TOOLBAR
420 void wxFrame::PositionBars()