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
174 void wxFrameBase::SendSizeEvent()
176 wxSizeEvent
event( GetSize(), GetId() );
177 event
.SetEventObject( this );
178 GetEventHandler()->AddPendingEvent( event
);
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 bool wxFrameBase::ProcessCommand(int id
)
189 wxMenuBar
*bar
= GetMenuBar();
193 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
194 commandEvent
.SetEventObject(this);
196 wxMenuItem
*item
= bar
->FindItem(id
);
199 if (!item
->IsEnabled())
202 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
205 if (item
->IsCheckable())
210 commandEvent
.SetInt(item
->IsChecked());
214 GetEventHandler()->ProcessEvent(commandEvent
);
216 #else // !wxUSE_MENUS
220 #endif // wxUSE_MENUS/!wxUSE_MENUS
223 // Do the UI update processing for this window. This is
224 // provided for the application to call if it wants to
225 // force a UI update, particularly for the menus and toolbar.
226 void wxFrameBase::UpdateWindowUI(long flags
)
228 wxWindowBase::UpdateWindowUI(flags
);
232 GetToolBar()->UpdateWindowUI(flags
);
238 if ((flags
& wxUPDATE_UI_FROMIDLE
) && !wxUSE_IDLEMENUUPDATES
)
240 // If coming from an idle event, we only
241 // want to update the menus if we're
242 // in the wxUSE_IDLEMENUUPDATES configuration:
243 // so if we're not, do nothing
248 #endif // wxUSE_MENUS
251 // ----------------------------------------------------------------------------
252 // event handlers for status bar updates from menus
253 // ----------------------------------------------------------------------------
255 #if wxUSE_MENUS && wxUSE_STATUSBAR
257 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
260 (void)ShowMenuHelp(event
.GetMenuId());
261 #endif // wxUSE_STATUSBAR
264 #if !wxUSE_IDLEMENUUPDATES
265 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
267 void wxFrameBase::OnMenuOpen(wxMenuEvent
& WXUNUSED(event
))
270 #if !wxUSE_IDLEMENUUPDATES
271 DoMenuUpdates(event
.GetMenu());
272 #endif // !wxUSE_IDLEMENUUPDATES
275 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
277 DoGiveHelp(wxEmptyString
, false);
280 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
282 // Implement internal behaviour (menu updating on some platforms)
283 void wxFrameBase::OnInternalIdle()
285 wxTopLevelWindow::OnInternalIdle();
287 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
288 if (wxUpdateUIEvent::CanUpdate(this))
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
299 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
302 const wxString
& name
)
304 // the main status bar can only be created once (or else it should be
305 // deleted before calling CreateStatusBar() again)
306 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
307 wxT("recreating status bar in wxFrame") );
309 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
311 return m_frameStatusBar
;
314 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
317 const wxString
& name
)
319 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
321 statusBar
->SetFieldsCount(number
);
326 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
328 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
330 m_frameStatusBar
->SetStatusText(text
, number
);
333 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
335 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
337 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
342 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
344 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
346 m_frameStatusBar
->PushStatusText(text
, number
);
349 void wxFrameBase::PopStatusText(int number
)
351 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
353 m_frameStatusBar
->PopStatusText(number
);
356 bool wxFrameBase::ShowMenuHelp(int menuId
)
359 // if no help string found, we will clear the status bar text
361 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -3 /* wxID_TITLE */ )
363 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
365 helpString
= item
->GetHelp();
367 // notice that it's ok if we don't find the item because it might
368 // belong to the popup menu, so don't assert here
371 DoGiveHelp(helpString
, true);
373 return !helpString
.empty();
374 #else // !wxUSE_MENUS
376 #endif // wxUSE_MENUS/!wxUSE_MENUS
379 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
381 bool hadBar
= m_frameStatusBar
!= NULL
;
382 m_frameStatusBar
= statBar
;
384 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
392 #endif // wxUSE_STATUSBAR
394 #if wxUSE_MENUS || wxUSE_TOOLBAR
395 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
398 if ( m_statusBarPane
< 0 )
400 // status bar messages disabled
404 wxStatusBar
*statbar
= GetStatusBar();
411 // remember the old status bar text if this is the first time we're
412 // called since the menu has been opened as we're going to overwrite it
413 // in our DoGiveHelp() and we want to restore it when the menu is
416 // note that it would be logical to do this in OnMenuOpen() but under
417 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
418 // enough, and so this doesn't work and instead we use the ugly trick
419 // with using special m_oldStatusText value as "menu opened" (but it is
420 // arguably better than adding yet another member variable to wxFrame
422 if ( m_oldStatusText
.empty() )
424 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
425 if ( m_oldStatusText
.empty() )
427 // use special value to prevent us from doing this the next time
428 m_oldStatusText
+= _T('\0');
434 else // hide help, restore the original text
436 text
= m_oldStatusText
;
437 m_oldStatusText
.clear();
440 statbar
->SetStatusText(text
, m_statusBarPane
);
444 #endif // wxUSE_STATUSBAR
446 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
455 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
457 const wxString
& name
)
459 // the main toolbar can't be recreated (unless it was explicitly deleted
461 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
462 wxT("recreating toolbar in wxFrame") );
468 // NB: we don't specify the default value in the method declaration
470 // a) this allows us to have different defaults for different
471 // platforms (even if we don't have them right now)
472 // b) we don't need to include wx/toolbar.h in the header then
473 style
= wxBORDER_NONE
| wxTB_HORIZONTAL
| wxTB_FLAT
;
476 SetToolBar(OnCreateToolBar(style
, id
, name
));
478 return m_frameToolBar
;
481 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
483 const wxString
& name
)
485 #if defined(__WXWINCE__) && defined(__POCKETPC__)
486 return new wxToolMenuBar(this, id
,
487 wxDefaultPosition
, wxDefaultSize
,
490 return new wxToolBar(this, id
,
491 wxDefaultPosition
, wxDefaultSize
,
496 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
498 bool hadBar
= m_frameToolBar
!= NULL
;
499 m_frameToolBar
= toolbar
;
501 if ( (m_frameToolBar
!= NULL
) != hadBar
)
509 #endif // wxUSE_TOOLBAR
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
518 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
522 wxEvtHandler
* source
= GetEventHandler();
523 menu
->UpdateUI(source
);
527 wxMenuBar
* bar
= GetMenuBar();
533 void wxFrameBase::DetachMenuBar()
535 if ( m_frameMenuBar
)
537 m_frameMenuBar
->Detach();
538 m_frameMenuBar
= NULL
;
542 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
546 menubar
->Attach((wxFrame
*)this);
547 m_frameMenuBar
= menubar
;
551 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
553 if ( menubar
== GetMenuBar() )
561 this->AttachMenuBar(menubar
);
564 const wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
566 const wxMenuBar
* const menuBar
= GetMenuBar();
568 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
571 #endif // wxUSE_MENUS