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