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, Julian Smart and Markus Holzem
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 // FIXME - temporary hack in absence of wxTLW in all ports!
45 #ifndef wxTopLevelWindowNative
46 #define wxTopLevelWindow wxTopLevelWindowBase
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
54 EVT_IDLE(wxFrameBase::OnIdle
)
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
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
139 // ----------------------------------------------------------------------------
140 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
141 // from the client area, so the client area is what's really available for the
143 // ----------------------------------------------------------------------------
145 // get the origin of the client area in the client coordinates
146 wxPoint
wxFrameBase::GetClientAreaOrigin() const
148 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
151 if ( GetToolBar() && GetToolBar()->IsShown() )
154 GetToolBar()->GetSize(& w
, & h
);
156 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
165 #endif // wxUSE_TOOLBAR
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 bool wxFrameBase::ProcessCommand(int id
)
177 wxMenuBar
*bar
= GetMenuBar();
181 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
182 commandEvent
.SetEventObject(this);
184 wxMenuItem
*item
= bar
->FindItem(id
);
185 if ( item
&& item
->IsCheckable() )
190 commandEvent
.SetInt(item
->IsChecked());
193 return GetEventHandler()->ProcessEvent(commandEvent
);
194 #else // !wxUSE_MENUS
196 #endif // wxUSE_MENUS/!wxUSE_MENUS
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
206 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
207 #endif // wxUSE_STATUSBAR
210 void wxFrameBase::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
214 #endif // wxUSE_MENUS
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
223 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
226 const wxString
& name
)
228 // the main status bar can only be created once (or else it should be
229 // deleted before calling CreateStatusBar() again)
230 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
231 wxT("recreating status bar in wxFrame") );
233 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
234 if ( m_frameStatusBar
)
237 return m_frameStatusBar
;
240 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
243 const wxString
& name
)
245 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
247 // Set the height according to the font and the border size
248 wxClientDC
dc(statusBar
);
249 dc
.SetFont(statusBar
->GetFont());
252 dc
.GetTextExtent( "X", NULL
, &y
);
254 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
256 statusBar
->SetSize( -1, -1, -1, height
);
258 statusBar
->SetFieldsCount(number
);
263 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
265 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
267 m_frameStatusBar
->SetStatusText(text
, number
);
270 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
272 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
274 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
279 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*statbar
, int menuId
)
285 // if no help string found, we will clear the status bar text
288 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */ )
290 wxMenuBar
*menuBar
= GetMenuBar();
293 // it's ok if we don't find the item because it might belong
295 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
297 helpString
= item
->GetHelp();
301 // set status text even if the string is empty - this will at least
302 // remove the string from the item which was previously selected
303 statbar
->SetStatusText(helpString
);
305 return !helpString
.IsEmpty();
306 #else // !wxUSE_MENUS
308 #endif // wxUSE_MENUS/!wxUSE_MENUS
311 #endif // wxUSE_STATUSBAR
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
319 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
321 const wxString
& name
)
323 // the main toolbar can't be recreated (unless it was explicitly deeleted
325 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
326 wxT("recreating toolbar in wxFrame") );
328 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
330 return m_frameToolBar
;
333 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
335 const wxString
& name
)
337 return new wxToolBar(this, id
,
338 wxDefaultPosition
, wxDefaultSize
,
342 #endif // wxUSE_TOOLBAR
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
351 void wxFrameBase::DoMenuUpdates()
353 wxMenuBar
* bar
= GetMenuBar();
356 wxWindow
* focusWin
= wxFindFocusDescendant((wxWindow
*) this);
358 wxWindow
* focusWin
= (wxWindow
*) NULL
;
362 int nCount
= bar
->GetMenuCount();
363 for (int n
= 0; n
< nCount
; n
++)
364 DoMenuUpdates(bar
->GetMenu(n
), focusWin
);
368 // update a menu and all submenus recursively
369 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
, wxWindow
* focusWin
)
371 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler();
372 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
375 wxMenuItem
* item
= node
->GetData();
376 if ( !item
->IsSeparator() )
378 wxWindowID id
= item
->GetId();
379 wxUpdateUIEvent
event(id
);
380 event
.SetEventObject( this );
382 if (evtHandler
->ProcessEvent(event
))
384 if (event
.GetSetText())
385 menu
->SetLabel(id
, event
.GetText());
386 if (event
.GetSetChecked())
387 menu
->Check(id
, event
.GetChecked());
388 if (event
.GetSetEnabled())
389 menu
->Enable(id
, event
.GetEnabled());
392 if (item
->GetSubMenu())
393 DoMenuUpdates(item
->GetSubMenu(), (wxWindow
*) NULL
);
395 node
= node
->GetNext();
399 void wxFrameBase::DetachMenuBar()
401 if ( m_frameMenuBar
)
403 m_frameMenuBar
->Detach();
404 m_frameMenuBar
= NULL
;
408 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
412 m_frameMenuBar
= menubar
;
413 menubar
->Attach((wxFrame
*)this);
417 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
419 if ( menubar
== GetMenuBar() )
427 AttachMenuBar(menubar
);
430 #endif // wxUSE_MENUS