]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Add ability to have a wxMenuBar as a common menu
[wxWidgets.git] / src / common / framecmn.cpp
CommitLineData
63fec618 1/////////////////////////////////////////////////////////////////////////////
7c0ea335 2// Name: common/framecmn.cpp
63fec618
VZ
3// Purpose: common (for all platforms) wxFrame functions
4// Author: Julian Smart, Vadim Zeitlin
5// Created: 01/02/97
439b3bf1 6// Id: $Id$
55d99c7a 7// Copyright: (c) 1998 Robert Roebling and Julian Smart
63fec618
VZ
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
7c0ea335
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#ifdef __GNUG__
20 #pragma implementation "framebase.h"
21#endif
22
f701d7ab
JS
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
7c0ea335 27 #pragma hdrstop
f701d7ab
JS
28#endif
29
1e6feb95
VZ
30#ifndef WX_PRECOMP
31 #include "wx/frame.h"
32 #include "wx/menu.h"
33 #include "wx/menuitem.h"
34 #include "wx/dcclient.h"
35#endif // WX_PRECOMP
7c0ea335
VZ
36
37#if wxUSE_TOOLBAR
38 #include "wx/toolbar.h"
39#endif
40#if wxUSE_STATUSBAR
41 #include "wx/statusbr.h"
42#endif
43
44// ----------------------------------------------------------------------------
45// event table
46// ----------------------------------------------------------------------------
47
7d9f12f3 48BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
7c0ea335 49 EVT_IDLE(wxFrameBase::OnIdle)
7c0ea335 50 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
51END_EVENT_TABLE()
52
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ----------------------------------------------------------------------------
58// construction/destruction
59// ----------------------------------------------------------------------------
60
61wxFrameBase::wxFrameBase()
62{
1e6feb95 63#if wxUSE_MENUS
7c0ea335 64 m_frameMenuBar = NULL;
1e6feb95 65#endif // wxUSE_MENUS
7c0ea335
VZ
66
67#if wxUSE_TOOLBAR
68 m_frameToolBar = NULL;
69#endif // wxUSE_TOOLBAR
70
71#if wxUSE_STATUSBAR
72 m_frameStatusBar = NULL;
73#endif // wxUSE_STATUSBAR
1f361cdd
MB
74
75 m_statusBarPane = 0;
7c0ea335
VZ
76}
77
799ea011
GD
78wxFrameBase::~wxFrameBase()
79{
80 // this destructor is required for Darwin
81}
82
7c0ea335
VZ
83wxFrame *wxFrameBase::New(wxWindow *parent,
84 wxWindowID id,
85 const wxString& title,
86 const wxPoint& pos,
87 const wxSize& size,
88 long style,
89 const wxString& name)
90{
91 return new wxFrame(parent, id, title, pos, size, style, name);
92}
93
94void wxFrameBase::DeleteAllBars()
95{
1e6feb95 96#if wxUSE_MENUS
7c0ea335
VZ
97 if ( m_frameMenuBar )
98 {
99 delete m_frameMenuBar;
100 m_frameMenuBar = (wxMenuBar *) NULL;
101 }
1e6feb95 102#endif // wxUSE_MENUS
7c0ea335
VZ
103
104#if wxUSE_STATUSBAR
105 if ( m_frameStatusBar )
106 {
107 delete m_frameStatusBar;
108 m_frameStatusBar = (wxStatusBar *) NULL;
109 }
110#endif // wxUSE_STATUSBAR
111
112#if wxUSE_TOOLBAR
113 if ( m_frameToolBar )
114 {
115 delete m_frameToolBar;
116 m_frameToolBar = (wxToolBar *) NULL;
117 }
118#endif // wxUSE_TOOLBAR
119}
120
1e6feb95
VZ
121bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
122{
123#if wxUSE_MENUS
124 if ( win == GetMenuBar() )
125 return TRUE;
126#endif // wxUSE_MENUS
127
128#if wxUSE_STATUSBAR
129 if ( win == GetStatusBar() )
130 return TRUE;
131#endif // wxUSE_STATUSBAR
132
133#if wxUSE_TOOLBAR
134 if ( win == GetToolBar() )
135 return TRUE;
136#endif // wxUSE_TOOLBAR
137
138 return FALSE;
139}
140
1c4f8f8d
VZ
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
144// frame contents
145// ----------------------------------------------------------------------------
146
147// get the origin of the client area in the client coordinates
148wxPoint wxFrameBase::GetClientAreaOrigin() const
149{
7d9f12f3 150 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 151
16c9a425 152#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
153 wxToolBar *toolbar = GetToolBar();
154 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
155 {
156 int w, h;
d4597e13 157 toolbar->GetSize(&w, &h);
1c4f8f8d 158
d4597e13 159 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
160 {
161 pt.x += w;
162 }
163 else
164 {
165 pt.y += h;
166 }
167 }
168#endif // wxUSE_TOOLBAR
169
170 return pt;
171}
172
7c0ea335
VZ
173// ----------------------------------------------------------------------------
174// misc
175// ----------------------------------------------------------------------------
176
7c0ea335
VZ
177bool wxFrameBase::ProcessCommand(int id)
178{
1e6feb95 179#if wxUSE_MENUS
7c0ea335
VZ
180 wxMenuBar *bar = GetMenuBar();
181 if ( !bar )
182 return FALSE;
183
3ca6a5f0
BP
184 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
185 commandEvent.SetEventObject(this);
186
7c0ea335 187 wxMenuItem *item = bar->FindItem(id);
84f7908b 188 if (item)
7c0ea335 189 {
84f7908b
RR
190 if (!item->IsEnabled())
191 return TRUE;
d4597e13 192
84f7908b
RR
193 if (item->IsCheckable())
194 {
195 item->Toggle();
0472ece7 196
84f7908b
RR
197 // use the new value
198 commandEvent.SetInt(item->IsChecked());
199 }
3ca6a5f0 200 }
7c0ea335 201
a01fe3d6
RD
202 GetEventHandler()->ProcessEvent(commandEvent);
203 return TRUE;
1e6feb95
VZ
204#else // !wxUSE_MENUS
205 return FALSE;
206#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
207}
208
209// ----------------------------------------------------------------------------
210// event handlers
211// ----------------------------------------------------------------------------
212
7c0ea335
VZ
213void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
214{
215#if wxUSE_STATUSBAR
f6bcfd97 216 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
217#endif // wxUSE_STATUSBAR
218}
219
6522713c
VZ
220void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
221{
222#if wxUSE_MENUS
223 DoMenuUpdates();
224#endif // wxUSE_MENUS
225}
226
7c0ea335
VZ
227// ----------------------------------------------------------------------------
228// status bar stuff
229// ----------------------------------------------------------------------------
230
231#if wxUSE_STATUSBAR
232
233wxStatusBar* wxFrameBase::CreateStatusBar(int number,
234 long style,
235 wxWindowID id,
236 const wxString& name)
237{
238 // the main status bar can only be created once (or else it should be
239 // deleted before calling CreateStatusBar() again)
240 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
241 wxT("recreating status bar in wxFrame") );
242
243 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
244 if ( m_frameStatusBar )
245 PositionStatusBar();
246
247 return m_frameStatusBar;
248}
249
250wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
251 long style,
252 wxWindowID id,
253 const wxString& name)
254{
ed791986 255 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 256
7c0ea335
VZ
257 statusBar->SetFieldsCount(number);
258
259 return statusBar;
260}
261
262void wxFrameBase::SetStatusText(const wxString& text, int number)
263{
7c0ea335
VZ
264 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
265
266 m_frameStatusBar->SetStatusText(text, number);
267}
268
269void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
270{
7c0ea335
VZ
271 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
272
273 m_frameStatusBar->SetStatusWidths(n, widths_field);
274
275 PositionStatusBar();
276}
277
1f361cdd 278void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 279{
1f361cdd
MB
280 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
281
282 m_frameStatusBar->PushStatusText(text, number);
283}
f6bcfd97 284
1f361cdd
MB
285void wxFrameBase::PopStatusText(int number)
286{
287 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
288
289 m_frameStatusBar->PopStatusText(number);
290}
291
1f361cdd
MB
292bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
293{
294#if wxUSE_MENUS
f6bcfd97
BP
295 // if no help string found, we will clear the status bar text
296 wxString helpString;
1f361cdd 297 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 298
1f361cdd 299 if ( show )
f6bcfd97
BP
300 {
301 wxMenuBar *menuBar = GetMenuBar();
302 if ( menuBar )
303 {
304 // it's ok if we don't find the item because it might belong
305 // to the popup menu
306 wxMenuItem *item = menuBar->FindItem(menuId);
307 if ( item )
308 helpString = item->GetHelp();
309 }
310 }
311
1f361cdd 312 DoGiveHelp(helpString, show);
f6bcfd97
BP
313
314 return !helpString.IsEmpty();
3379ed37
VZ
315#else // !wxUSE_MENUS
316 return FALSE;
317#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
318}
319
7c0ea335
VZ
320#endif // wxUSE_STATUSBAR
321
c60a36d5
VZ
322void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
323{
324#if wxUSE_STATUSBAR
325 if ( m_statusBarPane < 0 ) return;
326 wxStatusBar* statbar = GetStatusBar();
327 if ( !statbar ) return;
328
329 wxString help = show ? text : wxString();
330 statbar->SetStatusText( help, m_statusBarPane );
331#endif // wxUSE_STATUSBAR
332}
333
334
7c0ea335
VZ
335// ----------------------------------------------------------------------------
336// toolbar stuff
337// ----------------------------------------------------------------------------
338
339#if wxUSE_TOOLBAR
340
341wxToolBar* wxFrameBase::CreateToolBar(long style,
342 wxWindowID id,
343 const wxString& name)
344{
345 // the main toolbar can't be recreated (unless it was explicitly deeleted
346 // before)
347 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
348 wxT("recreating toolbar in wxFrame") );
349
350 m_frameToolBar = OnCreateToolBar(style, id, name);
351
352 return m_frameToolBar;
353}
354
355wxToolBar* wxFrameBase::OnCreateToolBar(long style,
356 wxWindowID id,
357 const wxString& name)
358{
359 return new wxToolBar(this, id,
360 wxDefaultPosition, wxDefaultSize,
361 style, name);
362}
363
364#endif // wxUSE_TOOLBAR
365
366// ----------------------------------------------------------------------------
6522713c 367// menus
7c0ea335
VZ
368// ----------------------------------------------------------------------------
369
1e6feb95
VZ
370#if wxUSE_MENUS
371
63fec618 372// update all menus
7c0ea335 373void wxFrameBase::DoMenuUpdates()
63fec618 374{
54517652 375 wxMenuBar* bar = GetMenuBar();
e702ff0f 376
f0e9f0d3 377#ifdef __WXMSW__
e63fdcd6 378 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
f0e9f0d3
JS
379#else
380 wxWindow* focusWin = (wxWindow*) NULL;
381#endif
7c0ea335 382 if ( bar != NULL )
54517652
RR
383 {
384 int nCount = bar->GetMenuCount();
385 for (int n = 0; n < nCount; n++)
e63fdcd6 386 DoMenuUpdates(bar->GetMenu(n), focusWin);
54517652 387 }
63fec618
VZ
388}
389
390// update a menu and all submenus recursively
e63fdcd6 391void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
7c0ea335 392{
e63fdcd6 393 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
7c0ea335
VZ
394 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
395 while (node)
63fec618 396 {
7c0ea335
VZ
397 wxMenuItem* item = node->GetData();
398 if ( !item->IsSeparator() )
399 {
400 wxWindowID id = item->GetId();
401 wxUpdateUIEvent event(id);
402 event.SetEventObject( this );
403
404 if (evtHandler->ProcessEvent(event))
405 {
406 if (event.GetSetText())
407 menu->SetLabel(id, event.GetText());
408 if (event.GetSetChecked())
409 menu->Check(id, event.GetChecked());
410 if (event.GetSetEnabled())
411 menu->Enable(id, event.GetEnabled());
412 }
413
414 if (item->GetSubMenu())
7ac4c27a 415 DoMenuUpdates(item->GetSubMenu(), focusWin);
7c0ea335
VZ
416 }
417 node = node->GetNext();
63fec618 418 }
63fec618 419}
1e6feb95 420
6522713c
VZ
421void wxFrameBase::DetachMenuBar()
422{
423 if ( m_frameMenuBar )
424 {
425 m_frameMenuBar->Detach();
426 m_frameMenuBar = NULL;
427 }
428}
429
430void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
431{
432 if ( menubar )
433 {
6522713c 434 menubar->Attach((wxFrame *)this);
3dbe38c3 435 m_frameMenuBar = menubar;
6522713c
VZ
436 }
437}
438
439void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
440{
441 if ( menubar == GetMenuBar() )
442 {
443 // nothing to do
444 return;
445 }
446
447 DetachMenuBar();
448
449 AttachMenuBar(menubar);
450}
451
1e6feb95 452#endif // wxUSE_MENUS