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