]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/frame.cpp
dbc406c76886dfd7f2fd7d1f24665d6c525b4fb7
[wxWidgets.git] / src / mac / carbon / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/statusbr.h"
16 #include "wx/toolbar.h"
17 #include "wx/menuitem.h"
18 #include "wx/menu.h"
19 #include "wx/dcclient.h"
20 #include "wx/dialog.h"
21 #include "wx/settings.h"
22 #include "wx/app.h"
23
24 #include "wx/mac/uma.h"
25
26 extern wxWindowList wxModelessWindows;
27 extern wxList wxPendingDelete;
28
29 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
30 EVT_ACTIVATE(wxFrame::OnActivate)
31 // EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
32 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
33 // EVT_IDLE(wxFrame::OnIdle)
34 // EVT_CLOSE(wxFrame::OnCloseWindow)
35 END_EVENT_TABLE()
36
37 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
38
39 #define WX_MAC_STATUSBAR_HEIGHT 18
40
41 // ----------------------------------------------------------------------------
42 // creation/destruction
43 // ----------------------------------------------------------------------------
44
45 void wxFrame::Init()
46 {
47 m_frameMenuBar = NULL;
48 m_frameStatusBar = NULL;
49 m_winLastFocused = NULL;
50
51 #if wxUSE_TOOLBAR
52 m_frameToolBar = NULL;
53 #endif
54
55 #if wxUSE_TOOLTIPS
56 // NB: is this used anywhere?
57 m_hwndToolTip = NULL;
58 #endif
59
60 m_iconized = false;
61 }
62
63 bool wxFrame::Create(wxWindow *parent,
64 wxWindowID id,
65 const wxString& title,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 const wxString& name)
70 {
71
72 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
73 return false;
74
75 wxModelessWindows.Append(this);
76
77 return true;
78 }
79
80 wxFrame::~wxFrame()
81 {
82 m_isBeingDeleted = true;
83 DeleteAllBars();
84 }
85
86 // get the origin of the client area in the client coordinates
87 wxPoint wxFrame::GetClientAreaOrigin() const
88 {
89 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
90
91 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
92 wxToolBar *toolbar = GetToolBar();
93 if ( toolbar && toolbar->IsShown() )
94 {
95 int w, h;
96 toolbar->GetSize(&w, &h);
97
98 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
99 {
100 pt.x += w;
101 }
102 else
103 {
104 #if !wxMAC_USE_NATIVE_TOOLBAR
105 pt.y += h;
106 #endif
107 }
108 }
109 #endif
110
111 return pt;
112 }
113
114 bool wxFrame::Enable(bool enable)
115 {
116 if ( !wxWindow::Enable(enable) )
117 return false;
118
119 if ( m_frameMenuBar && m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar() )
120 {
121 int iMaxMenu = m_frameMenuBar->GetMenuCount();
122 for ( int i = 0 ; i < iMaxMenu ; ++ i )
123 {
124 m_frameMenuBar->EnableTop( i , enable ) ;
125 }
126 }
127
128 return true;
129 }
130
131 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
132 const wxString& name)
133 {
134 wxStatusBar *statusBar;
135
136 statusBar = new wxStatusBar(this, id, style, name);
137 statusBar->SetSize(100 , WX_MAC_STATUSBAR_HEIGHT);
138 statusBar->SetFieldsCount(number);
139
140 return statusBar;
141 }
142
143 void wxFrame::PositionStatusBar()
144 {
145 if (m_frameStatusBar && m_frameStatusBar->IsShown() )
146 {
147 int w, h;
148 GetClientSize(&w, &h);
149
150 // Since we wish the status bar to be directly under the client area,
151 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
152 m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT);
153 }
154 }
155
156 // Responds to colour changes, and passes event on to children.
157 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
158 {
159 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
160 Refresh();
161
162 if ( m_frameStatusBar )
163 {
164 wxSysColourChangedEvent event2;
165
166 event2.SetEventObject( m_frameStatusBar );
167 m_frameStatusBar->ProcessEvent(event2);
168 }
169
170 // Propagate the event to the non-top-level children
171 wxWindow::OnSysColourChanged(event);
172 }
173
174 // Default activation behaviour - set the focus for the first child
175 // subwindow found.
176 void wxFrame::OnActivate(wxActivateEvent& event)
177 {
178 if ( !event.GetActive() )
179 {
180 // remember the last focused child if it is our child
181 m_winLastFocused = FindFocus();
182
183 // so we NULL it out if it's a child from some other frame
184 wxWindow *win = m_winLastFocused;
185 while ( win )
186 {
187 if ( win->IsTopLevel() )
188 {
189 if ( win != this )
190 m_winLastFocused = NULL;
191
192 break;
193 }
194
195 win = win->GetParent();
196 }
197
198 event.Skip();
199 }
200 else
201 {
202 // restore focus to the child which was last focused
203 wxWindow *parent = m_winLastFocused
204 ? m_winLastFocused->GetParent()
205 : NULL;
206
207 if ( !parent )
208 parent = this;
209
210 wxSetFocusToChild(parent, &m_winLastFocused);
211
212 if (m_frameMenuBar != NULL)
213 {
214 m_frameMenuBar->MacInstallMenuBar() ;
215 }
216 else if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)))
217 {
218 // Trying toplevel frame membar
219 if (((wxFrame*)wxTheApp->GetTopWindow())->GetMenuBar())
220 ((wxFrame*)wxTheApp->GetTopWindow())->GetMenuBar()->MacInstallMenuBar();
221 }
222 }
223 }
224
225 void wxFrame::DetachMenuBar()
226 {
227 if ( m_frameMenuBar )
228 m_frameMenuBar->UnsetInvokingWindow();
229
230 wxFrameBase::DetachMenuBar();
231 }
232
233 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
234 {
235 wxToplLevelWindowMac* tlw = wxFindWinFromMacWindow(FrontNonFloatingWindow()) ;
236
237 bool makeCurrent = false ;
238
239 // if this is already the current menubar or we are the frontmost window
240 if ( m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar() || tlw == this )
241 makeCurrent = true ;
242 // or we have a situation where this is a App Level Menubar like MDI
243 else if ( tlw != NULL && tlw->GetMenuBar() == NULL && ((wxFrame*)wxTheApp->GetTopWindow()) == this )
244 makeCurrent = true ;
245
246 wxFrameBase::AttachMenuBar(menuBar);
247
248 if (m_frameMenuBar)
249 {
250 m_frameMenuBar->SetInvokingWindow( this );
251 if (makeCurrent)
252 m_frameMenuBar->MacInstallMenuBar() ;
253 }
254 }
255
256 void wxFrame::DoGetClientSize(int *x, int *y) const
257 {
258 wxTopLevelWindow::DoGetClientSize( x , y );
259
260 #if wxUSE_STATUSBAR
261 if ( GetStatusBar() && GetStatusBar()->IsShown() && y )
262 *y -= WX_MAC_STATUSBAR_HEIGHT;
263 #endif
264
265 #if wxUSE_TOOLBAR
266 wxToolBar *toolbar = GetToolBar();
267 if ( toolbar && toolbar->IsShown() )
268 {
269 int w, h;
270 toolbar->GetSize(&w, &h);
271
272 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
273 {
274 if ( x )
275 *x -= w;
276 }
277 else
278 {
279 #if !wxMAC_USE_NATIVE_TOOLBAR
280 if ( y )
281 *y -= h;
282 #endif
283 }
284 }
285 #endif
286 }
287
288 bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const
289 {
290 #if wxUSE_STATUSBAR
291 if ( child == GetStatusBar() )
292 return false ;
293 #endif
294
295 #if wxUSE_TOOLBAR
296 if ( child == GetToolBar() )
297 return false ;
298 #endif
299
300 return wxFrameBase::MacIsChildOfClientArea( child ) ;
301 }
302
303 void wxFrame::DoSetClientSize(int clientwidth, int clientheight)
304 {
305 int currentclientwidth , currentclientheight ;
306 int currentwidth , currentheight ;
307
308 GetClientSize( &currentclientwidth , &currentclientheight ) ;
309 if ( clientwidth == -1 )
310 clientwidth = currentclientwidth ;
311 if ( clientheight == -1 )
312 clientheight = currentclientheight ;
313 GetSize( &currentwidth , &currentheight ) ;
314
315 // find the current client size
316
317 // Find the difference between the entire window (title bar and all) and
318 // the client area; add this to the new client size to move the window
319 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
320 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
321 }
322
323 #if wxUSE_TOOLBAR
324 void wxFrame::SetToolBar(wxToolBar *toolbar)
325 {
326 if ( m_frameToolBar == toolbar )
327 return ;
328
329 #if wxMAC_USE_NATIVE_TOOLBAR
330 if ( m_frameToolBar )
331 m_frameToolBar->MacInstallNativeToolbar( false ) ;
332 #endif
333
334 m_frameToolBar = toolbar ;
335
336 #if wxMAC_USE_NATIVE_TOOLBAR
337 if ( toolbar )
338 toolbar->MacInstallNativeToolbar( true ) ;
339 #endif
340 }
341
342 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
343 {
344 if ( wxFrameBase::CreateToolBar(style, id, name) )
345 PositionToolBar();
346
347 return m_frameToolBar;
348 }
349
350 void wxFrame::PositionToolBar()
351 {
352 int cw, ch;
353
354 GetSize( &cw , &ch ) ;
355
356 if (GetStatusBar() && GetStatusBar()->IsShown())
357 {
358 int statusX, statusY;
359
360 GetStatusBar()->GetClientSize(&statusX, &statusY);
361 ch -= statusY;
362 }
363
364 if (GetToolBar())
365 {
366 int tx, ty, tw, th;
367
368 tx = ty = 0 ;
369 GetToolBar()->GetSize(&tw, &th);
370 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
371 {
372 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
373 // means, pretend we don't have toolbar/status bar, so we
374 // have the original client size.
375 GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
376 }
377 else
378 {
379 #if !wxMAC_USE_NATIVE_TOOLBAR
380 // Use the 'real' position
381 GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS );
382 #endif
383 }
384 }
385 }
386 #endif
387