]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
code clean up, avoid duplicating the same code in font.cpp and fontutil.cpp
[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
d4597e13
VZ
151 wxToolBar *toolbar = GetToolBar();
152 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
153 {
154 int w, h;
d4597e13 155 toolbar->GetSize(&w, &h);
1c4f8f8d 156
d4597e13 157 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
158 {
159 pt.x += w;
160 }
161 else
162 {
163 pt.y += h;
164 }
165 }
166#endif // wxUSE_TOOLBAR
167
168 return pt;
169}
170
7c0ea335
VZ
171// ----------------------------------------------------------------------------
172// misc
173// ----------------------------------------------------------------------------
174
7c0ea335
VZ
175bool wxFrameBase::ProcessCommand(int id)
176{
1e6feb95 177#if wxUSE_MENUS
7c0ea335
VZ
178 wxMenuBar *bar = GetMenuBar();
179 if ( !bar )
180 return FALSE;
181
3ca6a5f0
BP
182 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
183 commandEvent.SetEventObject(this);
184
7c0ea335 185 wxMenuItem *item = bar->FindItem(id);
84f7908b 186 if (item)
7c0ea335 187 {
84f7908b
RR
188 if (!item->IsEnabled())
189 return TRUE;
d4597e13 190
84f7908b
RR
191 if (item->IsCheckable())
192 {
193 item->Toggle();
194 // use the new value
195 commandEvent.SetInt(item->IsChecked());
196 }
3ca6a5f0 197 }
7c0ea335
VZ
198
199 return GetEventHandler()->ProcessEvent(commandEvent);
1e6feb95
VZ
200#else // !wxUSE_MENUS
201 return FALSE;
202#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
203}
204
205// ----------------------------------------------------------------------------
206// event handlers
207// ----------------------------------------------------------------------------
208
7c0ea335
VZ
209void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
210{
211#if wxUSE_STATUSBAR
f6bcfd97 212 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
213#endif // wxUSE_STATUSBAR
214}
215
6522713c
VZ
216void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
217{
218#if wxUSE_MENUS
219 DoMenuUpdates();
220#endif // wxUSE_MENUS
221}
222
7c0ea335
VZ
223// ----------------------------------------------------------------------------
224// status bar stuff
225// ----------------------------------------------------------------------------
226
227#if wxUSE_STATUSBAR
228
229wxStatusBar* wxFrameBase::CreateStatusBar(int number,
230 long style,
231 wxWindowID id,
232 const wxString& name)
233{
234 // the main status bar can only be created once (or else it should be
235 // deleted before calling CreateStatusBar() again)
236 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
237 wxT("recreating status bar in wxFrame") );
238
239 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
240 if ( m_frameStatusBar )
241 PositionStatusBar();
242
243 return m_frameStatusBar;
244}
245
246wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
247 long style,
248 wxWindowID id,
249 const wxString& name)
250{
ed791986 251 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 252
7c0ea335
VZ
253 statusBar->SetFieldsCount(number);
254
255 return statusBar;
256}
257
258void wxFrameBase::SetStatusText(const wxString& text, int number)
259{
7c0ea335
VZ
260 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
261
262 m_frameStatusBar->SetStatusText(text, number);
263}
264
265void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
266{
7c0ea335
VZ
267 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
268
269 m_frameStatusBar->SetStatusWidths(n, widths_field);
270
271 PositionStatusBar();
272}
273
f6bcfd97
BP
274bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId)
275{
3379ed37 276#if wxUSE_MENUS
f6bcfd97
BP
277 if ( !statbar )
278 return FALSE;
279
280 // if no help string found, we will clear the status bar text
281 wxString helpString;
282
283 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
284 {
285 wxMenuBar *menuBar = GetMenuBar();
286 if ( menuBar )
287 {
288 // it's ok if we don't find the item because it might belong
289 // to the popup menu
290 wxMenuItem *item = menuBar->FindItem(menuId);
291 if ( item )
292 helpString = item->GetHelp();
293 }
294 }
295
296 // set status text even if the string is empty - this will at least
297 // remove the string from the item which was previously selected
298 statbar->SetStatusText(helpString);
299
300 return !helpString.IsEmpty();
3379ed37
VZ
301#else // !wxUSE_MENUS
302 return FALSE;
303#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
304}
305
7c0ea335
VZ
306#endif // wxUSE_STATUSBAR
307
308// ----------------------------------------------------------------------------
309// toolbar stuff
310// ----------------------------------------------------------------------------
311
312#if wxUSE_TOOLBAR
313
314wxToolBar* wxFrameBase::CreateToolBar(long style,
315 wxWindowID id,
316 const wxString& name)
317{
318 // the main toolbar can't be recreated (unless it was explicitly deeleted
319 // before)
320 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
321 wxT("recreating toolbar in wxFrame") );
322
323 m_frameToolBar = OnCreateToolBar(style, id, name);
324
325 return m_frameToolBar;
326}
327
328wxToolBar* wxFrameBase::OnCreateToolBar(long style,
329 wxWindowID id,
330 const wxString& name)
331{
332 return new wxToolBar(this, id,
333 wxDefaultPosition, wxDefaultSize,
334 style, name);
335}
336
337#endif // wxUSE_TOOLBAR
338
339// ----------------------------------------------------------------------------
6522713c 340// menus
7c0ea335
VZ
341// ----------------------------------------------------------------------------
342
1e6feb95
VZ
343#if wxUSE_MENUS
344
63fec618 345// update all menus
7c0ea335 346void wxFrameBase::DoMenuUpdates()
63fec618 347{
54517652 348 wxMenuBar* bar = GetMenuBar();
e702ff0f 349
f0e9f0d3 350#ifdef __WXMSW__
e63fdcd6 351 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
f0e9f0d3
JS
352#else
353 wxWindow* focusWin = (wxWindow*) NULL;
354#endif
7c0ea335 355 if ( bar != NULL )
54517652
RR
356 {
357 int nCount = bar->GetMenuCount();
358 for (int n = 0; n < nCount; n++)
e63fdcd6 359 DoMenuUpdates(bar->GetMenu(n), focusWin);
54517652 360 }
63fec618
VZ
361}
362
363// update a menu and all submenus recursively
e63fdcd6 364void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
7c0ea335 365{
e63fdcd6 366 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
7c0ea335
VZ
367 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
368 while (node)
63fec618 369 {
7c0ea335
VZ
370 wxMenuItem* item = node->GetData();
371 if ( !item->IsSeparator() )
372 {
373 wxWindowID id = item->GetId();
374 wxUpdateUIEvent event(id);
375 event.SetEventObject( this );
376
377 if (evtHandler->ProcessEvent(event))
378 {
379 if (event.GetSetText())
380 menu->SetLabel(id, event.GetText());
381 if (event.GetSetChecked())
382 menu->Check(id, event.GetChecked());
383 if (event.GetSetEnabled())
384 menu->Enable(id, event.GetEnabled());
385 }
386
387 if (item->GetSubMenu())
388 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
389 }
390 node = node->GetNext();
63fec618 391 }
63fec618 392}
1e6feb95 393
6522713c
VZ
394void wxFrameBase::DetachMenuBar()
395{
396 if ( m_frameMenuBar )
397 {
398 m_frameMenuBar->Detach();
399 m_frameMenuBar = NULL;
400 }
401}
402
403void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
404{
405 if ( menubar )
406 {
407 m_frameMenuBar = menubar;
408 menubar->Attach((wxFrame *)this);
409 }
410}
411
412void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
413{
414 if ( menubar == GetMenuBar() )
415 {
416 // nothing to do
417 return;
418 }
419
420 DetachMenuBar();
421
422 AttachMenuBar(menubar);
423}
424
1e6feb95 425#endif // wxUSE_MENUS