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"
30 #include "wx/menuitem.h"
31 #include "wx/dcclient.h"
32 #include "wx/toolbar.h"
33 #include "wx/statusbr.h"
36 extern WXDLLEXPORT_DATA(const char) wxFrameNameStr
[] = "frame";
37 extern WXDLLEXPORT_DATA(const char) wxStatusLineNameStr
[] = "status_line";
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if wxUSE_MENUS && wxUSE_STATUSBAR
44 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
45 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
46 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose
)
48 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
51 #endif // wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
54 bool wxFrameBase::ShouldUpdateMenuFromIdle()
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.
62 if ( wxApp::GTKIsUsingGlobalMenu() )
66 return wxUSE_IDLEMENUUPDATES
!= 0;
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
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
)
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
)
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
)
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
)
121 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW
)
122 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT
)
124 wxFLAGS_MEMBER(wxFRAME_SHAPED
)
125 wxEND_FLAGS( wxFrameStyle
)
127 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame
, wxTopLevelWindow
, "wx/frame.h")
129 wxBEGIN_PROPERTIES_TABLE(wxFrame
)
130 wxEVENT_PROPERTY( Menu
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
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
138 wxPROPERTY( MenuBar
, wxMenuBar
*, SetMenuBar
, GetMenuBar
, wxEMPTY_PARAMETER_VALUE
, \
139 0 /*flags*/, wxT("Helpstring"), wxT("group"))
141 wxEND_PROPERTIES_TABLE()
143 wxEMPTY_HANDLERS_TABLE(wxFrame
)
145 wxCONSTRUCTOR_6( wxFrame
, wxWindow
*, Parent
, wxWindowID
, Id
, wxString
, Title
, \
146 wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
148 // ----------------------------------------------------------------------------
149 // construction/destruction
150 // ----------------------------------------------------------------------------
152 wxFrameBase::wxFrameBase()
155 m_frameMenuBar
= NULL
;
156 #endif // wxUSE_MENUS
159 m_frameToolBar
= NULL
;
160 #endif // wxUSE_TOOLBAR
163 m_frameStatusBar
= NULL
;
164 #endif // wxUSE_STATUSBAR
169 wxFrameBase::~wxFrameBase()
171 // this destructor is required for Darwin
174 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
176 const wxString
& title
,
180 const wxString
& name
)
182 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
185 void wxFrameBase::DeleteAllBars()
188 wxDELETE(m_frameMenuBar
);
189 #endif // wxUSE_MENUS
192 wxDELETE(m_frameStatusBar
);
193 #endif // wxUSE_STATUSBAR
196 wxDELETE(m_frameToolBar
);
197 #endif // wxUSE_TOOLBAR
200 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
203 if ( win
== GetMenuBar() )
205 #endif // wxUSE_MENUS
208 if ( win
== GetStatusBar() )
210 #endif // wxUSE_STATUSBAR
213 if ( win
== GetToolBar() )
215 #endif // wxUSE_TOOLBAR
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
226 // ----------------------------------------------------------------------------
228 // get the origin of the client area in the client coordinates
229 wxPoint
wxFrameBase::GetClientAreaOrigin() const
231 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
233 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
234 wxToolBar
*toolbar
= GetToolBar();
235 if ( toolbar
&& toolbar
->IsShown() )
238 toolbar
->GetSize(&w
, &h
);
240 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
249 #endif // wxUSE_TOOLBAR
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
260 bool wxFrameBase::ProcessCommand(int id
)
262 wxMenuItem
* const item
= FindItemInMenuBar(id
);
266 return ProcessCommand(item
);
269 bool wxFrameBase::ProcessCommand(wxMenuItem
*item
)
271 wxCHECK_MSG( item
, false, wxS("Menu item can't be NULL") );
273 if (!item
->IsEnabled())
276 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
280 if (item
->IsCheckable())
285 checked
= item
->IsChecked();
287 else // Uncheckable item.
292 wxMenu
* const menu
= item
->GetMenu();
293 wxCHECK_MSG( menu
, false, wxS("Menu item should be attached to a menu") );
295 return menu
->SendEvent(item
->GetId(), checked
);
298 #endif // wxUSE_MENUS
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
)
305 wxWindowBase::UpdateWindowUI(flags
);
309 GetToolBar()->UpdateWindowUI(flags
);
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() )
321 #endif // wxUSE_MENUS
324 // ----------------------------------------------------------------------------
325 // event handlers for status bar updates from menus
326 // ----------------------------------------------------------------------------
328 #if wxUSE_MENUS && wxUSE_STATUSBAR
330 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
333 (void)ShowMenuHelp(event
.GetMenuId());
334 #endif // wxUSE_STATUSBAR
337 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
339 if ( !ShouldUpdateMenuFromIdle() )
341 // as we didn't update the menus from idle time, do it now
342 DoMenuUpdates(event
.GetMenu());
346 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
348 DoGiveHelp(wxEmptyString
, false);
351 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
353 // Implement internal behaviour (menu updating on some platforms)
354 void wxFrameBase::OnInternalIdle()
356 wxTopLevelWindow::OnInternalIdle();
359 if ( ShouldUpdateMenuFromIdle() && wxUpdateUIEvent::CanUpdate(this) )
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
370 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
373 const wxString
& name
)
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") );
380 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
382 return m_frameStatusBar
;
385 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
388 const wxString
& name
)
390 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
392 statusBar
->SetFieldsCount(number
);
397 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
399 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
401 m_frameStatusBar
->SetStatusText(text
, number
);
404 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
406 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
408 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
413 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
415 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
417 m_frameStatusBar
->PushStatusText(text
, number
);
420 void wxFrameBase::PopStatusText(int number
)
422 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
424 m_frameStatusBar
->PopStatusText(number
);
427 bool wxFrameBase::ShowMenuHelp(int menuId
)
430 // if no help string found, we will clear the status bar text
432 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
434 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= wxID_NONE
)
436 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
437 if ( item
&& !item
->IsSeparator() )
438 helpString
= item
->GetHelp();
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
444 DoGiveHelp(helpString
, true);
446 return !helpString
.empty();
447 #else // !wxUSE_MENUS
449 #endif // wxUSE_MENUS/!wxUSE_MENUS
452 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
454 bool hadBar
= m_frameStatusBar
!= NULL
;
455 m_frameStatusBar
= statBar
;
457 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
465 #endif // wxUSE_STATUSBAR
467 #if wxUSE_MENUS || wxUSE_TOOLBAR
468 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
471 if ( m_statusBarPane
< 0 )
473 // status bar messages disabled
477 wxStatusBar
*statbar
= GetStatusBar();
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
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
495 if ( m_oldStatusText
.empty() )
497 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
498 if ( m_oldStatusText
.empty() )
500 // use special value to prevent us from doing this the next time
501 m_oldStatusText
+= wxT('\0');
508 else // hide help, restore the original text
510 // clear the last shown help string but remember its value
511 wxString lastHelpShown
;
512 lastHelpShown
.swap(m_lastHelpShown
);
514 // also clear the old status text but remember it too to restore it
516 text
.swap(m_oldStatusText
);
518 if ( statbar
->GetStatusText(m_statusBarPane
) != lastHelpShown
)
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
528 statbar
->SetStatusText(text
, m_statusBarPane
);
532 #endif // wxUSE_STATUSBAR
534 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
543 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
545 const wxString
& name
)
547 // the main toolbar can't be recreated (unless it was explicitly deleted
549 wxCHECK_MSG( !m_frameToolBar
, NULL
,
550 wxT("recreating toolbar in wxFrame") );
556 // NB: we don't specify the default value in the method declaration
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
;
564 SetToolBar(OnCreateToolBar(style
, id
, name
));
566 return m_frameToolBar
;
569 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
571 const wxString
& name
)
573 #if defined(__WXWINCE__) && defined(__POCKETPC__)
574 return new wxToolMenuBar(this, id
,
575 wxDefaultPosition
, wxDefaultSize
,
578 return new wxToolBar(this, id
,
579 wxDefaultPosition
, wxDefaultSize
,
584 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
586 if ( (toolbar
!= NULL
) != (m_frameToolBar
!= NULL
) )
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
594 // we need to assign it to m_frameToolBar for PositionToolBar() to
596 m_frameToolBar
= toolbar
;
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
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();
613 if ( m_frameToolBar
)
614 m_frameToolBar
->Show();
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
;
622 #endif // wxUSE_TOOLBAR
624 // ----------------------------------------------------------------------------
626 // ----------------------------------------------------------------------------
631 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
635 wxEvtHandler
* source
= GetEventHandler();
636 menu
->UpdateUI(source
);
640 wxMenuBar
* bar
= GetMenuBar();
646 void wxFrameBase::DetachMenuBar()
648 if ( m_frameMenuBar
)
650 m_frameMenuBar
->Detach();
651 m_frameMenuBar
= NULL
;
655 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
659 menubar
->Attach((wxFrame
*)this);
660 m_frameMenuBar
= menubar
;
664 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
666 if ( menubar
== GetMenuBar() )
674 this->AttachMenuBar(menubar
);
677 wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
679 const wxMenuBar
* const menuBar
= GetMenuBar();
681 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
684 #endif // wxUSE_MENUS