]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
No significant change
[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
63fec618
VZ
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
7c0ea335
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#ifdef __GNUG__
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
7d9f12f3 48BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
0b30bb0b
JS
49#if wxUSE_MENUS && !wxUSE_IDLEMENUUPDATES
50 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
51#endif
7c0ea335 52 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
53END_EVENT_TABLE()
54
55// ============================================================================
56// implementation
57// ============================================================================
58
59// ----------------------------------------------------------------------------
60// construction/destruction
61// ----------------------------------------------------------------------------
62
63wxFrameBase::wxFrameBase()
64{
1e6feb95 65#if wxUSE_MENUS
7c0ea335 66 m_frameMenuBar = NULL;
1e6feb95 67#endif // wxUSE_MENUS
7c0ea335
VZ
68
69#if wxUSE_TOOLBAR
70 m_frameToolBar = NULL;
71#endif // wxUSE_TOOLBAR
72
73#if wxUSE_STATUSBAR
74 m_frameStatusBar = NULL;
75#endif // wxUSE_STATUSBAR
1f361cdd
MB
76
77 m_statusBarPane = 0;
7c0ea335
VZ
78}
79
799ea011
GD
80wxFrameBase::~wxFrameBase()
81{
82 // this destructor is required for Darwin
83}
84
7c0ea335
VZ
85wxFrame *wxFrameBase::New(wxWindow *parent,
86 wxWindowID id,
87 const wxString& title,
88 const wxPoint& pos,
89 const wxSize& size,
90 long style,
91 const wxString& name)
92{
93 return new wxFrame(parent, id, title, pos, size, style, name);
94}
95
96void wxFrameBase::DeleteAllBars()
97{
1e6feb95 98#if wxUSE_MENUS
7c0ea335
VZ
99 if ( m_frameMenuBar )
100 {
101 delete m_frameMenuBar;
102 m_frameMenuBar = (wxMenuBar *) NULL;
103 }
1e6feb95 104#endif // wxUSE_MENUS
7c0ea335
VZ
105
106#if wxUSE_STATUSBAR
107 if ( m_frameStatusBar )
108 {
109 delete m_frameStatusBar;
110 m_frameStatusBar = (wxStatusBar *) NULL;
111 }
112#endif // wxUSE_STATUSBAR
113
114#if wxUSE_TOOLBAR
115 if ( m_frameToolBar )
116 {
117 delete m_frameToolBar;
118 m_frameToolBar = (wxToolBar *) NULL;
119 }
120#endif // wxUSE_TOOLBAR
121}
122
1e6feb95
VZ
123bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
124{
125#if wxUSE_MENUS
126 if ( win == GetMenuBar() )
127 return TRUE;
128#endif // wxUSE_MENUS
129
130#if wxUSE_STATUSBAR
131 if ( win == GetStatusBar() )
132 return TRUE;
133#endif // wxUSE_STATUSBAR
134
135#if wxUSE_TOOLBAR
136 if ( win == GetToolBar() )
137 return TRUE;
138#endif // wxUSE_TOOLBAR
139
140 return FALSE;
141}
142
1c4f8f8d
VZ
143// ----------------------------------------------------------------------------
144// wxFrame size management: we exclude the areas taken by menu/status/toolbars
145// from the client area, so the client area is what's really available for the
146// frame contents
147// ----------------------------------------------------------------------------
148
149// get the origin of the client area in the client coordinates
150wxPoint wxFrameBase::GetClientAreaOrigin() const
151{
7d9f12f3 152 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 153
bf95a04f 154#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) && !defined(__WXWINCE__)
d4597e13
VZ
155 wxToolBar *toolbar = GetToolBar();
156 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
157 {
158 int w, h;
d4597e13 159 toolbar->GetSize(&w, &h);
1c4f8f8d 160
d4597e13 161 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
162 {
163 pt.x += w;
164 }
165 else
166 {
167 pt.y += h;
168 }
169 }
170#endif // wxUSE_TOOLBAR
171
172 return pt;
173}
174
7c0ea335
VZ
175// ----------------------------------------------------------------------------
176// misc
177// ----------------------------------------------------------------------------
178
7c0ea335
VZ
179bool wxFrameBase::ProcessCommand(int id)
180{
1e6feb95 181#if wxUSE_MENUS
7c0ea335
VZ
182 wxMenuBar *bar = GetMenuBar();
183 if ( !bar )
184 return FALSE;
185
3ca6a5f0
BP
186 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
187 commandEvent.SetEventObject(this);
188
7c0ea335 189 wxMenuItem *item = bar->FindItem(id);
84f7908b 190 if (item)
7c0ea335 191 {
84f7908b
RR
192 if (!item->IsEnabled())
193 return TRUE;
d4597e13 194
84f7908b
RR
195 if (item->IsCheckable())
196 {
197 item->Toggle();
0472ece7 198
84f7908b
RR
199 // use the new value
200 commandEvent.SetInt(item->IsChecked());
201 }
3ca6a5f0 202 }
7c0ea335 203
a01fe3d6
RD
204 GetEventHandler()->ProcessEvent(commandEvent);
205 return TRUE;
1e6feb95
VZ
206#else // !wxUSE_MENUS
207 return FALSE;
208#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
209}
210
e39af974
JS
211// Do the UI update processing for this window. This is
212// provided for the application to call if it wants to
213// force a UI update, particularly for the menus and toolbar.
214void wxFrameBase::UpdateWindowUI(long flags)
215{
216 wxWindowBase::UpdateWindowUI(flags);
217
218#if wxUSE_TOOLBAR
219 if (GetToolBar())
220 GetToolBar()->UpdateWindowUI(flags);
221#endif
222
223#if wxUSE_MENUS
224 if (GetMenuBar())
225 {
226 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
227 {
228 // If coming from an idle event, we only
229 // want to update the menus if we're
230 // in the wxUSE_IDLEMENUUPDATES configuration:
231 // so if we're not, do nothing
232 }
233 else
234 DoMenuUpdates();
235 }
236#endif
237}
238
7c0ea335
VZ
239// ----------------------------------------------------------------------------
240// event handlers
241// ----------------------------------------------------------------------------
242
7c0ea335
VZ
243void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
244{
245#if wxUSE_STATUSBAR
f6bcfd97 246 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
247#endif // wxUSE_STATUSBAR
248}
249
e39af974
JS
250// Implement internal behaviour (menu updating on some platforms)
251void wxFrameBase::OnInternalIdle()
6522713c 252{
e2b6d07d
JS
253 wxTopLevelWindow::OnInternalIdle();
254
0b30bb0b 255#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 256 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
257 DoMenuUpdates();
258#endif
259}
260
92f1a59c 261void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
0b30bb0b
JS
262{
263#if wxUSE_MENUS && !wxUSE_IDLEMENUUPDATES
92f1a59c 264 DoMenuUpdates(event.GetMenu());
0b30bb0b 265#endif
6522713c
VZ
266}
267
7c0ea335
VZ
268// ----------------------------------------------------------------------------
269// status bar stuff
270// ----------------------------------------------------------------------------
271
272#if wxUSE_STATUSBAR
273
274wxStatusBar* wxFrameBase::CreateStatusBar(int number,
275 long style,
276 wxWindowID id,
277 const wxString& name)
278{
279 // the main status bar can only be created once (or else it should be
280 // deleted before calling CreateStatusBar() again)
281 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
282 wxT("recreating status bar in wxFrame") );
283
284 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
285 if ( m_frameStatusBar )
286 PositionStatusBar();
287
288 return m_frameStatusBar;
289}
290
291wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
292 long style,
293 wxWindowID id,
294 const wxString& name)
295{
ed791986 296 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 297
7c0ea335
VZ
298 statusBar->SetFieldsCount(number);
299
300 return statusBar;
301}
302
303void wxFrameBase::SetStatusText(const wxString& text, int number)
304{
7c0ea335
VZ
305 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
306
307 m_frameStatusBar->SetStatusText(text, number);
308}
309
310void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
311{
7c0ea335
VZ
312 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
313
314 m_frameStatusBar->SetStatusWidths(n, widths_field);
315
316 PositionStatusBar();
317}
318
1f361cdd 319void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 320{
1f361cdd
MB
321 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
322
323 m_frameStatusBar->PushStatusText(text, number);
324}
f6bcfd97 325
1f361cdd
MB
326void wxFrameBase::PopStatusText(int number)
327{
328 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
329
330 m_frameStatusBar->PopStatusText(number);
331}
332
1f361cdd
MB
333bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
334{
335#if wxUSE_MENUS
f6bcfd97
BP
336 // if no help string found, we will clear the status bar text
337 wxString helpString;
1f361cdd 338 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 339
1f361cdd 340 if ( show )
f6bcfd97
BP
341 {
342 wxMenuBar *menuBar = GetMenuBar();
343 if ( menuBar )
344 {
345 // it's ok if we don't find the item because it might belong
346 // to the popup menu
347 wxMenuItem *item = menuBar->FindItem(menuId);
348 if ( item )
349 helpString = item->GetHelp();
350 }
351 }
352
1f361cdd 353 DoGiveHelp(helpString, show);
f6bcfd97
BP
354
355 return !helpString.IsEmpty();
3379ed37
VZ
356#else // !wxUSE_MENUS
357 return FALSE;
358#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
359}
360
7c0ea335
VZ
361#endif // wxUSE_STATUSBAR
362
c60a36d5
VZ
363void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
364{
365#if wxUSE_STATUSBAR
366 if ( m_statusBarPane < 0 ) return;
367 wxStatusBar* statbar = GetStatusBar();
368 if ( !statbar ) return;
369
370 wxString help = show ? text : wxString();
371 statbar->SetStatusText( help, m_statusBarPane );
372#endif // wxUSE_STATUSBAR
373}
374
375
7c0ea335
VZ
376// ----------------------------------------------------------------------------
377// toolbar stuff
378// ----------------------------------------------------------------------------
379
380#if wxUSE_TOOLBAR
381
382wxToolBar* wxFrameBase::CreateToolBar(long style,
383 wxWindowID id,
384 const wxString& name)
385{
386 // the main toolbar can't be recreated (unless it was explicitly deeleted
387 // before)
388 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
389 wxT("recreating toolbar in wxFrame") );
390
f9dae779
VZ
391 if ( style == -1 )
392 {
393 // use default style
394 //
395 // NB: we don't specify the default value in the method declaration
396 // because
397 // a) this allows us to have different defaults for different
398 // platforms (even if we don't have them right now)
399 // b) we don't need to include wx/toolbar.h in the header then
400 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
401 }
402
7c0ea335
VZ
403 m_frameToolBar = OnCreateToolBar(style, id, name);
404
405 return m_frameToolBar;
406}
407
408wxToolBar* wxFrameBase::OnCreateToolBar(long style,
409 wxWindowID id,
410 const wxString& name)
411{
412 return new wxToolBar(this, id,
413 wxDefaultPosition, wxDefaultSize,
414 style, name);
415}
416
417#endif // wxUSE_TOOLBAR
418
419// ----------------------------------------------------------------------------
6522713c 420// menus
7c0ea335
VZ
421// ----------------------------------------------------------------------------
422
1e6feb95
VZ
423#if wxUSE_MENUS
424
63fec618 425// update all menus
92f1a59c 426void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 427{
92f1a59c 428 wxEvtHandler* source = GetEventHandler();
54517652 429 wxMenuBar* bar = GetMenuBar();
e702ff0f 430
92f1a59c
JS
431 if (menu)
432 menu->UpdateUI(source);
433 else if ( bar != NULL )
54517652
RR
434 {
435 int nCount = bar->GetMenuCount();
436 for (int n = 0; n < nCount; n++)
18afa2ac 437 bar->GetMenu(n)->UpdateUI(source);
63fec618 438 }
63fec618 439}
1e6feb95 440
6522713c
VZ
441void wxFrameBase::DetachMenuBar()
442{
443 if ( m_frameMenuBar )
444 {
445 m_frameMenuBar->Detach();
446 m_frameMenuBar = NULL;
447 }
448}
449
450void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
451{
452 if ( menubar )
453 {
6522713c 454 menubar->Attach((wxFrame *)this);
3dbe38c3 455 m_frameMenuBar = menubar;
6522713c
VZ
456 }
457}
458
459void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
460{
461 if ( menubar == GetMenuBar() )
462 {
463 // nothing to do
464 return;
465 }
466
467 DetachMenuBar();
468
469 AttachMenuBar(menubar);
470}
471
1e6feb95 472#endif // wxUSE_MENUS