]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Add another test for the insertion point position after SetValue().
[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
5276b0a5 95 wxDELETE(m_frameMenuBar);
1e6feb95 96#endif // wxUSE_MENUS
7c0ea335
VZ
97
98#if wxUSE_STATUSBAR
5276b0a5 99 wxDELETE(m_frameStatusBar);
7c0ea335
VZ
100#endif // wxUSE_STATUSBAR
101
102#if wxUSE_TOOLBAR
5276b0a5 103 wxDELETE(m_frameToolBar);
7c0ea335
VZ
104#endif // wxUSE_TOOLBAR
105}
106
1e6feb95
VZ
107bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
108{
109#if wxUSE_MENUS
110 if ( win == GetMenuBar() )
d1b20379 111 return true;
1e6feb95
VZ
112#endif // wxUSE_MENUS
113
114#if wxUSE_STATUSBAR
115 if ( win == GetStatusBar() )
d1b20379 116 return true;
1e6feb95
VZ
117#endif // wxUSE_STATUSBAR
118
119#if wxUSE_TOOLBAR
120 if ( win == GetToolBar() )
d1b20379 121 return true;
1e6feb95
VZ
122#endif // wxUSE_TOOLBAR
123
8d22935d
VZ
124 wxUnusedVar(win);
125
d1b20379 126 return false;
1e6feb95
VZ
127}
128
1c4f8f8d
VZ
129// ----------------------------------------------------------------------------
130// wxFrame size management: we exclude the areas taken by menu/status/toolbars
131// from the client area, so the client area is what's really available for the
132// frame contents
133// ----------------------------------------------------------------------------
134
135// get the origin of the client area in the client coordinates
136wxPoint wxFrameBase::GetClientAreaOrigin() const
137{
7d9f12f3 138 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 139
a9928e9d 140#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
141 wxToolBar *toolbar = GetToolBar();
142 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
143 {
144 int w, h;
d4597e13 145 toolbar->GetSize(&w, &h);
1c4f8f8d 146
d4597e13 147 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
148 {
149 pt.x += w;
150 }
151 else
152 {
153 pt.y += h;
154 }
155 }
156#endif // wxUSE_TOOLBAR
157
158 return pt;
159}
160
7c0ea335
VZ
161// ----------------------------------------------------------------------------
162// misc
163// ----------------------------------------------------------------------------
164
a6ac49b1
VZ
165#if wxUSE_MENUS
166
7c0ea335
VZ
167bool wxFrameBase::ProcessCommand(int id)
168{
169 wxMenuBar *bar = GetMenuBar();
170 if ( !bar )
d1b20379 171 return false;
7c0ea335 172
a6ac49b1
VZ
173 wxMenuItem *item = bar->FindItem(id);
174 if ( !item )
175 return false;
176
177 return ProcessCommand(item);
178}
179
180bool wxFrameBase::ProcessCommand(wxMenuItem *item)
181{
182 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
3ca6a5f0
BP
183 commandEvent.SetEventObject(this);
184
a6ac49b1
VZ
185 if (!item->IsEnabled())
186 return true;
d4597e13 187
a6ac49b1
VZ
188 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
189 return true;
26c36d75 190
a6ac49b1
VZ
191 if (item->IsCheckable())
192 {
193 item->Toggle();
0472ece7 194
a6ac49b1
VZ
195 // use the new value
196 commandEvent.SetInt(item->IsChecked());
3ca6a5f0 197 }
7c0ea335 198
e047f594 199 return HandleWindowEvent(commandEvent);
7c0ea335
VZ
200}
201
a6ac49b1
VZ
202#endif // wxUSE_MENUS
203
e39af974
JS
204// Do the UI update processing for this window. This is
205// provided for the application to call if it wants to
206// force a UI update, particularly for the menus and toolbar.
207void wxFrameBase::UpdateWindowUI(long flags)
208{
209 wxWindowBase::UpdateWindowUI(flags);
a62848fd 210
e39af974
JS
211#if wxUSE_TOOLBAR
212 if (GetToolBar())
213 GetToolBar()->UpdateWindowUI(flags);
214#endif
215
216#if wxUSE_MENUS
217 if (GetMenuBar())
218 {
a2289551
VZ
219 // If coming from an idle event, we only want to update the menus if
220 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
221 // will be update when the menu is opened later
222#if !wxUSE_IDLEMENUUPDATES
223 if ( !(flags & wxUPDATE_UI_FROMIDLE) )
224#endif // wxUSE_IDLEMENUUPDATES
e39af974
JS
225 DoMenuUpdates();
226 }
96ac065f 227#endif // wxUSE_MENUS
e39af974
JS
228}
229
7c0ea335 230// ----------------------------------------------------------------------------
96ac065f 231// event handlers for status bar updates from menus
7c0ea335
VZ
232// ----------------------------------------------------------------------------
233
96ac065f
VZ
234#if wxUSE_MENUS && wxUSE_STATUSBAR
235
7c0ea335
VZ
236void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
237{
238#if wxUSE_STATUSBAR
722ed5be 239 (void)ShowMenuHelp(event.GetMenuId());
7c0ea335
VZ
240#endif // wxUSE_STATUSBAR
241}
242
96ac065f
VZ
243void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
244{
a2289551
VZ
245#if wxUSE_IDLEMENUUPDATES
246 wxUnusedVar(event);
247#else // !wxUSE_IDLEMENUUPDATES
248 // as we didn't update the menus from idle time, do it now
96ac065f 249 DoMenuUpdates(event.GetMenu());
a2289551 250#endif // wxUSE_IDLEMENUUPDATES/!wxUSE_IDLEMENUUPDATES
96ac065f
VZ
251}
252
253void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
254{
6d99eb3e 255 DoGiveHelp(wxEmptyString, false);
96ac065f
VZ
256}
257
258#endif // wxUSE_MENUS && wxUSE_STATUSBAR
259
e39af974
JS
260// Implement internal behaviour (menu updating on some platforms)
261void wxFrameBase::OnInternalIdle()
6522713c 262{
e2b6d07d 263 wxTopLevelWindow::OnInternalIdle();
a62848fd 264
0b30bb0b 265#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 266 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
267 DoMenuUpdates();
268#endif
269}
270
7c0ea335
VZ
271// ----------------------------------------------------------------------------
272// status bar stuff
273// ----------------------------------------------------------------------------
274
275#if wxUSE_STATUSBAR
276
277wxStatusBar* wxFrameBase::CreateStatusBar(int number,
278 long style,
279 wxWindowID id,
280 const wxString& name)
281{
282 // the main status bar can only be created once (or else it should be
283 // deleted before calling CreateStatusBar() again)
d3b9f782 284 wxCHECK_MSG( !m_frameStatusBar, NULL,
7c0ea335
VZ
285 wxT("recreating status bar in wxFrame") );
286
a4f01f05 287 SetStatusBar(OnCreateStatusBar(number, style, id, name));
7c0ea335
VZ
288
289 return m_frameStatusBar;
290}
291
292wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
293 long style,
294 wxWindowID id,
295 const wxString& name)
296{
ed791986 297 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 298
7c0ea335
VZ
299 statusBar->SetFieldsCount(number);
300
301 return statusBar;
302}
303
304void wxFrameBase::SetStatusText(const wxString& text, int number)
305{
7c0ea335
VZ
306 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
307
308 m_frameStatusBar->SetStatusText(text, number);
309}
310
311void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
312{
7c0ea335
VZ
313 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
314
315 m_frameStatusBar->SetStatusWidths(n, widths_field);
316
317 PositionStatusBar();
318}
319
1f361cdd 320void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 321{
1f361cdd
MB
322 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
323
324 m_frameStatusBar->PushStatusText(text, number);
325}
f6bcfd97 326
1f361cdd
MB
327void wxFrameBase::PopStatusText(int number)
328{
329 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
330
331 m_frameStatusBar->PopStatusText(number);
332}
333
722ed5be 334bool wxFrameBase::ShowMenuHelp(int menuId)
1f361cdd
MB
335{
336#if wxUSE_MENUS
f6bcfd97 337 // if no help string found, we will clear the status bar text
29c7962a
VZ
338 //
339 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
f6bcfd97 340 wxString helpString;
29c7962a 341 if ( menuId != wxID_SEPARATOR && menuId != wxID_NONE )
f6bcfd97 342 {
10816efb 343 const wxMenuItem * const item = FindItemInMenuBar(menuId);
fa7134b0 344 if ( item && !item->IsSeparator() )
10816efb
VZ
345 helpString = item->GetHelp();
346
347 // notice that it's ok if we don't find the item because it might
348 // belong to the popup menu, so don't assert here
f6bcfd97
BP
349 }
350
6d99eb3e 351 DoGiveHelp(helpString, true);
f6bcfd97 352
1729813a 353 return !helpString.empty();
3379ed37 354#else // !wxUSE_MENUS
d1b20379 355 return false;
3379ed37 356#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
357}
358
a4f01f05
VZ
359void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
360{
361 bool hadBar = m_frameStatusBar != NULL;
362 m_frameStatusBar = statBar;
363
364 if ( (m_frameStatusBar != NULL) != hadBar )
365 {
366 PositionStatusBar();
367
368 DoLayout();
369 }
370}
371
7c0ea335
VZ
372#endif // wxUSE_STATUSBAR
373
f257ac87 374#if wxUSE_MENUS || wxUSE_TOOLBAR
6d99eb3e 375void wxFrameBase::DoGiveHelp(const wxString& help, bool show)
c60a36d5
VZ
376{
377#if wxUSE_STATUSBAR
96ac065f
VZ
378 if ( m_statusBarPane < 0 )
379 {
380 // status bar messages disabled
381 return;
382 }
383
384 wxStatusBar *statbar = GetStatusBar();
385 if ( !statbar )
386 return;
387
6d99eb3e
VZ
388 wxString text;
389 if ( show )
4cbc928a 390 {
6d99eb3e
VZ
391 // remember the old status bar text if this is the first time we're
392 // called since the menu has been opened as we're going to overwrite it
393 // in our DoGiveHelp() and we want to restore it when the menu is
394 // closed
395 //
396 // note that it would be logical to do this in OnMenuOpen() but under
397 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
398 // enough, and so this doesn't work and instead we use the ugly trick
399 // with using special m_oldStatusText value as "menu opened" (but it is
400 // arguably better than adding yet another member variable to wxFrame
401 // on all platforms)
96ac065f
VZ
402 if ( m_oldStatusText.empty() )
403 {
6d99eb3e
VZ
404 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
405 if ( m_oldStatusText.empty() )
406 {
407 // use special value to prevent us from doing this the next time
9a83f860 408 m_oldStatusText += wxT('\0');
6d99eb3e 409 }
96ac065f 410 }
6d99eb3e 411
f0f03f32 412 m_lastHelpShown =
6d99eb3e
VZ
413 text = help;
414 }
415 else // hide help, restore the original text
416 {
f0f03f32
VZ
417 // clear the last shown help string but remember its value
418 wxString lastHelpShown;
419 lastHelpShown.swap(m_lastHelpShown);
420
421 // also clear the old status text but remember it too to restore it
422 // below
423 text.swap(m_oldStatusText);
424
425 if ( statbar->GetStatusText(m_statusBarPane) != lastHelpShown )
426 {
427 // if the text was changed with an explicit SetStatusText() call
428 // from the user code in the meanwhile, do not overwrite it with
429 // the old status bar contents -- this is almost certainly not what
430 // the user expects and would be very hard to avoid from user code
431 return;
432 }
96ac065f 433 }
c60a36d5 434
6d99eb3e 435 statbar->SetStatusText(text, m_statusBarPane);
f428e6c5 436#else
3b257996 437 wxUnusedVar(help);
f428e6c5 438 wxUnusedVar(show);
c60a36d5
VZ
439#endif // wxUSE_STATUSBAR
440}
f257ac87 441#endif // wxUSE_MENUS || wxUSE_TOOLBAR
c60a36d5
VZ
442
443
7c0ea335
VZ
444// ----------------------------------------------------------------------------
445// toolbar stuff
446// ----------------------------------------------------------------------------
447
448#if wxUSE_TOOLBAR
449
450wxToolBar* wxFrameBase::CreateToolBar(long style,
451 wxWindowID id,
452 const wxString& name)
453{
6a17b868 454 // the main toolbar can't be recreated (unless it was explicitly deleted
7c0ea335 455 // before)
d3b9f782 456 wxCHECK_MSG( !m_frameToolBar, NULL,
7c0ea335
VZ
457 wxT("recreating toolbar in wxFrame") );
458
f9dae779
VZ
459 if ( style == -1 )
460 {
461 // use default style
462 //
463 // NB: we don't specify the default value in the method declaration
464 // because
465 // a) this allows us to have different defaults for different
466 // platforms (even if we don't have them right now)
467 // b) we don't need to include wx/toolbar.h in the header then
468 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
469 }
470
a4f01f05 471 SetToolBar(OnCreateToolBar(style, id, name));
7c0ea335
VZ
472
473 return m_frameToolBar;
474}
475
476wxToolBar* wxFrameBase::OnCreateToolBar(long style,
477 wxWindowID id,
478 const wxString& name)
479{
a9102b36
JS
480#if defined(__WXWINCE__) && defined(__POCKETPC__)
481 return new wxToolMenuBar(this, id,
482 wxDefaultPosition, wxDefaultSize,
483 style, name);
484#else
7c0ea335
VZ
485 return new wxToolBar(this, id,
486 wxDefaultPosition, wxDefaultSize,
487 style, name);
a9102b36 488#endif
7c0ea335
VZ
489}
490
a4f01f05
VZ
491void wxFrameBase::SetToolBar(wxToolBar *toolbar)
492{
62f6be44 493 if ( (toolbar != NULL) != (m_frameToolBar != NULL) )
a4f01f05 494 {
62f6be44
VZ
495 // the toolbar visibility must have changed so we need to both position
496 // the toolbar itself (if it appeared) and to relayout the frame
497 // contents in any case
498
499 if ( toolbar )
500 {
501 // we need to assign it to m_frameToolBar for PositionToolBar() to
502 // do anything
503 m_frameToolBar = toolbar;
504 PositionToolBar();
505 }
506 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
507 // wouldn't recognize the (still existing) toolbar as one of our
508 // bars and wouldn't layout the single child of the frame correctly
509
510
511 // and this is even more tricky: we want DoLayout() to recognize the
512 // old toolbar for the purpose of not counting it among our non-bar
513 // children but we don't want to reserve any more space for it so we
514 // temporarily hide it
515 if ( m_frameToolBar )
516 m_frameToolBar->Hide();
a4f01f05
VZ
517
518 DoLayout();
62f6be44
VZ
519
520 if ( m_frameToolBar )
521 m_frameToolBar->Show();
a4f01f05 522 }
62f6be44
VZ
523
524 // this might have been already done above but it's simpler to just always
525 // do it unconditionally instead of testing for whether we already did it
526 m_frameToolBar = toolbar;
a4f01f05
VZ
527}
528
7c0ea335
VZ
529#endif // wxUSE_TOOLBAR
530
531// ----------------------------------------------------------------------------
6522713c 532// menus
7c0ea335
VZ
533// ----------------------------------------------------------------------------
534
1e6feb95
VZ
535#if wxUSE_MENUS
536
63fec618 537// update all menus
92f1a59c 538void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 539{
92f1a59c 540 if (menu)
4d538595
DS
541 {
542 wxEvtHandler* source = GetEventHandler();
92f1a59c 543 menu->UpdateUI(source);
4d538595
DS
544 }
545 else
54517652 546 {
4d538595
DS
547 wxMenuBar* bar = GetMenuBar();
548 if (bar != NULL)
549 bar->UpdateMenus();
63fec618 550 }
63fec618 551}
1e6feb95 552
6522713c
VZ
553void wxFrameBase::DetachMenuBar()
554{
555 if ( m_frameMenuBar )
556 {
557 m_frameMenuBar->Detach();
558 m_frameMenuBar = NULL;
559 }
560}
561
562void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
563{
564 if ( menubar )
565 {
6522713c 566 menubar->Attach((wxFrame *)this);
3dbe38c3 567 m_frameMenuBar = menubar;
6522713c
VZ
568 }
569}
570
571void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
572{
573 if ( menubar == GetMenuBar() )
574 {
575 // nothing to do
576 return;
577 }
578
579 DetachMenuBar();
580
a96b4743 581 this->AttachMenuBar(menubar);
6522713c
VZ
582}
583
79f9ea05 584wxMenuItem *wxFrameBase::FindItemInMenuBar(int menuId) const
10816efb
VZ
585{
586 const wxMenuBar * const menuBar = GetMenuBar();
587
588 return menuBar ? menuBar->FindItem(menuId) : NULL;
589}
590
1e6feb95 591#endif // wxUSE_MENUS