]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/framecmn.cpp | |
3 | // Purpose: common (for all platforms) wxFrame functions | |
4 | // Author: Julian Smart, Vadim Zeitlin | |
5 | // Created: 01/02/97 | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling and Julian Smart | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #include "wx/frame.h" | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/menu.h" | |
30 | #include "wx/menuitem.h" | |
31 | #include "wx/dcclient.h" | |
32 | #include "wx/toolbar.h" | |
33 | #include "wx/statusbr.h" | |
34 | #endif // WX_PRECOMP | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // event table | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | #if wxUSE_MENUS && wxUSE_STATUSBAR | |
41 | ||
42 | BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow) | |
43 | EVT_MENU_OPEN(wxFrameBase::OnMenuOpen) | |
44 | EVT_MENU_CLOSE(wxFrameBase::OnMenuClose) | |
45 | ||
46 | EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight) | |
47 | END_EVENT_TABLE() | |
48 | ||
49 | #endif // wxUSE_MENUS && wxUSE_STATUSBAR | |
50 | ||
51 | // ============================================================================ | |
52 | // implementation | |
53 | // ============================================================================ | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // construction/destruction | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | wxFrameBase::wxFrameBase() | |
60 | { | |
61 | #if wxUSE_MENUS | |
62 | m_frameMenuBar = NULL; | |
63 | #endif // wxUSE_MENUS | |
64 | ||
65 | #if wxUSE_TOOLBAR | |
66 | m_frameToolBar = NULL; | |
67 | #endif // wxUSE_TOOLBAR | |
68 | ||
69 | #if wxUSE_STATUSBAR | |
70 | m_frameStatusBar = NULL; | |
71 | #endif // wxUSE_STATUSBAR | |
72 | ||
73 | m_statusBarPane = 0; | |
74 | } | |
75 | ||
76 | wxFrameBase::~wxFrameBase() | |
77 | { | |
78 | // this destructor is required for Darwin | |
79 | } | |
80 | ||
81 | wxFrame *wxFrameBase::New(wxWindow *parent, | |
82 | wxWindowID id, | |
83 | const wxString& title, | |
84 | const wxPoint& pos, | |
85 | const wxSize& size, | |
86 | long style, | |
87 | const wxString& name) | |
88 | { | |
89 | return new wxFrame(parent, id, title, pos, size, style, name); | |
90 | } | |
91 | ||
92 | void wxFrameBase::DeleteAllBars() | |
93 | { | |
94 | #if wxUSE_MENUS | |
95 | if ( m_frameMenuBar ) | |
96 | { | |
97 | delete m_frameMenuBar; | |
98 | m_frameMenuBar = NULL; | |
99 | } | |
100 | #endif // wxUSE_MENUS | |
101 | ||
102 | #if wxUSE_STATUSBAR | |
103 | if ( m_frameStatusBar ) | |
104 | { | |
105 | delete m_frameStatusBar; | |
106 | m_frameStatusBar = NULL; | |
107 | } | |
108 | #endif // wxUSE_STATUSBAR | |
109 | ||
110 | #if wxUSE_TOOLBAR | |
111 | if ( m_frameToolBar ) | |
112 | { | |
113 | delete m_frameToolBar; | |
114 | m_frameToolBar = NULL; | |
115 | } | |
116 | #endif // wxUSE_TOOLBAR | |
117 | } | |
118 | ||
119 | bool wxFrameBase::IsOneOfBars(const wxWindow *win) const | |
120 | { | |
121 | #if wxUSE_MENUS | |
122 | if ( win == GetMenuBar() ) | |
123 | return true; | |
124 | #endif // wxUSE_MENUS | |
125 | ||
126 | #if wxUSE_STATUSBAR | |
127 | if ( win == GetStatusBar() ) | |
128 | return true; | |
129 | #endif // wxUSE_STATUSBAR | |
130 | ||
131 | #if wxUSE_TOOLBAR | |
132 | if ( win == GetToolBar() ) | |
133 | return true; | |
134 | #endif // wxUSE_TOOLBAR | |
135 | ||
136 | wxUnusedVar(win); | |
137 | ||
138 | return false; | |
139 | } | |
140 | ||
141 | // ---------------------------------------------------------------------------- | |
142 | // wxFrame size management: we exclude the areas taken by menu/status/toolbars | |
143 | // from the client area, so the client area is what's really available for the | |
144 | // frame contents | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
147 | // get the origin of the client area in the client coordinates | |
148 | wxPoint wxFrameBase::GetClientAreaOrigin() const | |
149 | { | |
150 | wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); | |
151 | ||
152 | #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) | |
153 | wxToolBar *toolbar = GetToolBar(); | |
154 | if ( toolbar && toolbar->IsShown() ) | |
155 | { | |
156 | int w, h; | |
157 | toolbar->GetSize(&w, &h); | |
158 | ||
159 | if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) | |
160 | { | |
161 | pt.x += w; | |
162 | } | |
163 | else | |
164 | { | |
165 | pt.y += h; | |
166 | } | |
167 | } | |
168 | #endif // wxUSE_TOOLBAR | |
169 | ||
170 | return pt; | |
171 | } | |
172 | ||
173 | // ---------------------------------------------------------------------------- | |
174 | // misc | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | #if wxUSE_MENUS | |
178 | ||
179 | bool wxFrameBase::ProcessCommand(int id) | |
180 | { | |
181 | wxMenuBar *bar = GetMenuBar(); | |
182 | if ( !bar ) | |
183 | return false; | |
184 | ||
185 | wxMenuItem *item = bar->FindItem(id); | |
186 | if ( !item ) | |
187 | return false; | |
188 | ||
189 | return ProcessCommand(item); | |
190 | } | |
191 | ||
192 | bool wxFrameBase::ProcessCommand(wxMenuItem *item) | |
193 | { | |
194 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
195 | commandEvent.SetEventObject(this); | |
196 | ||
197 | if (!item->IsEnabled()) | |
198 | return true; | |
199 | ||
200 | if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() ) | |
201 | return true; | |
202 | ||
203 | if (item->IsCheckable()) | |
204 | { | |
205 | item->Toggle(); | |
206 | ||
207 | // use the new value | |
208 | commandEvent.SetInt(item->IsChecked()); | |
209 | } | |
210 | ||
211 | return HandleWindowEvent(commandEvent); | |
212 | } | |
213 | ||
214 | #endif // wxUSE_MENUS | |
215 | ||
216 | // Do the UI update processing for this window. This is | |
217 | // provided for the application to call if it wants to | |
218 | // force a UI update, particularly for the menus and toolbar. | |
219 | void wxFrameBase::UpdateWindowUI(long flags) | |
220 | { | |
221 | wxWindowBase::UpdateWindowUI(flags); | |
222 | ||
223 | #if wxUSE_TOOLBAR | |
224 | if (GetToolBar()) | |
225 | GetToolBar()->UpdateWindowUI(flags); | |
226 | #endif | |
227 | ||
228 | #if wxUSE_MENUS | |
229 | if (GetMenuBar()) | |
230 | { | |
231 | // If coming from an idle event, we only want to update the menus if | |
232 | // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they | |
233 | // will be update when the menu is opened later | |
234 | #if !wxUSE_IDLEMENUUPDATES | |
235 | if ( !(flags & wxUPDATE_UI_FROMIDLE) ) | |
236 | #endif // wxUSE_IDLEMENUUPDATES | |
237 | DoMenuUpdates(); | |
238 | } | |
239 | #endif // wxUSE_MENUS | |
240 | } | |
241 | ||
242 | // ---------------------------------------------------------------------------- | |
243 | // event handlers for status bar updates from menus | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
246 | #if wxUSE_MENUS && wxUSE_STATUSBAR | |
247 | ||
248 | void wxFrameBase::OnMenuHighlight(wxMenuEvent& event) | |
249 | { | |
250 | #if wxUSE_STATUSBAR | |
251 | (void)ShowMenuHelp(event.GetMenuId()); | |
252 | #endif // wxUSE_STATUSBAR | |
253 | } | |
254 | ||
255 | void wxFrameBase::OnMenuOpen(wxMenuEvent& event) | |
256 | { | |
257 | #if wxUSE_IDLEMENUUPDATES | |
258 | wxUnusedVar(event); | |
259 | #else // !wxUSE_IDLEMENUUPDATES | |
260 | // as we didn't update the menus from idle time, do it now | |
261 | DoMenuUpdates(event.GetMenu()); | |
262 | #endif // wxUSE_IDLEMENUUPDATES/!wxUSE_IDLEMENUUPDATES | |
263 | } | |
264 | ||
265 | void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event)) | |
266 | { | |
267 | DoGiveHelp(wxEmptyString, false); | |
268 | } | |
269 | ||
270 | #endif // wxUSE_MENUS && wxUSE_STATUSBAR | |
271 | ||
272 | // Implement internal behaviour (menu updating on some platforms) | |
273 | void wxFrameBase::OnInternalIdle() | |
274 | { | |
275 | wxTopLevelWindow::OnInternalIdle(); | |
276 | ||
277 | #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES | |
278 | if (wxUpdateUIEvent::CanUpdate(this)) | |
279 | DoMenuUpdates(); | |
280 | #endif | |
281 | } | |
282 | ||
283 | // ---------------------------------------------------------------------------- | |
284 | // status bar stuff | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
287 | #if wxUSE_STATUSBAR | |
288 | ||
289 | wxStatusBar* wxFrameBase::CreateStatusBar(int number, | |
290 | long style, | |
291 | wxWindowID id, | |
292 | const wxString& name) | |
293 | { | |
294 | // the main status bar can only be created once (or else it should be | |
295 | // deleted before calling CreateStatusBar() again) | |
296 | wxCHECK_MSG( !m_frameStatusBar, NULL, | |
297 | wxT("recreating status bar in wxFrame") ); | |
298 | ||
299 | SetStatusBar(OnCreateStatusBar(number, style, id, name)); | |
300 | ||
301 | return m_frameStatusBar; | |
302 | } | |
303 | ||
304 | wxStatusBar *wxFrameBase::OnCreateStatusBar(int number, | |
305 | long style, | |
306 | wxWindowID id, | |
307 | const wxString& name) | |
308 | { | |
309 | wxStatusBar *statusBar = new wxStatusBar(this, id, style, name); | |
310 | ||
311 | statusBar->SetFieldsCount(number); | |
312 | ||
313 | return statusBar; | |
314 | } | |
315 | ||
316 | void wxFrameBase::SetStatusText(const wxString& text, int number) | |
317 | { | |
318 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); | |
319 | ||
320 | m_frameStatusBar->SetStatusText(text, number); | |
321 | } | |
322 | ||
323 | void wxFrameBase::SetStatusWidths(int n, const int widths_field[] ) | |
324 | { | |
325 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") ); | |
326 | ||
327 | m_frameStatusBar->SetStatusWidths(n, widths_field); | |
328 | ||
329 | PositionStatusBar(); | |
330 | } | |
331 | ||
332 | void wxFrameBase::PushStatusText(const wxString& text, int number) | |
333 | { | |
334 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); | |
335 | ||
336 | m_frameStatusBar->PushStatusText(text, number); | |
337 | } | |
338 | ||
339 | void wxFrameBase::PopStatusText(int number) | |
340 | { | |
341 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); | |
342 | ||
343 | m_frameStatusBar->PopStatusText(number); | |
344 | } | |
345 | ||
346 | bool wxFrameBase::ShowMenuHelp(int menuId) | |
347 | { | |
348 | #if wxUSE_MENUS | |
349 | // if no help string found, we will clear the status bar text | |
350 | wxString helpString; | |
351 | if ( menuId != wxID_SEPARATOR && menuId != -3 /* wxID_TITLE */ ) | |
352 | { | |
353 | const wxMenuItem * const item = FindItemInMenuBar(menuId); | |
354 | if ( item && !item->IsSeparator() ) | |
355 | helpString = item->GetHelp(); | |
356 | ||
357 | // notice that it's ok if we don't find the item because it might | |
358 | // belong to the popup menu, so don't assert here | |
359 | } | |
360 | ||
361 | DoGiveHelp(helpString, true); | |
362 | ||
363 | return !helpString.empty(); | |
364 | #else // !wxUSE_MENUS | |
365 | return false; | |
366 | #endif // wxUSE_MENUS/!wxUSE_MENUS | |
367 | } | |
368 | ||
369 | void wxFrameBase::SetStatusBar(wxStatusBar *statBar) | |
370 | { | |
371 | bool hadBar = m_frameStatusBar != NULL; | |
372 | m_frameStatusBar = statBar; | |
373 | ||
374 | if ( (m_frameStatusBar != NULL) != hadBar ) | |
375 | { | |
376 | PositionStatusBar(); | |
377 | ||
378 | DoLayout(); | |
379 | } | |
380 | } | |
381 | ||
382 | #endif // wxUSE_STATUSBAR | |
383 | ||
384 | #if wxUSE_MENUS || wxUSE_TOOLBAR | |
385 | void wxFrameBase::DoGiveHelp(const wxString& help, bool show) | |
386 | { | |
387 | #if wxUSE_STATUSBAR | |
388 | if ( m_statusBarPane < 0 ) | |
389 | { | |
390 | // status bar messages disabled | |
391 | return; | |
392 | } | |
393 | ||
394 | wxStatusBar *statbar = GetStatusBar(); | |
395 | if ( !statbar ) | |
396 | return; | |
397 | ||
398 | wxString text; | |
399 | if ( show ) | |
400 | { | |
401 | // remember the old status bar text if this is the first time we're | |
402 | // called since the menu has been opened as we're going to overwrite it | |
403 | // in our DoGiveHelp() and we want to restore it when the menu is | |
404 | // closed | |
405 | // | |
406 | // note that it would be logical to do this in OnMenuOpen() but under | |
407 | // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely | |
408 | // enough, and so this doesn't work and instead we use the ugly trick | |
409 | // with using special m_oldStatusText value as "menu opened" (but it is | |
410 | // arguably better than adding yet another member variable to wxFrame | |
411 | // on all platforms) | |
412 | if ( m_oldStatusText.empty() ) | |
413 | { | |
414 | m_oldStatusText = statbar->GetStatusText(m_statusBarPane); | |
415 | if ( m_oldStatusText.empty() ) | |
416 | { | |
417 | // use special value to prevent us from doing this the next time | |
418 | m_oldStatusText += wxT('\0'); | |
419 | } | |
420 | } | |
421 | ||
422 | m_lastHelpShown = | |
423 | text = help; | |
424 | } | |
425 | else // hide help, restore the original text | |
426 | { | |
427 | // clear the last shown help string but remember its value | |
428 | wxString lastHelpShown; | |
429 | lastHelpShown.swap(m_lastHelpShown); | |
430 | ||
431 | // also clear the old status text but remember it too to restore it | |
432 | // below | |
433 | text.swap(m_oldStatusText); | |
434 | ||
435 | if ( statbar->GetStatusText(m_statusBarPane) != lastHelpShown ) | |
436 | { | |
437 | // if the text was changed with an explicit SetStatusText() call | |
438 | // from the user code in the meanwhile, do not overwrite it with | |
439 | // the old status bar contents -- this is almost certainly not what | |
440 | // the user expects and would be very hard to avoid from user code | |
441 | return; | |
442 | } | |
443 | } | |
444 | ||
445 | statbar->SetStatusText(text, m_statusBarPane); | |
446 | #else | |
447 | wxUnusedVar(help); | |
448 | wxUnusedVar(show); | |
449 | #endif // wxUSE_STATUSBAR | |
450 | } | |
451 | #endif // wxUSE_MENUS || wxUSE_TOOLBAR | |
452 | ||
453 | ||
454 | // ---------------------------------------------------------------------------- | |
455 | // toolbar stuff | |
456 | // ---------------------------------------------------------------------------- | |
457 | ||
458 | #if wxUSE_TOOLBAR | |
459 | ||
460 | wxToolBar* wxFrameBase::CreateToolBar(long style, | |
461 | wxWindowID id, | |
462 | const wxString& name) | |
463 | { | |
464 | // the main toolbar can't be recreated (unless it was explicitly deleted | |
465 | // before) | |
466 | wxCHECK_MSG( !m_frameToolBar, NULL, | |
467 | wxT("recreating toolbar in wxFrame") ); | |
468 | ||
469 | if ( style == -1 ) | |
470 | { | |
471 | // use default style | |
472 | // | |
473 | // NB: we don't specify the default value in the method declaration | |
474 | // because | |
475 | // a) this allows us to have different defaults for different | |
476 | // platforms (even if we don't have them right now) | |
477 | // b) we don't need to include wx/toolbar.h in the header then | |
478 | style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT; | |
479 | } | |
480 | ||
481 | SetToolBar(OnCreateToolBar(style, id, name)); | |
482 | ||
483 | return m_frameToolBar; | |
484 | } | |
485 | ||
486 | wxToolBar* wxFrameBase::OnCreateToolBar(long style, | |
487 | wxWindowID id, | |
488 | const wxString& name) | |
489 | { | |
490 | #if defined(__WXWINCE__) && defined(__POCKETPC__) | |
491 | return new wxToolMenuBar(this, id, | |
492 | wxDefaultPosition, wxDefaultSize, | |
493 | style, name); | |
494 | #else | |
495 | return new wxToolBar(this, id, | |
496 | wxDefaultPosition, wxDefaultSize, | |
497 | style, name); | |
498 | #endif | |
499 | } | |
500 | ||
501 | void wxFrameBase::SetToolBar(wxToolBar *toolbar) | |
502 | { | |
503 | if ( (toolbar != NULL) != (m_frameToolBar != NULL) ) | |
504 | { | |
505 | // the toolbar visibility must have changed so we need to both position | |
506 | // the toolbar itself (if it appeared) and to relayout the frame | |
507 | // contents in any case | |
508 | ||
509 | if ( toolbar ) | |
510 | { | |
511 | // we need to assign it to m_frameToolBar for PositionToolBar() to | |
512 | // do anything | |
513 | m_frameToolBar = toolbar; | |
514 | PositionToolBar(); | |
515 | } | |
516 | //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout() | |
517 | // wouldn't recognize the (still existing) toolbar as one of our | |
518 | // bars and wouldn't layout the single child of the frame correctly | |
519 | ||
520 | ||
521 | // and this is even more tricky: we want DoLayout() to recognize the | |
522 | // old toolbar for the purpose of not counting it among our non-bar | |
523 | // children but we don't want to reserve any more space for it so we | |
524 | // temporarily hide it | |
525 | if ( m_frameToolBar ) | |
526 | m_frameToolBar->Hide(); | |
527 | ||
528 | DoLayout(); | |
529 | ||
530 | if ( m_frameToolBar ) | |
531 | m_frameToolBar->Show(); | |
532 | } | |
533 | ||
534 | // this might have been already done above but it's simpler to just always | |
535 | // do it unconditionally instead of testing for whether we already did it | |
536 | m_frameToolBar = toolbar; | |
537 | } | |
538 | ||
539 | #endif // wxUSE_TOOLBAR | |
540 | ||
541 | // ---------------------------------------------------------------------------- | |
542 | // menus | |
543 | // ---------------------------------------------------------------------------- | |
544 | ||
545 | #if wxUSE_MENUS | |
546 | ||
547 | // update all menus | |
548 | void wxFrameBase::DoMenuUpdates(wxMenu* menu) | |
549 | { | |
550 | if (menu) | |
551 | { | |
552 | wxEvtHandler* source = GetEventHandler(); | |
553 | menu->UpdateUI(source); | |
554 | } | |
555 | else | |
556 | { | |
557 | wxMenuBar* bar = GetMenuBar(); | |
558 | if (bar != NULL) | |
559 | bar->UpdateMenus(); | |
560 | } | |
561 | } | |
562 | ||
563 | void wxFrameBase::DetachMenuBar() | |
564 | { | |
565 | if ( m_frameMenuBar ) | |
566 | { | |
567 | m_frameMenuBar->Detach(); | |
568 | m_frameMenuBar = NULL; | |
569 | } | |
570 | } | |
571 | ||
572 | void wxFrameBase::AttachMenuBar(wxMenuBar *menubar) | |
573 | { | |
574 | if ( menubar ) | |
575 | { | |
576 | menubar->Attach((wxFrame *)this); | |
577 | m_frameMenuBar = menubar; | |
578 | } | |
579 | } | |
580 | ||
581 | void wxFrameBase::SetMenuBar(wxMenuBar *menubar) | |
582 | { | |
583 | if ( menubar == GetMenuBar() ) | |
584 | { | |
585 | // nothing to do | |
586 | return; | |
587 | } | |
588 | ||
589 | DetachMenuBar(); | |
590 | ||
591 | this->AttachMenuBar(menubar); | |
592 | } | |
593 | ||
594 | wxMenuItem *wxFrameBase::FindItemInMenuBar(int menuId) const | |
595 | { | |
596 | const wxMenuBar * const menuBar = GetMenuBar(); | |
597 | ||
598 | return menuBar ? menuBar->FindItem(menuId) : NULL; | |
599 | } | |
600 | ||
601 | #endif // wxUSE_MENUS |