]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/frame.cpp
Fixing compilation on Mac, and attempting to better calculate the bottom position...
[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 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
121 const wxString& name)
122 {
123 wxStatusBar *statusBar;
124
125 statusBar = new wxStatusBar(this, id, style, name);
126 statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT);
127 statusBar->SetFieldsCount(number);
128
129 return statusBar;
130 }
131
132 void wxFrame::PositionStatusBar()
133 {
134 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
135 {
136 int w, h;
137 GetClientSize(&w, &h);
138
139 // Since we wish the status bar to be directly under the client area,
140 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
141 m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT);
142 }
143 }
144
145 // Responds to colour changes, and passes event on to children.
146 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
147 {
148 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
149 Refresh();
150
151 if ( m_frameStatusBar )
152 {
153 wxSysColourChangedEvent event2;
154
155 event2.SetEventObject( m_frameStatusBar );
156 m_frameStatusBar->ProcessEvent(event2);
157 }
158
159 // Propagate the event to the non-top-level children
160 wxWindow::OnSysColourChanged(event);
161 }
162
163 // Default activation behaviour - set the focus for the first child
164 // subwindow found.
165 void wxFrame::OnActivate(wxActivateEvent& event)
166 {
167 if ( !event.GetActive() )
168 {
169 // remember the last focused child if it is our child
170 m_winLastFocused = FindFocus();
171
172 // so we NULL it out if it's a child from some other frame
173 wxWindow *win = m_winLastFocused;
174 while ( win )
175 {
176 if ( win->IsTopLevel() )
177 {
178 if ( win != this )
179 m_winLastFocused = NULL;
180
181 break;
182 }
183
184 win = win->GetParent();
185 }
186
187 event.Skip();
188 }
189 else
190 {
191 // restore focus to the child which was last focused
192 wxWindow *parent = m_winLastFocused
193 ? m_winLastFocused->GetParent()
194 : NULL;
195
196 if (parent == NULL)
197 parent = this;
198
199 wxSetFocusToChild(parent, &m_winLastFocused);
200
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 }
215 }
216 }
217
218 void wxFrame::DetachMenuBar()
219 {
220 if ( m_frameMenuBar )
221 m_frameMenuBar->UnsetInvokingWindow();
222
223 wxFrameBase::DetachMenuBar();
224 }
225
226 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
227 {
228 wxFrame* tlf = wxDynamicCast( wxFindWinFromMacWindow( FrontNonFloatingWindow() ) , wxFrame );
229 bool makeCurrent = false;
230
231 // if this is already the current menubar or we are the frontmost window
232 if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) )
233 makeCurrent = true;
234 // or there is an app-level menubar like MDI
235 else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) )
236 makeCurrent = true;
237
238 wxFrameBase::AttachMenuBar( menuBar );
239
240 if (m_frameMenuBar)
241 {
242 m_frameMenuBar->SetInvokingWindow( this );
243 if (makeCurrent)
244 m_frameMenuBar->MacInstallMenuBar();
245 }
246 }
247
248 void wxFrame::DoGetClientSize(int *x, int *y) const
249 {
250 wxTopLevelWindow::DoGetClientSize( x , y );
251
252 #if wxUSE_STATUSBAR
253 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
254 *y -= WX_MAC_STATUSBAR_HEIGHT;
255 #endif
256
257 #if wxUSE_TOOLBAR
258 wxToolBar *toolbar = GetToolBar();
259 if ( toolbar && toolbar->IsShown() )
260 {
261 int w, h;
262 toolbar->GetSize(&w, &h);
263
264 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
265 {
266 if ( x )
267 *x -= w;
268 }
269 else
270 {
271 #if !wxMAC_USE_NATIVE_TOOLBAR
272 if ( y )
273 *y -= h;
274 #endif
275 }
276 }
277 #endif
278 }
279
280 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
281 {
282 #if wxUSE_STATUSBAR
283 if ( child == GetStatusBar() )
284 return false ;
285 #endif
286
287 #if wxUSE_TOOLBAR
288 if ( child == GetToolBar() )
289 return false ;
290 #endif
291
292 return wxFrameBase::MacIsChildOfClientArea( child ) ;
293 }
294
295 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
296 {
297 int currentclientwidth , currentclientheight ;
298 int currentwidth , currentheight ;
299
300 GetClientSize( &currentclientwidth , &currentclientheight ) ;
301 if ( clientwidth == -1 )
302 clientwidth = currentclientwidth ;
303 if ( clientheight == -1 )
304 clientheight = currentclientheight ;
305 GetSize( &currentwidth , &currentheight ) ;
306
307 // find the current client size
308
309 // Find the difference between the entire window (title bar and all) and
310 // the client area; add this to the new client size to move the window
311 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
312 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
313 }
314
315 #if wxUSE_TOOLBAR
316 void wxFrame::SetToolBar(wxToolBar *toolbar)
317 {
318 if ( m_frameToolBar == toolbar )
319 return ;
320
321 #if wxMAC_USE_NATIVE_TOOLBAR
322 if ( m_frameToolBar )
323 m_frameToolBar->MacInstallNativeToolbar( false ) ;
324 #endif
325
326 m_frameToolBar = toolbar ;
327
328 #if wxMAC_USE_NATIVE_TOOLBAR
329 if ( toolbar )
330 toolbar->MacInstallNativeToolbar( true ) ;
331 #endif
332 }
333
334 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
335 {
336 if ( wxFrameBase::CreateToolBar(style, id, name) )
337 PositionToolBar();
338
339 return m_frameToolBar;
340 }
341
342 void wxFrame::PositionToolBar()
343 {
344 int cw, ch;
345
346 GetSize( &cw , &ch ) ;
347
348 int statusX, statusY;
349 GetStatusBar()->GetClientSize(&statusX, &statusY);
350
351 if (GetStatusBar() && GetStatusBar()->IsShown())
352 {
353 ch -= statusY;
354 }
355
356 if (GetToolBar())
357 {
358 int tx, ty, tw, th;
359
360 tx = ty = 0 ;
361 GetToolBar()->GetSize(&tw, &th);
362 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
363 {
364 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
365 // means, pretend we don't have toolbar/status bar, so we
366 // have the original client size.
367 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
368 }
369 else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM)
370 {
371 //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet,
372 //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar
373 //doesn't extend it's width to the width of the frame.
374 tx = 0;
375 ty = ch - (th + statusY);
376 GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS );
377 }
378 else
379 {
380 #if !wxMAC_USE_NATIVE_TOOLBAR
381 // Use the 'real' position
382 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
383 #endif
384 }
385 }
386 }
387
388 void wxFrame::PositionBars()
389 {
390 #if wxUSE_STATUSBAR
391 PositionStatusBar();
392 #endif
393 #if wxUSE_TOOLBAR
394 PositionToolBar();
395 #endif
396 }
397
398 #endif