1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ----------------------------------------------------------------------------
20 #pragma implementation "framebase.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
33 #include "wx/menuitem.h"
34 #include "wx/dcclient.h"
38 #include "wx/toolbar.h"
41 #include "wx/statusbr.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
49 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
50 EVT_IDLE(wxFrameBase::OnIdle
)
52 #if wxUSE_MENUS && !wxUSE_IDLEMENUUPDATES
53 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
55 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // construction/destruction
64 // ----------------------------------------------------------------------------
66 wxFrameBase::wxFrameBase()
69 m_frameMenuBar
= NULL
;
73 m_frameToolBar
= NULL
;
74 #endif // wxUSE_TOOLBAR
77 m_frameStatusBar
= NULL
;
78 #endif // wxUSE_STATUSBAR
83 wxFrameBase::~wxFrameBase()
85 // this destructor is required for Darwin
88 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
90 const wxString
& title
,
96 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
99 void wxFrameBase::DeleteAllBars()
102 if ( m_frameMenuBar
)
104 delete m_frameMenuBar
;
105 m_frameMenuBar
= (wxMenuBar
*) NULL
;
107 #endif // wxUSE_MENUS
110 if ( m_frameStatusBar
)
112 delete m_frameStatusBar
;
113 m_frameStatusBar
= (wxStatusBar
*) NULL
;
115 #endif // wxUSE_STATUSBAR
118 if ( m_frameToolBar
)
120 delete m_frameToolBar
;
121 m_frameToolBar
= (wxToolBar
*) NULL
;
123 #endif // wxUSE_TOOLBAR
126 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
129 if ( win
== GetMenuBar() )
131 #endif // wxUSE_MENUS
134 if ( win
== GetStatusBar() )
136 #endif // wxUSE_STATUSBAR
139 if ( win
== GetToolBar() )
141 #endif // wxUSE_TOOLBAR
146 // ----------------------------------------------------------------------------
147 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
148 // from the client area, so the client area is what's really available for the
150 // ----------------------------------------------------------------------------
152 // get the origin of the client area in the client coordinates
153 wxPoint
wxFrameBase::GetClientAreaOrigin() const
155 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
157 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
158 wxToolBar
*toolbar
= GetToolBar();
159 if ( toolbar
&& toolbar
->IsShown() )
162 toolbar
->GetSize(&w
, &h
);
164 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
173 #endif // wxUSE_TOOLBAR
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 bool wxFrameBase::ProcessCommand(int id
)
185 wxMenuBar
*bar
= GetMenuBar();
189 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
190 commandEvent
.SetEventObject(this);
192 wxMenuItem
*item
= bar
->FindItem(id
);
195 if (!item
->IsEnabled())
198 if (item
->IsCheckable())
203 commandEvent
.SetInt(item
->IsChecked());
207 GetEventHandler()->ProcessEvent(commandEvent
);
209 #else // !wxUSE_MENUS
211 #endif // wxUSE_MENUS/!wxUSE_MENUS
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
221 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
222 #endif // wxUSE_STATUSBAR
225 void wxFrameBase::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
227 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
228 if (wxUpdateUIEvent::CanUpdate())
233 void wxFrameBase::OnMenuOpen(wxMenuEvent
& WXUNUSED(event
))
235 #if wxUSE_MENUS && !wxUSE_IDLEMENUUPDATES
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
246 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
249 const wxString
& name
)
251 // the main status bar can only be created once (or else it should be
252 // deleted before calling CreateStatusBar() again)
253 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
254 wxT("recreating status bar in wxFrame") );
256 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
257 if ( m_frameStatusBar
)
260 return m_frameStatusBar
;
263 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
266 const wxString
& name
)
268 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
270 statusBar
->SetFieldsCount(number
);
275 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
277 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
279 m_frameStatusBar
->SetStatusText(text
, number
);
282 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
284 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
286 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
291 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
293 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
295 m_frameStatusBar
->PushStatusText(text
, number
);
298 void wxFrameBase::PopStatusText(int number
)
300 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
302 m_frameStatusBar
->PopStatusText(number
);
305 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*WXUNUSED(statbar
), int menuId
)
308 // if no help string found, we will clear the status bar text
310 bool show
= menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */;
314 wxMenuBar
*menuBar
= GetMenuBar();
317 // it's ok if we don't find the item because it might belong
319 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
321 helpString
= item
->GetHelp();
325 DoGiveHelp(helpString
, show
);
327 return !helpString
.IsEmpty();
328 #else // !wxUSE_MENUS
330 #endif // wxUSE_MENUS/!wxUSE_MENUS
333 #endif // wxUSE_STATUSBAR
335 void wxFrameBase::DoGiveHelp(const wxString
& text
, bool show
)
338 if ( m_statusBarPane
< 0 ) return;
339 wxStatusBar
* statbar
= GetStatusBar();
340 if ( !statbar
) return;
342 wxString help
= show
? text
: wxString();
343 statbar
->SetStatusText( help
, m_statusBarPane
);
344 #endif // wxUSE_STATUSBAR
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
354 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
356 const wxString
& name
)
358 // the main toolbar can't be recreated (unless it was explicitly deeleted
360 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
361 wxT("recreating toolbar in wxFrame") );
363 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
365 return m_frameToolBar
;
368 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
370 const wxString
& name
)
372 return new wxToolBar(this, id
,
373 wxDefaultPosition
, wxDefaultSize
,
377 #endif // wxUSE_TOOLBAR
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
386 void wxFrameBase::DoMenuUpdates()
388 wxMenuBar
* bar
= GetMenuBar();
391 wxWindow
* focusWin
= wxFindFocusDescendant((wxWindow
*) this);
393 wxWindow
* focusWin
= (wxWindow
*) NULL
;
397 int nCount
= bar
->GetMenuCount();
398 for (int n
= 0; n
< nCount
; n
++)
399 DoMenuUpdates(bar
->GetMenu(n
), focusWin
);
403 // update a menu and all submenus recursively
404 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
, wxWindow
* focusWin
)
406 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler();
407 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
410 wxMenuItem
* item
= node
->GetData();
411 if ( !item
->IsSeparator() )
413 wxWindowID id
= item
->GetId();
414 wxUpdateUIEvent
event(id
);
415 event
.SetEventObject( this );
417 if (evtHandler
->ProcessEvent(event
))
419 if (event
.GetSetText())
420 menu
->SetLabel(id
, event
.GetText());
421 if (event
.GetSetChecked())
422 menu
->Check(id
, event
.GetChecked());
423 if (event
.GetSetEnabled())
424 menu
->Enable(id
, event
.GetEnabled());
427 if (item
->GetSubMenu())
428 DoMenuUpdates(item
->GetSubMenu(), focusWin
);
430 node
= node
->GetNext();
434 void wxFrameBase::DetachMenuBar()
436 if ( m_frameMenuBar
)
438 m_frameMenuBar
->Detach();
439 m_frameMenuBar
= NULL
;
443 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
447 menubar
->Attach((wxFrame
*)this);
448 m_frameMenuBar
= menubar
;
452 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
454 if ( menubar
== GetMenuBar() )
462 AttachMenuBar(menubar
);
465 #endif // wxUSE_MENUS