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