]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Update to zlib 1.2.3
[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
65571936 8// Licence: wxWindows licence
63fec618
VZ
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
a9928e9d 158#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
159 wxToolBar *toolbar = GetToolBar();
160 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
161 {
162 int w, h;
d4597e13 163 toolbar->GetSize(&w, &h);
1c4f8f8d 164
d4597e13 165 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
166 {
167 pt.x += w;
168 }
169 else
170 {
171 pt.y += h;
172 }
173 }
174#endif // wxUSE_TOOLBAR
175
176 return pt;
177}
178
2b022169
RD
179
180void wxFrameBase::SendSizeEvent()
181{
182 wxSizeEvent event( GetSize(), GetId() );
183 event.SetEventObject( this );
184 GetEventHandler()->AddPendingEvent( event );
185}
186
187
7c0ea335
VZ
188// ----------------------------------------------------------------------------
189// misc
190// ----------------------------------------------------------------------------
191
7c0ea335
VZ
192bool wxFrameBase::ProcessCommand(int id)
193{
1e6feb95 194#if wxUSE_MENUS
7c0ea335
VZ
195 wxMenuBar *bar = GetMenuBar();
196 if ( !bar )
d1b20379 197 return false;
7c0ea335 198
3ca6a5f0
BP
199 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
200 commandEvent.SetEventObject(this);
201
7c0ea335 202 wxMenuItem *item = bar->FindItem(id);
84f7908b 203 if (item)
7c0ea335 204 {
84f7908b 205 if (!item->IsEnabled())
d1b20379 206 return true;
d4597e13 207
26c36d75
WS
208 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
209 return true;
210
84f7908b
RR
211 if (item->IsCheckable())
212 {
213 item->Toggle();
0472ece7 214
84f7908b
RR
215 // use the new value
216 commandEvent.SetInt(item->IsChecked());
217 }
3ca6a5f0 218 }
7c0ea335 219
a01fe3d6 220 GetEventHandler()->ProcessEvent(commandEvent);
d1b20379 221 return true;
1e6feb95 222#else // !wxUSE_MENUS
d1b20379 223 return false;
1e6feb95 224#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
225}
226
e39af974
JS
227// Do the UI update processing for this window. This is
228// provided for the application to call if it wants to
229// force a UI update, particularly for the menus and toolbar.
230void wxFrameBase::UpdateWindowUI(long flags)
231{
232 wxWindowBase::UpdateWindowUI(flags);
a62848fd 233
e39af974
JS
234#if wxUSE_TOOLBAR
235 if (GetToolBar())
236 GetToolBar()->UpdateWindowUI(flags);
237#endif
238
239#if wxUSE_MENUS
240 if (GetMenuBar())
241 {
242 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
243 {
244 // If coming from an idle event, we only
245 // want to update the menus if we're
246 // in the wxUSE_IDLEMENUUPDATES configuration:
247 // so if we're not, do nothing
248 }
249 else
250 DoMenuUpdates();
251 }
96ac065f 252#endif // wxUSE_MENUS
e39af974
JS
253}
254
7c0ea335 255// ----------------------------------------------------------------------------
96ac065f 256// event handlers for status bar updates from menus
7c0ea335
VZ
257// ----------------------------------------------------------------------------
258
96ac065f
VZ
259#if wxUSE_MENUS && wxUSE_STATUSBAR
260
7c0ea335
VZ
261void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
262{
263#if wxUSE_STATUSBAR
f6bcfd97 264 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
265#endif // wxUSE_STATUSBAR
266}
267
d1b20379 268#if !wxUSE_IDLEMENUUPDATES
96ac065f 269void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
d1b20379
DS
270#else
271void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
272#endif
96ac065f
VZ
273{
274#if !wxUSE_IDLEMENUUPDATES
275 DoMenuUpdates(event.GetMenu());
276#endif // !wxUSE_IDLEMENUUPDATES
277}
278
279void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
280{
281 // do we have real status text to restore?
adbc621d 282 if ( !m_oldStatusText.empty() )
96ac065f
VZ
283 {
284 if ( m_statusBarPane >= 0 )
285 {
286 wxStatusBar *statbar = GetStatusBar();
287 if ( statbar )
288 statbar->SetStatusText(m_oldStatusText, m_statusBarPane);
289 }
290
291 m_oldStatusText.clear();
292 }
293}
294
295#endif // wxUSE_MENUS && wxUSE_STATUSBAR
296
e39af974
JS
297// Implement internal behaviour (menu updating on some platforms)
298void wxFrameBase::OnInternalIdle()
6522713c 299{
e2b6d07d 300 wxTopLevelWindow::OnInternalIdle();
a62848fd 301
0b30bb0b 302#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 303 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
304 DoMenuUpdates();
305#endif
306}
307
7c0ea335
VZ
308// ----------------------------------------------------------------------------
309// status bar stuff
310// ----------------------------------------------------------------------------
311
312#if wxUSE_STATUSBAR
313
314wxStatusBar* wxFrameBase::CreateStatusBar(int number,
315 long style,
316 wxWindowID id,
317 const wxString& name)
318{
319 // the main status bar can only be created once (or else it should be
320 // deleted before calling CreateStatusBar() again)
321 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
322 wxT("recreating status bar in wxFrame") );
323
a4f01f05 324 SetStatusBar(OnCreateStatusBar(number, style, id, name));
7c0ea335
VZ
325
326 return m_frameStatusBar;
327}
328
329wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
330 long style,
331 wxWindowID id,
332 const wxString& name)
333{
ed791986 334 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 335
7c0ea335
VZ
336 statusBar->SetFieldsCount(number);
337
338 return statusBar;
339}
340
341void wxFrameBase::SetStatusText(const wxString& text, int number)
342{
7c0ea335
VZ
343 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
344
345 m_frameStatusBar->SetStatusText(text, number);
346}
347
348void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
349{
7c0ea335
VZ
350 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
351
352 m_frameStatusBar->SetStatusWidths(n, widths_field);
353
354 PositionStatusBar();
355}
356
1f361cdd 357void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 358{
1f361cdd
MB
359 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
360
361 m_frameStatusBar->PushStatusText(text, number);
362}
f6bcfd97 363
1f361cdd
MB
364void wxFrameBase::PopStatusText(int number)
365{
366 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
367
368 m_frameStatusBar->PopStatusText(number);
369}
370
1f361cdd
MB
371bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
372{
373#if wxUSE_MENUS
f6bcfd97
BP
374 // if no help string found, we will clear the status bar text
375 wxString helpString;
1f361cdd 376 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 377
1f361cdd 378 if ( show )
f6bcfd97
BP
379 {
380 wxMenuBar *menuBar = GetMenuBar();
381 if ( menuBar )
382 {
383 // it's ok if we don't find the item because it might belong
384 // to the popup menu
385 wxMenuItem *item = menuBar->FindItem(menuId);
386 if ( item )
387 helpString = item->GetHelp();
388 }
389 }
390
1f361cdd 391 DoGiveHelp(helpString, show);
f6bcfd97 392
1729813a 393 return !helpString.empty();
3379ed37 394#else // !wxUSE_MENUS
d1b20379 395 return false;
3379ed37 396#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
397}
398
a4f01f05
VZ
399void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
400{
401 bool hadBar = m_frameStatusBar != NULL;
402 m_frameStatusBar = statBar;
403
404 if ( (m_frameStatusBar != NULL) != hadBar )
405 {
406 PositionStatusBar();
407
408 DoLayout();
409 }
410}
411
7c0ea335
VZ
412#endif // wxUSE_STATUSBAR
413
c60a36d5
VZ
414void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
415{
416#if wxUSE_STATUSBAR
96ac065f
VZ
417 if ( m_statusBarPane < 0 )
418 {
419 // status bar messages disabled
420 return;
421 }
422
423 wxStatusBar *statbar = GetStatusBar();
424 if ( !statbar )
425 return;
426
427 wxString help;
428 if ( show )
429 help = text;
430
431 // remember the old status bar text if this is the first time we're called
432 // since the menu has been opened as we're going to overwrite it in our
433 // DoGiveHelp() and we want to restore it when the menu is closed
434 //
435 // note that it would be logical to do this in OnMenuOpen() but under MSW
436 // we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely enough, and
437 // so this doesn't work and instead we use the ugly trick with using
438 // special m_oldStatusText value as "menu opened" (but it is arguably
439 // better than adding yet another member variable to wxFrame on all
440 // platforms)
441 if ( m_oldStatusText.empty() )
442 {
443 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
444 if ( m_oldStatusText.empty() )
445 {
446 // use special value to prevent us from doing this the next time
447 m_oldStatusText += _T('\0');
448 }
449 }
c60a36d5 450
96ac065f 451 statbar->SetStatusText(help, m_statusBarPane);
f428e6c5
WS
452#else
453 wxUnusedVar(text);
454 wxUnusedVar(show);
c60a36d5
VZ
455#endif // wxUSE_STATUSBAR
456}
457
458
7c0ea335
VZ
459// ----------------------------------------------------------------------------
460// toolbar stuff
461// ----------------------------------------------------------------------------
462
463#if wxUSE_TOOLBAR
464
465wxToolBar* wxFrameBase::CreateToolBar(long style,
466 wxWindowID id,
467 const wxString& name)
468{
469 // the main toolbar can't be recreated (unless it was explicitly deeleted
470 // before)
471 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
472 wxT("recreating toolbar in wxFrame") );
473
f9dae779
VZ
474 if ( style == -1 )
475 {
476 // use default style
477 //
478 // NB: we don't specify the default value in the method declaration
479 // because
480 // a) this allows us to have different defaults for different
481 // platforms (even if we don't have them right now)
482 // b) we don't need to include wx/toolbar.h in the header then
483 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
484 }
485
a4f01f05 486 SetToolBar(OnCreateToolBar(style, id, name));
7c0ea335
VZ
487
488 return m_frameToolBar;
489}
490
491wxToolBar* wxFrameBase::OnCreateToolBar(long style,
492 wxWindowID id,
493 const wxString& name)
494{
a9102b36
JS
495#if defined(__WXWINCE__) && defined(__POCKETPC__)
496 return new wxToolMenuBar(this, id,
497 wxDefaultPosition, wxDefaultSize,
498 style, name);
499#else
7c0ea335
VZ
500 return new wxToolBar(this, id,
501 wxDefaultPosition, wxDefaultSize,
502 style, name);
a9102b36 503#endif
7c0ea335
VZ
504}
505
a4f01f05
VZ
506void wxFrameBase::SetToolBar(wxToolBar *toolbar)
507{
508 bool hadBar = m_frameToolBar != NULL;
509 m_frameToolBar = toolbar;
510
511 if ( (m_frameToolBar != NULL) != hadBar )
512 {
513 PositionToolBar();
514
515 DoLayout();
516 }
517}
518
7c0ea335
VZ
519#endif // wxUSE_TOOLBAR
520
521// ----------------------------------------------------------------------------
6522713c 522// menus
7c0ea335
VZ
523// ----------------------------------------------------------------------------
524
1e6feb95
VZ
525#if wxUSE_MENUS
526
63fec618 527// update all menus
92f1a59c 528void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 529{
92f1a59c 530 wxEvtHandler* source = GetEventHandler();
54517652 531 wxMenuBar* bar = GetMenuBar();
e702ff0f 532
92f1a59c
JS
533 if (menu)
534 menu->UpdateUI(source);
535 else if ( bar != NULL )
54517652
RR
536 {
537 int nCount = bar->GetMenuCount();
538 for (int n = 0; n < nCount; n++)
18afa2ac 539 bar->GetMenu(n)->UpdateUI(source);
63fec618 540 }
63fec618 541}
1e6feb95 542
6522713c
VZ
543void wxFrameBase::DetachMenuBar()
544{
545 if ( m_frameMenuBar )
546 {
547 m_frameMenuBar->Detach();
548 m_frameMenuBar = NULL;
549 }
550}
551
552void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
553{
554 if ( menubar )
555 {
6522713c 556 menubar->Attach((wxFrame *)this);
3dbe38c3 557 m_frameMenuBar = menubar;
6522713c
VZ
558 }
559}
560
561void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
562{
563 if ( menubar == GetMenuBar() )
564 {
565 // nothing to do
566 return;
567 }
568
569 DetachMenuBar();
570
a96b4743 571 this->AttachMenuBar(menubar);
6522713c
VZ
572}
573
1e6feb95 574#endif // wxUSE_MENUS
1729813a
WS
575
576#if WXWIN_COMPATIBILITY_2_2
577
578bool wxFrameBase::Command(int winid)
579{
580 return ProcessCommand(winid);
581}
582
583#endif // WXWIN_COMPATIBILITY_2_2