]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Added Set/GetQuickBestSize
[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"
32#endif // WX_PRECOMP
7c0ea335
VZ
33
34#if wxUSE_TOOLBAR
35 #include "wx/toolbar.h"
36#endif
37#if wxUSE_STATUSBAR
38 #include "wx/statusbr.h"
39#endif
40
41// ----------------------------------------------------------------------------
42// event table
43// ----------------------------------------------------------------------------
44
96ac065f
VZ
45#if wxUSE_MENUS && wxUSE_STATUSBAR
46
7d9f12f3 47BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
0b30bb0b 48 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
96ac065f
VZ
49 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
50
7c0ea335 51 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
52END_EVENT_TABLE()
53
96ac065f
VZ
54#endif // wxUSE_MENUS && wxUSE_STATUSBAR
55
7c0ea335
VZ
56// ============================================================================
57// implementation
58// ============================================================================
59
60// ----------------------------------------------------------------------------
61// construction/destruction
62// ----------------------------------------------------------------------------
63
64wxFrameBase::wxFrameBase()
65{
1e6feb95 66#if wxUSE_MENUS
7c0ea335 67 m_frameMenuBar = NULL;
1e6feb95 68#endif // wxUSE_MENUS
7c0ea335
VZ
69
70#if wxUSE_TOOLBAR
71 m_frameToolBar = NULL;
72#endif // wxUSE_TOOLBAR
73
74#if wxUSE_STATUSBAR
75 m_frameStatusBar = NULL;
76#endif // wxUSE_STATUSBAR
1f361cdd
MB
77
78 m_statusBarPane = 0;
7c0ea335
VZ
79}
80
799ea011
GD
81wxFrameBase::~wxFrameBase()
82{
83 // this destructor is required for Darwin
84}
85
7c0ea335
VZ
86wxFrame *wxFrameBase::New(wxWindow *parent,
87 wxWindowID id,
88 const wxString& title,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93{
94 return new wxFrame(parent, id, title, pos, size, style, name);
95}
96
97void wxFrameBase::DeleteAllBars()
98{
1e6feb95 99#if wxUSE_MENUS
7c0ea335
VZ
100 if ( m_frameMenuBar )
101 {
102 delete m_frameMenuBar;
103 m_frameMenuBar = (wxMenuBar *) NULL;
104 }
1e6feb95 105#endif // wxUSE_MENUS
7c0ea335
VZ
106
107#if wxUSE_STATUSBAR
108 if ( m_frameStatusBar )
109 {
110 delete m_frameStatusBar;
111 m_frameStatusBar = (wxStatusBar *) NULL;
112 }
113#endif // wxUSE_STATUSBAR
114
115#if wxUSE_TOOLBAR
116 if ( m_frameToolBar )
117 {
118 delete m_frameToolBar;
119 m_frameToolBar = (wxToolBar *) NULL;
120 }
121#endif // wxUSE_TOOLBAR
122}
123
1e6feb95
VZ
124bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
125{
126#if wxUSE_MENUS
127 if ( win == GetMenuBar() )
d1b20379 128 return true;
1e6feb95
VZ
129#endif // wxUSE_MENUS
130
131#if wxUSE_STATUSBAR
132 if ( win == GetStatusBar() )
d1b20379 133 return true;
1e6feb95
VZ
134#endif // wxUSE_STATUSBAR
135
136#if wxUSE_TOOLBAR
137 if ( win == GetToolBar() )
d1b20379 138 return true;
1e6feb95
VZ
139#endif // wxUSE_TOOLBAR
140
d1b20379 141 return false;
1e6feb95
VZ
142}
143
1c4f8f8d
VZ
144// ----------------------------------------------------------------------------
145// wxFrame size management: we exclude the areas taken by menu/status/toolbars
146// from the client area, so the client area is what's really available for the
147// frame contents
148// ----------------------------------------------------------------------------
149
150// get the origin of the client area in the client coordinates
151wxPoint wxFrameBase::GetClientAreaOrigin() const
152{
7d9f12f3 153 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 154
a9928e9d 155#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
156 wxToolBar *toolbar = GetToolBar();
157 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
158 {
159 int w, h;
d4597e13 160 toolbar->GetSize(&w, &h);
1c4f8f8d 161
d4597e13 162 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
163 {
164 pt.x += w;
165 }
166 else
167 {
168 pt.y += h;
169 }
170 }
171#endif // wxUSE_TOOLBAR
172
173 return pt;
174}
175
2b022169
RD
176
177void wxFrameBase::SendSizeEvent()
178{
179 wxSizeEvent event( GetSize(), GetId() );
180 event.SetEventObject( this );
181 GetEventHandler()->AddPendingEvent( event );
182}
183
184
7c0ea335
VZ
185// ----------------------------------------------------------------------------
186// misc
187// ----------------------------------------------------------------------------
188
7c0ea335
VZ
189bool wxFrameBase::ProcessCommand(int id)
190{
1e6feb95 191#if wxUSE_MENUS
7c0ea335
VZ
192 wxMenuBar *bar = GetMenuBar();
193 if ( !bar )
d1b20379 194 return false;
7c0ea335 195
3ca6a5f0
BP
196 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
197 commandEvent.SetEventObject(this);
198
7c0ea335 199 wxMenuItem *item = bar->FindItem(id);
84f7908b 200 if (item)
7c0ea335 201 {
84f7908b 202 if (!item->IsEnabled())
d1b20379 203 return true;
d4597e13 204
26c36d75
WS
205 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
206 return true;
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?
adbc621d 279 if ( !m_oldStatusText.empty() )
96ac065f
VZ
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 389
1729813a 390 return !helpString.empty();
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{
a9102b36
JS
492#if defined(__WXWINCE__) && defined(__POCKETPC__)
493 return new wxToolMenuBar(this, id,
494 wxDefaultPosition, wxDefaultSize,
495 style, name);
496#else
7c0ea335
VZ
497 return new wxToolBar(this, id,
498 wxDefaultPosition, wxDefaultSize,
499 style, name);
a9102b36 500#endif
7c0ea335
VZ
501}
502
a4f01f05
VZ
503void wxFrameBase::SetToolBar(wxToolBar *toolbar)
504{
505 bool hadBar = m_frameToolBar != NULL;
506 m_frameToolBar = toolbar;
507
508 if ( (m_frameToolBar != NULL) != hadBar )
509 {
510 PositionToolBar();
511
512 DoLayout();
513 }
514}
515
7c0ea335
VZ
516#endif // wxUSE_TOOLBAR
517
518// ----------------------------------------------------------------------------
6522713c 519// menus
7c0ea335
VZ
520// ----------------------------------------------------------------------------
521
1e6feb95
VZ
522#if wxUSE_MENUS
523
63fec618 524// update all menus
92f1a59c 525void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 526{
92f1a59c 527 if (menu)
4d538595
DS
528 {
529 wxEvtHandler* source = GetEventHandler();
92f1a59c 530 menu->UpdateUI(source);
4d538595
DS
531 }
532 else
54517652 533 {
4d538595
DS
534 wxMenuBar* bar = GetMenuBar();
535 if (bar != NULL)
536 bar->UpdateMenus();
63fec618 537 }
63fec618 538}
1e6feb95 539
6522713c
VZ
540void wxFrameBase::DetachMenuBar()
541{
542 if ( m_frameMenuBar )
543 {
544 m_frameMenuBar->Detach();
545 m_frameMenuBar = NULL;
546 }
547}
548
549void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
550{
551 if ( menubar )
552 {
6522713c 553 menubar->Attach((wxFrame *)this);
3dbe38c3 554 m_frameMenuBar = menubar;
6522713c
VZ
555 }
556}
557
558void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
559{
560 if ( menubar == GetMenuBar() )
561 {
562 // nothing to do
563 return;
564 }
565
566 DetachMenuBar();
567
a96b4743 568 this->AttachMenuBar(menubar);
6522713c
VZ
569}
570
1e6feb95 571#endif // wxUSE_MENUS