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