]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Fix tracking rectangles in 64-bit build by remembering the tag as the 64-bit integer...
[wxWidgets.git] / src / common / framecmn.cpp
CommitLineData
63fec618 1/////////////////////////////////////////////////////////////////////////////
76b49cf4 2// Name: src/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
f701d7ab
JS
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
7c0ea335 23 #pragma hdrstop
f701d7ab
JS
24#endif
25
76b49cf4
WS
26#include "wx/frame.h"
27
1e6feb95 28#ifndef WX_PRECOMP
1e6feb95
VZ
29 #include "wx/menu.h"
30 #include "wx/menuitem.h"
31 #include "wx/dcclient.h"
4e3e485b 32 #include "wx/toolbar.h"
7c0ea335 33 #include "wx/statusbr.h"
3304646d 34#endif // WX_PRECOMP
7c0ea335
VZ
35
36// ----------------------------------------------------------------------------
37// event table
38// ----------------------------------------------------------------------------
39
96ac065f
VZ
40#if wxUSE_MENUS && wxUSE_STATUSBAR
41
7d9f12f3 42BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
0b30bb0b 43 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
96ac065f
VZ
44 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
45
7c0ea335 46 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
47END_EVENT_TABLE()
48
96ac065f
VZ
49#endif // wxUSE_MENUS && wxUSE_STATUSBAR
50
7c0ea335
VZ
51// ============================================================================
52// implementation
53// ============================================================================
54
55// ----------------------------------------------------------------------------
56// construction/destruction
57// ----------------------------------------------------------------------------
58
59wxFrameBase::wxFrameBase()
60{
1e6feb95 61#if wxUSE_MENUS
7c0ea335 62 m_frameMenuBar = NULL;
1e6feb95 63#endif // wxUSE_MENUS
7c0ea335
VZ
64
65#if wxUSE_TOOLBAR
66 m_frameToolBar = NULL;
67#endif // wxUSE_TOOLBAR
68
69#if wxUSE_STATUSBAR
70 m_frameStatusBar = NULL;
71#endif // wxUSE_STATUSBAR
1f361cdd
MB
72
73 m_statusBarPane = 0;
7c0ea335
VZ
74}
75
799ea011
GD
76wxFrameBase::~wxFrameBase()
77{
78 // this destructor is required for Darwin
79}
80
7c0ea335
VZ
81wxFrame *wxFrameBase::New(wxWindow *parent,
82 wxWindowID id,
83 const wxString& title,
84 const wxPoint& pos,
85 const wxSize& size,
86 long style,
87 const wxString& name)
88{
89 return new wxFrame(parent, id, title, pos, size, style, name);
90}
91
92void wxFrameBase::DeleteAllBars()
93{
1e6feb95 94#if wxUSE_MENUS
7c0ea335
VZ
95 if ( m_frameMenuBar )
96 {
97 delete m_frameMenuBar;
98 m_frameMenuBar = (wxMenuBar *) NULL;
99 }
1e6feb95 100#endif // wxUSE_MENUS
7c0ea335
VZ
101
102#if wxUSE_STATUSBAR
103 if ( m_frameStatusBar )
104 {
105 delete m_frameStatusBar;
106 m_frameStatusBar = (wxStatusBar *) NULL;
107 }
108#endif // wxUSE_STATUSBAR
109
110#if wxUSE_TOOLBAR
111 if ( m_frameToolBar )
112 {
113 delete m_frameToolBar;
114 m_frameToolBar = (wxToolBar *) NULL;
115 }
116#endif // wxUSE_TOOLBAR
117}
118
1e6feb95
VZ
119bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
120{
121#if wxUSE_MENUS
122 if ( win == GetMenuBar() )
d1b20379 123 return true;
1e6feb95
VZ
124#endif // wxUSE_MENUS
125
126#if wxUSE_STATUSBAR
127 if ( win == GetStatusBar() )
d1b20379 128 return true;
1e6feb95
VZ
129#endif // wxUSE_STATUSBAR
130
131#if wxUSE_TOOLBAR
132 if ( win == GetToolBar() )
d1b20379 133 return true;
1e6feb95
VZ
134#endif // wxUSE_TOOLBAR
135
8d22935d
VZ
136 wxUnusedVar(win);
137
d1b20379 138 return false;
1e6feb95
VZ
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
a9928e9d 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
2b022169
RD
173
174void wxFrameBase::SendSizeEvent()
175{
176 wxSizeEvent event( GetSize(), GetId() );
177 event.SetEventObject( this );
178 GetEventHandler()->AddPendingEvent( event );
179}
180
181
7c0ea335
VZ
182// ----------------------------------------------------------------------------
183// misc
184// ----------------------------------------------------------------------------
185
7c0ea335
VZ
186bool wxFrameBase::ProcessCommand(int id)
187{
1e6feb95 188#if wxUSE_MENUS
7c0ea335
VZ
189 wxMenuBar *bar = GetMenuBar();
190 if ( !bar )
d1b20379 191 return false;
7c0ea335 192
3ca6a5f0
BP
193 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
194 commandEvent.SetEventObject(this);
195
7c0ea335 196 wxMenuItem *item = bar->FindItem(id);
84f7908b 197 if (item)
7c0ea335 198 {
84f7908b 199 if (!item->IsEnabled())
d1b20379 200 return true;
d4597e13 201
26c36d75
WS
202 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
203 return true;
204
84f7908b
RR
205 if (item->IsCheckable())
206 {
207 item->Toggle();
0472ece7 208
84f7908b
RR
209 // use the new value
210 commandEvent.SetInt(item->IsChecked());
211 }
3ca6a5f0 212 }
7c0ea335 213
e047f594 214 return HandleWindowEvent(commandEvent);
1e6feb95 215#else // !wxUSE_MENUS
8d22935d
VZ
216 wxUnusedVar(id);
217
d1b20379 218 return false;
1e6feb95 219#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
220}
221
e39af974
JS
222// Do the UI update processing for this window. This is
223// provided for the application to call if it wants to
224// force a UI update, particularly for the menus and toolbar.
225void wxFrameBase::UpdateWindowUI(long flags)
226{
227 wxWindowBase::UpdateWindowUI(flags);
a62848fd 228
e39af974
JS
229#if wxUSE_TOOLBAR
230 if (GetToolBar())
231 GetToolBar()->UpdateWindowUI(flags);
232#endif
233
234#if wxUSE_MENUS
235 if (GetMenuBar())
236 {
237 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
238 {
239 // If coming from an idle event, we only
240 // want to update the menus if we're
241 // in the wxUSE_IDLEMENUUPDATES configuration:
242 // so if we're not, do nothing
243 }
244 else
245 DoMenuUpdates();
246 }
96ac065f 247#endif // wxUSE_MENUS
e39af974
JS
248}
249
7c0ea335 250// ----------------------------------------------------------------------------
96ac065f 251// event handlers for status bar updates from menus
7c0ea335
VZ
252// ----------------------------------------------------------------------------
253
96ac065f
VZ
254#if wxUSE_MENUS && wxUSE_STATUSBAR
255
7c0ea335
VZ
256void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
257{
258#if wxUSE_STATUSBAR
722ed5be 259 (void)ShowMenuHelp(event.GetMenuId());
7c0ea335
VZ
260#endif // wxUSE_STATUSBAR
261}
262
d1b20379 263#if !wxUSE_IDLEMENUUPDATES
96ac065f 264void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
d1b20379
DS
265#else
266void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
267#endif
96ac065f
VZ
268{
269#if !wxUSE_IDLEMENUUPDATES
270 DoMenuUpdates(event.GetMenu());
271#endif // !wxUSE_IDLEMENUUPDATES
272}
273
274void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
275{
6d99eb3e 276 DoGiveHelp(wxEmptyString, false);
96ac065f
VZ
277}
278
279#endif // wxUSE_MENUS && wxUSE_STATUSBAR
280
e39af974
JS
281// Implement internal behaviour (menu updating on some platforms)
282void wxFrameBase::OnInternalIdle()
6522713c 283{
e2b6d07d 284 wxTopLevelWindow::OnInternalIdle();
a62848fd 285
0b30bb0b 286#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 287 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
288 DoMenuUpdates();
289#endif
290}
291
7c0ea335
VZ
292// ----------------------------------------------------------------------------
293// status bar stuff
294// ----------------------------------------------------------------------------
295
296#if wxUSE_STATUSBAR
297
298wxStatusBar* wxFrameBase::CreateStatusBar(int number,
299 long style,
300 wxWindowID id,
301 const wxString& name)
302{
303 // the main status bar can only be created once (or else it should be
304 // deleted before calling CreateStatusBar() again)
305 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
306 wxT("recreating status bar in wxFrame") );
307
a4f01f05 308 SetStatusBar(OnCreateStatusBar(number, style, id, name));
7c0ea335
VZ
309
310 return m_frameStatusBar;
311}
312
313wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
314 long style,
315 wxWindowID id,
316 const wxString& name)
317{
ed791986 318 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 319
7c0ea335
VZ
320 statusBar->SetFieldsCount(number);
321
322 return statusBar;
323}
324
325void wxFrameBase::SetStatusText(const wxString& text, int number)
326{
7c0ea335
VZ
327 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
328
329 m_frameStatusBar->SetStatusText(text, number);
330}
331
332void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
333{
7c0ea335
VZ
334 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
335
336 m_frameStatusBar->SetStatusWidths(n, widths_field);
337
338 PositionStatusBar();
339}
340
1f361cdd 341void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 342{
1f361cdd
MB
343 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
344
345 m_frameStatusBar->PushStatusText(text, number);
346}
f6bcfd97 347
1f361cdd
MB
348void wxFrameBase::PopStatusText(int number)
349{
350 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
351
352 m_frameStatusBar->PopStatusText(number);
353}
354
722ed5be 355bool wxFrameBase::ShowMenuHelp(int menuId)
1f361cdd
MB
356{
357#if wxUSE_MENUS
f6bcfd97
BP
358 // if no help string found, we will clear the status bar text
359 wxString helpString;
fa36fe36 360 if ( menuId != wxID_SEPARATOR && menuId != -3 /* wxID_TITLE */ )
f6bcfd97 361 {
10816efb
VZ
362 const wxMenuItem * const item = FindItemInMenuBar(menuId);
363 if ( item )
364 helpString = item->GetHelp();
365
366 // notice that it's ok if we don't find the item because it might
367 // belong to the popup menu, so don't assert here
f6bcfd97
BP
368 }
369
6d99eb3e 370 DoGiveHelp(helpString, true);
f6bcfd97 371
1729813a 372 return !helpString.empty();
3379ed37 373#else // !wxUSE_MENUS
d1b20379 374 return false;
3379ed37 375#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
376}
377
a4f01f05
VZ
378void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
379{
380 bool hadBar = m_frameStatusBar != NULL;
381 m_frameStatusBar = statBar;
382
383 if ( (m_frameStatusBar != NULL) != hadBar )
384 {
385 PositionStatusBar();
386
387 DoLayout();
388 }
389}
390
7c0ea335
VZ
391#endif // wxUSE_STATUSBAR
392
f257ac87 393#if wxUSE_MENUS || wxUSE_TOOLBAR
6d99eb3e 394void wxFrameBase::DoGiveHelp(const wxString& help, bool show)
c60a36d5
VZ
395{
396#if wxUSE_STATUSBAR
96ac065f
VZ
397 if ( m_statusBarPane < 0 )
398 {
399 // status bar messages disabled
400 return;
401 }
402
403 wxStatusBar *statbar = GetStatusBar();
404 if ( !statbar )
405 return;
406
6d99eb3e
VZ
407 wxString text;
408 if ( show )
4cbc928a 409 {
6d99eb3e
VZ
410 // remember the old status bar text if this is the first time we're
411 // called since the menu has been opened as we're going to overwrite it
412 // in our DoGiveHelp() and we want to restore it when the menu is
413 // closed
414 //
415 // note that it would be logical to do this in OnMenuOpen() but under
416 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
417 // enough, and so this doesn't work and instead we use the ugly trick
418 // with using special m_oldStatusText value as "menu opened" (but it is
419 // arguably better than adding yet another member variable to wxFrame
420 // on all platforms)
96ac065f
VZ
421 if ( m_oldStatusText.empty() )
422 {
6d99eb3e
VZ
423 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
424 if ( m_oldStatusText.empty() )
425 {
426 // use special value to prevent us from doing this the next time
427 m_oldStatusText += _T('\0');
428 }
96ac065f 429 }
6d99eb3e
VZ
430
431 text = help;
432 }
433 else // hide help, restore the original text
434 {
435 text = m_oldStatusText;
436 m_oldStatusText.clear();
96ac065f 437 }
c60a36d5 438
6d99eb3e 439 statbar->SetStatusText(text, m_statusBarPane);
f428e6c5 440#else
3b257996 441 wxUnusedVar(help);
f428e6c5 442 wxUnusedVar(show);
c60a36d5
VZ
443#endif // wxUSE_STATUSBAR
444}
f257ac87 445#endif // wxUSE_MENUS || wxUSE_TOOLBAR
c60a36d5
VZ
446
447
7c0ea335
VZ
448// ----------------------------------------------------------------------------
449// toolbar stuff
450// ----------------------------------------------------------------------------
451
452#if wxUSE_TOOLBAR
453
454wxToolBar* wxFrameBase::CreateToolBar(long style,
455 wxWindowID id,
456 const wxString& name)
457{
6a17b868 458 // the main toolbar can't be recreated (unless it was explicitly deleted
7c0ea335
VZ
459 // before)
460 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
461 wxT("recreating toolbar in wxFrame") );
462
f9dae779
VZ
463 if ( style == -1 )
464 {
465 // use default style
466 //
467 // NB: we don't specify the default value in the method declaration
468 // because
469 // a) this allows us to have different defaults for different
470 // platforms (even if we don't have them right now)
471 // b) we don't need to include wx/toolbar.h in the header then
472 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
473 }
474
a4f01f05 475 SetToolBar(OnCreateToolBar(style, id, name));
7c0ea335
VZ
476
477 return m_frameToolBar;
478}
479
480wxToolBar* wxFrameBase::OnCreateToolBar(long style,
481 wxWindowID id,
482 const wxString& name)
483{
a9102b36
JS
484#if defined(__WXWINCE__) && defined(__POCKETPC__)
485 return new wxToolMenuBar(this, id,
486 wxDefaultPosition, wxDefaultSize,
487 style, name);
488#else
7c0ea335
VZ
489 return new wxToolBar(this, id,
490 wxDefaultPosition, wxDefaultSize,
491 style, name);
a9102b36 492#endif
7c0ea335
VZ
493}
494
a4f01f05
VZ
495void wxFrameBase::SetToolBar(wxToolBar *toolbar)
496{
497 bool hadBar = m_frameToolBar != NULL;
498 m_frameToolBar = toolbar;
499
500 if ( (m_frameToolBar != NULL) != hadBar )
501 {
502 PositionToolBar();
503
504 DoLayout();
505 }
506}
507
7c0ea335
VZ
508#endif // wxUSE_TOOLBAR
509
510// ----------------------------------------------------------------------------
6522713c 511// menus
7c0ea335
VZ
512// ----------------------------------------------------------------------------
513
1e6feb95
VZ
514#if wxUSE_MENUS
515
63fec618 516// update all menus
92f1a59c 517void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 518{
92f1a59c 519 if (menu)
4d538595
DS
520 {
521 wxEvtHandler* source = GetEventHandler();
92f1a59c 522 menu->UpdateUI(source);
4d538595
DS
523 }
524 else
54517652 525 {
4d538595
DS
526 wxMenuBar* bar = GetMenuBar();
527 if (bar != NULL)
528 bar->UpdateMenus();
63fec618 529 }
63fec618 530}
1e6feb95 531
6522713c
VZ
532void wxFrameBase::DetachMenuBar()
533{
534 if ( m_frameMenuBar )
535 {
536 m_frameMenuBar->Detach();
537 m_frameMenuBar = NULL;
538 }
539}
540
541void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
542{
543 if ( menubar )
544 {
6522713c 545 menubar->Attach((wxFrame *)this);
3dbe38c3 546 m_frameMenuBar = menubar;
6522713c
VZ
547 }
548}
549
550void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
551{
552 if ( menubar == GetMenuBar() )
553 {
554 // nothing to do
555 return;
556 }
557
558 DetachMenuBar();
559
a96b4743 560 this->AttachMenuBar(menubar);
6522713c
VZ
561}
562
10816efb
VZ
563const wxMenuItem *wxFrameBase::FindItemInMenuBar(int menuId) const
564{
565 const wxMenuBar * const menuBar = GetMenuBar();
566
567 return menuBar ? menuBar->FindItem(menuId) : NULL;
568}
569
1e6feb95 570#endif // wxUSE_MENUS