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 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -3 /* wxID_TITLE */ )
353 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
354 if ( item
&& !item
->IsSeparator() )
355 helpString
= item
->GetHelp();
357 // notice that it's ok if we don't find the item because it might
358 // belong to the popup menu, so don't assert here
361 DoGiveHelp(helpString
, true);
363 return !helpString
.empty();
364 #else // !wxUSE_MENUS
366 #endif // wxUSE_MENUS/!wxUSE_MENUS
369 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
371 bool hadBar
= m_frameStatusBar
!= NULL
;
372 m_frameStatusBar
= statBar
;
374 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
382 #endif // wxUSE_STATUSBAR
384 #if wxUSE_MENUS || wxUSE_TOOLBAR
385 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
388 if ( m_statusBarPane
< 0 )
390 // status bar messages disabled
394 wxStatusBar
*statbar
= GetStatusBar();
401 // remember the old status bar text if this is the first time we're
402 // called since the menu has been opened as we're going to overwrite it
403 // in our DoGiveHelp() and we want to restore it when the menu is
406 // note that it would be logical to do this in OnMenuOpen() but under
407 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
408 // enough, and so this doesn't work and instead we use the ugly trick
409 // with using special m_oldStatusText value as "menu opened" (but it is
410 // arguably better than adding yet another member variable to wxFrame
412 if ( m_oldStatusText
.empty() )
414 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
415 if ( m_oldStatusText
.empty() )
417 // use special value to prevent us from doing this the next time
418 m_oldStatusText
+= _T('\0');
424 else // hide help, restore the original text
426 text
= m_oldStatusText
;
427 m_oldStatusText
.clear();
430 statbar
->SetStatusText(text
, m_statusBarPane
);
434 #endif // wxUSE_STATUSBAR
436 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
445 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
447 const wxString
& name
)
449 // the main toolbar can't be recreated (unless it was explicitly deleted
451 wxCHECK_MSG( !m_frameToolBar
, NULL
,
452 wxT("recreating toolbar in wxFrame") );
458 // NB: we don't specify the default value in the method declaration
460 // a) this allows us to have different defaults for different
461 // platforms (even if we don't have them right now)
462 // b) we don't need to include wx/toolbar.h in the header then
463 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
466 SetToolBar(OnCreateToolBar(style
, id
, name
));
468 return m_frameToolBar
;
471 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
473 const wxString
& name
)
475 #if defined(__WXWINCE__) && defined(__POCKETPC__)
476 return new wxToolMenuBar(this, id
,
477 wxDefaultPosition
, wxDefaultSize
,
480 return new wxToolBar(this, id
,
481 wxDefaultPosition
, wxDefaultSize
,
486 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
488 bool hadBar
= m_frameToolBar
!= NULL
;
489 m_frameToolBar
= toolbar
;
491 if ( (m_frameToolBar
!= NULL
) != hadBar
)
499 #endif // wxUSE_TOOLBAR
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
508 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
512 wxEvtHandler
* source
= GetEventHandler();
513 menu
->UpdateUI(source
);
517 wxMenuBar
* bar
= GetMenuBar();
523 void wxFrameBase::DetachMenuBar()
525 if ( m_frameMenuBar
)
527 m_frameMenuBar
->Detach();
528 m_frameMenuBar
= NULL
;
532 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
536 menubar
->Attach((wxFrame
*)this);
537 m_frameMenuBar
= menubar
;
541 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
543 if ( menubar
== GetMenuBar() )
551 this->AttachMenuBar(menubar
);
554 const wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
556 const wxMenuBar
* const menuBar
= GetMenuBar();
558 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
561 #endif // wxUSE_MENUS