]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/frame.cpp
fixing toolbar repositioning (iOS)
[wxWidgets.git] / src / osx / carbon / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/frame.cpp
3 // Purpose: wxFrame
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/frame.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/dcclient.h"
19 #include "wx/menu.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"
25 #endif // WX_PRECOMP
26
27 #include "wx/osx/private.h"
28
29 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
30 EVT_ACTIVATE(wxFrame::OnActivate)
31 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
32 END_EVENT_TABLE()
33
34 #define WX_MAC_STATUSBAR_HEIGHT 18
35
36 // ----------------------------------------------------------------------------
37 // creation/destruction
38 // ----------------------------------------------------------------------------
39
40 void wxFrame::Init()
41 {
42 m_winLastFocused = NULL;
43 }
44
45 bool wxFrame::Create(wxWindow *parent,
46 wxWindowID id,
47 const wxString& title,
48 const wxPoint& pos,
49 const wxSize& size,
50 long style,
51 const wxString& name)
52 {
53
54 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
55 return false;
56
57 return true;
58 }
59
60 wxFrame::~wxFrame()
61 {
62 SendDestroyEvent();
63
64 DeleteAllBars();
65 }
66
67 // get the origin of the client area in the client coordinates
68 wxPoint wxFrame::GetClientAreaOrigin() const
69 {
70 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
71
72 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
73 wxToolBar *toolbar = GetToolBar();
74 if ( toolbar && toolbar->IsShown() )
75 {
76 int w, h;
77 toolbar->GetSize(&w, &h);
78
79 if ( toolbar->HasFlag(wxTB_LEFT) )
80 {
81 pt.x += w;
82 }
83 else if ( toolbar->HasFlag(wxTB_TOP) )
84 {
85 #if !wxOSX_USE_NATIVE_TOOLBAR
86 pt.y += h;
87 #endif
88 }
89 }
90 #endif
91
92 return pt;
93 }
94
95 bool wxFrame::Enable(bool enable)
96 {
97 if ( !wxWindow::Enable(enable) )
98 return false;
99
100 #if wxUSE_MENUS
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()*/)
104 {
105 int iMaxMenu = m_frameMenuBar->GetMenuCount();
106 for ( int i = 0 ; i < iMaxMenu ; ++ i )
107 {
108 m_frameMenuBar->EnableTop( i , enable ) ;
109 }
110 }
111 #endif
112 return true;
113 }
114
115 #if wxUSE_STATUSBAR
116 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
117 const wxString& name)
118 {
119 wxStatusBar *statusBar;
120
121 statusBar = new wxStatusBar(this, id, style, name);
122 statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT);
123 statusBar->SetFieldsCount(number);
124
125 return statusBar;
126 }
127
128 void wxFrame::PositionStatusBar()
129 {
130 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
131 {
132 int w, h;
133 GetClientSize(&w, &h);
134
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);
138 }
139 }
140 #endif // wxUSE_STATUSBAR
141
142 // Responds to colour changes, and passes event on to children.
143 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
144 {
145 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
146 Refresh();
147
148 #if wxUSE_STATUSBAR
149 if ( m_frameStatusBar )
150 {
151 wxSysColourChangedEvent event2;
152
153 event2.SetEventObject( m_frameStatusBar );
154 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
155 }
156 #endif // wxUSE_STATUSBAR
157
158 // Propagate the event to the non-top-level children
159 wxWindow::OnSysColourChanged(event);
160 }
161
162 // Default activation behaviour - set the focus for the first child
163 // subwindow found.
164 void wxFrame::OnActivate(wxActivateEvent& event)
165 {
166 if ( !event.GetActive() )
167 {
168 // remember the last focused child if it is our child
169 m_winLastFocused = FindFocus();
170
171 // so we NULL it out if it's a child from some other frame
172 wxWindow *win = m_winLastFocused;
173 while ( win )
174 {
175 if ( win->IsTopLevel() )
176 {
177 if ( win != this )
178 m_winLastFocused = NULL;
179
180 break;
181 }
182
183 win = win->GetParent();
184 }
185
186 event.Skip();
187 }
188 else
189 {
190 // restore focus to the child which was last focused
191 wxWindow *parent = m_winLastFocused
192 ? m_winLastFocused->GetParent()
193 : NULL;
194
195 if (parent == NULL)
196 parent = this;
197
198 wxSetFocusToChild(parent, &m_winLastFocused);
199
200 #if wxUSE_MENUS
201 if (m_frameMenuBar != NULL)
202 {
203 m_frameMenuBar->MacInstallMenuBar();
204 }
205 else
206 {
207 wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame );
208 if (tlf != NULL)
209 {
210 // Trying top-level frame membar
211 if (tlf->GetMenuBar())
212 tlf->GetMenuBar()->MacInstallMenuBar();
213 }
214 }
215 #endif
216 }
217 }
218
219 void wxFrame::HandleResized( double timestampsec )
220 {
221 // according to the other ports we handle this within the OS level
222 // resize event, not within a wxSizeEvent
223
224 PositionBars();
225
226 wxNonOwnedWindow::HandleResized( timestampsec );
227 }
228
229 #if wxUSE_MENUS
230 void wxFrame::DetachMenuBar()
231 {
232 wxFrameBase::DetachMenuBar();
233 }
234
235 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
236 {
237 #if wxOSX_USE_CARBON
238 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame );
239 #else
240 wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow();
241 #endif
242 bool makeCurrent = false;
243
244 // if this is already the current menubar or we are the frontmost window
245 if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) )
246 makeCurrent = true;
247 // or there is an app-level menubar like MDI
248 else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) )
249 makeCurrent = true;
250
251 wxFrameBase::AttachMenuBar( menuBar );
252
253 if (m_frameMenuBar)
254 {
255 if (makeCurrent)
256 m_frameMenuBar->MacInstallMenuBar();
257 }
258 }
259 #endif
260
261 void wxFrame::DoGetClientSize(int *x, int *y) const
262 {
263 wxTopLevelWindow::DoGetClientSize( x , y );
264
265 #if wxUSE_STATUSBAR
266 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
267 *y -= WX_MAC_STATUSBAR_HEIGHT;
268 #endif
269
270 #if wxUSE_TOOLBAR
271 wxToolBar *toolbar = GetToolBar();
272 if ( toolbar && toolbar->IsShown() )
273 {
274 int w, h;
275 toolbar->GetSize(&w, &h);
276
277 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
278 {
279 if ( x )
280 *x -= w;
281 }
282 else
283 {
284 #if !wxOSX_USE_NATIVE_TOOLBAR
285 if ( y )
286 *y -= h;
287 #endif
288 }
289 }
290 #endif
291 }
292
293 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
294 {
295 #if wxUSE_STATUSBAR
296 if ( child == GetStatusBar() )
297 return false ;
298 #endif
299
300 #if wxUSE_TOOLBAR
301 if ( child == GetToolBar() )
302 return false ;
303 #endif
304
305 return wxFrameBase::MacIsChildOfClientArea( child ) ;
306 }
307
308 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
309 {
310 int currentclientwidth , currentclientheight ;
311 int currentwidth , currentheight ;
312
313 GetClientSize( &currentclientwidth , &currentclientheight ) ;
314 if ( clientwidth == -1 )
315 clientwidth = currentclientwidth ;
316 if ( clientheight == -1 )
317 clientheight = currentclientheight ;
318 GetSize( &currentwidth , &currentheight ) ;
319
320 // find the current client size
321
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 ) ;
326 }
327
328 #if wxUSE_TOOLBAR
329 void wxFrame::SetToolBar(wxToolBar *toolbar)
330 {
331 if ( m_frameToolBar == toolbar )
332 return ;
333
334 #ifndef __WXOSX_IPHONE__
335 #if wxOSX_USE_NATIVE_TOOLBAR
336 if ( m_frameToolBar )
337 m_frameToolBar->MacInstallNativeToolbar( false ) ;
338 #endif
339 #endif
340 m_frameToolBar = toolbar ;
341
342 #ifndef __WXOSX_IPHONE__
343 #if wxOSX_USE_NATIVE_TOOLBAR
344 if ( toolbar )
345 toolbar->MacInstallNativeToolbar( true ) ;
346 #endif
347 #endif
348 }
349
350 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
351 {
352 if ( wxFrameBase::CreateToolBar(style, id, name) )
353 PositionToolBar();
354
355 return m_frameToolBar;
356 }
357
358 void wxFrame::PositionToolBar()
359 {
360 int cw, ch;
361
362 GetSize( &cw , &ch ) ;
363
364 int statusX = 0 ;
365 int statusY = 0 ;
366
367 #if wxUSE_STATUSBAR
368 if (GetStatusBar() && GetStatusBar()->IsShown())
369 {
370 GetStatusBar()->GetClientSize(&statusX, &statusY);
371 ch -= statusY;
372 }
373 #endif
374
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
378 ch -= 20;
379 #endif
380
381 if (GetToolBar())
382 {
383 int tx, ty, tw, th;
384
385 tx = ty = 0 ;
386 GetToolBar()->GetSize(&tw, &th);
387 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
388 {
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 );
393 }
394 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM)
395 {
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.
399 tx = 0;
400 ty = ch - (th + statusY);
401 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
402 }
403 else
404 {
405 #if !wxOSX_USE_NATIVE_TOOLBAR
406 // Use the 'real' position
407 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
408 #endif
409 }
410 }
411 }
412 #endif // wxUSE_TOOLBAR
413
414 void wxFrame::PositionBars()
415 {
416 #if wxUSE_STATUSBAR
417 PositionStatusBar();
418 #endif
419 #if wxUSE_TOOLBAR
420 PositionToolBar();
421 #endif
422 }
423
424