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
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_STATUSBAR
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
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
)
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
)
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
)
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
)
106 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW
)
107 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT
)
109 wxFLAGS_MEMBER(wxFRAME_SHAPED
)
110 wxEND_FLAGS( wxFrameStyle
)
112 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame
, wxTopLevelWindow
, "wx/frame.h")
114 wxBEGIN_PROPERTIES_TABLE(wxFrame
)
115 wxEVENT_PROPERTY( Menu
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
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()
126 wxEMPTY_HANDLERS_TABLE(wxFrame
)
128 wxCONSTRUCTOR_6( wxFrame
, wxWindow
*, Parent
, wxWindowID
, Id
, wxString
, Title
, \
129 wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
131 // ----------------------------------------------------------------------------
132 // construction/destruction
133 // ----------------------------------------------------------------------------
135 wxFrameBase::wxFrameBase()
138 m_frameMenuBar
= NULL
;
139 #endif // wxUSE_MENUS
142 m_frameToolBar
= NULL
;
143 #endif // wxUSE_TOOLBAR
146 m_frameStatusBar
= NULL
;
147 #endif // wxUSE_STATUSBAR
152 wxFrameBase::~wxFrameBase()
154 // this destructor is required for Darwin
157 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
159 const wxString
& title
,
163 const wxString
& name
)
165 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
168 void wxFrameBase::DeleteAllBars()
171 wxDELETE(m_frameMenuBar
);
172 #endif // wxUSE_MENUS
175 wxDELETE(m_frameStatusBar
);
176 #endif // wxUSE_STATUSBAR
179 wxDELETE(m_frameToolBar
);
180 #endif // wxUSE_TOOLBAR
183 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
186 if ( win
== GetMenuBar() )
188 #endif // wxUSE_MENUS
191 if ( win
== GetStatusBar() )
193 #endif // wxUSE_STATUSBAR
196 if ( win
== GetToolBar() )
198 #endif // wxUSE_TOOLBAR
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
209 // ----------------------------------------------------------------------------
211 // get the origin of the client area in the client coordinates
212 wxPoint
wxFrameBase::GetClientAreaOrigin() const
214 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
216 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
217 wxToolBar
*toolbar
= GetToolBar();
218 if ( toolbar
&& toolbar
->IsShown() )
221 toolbar
->GetSize(&w
, &h
);
223 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
232 #endif // wxUSE_TOOLBAR
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
243 bool wxFrameBase::ProcessCommand(int id
)
245 wxMenuBar
*bar
= GetMenuBar();
249 wxMenuItem
*item
= bar
->FindItem(id
);
253 return ProcessCommand(item
);
256 bool wxFrameBase::ProcessCommand(wxMenuItem
*item
)
258 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
259 commandEvent
.SetEventObject(this);
261 if (!item
->IsEnabled())
264 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
267 if (item
->IsCheckable())
272 commandEvent
.SetInt(item
->IsChecked());
275 return HandleWindowEvent(commandEvent
);
278 #endif // wxUSE_MENUS
280 // Do the UI update processing for this window. This is
281 // provided for the application to call if it wants to
282 // force a UI update, particularly for the menus and toolbar.
283 void wxFrameBase::UpdateWindowUI(long flags
)
285 wxWindowBase::UpdateWindowUI(flags
);
289 GetToolBar()->UpdateWindowUI(flags
);
295 // If coming from an idle event, we only want to update the menus if
296 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
297 // will be update when the menu is opened later
298 #if !wxUSE_IDLEMENUUPDATES
299 if ( !(flags
& wxUPDATE_UI_FROMIDLE
) )
300 #endif // wxUSE_IDLEMENUUPDATES
303 #endif // wxUSE_MENUS
306 // ----------------------------------------------------------------------------
307 // event handlers for status bar updates from menus
308 // ----------------------------------------------------------------------------
310 #if wxUSE_MENUS && wxUSE_STATUSBAR
312 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
315 (void)ShowMenuHelp(event
.GetMenuId());
316 #endif // wxUSE_STATUSBAR
319 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
321 #if wxUSE_IDLEMENUUPDATES
323 #else // !wxUSE_IDLEMENUUPDATES
324 // as we didn't update the menus from idle time, do it now
325 DoMenuUpdates(event
.GetMenu());
326 #endif // wxUSE_IDLEMENUUPDATES/!wxUSE_IDLEMENUUPDATES
329 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
331 DoGiveHelp(wxEmptyString
, false);
334 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
336 // Implement internal behaviour (menu updating on some platforms)
337 void wxFrameBase::OnInternalIdle()
339 wxTopLevelWindow::OnInternalIdle();
341 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
342 if (wxUpdateUIEvent::CanUpdate(this))
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
353 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
356 const wxString
& name
)
358 // the main status bar can only be created once (or else it should be
359 // deleted before calling CreateStatusBar() again)
360 wxCHECK_MSG( !m_frameStatusBar
, NULL
,
361 wxT("recreating status bar in wxFrame") );
363 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
365 return m_frameStatusBar
;
368 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
371 const wxString
& name
)
373 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
375 statusBar
->SetFieldsCount(number
);
380 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
382 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
384 m_frameStatusBar
->SetStatusText(text
, number
);
387 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
389 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
391 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
396 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
398 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
400 m_frameStatusBar
->PushStatusText(text
, number
);
403 void wxFrameBase::PopStatusText(int number
)
405 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
407 m_frameStatusBar
->PopStatusText(number
);
410 bool wxFrameBase::ShowMenuHelp(int menuId
)
413 // if no help string found, we will clear the status bar text
415 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
417 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= wxID_NONE
)
419 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
420 if ( item
&& !item
->IsSeparator() )
421 helpString
= item
->GetHelp();
423 // notice that it's ok if we don't find the item because it might
424 // belong to the popup menu, so don't assert here
427 DoGiveHelp(helpString
, true);
429 return !helpString
.empty();
430 #else // !wxUSE_MENUS
432 #endif // wxUSE_MENUS/!wxUSE_MENUS
435 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
437 bool hadBar
= m_frameStatusBar
!= NULL
;
438 m_frameStatusBar
= statBar
;
440 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
448 #endif // wxUSE_STATUSBAR
450 #if wxUSE_MENUS || wxUSE_TOOLBAR
451 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
454 if ( m_statusBarPane
< 0 )
456 // status bar messages disabled
460 wxStatusBar
*statbar
= GetStatusBar();
467 // remember the old status bar text if this is the first time we're
468 // called since the menu has been opened as we're going to overwrite it
469 // in our DoGiveHelp() and we want to restore it when the menu is
472 // note that it would be logical to do this in OnMenuOpen() but under
473 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
474 // enough, and so this doesn't work and instead we use the ugly trick
475 // with using special m_oldStatusText value as "menu opened" (but it is
476 // arguably better than adding yet another member variable to wxFrame
478 if ( m_oldStatusText
.empty() )
480 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
481 if ( m_oldStatusText
.empty() )
483 // use special value to prevent us from doing this the next time
484 m_oldStatusText
+= wxT('\0');
491 else // hide help, restore the original text
493 // clear the last shown help string but remember its value
494 wxString lastHelpShown
;
495 lastHelpShown
.swap(m_lastHelpShown
);
497 // also clear the old status text but remember it too to restore it
499 text
.swap(m_oldStatusText
);
501 if ( statbar
->GetStatusText(m_statusBarPane
) != lastHelpShown
)
503 // if the text was changed with an explicit SetStatusText() call
504 // from the user code in the meanwhile, do not overwrite it with
505 // the old status bar contents -- this is almost certainly not what
506 // the user expects and would be very hard to avoid from user code
511 statbar
->SetStatusText(text
, m_statusBarPane
);
515 #endif // wxUSE_STATUSBAR
517 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
526 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
528 const wxString
& name
)
530 // the main toolbar can't be recreated (unless it was explicitly deleted
532 wxCHECK_MSG( !m_frameToolBar
, NULL
,
533 wxT("recreating toolbar in wxFrame") );
539 // NB: we don't specify the default value in the method declaration
541 // a) this allows us to have different defaults for different
542 // platforms (even if we don't have them right now)
543 // b) we don't need to include wx/toolbar.h in the header then
544 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
547 SetToolBar(OnCreateToolBar(style
, id
, name
));
549 return m_frameToolBar
;
552 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
554 const wxString
& name
)
556 #if defined(__WXWINCE__) && defined(__POCKETPC__)
557 return new wxToolMenuBar(this, id
,
558 wxDefaultPosition
, wxDefaultSize
,
561 return new wxToolBar(this, id
,
562 wxDefaultPosition
, wxDefaultSize
,
567 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
569 if ( (toolbar
!= NULL
) != (m_frameToolBar
!= NULL
) )
571 // the toolbar visibility must have changed so we need to both position
572 // the toolbar itself (if it appeared) and to relayout the frame
573 // contents in any case
577 // we need to assign it to m_frameToolBar for PositionToolBar() to
579 m_frameToolBar
= toolbar
;
582 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
583 // wouldn't recognize the (still existing) toolbar as one of our
584 // bars and wouldn't layout the single child of the frame correctly
587 // and this is even more tricky: we want DoLayout() to recognize the
588 // old toolbar for the purpose of not counting it among our non-bar
589 // children but we don't want to reserve any more space for it so we
590 // temporarily hide it
591 if ( m_frameToolBar
)
592 m_frameToolBar
->Hide();
596 if ( m_frameToolBar
)
597 m_frameToolBar
->Show();
600 // this might have been already done above but it's simpler to just always
601 // do it unconditionally instead of testing for whether we already did it
602 m_frameToolBar
= toolbar
;
605 #endif // wxUSE_TOOLBAR
607 // ----------------------------------------------------------------------------
609 // ----------------------------------------------------------------------------
614 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
618 wxEvtHandler
* source
= GetEventHandler();
619 menu
->UpdateUI(source
);
623 wxMenuBar
* bar
= GetMenuBar();
629 void wxFrameBase::DetachMenuBar()
631 if ( m_frameMenuBar
)
633 m_frameMenuBar
->Detach();
634 m_frameMenuBar
= NULL
;
638 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
642 menubar
->Attach((wxFrame
*)this);
643 m_frameMenuBar
= menubar
;
647 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
649 if ( menubar
== GetMenuBar() )
657 this->AttachMenuBar(menubar
);
660 wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
662 const wxMenuBar
* const menuBar
= GetMenuBar();
664 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
667 #endif // wxUSE_MENUS