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