1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/menuitem.h"
32 #include "wx/dcclient.h"
33 #include "wx/toolbar.h"
34 #include "wx/statusbr.h"
37 extern WXDLLEXPORT_DATA(const char) wxFrameNameStr
[] = "frame";
38 extern WXDLLEXPORT_DATA(const char) wxStatusLineNameStr
[] = "status_line";
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 #if wxUSE_MENUS && wxUSE_STATUSBAR
45 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
46 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
47 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose
)
49 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
52 #endif // wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
55 bool wxFrameBase::ShouldUpdateMenuFromIdle()
57 // Usually this is determined at compile time and is determined by whether
58 // the platform supports wxEVT_MENU_OPEN, however in wxGTK we need to also
59 // check if we're using the global menu bar as we don't get EVT_MENU_OPEN
60 // for it and need to fall back to idle time updating even if normally
61 // wxUSE_IDLEMENUUPDATES is set to 0 for wxGTK.
63 if ( wxApp::GTKIsUsingGlobalMenu() )
67 return wxUSE_IDLEMENUUPDATES
!= 0;
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 wxDEFINE_FLAGS( wxFrameStyle
)
79 wxBEGIN_FLAGS( wxFrameStyle
)
80 // new style border flags, we put them first to
81 // use them for streaming out
82 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
83 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
84 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
85 wxFLAGS_MEMBER(wxBORDER_RAISED
)
86 wxFLAGS_MEMBER(wxBORDER_STATIC
)
87 wxFLAGS_MEMBER(wxBORDER_NONE
)
89 // old style border flags
90 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
91 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
92 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
93 wxFLAGS_MEMBER(wxRAISED_BORDER
)
94 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
95 wxFLAGS_MEMBER(wxBORDER
)
97 // standard window styles
98 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
99 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
100 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
101 wxFLAGS_MEMBER(wxWANTS_CHARS
)
102 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
103 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
104 wxFLAGS_MEMBER(wxVSCROLL
)
105 wxFLAGS_MEMBER(wxHSCROLL
)
108 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
109 wxFLAGS_MEMBER(wxCAPTION
)
110 #if WXWIN_COMPATIBILITY_2_6
111 wxFLAGS_MEMBER(wxTHICK_FRAME
)
112 #endif // WXWIN_COMPATIBILITY_2_6
113 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
114 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
115 #if WXWIN_COMPATIBILITY_2_6
116 wxFLAGS_MEMBER(wxRESIZE_BOX
)
117 #endif // WXWIN_COMPATIBILITY_2_6
118 wxFLAGS_MEMBER(wxCLOSE_BOX
)
119 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
120 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
122 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW
)
123 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT
)
125 wxFLAGS_MEMBER(wxFRAME_SHAPED
)
126 wxEND_FLAGS( wxFrameStyle
)
128 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame
, wxTopLevelWindow
, "wx/frame.h")
130 wxBEGIN_PROPERTIES_TABLE(wxFrame
)
131 wxEVENT_PROPERTY( Menu
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
133 wxPROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/, \
134 wxT("Helpstring"), wxT("group"))
135 wxPROPERTY_FLAGS( WindowStyle
, wxFrameStyle
, long, SetWindowStyleFlag
, \
136 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
137 wxT("Helpstring"), wxT("group")) // style
139 wxPROPERTY( MenuBar
, wxMenuBar
*, SetMenuBar
, GetMenuBar
, wxEMPTY_PARAMETER_VALUE
, \
140 0 /*flags*/, wxT("Helpstring"), wxT("group"))
142 wxEND_PROPERTIES_TABLE()
144 wxEMPTY_HANDLERS_TABLE(wxFrame
)
146 wxCONSTRUCTOR_6( wxFrame
, wxWindow
*, Parent
, wxWindowID
, Id
, wxString
, Title
, \
147 wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
149 // ----------------------------------------------------------------------------
150 // construction/destruction
151 // ----------------------------------------------------------------------------
153 wxFrameBase::wxFrameBase()
156 m_frameMenuBar
= NULL
;
157 #endif // wxUSE_MENUS
160 m_frameToolBar
= NULL
;
161 #endif // wxUSE_TOOLBAR
164 m_frameStatusBar
= NULL
;
165 #endif // wxUSE_STATUSBAR
170 wxFrameBase::~wxFrameBase()
172 // this destructor is required for Darwin
175 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
177 const wxString
& title
,
181 const wxString
& name
)
183 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
186 void wxFrameBase::DeleteAllBars()
189 wxDELETE(m_frameMenuBar
);
190 #endif // wxUSE_MENUS
193 wxDELETE(m_frameStatusBar
);
194 #endif // wxUSE_STATUSBAR
197 wxDELETE(m_frameToolBar
);
198 #endif // wxUSE_TOOLBAR
201 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
204 if ( win
== GetMenuBar() )
206 #endif // wxUSE_MENUS
209 if ( win
== GetStatusBar() )
211 #endif // wxUSE_STATUSBAR
214 if ( win
== GetToolBar() )
216 #endif // wxUSE_TOOLBAR
223 // ----------------------------------------------------------------------------
224 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
225 // from the client area, so the client area is what's really available for the
227 // ----------------------------------------------------------------------------
229 // get the origin of the client area in the client coordinates
230 wxPoint
wxFrameBase::GetClientAreaOrigin() const
232 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
234 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
235 wxToolBar
*toolbar
= GetToolBar();
236 if ( toolbar
&& toolbar
->IsShown() )
239 toolbar
->GetSize(&w
, &h
);
241 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
250 #endif // wxUSE_TOOLBAR
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
261 bool wxFrameBase::ProcessCommand(int id
)
263 wxMenuItem
* const item
= FindItemInMenuBar(id
);
267 return ProcessCommand(item
);
270 bool wxFrameBase::ProcessCommand(wxMenuItem
*item
)
272 wxCHECK_MSG( item
, false, wxS("Menu item can't be NULL") );
274 if (!item
->IsEnabled())
277 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
281 if (item
->IsCheckable())
286 checked
= item
->IsChecked();
288 else // Uncheckable item.
293 wxMenu
* const menu
= item
->GetMenu();
294 wxCHECK_MSG( menu
, false, wxS("Menu item should be attached to a menu") );
296 return menu
->SendEvent(item
->GetId(), checked
);
299 #endif // wxUSE_MENUS
301 // Do the UI update processing for this window. This is
302 // provided for the application to call if it wants to
303 // force a UI update, particularly for the menus and toolbar.
304 void wxFrameBase::UpdateWindowUI(long flags
)
306 wxWindowBase::UpdateWindowUI(flags
);
310 GetToolBar()->UpdateWindowUI(flags
);
316 // If coming from an idle event, we only want to update the menus if
317 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
318 // will be update when the menu is opened later
319 if ( !(flags
& wxUPDATE_UI_FROMIDLE
) || ShouldUpdateMenuFromIdle() )
322 #endif // wxUSE_MENUS
325 // ----------------------------------------------------------------------------
326 // event handlers for status bar updates from menus
327 // ----------------------------------------------------------------------------
329 #if wxUSE_MENUS && wxUSE_STATUSBAR
331 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
334 (void)ShowMenuHelp(event
.GetMenuId());
335 #endif // wxUSE_STATUSBAR
338 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
340 if ( !ShouldUpdateMenuFromIdle() )
342 // as we didn't update the menus from idle time, do it now
343 DoMenuUpdates(event
.GetMenu());
347 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
349 DoGiveHelp(wxEmptyString
, false);
352 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
354 // Implement internal behaviour (menu updating on some platforms)
355 void wxFrameBase::OnInternalIdle()
357 wxTopLevelWindow::OnInternalIdle();
360 if ( ShouldUpdateMenuFromIdle() && wxUpdateUIEvent::CanUpdate(this) )
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
371 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
374 const wxString
& name
)
376 // the main status bar can only be created once (or else it should be
377 // deleted before calling CreateStatusBar() again)
378 wxCHECK_MSG( !m_frameStatusBar
, NULL
,
379 wxT("recreating status bar in wxFrame") );
381 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
383 return m_frameStatusBar
;
386 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
389 const wxString
& name
)
391 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
393 statusBar
->SetFieldsCount(number
);
398 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
400 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
402 m_frameStatusBar
->SetStatusText(text
, number
);
405 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
407 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
409 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
414 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
416 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
418 m_frameStatusBar
->PushStatusText(text
, number
);
421 void wxFrameBase::PopStatusText(int number
)
423 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
425 m_frameStatusBar
->PopStatusText(number
);
428 bool wxFrameBase::ShowMenuHelp(int menuId
)
431 // if no help string found, we will clear the status bar text
433 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
435 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= wxID_NONE
)
437 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
438 if ( item
&& !item
->IsSeparator() )
439 helpString
= item
->GetHelp();
441 // notice that it's ok if we don't find the item because it might
442 // belong to the popup menu, so don't assert here
445 DoGiveHelp(helpString
, true);
447 return !helpString
.empty();
448 #else // !wxUSE_MENUS
450 #endif // wxUSE_MENUS/!wxUSE_MENUS
453 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
455 bool hadBar
= m_frameStatusBar
!= NULL
;
456 m_frameStatusBar
= statBar
;
458 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
466 #endif // wxUSE_STATUSBAR
468 #if wxUSE_MENUS || wxUSE_TOOLBAR
469 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
472 if ( m_statusBarPane
< 0 )
474 // status bar messages disabled
478 wxStatusBar
*statbar
= GetStatusBar();
485 // remember the old status bar text if this is the first time we're
486 // called since the menu has been opened as we're going to overwrite it
487 // in our DoGiveHelp() and we want to restore it when the menu is
490 // note that it would be logical to do this in OnMenuOpen() but under
491 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
492 // enough, and so this doesn't work and instead we use the ugly trick
493 // with using special m_oldStatusText value as "menu opened" (but it is
494 // arguably better than adding yet another member variable to wxFrame
496 if ( m_oldStatusText
.empty() )
498 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
499 if ( m_oldStatusText
.empty() )
501 // use special value to prevent us from doing this the next time
502 m_oldStatusText
+= wxT('\0');
509 else // hide help, restore the original text
511 // clear the last shown help string but remember its value
512 wxString lastHelpShown
;
513 lastHelpShown
.swap(m_lastHelpShown
);
515 // also clear the old status text but remember it too to restore it
517 text
.swap(m_oldStatusText
);
519 if ( statbar
->GetStatusText(m_statusBarPane
) != lastHelpShown
)
521 // if the text was changed with an explicit SetStatusText() call
522 // from the user code in the meanwhile, do not overwrite it with
523 // the old status bar contents -- this is almost certainly not what
524 // the user expects and would be very hard to avoid from user code
529 statbar
->SetStatusText(text
, m_statusBarPane
);
533 #endif // wxUSE_STATUSBAR
535 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
544 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
546 const wxString
& name
)
548 // the main toolbar can't be recreated (unless it was explicitly deleted
550 wxCHECK_MSG( !m_frameToolBar
, NULL
,
551 wxT("recreating toolbar in wxFrame") );
557 // NB: we don't specify the default value in the method declaration
559 // a) this allows us to have different defaults for different
560 // platforms (even if we don't have them right now)
561 // b) we don't need to include wx/toolbar.h in the header then
562 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
565 SetToolBar(OnCreateToolBar(style
, id
, name
));
567 return m_frameToolBar
;
570 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
572 const wxString
& name
)
574 #if defined(__WXWINCE__) && defined(__POCKETPC__)
575 return new wxToolMenuBar(this, id
,
576 wxDefaultPosition
, wxDefaultSize
,
579 return new wxToolBar(this, id
,
580 wxDefaultPosition
, wxDefaultSize
,
585 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
587 if ( (toolbar
!= NULL
) != (m_frameToolBar
!= NULL
) )
589 // the toolbar visibility must have changed so we need to both position
590 // the toolbar itself (if it appeared) and to relayout the frame
591 // contents in any case
595 // we need to assign it to m_frameToolBar for PositionToolBar() to
597 m_frameToolBar
= toolbar
;
600 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
601 // wouldn't recognize the (still existing) toolbar as one of our
602 // bars and wouldn't layout the single child of the frame correctly
605 // and this is even more tricky: we want DoLayout() to recognize the
606 // old toolbar for the purpose of not counting it among our non-bar
607 // children but we don't want to reserve any more space for it so we
608 // temporarily hide it
609 if ( m_frameToolBar
)
610 m_frameToolBar
->Hide();
614 if ( m_frameToolBar
)
615 m_frameToolBar
->Show();
618 // this might have been already done above but it's simpler to just always
619 // do it unconditionally instead of testing for whether we already did it
620 m_frameToolBar
= toolbar
;
623 #endif // wxUSE_TOOLBAR
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
632 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
636 wxEvtHandler
* source
= GetEventHandler();
637 menu
->UpdateUI(source
);
641 wxMenuBar
* bar
= GetMenuBar();
647 void wxFrameBase::DetachMenuBar()
649 if ( m_frameMenuBar
)
651 m_frameMenuBar
->Detach();
652 m_frameMenuBar
= NULL
;
656 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
660 menubar
->Attach((wxFrame
*)this);
661 m_frameMenuBar
= menubar
;
665 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
667 if ( menubar
== GetMenuBar() )
675 this->AttachMenuBar(menubar
);
678 wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
680 const wxMenuBar
* const menuBar
= GetMenuBar();
682 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
685 #endif // wxUSE_MENUS