]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
implemented radio menu items for wxMSW
[wxWidgets.git] / src / common / framecmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Created: 01/02/97
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma implementation "framebase.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
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
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 // FIXME - temporary hack in absence of wxTLW in all ports!
45 #ifndef wxTopLevelWindowNative
46 #define wxTopLevelWindow wxTopLevelWindowBase
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // event table
51 // ----------------------------------------------------------------------------
52
53 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
54 EVT_IDLE(wxFrameBase::OnIdle)
55 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
56 END_EVENT_TABLE()
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // construction/destruction
64 // ----------------------------------------------------------------------------
65
66 wxFrameBase::wxFrameBase()
67 {
68 #if wxUSE_MENUS
69 m_frameMenuBar = NULL;
70 #endif // wxUSE_MENUS
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
81 wxFrameBase::~wxFrameBase()
82 {
83 // this destructor is required for Darwin
84 }
85
86 wxFrame *wxFrameBase::New(wxWindow *parent,
87 wxWindowID id,
88 const wxString& title,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 return new wxFrame(parent, id, title, pos, size, style, name);
95 }
96
97 void wxFrameBase::DeleteAllBars()
98 {
99 #if wxUSE_MENUS
100 if ( m_frameMenuBar )
101 {
102 delete m_frameMenuBar;
103 m_frameMenuBar = (wxMenuBar *) NULL;
104 }
105 #endif // wxUSE_MENUS
106
107 #if wxUSE_STATUSBAR
108 if ( m_frameStatusBar )
109 {
110 delete m_frameStatusBar;
111 m_frameStatusBar = (wxStatusBar *) NULL;
112 }
113 #endif // wxUSE_STATUSBAR
114
115 #if wxUSE_TOOLBAR
116 if ( m_frameToolBar )
117 {
118 delete m_frameToolBar;
119 m_frameToolBar = (wxToolBar *) NULL;
120 }
121 #endif // wxUSE_TOOLBAR
122 }
123
124 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
125 {
126 #if wxUSE_MENUS
127 if ( win == GetMenuBar() )
128 return TRUE;
129 #endif // wxUSE_MENUS
130
131 #if wxUSE_STATUSBAR
132 if ( win == GetStatusBar() )
133 return TRUE;
134 #endif // wxUSE_STATUSBAR
135
136 #if wxUSE_TOOLBAR
137 if ( win == GetToolBar() )
138 return TRUE;
139 #endif // wxUSE_TOOLBAR
140
141 return FALSE;
142 }
143
144 // ----------------------------------------------------------------------------
145 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
146 // from the client area, so the client area is what's really available for the
147 // frame contents
148 // ----------------------------------------------------------------------------
149
150 // get the origin of the client area in the client coordinates
151 wxPoint wxFrameBase::GetClientAreaOrigin() const
152 {
153 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
154
155 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
156 wxToolBar *toolbar = GetToolBar();
157 if ( toolbar && toolbar->IsShown() )
158 {
159 int w, h;
160 toolbar->GetSize(&w, &h);
161
162 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
163 {
164 pt.x += w;
165 }
166 else
167 {
168 pt.y += h;
169 }
170 }
171 #endif // wxUSE_TOOLBAR
172
173 return pt;
174 }
175
176 // ----------------------------------------------------------------------------
177 // misc
178 // ----------------------------------------------------------------------------
179
180 bool wxFrameBase::ProcessCommand(int id)
181 {
182 #if wxUSE_MENUS
183 wxMenuBar *bar = GetMenuBar();
184 if ( !bar )
185 return FALSE;
186
187 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
188 commandEvent.SetEventObject(this);
189
190 wxMenuItem *item = bar->FindItem(id);
191 if (item)
192 {
193 if (!item->IsEnabled())
194 return TRUE;
195
196 if (item->IsCheckable())
197 {
198 item->Toggle();
199
200 // use the new value
201 commandEvent.SetInt(item->IsChecked());
202 }
203 }
204
205 return GetEventHandler()->ProcessEvent(commandEvent);
206 #else // !wxUSE_MENUS
207 return FALSE;
208 #endif // wxUSE_MENUS/!wxUSE_MENUS
209 }
210
211 // ----------------------------------------------------------------------------
212 // event handlers
213 // ----------------------------------------------------------------------------
214
215 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
216 {
217 #if wxUSE_STATUSBAR
218 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
219 #endif // wxUSE_STATUSBAR
220 }
221
222 void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
223 {
224 #if wxUSE_MENUS
225 DoMenuUpdates();
226 #endif // wxUSE_MENUS
227 }
228
229 // ----------------------------------------------------------------------------
230 // status bar stuff
231 // ----------------------------------------------------------------------------
232
233 #if wxUSE_STATUSBAR
234
235 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
236 long style,
237 wxWindowID id,
238 const wxString& name)
239 {
240 // the main status bar can only be created once (or else it should be
241 // deleted before calling CreateStatusBar() again)
242 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
243 wxT("recreating status bar in wxFrame") );
244
245 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
246 if ( m_frameStatusBar )
247 PositionStatusBar();
248
249 return m_frameStatusBar;
250 }
251
252 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
253 long style,
254 wxWindowID id,
255 const wxString& name)
256 {
257 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
258
259 statusBar->SetFieldsCount(number);
260
261 return statusBar;
262 }
263
264 void wxFrameBase::SetStatusText(const wxString& text, int number)
265 {
266 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
267
268 m_frameStatusBar->SetStatusText(text, number);
269 }
270
271 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
272 {
273 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
274
275 m_frameStatusBar->SetStatusWidths(n, widths_field);
276
277 PositionStatusBar();
278 }
279
280 bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId)
281 {
282 #if wxUSE_MENUS
283 if ( !statbar )
284 return FALSE;
285
286 // if no help string found, we will clear the status bar text
287 wxString helpString;
288
289 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
290 {
291 wxMenuBar *menuBar = GetMenuBar();
292 if ( menuBar )
293 {
294 // it's ok if we don't find the item because it might belong
295 // to the popup menu
296 wxMenuItem *item = menuBar->FindItem(menuId);
297 if ( item )
298 helpString = item->GetHelp();
299 }
300 }
301
302 // set status text even if the string is empty - this will at least
303 // remove the string from the item which was previously selected
304 statbar->SetStatusText(helpString);
305
306 return !helpString.IsEmpty();
307 #else // !wxUSE_MENUS
308 return FALSE;
309 #endif // wxUSE_MENUS/!wxUSE_MENUS
310 }
311
312 #endif // wxUSE_STATUSBAR
313
314 // ----------------------------------------------------------------------------
315 // toolbar stuff
316 // ----------------------------------------------------------------------------
317
318 #if wxUSE_TOOLBAR
319
320 wxToolBar* wxFrameBase::CreateToolBar(long style,
321 wxWindowID id,
322 const wxString& name)
323 {
324 // the main toolbar can't be recreated (unless it was explicitly deeleted
325 // before)
326 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
327 wxT("recreating toolbar in wxFrame") );
328
329 m_frameToolBar = OnCreateToolBar(style, id, name);
330
331 return m_frameToolBar;
332 }
333
334 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
335 wxWindowID id,
336 const wxString& name)
337 {
338 return new wxToolBar(this, id,
339 wxDefaultPosition, wxDefaultSize,
340 style, name);
341 }
342
343 #endif // wxUSE_TOOLBAR
344
345 // ----------------------------------------------------------------------------
346 // menus
347 // ----------------------------------------------------------------------------
348
349 #if wxUSE_MENUS
350
351 // update all menus
352 void wxFrameBase::DoMenuUpdates()
353 {
354 wxMenuBar* bar = GetMenuBar();
355
356 #ifdef __WXMSW__
357 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
358 #else
359 wxWindow* focusWin = (wxWindow*) NULL;
360 #endif
361 if ( bar != NULL )
362 {
363 int nCount = bar->GetMenuCount();
364 for (int n = 0; n < nCount; n++)
365 DoMenuUpdates(bar->GetMenu(n), focusWin);
366 }
367 }
368
369 // update a menu and all submenus recursively
370 void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
371 {
372 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
373 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
374 while (node)
375 {
376 wxMenuItem* item = node->GetData();
377 if ( !item->IsSeparator() )
378 {
379 wxWindowID id = item->GetId();
380 wxUpdateUIEvent event(id);
381 event.SetEventObject( this );
382
383 if (evtHandler->ProcessEvent(event))
384 {
385 if (event.GetSetText())
386 menu->SetLabel(id, event.GetText());
387 if (event.GetSetChecked())
388 menu->Check(id, event.GetChecked());
389 if (event.GetSetEnabled())
390 menu->Enable(id, event.GetEnabled());
391 }
392
393 if (item->GetSubMenu())
394 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
395 }
396 node = node->GetNext();
397 }
398 }
399
400 void wxFrameBase::DetachMenuBar()
401 {
402 if ( m_frameMenuBar )
403 {
404 m_frameMenuBar->Detach();
405 m_frameMenuBar = NULL;
406 }
407 }
408
409 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
410 {
411 if ( menubar )
412 {
413 menubar->Attach((wxFrame *)this);
414 m_frameMenuBar = menubar;
415 }
416 }
417
418 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
419 {
420 if ( menubar == GetMenuBar() )
421 {
422 // nothing to do
423 return;
424 }
425
426 DetachMenuBar();
427
428 AttachMenuBar(menubar);
429 }
430
431 #endif // wxUSE_MENUS