]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
Use idle time menu updating when using global menu bar in wxGTK.
[wxWidgets.git] / src / common / framecmn.cpp
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 extern WXDLLEXPORT_DATA(const char) wxFrameNameStr[] = "frame";
37 extern WXDLLEXPORT_DATA(const char) wxStatusLineNameStr[] = "status_line";
38
39 // ----------------------------------------------------------------------------
40 // event table
41 // ----------------------------------------------------------------------------
42
43 #if wxUSE_MENUS && wxUSE_STATUSBAR
44 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
45 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
46 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
47
48 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
49 END_EVENT_TABLE()
50
51 #endif // wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
52
53 /* static */
54 bool wxFrameBase::ShouldUpdateMenuFromIdle()
55 {
56 // Usually this is determined at compile time and is determined by whether
57 // the platform supports wxEVT_MENU_OPEN, however in wxGTK we need to also
58 // check if we're using the global menu bar as we don't get EVT_MENU_OPEN
59 // for it and need to fall back to idle time updating even if normally
60 // wxUSE_IDLEMENUUPDATES is set to 0 for wxGTK.
61 #ifdef __WXGTK__
62 if ( wxApp::GTKIsUsingGlobalMenu() )
63 return true;
64 #endif // !__WXGTK__
65
66 return wxUSE_IDLEMENUUPDATES != 0;
67 }
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73 // ----------------------------------------------------------------------------
74 // XTI
75 // ----------------------------------------------------------------------------
76
77 wxDEFINE_FLAGS( wxFrameStyle )
78 wxBEGIN_FLAGS( wxFrameStyle )
79 // new style border flags, we put them first to
80 // use them for streaming out
81 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
82 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
83 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
84 wxFLAGS_MEMBER(wxBORDER_RAISED)
85 wxFLAGS_MEMBER(wxBORDER_STATIC)
86 wxFLAGS_MEMBER(wxBORDER_NONE)
87
88 // old style border flags
89 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
90 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
91 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
92 wxFLAGS_MEMBER(wxRAISED_BORDER)
93 wxFLAGS_MEMBER(wxSTATIC_BORDER)
94 wxFLAGS_MEMBER(wxBORDER)
95
96 // standard window styles
97 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
98 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
99 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
100 wxFLAGS_MEMBER(wxWANTS_CHARS)
101 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
102 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
103 wxFLAGS_MEMBER(wxVSCROLL)
104 wxFLAGS_MEMBER(wxHSCROLL)
105
106 // frame styles
107 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
108 wxFLAGS_MEMBER(wxCAPTION)
109 #if WXWIN_COMPATIBILITY_2_6
110 wxFLAGS_MEMBER(wxTHICK_FRAME)
111 #endif // WXWIN_COMPATIBILITY_2_6
112 wxFLAGS_MEMBER(wxSYSTEM_MENU)
113 wxFLAGS_MEMBER(wxRESIZE_BORDER)
114 #if WXWIN_COMPATIBILITY_2_6
115 wxFLAGS_MEMBER(wxRESIZE_BOX)
116 #endif // WXWIN_COMPATIBILITY_2_6
117 wxFLAGS_MEMBER(wxCLOSE_BOX)
118 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
119 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
120
121 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW)
122 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT)
123
124 wxFLAGS_MEMBER(wxFRAME_SHAPED)
125 wxEND_FLAGS( wxFrameStyle )
126
127 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame, wxTopLevelWindow, "wx/frame.h")
128
129 wxBEGIN_PROPERTIES_TABLE(wxFrame)
130 wxEVENT_PROPERTY( Menu, wxEVT_COMMAND_MENU_SELECTED, wxCommandEvent)
131
132 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxString(), 0 /*flags*/, \
133 wxT("Helpstring"), wxT("group"))
134 wxPROPERTY_FLAGS( WindowStyle, wxFrameStyle, long, SetWindowStyleFlag, \
135 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
136 wxT("Helpstring"), wxT("group")) // style
137 #if wxUSE_MENUS
138 wxPROPERTY( MenuBar, wxMenuBar *, SetMenuBar, GetMenuBar, wxEMPTY_PARAMETER_VALUE, \
139 0 /*flags*/, wxT("Helpstring"), wxT("group"))
140 #endif
141 wxEND_PROPERTIES_TABLE()
142
143 wxEMPTY_HANDLERS_TABLE(wxFrame)
144
145 wxCONSTRUCTOR_6( wxFrame, wxWindow*, Parent, wxWindowID, Id, wxString, Title, \
146 wxPoint, Position, wxSize, Size, long, WindowStyle)
147
148 // ----------------------------------------------------------------------------
149 // construction/destruction
150 // ----------------------------------------------------------------------------
151
152 wxFrameBase::wxFrameBase()
153 {
154 #if wxUSE_MENUS
155 m_frameMenuBar = NULL;
156 #endif // wxUSE_MENUS
157
158 #if wxUSE_TOOLBAR
159 m_frameToolBar = NULL;
160 #endif // wxUSE_TOOLBAR
161
162 #if wxUSE_STATUSBAR
163 m_frameStatusBar = NULL;
164 #endif // wxUSE_STATUSBAR
165
166 m_statusBarPane = 0;
167 }
168
169 wxFrameBase::~wxFrameBase()
170 {
171 // this destructor is required for Darwin
172 }
173
174 wxFrame *wxFrameBase::New(wxWindow *parent,
175 wxWindowID id,
176 const wxString& title,
177 const wxPoint& pos,
178 const wxSize& size,
179 long style,
180 const wxString& name)
181 {
182 return new wxFrame(parent, id, title, pos, size, style, name);
183 }
184
185 void wxFrameBase::DeleteAllBars()
186 {
187 #if wxUSE_MENUS
188 wxDELETE(m_frameMenuBar);
189 #endif // wxUSE_MENUS
190
191 #if wxUSE_STATUSBAR
192 wxDELETE(m_frameStatusBar);
193 #endif // wxUSE_STATUSBAR
194
195 #if wxUSE_TOOLBAR
196 wxDELETE(m_frameToolBar);
197 #endif // wxUSE_TOOLBAR
198 }
199
200 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
201 {
202 #if wxUSE_MENUS
203 if ( win == GetMenuBar() )
204 return true;
205 #endif // wxUSE_MENUS
206
207 #if wxUSE_STATUSBAR
208 if ( win == GetStatusBar() )
209 return true;
210 #endif // wxUSE_STATUSBAR
211
212 #if wxUSE_TOOLBAR
213 if ( win == GetToolBar() )
214 return true;
215 #endif // wxUSE_TOOLBAR
216
217 wxUnusedVar(win);
218
219 return false;
220 }
221
222 // ----------------------------------------------------------------------------
223 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
224 // from the client area, so the client area is what's really available for the
225 // frame contents
226 // ----------------------------------------------------------------------------
227
228 // get the origin of the client area in the client coordinates
229 wxPoint wxFrameBase::GetClientAreaOrigin() const
230 {
231 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
232
233 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
234 wxToolBar *toolbar = GetToolBar();
235 if ( toolbar && toolbar->IsShown() )
236 {
237 int w, h;
238 toolbar->GetSize(&w, &h);
239
240 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
241 {
242 pt.x += w;
243 }
244 else
245 {
246 pt.y += h;
247 }
248 }
249 #endif // wxUSE_TOOLBAR
250
251 return pt;
252 }
253
254 // ----------------------------------------------------------------------------
255 // misc
256 // ----------------------------------------------------------------------------
257
258 #if wxUSE_MENUS
259
260 bool wxFrameBase::ProcessCommand(int id)
261 {
262 wxMenuItem* const item = FindItemInMenuBar(id);
263 if ( !item )
264 return false;
265
266 return ProcessCommand(item);
267 }
268
269 bool wxFrameBase::ProcessCommand(wxMenuItem *item)
270 {
271 wxCHECK_MSG( item, false, wxS("Menu item can't be NULL") );
272
273 if (!item->IsEnabled())
274 return true;
275
276 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
277 return true;
278
279 int checked;
280 if (item->IsCheckable())
281 {
282 item->Toggle();
283
284 // use the new value
285 checked = item->IsChecked();
286 }
287 else // Uncheckable item.
288 {
289 checked = -1;
290 }
291
292 wxMenu* const menu = item->GetMenu();
293 wxCHECK_MSG( menu, false, wxS("Menu item should be attached to a menu") );
294
295 return menu->SendEvent(item->GetId(), checked);
296 }
297
298 #endif // wxUSE_MENUS
299
300 // Do the UI update processing for this window. This is
301 // provided for the application to call if it wants to
302 // force a UI update, particularly for the menus and toolbar.
303 void wxFrameBase::UpdateWindowUI(long flags)
304 {
305 wxWindowBase::UpdateWindowUI(flags);
306
307 #if wxUSE_TOOLBAR
308 if (GetToolBar())
309 GetToolBar()->UpdateWindowUI(flags);
310 #endif
311
312 #if wxUSE_MENUS
313 if (GetMenuBar())
314 {
315 // If coming from an idle event, we only want to update the menus if
316 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
317 // will be update when the menu is opened later
318 if ( !(flags & wxUPDATE_UI_FROMIDLE) || ShouldUpdateMenuFromIdle() )
319 DoMenuUpdates();
320 }
321 #endif // wxUSE_MENUS
322 }
323
324 // ----------------------------------------------------------------------------
325 // event handlers for status bar updates from menus
326 // ----------------------------------------------------------------------------
327
328 #if wxUSE_MENUS && wxUSE_STATUSBAR
329
330 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
331 {
332 #if wxUSE_STATUSBAR
333 (void)ShowMenuHelp(event.GetMenuId());
334 #endif // wxUSE_STATUSBAR
335 }
336
337 void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
338 {
339 if ( !ShouldUpdateMenuFromIdle() )
340 {
341 // as we didn't update the menus from idle time, do it now
342 DoMenuUpdates(event.GetMenu());
343 }
344 }
345
346 void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
347 {
348 DoGiveHelp(wxEmptyString, false);
349 }
350
351 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
352
353 // Implement internal behaviour (menu updating on some platforms)
354 void wxFrameBase::OnInternalIdle()
355 {
356 wxTopLevelWindow::OnInternalIdle();
357
358 #if wxUSE_MENUS
359 if ( ShouldUpdateMenuFromIdle() && wxUpdateUIEvent::CanUpdate(this) )
360 DoMenuUpdates();
361 #endif
362 }
363
364 // ----------------------------------------------------------------------------
365 // status bar stuff
366 // ----------------------------------------------------------------------------
367
368 #if wxUSE_STATUSBAR
369
370 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
371 long style,
372 wxWindowID id,
373 const wxString& name)
374 {
375 // the main status bar can only be created once (or else it should be
376 // deleted before calling CreateStatusBar() again)
377 wxCHECK_MSG( !m_frameStatusBar, NULL,
378 wxT("recreating status bar in wxFrame") );
379
380 SetStatusBar(OnCreateStatusBar(number, style, id, name));
381
382 return m_frameStatusBar;
383 }
384
385 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
386 long style,
387 wxWindowID id,
388 const wxString& name)
389 {
390 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
391
392 statusBar->SetFieldsCount(number);
393
394 return statusBar;
395 }
396
397 void wxFrameBase::SetStatusText(const wxString& text, int number)
398 {
399 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
400
401 m_frameStatusBar->SetStatusText(text, number);
402 }
403
404 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
405 {
406 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
407
408 m_frameStatusBar->SetStatusWidths(n, widths_field);
409
410 PositionStatusBar();
411 }
412
413 void wxFrameBase::PushStatusText(const wxString& text, int number)
414 {
415 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
416
417 m_frameStatusBar->PushStatusText(text, number);
418 }
419
420 void wxFrameBase::PopStatusText(int number)
421 {
422 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
423
424 m_frameStatusBar->PopStatusText(number);
425 }
426
427 bool wxFrameBase::ShowMenuHelp(int menuId)
428 {
429 #if wxUSE_MENUS
430 // if no help string found, we will clear the status bar text
431 //
432 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
433 wxString helpString;
434 if ( menuId != wxID_SEPARATOR && menuId != wxID_NONE )
435 {
436 const wxMenuItem * const item = FindItemInMenuBar(menuId);
437 if ( item && !item->IsSeparator() )
438 helpString = item->GetHelp();
439
440 // notice that it's ok if we don't find the item because it might
441 // belong to the popup menu, so don't assert here
442 }
443
444 DoGiveHelp(helpString, true);
445
446 return !helpString.empty();
447 #else // !wxUSE_MENUS
448 return false;
449 #endif // wxUSE_MENUS/!wxUSE_MENUS
450 }
451
452 void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
453 {
454 bool hadBar = m_frameStatusBar != NULL;
455 m_frameStatusBar = statBar;
456
457 if ( (m_frameStatusBar != NULL) != hadBar )
458 {
459 PositionStatusBar();
460
461 DoLayout();
462 }
463 }
464
465 #endif // wxUSE_STATUSBAR
466
467 #if wxUSE_MENUS || wxUSE_TOOLBAR
468 void wxFrameBase::DoGiveHelp(const wxString& help, bool show)
469 {
470 #if wxUSE_STATUSBAR
471 if ( m_statusBarPane < 0 )
472 {
473 // status bar messages disabled
474 return;
475 }
476
477 wxStatusBar *statbar = GetStatusBar();
478 if ( !statbar )
479 return;
480
481 wxString text;
482 if ( show )
483 {
484 // remember the old status bar text if this is the first time we're
485 // called since the menu has been opened as we're going to overwrite it
486 // in our DoGiveHelp() and we want to restore it when the menu is
487 // closed
488 //
489 // note that it would be logical to do this in OnMenuOpen() but under
490 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
491 // enough, and so this doesn't work and instead we use the ugly trick
492 // with using special m_oldStatusText value as "menu opened" (but it is
493 // arguably better than adding yet another member variable to wxFrame
494 // on all platforms)
495 if ( m_oldStatusText.empty() )
496 {
497 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
498 if ( m_oldStatusText.empty() )
499 {
500 // use special value to prevent us from doing this the next time
501 m_oldStatusText += wxT('\0');
502 }
503 }
504
505 m_lastHelpShown =
506 text = help;
507 }
508 else // hide help, restore the original text
509 {
510 // clear the last shown help string but remember its value
511 wxString lastHelpShown;
512 lastHelpShown.swap(m_lastHelpShown);
513
514 // also clear the old status text but remember it too to restore it
515 // below
516 text.swap(m_oldStatusText);
517
518 if ( statbar->GetStatusText(m_statusBarPane) != lastHelpShown )
519 {
520 // if the text was changed with an explicit SetStatusText() call
521 // from the user code in the meanwhile, do not overwrite it with
522 // the old status bar contents -- this is almost certainly not what
523 // the user expects and would be very hard to avoid from user code
524 return;
525 }
526 }
527
528 statbar->SetStatusText(text, m_statusBarPane);
529 #else
530 wxUnusedVar(help);
531 wxUnusedVar(show);
532 #endif // wxUSE_STATUSBAR
533 }
534 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
535
536
537 // ----------------------------------------------------------------------------
538 // toolbar stuff
539 // ----------------------------------------------------------------------------
540
541 #if wxUSE_TOOLBAR
542
543 wxToolBar* wxFrameBase::CreateToolBar(long style,
544 wxWindowID id,
545 const wxString& name)
546 {
547 // the main toolbar can't be recreated (unless it was explicitly deleted
548 // before)
549 wxCHECK_MSG( !m_frameToolBar, NULL,
550 wxT("recreating toolbar in wxFrame") );
551
552 if ( style == -1 )
553 {
554 // use default style
555 //
556 // NB: we don't specify the default value in the method declaration
557 // because
558 // a) this allows us to have different defaults for different
559 // platforms (even if we don't have them right now)
560 // b) we don't need to include wx/toolbar.h in the header then
561 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
562 }
563
564 SetToolBar(OnCreateToolBar(style, id, name));
565
566 return m_frameToolBar;
567 }
568
569 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
570 wxWindowID id,
571 const wxString& name)
572 {
573 #if defined(__WXWINCE__) && defined(__POCKETPC__)
574 return new wxToolMenuBar(this, id,
575 wxDefaultPosition, wxDefaultSize,
576 style, name);
577 #else
578 return new wxToolBar(this, id,
579 wxDefaultPosition, wxDefaultSize,
580 style, name);
581 #endif
582 }
583
584 void wxFrameBase::SetToolBar(wxToolBar *toolbar)
585 {
586 if ( (toolbar != NULL) != (m_frameToolBar != NULL) )
587 {
588 // the toolbar visibility must have changed so we need to both position
589 // the toolbar itself (if it appeared) and to relayout the frame
590 // contents in any case
591
592 if ( toolbar )
593 {
594 // we need to assign it to m_frameToolBar for PositionToolBar() to
595 // do anything
596 m_frameToolBar = toolbar;
597 PositionToolBar();
598 }
599 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
600 // wouldn't recognize the (still existing) toolbar as one of our
601 // bars and wouldn't layout the single child of the frame correctly
602
603
604 // and this is even more tricky: we want DoLayout() to recognize the
605 // old toolbar for the purpose of not counting it among our non-bar
606 // children but we don't want to reserve any more space for it so we
607 // temporarily hide it
608 if ( m_frameToolBar )
609 m_frameToolBar->Hide();
610
611 DoLayout();
612
613 if ( m_frameToolBar )
614 m_frameToolBar->Show();
615 }
616
617 // this might have been already done above but it's simpler to just always
618 // do it unconditionally instead of testing for whether we already did it
619 m_frameToolBar = toolbar;
620 }
621
622 #endif // wxUSE_TOOLBAR
623
624 // ----------------------------------------------------------------------------
625 // menus
626 // ----------------------------------------------------------------------------
627
628 #if wxUSE_MENUS
629
630 // update all menus
631 void wxFrameBase::DoMenuUpdates(wxMenu* menu)
632 {
633 if (menu)
634 {
635 wxEvtHandler* source = GetEventHandler();
636 menu->UpdateUI(source);
637 }
638 else
639 {
640 wxMenuBar* bar = GetMenuBar();
641 if (bar != NULL)
642 bar->UpdateMenus();
643 }
644 }
645
646 void wxFrameBase::DetachMenuBar()
647 {
648 if ( m_frameMenuBar )
649 {
650 m_frameMenuBar->Detach();
651 m_frameMenuBar = NULL;
652 }
653 }
654
655 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
656 {
657 if ( menubar )
658 {
659 menubar->Attach((wxFrame *)this);
660 m_frameMenuBar = menubar;
661 }
662 }
663
664 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
665 {
666 if ( menubar == GetMenuBar() )
667 {
668 // nothing to do
669 return;
670 }
671
672 DetachMenuBar();
673
674 this->AttachMenuBar(menubar);
675 }
676
677 wxMenuItem *wxFrameBase::FindItemInMenuBar(int menuId) const
678 {
679 const wxMenuBar * const menuBar = GetMenuBar();
680
681 return menuBar ? menuBar->FindItem(menuId) : NULL;
682 }
683
684 #endif // wxUSE_MENUS