]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/frame.cpp
merging back XTI branch part 2
[wxWidgets.git] / src / osx / carbon / frame.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/frame.cpp
489468fe
SC
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
524c47aa 27#include "wx/osx/private.h"
489468fe
SC
28
29BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
30 EVT_ACTIVATE(wxFrame::OnActivate)
489468fe 31 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
489468fe
SC
32END_EVENT_TABLE()
33
489468fe
SC
34#define WX_MAC_STATUSBAR_HEIGHT 18
35
36// ----------------------------------------------------------------------------
37// creation/destruction
38// ----------------------------------------------------------------------------
39
40void wxFrame::Init()
41{
42 m_winLastFocused = NULL;
43}
44
45bool 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
60wxFrame::~wxFrame()
61{
c6212a0c
VZ
62 SendDestroyEvent();
63
489468fe
SC
64 DeleteAllBars();
65}
66
67// get the origin of the client area in the client coordinates
68wxPoint 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
85c04b8f 79 if ( toolbar->HasFlag(wxTB_LEFT) )
489468fe
SC
80 {
81 pt.x += w;
82 }
85c04b8f 83 else if ( HasFlag(wxTB_TOP) )
489468fe 84 {
292e5e1f 85#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
86 pt.y += h;
87#endif
88 }
89 }
90#endif
91
92 return pt;
93}
94
95bool wxFrame::Enable(bool enable)
96{
97 if ( !wxWindow::Enable(enable) )
98 return false;
99
524c47aa 100#if wxUSE_MENUS
fc7ba044
SC
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()*/)
489468fe
SC
104 {
105 int iMaxMenu = m_frameMenuBar->GetMenuCount();
106 for ( int i = 0 ; i < iMaxMenu ; ++ i )
107 {
108 m_frameMenuBar->EnableTop( i , enable ) ;
109 }
110 }
524c47aa 111#endif
489468fe
SC
112 return true;
113}
114
115#if wxUSE_STATUSBAR
116wxStatusBar *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
128void 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.
143void 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 );
ac17f728 154 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
489468fe
SC
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.
164void 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
524c47aa 200#if wxUSE_MENUS
489468fe
SC
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 }
524c47aa 215#endif
489468fe
SC
216 }
217}
218
e0e2cbbe 219void wxFrame::HandleResized( double timestampsec )
524c47aa
SC
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
e0e2cbbe 226 wxNonOwnedWindow::HandleResized( timestampsec );
524c47aa
SC
227}
228
229#if wxUSE_MENUS
489468fe
SC
230void wxFrame::DetachMenuBar()
231{
489468fe
SC
232 wxFrameBase::DetachMenuBar();
233}
234
235void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
236{
524c47aa 237#if wxOSX_USE_CARBON
b2680ced 238 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame );
524c47aa
SC
239#else
240 wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow();
241#endif
489468fe
SC
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 {
489468fe
SC
255 if (makeCurrent)
256 m_frameMenuBar->MacInstallMenuBar();
257 }
258}
524c47aa 259#endif
489468fe
SC
260
261void 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 {
292e5e1f 284#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
285 if ( y )
286 *y -= h;
287#endif
288 }
289 }
290#endif
291}
292
293bool 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
308void 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
329void wxFrame::SetToolBar(wxToolBar *toolbar)
330{
331 if ( m_frameToolBar == toolbar )
332 return ;
333
85c04b8f 334#ifndef __WXOSX_IPHONE__
292e5e1f 335#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
336 if ( m_frameToolBar )
337 m_frameToolBar->MacInstallNativeToolbar( false ) ;
338#endif
85c04b8f 339#endif
489468fe
SC
340 m_frameToolBar = toolbar ;
341
85c04b8f 342#ifndef __WXOSX_IPHONE__
292e5e1f 343#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
344 if ( toolbar )
345 toolbar->MacInstallNativeToolbar( true ) ;
346#endif
85c04b8f 347#endif
489468fe
SC
348}
349
350wxToolBar* 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
358void wxFrame::PositionToolBar()
359{
360 int cw, ch;
361
362 GetSize( &cw , &ch ) ;
c6212a0c 363
489468fe
SC
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
85c04b8f
SC
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
489468fe
SC
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 {
292e5e1f 405#if !wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
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
414void wxFrame::PositionBars()
415{
416#if wxUSE_STATUSBAR
417 PositionStatusBar();
418#endif
419#if wxUSE_TOOLBAR
420 PositionToolBar();
421#endif
422}
423
424