]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/frame.cpp
new file added
[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 #elif wxOSX_USE_COCOA
229 wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( wxOSXGetMainWindow() ) , wxFrame );
230 #else
231 wxFrame* tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame );
232 #endif
233 bool makeCurrent = false;
234
235 // if this is already the current menubar or we are the frontmost window
236 if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) )
237 makeCurrent = true;
238 // or there is an app-level menubar like MDI
239 else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) )
240 makeCurrent = true;
241
242 wxFrameBase::AttachMenuBar( menuBar );
243
244 if (m_frameMenuBar)
245 {
246 if (makeCurrent)
247 m_frameMenuBar->MacInstallMenuBar();
248 }
249 }
250 #endif
251
252 void wxFrame::DoGetClientSize(int *x, int *y) const
253 {
254 wxTopLevelWindow::DoGetClientSize( x , y );
255
256 #if wxUSE_STATUSBAR
257 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
258 *y -= WX_MAC_STATUSBAR_HEIGHT;
259 #endif
260
261 #if wxUSE_TOOLBAR
262 wxToolBar *toolbar = GetToolBar();
263 if ( toolbar && toolbar->IsShown() )
264 {
265 int w, h;
266 toolbar->GetSize(&w, &h);
267
268 if ( toolbar->IsVertical() )
269 {
270 if ( x )
271 *x -= w;
272 }
273 else if ( toolbar->HasFlag( wxTB_BOTTOM ) )
274 {
275 if ( y )
276 *y -= h;
277 }
278 else
279 {
280 #if !wxOSX_USE_NATIVE_TOOLBAR
281 if ( y )
282 *y -= h;
283 #endif
284 }
285 }
286 #endif
287 }
288
289 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
290 {
291 #if wxUSE_STATUSBAR
292 if ( child == GetStatusBar() )
293 return false ;
294 #endif
295
296 #if wxUSE_TOOLBAR
297 if ( child == GetToolBar() )
298 return false ;
299 #endif
300
301 return wxFrameBase::MacIsChildOfClientArea( child ) ;
302 }
303
304 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
305 {
306 int currentclientwidth , currentclientheight ;
307 int currentwidth , currentheight ;
308
309 GetClientSize( &currentclientwidth , &currentclientheight ) ;
310 if ( clientwidth == -1 )
311 clientwidth = currentclientwidth ;
312 if ( clientheight == -1 )
313 clientheight = currentclientheight ;
314 GetSize( &currentwidth , &currentheight ) ;
315
316 // find the current client size
317
318 // Find the difference between the entire window (title bar and all) and
319 // the client area; add this to the new client size to move the window
320 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
321 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
322 }
323
324 #if wxUSE_TOOLBAR
325 void wxFrame::SetToolBar(wxToolBar *toolbar)
326 {
327 if ( m_frameToolBar == toolbar )
328 return ;
329
330 #ifndef __WXOSX_IPHONE__
331 #if wxOSX_USE_NATIVE_TOOLBAR
332 if ( m_frameToolBar )
333 m_frameToolBar->MacInstallNativeToolbar( false ) ;
334 #endif
335 #endif
336 m_frameToolBar = toolbar ;
337
338 #ifndef __WXOSX_IPHONE__
339 #if wxOSX_USE_NATIVE_TOOLBAR
340 if ( toolbar )
341 toolbar->MacInstallNativeToolbar( true ) ;
342 #endif
343 #endif
344 }
345
346 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
347 {
348 if ( wxFrameBase::CreateToolBar(style, id, name) )
349 PositionToolBar();
350
351 return m_frameToolBar;
352 }
353
354 void wxFrame::PositionToolBar()
355 {
356 int cw, ch;
357
358 wxTopLevelWindow::DoGetClientSize( &cw , &ch );
359
360 int statusX = 0 ;
361 int statusY = 0 ;
362
363 #if wxUSE_STATUSBAR
364 if (GetStatusBar() && GetStatusBar()->IsShown())
365 {
366 GetStatusBar()->GetSize(&statusX, &statusY);
367 ch -= statusY;
368 }
369 #endif
370
371 #ifdef __WXOSX_IPHONE__
372 // TODO integrate this in a better way, on iphone the status bar is not a child of the content view
373 // but the toolbar is
374 ch -= 20;
375 #endif
376
377 if (GetToolBar())
378 {
379 int tx, ty, tw, th;
380
381 tx = ty = 0 ;
382 GetToolBar()->GetSize(&tw, &th);
383 if (GetToolBar()->HasFlag(wxTB_LEFT))
384 {
385 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
386 // means, pretend we don't have toolbar/status bar, so we
387 // have the original client size.
388 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
389 }
390 else if (GetToolBar()->HasFlag(wxTB_RIGHT))
391 {
392 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
393 // means, pretend we don't have toolbar/status bar, so we
394 // have the original client size.
395 tx = cw - tw;
396 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
397 }
398 else if (GetToolBar()->HasFlag(wxTB_BOTTOM))
399 {
400 tx = 0;
401 ty = ch - th;
402 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
403 }
404 else
405 {
406 #if !wxOSX_USE_NATIVE_TOOLBAR
407 // Use the 'real' position
408 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
409 #endif
410 }
411 }
412 }
413 #endif // wxUSE_TOOLBAR
414
415 void wxFrame::PositionBars()
416 {
417 #if wxUSE_STATUSBAR
418 PositionStatusBar();
419 #endif
420 #if wxUSE_TOOLBAR
421 PositionToolBar();
422 #endif
423 }
424
425