]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/frame.cpp
Include wx/stopwatch.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/uma.h"
28
29 extern wxWindowList wxModelessWindows;
30
31 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
32 EVT_ACTIVATE(wxFrame::OnActivate)
33 // EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
34 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
35 // EVT_IDLE(wxFrame::OnIdle)
36 // EVT_CLOSE(wxFrame::OnCloseWindow)
37 END_EVENT_TABLE()
38
39 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
40
41 #define WX_MAC_STATUSBAR_HEIGHT 18
42
43 // ----------------------------------------------------------------------------
44 // creation/destruction
45 // ----------------------------------------------------------------------------
46
47 void wxFrame::Init()
48 {
49 m_frameMenuBar = NULL;
50 m_frameStatusBar = NULL;
51 m_winLastFocused = NULL;
52
53 #if wxUSE_TOOLBAR
54 m_frameToolBar = NULL;
55 #endif
56
57 #if wxUSE_TOOLTIPS
58 // NB: is this used anywhere?
59 m_hwndToolTip = NULL;
60 #endif
61
62 m_iconized = false;
63 }
64
65 bool wxFrame::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxString& title,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73
74 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
75 return false;
76
77 wxModelessWindows.Append(this);
78
79 return true;
80 }
81
82 wxFrame::~wxFrame()
83 {
84 m_isBeingDeleted = true;
85 DeleteAllBars();
86 }
87
88 // get the origin of the client area in the client coordinates
89 wxPoint wxFrame::GetClientAreaOrigin() const
90 {
91 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
92
93 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
94 wxToolBar *toolbar = GetToolBar();
95 if ( toolbar && toolbar->IsShown() )
96 {
97 int w, h;
98 toolbar->GetSize(&w, &h);
99
100 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
101 {
102 pt.x += w;
103 }
104 else
105 {
106 #if !wxMAC_USE_NATIVE_TOOLBAR
107 pt.y += h;
108 #endif
109 }
110 }
111 #endif
112
113 return pt;
114 }
115
116 bool wxFrame::Enable(bool enable)
117 {
118 if ( !wxWindow::Enable(enable) )
119 return false;
120
121 if ( m_frameMenuBar && m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar() )
122 {
123 int iMaxMenu = m_frameMenuBar->GetMenuCount();
124 for ( int i = 0 ; i < iMaxMenu ; ++ i )
125 {
126 m_frameMenuBar->EnableTop( i , enable ) ;
127 }
128 }
129
130 return true;
131 }
132
133 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
134 const wxString& name)
135 {
136 wxStatusBar *statusBar;
137
138 statusBar = new wxStatusBar(this, id, style, name);
139 statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT);
140 statusBar->SetFieldsCount(number);
141
142 return statusBar;
143 }
144
145 void wxFrame::PositionStatusBar()
146 {
147 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
148 {
149 int w, h;
150 GetClientSize(&w, &h);
151
152 // Since we wish the status bar to be directly under the client area,
153 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
154 m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT);
155 }
156 }
157
158 // Responds to colour changes, and passes event on to children.
159 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
160 {
161 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
162 Refresh();
163
164 if ( m_frameStatusBar )
165 {
166 wxSysColourChangedEvent event2;
167
168 event2.SetEventObject( m_frameStatusBar );
169 m_frameStatusBar->ProcessEvent(event2);
170 }
171
172 // Propagate the event to the non-top-level children
173 wxWindow::OnSysColourChanged(event);
174 }
175
176 // Default activation behaviour - set the focus for the first child
177 // subwindow found.
178 void wxFrame::OnActivate(wxActivateEvent& event)
179 {
180 if ( !event.GetActive() )
181 {
182 // remember the last focused child if it is our child
183 m_winLastFocused = FindFocus();
184
185 // so we NULL it out if it's a child from some other frame
186 wxWindow *win = m_winLastFocused;
187 while ( win )
188 {
189 if ( win->IsTopLevel() )
190 {
191 if ( win != this )
192 m_winLastFocused = NULL;
193
194 break;
195 }
196
197 win = win->GetParent();
198 }
199
200 event.Skip();
201 }
202 else
203 {
204 // restore focus to the child which was last focused
205 wxWindow *parent = m_winLastFocused
206 ? m_winLastFocused->GetParent()
207 : NULL;
208
209 if (parent == NULL)
210 parent = this;
211
212 wxSetFocusToChild(parent, &m_winLastFocused);
213
214 if (m_frameMenuBar != NULL)
215 {
216 m_frameMenuBar->MacInstallMenuBar();
217 }
218 else
219 {
220 wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame );
221 if (tlf != NULL)
222 {
223 // Trying top-level frame membar
224 if (tlf->GetMenuBar())
225 tlf->GetMenuBar()->MacInstallMenuBar();
226 }
227 }
228 }
229 }
230
231 void wxFrame::DetachMenuBar()
232 {
233 if ( m_frameMenuBar )
234 m_frameMenuBar->UnsetInvokingWindow();
235
236 wxFrameBase::DetachMenuBar();
237 }
238
239 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
240 {
241 wxFrame* tlf = wxDynamicCast( wxFindWinFromMacWindow( FrontNonFloatingWindow() ) , wxFrame );
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 {
255 m_frameMenuBar->SetInvokingWindow( this );
256 if (makeCurrent)
257 m_frameMenuBar->MacInstallMenuBar();
258 }
259 }
260
261 void 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 {
284 #if !wxMAC_USE_NATIVE_TOOLBAR
285 if ( y )
286 *y -= h;
287 #endif
288 }
289 }
290 #endif
291 }
292
293 bool 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
308 void 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
329 void wxFrame::SetToolBar(wxToolBar *toolbar)
330 {
331 if ( m_frameToolBar == toolbar )
332 return ;
333
334 #if wxMAC_USE_NATIVE_TOOLBAR
335 if ( m_frameToolBar )
336 m_frameToolBar->MacInstallNativeToolbar( false ) ;
337 #endif
338
339 m_frameToolBar = toolbar ;
340
341 #if wxMAC_USE_NATIVE_TOOLBAR
342 if ( toolbar )
343 toolbar->MacInstallNativeToolbar( true ) ;
344 #endif
345 }
346
347 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
348 {
349 if ( wxFrameBase::CreateToolBar(style, id, name) )
350 PositionToolBar();
351
352 return m_frameToolBar;
353 }
354
355 void wxFrame::PositionToolBar()
356 {
357 int cw, ch;
358
359 GetSize( &cw , &ch ) ;
360
361 if (GetStatusBar() && GetStatusBar()->IsShown())
362 {
363 int statusX, statusY;
364
365 GetStatusBar()->GetClientSize(&statusX, &statusY);
366 ch -= statusY;
367 }
368
369 if (GetToolBar())
370 {
371 int tx, ty, tw, th;
372
373 tx = ty = 0 ;
374 GetToolBar()->GetSize(&tw, &th);
375 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
376 {
377 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
378 // means, pretend we don't have toolbar/status bar, so we
379 // have the original client size.
380 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
381 }
382 else
383 {
384 #if !wxMAC_USE_NATIVE_TOOLBAR
385 // Use the 'real' position
386 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
387 #endif
388 }
389 }
390 }
391
392 void wxFrame::PositionBars()
393 {
394 #if wxUSE_STATUSBAR
395 PositionStatusBar();
396 #endif
397 #if wxUSE_TOOLBAR
398 PositionToolBar();
399 #endif
400 }
401
402 #endif