]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
524c47aa | 2 | // Name: src/osx/carbon/frame.cpp |
489468fe SC |
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 | ||
524c47aa | 27 | #include "wx/osx/private.h" |
489468fe SC |
28 | |
29 | BEGIN_EVENT_TABLE(wxFrame, wxFrameBase) | |
30 | EVT_ACTIVATE(wxFrame::OnActivate) | |
489468fe | 31 | EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged) |
489468fe SC |
32 | END_EVENT_TABLE() |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow) | |
35 | ||
36 | #define WX_MAC_STATUSBAR_HEIGHT 18 | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // creation/destruction | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | void wxFrame::Init() | |
43 | { | |
44 | m_winLastFocused = NULL; | |
45 | } | |
46 | ||
47 | bool wxFrame::Create(wxWindow *parent, | |
48 | wxWindowID id, | |
49 | const wxString& title, | |
50 | const wxPoint& pos, | |
51 | const wxSize& size, | |
52 | long style, | |
53 | const wxString& name) | |
54 | { | |
55 | ||
56 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) | |
57 | return false; | |
58 | ||
59 | return true; | |
60 | } | |
61 | ||
62 | wxFrame::~wxFrame() | |
63 | { | |
64 | m_isBeingDeleted = true; | |
65 | DeleteAllBars(); | |
66 | } | |
67 | ||
68 | // get the origin of the client area in the client coordinates | |
69 | wxPoint wxFrame::GetClientAreaOrigin() const | |
70 | { | |
71 | wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); | |
72 | ||
73 | #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) | |
74 | wxToolBar *toolbar = GetToolBar(); | |
75 | if ( toolbar && toolbar->IsShown() ) | |
76 | { | |
77 | int w, h; | |
78 | toolbar->GetSize(&w, &h); | |
79 | ||
80 | if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) | |
81 | { | |
82 | pt.x += w; | |
83 | } | |
84 | else | |
85 | { | |
292e5e1f | 86 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
87 | pt.y += h; |
88 | #endif | |
89 | } | |
90 | } | |
91 | #endif | |
92 | ||
93 | return pt; | |
94 | } | |
95 | ||
96 | bool wxFrame::Enable(bool enable) | |
97 | { | |
98 | if ( !wxWindow::Enable(enable) ) | |
99 | return false; | |
100 | ||
524c47aa | 101 | #if wxUSE_MENUS |
fc7ba044 SC |
102 | // we should always enable/disable the menubar, even if we are not current, otherwise |
103 | // we might miss some state change later (happened eg in the docview sample after PrintPreview) | |
104 | if ( m_frameMenuBar /*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/) | |
489468fe SC |
105 | { |
106 | int iMaxMenu = m_frameMenuBar->GetMenuCount(); | |
107 | for ( int i = 0 ; i < iMaxMenu ; ++ i ) | |
108 | { | |
109 | m_frameMenuBar->EnableTop( i , enable ) ; | |
110 | } | |
111 | } | |
524c47aa | 112 | #endif |
489468fe SC |
113 | return true; |
114 | } | |
115 | ||
116 | #if wxUSE_STATUSBAR | |
117 | wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id, | |
118 | const wxString& name) | |
119 | { | |
120 | wxStatusBar *statusBar; | |
121 | ||
122 | statusBar = new wxStatusBar(this, id, style, name); | |
123 | statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT); | |
124 | statusBar->SetFieldsCount(number); | |
125 | ||
126 | return statusBar; | |
127 | } | |
128 | ||
129 | void wxFrame::PositionStatusBar() | |
130 | { | |
131 | if (m_frameStatusBar && m_frameStatusBar->IsShown() ) | |
132 | { | |
133 | int w, h; | |
134 | GetClientSize(&w, &h); | |
135 | ||
136 | // Since we wish the status bar to be directly under the client area, | |
137 | // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. | |
138 | m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT); | |
139 | } | |
140 | } | |
141 | #endif // wxUSE_STATUSBAR | |
142 | ||
143 | // Responds to colour changes, and passes event on to children. | |
144 | void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event) | |
145 | { | |
146 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE)); | |
147 | Refresh(); | |
148 | ||
149 | #if wxUSE_STATUSBAR | |
150 | if ( m_frameStatusBar ) | |
151 | { | |
152 | wxSysColourChangedEvent event2; | |
153 | ||
154 | event2.SetEventObject( m_frameStatusBar ); | |
155 | m_frameStatusBar->ProcessEvent(event2); | |
156 | } | |
157 | #endif // wxUSE_STATUSBAR | |
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 | ||
524c47aa | 201 | #if wxUSE_MENUS |
489468fe SC |
202 | if (m_frameMenuBar != NULL) |
203 | { | |
204 | m_frameMenuBar->MacInstallMenuBar(); | |
205 | } | |
206 | else | |
207 | { | |
208 | wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame ); | |
209 | if (tlf != NULL) | |
210 | { | |
211 | // Trying top-level frame membar | |
212 | if (tlf->GetMenuBar()) | |
213 | tlf->GetMenuBar()->MacInstallMenuBar(); | |
214 | } | |
215 | } | |
524c47aa | 216 | #endif |
489468fe SC |
217 | } |
218 | } | |
219 | ||
e0e2cbbe | 220 | void wxFrame::HandleResized( double timestampsec ) |
524c47aa SC |
221 | { |
222 | // according to the other ports we handle this within the OS level | |
223 | // resize event, not within a wxSizeEvent | |
224 | ||
225 | PositionBars(); | |
226 | ||
e0e2cbbe | 227 | wxNonOwnedWindow::HandleResized( timestampsec ); |
524c47aa SC |
228 | } |
229 | ||
230 | #if wxUSE_MENUS | |
489468fe SC |
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 | { | |
524c47aa | 241 | #if wxOSX_USE_CARBON |
b2680ced | 242 | wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame ); |
524c47aa SC |
243 | #else |
244 | wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow(); | |
245 | #endif | |
489468fe SC |
246 | bool makeCurrent = false; |
247 | ||
248 | // if this is already the current menubar or we are the frontmost window | |
249 | if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) ) | |
250 | makeCurrent = true; | |
251 | // or there is an app-level menubar like MDI | |
252 | else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) ) | |
253 | makeCurrent = true; | |
254 | ||
255 | wxFrameBase::AttachMenuBar( menuBar ); | |
256 | ||
257 | if (m_frameMenuBar) | |
258 | { | |
259 | m_frameMenuBar->SetInvokingWindow( this ); | |
260 | if (makeCurrent) | |
261 | m_frameMenuBar->MacInstallMenuBar(); | |
262 | } | |
263 | } | |
524c47aa | 264 | #endif |
489468fe SC |
265 | |
266 | void wxFrame::DoGetClientSize(int *x, int *y) const | |
267 | { | |
268 | wxTopLevelWindow::DoGetClientSize( x , y ); | |
269 | ||
270 | #if wxUSE_STATUSBAR | |
271 | if ( GetStatusBar() && GetStatusBar()->IsShown() && y ) | |
272 | *y -= WX_MAC_STATUSBAR_HEIGHT; | |
273 | #endif | |
274 | ||
275 | #if wxUSE_TOOLBAR | |
276 | wxToolBar *toolbar = GetToolBar(); | |
277 | if ( toolbar && toolbar->IsShown() ) | |
278 | { | |
279 | int w, h; | |
280 | toolbar->GetSize(&w, &h); | |
281 | ||
282 | if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) | |
283 | { | |
284 | if ( x ) | |
285 | *x -= w; | |
286 | } | |
287 | else | |
288 | { | |
292e5e1f | 289 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
290 | if ( y ) |
291 | *y -= h; | |
292 | #endif | |
293 | } | |
294 | } | |
295 | #endif | |
296 | } | |
297 | ||
298 | bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const | |
299 | { | |
300 | #if wxUSE_STATUSBAR | |
301 | if ( child == GetStatusBar() ) | |
302 | return false ; | |
303 | #endif | |
304 | ||
305 | #if wxUSE_TOOLBAR | |
306 | if ( child == GetToolBar() ) | |
307 | return false ; | |
308 | #endif | |
309 | ||
310 | return wxFrameBase::MacIsChildOfClientArea( child ) ; | |
311 | } | |
312 | ||
313 | void wxFrame::DoSetClientSize(int clientwidth, int clientheight) | |
314 | { | |
315 | int currentclientwidth , currentclientheight ; | |
316 | int currentwidth , currentheight ; | |
317 | ||
318 | GetClientSize( ¤tclientwidth , ¤tclientheight ) ; | |
319 | if ( clientwidth == -1 ) | |
320 | clientwidth = currentclientwidth ; | |
321 | if ( clientheight == -1 ) | |
322 | clientheight = currentclientheight ; | |
323 | GetSize( ¤twidth , ¤theight ) ; | |
324 | ||
325 | // find the current client size | |
326 | ||
327 | // Find the difference between the entire window (title bar and all) and | |
328 | // the client area; add this to the new client size to move the window | |
329 | DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth , | |
330 | currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ; | |
331 | } | |
332 | ||
333 | #if wxUSE_TOOLBAR | |
334 | void wxFrame::SetToolBar(wxToolBar *toolbar) | |
335 | { | |
336 | if ( m_frameToolBar == toolbar ) | |
337 | return ; | |
338 | ||
292e5e1f | 339 | #if wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
340 | if ( m_frameToolBar ) |
341 | m_frameToolBar->MacInstallNativeToolbar( false ) ; | |
342 | #endif | |
343 | ||
344 | m_frameToolBar = toolbar ; | |
345 | ||
292e5e1f | 346 | #if wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
347 | if ( toolbar ) |
348 | toolbar->MacInstallNativeToolbar( true ) ; | |
349 | #endif | |
350 | } | |
351 | ||
352 | wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name) | |
353 | { | |
354 | if ( wxFrameBase::CreateToolBar(style, id, name) ) | |
355 | PositionToolBar(); | |
356 | ||
357 | return m_frameToolBar; | |
358 | } | |
359 | ||
360 | void wxFrame::PositionToolBar() | |
361 | { | |
362 | int cw, ch; | |
363 | ||
364 | GetSize( &cw , &ch ) ; | |
365 | ||
366 | int statusX = 0 ; | |
367 | int statusY = 0 ; | |
368 | ||
369 | #if wxUSE_STATUSBAR | |
370 | if (GetStatusBar() && GetStatusBar()->IsShown()) | |
371 | { | |
372 | GetStatusBar()->GetClientSize(&statusX, &statusY); | |
373 | ch -= statusY; | |
374 | } | |
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()->GetWindowStyleFlag() & wxTB_VERTICAL) | |
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()->GetWindowStyleFlag() & wxTB_BOTTOM) | |
391 | { | |
392 | //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet, | |
393 | //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar | |
394 | //doesn't extend it's width to the width of the frame. | |
395 | tx = 0; | |
396 | ty = ch - (th + statusY); | |
397 | GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS ); | |
398 | } | |
399 | else | |
400 | { | |
292e5e1f | 401 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
402 | // Use the 'real' position |
403 | GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS ); | |
404 | #endif | |
405 | } | |
406 | } | |
407 | } | |
408 | #endif // wxUSE_TOOLBAR | |
409 | ||
410 | void wxFrame::PositionBars() | |
411 | { | |
412 | #if wxUSE_STATUSBAR | |
413 | PositionStatusBar(); | |
414 | #endif | |
415 | #if wxUSE_TOOLBAR | |
416 | PositionToolBar(); | |
417 | #endif | |
418 | } | |
419 | ||
420 |