]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
better learn the operators...
[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$
63fec618
VZ
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
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
7d9f12f3
VS
44// FIXME - temporary hack in absence of wxTLW in all ports!
45#ifndef wxTopLevelWindowNative
46 #define wxTopLevelWindow wxTopLevelWindowBase
47#endif
48
7c0ea335
VZ
49// ----------------------------------------------------------------------------
50// event table
51// ----------------------------------------------------------------------------
52
7d9f12f3 53BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
7c0ea335 54 EVT_IDLE(wxFrameBase::OnIdle)
7c0ea335 55 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
56END_EVENT_TABLE()
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// construction/destruction
64// ----------------------------------------------------------------------------
65
66wxFrameBase::wxFrameBase()
67{
1e6feb95 68#if wxUSE_MENUS
7c0ea335 69 m_frameMenuBar = NULL;
1e6feb95 70#endif // wxUSE_MENUS
7c0ea335
VZ
71
72#if wxUSE_TOOLBAR
73 m_frameToolBar = NULL;
74#endif // wxUSE_TOOLBAR
75
76#if wxUSE_STATUSBAR
77 m_frameStatusBar = NULL;
78#endif // wxUSE_STATUSBAR
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() )
123 return TRUE;
124#endif // wxUSE_MENUS
125
126#if wxUSE_STATUSBAR
127 if ( win == GetStatusBar() )
128 return TRUE;
129#endif // wxUSE_STATUSBAR
130
131#if wxUSE_TOOLBAR
132 if ( win == GetToolBar() )
133 return TRUE;
134#endif // wxUSE_TOOLBAR
135
136 return FALSE;
137}
138
1c4f8f8d
VZ
139// ----------------------------------------------------------------------------
140// wxFrame size management: we exclude the areas taken by menu/status/toolbars
141// from the client area, so the client area is what's really available for the
142// frame contents
143// ----------------------------------------------------------------------------
144
145// get the origin of the client area in the client coordinates
146wxPoint wxFrameBase::GetClientAreaOrigin() const
147{
7d9f12f3 148 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d
VZ
149
150#if wxUSE_TOOLBAR
3e0b743f 151 if ( GetToolBar() && GetToolBar()->IsShown() )
1c4f8f8d
VZ
152 {
153 int w, h;
154 GetToolBar()->GetSize(& w, & h);
155
156 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
157 {
158 pt.x += w;
159 }
160 else
161 {
162 pt.y += h;
163 }
164 }
165#endif // wxUSE_TOOLBAR
166
167 return pt;
168}
169
7c0ea335
VZ
170// ----------------------------------------------------------------------------
171// misc
172// ----------------------------------------------------------------------------
173
7c0ea335
VZ
174bool wxFrameBase::ProcessCommand(int id)
175{
1e6feb95 176#if wxUSE_MENUS
7c0ea335
VZ
177 wxMenuBar *bar = GetMenuBar();
178 if ( !bar )
179 return FALSE;
180
3ca6a5f0
BP
181 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
182 commandEvent.SetEventObject(this);
183
7c0ea335
VZ
184 wxMenuItem *item = bar->FindItem(id);
185 if ( item && item->IsCheckable() )
186 {
187 item->Toggle();
7c0ea335 188
3ca6a5f0
BP
189 // use the new value
190 commandEvent.SetInt(item->IsChecked());
191 }
7c0ea335
VZ
192
193 return GetEventHandler()->ProcessEvent(commandEvent);
1e6feb95
VZ
194#else // !wxUSE_MENUS
195 return FALSE;
196#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
197}
198
199// ----------------------------------------------------------------------------
200// event handlers
201// ----------------------------------------------------------------------------
202
7c0ea335
VZ
203void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
204{
205#if wxUSE_STATUSBAR
f6bcfd97 206 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
207#endif // wxUSE_STATUSBAR
208}
209
6522713c
VZ
210void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
211{
212#if wxUSE_MENUS
213 DoMenuUpdates();
214#endif // wxUSE_MENUS
215}
216
7c0ea335
VZ
217// ----------------------------------------------------------------------------
218// status bar stuff
219// ----------------------------------------------------------------------------
220
221#if wxUSE_STATUSBAR
222
223wxStatusBar* wxFrameBase::CreateStatusBar(int number,
224 long style,
225 wxWindowID id,
226 const wxString& name)
227{
228 // the main status bar can only be created once (or else it should be
229 // deleted before calling CreateStatusBar() again)
230 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
231 wxT("recreating status bar in wxFrame") );
232
233 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
234 if ( m_frameStatusBar )
235 PositionStatusBar();
236
237 return m_frameStatusBar;
238}
239
240wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
241 long style,
242 wxWindowID id,
243 const wxString& name)
244{
ed791986 245 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335
VZ
246
247 // Set the height according to the font and the border size
248 wxClientDC dc(statusBar);
249 dc.SetFont(statusBar->GetFont());
250
ed791986 251 wxCoord y;
7c0ea335
VZ
252 dc.GetTextExtent( "X", NULL, &y );
253
254 int height = (int)( (11*y)/10 + 2*statusBar->GetBorderY());
255
ed791986 256 statusBar->SetSize( -1, -1, -1, height );
7c0ea335
VZ
257
258 statusBar->SetFieldsCount(number);
259
260 return statusBar;
261}
262
263void wxFrameBase::SetStatusText(const wxString& text, int number)
264{
7c0ea335
VZ
265 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
266
267 m_frameStatusBar->SetStatusText(text, number);
268}
269
270void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
271{
7c0ea335
VZ
272 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
273
274 m_frameStatusBar->SetStatusWidths(n, widths_field);
275
276 PositionStatusBar();
277}
278
f6bcfd97
BP
279bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId)
280{
3379ed37 281#if wxUSE_MENUS
f6bcfd97
BP
282 if ( !statbar )
283 return FALSE;
284
285 // if no help string found, we will clear the status bar text
286 wxString helpString;
287
288 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
289 {
290 wxMenuBar *menuBar = GetMenuBar();
291 if ( menuBar )
292 {
293 // it's ok if we don't find the item because it might belong
294 // to the popup menu
295 wxMenuItem *item = menuBar->FindItem(menuId);
296 if ( item )
297 helpString = item->GetHelp();
298 }
299 }
300
301 // set status text even if the string is empty - this will at least
302 // remove the string from the item which was previously selected
303 statbar->SetStatusText(helpString);
304
305 return !helpString.IsEmpty();
3379ed37
VZ
306#else // !wxUSE_MENUS
307 return FALSE;
308#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
309}
310
7c0ea335
VZ
311#endif // wxUSE_STATUSBAR
312
313// ----------------------------------------------------------------------------
314// toolbar stuff
315// ----------------------------------------------------------------------------
316
317#if wxUSE_TOOLBAR
318
319wxToolBar* wxFrameBase::CreateToolBar(long style,
320 wxWindowID id,
321 const wxString& name)
322{
323 // the main toolbar can't be recreated (unless it was explicitly deeleted
324 // before)
325 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
326 wxT("recreating toolbar in wxFrame") );
327
328 m_frameToolBar = OnCreateToolBar(style, id, name);
329
330 return m_frameToolBar;
331}
332
333wxToolBar* wxFrameBase::OnCreateToolBar(long style,
334 wxWindowID id,
335 const wxString& name)
336{
337 return new wxToolBar(this, id,
338 wxDefaultPosition, wxDefaultSize,
339 style, name);
340}
341
342#endif // wxUSE_TOOLBAR
343
344// ----------------------------------------------------------------------------
6522713c 345// menus
7c0ea335
VZ
346// ----------------------------------------------------------------------------
347
1e6feb95
VZ
348#if wxUSE_MENUS
349
63fec618 350// update all menus
7c0ea335 351void wxFrameBase::DoMenuUpdates()
63fec618 352{
54517652 353 wxMenuBar* bar = GetMenuBar();
e702ff0f 354
f0e9f0d3 355#ifdef __WXMSW__
e63fdcd6 356 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
f0e9f0d3
JS
357#else
358 wxWindow* focusWin = (wxWindow*) NULL;
359#endif
7c0ea335 360 if ( bar != NULL )
54517652
RR
361 {
362 int nCount = bar->GetMenuCount();
363 for (int n = 0; n < nCount; n++)
e63fdcd6 364 DoMenuUpdates(bar->GetMenu(n), focusWin);
54517652 365 }
63fec618
VZ
366}
367
368// update a menu and all submenus recursively
e63fdcd6 369void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
7c0ea335 370{
e63fdcd6 371 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
7c0ea335
VZ
372 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
373 while (node)
63fec618 374 {
7c0ea335
VZ
375 wxMenuItem* item = node->GetData();
376 if ( !item->IsSeparator() )
377 {
378 wxWindowID id = item->GetId();
379 wxUpdateUIEvent event(id);
380 event.SetEventObject( this );
381
382 if (evtHandler->ProcessEvent(event))
383 {
384 if (event.GetSetText())
385 menu->SetLabel(id, event.GetText());
386 if (event.GetSetChecked())
387 menu->Check(id, event.GetChecked());
388 if (event.GetSetEnabled())
389 menu->Enable(id, event.GetEnabled());
390 }
391
392 if (item->GetSubMenu())
393 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
394 }
395 node = node->GetNext();
63fec618 396 }
63fec618 397}
1e6feb95 398
6522713c
VZ
399void wxFrameBase::DetachMenuBar()
400{
401 if ( m_frameMenuBar )
402 {
403 m_frameMenuBar->Detach();
404 m_frameMenuBar = NULL;
405 }
406}
407
408void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
409{
410 if ( menubar )
411 {
412 m_frameMenuBar = menubar;
413 menubar->Attach((wxFrame *)this);
414 }
415}
416
417void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
418{
419 if ( menubar == GetMenuBar() )
420 {
421 // nothing to do
422 return;
423 }
424
425 DetachMenuBar();
426
427 AttachMenuBar(menubar);
428}
429
1e6feb95 430#endif // wxUSE_MENUS