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