]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
#
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if wxUSE_MENUS && wxUSE_STATUSBAR
49
50 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
51 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
52 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
53
54 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
55 END_EVENT_TABLE()
56
57 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // construction/destruction
65 // ----------------------------------------------------------------------------
66
67 wxFrameBase::wxFrameBase()
68 {
69 #if wxUSE_MENUS
70 m_frameMenuBar = NULL;
71 #endif // wxUSE_MENUS
72
73 #if wxUSE_TOOLBAR
74 m_frameToolBar = NULL;
75 #endif // wxUSE_TOOLBAR
76
77 #if wxUSE_STATUSBAR
78 m_frameStatusBar = NULL;
79 #endif // wxUSE_STATUSBAR
80
81 m_statusBarPane = 0;
82 }
83
84 wxFrameBase::~wxFrameBase()
85 {
86 // this destructor is required for Darwin
87 }
88
89 wxFrame *wxFrameBase::New(wxWindow *parent,
90 wxWindowID id,
91 const wxString& title,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 return new wxFrame(parent, id, title, pos, size, style, name);
98 }
99
100 void wxFrameBase::DeleteAllBars()
101 {
102 #if wxUSE_MENUS
103 if ( m_frameMenuBar )
104 {
105 delete m_frameMenuBar;
106 m_frameMenuBar = (wxMenuBar *) NULL;
107 }
108 #endif // wxUSE_MENUS
109
110 #if wxUSE_STATUSBAR
111 if ( m_frameStatusBar )
112 {
113 delete m_frameStatusBar;
114 m_frameStatusBar = (wxStatusBar *) NULL;
115 }
116 #endif // wxUSE_STATUSBAR
117
118 #if wxUSE_TOOLBAR
119 if ( m_frameToolBar )
120 {
121 delete m_frameToolBar;
122 m_frameToolBar = (wxToolBar *) NULL;
123 }
124 #endif // wxUSE_TOOLBAR
125 }
126
127 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
128 {
129 #if wxUSE_MENUS
130 if ( win == GetMenuBar() )
131 return true;
132 #endif // wxUSE_MENUS
133
134 #if wxUSE_STATUSBAR
135 if ( win == GetStatusBar() )
136 return true;
137 #endif // wxUSE_STATUSBAR
138
139 #if wxUSE_TOOLBAR
140 if ( win == GetToolBar() )
141 return true;
142 #endif // wxUSE_TOOLBAR
143
144 return false;
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
149 // from the client area, so the client area is what's really available for the
150 // frame contents
151 // ----------------------------------------------------------------------------
152
153 // get the origin of the client area in the client coordinates
154 wxPoint wxFrameBase::GetClientAreaOrigin() const
155 {
156 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
157
158 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
159 wxToolBar *toolbar = GetToolBar();
160 if ( toolbar && toolbar->IsShown() )
161 {
162 int w, h;
163 toolbar->GetSize(&w, &h);
164
165 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
166 {
167 pt.x += w;
168 }
169 else
170 {
171 pt.y += h;
172 }
173 }
174 #endif // wxUSE_TOOLBAR
175
176 return pt;
177 }
178
179
180 void wxFrameBase::SendSizeEvent()
181 {
182 wxSizeEvent event( GetSize(), GetId() );
183 event.SetEventObject( this );
184 GetEventHandler()->AddPendingEvent( event );
185 }
186
187
188 // ----------------------------------------------------------------------------
189 // misc
190 // ----------------------------------------------------------------------------
191
192 bool wxFrameBase::ProcessCommand(int id)
193 {
194 #if wxUSE_MENUS
195 wxMenuBar *bar = GetMenuBar();
196 if ( !bar )
197 return false;
198
199 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
200 commandEvent.SetEventObject(this);
201
202 wxMenuItem *item = bar->FindItem(id);
203 if (item)
204 {
205 if (!item->IsEnabled())
206 return true;
207
208 if (item->IsCheckable())
209 {
210 item->Toggle();
211
212 // use the new value
213 commandEvent.SetInt(item->IsChecked());
214 }
215 }
216
217 GetEventHandler()->ProcessEvent(commandEvent);
218 return true;
219 #else // !wxUSE_MENUS
220 return false;
221 #endif // wxUSE_MENUS/!wxUSE_MENUS
222 }
223
224 // Do the UI update processing for this window. This is
225 // provided for the application to call if it wants to
226 // force a UI update, particularly for the menus and toolbar.
227 void wxFrameBase::UpdateWindowUI(long flags)
228 {
229 wxWindowBase::UpdateWindowUI(flags);
230
231 #if wxUSE_TOOLBAR
232 if (GetToolBar())
233 GetToolBar()->UpdateWindowUI(flags);
234 #endif
235
236 #if wxUSE_MENUS
237 if (GetMenuBar())
238 {
239 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
240 {
241 // If coming from an idle event, we only
242 // want to update the menus if we're
243 // in the wxUSE_IDLEMENUUPDATES configuration:
244 // so if we're not, do nothing
245 }
246 else
247 DoMenuUpdates();
248 }
249 #endif // wxUSE_MENUS
250 }
251
252 // ----------------------------------------------------------------------------
253 // event handlers for status bar updates from menus
254 // ----------------------------------------------------------------------------
255
256 #if wxUSE_MENUS && wxUSE_STATUSBAR
257
258 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
259 {
260 #if wxUSE_STATUSBAR
261 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
262 #endif // wxUSE_STATUSBAR
263 }
264
265 #if !wxUSE_IDLEMENUUPDATES
266 void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
267 #else
268 void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
269 #endif
270 {
271 #if !wxUSE_IDLEMENUUPDATES
272 DoMenuUpdates(event.GetMenu());
273 #endif // !wxUSE_IDLEMENUUPDATES
274 }
275
276 void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
277 {
278 // do we have real status text to restore?
279 if ( m_oldStatusText.length() > 1 || m_oldStatusText[0u] )
280 {
281 if ( m_statusBarPane >= 0 )
282 {
283 wxStatusBar *statbar = GetStatusBar();
284 if ( statbar )
285 statbar->SetStatusText(m_oldStatusText, m_statusBarPane);
286 }
287
288 m_oldStatusText.clear();
289 }
290 }
291
292 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
293
294 // Implement internal behaviour (menu updating on some platforms)
295 void wxFrameBase::OnInternalIdle()
296 {
297 wxTopLevelWindow::OnInternalIdle();
298
299 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
300 if (wxUpdateUIEvent::CanUpdate(this))
301 DoMenuUpdates();
302 #endif
303 }
304
305 // ----------------------------------------------------------------------------
306 // status bar stuff
307 // ----------------------------------------------------------------------------
308
309 #if wxUSE_STATUSBAR
310
311 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
312 long style,
313 wxWindowID id,
314 const wxString& name)
315 {
316 // the main status bar can only be created once (or else it should be
317 // deleted before calling CreateStatusBar() again)
318 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
319 wxT("recreating status bar in wxFrame") );
320
321 SetStatusBar(OnCreateStatusBar(number, style, id, name));
322
323 return m_frameStatusBar;
324 }
325
326 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
327 long style,
328 wxWindowID id,
329 const wxString& name)
330 {
331 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
332
333 statusBar->SetFieldsCount(number);
334
335 return statusBar;
336 }
337
338 void wxFrameBase::SetStatusText(const wxString& text, int number)
339 {
340 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
341
342 m_frameStatusBar->SetStatusText(text, number);
343 }
344
345 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
346 {
347 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
348
349 m_frameStatusBar->SetStatusWidths(n, widths_field);
350
351 PositionStatusBar();
352 }
353
354 void wxFrameBase::PushStatusText(const wxString& text, int number)
355 {
356 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
357
358 m_frameStatusBar->PushStatusText(text, number);
359 }
360
361 void wxFrameBase::PopStatusText(int number)
362 {
363 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
364
365 m_frameStatusBar->PopStatusText(number);
366 }
367
368 bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
369 {
370 #if wxUSE_MENUS
371 // if no help string found, we will clear the status bar text
372 wxString helpString;
373 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
374
375 if ( show )
376 {
377 wxMenuBar *menuBar = GetMenuBar();
378 if ( menuBar )
379 {
380 // it's ok if we don't find the item because it might belong
381 // to the popup menu
382 wxMenuItem *item = menuBar->FindItem(menuId);
383 if ( item )
384 helpString = item->GetHelp();
385 }
386 }
387
388 DoGiveHelp(helpString, show);
389
390 return !helpString.empty();
391 #else // !wxUSE_MENUS
392 return false;
393 #endif // wxUSE_MENUS/!wxUSE_MENUS
394 }
395
396 void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
397 {
398 bool hadBar = m_frameStatusBar != NULL;
399 m_frameStatusBar = statBar;
400
401 if ( (m_frameStatusBar != NULL) != hadBar )
402 {
403 PositionStatusBar();
404
405 DoLayout();
406 }
407 }
408
409 #endif // wxUSE_STATUSBAR
410
411 void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
412 {
413 #if wxUSE_STATUSBAR
414 if ( m_statusBarPane < 0 )
415 {
416 // status bar messages disabled
417 return;
418 }
419
420 wxStatusBar *statbar = GetStatusBar();
421 if ( !statbar )
422 return;
423
424 wxString help;
425 if ( show )
426 help = text;
427
428 // remember the old status bar text if this is the first time we're called
429 // since the menu has been opened as we're going to overwrite it in our
430 // DoGiveHelp() and we want to restore it when the menu is closed
431 //
432 // note that it would be logical to do this in OnMenuOpen() but under MSW
433 // we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely enough, and
434 // so this doesn't work and instead we use the ugly trick with using
435 // special m_oldStatusText value as "menu opened" (but it is arguably
436 // better than adding yet another member variable to wxFrame on all
437 // platforms)
438 if ( m_oldStatusText.empty() )
439 {
440 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
441 if ( m_oldStatusText.empty() )
442 {
443 // use special value to prevent us from doing this the next time
444 m_oldStatusText += _T('\0');
445 }
446 }
447
448 statbar->SetStatusText(help, m_statusBarPane);
449 #else
450 wxUnusedVar(text);
451 wxUnusedVar(show);
452 #endif // wxUSE_STATUSBAR
453 }
454
455
456 // ----------------------------------------------------------------------------
457 // toolbar stuff
458 // ----------------------------------------------------------------------------
459
460 #if wxUSE_TOOLBAR
461
462 wxToolBar* wxFrameBase::CreateToolBar(long style,
463 wxWindowID id,
464 const wxString& name)
465 {
466 // the main toolbar can't be recreated (unless it was explicitly deeleted
467 // before)
468 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
469 wxT("recreating toolbar in wxFrame") );
470
471 if ( style == -1 )
472 {
473 // use default style
474 //
475 // NB: we don't specify the default value in the method declaration
476 // because
477 // a) this allows us to have different defaults for different
478 // platforms (even if we don't have them right now)
479 // b) we don't need to include wx/toolbar.h in the header then
480 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
481 }
482
483 SetToolBar(OnCreateToolBar(style, id, name));
484
485 return m_frameToolBar;
486 }
487
488 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
489 wxWindowID id,
490 const wxString& name)
491 {
492 return new wxToolBar(this, id,
493 wxDefaultPosition, wxDefaultSize,
494 style, name);
495 }
496
497 void wxFrameBase::SetToolBar(wxToolBar *toolbar)
498 {
499 bool hadBar = m_frameToolBar != NULL;
500 m_frameToolBar = toolbar;
501
502 if ( (m_frameToolBar != NULL) != hadBar )
503 {
504 PositionToolBar();
505
506 DoLayout();
507 }
508 }
509
510 #endif // wxUSE_TOOLBAR
511
512 // ----------------------------------------------------------------------------
513 // menus
514 // ----------------------------------------------------------------------------
515
516 #if wxUSE_MENUS
517
518 // update all menus
519 void wxFrameBase::DoMenuUpdates(wxMenu* menu)
520 {
521 wxEvtHandler* source = GetEventHandler();
522 wxMenuBar* bar = GetMenuBar();
523
524 if (menu)
525 menu->UpdateUI(source);
526 else if ( bar != NULL )
527 {
528 int nCount = bar->GetMenuCount();
529 for (int n = 0; n < nCount; n++)
530 bar->GetMenu(n)->UpdateUI(source);
531 }
532 }
533
534 void wxFrameBase::DetachMenuBar()
535 {
536 if ( m_frameMenuBar )
537 {
538 m_frameMenuBar->Detach();
539 m_frameMenuBar = NULL;
540 }
541 }
542
543 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
544 {
545 if ( menubar )
546 {
547 menubar->Attach((wxFrame *)this);
548 m_frameMenuBar = menubar;
549 }
550 }
551
552 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
553 {
554 if ( menubar == GetMenuBar() )
555 {
556 // nothing to do
557 return;
558 }
559
560 DetachMenuBar();
561
562 this->AttachMenuBar(menubar);
563 }
564
565 #endif // wxUSE_MENUS
566
567 #if WXWIN_COMPATIBILITY_2_2
568
569 bool wxFrameBase::Command(int winid)
570 {
571 return ProcessCommand(winid);
572 }
573
574 #endif // WXWIN_COMPATIBILITY_2_2