]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
[wxGTK2] Move wxFontRefData::SetStyle/SetWeight to wxNativeFontInfo
[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->GetKind() == wxITEM_RADIO) && item->IsChecked() )
209 return true;
210
211 if (item->IsCheckable())
212 {
213 item->Toggle();
214
215 // use the new value
216 commandEvent.SetInt(item->IsChecked());
217 }
218 }
219
220 GetEventHandler()->ProcessEvent(commandEvent);
221 return true;
222 #else // !wxUSE_MENUS
223 return false;
224 #endif // wxUSE_MENUS/!wxUSE_MENUS
225 }
226
227 // Do the UI update processing for this window. This is
228 // provided for the application to call if it wants to
229 // force a UI update, particularly for the menus and toolbar.
230 void wxFrameBase::UpdateWindowUI(long flags)
231 {
232 wxWindowBase::UpdateWindowUI(flags);
233
234 #if wxUSE_TOOLBAR
235 if (GetToolBar())
236 GetToolBar()->UpdateWindowUI(flags);
237 #endif
238
239 #if wxUSE_MENUS
240 if (GetMenuBar())
241 {
242 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
243 {
244 // If coming from an idle event, we only
245 // want to update the menus if we're
246 // in the wxUSE_IDLEMENUUPDATES configuration:
247 // so if we're not, do nothing
248 }
249 else
250 DoMenuUpdates();
251 }
252 #endif // wxUSE_MENUS
253 }
254
255 // ----------------------------------------------------------------------------
256 // event handlers for status bar updates from menus
257 // ----------------------------------------------------------------------------
258
259 #if wxUSE_MENUS && wxUSE_STATUSBAR
260
261 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
262 {
263 #if wxUSE_STATUSBAR
264 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
265 #endif // wxUSE_STATUSBAR
266 }
267
268 #if !wxUSE_IDLEMENUUPDATES
269 void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
270 #else
271 void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
272 #endif
273 {
274 #if !wxUSE_IDLEMENUUPDATES
275 DoMenuUpdates(event.GetMenu());
276 #endif // !wxUSE_IDLEMENUUPDATES
277 }
278
279 void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
280 {
281 // do we have real status text to restore?
282 if ( !m_oldStatusText.empty() )
283 {
284 if ( m_statusBarPane >= 0 )
285 {
286 wxStatusBar *statbar = GetStatusBar();
287 if ( statbar )
288 statbar->SetStatusText(m_oldStatusText, m_statusBarPane);
289 }
290
291 m_oldStatusText.clear();
292 }
293 }
294
295 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
296
297 // Implement internal behaviour (menu updating on some platforms)
298 void wxFrameBase::OnInternalIdle()
299 {
300 wxTopLevelWindow::OnInternalIdle();
301
302 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
303 if (wxUpdateUIEvent::CanUpdate(this))
304 DoMenuUpdates();
305 #endif
306 }
307
308 // ----------------------------------------------------------------------------
309 // status bar stuff
310 // ----------------------------------------------------------------------------
311
312 #if wxUSE_STATUSBAR
313
314 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
315 long style,
316 wxWindowID id,
317 const wxString& name)
318 {
319 // the main status bar can only be created once (or else it should be
320 // deleted before calling CreateStatusBar() again)
321 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
322 wxT("recreating status bar in wxFrame") );
323
324 SetStatusBar(OnCreateStatusBar(number, style, id, name));
325
326 return m_frameStatusBar;
327 }
328
329 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
330 long style,
331 wxWindowID id,
332 const wxString& name)
333 {
334 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
335
336 statusBar->SetFieldsCount(number);
337
338 return statusBar;
339 }
340
341 void wxFrameBase::SetStatusText(const wxString& text, int number)
342 {
343 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
344
345 m_frameStatusBar->SetStatusText(text, number);
346 }
347
348 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
349 {
350 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
351
352 m_frameStatusBar->SetStatusWidths(n, widths_field);
353
354 PositionStatusBar();
355 }
356
357 void wxFrameBase::PushStatusText(const wxString& text, int number)
358 {
359 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
360
361 m_frameStatusBar->PushStatusText(text, number);
362 }
363
364 void wxFrameBase::PopStatusText(int number)
365 {
366 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
367
368 m_frameStatusBar->PopStatusText(number);
369 }
370
371 bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
372 {
373 #if wxUSE_MENUS
374 // if no help string found, we will clear the status bar text
375 wxString helpString;
376 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
377
378 if ( show )
379 {
380 wxMenuBar *menuBar = GetMenuBar();
381 if ( menuBar )
382 {
383 // it's ok if we don't find the item because it might belong
384 // to the popup menu
385 wxMenuItem *item = menuBar->FindItem(menuId);
386 if ( item )
387 helpString = item->GetHelp();
388 }
389 }
390
391 DoGiveHelp(helpString, show);
392
393 return !helpString.empty();
394 #else // !wxUSE_MENUS
395 return false;
396 #endif // wxUSE_MENUS/!wxUSE_MENUS
397 }
398
399 void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
400 {
401 bool hadBar = m_frameStatusBar != NULL;
402 m_frameStatusBar = statBar;
403
404 if ( (m_frameStatusBar != NULL) != hadBar )
405 {
406 PositionStatusBar();
407
408 DoLayout();
409 }
410 }
411
412 #endif // wxUSE_STATUSBAR
413
414 void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
415 {
416 #if wxUSE_STATUSBAR
417 if ( m_statusBarPane < 0 )
418 {
419 // status bar messages disabled
420 return;
421 }
422
423 wxStatusBar *statbar = GetStatusBar();
424 if ( !statbar )
425 return;
426
427 wxString help;
428 if ( show )
429 help = text;
430
431 // remember the old status bar text if this is the first time we're called
432 // since the menu has been opened as we're going to overwrite it in our
433 // DoGiveHelp() and we want to restore it when the menu is closed
434 //
435 // note that it would be logical to do this in OnMenuOpen() but under MSW
436 // we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely enough, and
437 // so this doesn't work and instead we use the ugly trick with using
438 // special m_oldStatusText value as "menu opened" (but it is arguably
439 // better than adding yet another member variable to wxFrame on all
440 // platforms)
441 if ( m_oldStatusText.empty() )
442 {
443 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
444 if ( m_oldStatusText.empty() )
445 {
446 // use special value to prevent us from doing this the next time
447 m_oldStatusText += _T('\0');
448 }
449 }
450
451 statbar->SetStatusText(help, m_statusBarPane);
452 #else
453 wxUnusedVar(text);
454 wxUnusedVar(show);
455 #endif // wxUSE_STATUSBAR
456 }
457
458
459 // ----------------------------------------------------------------------------
460 // toolbar stuff
461 // ----------------------------------------------------------------------------
462
463 #if wxUSE_TOOLBAR
464
465 wxToolBar* wxFrameBase::CreateToolBar(long style,
466 wxWindowID id,
467 const wxString& name)
468 {
469 // the main toolbar can't be recreated (unless it was explicitly deeleted
470 // before)
471 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
472 wxT("recreating toolbar in wxFrame") );
473
474 if ( style == -1 )
475 {
476 // use default style
477 //
478 // NB: we don't specify the default value in the method declaration
479 // because
480 // a) this allows us to have different defaults for different
481 // platforms (even if we don't have them right now)
482 // b) we don't need to include wx/toolbar.h in the header then
483 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
484 }
485
486 SetToolBar(OnCreateToolBar(style, id, name));
487
488 return m_frameToolBar;
489 }
490
491 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
492 wxWindowID id,
493 const wxString& name)
494 {
495 #if defined(__WXWINCE__) && defined(__POCKETPC__)
496 return new wxToolMenuBar(this, id,
497 wxDefaultPosition, wxDefaultSize,
498 style, name);
499 #else
500 return new wxToolBar(this, id,
501 wxDefaultPosition, wxDefaultSize,
502 style, name);
503 #endif
504 }
505
506 void wxFrameBase::SetToolBar(wxToolBar *toolbar)
507 {
508 bool hadBar = m_frameToolBar != NULL;
509 m_frameToolBar = toolbar;
510
511 if ( (m_frameToolBar != NULL) != hadBar )
512 {
513 PositionToolBar();
514
515 DoLayout();
516 }
517 }
518
519 #endif // wxUSE_TOOLBAR
520
521 // ----------------------------------------------------------------------------
522 // menus
523 // ----------------------------------------------------------------------------
524
525 #if wxUSE_MENUS
526
527 // update all menus
528 void wxFrameBase::DoMenuUpdates(wxMenu* menu)
529 {
530 wxEvtHandler* source = GetEventHandler();
531 wxMenuBar* bar = GetMenuBar();
532
533 if (menu)
534 menu->UpdateUI(source);
535 else if ( bar != NULL )
536 {
537 int nCount = bar->GetMenuCount();
538 for (int n = 0; n < nCount; n++)
539 bar->GetMenu(n)->UpdateUI(source);
540 }
541 }
542
543 void wxFrameBase::DetachMenuBar()
544 {
545 if ( m_frameMenuBar )
546 {
547 m_frameMenuBar->Detach();
548 m_frameMenuBar = NULL;
549 }
550 }
551
552 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
553 {
554 if ( menubar )
555 {
556 menubar->Attach((wxFrame *)this);
557 m_frameMenuBar = menubar;
558 }
559 }
560
561 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
562 {
563 if ( menubar == GetMenuBar() )
564 {
565 // nothing to do
566 return;
567 }
568
569 DetachMenuBar();
570
571 this->AttachMenuBar(menubar);
572 }
573
574 #endif // wxUSE_MENUS
575
576 #if WXWIN_COMPATIBILITY_2_2
577
578 bool wxFrameBase::Command(int winid)
579 {
580 return ProcessCommand(winid);
581 }
582
583 #endif // WXWIN_COMPATIBILITY_2_2