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 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 #if wxUSE_MENUS && wxUSE_STATUSBAR
42 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
43 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
44 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose
)
46 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
49 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // construction/destruction
57 // ----------------------------------------------------------------------------
59 wxFrameBase::wxFrameBase()
62 m_frameMenuBar
= NULL
;
66 m_frameToolBar
= NULL
;
67 #endif // wxUSE_TOOLBAR
70 m_frameStatusBar
= NULL
;
71 #endif // wxUSE_STATUSBAR
76 wxFrameBase::~wxFrameBase()
78 // this destructor is required for Darwin
81 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
83 const wxString
& title
,
89 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
92 void wxFrameBase::DeleteAllBars()
97 delete m_frameMenuBar
;
98 m_frameMenuBar
= NULL
;
100 #endif // wxUSE_MENUS
103 if ( m_frameStatusBar
)
105 delete m_frameStatusBar
;
106 m_frameStatusBar
= NULL
;
108 #endif // wxUSE_STATUSBAR
111 if ( m_frameToolBar
)
113 delete m_frameToolBar
;
114 m_frameToolBar
= NULL
;
116 #endif // wxUSE_TOOLBAR
119 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
122 if ( win
== GetMenuBar() )
124 #endif // wxUSE_MENUS
127 if ( win
== GetStatusBar() )
129 #endif // wxUSE_STATUSBAR
132 if ( win
== GetToolBar() )
134 #endif // wxUSE_TOOLBAR
141 // ----------------------------------------------------------------------------
142 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
143 // from the client area, so the client area is what's really available for the
145 // ----------------------------------------------------------------------------
147 // get the origin of the client area in the client coordinates
148 wxPoint
wxFrameBase::GetClientAreaOrigin() const
150 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
152 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
153 wxToolBar
*toolbar
= GetToolBar();
154 if ( toolbar
&& toolbar
->IsShown() )
157 toolbar
->GetSize(&w
, &h
);
159 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
168 #endif // wxUSE_TOOLBAR
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
179 bool wxFrameBase::ProcessCommand(int id
)
181 wxMenuBar
*bar
= GetMenuBar();
185 wxMenuItem
*item
= bar
->FindItem(id
);
189 return ProcessCommand(item
);
192 bool wxFrameBase::ProcessCommand(wxMenuItem
*item
)
194 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
195 commandEvent
.SetEventObject(this);
197 if (!item
->IsEnabled())
200 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
203 if (item
->IsCheckable())
208 commandEvent
.SetInt(item
->IsChecked());
211 return HandleWindowEvent(commandEvent
);
214 #endif // wxUSE_MENUS
216 // Do the UI update processing for this window. This is
217 // provided for the application to call if it wants to
218 // force a UI update, particularly for the menus and toolbar.
219 void wxFrameBase::UpdateWindowUI(long flags
)
221 wxWindowBase::UpdateWindowUI(flags
);
225 GetToolBar()->UpdateWindowUI(flags
);
231 // If coming from an idle event, we only want to update the menus if
232 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
233 // will be update when the menu is opened later
234 #if !wxUSE_IDLEMENUUPDATES
235 if ( !(flags
& wxUPDATE_UI_FROMIDLE
) )
236 #endif // wxUSE_IDLEMENUUPDATES
239 #endif // wxUSE_MENUS
242 // ----------------------------------------------------------------------------
243 // event handlers for status bar updates from menus
244 // ----------------------------------------------------------------------------
246 #if wxUSE_MENUS && wxUSE_STATUSBAR
248 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
251 (void)ShowMenuHelp(event
.GetMenuId());
252 #endif // wxUSE_STATUSBAR
255 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
257 #if wxUSE_IDLEMENUUPDATES
259 #else // !wxUSE_IDLEMENUUPDATES
260 // as we didn't update the menus from idle time, do it now
261 DoMenuUpdates(event
.GetMenu());
262 #endif // wxUSE_IDLEMENUUPDATES/!wxUSE_IDLEMENUUPDATES
265 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
267 DoGiveHelp(wxEmptyString
, false);
270 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
272 // Implement internal behaviour (menu updating on some platforms)
273 void wxFrameBase::OnInternalIdle()
275 wxTopLevelWindow::OnInternalIdle();
277 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
278 if (wxUpdateUIEvent::CanUpdate(this))
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
289 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
292 const wxString
& name
)
294 // the main status bar can only be created once (or else it should be
295 // deleted before calling CreateStatusBar() again)
296 wxCHECK_MSG( !m_frameStatusBar
, NULL
,
297 wxT("recreating status bar in wxFrame") );
299 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
301 return m_frameStatusBar
;
304 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
307 const wxString
& name
)
309 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
311 statusBar
->SetFieldsCount(number
);
316 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
318 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
320 m_frameStatusBar
->SetStatusText(text
, number
);
323 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
325 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
327 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
332 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
334 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
336 m_frameStatusBar
->PushStatusText(text
, number
);
339 void wxFrameBase::PopStatusText(int number
)
341 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
343 m_frameStatusBar
->PopStatusText(number
);
346 bool wxFrameBase::ShowMenuHelp(int menuId
)
349 // if no help string found, we will clear the status bar text
351 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
353 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= wxID_NONE
)
355 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
356 if ( item
&& !item
->IsSeparator() )
357 helpString
= item
->GetHelp();
359 // notice that it's ok if we don't find the item because it might
360 // belong to the popup menu, so don't assert here
363 DoGiveHelp(helpString
, true);
365 return !helpString
.empty();
366 #else // !wxUSE_MENUS
368 #endif // wxUSE_MENUS/!wxUSE_MENUS
371 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
373 bool hadBar
= m_frameStatusBar
!= NULL
;
374 m_frameStatusBar
= statBar
;
376 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
384 #endif // wxUSE_STATUSBAR
386 #if wxUSE_MENUS || wxUSE_TOOLBAR
387 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
390 if ( m_statusBarPane
< 0 )
392 // status bar messages disabled
396 wxStatusBar
*statbar
= GetStatusBar();
403 // remember the old status bar text if this is the first time we're
404 // called since the menu has been opened as we're going to overwrite it
405 // in our DoGiveHelp() and we want to restore it when the menu is
408 // note that it would be logical to do this in OnMenuOpen() but under
409 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
410 // enough, and so this doesn't work and instead we use the ugly trick
411 // with using special m_oldStatusText value as "menu opened" (but it is
412 // arguably better than adding yet another member variable to wxFrame
414 if ( m_oldStatusText
.empty() )
416 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
417 if ( m_oldStatusText
.empty() )
419 // use special value to prevent us from doing this the next time
420 m_oldStatusText
+= wxT('\0');
427 else // hide help, restore the original text
429 // clear the last shown help string but remember its value
430 wxString lastHelpShown
;
431 lastHelpShown
.swap(m_lastHelpShown
);
433 // also clear the old status text but remember it too to restore it
435 text
.swap(m_oldStatusText
);
437 if ( statbar
->GetStatusText(m_statusBarPane
) != lastHelpShown
)
439 // if the text was changed with an explicit SetStatusText() call
440 // from the user code in the meanwhile, do not overwrite it with
441 // the old status bar contents -- this is almost certainly not what
442 // the user expects and would be very hard to avoid from user code
447 statbar
->SetStatusText(text
, m_statusBarPane
);
451 #endif // wxUSE_STATUSBAR
453 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
462 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
464 const wxString
& name
)
466 // the main toolbar can't be recreated (unless it was explicitly deleted
468 wxCHECK_MSG( !m_frameToolBar
, NULL
,
469 wxT("recreating toolbar in wxFrame") );
475 // NB: we don't specify the default value in the method declaration
477 // a) this allows us to have different defaults for different
478 // platforms (even if we don't have them right now)
479 // b) we don't need to include wx/toolbar.h in the header then
480 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
483 SetToolBar(OnCreateToolBar(style
, id
, name
));
485 return m_frameToolBar
;
488 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
490 const wxString
& name
)
492 #if defined(__WXWINCE__) && defined(__POCKETPC__)
493 return new wxToolMenuBar(this, id
,
494 wxDefaultPosition
, wxDefaultSize
,
497 return new wxToolBar(this, id
,
498 wxDefaultPosition
, wxDefaultSize
,
503 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
505 if ( (toolbar
!= NULL
) != (m_frameToolBar
!= NULL
) )
507 // the toolbar visibility must have changed so we need to both position
508 // the toolbar itself (if it appeared) and to relayout the frame
509 // contents in any case
513 // we need to assign it to m_frameToolBar for PositionToolBar() to
515 m_frameToolBar
= toolbar
;
518 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
519 // wouldn't recognize the (still existing) toolbar as one of our
520 // bars and wouldn't layout the single child of the frame correctly
523 // and this is even more tricky: we want DoLayout() to recognize the
524 // old toolbar for the purpose of not counting it among our non-bar
525 // children but we don't want to reserve any more space for it so we
526 // temporarily hide it
527 if ( m_frameToolBar
)
528 m_frameToolBar
->Hide();
532 if ( m_frameToolBar
)
533 m_frameToolBar
->Show();
536 // this might have been already done above but it's simpler to just always
537 // do it unconditionally instead of testing for whether we already did it
538 m_frameToolBar
= toolbar
;
541 #endif // wxUSE_TOOLBAR
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
550 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
554 wxEvtHandler
* source
= GetEventHandler();
555 menu
->UpdateUI(source
);
559 wxMenuBar
* bar
= GetMenuBar();
565 void wxFrameBase::DetachMenuBar()
567 if ( m_frameMenuBar
)
569 m_frameMenuBar
->Detach();
570 m_frameMenuBar
= NULL
;
574 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
578 menubar
->Attach((wxFrame
*)this);
579 m_frameMenuBar
= menubar
;
583 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
585 if ( menubar
== GetMenuBar() )
593 this->AttachMenuBar(menubar
);
596 wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
598 const wxMenuBar
* const menuBar
= GetMenuBar();
600 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
603 #endif // wxUSE_MENUS