]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
open registry keys in read-only mode as we might not have the rights for full access...
[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
14f355c2 19#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7c0ea335
VZ
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
96ac065f
VZ
48#if wxUSE_MENUS && wxUSE_STATUSBAR
49
7d9f12f3 50BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
0b30bb0b 51 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
96ac065f
VZ
52 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
53
7c0ea335 54 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
55END_EVENT_TABLE()
56
96ac065f
VZ
57#endif // wxUSE_MENUS && wxUSE_STATUSBAR
58
7c0ea335
VZ
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// construction/destruction
65// ----------------------------------------------------------------------------
66
67wxFrameBase::wxFrameBase()
68{
1e6feb95 69#if wxUSE_MENUS
7c0ea335 70 m_frameMenuBar = NULL;
1e6feb95 71#endif // wxUSE_MENUS
7c0ea335
VZ
72
73#if wxUSE_TOOLBAR
74 m_frameToolBar = NULL;
75#endif // wxUSE_TOOLBAR
76
77#if wxUSE_STATUSBAR
78 m_frameStatusBar = NULL;
79#endif // wxUSE_STATUSBAR
1f361cdd
MB
80
81 m_statusBarPane = 0;
7c0ea335
VZ
82}
83
799ea011
GD
84wxFrameBase::~wxFrameBase()
85{
86 // this destructor is required for Darwin
87}
88
7c0ea335
VZ
89wxFrame *wxFrameBase::New(wxWindow *parent,
90 wxWindowID id,
91 const wxString& title,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96{
97 return new wxFrame(parent, id, title, pos, size, style, name);
98}
99
100void wxFrameBase::DeleteAllBars()
101{
1e6feb95 102#if wxUSE_MENUS
7c0ea335
VZ
103 if ( m_frameMenuBar )
104 {
105 delete m_frameMenuBar;
106 m_frameMenuBar = (wxMenuBar *) NULL;
107 }
1e6feb95 108#endif // wxUSE_MENUS
7c0ea335
VZ
109
110#if wxUSE_STATUSBAR
111 if ( m_frameStatusBar )
112 {
113 delete m_frameStatusBar;
114 m_frameStatusBar = (wxStatusBar *) NULL;
115 }
116#endif // wxUSE_STATUSBAR
117
118#if wxUSE_TOOLBAR
119 if ( m_frameToolBar )
120 {
121 delete m_frameToolBar;
122 m_frameToolBar = (wxToolBar *) NULL;
123 }
124#endif // wxUSE_TOOLBAR
125}
126
1e6feb95
VZ
127bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
128{
129#if wxUSE_MENUS
130 if ( win == GetMenuBar() )
d1b20379 131 return true;
1e6feb95
VZ
132#endif // wxUSE_MENUS
133
134#if wxUSE_STATUSBAR
135 if ( win == GetStatusBar() )
d1b20379 136 return true;
1e6feb95
VZ
137#endif // wxUSE_STATUSBAR
138
139#if wxUSE_TOOLBAR
140 if ( win == GetToolBar() )
d1b20379 141 return true;
1e6feb95
VZ
142#endif // wxUSE_TOOLBAR
143
d1b20379 144 return false;
1e6feb95
VZ
145}
146
1c4f8f8d
VZ
147// ----------------------------------------------------------------------------
148// wxFrame size management: we exclude the areas taken by menu/status/toolbars
149// from the client area, so the client area is what's really available for the
150// frame contents
151// ----------------------------------------------------------------------------
152
153// get the origin of the client area in the client coordinates
154wxPoint wxFrameBase::GetClientAreaOrigin() const
155{
7d9f12f3 156 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 157
7ef67c25 158#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) && \
960b193e 159 (!defined(__WXWINCE__) || (_WIN32_WCE >= 400 && !defined(WIN32_PLATFORM_PSPC) && !defined(WIN32_PLATFORM_WFSP)))
d4597e13
VZ
160 wxToolBar *toolbar = GetToolBar();
161 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
162 {
163 int w, h;
d4597e13 164 toolbar->GetSize(&w, &h);
1c4f8f8d 165
d4597e13 166 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
167 {
168 pt.x += w;
169 }
170 else
171 {
172 pt.y += h;
173 }
174 }
175#endif // wxUSE_TOOLBAR
176
177 return pt;
178}
179
7c0ea335
VZ
180// ----------------------------------------------------------------------------
181// misc
182// ----------------------------------------------------------------------------
183
7c0ea335
VZ
184bool wxFrameBase::ProcessCommand(int id)
185{
1e6feb95 186#if wxUSE_MENUS
7c0ea335
VZ
187 wxMenuBar *bar = GetMenuBar();
188 if ( !bar )
d1b20379 189 return false;
7c0ea335 190
3ca6a5f0
BP
191 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
192 commandEvent.SetEventObject(this);
193
7c0ea335 194 wxMenuItem *item = bar->FindItem(id);
84f7908b 195 if (item)
7c0ea335 196 {
84f7908b 197 if (!item->IsEnabled())
d1b20379 198 return true;
d4597e13 199
84f7908b
RR
200 if (item->IsCheckable())
201 {
202 item->Toggle();
0472ece7 203
84f7908b
RR
204 // use the new value
205 commandEvent.SetInt(item->IsChecked());
206 }
3ca6a5f0 207 }
7c0ea335 208
a01fe3d6 209 GetEventHandler()->ProcessEvent(commandEvent);
d1b20379 210 return true;
1e6feb95 211#else // !wxUSE_MENUS
d1b20379 212 return false;
1e6feb95 213#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
214}
215
e39af974
JS
216// Do the UI update processing for this window. This is
217// provided for the application to call if it wants to
218// force a UI update, particularly for the menus and toolbar.
219void wxFrameBase::UpdateWindowUI(long flags)
220{
221 wxWindowBase::UpdateWindowUI(flags);
222
223#if wxUSE_TOOLBAR
224 if (GetToolBar())
225 GetToolBar()->UpdateWindowUI(flags);
226#endif
227
228#if wxUSE_MENUS
229 if (GetMenuBar())
230 {
231 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
232 {
233 // If coming from an idle event, we only
234 // want to update the menus if we're
235 // in the wxUSE_IDLEMENUUPDATES configuration:
236 // so if we're not, do nothing
237 }
238 else
239 DoMenuUpdates();
240 }
96ac065f 241#endif // wxUSE_MENUS
e39af974
JS
242}
243
7c0ea335 244// ----------------------------------------------------------------------------
96ac065f 245// event handlers for status bar updates from menus
7c0ea335
VZ
246// ----------------------------------------------------------------------------
247
96ac065f
VZ
248#if wxUSE_MENUS && wxUSE_STATUSBAR
249
7c0ea335
VZ
250void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
251{
252#if wxUSE_STATUSBAR
f6bcfd97 253 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
254#endif // wxUSE_STATUSBAR
255}
256
d1b20379 257#if !wxUSE_IDLEMENUUPDATES
96ac065f 258void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
d1b20379
DS
259#else
260void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
261#endif
96ac065f
VZ
262{
263#if !wxUSE_IDLEMENUUPDATES
264 DoMenuUpdates(event.GetMenu());
265#endif // !wxUSE_IDLEMENUUPDATES
266}
267
268void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
269{
270 // do we have real status text to restore?
271 if ( m_oldStatusText.length() > 1 || m_oldStatusText[0u] )
272 {
273 if ( m_statusBarPane >= 0 )
274 {
275 wxStatusBar *statbar = GetStatusBar();
276 if ( statbar )
277 statbar->SetStatusText(m_oldStatusText, m_statusBarPane);
278 }
279
280 m_oldStatusText.clear();
281 }
282}
283
284#endif // wxUSE_MENUS && wxUSE_STATUSBAR
285
e39af974
JS
286// Implement internal behaviour (menu updating on some platforms)
287void wxFrameBase::OnInternalIdle()
6522713c 288{
e2b6d07d
JS
289 wxTopLevelWindow::OnInternalIdle();
290
0b30bb0b 291#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 292 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
293 DoMenuUpdates();
294#endif
295}
296
7c0ea335
VZ
297// ----------------------------------------------------------------------------
298// status bar stuff
299// ----------------------------------------------------------------------------
300
301#if wxUSE_STATUSBAR
302
303wxStatusBar* wxFrameBase::CreateStatusBar(int number,
304 long style,
305 wxWindowID id,
306 const wxString& name)
307{
308 // the main status bar can only be created once (or else it should be
309 // deleted before calling CreateStatusBar() again)
310 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
311 wxT("recreating status bar in wxFrame") );
312
313 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
314 if ( m_frameStatusBar )
315 PositionStatusBar();
316
317 return m_frameStatusBar;
318}
319
320wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
321 long style,
322 wxWindowID id,
323 const wxString& name)
324{
ed791986 325 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 326
7c0ea335
VZ
327 statusBar->SetFieldsCount(number);
328
329 return statusBar;
330}
331
332void wxFrameBase::SetStatusText(const wxString& text, int number)
333{
7c0ea335
VZ
334 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
335
336 m_frameStatusBar->SetStatusText(text, number);
337}
338
339void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
340{
7c0ea335
VZ
341 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
342
343 m_frameStatusBar->SetStatusWidths(n, widths_field);
344
345 PositionStatusBar();
346}
347
1f361cdd 348void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 349{
1f361cdd
MB
350 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
351
352 m_frameStatusBar->PushStatusText(text, number);
353}
f6bcfd97 354
1f361cdd
MB
355void wxFrameBase::PopStatusText(int number)
356{
357 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
358
359 m_frameStatusBar->PopStatusText(number);
360}
361
1f361cdd
MB
362bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
363{
364#if wxUSE_MENUS
f6bcfd97
BP
365 // if no help string found, we will clear the status bar text
366 wxString helpString;
1f361cdd 367 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 368
1f361cdd 369 if ( show )
f6bcfd97
BP
370 {
371 wxMenuBar *menuBar = GetMenuBar();
372 if ( menuBar )
373 {
374 // it's ok if we don't find the item because it might belong
375 // to the popup menu
376 wxMenuItem *item = menuBar->FindItem(menuId);
377 if ( item )
378 helpString = item->GetHelp();
379 }
380 }
381
1f361cdd 382 DoGiveHelp(helpString, show);
f6bcfd97
BP
383
384 return !helpString.IsEmpty();
3379ed37 385#else // !wxUSE_MENUS
d1b20379 386 return false;
3379ed37 387#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
388}
389
7c0ea335
VZ
390#endif // wxUSE_STATUSBAR
391
c60a36d5
VZ
392void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
393{
394#if wxUSE_STATUSBAR
96ac065f
VZ
395 if ( m_statusBarPane < 0 )
396 {
397 // status bar messages disabled
398 return;
399 }
400
401 wxStatusBar *statbar = GetStatusBar();
402 if ( !statbar )
403 return;
404
405 wxString help;
406 if ( show )
407 help = text;
408
409 // remember the old status bar text if this is the first time we're called
410 // since the menu has been opened as we're going to overwrite it in our
411 // DoGiveHelp() and we want to restore it when the menu is closed
412 //
413 // note that it would be logical to do this in OnMenuOpen() but under MSW
414 // we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely enough, and
415 // so this doesn't work and instead we use the ugly trick with using
416 // special m_oldStatusText value as "menu opened" (but it is arguably
417 // better than adding yet another member variable to wxFrame on all
418 // platforms)
419 if ( m_oldStatusText.empty() )
420 {
421 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
422 if ( m_oldStatusText.empty() )
423 {
424 // use special value to prevent us from doing this the next time
425 m_oldStatusText += _T('\0');
426 }
427 }
c60a36d5 428
96ac065f 429 statbar->SetStatusText(help, m_statusBarPane);
c60a36d5
VZ
430#endif // wxUSE_STATUSBAR
431}
432
433
7c0ea335
VZ
434// ----------------------------------------------------------------------------
435// toolbar stuff
436// ----------------------------------------------------------------------------
437
438#if wxUSE_TOOLBAR
439
440wxToolBar* wxFrameBase::CreateToolBar(long style,
441 wxWindowID id,
442 const wxString& name)
443{
444 // the main toolbar can't be recreated (unless it was explicitly deeleted
445 // before)
446 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
447 wxT("recreating toolbar in wxFrame") );
448
f9dae779
VZ
449 if ( style == -1 )
450 {
451 // use default style
452 //
453 // NB: we don't specify the default value in the method declaration
454 // because
455 // a) this allows us to have different defaults for different
456 // platforms (even if we don't have them right now)
457 // b) we don't need to include wx/toolbar.h in the header then
458 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
459 }
460
7c0ea335
VZ
461 m_frameToolBar = OnCreateToolBar(style, id, name);
462
463 return m_frameToolBar;
464}
465
466wxToolBar* wxFrameBase::OnCreateToolBar(long style,
467 wxWindowID id,
468 const wxString& name)
469{
470 return new wxToolBar(this, id,
471 wxDefaultPosition, wxDefaultSize,
472 style, name);
473}
474
475#endif // wxUSE_TOOLBAR
476
477// ----------------------------------------------------------------------------
6522713c 478// menus
7c0ea335
VZ
479// ----------------------------------------------------------------------------
480
1e6feb95
VZ
481#if wxUSE_MENUS
482
63fec618 483// update all menus
92f1a59c 484void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 485{
92f1a59c 486 wxEvtHandler* source = GetEventHandler();
54517652 487 wxMenuBar* bar = GetMenuBar();
e702ff0f 488
92f1a59c
JS
489 if (menu)
490 menu->UpdateUI(source);
491 else if ( bar != NULL )
54517652
RR
492 {
493 int nCount = bar->GetMenuCount();
494 for (int n = 0; n < nCount; n++)
18afa2ac 495 bar->GetMenu(n)->UpdateUI(source);
63fec618 496 }
63fec618 497}
1e6feb95 498
6522713c
VZ
499void wxFrameBase::DetachMenuBar()
500{
501 if ( m_frameMenuBar )
502 {
503 m_frameMenuBar->Detach();
504 m_frameMenuBar = NULL;
505 }
506}
507
508void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
509{
510 if ( menubar )
511 {
6522713c 512 menubar->Attach((wxFrame *)this);
3dbe38c3 513 m_frameMenuBar = menubar;
6522713c
VZ
514 }
515}
516
517void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
518{
519 if ( menubar == GetMenuBar() )
520 {
521 // nothing to do
522 return;
523 }
524
525 DetachMenuBar();
526
a96b4743 527 this->AttachMenuBar(menubar);
6522713c
VZ
528}
529
1e6feb95 530#endif // wxUSE_MENUS