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