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