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