]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
bundle icon
[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
VZ
201
202 return GetEventHandler()->ProcessEvent(commandEvent);
1e6feb95
VZ
203#else // !wxUSE_MENUS
204 return FALSE;
205#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
206}
207
208// ----------------------------------------------------------------------------
209// event handlers
210// ----------------------------------------------------------------------------
211
7c0ea335
VZ
212void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
213{
214#if wxUSE_STATUSBAR
f6bcfd97 215 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
216#endif // wxUSE_STATUSBAR
217}
218
6522713c
VZ
219void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
220{
221#if wxUSE_MENUS
222 DoMenuUpdates();
223#endif // wxUSE_MENUS
224}
225
7c0ea335
VZ
226// ----------------------------------------------------------------------------
227// status bar stuff
228// ----------------------------------------------------------------------------
229
230#if wxUSE_STATUSBAR
231
232wxStatusBar* wxFrameBase::CreateStatusBar(int number,
233 long style,
234 wxWindowID id,
235 const wxString& name)
236{
237 // the main status bar can only be created once (or else it should be
238 // deleted before calling CreateStatusBar() again)
239 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
240 wxT("recreating status bar in wxFrame") );
241
242 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
243 if ( m_frameStatusBar )
244 PositionStatusBar();
245
246 return m_frameStatusBar;
247}
248
249wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
250 long style,
251 wxWindowID id,
252 const wxString& name)
253{
ed791986 254 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 255
7c0ea335
VZ
256 statusBar->SetFieldsCount(number);
257
258 return statusBar;
259}
260
261void wxFrameBase::SetStatusText(const wxString& text, int number)
262{
7c0ea335
VZ
263 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
264
265 m_frameStatusBar->SetStatusText(text, number);
266}
267
268void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
269{
7c0ea335
VZ
270 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
271
272 m_frameStatusBar->SetStatusWidths(n, widths_field);
273
274 PositionStatusBar();
275}
276
1f361cdd 277void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 278{
1f361cdd
MB
279 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
280
281 m_frameStatusBar->PushStatusText(text, number);
282}
f6bcfd97 283
1f361cdd
MB
284void wxFrameBase::PopStatusText(int number)
285{
286 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
287
288 m_frameStatusBar->PopStatusText(number);
289}
290
291void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
292{
293#if wxUSE_STATUSBAR
294 if ( m_statusBarPane < 0 ) return;
295 wxStatusBar* statbar = GetStatusBar();
296 if ( !statbar ) return;
297
298 wxString help = show ? text : wxString();
299 statbar->SetStatusText( help, m_statusBarPane );
300#endif // wxUSE_STATUSBAR
301}
302
303bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
304{
305#if wxUSE_MENUS
f6bcfd97
BP
306 // if no help string found, we will clear the status bar text
307 wxString helpString;
1f361cdd 308 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 309
1f361cdd 310 if ( show )
f6bcfd97
BP
311 {
312 wxMenuBar *menuBar = GetMenuBar();
313 if ( menuBar )
314 {
315 // it's ok if we don't find the item because it might belong
316 // to the popup menu
317 wxMenuItem *item = menuBar->FindItem(menuId);
318 if ( item )
319 helpString = item->GetHelp();
320 }
321 }
322
1f361cdd 323 DoGiveHelp(helpString, show);
f6bcfd97
BP
324
325 return !helpString.IsEmpty();
3379ed37
VZ
326#else // !wxUSE_MENUS
327 return FALSE;
328#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
329}
330
7c0ea335
VZ
331#endif // wxUSE_STATUSBAR
332
333// ----------------------------------------------------------------------------
334// toolbar stuff
335// ----------------------------------------------------------------------------
336
337#if wxUSE_TOOLBAR
338
339wxToolBar* wxFrameBase::CreateToolBar(long style,
340 wxWindowID id,
341 const wxString& name)
342{
343 // the main toolbar can't be recreated (unless it was explicitly deeleted
344 // before)
345 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
346 wxT("recreating toolbar in wxFrame") );
347
348 m_frameToolBar = OnCreateToolBar(style, id, name);
349
350 return m_frameToolBar;
351}
352
353wxToolBar* wxFrameBase::OnCreateToolBar(long style,
354 wxWindowID id,
355 const wxString& name)
356{
357 return new wxToolBar(this, id,
358 wxDefaultPosition, wxDefaultSize,
359 style, name);
360}
361
362#endif // wxUSE_TOOLBAR
363
364// ----------------------------------------------------------------------------
6522713c 365// menus
7c0ea335
VZ
366// ----------------------------------------------------------------------------
367
1e6feb95
VZ
368#if wxUSE_MENUS
369
63fec618 370// update all menus
7c0ea335 371void wxFrameBase::DoMenuUpdates()
63fec618 372{
54517652 373 wxMenuBar* bar = GetMenuBar();
e702ff0f 374
f0e9f0d3 375#ifdef __WXMSW__
e63fdcd6 376 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
f0e9f0d3
JS
377#else
378 wxWindow* focusWin = (wxWindow*) NULL;
379#endif
7c0ea335 380 if ( bar != NULL )
54517652
RR
381 {
382 int nCount = bar->GetMenuCount();
383 for (int n = 0; n < nCount; n++)
e63fdcd6 384 DoMenuUpdates(bar->GetMenu(n), focusWin);
54517652 385 }
63fec618
VZ
386}
387
388// update a menu and all submenus recursively
e63fdcd6 389void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
7c0ea335 390{
e63fdcd6 391 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
7c0ea335
VZ
392 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
393 while (node)
63fec618 394 {
7c0ea335
VZ
395 wxMenuItem* item = node->GetData();
396 if ( !item->IsSeparator() )
397 {
398 wxWindowID id = item->GetId();
399 wxUpdateUIEvent event(id);
400 event.SetEventObject( this );
401
402 if (evtHandler->ProcessEvent(event))
403 {
404 if (event.GetSetText())
405 menu->SetLabel(id, event.GetText());
406 if (event.GetSetChecked())
407 menu->Check(id, event.GetChecked());
408 if (event.GetSetEnabled())
409 menu->Enable(id, event.GetEnabled());
410 }
411
412 if (item->GetSubMenu())
413 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
414 }
415 node = node->GetNext();
63fec618 416 }
63fec618 417}
1e6feb95 418
6522713c
VZ
419void wxFrameBase::DetachMenuBar()
420{
421 if ( m_frameMenuBar )
422 {
423 m_frameMenuBar->Detach();
424 m_frameMenuBar = NULL;
425 }
426}
427
428void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
429{
430 if ( menubar )
431 {
6522713c 432 menubar->Attach((wxFrame *)this);
3dbe38c3 433 m_frameMenuBar = menubar;
6522713c
VZ
434 }
435}
436
437void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
438{
439 if ( menubar == GetMenuBar() )
440 {
441 // nothing to do
442 return;
443 }
444
445 DetachMenuBar();
446
447 AttachMenuBar(menubar);
448}
449
1e6feb95 450#endif // wxUSE_MENUS