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