]>
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 | ||
489468fe SC |
34 | #define WX_MAC_STATUSBAR_HEIGHT 18 |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // creation/destruction | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | void wxFrame::Init() | |
41 | { | |
42 | m_winLastFocused = NULL; | |
43 | } | |
44 | ||
45 | bool wxFrame::Create(wxWindow *parent, | |
46 | wxWindowID id, | |
47 | const wxString& title, | |
48 | const wxPoint& pos, | |
49 | const wxSize& size, | |
50 | long style, | |
51 | const wxString& name) | |
52 | { | |
489468fe SC |
53 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) |
54 | return false; | |
55 | ||
56 | return true; | |
57 | } | |
58 | ||
59 | wxFrame::~wxFrame() | |
60 | { | |
c6212a0c VZ |
61 | SendDestroyEvent(); |
62 | ||
489468fe SC |
63 | DeleteAllBars(); |
64 | } | |
65 | ||
66 | // get the origin of the client area in the client coordinates | |
67 | wxPoint wxFrame::GetClientAreaOrigin() const | |
68 | { | |
69 | wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); | |
70 | ||
71 | #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) | |
72 | wxToolBar *toolbar = GetToolBar(); | |
73 | if ( toolbar && toolbar->IsShown() ) | |
74 | { | |
75 | int w, h; | |
76 | toolbar->GetSize(&w, &h); | |
77 | ||
85c04b8f | 78 | if ( toolbar->HasFlag(wxTB_LEFT) ) |
489468fe SC |
79 | { |
80 | pt.x += w; | |
81 | } | |
aa642a45 | 82 | else if ( toolbar->HasFlag(wxTB_TOP) ) |
489468fe | 83 | { |
292e5e1f | 84 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
85 | pt.y += h; |
86 | #endif | |
87 | } | |
88 | } | |
89 | #endif | |
90 | ||
91 | return pt; | |
92 | } | |
93 | ||
94 | bool wxFrame::Enable(bool enable) | |
95 | { | |
96 | if ( !wxWindow::Enable(enable) ) | |
97 | return false; | |
98 | ||
524c47aa | 99 | #if wxUSE_MENUS |
fc7ba044 SC |
100 | // we should always enable/disable the menubar, even if we are not current, otherwise |
101 | // we might miss some state change later (happened eg in the docview sample after PrintPreview) | |
102 | if ( m_frameMenuBar /*&& m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()*/) | |
489468fe SC |
103 | { |
104 | int iMaxMenu = m_frameMenuBar->GetMenuCount(); | |
105 | for ( int i = 0 ; i < iMaxMenu ; ++ i ) | |
106 | { | |
107 | m_frameMenuBar->EnableTop( i , enable ) ; | |
108 | } | |
109 | } | |
524c47aa | 110 | #endif |
489468fe SC |
111 | return true; |
112 | } | |
113 | ||
114 | #if wxUSE_STATUSBAR | |
115 | wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id, | |
116 | const wxString& name) | |
117 | { | |
118 | wxStatusBar *statusBar; | |
119 | ||
120 | statusBar = new wxStatusBar(this, id, style, name); | |
121 | statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT); | |
122 | statusBar->SetFieldsCount(number); | |
123 | ||
124 | return statusBar; | |
125 | } | |
126 | ||
127 | void wxFrame::PositionStatusBar() | |
128 | { | |
129 | if (m_frameStatusBar && m_frameStatusBar->IsShown() ) | |
130 | { | |
131 | int w, h; | |
132 | GetClientSize(&w, &h); | |
133 | ||
134 | // Since we wish the status bar to be directly under the client area, | |
135 | // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. | |
136 | m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT); | |
137 | } | |
138 | } | |
139 | #endif // wxUSE_STATUSBAR | |
140 | ||
141 | // Responds to colour changes, and passes event on to children. | |
142 | void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event) | |
143 | { | |
144 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE)); | |
145 | Refresh(); | |
146 | ||
147 | #if wxUSE_STATUSBAR | |
148 | if ( m_frameStatusBar ) | |
149 | { | |
150 | wxSysColourChangedEvent event2; | |
151 | ||
152 | event2.SetEventObject( m_frameStatusBar ); | |
ac17f728 | 153 | m_frameStatusBar->GetEventHandler()->ProcessEvent(event2); |
489468fe SC |
154 | } |
155 | #endif // wxUSE_STATUSBAR | |
156 | ||
157 | // Propagate the event to the non-top-level children | |
158 | wxWindow::OnSysColourChanged(event); | |
159 | } | |
160 | ||
161 | // Default activation behaviour - set the focus for the first child | |
162 | // subwindow found. | |
163 | void wxFrame::OnActivate(wxActivateEvent& event) | |
164 | { | |
165 | if ( !event.GetActive() ) | |
166 | { | |
167 | // remember the last focused child if it is our child | |
168 | m_winLastFocused = FindFocus(); | |
169 | ||
170 | // so we NULL it out if it's a child from some other frame | |
171 | wxWindow *win = m_winLastFocused; | |
172 | while ( win ) | |
173 | { | |
174 | if ( win->IsTopLevel() ) | |
175 | { | |
176 | if ( win != this ) | |
177 | m_winLastFocused = NULL; | |
178 | ||
179 | break; | |
180 | } | |
181 | ||
182 | win = win->GetParent(); | |
183 | } | |
184 | ||
185 | event.Skip(); | |
186 | } | |
187 | else | |
188 | { | |
189 | // restore focus to the child which was last focused | |
190 | wxWindow *parent = m_winLastFocused | |
191 | ? m_winLastFocused->GetParent() | |
192 | : NULL; | |
193 | ||
194 | if (parent == NULL) | |
195 | parent = this; | |
196 | ||
197 | wxSetFocusToChild(parent, &m_winLastFocused); | |
198 | ||
524c47aa | 199 | #if wxUSE_MENUS |
489468fe SC |
200 | if (m_frameMenuBar != NULL) |
201 | { | |
202 | m_frameMenuBar->MacInstallMenuBar(); | |
203 | } | |
204 | else | |
205 | { | |
206 | wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame ); | |
207 | if (tlf != NULL) | |
208 | { | |
209 | // Trying top-level frame membar | |
210 | if (tlf->GetMenuBar()) | |
211 | tlf->GetMenuBar()->MacInstallMenuBar(); | |
212 | } | |
213 | } | |
524c47aa | 214 | #endif |
489468fe SC |
215 | } |
216 | } | |
217 | ||
524c47aa | 218 | #if wxUSE_MENUS |
489468fe SC |
219 | void wxFrame::DetachMenuBar() |
220 | { | |
489468fe SC |
221 | wxFrameBase::DetachMenuBar(); |
222 | } | |
223 | ||
224 | void wxFrame::AttachMenuBar( wxMenuBar *menuBar ) | |
225 | { | |
524c47aa | 226 | #if wxOSX_USE_CARBON |
b2680ced | 227 | wxFrame* tlf = wxDynamicCast( wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontNonFloatingWindow() ) , wxFrame ); |
524c47aa SC |
228 | #else |
229 | wxFrame* tlf = (wxFrame*) wxTheApp->GetTopWindow(); | |
230 | #endif | |
489468fe SC |
231 | bool makeCurrent = false; |
232 | ||
233 | // if this is already the current menubar or we are the frontmost window | |
234 | if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) ) | |
235 | makeCurrent = true; | |
236 | // or there is an app-level menubar like MDI | |
237 | else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) ) | |
238 | makeCurrent = true; | |
239 | ||
240 | wxFrameBase::AttachMenuBar( menuBar ); | |
241 | ||
242 | if (m_frameMenuBar) | |
243 | { | |
489468fe SC |
244 | if (makeCurrent) |
245 | m_frameMenuBar->MacInstallMenuBar(); | |
246 | } | |
247 | } | |
524c47aa | 248 | #endif |
489468fe SC |
249 | |
250 | void wxFrame::DoGetClientSize(int *x, int *y) const | |
251 | { | |
252 | wxTopLevelWindow::DoGetClientSize( x , y ); | |
253 | ||
254 | #if wxUSE_STATUSBAR | |
255 | if ( GetStatusBar() && GetStatusBar()->IsShown() && y ) | |
256 | *y -= WX_MAC_STATUSBAR_HEIGHT; | |
257 | #endif | |
258 | ||
259 | #if wxUSE_TOOLBAR | |
260 | wxToolBar *toolbar = GetToolBar(); | |
261 | if ( toolbar && toolbar->IsShown() ) | |
262 | { | |
263 | int w, h; | |
264 | toolbar->GetSize(&w, &h); | |
265 | ||
baac7154 | 266 | if ( toolbar->IsVertical() ) |
489468fe SC |
267 | { |
268 | if ( x ) | |
269 | *x -= w; | |
270 | } | |
baac7154 SC |
271 | else if ( toolbar->HasFlag( wxTB_BOTTOM ) ) |
272 | { | |
273 | if ( y ) | |
274 | *y -= h; | |
275 | } | |
489468fe SC |
276 | else |
277 | { | |
292e5e1f | 278 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
279 | if ( y ) |
280 | *y -= h; | |
281 | #endif | |
282 | } | |
283 | } | |
284 | #endif | |
285 | } | |
286 | ||
287 | bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const | |
288 | { | |
289 | #if wxUSE_STATUSBAR | |
290 | if ( child == GetStatusBar() ) | |
291 | return false ; | |
292 | #endif | |
293 | ||
294 | #if wxUSE_TOOLBAR | |
295 | if ( child == GetToolBar() ) | |
296 | return false ; | |
297 | #endif | |
298 | ||
299 | return wxFrameBase::MacIsChildOfClientArea( child ) ; | |
300 | } | |
301 | ||
302 | void wxFrame::DoSetClientSize(int clientwidth, int clientheight) | |
303 | { | |
304 | int currentclientwidth , currentclientheight ; | |
305 | int currentwidth , currentheight ; | |
306 | ||
307 | GetClientSize( ¤tclientwidth , ¤tclientheight ) ; | |
308 | if ( clientwidth == -1 ) | |
309 | clientwidth = currentclientwidth ; | |
310 | if ( clientheight == -1 ) | |
311 | clientheight = currentclientheight ; | |
312 | GetSize( ¤twidth , ¤theight ) ; | |
313 | ||
314 | // find the current client size | |
315 | ||
316 | // Find the difference between the entire window (title bar and all) and | |
317 | // the client area; add this to the new client size to move the window | |
318 | DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth , | |
319 | currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ; | |
320 | } | |
321 | ||
322 | #if wxUSE_TOOLBAR | |
323 | void wxFrame::SetToolBar(wxToolBar *toolbar) | |
324 | { | |
325 | if ( m_frameToolBar == toolbar ) | |
326 | return ; | |
327 | ||
85c04b8f | 328 | #ifndef __WXOSX_IPHONE__ |
292e5e1f | 329 | #if wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
330 | if ( m_frameToolBar ) |
331 | m_frameToolBar->MacInstallNativeToolbar( false ) ; | |
332 | #endif | |
85c04b8f | 333 | #endif |
489468fe SC |
334 | m_frameToolBar = toolbar ; |
335 | ||
85c04b8f | 336 | #ifndef __WXOSX_IPHONE__ |
292e5e1f | 337 | #if wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
338 | if ( toolbar ) |
339 | toolbar->MacInstallNativeToolbar( true ) ; | |
340 | #endif | |
85c04b8f | 341 | #endif |
489468fe SC |
342 | } |
343 | ||
344 | wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name) | |
345 | { | |
346 | if ( wxFrameBase::CreateToolBar(style, id, name) ) | |
347 | PositionToolBar(); | |
348 | ||
349 | return m_frameToolBar; | |
350 | } | |
351 | ||
352 | void wxFrame::PositionToolBar() | |
353 | { | |
354 | int cw, ch; | |
355 | ||
baac7154 | 356 | wxTopLevelWindow::DoGetClientSize( &cw , &ch ); |
c6212a0c | 357 | |
489468fe SC |
358 | int statusX = 0 ; |
359 | int statusY = 0 ; | |
360 | ||
361 | #if wxUSE_STATUSBAR | |
362 | if (GetStatusBar() && GetStatusBar()->IsShown()) | |
363 | { | |
baac7154 | 364 | GetStatusBar()->GetSize(&statusX, &statusY); |
489468fe SC |
365 | ch -= statusY; |
366 | } | |
367 | #endif | |
368 | ||
85c04b8f SC |
369 | #ifdef __WXOSX_IPHONE__ |
370 | // TODO integrate this in a better way, on iphone the status bar is not a child of the content view | |
371 | // but the toolbar is | |
372 | ch -= 20; | |
373 | #endif | |
374 | ||
489468fe SC |
375 | if (GetToolBar()) |
376 | { | |
377 | int tx, ty, tw, th; | |
378 | ||
379 | tx = ty = 0 ; | |
380 | GetToolBar()->GetSize(&tw, &th); | |
baac7154 SC |
381 | if (GetToolBar()->HasFlag(wxTB_LEFT)) |
382 | { | |
383 | // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS | |
384 | // means, pretend we don't have toolbar/status bar, so we | |
385 | // have the original client size. | |
386 | GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS ); | |
387 | } | |
388 | else if (GetToolBar()->HasFlag(wxTB_RIGHT)) | |
489468fe SC |
389 | { |
390 | // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS | |
391 | // means, pretend we don't have toolbar/status bar, so we | |
392 | // have the original client size. | |
baac7154 | 393 | tx = cw - tw; |
489468fe SC |
394 | GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS ); |
395 | } | |
baac7154 | 396 | else if (GetToolBar()->HasFlag(wxTB_BOTTOM)) |
489468fe | 397 | { |
489468fe | 398 | tx = 0; |
baac7154 | 399 | ty = ch - th; |
489468fe SC |
400 | GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS ); |
401 | } | |
402 | else | |
403 | { | |
292e5e1f | 404 | #if !wxOSX_USE_NATIVE_TOOLBAR |
489468fe SC |
405 | // Use the 'real' position |
406 | GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS ); | |
407 | #endif | |
408 | } | |
409 | } | |
410 | } | |
411 | #endif // wxUSE_TOOLBAR | |
412 | ||
413 | void wxFrame::PositionBars() | |
414 | { | |
415 | #if wxUSE_STATUSBAR | |
416 | PositionStatusBar(); | |
417 | #endif | |
418 | #if wxUSE_TOOLBAR | |
419 | PositionToolBar(); | |
420 | #endif | |
421 | } | |
422 | ||
423 |