]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/frame.cpp
cleanup
[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_winLastFocused = NULL;
50 }
51
52 bool wxFrame::Create(wxWindow *parent,
53 wxWindowID id,
54 const wxString& title,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style,
58 const wxString& name)
59 {
60
61 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
62 return false;
63
64 wxModelessWindows.Append(this);
65
66 return true;
67 }
68
69 wxFrame::~wxFrame()
70 {
71 m_isBeingDeleted = true;
72 DeleteAllBars();
73 }
74
75 // get the origin of the client area in the client coordinates
76 wxPoint wxFrame::GetClientAreaOrigin() const
77 {
78 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
79
80 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
81 wxToolBar *toolbar = GetToolBar();
82 if ( toolbar && toolbar->IsShown() )
83 {
84 int w, h;
85 toolbar->GetSize(&w, &h);
86
87 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
88 {
89 pt.x += w;
90 }
91 else
92 {
93 #if !wxMAC_USE_NATIVE_TOOLBAR
94 pt.y += h;
95 #endif
96 }
97 }
98 #endif
99
100 return pt;
101 }
102
103 bool wxFrame::Enable(bool enable)
104 {
105 if ( !wxWindow::Enable(enable) )
106 return false;
107
108 if ( m_frameMenuBar && m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar() )
109 {
110 int iMaxMenu = m_frameMenuBar->GetMenuCount();
111 for ( int i = 0 ; i < iMaxMenu ; ++ i )
112 {
113 m_frameMenuBar->EnableTop( i , enable ) ;
114 }
115 }
116
117 return true;
118 }
119
120 #if wxUSE_STATUSBAR
121 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
122 const wxString& name)
123 {
124 wxStatusBar *statusBar;
125
126 statusBar = new wxStatusBar(this, id, style, name);
127 statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT);
128 statusBar->SetFieldsCount(number);
129
130 return statusBar;
131 }
132
133 void wxFrame::PositionStatusBar()
134 {
135 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
136 {
137 int w, h;
138 GetClientSize(&w, &h);
139
140 // Since we wish the status bar to be directly under the client area,
141 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
142 m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT);
143 }
144 }
145 #endif // wxUSE_STATUSBAR
146
147 // Responds to colour changes, and passes event on to children.
148 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
149 {
150 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
151 Refresh();
152
153 #if wxUSE_STATUSBAR
154 if ( m_frameStatusBar )
155 {
156 wxSysColourChangedEvent event2;
157
158 event2.SetEventObject( m_frameStatusBar );
159 m_frameStatusBar->ProcessEvent(event2);
160 }
161 #endif // wxUSE_STATUSBAR
162
163 // Propagate the event to the non-top-level children
164 wxWindow::OnSysColourChanged(event);
165 }
166
167 // Default activation behaviour - set the focus for the first child
168 // subwindow found.
169 void wxFrame::OnActivate(wxActivateEvent& event)
170 {
171 if ( !event.GetActive() )
172 {
173 // remember the last focused child if it is our child
174 m_winLastFocused = FindFocus();
175
176 // so we NULL it out if it's a child from some other frame
177 wxWindow *win = m_winLastFocused;
178 while ( win )
179 {
180 if ( win->IsTopLevel() )
181 {
182 if ( win != this )
183 m_winLastFocused = NULL;
184
185 break;
186 }
187
188 win = win->GetParent();
189 }
190
191 event.Skip();
192 }
193 else
194 {
195 // restore focus to the child which was last focused
196 wxWindow *parent = m_winLastFocused
197 ? m_winLastFocused->GetParent()
198 : NULL;
199
200 if (parent == NULL)
201 parent = this;
202
203 wxSetFocusToChild(parent, &m_winLastFocused);
204
205 if (m_frameMenuBar != NULL)
206 {
207 m_frameMenuBar->MacInstallMenuBar();
208 }
209 else
210 {
211 wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame );
212 if (tlf != NULL)
213 {
214 // Trying top-level frame membar
215 if (tlf->GetMenuBar())
216 tlf->GetMenuBar()->MacInstallMenuBar();
217 }
218 }
219 }
220 }
221
222 void wxFrame::DetachMenuBar()
223 {
224 if ( m_frameMenuBar )
225 m_frameMenuBar->UnsetInvokingWindow();
226
227 wxFrameBase::DetachMenuBar();
228 }
229
230 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
231 {
232 wxFrame* tlf = wxDynamicCast( wxFindWinFromMacWindow( FrontNonFloatingWindow() ) , wxFrame );
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 m_frameMenuBar->SetInvokingWindow( this );
247 if (makeCurrent)
248 m_frameMenuBar->MacInstallMenuBar();
249 }
250 }
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->GetWindowStyleFlag() & wxTB_VERTICAL )
269 {
270 if ( x )
271 *x -= w;
272 }
273 else
274 {
275 #if !wxMAC_USE_NATIVE_TOOLBAR
276 if ( y )
277 *y -= h;
278 #endif
279 }
280 }
281 #endif
282 }
283
284 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
285 {
286 #if wxUSE_STATUSBAR
287 if ( child == GetStatusBar() )
288 return false ;
289 #endif
290
291 #if wxUSE_TOOLBAR
292 if ( child == GetToolBar() )
293 return false ;
294 #endif
295
296 return wxFrameBase::MacIsChildOfClientArea( child ) ;
297 }
298
299 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
300 {
301 int currentclientwidth , currentclientheight ;
302 int currentwidth , currentheight ;
303
304 GetClientSize( &currentclientwidth , &currentclientheight ) ;
305 if ( clientwidth == -1 )
306 clientwidth = currentclientwidth ;
307 if ( clientheight == -1 )
308 clientheight = currentclientheight ;
309 GetSize( &currentwidth , &currentheight ) ;
310
311 // find the current client size
312
313 // Find the difference between the entire window (title bar and all) and
314 // the client area; add this to the new client size to move the window
315 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
316 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
317 }
318
319 #if wxUSE_TOOLBAR
320 void wxFrame::SetToolBar(wxToolBar *toolbar)
321 {
322 if ( m_frameToolBar == toolbar )
323 return ;
324
325 #if wxMAC_USE_NATIVE_TOOLBAR
326 if ( m_frameToolBar )
327 m_frameToolBar->MacInstallNativeToolbar( false ) ;
328 #endif
329
330 m_frameToolBar = toolbar ;
331
332 #if wxMAC_USE_NATIVE_TOOLBAR
333 if ( toolbar )
334 toolbar->MacInstallNativeToolbar( true ) ;
335 #endif
336 }
337
338 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
339 {
340 if ( wxFrameBase::CreateToolBar(style, id, name) )
341 PositionToolBar();
342
343 return m_frameToolBar;
344 }
345
346 void wxFrame::PositionToolBar()
347 {
348 int cw, ch;
349
350 GetSize( &cw , &ch ) ;
351
352 int statusX = 0 ;
353 int statusY = 0 ;
354
355 #if wxUSE_STATUSBAR
356 if (GetStatusBar() && GetStatusBar()->IsShown())
357 {
358 GetStatusBar()->GetClientSize(&statusX, &statusY);
359 ch -= statusY;
360 }
361 #endif
362
363 if (GetToolBar())
364 {
365 int tx, ty, tw, th;
366
367 tx = ty = 0 ;
368 GetToolBar()->GetSize(&tw, &th);
369 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
370 {
371 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
372 // means, pretend we don't have toolbar/status bar, so we
373 // have the original client size.
374 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
375 }
376 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM)
377 {
378 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
379 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
380 //doesn't extend it's width to the width of the frame.
381 tx = 0;
382 ty = ch - (th + statusY);
383 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
384 }
385 else
386 {
387 #if !wxMAC_USE_NATIVE_TOOLBAR
388 // Use the 'real' position
389 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
390 #endif
391 }
392 }
393 }
394 #endif // wxUSE_TOOLBAR
395
396 void wxFrame::PositionBars()
397 {
398 #if wxUSE_STATUSBAR
399 PositionStatusBar();
400 #endif
401 #if wxUSE_TOOLBAR
402 PositionToolBar();
403 #endif
404 }
405
406