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
= (wxMenuBar
*) NULL
;
100 #endif // wxUSE_MENUS
103 if ( m_frameStatusBar
)
105 delete m_frameStatusBar
;
106 m_frameStatusBar
= (wxStatusBar
*) NULL
;
108 #endif // wxUSE_STATUSBAR
111 if ( m_frameToolBar
)
113 delete m_frameToolBar
;
114 m_frameToolBar
= (wxToolBar
*) 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 ((flags
& wxUPDATE_UI_FROMIDLE
) && !wxUSE_IDLEMENUUPDATES
)
233 // If coming from an idle event, we only
234 // want to update the menus if we're
235 // in the wxUSE_IDLEMENUUPDATES configuration:
236 // so if we're not, do nothing
241 #endif // wxUSE_MENUS
244 // ----------------------------------------------------------------------------
245 // event handlers for status bar updates from menus
246 // ----------------------------------------------------------------------------
248 #if wxUSE_MENUS && wxUSE_STATUSBAR
250 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
253 (void)ShowMenuHelp(event
.GetMenuId());
254 #endif // wxUSE_STATUSBAR
257 #if !wxUSE_IDLEMENUUPDATES
258 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
260 void wxFrameBase::OnMenuOpen(wxMenuEvent
& WXUNUSED(event
))
263 #if !wxUSE_IDLEMENUUPDATES
264 DoMenuUpdates(event
.GetMenu());
265 #endif // !wxUSE_IDLEMENUUPDATES
268 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
270 DoGiveHelp(wxEmptyString
, false);
273 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
275 // Implement internal behaviour (menu updating on some platforms)
276 void wxFrameBase::OnInternalIdle()
278 wxTopLevelWindow::OnInternalIdle();
280 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
281 if (wxUpdateUIEvent::CanUpdate(this))
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
292 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
295 const wxString
& name
)
297 // the main status bar can only be created once (or else it should be
298 // deleted before calling CreateStatusBar() again)
299 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
300 wxT("recreating status bar in wxFrame") );
302 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
304 return m_frameStatusBar
;
307 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
310 const wxString
& name
)
312 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
314 statusBar
->SetFieldsCount(number
);
319 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
321 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
323 m_frameStatusBar
->SetStatusText(text
, number
);
326 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
328 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
330 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
335 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
337 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
339 m_frameStatusBar
->PushStatusText(text
, number
);
342 void wxFrameBase::PopStatusText(int number
)
344 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
346 m_frameStatusBar
->PopStatusText(number
);
349 bool wxFrameBase::ShowMenuHelp(int menuId
)
352 // if no help string found, we will clear the status bar text
354 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -3 /* wxID_TITLE */ )
356 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
357 if ( item
&& !item
->IsSeparator() )
358 helpString
= item
->GetHelp();
360 // notice that it's ok if we don't find the item because it might
361 // belong to the popup menu, so don't assert here
364 DoGiveHelp(helpString
, true);
366 return !helpString
.empty();
367 #else // !wxUSE_MENUS
369 #endif // wxUSE_MENUS/!wxUSE_MENUS
372 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
374 bool hadBar
= m_frameStatusBar
!= NULL
;
375 m_frameStatusBar
= statBar
;
377 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
385 #endif // wxUSE_STATUSBAR
387 #if wxUSE_MENUS || wxUSE_TOOLBAR
388 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
391 if ( m_statusBarPane
< 0 )
393 // status bar messages disabled
397 wxStatusBar
*statbar
= GetStatusBar();
404 // remember the old status bar text if this is the first time we're
405 // called since the menu has been opened as we're going to overwrite it
406 // in our DoGiveHelp() and we want to restore it when the menu is
409 // note that it would be logical to do this in OnMenuOpen() but under
410 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
411 // enough, and so this doesn't work and instead we use the ugly trick
412 // with using special m_oldStatusText value as "menu opened" (but it is
413 // arguably better than adding yet another member variable to wxFrame
415 if ( m_oldStatusText
.empty() )
417 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
418 if ( m_oldStatusText
.empty() )
420 // use special value to prevent us from doing this the next time
421 m_oldStatusText
+= _T('\0');
427 else // hide help, restore the original text
429 text
= m_oldStatusText
;
430 m_oldStatusText
.clear();
433 statbar
->SetStatusText(text
, m_statusBarPane
);
437 #endif // wxUSE_STATUSBAR
439 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
448 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
450 const wxString
& name
)
452 // the main toolbar can't be recreated (unless it was explicitly deleted
454 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
455 wxT("recreating toolbar in wxFrame") );
461 // NB: we don't specify the default value in the method declaration
463 // a) this allows us to have different defaults for different
464 // platforms (even if we don't have them right now)
465 // b) we don't need to include wx/toolbar.h in the header then
466 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
469 SetToolBar(OnCreateToolBar(style
, id
, name
));
471 return m_frameToolBar
;
474 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
476 const wxString
& name
)
478 #if defined(__WXWINCE__) && defined(__POCKETPC__)
479 return new wxToolMenuBar(this, id
,
480 wxDefaultPosition
, wxDefaultSize
,
483 return new wxToolBar(this, id
,
484 wxDefaultPosition
, wxDefaultSize
,
489 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
491 bool hadBar
= m_frameToolBar
!= NULL
;
492 m_frameToolBar
= toolbar
;
494 if ( (m_frameToolBar
!= NULL
) != hadBar
)
502 #endif // wxUSE_TOOLBAR
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
511 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
515 wxEvtHandler
* source
= GetEventHandler();
516 menu
->UpdateUI(source
);
520 wxMenuBar
* bar
= GetMenuBar();
526 void wxFrameBase::DetachMenuBar()
528 if ( m_frameMenuBar
)
530 m_frameMenuBar
->Detach();
531 m_frameMenuBar
= NULL
;
535 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
539 menubar
->Attach((wxFrame
*)this);
540 m_frameMenuBar
= menubar
;
544 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
546 if ( menubar
== GetMenuBar() )
554 this->AttachMenuBar(menubar
);
557 const wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
559 const wxMenuBar
* const menuBar
= GetMenuBar();
561 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
564 #endif // wxUSE_MENUS