]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/frame.cpp
osx new layout
[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 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
35
36 #define WX_MAC_STATUSBAR_HEIGHT 18
37
38 // ----------------------------------------------------------------------------
39 // creation/destruction
40 // ----------------------------------------------------------------------------
41
42 void wxFrame::Init()
43 {
44 m_winLastFocused = NULL;
45 }
46
47 bool wxFrame::Create(wxWindow *parent,
48 wxWindowID id,
49 const wxString& title,
50 const wxPoint& pos,
51 const wxSize& size,
52 long style,
53 const wxString& name)
54 {
55
56 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
57 return false;
58
59 return true;
60 }
61
62 wxFrame::~wxFrame()
63 {
64 m_isBeingDeleted = true;
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->GetWindowStyleFlag() & wxTB_VERTICAL )
81 {
82 pt.x += w;
83 }
84 else
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 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->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 void wxFrame::HandleResized( long timestamp )
219 {
220 // according to the other ports we handle this within the OS level
221 // resize event, not within a wxSizeEvent
222
223 PositionBars();
224
225 wxNonOwnedWindow::HandleResized( timestamp );
226 }
227
228 #if wxUSE_MENUS
229 void wxFrame::DetachMenuBar()
230 {
231 if ( m_frameMenuBar )
232 m_frameMenuBar->UnsetInvokingWindow();
233
234 wxFrameBase::DetachMenuBar();
235 }
236
237 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
238 {
239 #if wxOSX_USE_CARBON
240 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame );
241 #else
242 wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow();
243 #endif
244 bool makeCurrent = false;
245
246 // if this is already the current menubar or we are the frontmost window
247 if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) )
248 makeCurrent = true;
249 // or there is an app-level menubar like MDI
250 else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) )
251 makeCurrent = true;
252
253 wxFrameBase::AttachMenuBar( menuBar );
254
255 if (m_frameMenuBar)
256 {
257 m_frameMenuBar->SetInvokingWindow( this );
258 if (makeCurrent)
259 m_frameMenuBar->MacInstallMenuBar();
260 }
261 }
262 #endif
263
264 void wxFrame::DoGetClientSize(int *x, int *y) const
265 {
266 wxTopLevelWindow::DoGetClientSize( x , y );
267
268 #if wxUSE_STATUSBAR
269 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
270 *y -= WX_MAC_STATUSBAR_HEIGHT;
271 #endif
272
273 #if wxUSE_TOOLBAR
274 wxToolBar *toolbar = GetToolBar();
275 if ( toolbar && toolbar->IsShown() )
276 {
277 int w, h;
278 toolbar->GetSize(&w, &h);
279
280 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
281 {
282 if ( x )
283 *x -= w;
284 }
285 else
286 {
287 #if !wxOSX_USE_NATIVE_TOOLBAR
288 if ( y )
289 *y -= h;
290 #endif
291 }
292 }
293 #endif
294 }
295
296 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
297 {
298 #if wxUSE_STATUSBAR
299 if ( child == GetStatusBar() )
300 return false ;
301 #endif
302
303 #if wxUSE_TOOLBAR
304 if ( child == GetToolBar() )
305 return false ;
306 #endif
307
308 return wxFrameBase::MacIsChildOfClientArea( child ) ;
309 }
310
311 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
312 {
313 int currentclientwidth , currentclientheight ;
314 int currentwidth , currentheight ;
315
316 GetClientSize( &currentclientwidth , &currentclientheight ) ;
317 if ( clientwidth == -1 )
318 clientwidth = currentclientwidth ;
319 if ( clientheight == -1 )
320 clientheight = currentclientheight ;
321 GetSize( &currentwidth , &currentheight ) ;
322
323 // find the current client size
324
325 // Find the difference between the entire window (title bar and all) and
326 // the client area; add this to the new client size to move the window
327 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
328 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
329 }
330
331 #if wxUSE_TOOLBAR
332 void wxFrame::SetToolBar(wxToolBar *toolbar)
333 {
334 if ( m_frameToolBar == toolbar )
335 return ;
336
337 #if wxOSX_USE_NATIVE_TOOLBAR
338 if ( m_frameToolBar )
339 m_frameToolBar->MacInstallNativeToolbar( false ) ;
340 #endif
341
342 m_frameToolBar = toolbar ;
343
344 #if wxOSX_USE_NATIVE_TOOLBAR
345 if ( toolbar )
346 toolbar->MacInstallNativeToolbar( true ) ;
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 if (GetToolBar())
376 {
377 int tx, ty, tw, th;
378
379 tx = ty = 0 ;
380 GetToolBar()->GetSize(&tw, &th);
381 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
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()->GetWindowStyleFlag() & wxTB_BOTTOM)
389 {
390 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
391 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
392 //doesn't extend it's width to the width of the frame.
393 tx = 0;
394 ty = ch - (th + statusY);
395 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
396 }
397 else
398 {
399 #if !wxOSX_USE_NATIVE_TOOLBAR
400 // Use the 'real' position
401 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
402 #endif
403 }
404 }
405 }
406 #endif // wxUSE_TOOLBAR
407
408 void wxFrame::PositionBars()
409 {
410 #if wxUSE_STATUSBAR
411 PositionStatusBar();
412 #endif
413 #if wxUSE_TOOLBAR
414 PositionToolBar();
415 #endif
416 }
417
418