]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
Added Set/GetQuickBestSize
[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 #endif // WX_PRECOMP
33
34 #if wxUSE_TOOLBAR
35 #include "wx/toolbar.h"
36 #endif
37 #if wxUSE_STATUSBAR
38 #include "wx/statusbr.h"
39 #endif
40
41 // ----------------------------------------------------------------------------
42 // event table
43 // ----------------------------------------------------------------------------
44
45 #if wxUSE_MENUS && wxUSE_STATUSBAR
46
47 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
48 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
49 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
50
51 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
52 END_EVENT_TABLE()
53
54 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // construction/destruction
62 // ----------------------------------------------------------------------------
63
64 wxFrameBase::wxFrameBase()
65 {
66 #if wxUSE_MENUS
67 m_frameMenuBar = NULL;
68 #endif // wxUSE_MENUS
69
70 #if wxUSE_TOOLBAR
71 m_frameToolBar = NULL;
72 #endif // wxUSE_TOOLBAR
73
74 #if wxUSE_STATUSBAR
75 m_frameStatusBar = NULL;
76 #endif // wxUSE_STATUSBAR
77
78 m_statusBarPane = 0;
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 void wxFrameBase::SendSizeEvent()
178 {
179 wxSizeEvent event( GetSize(), GetId() );
180 event.SetEventObject( this );
181 GetEventHandler()->AddPendingEvent( event );
182 }
183
184
185 // ----------------------------------------------------------------------------
186 // misc
187 // ----------------------------------------------------------------------------
188
189 bool wxFrameBase::ProcessCommand(int id)
190 {
191 #if wxUSE_MENUS
192 wxMenuBar *bar = GetMenuBar();
193 if ( !bar )
194 return false;
195
196 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
197 commandEvent.SetEventObject(this);
198
199 wxMenuItem *item = bar->FindItem(id);
200 if (item)
201 {
202 if (!item->IsEnabled())
203 return true;
204
205 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
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.empty() )
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 #if defined(__WXWINCE__) && defined(__POCKETPC__)
493 return new wxToolMenuBar(this, id,
494 wxDefaultPosition, wxDefaultSize,
495 style, name);
496 #else
497 return new wxToolBar(this, id,
498 wxDefaultPosition, wxDefaultSize,
499 style, name);
500 #endif
501 }
502
503 void wxFrameBase::SetToolBar(wxToolBar *toolbar)
504 {
505 bool hadBar = m_frameToolBar != NULL;
506 m_frameToolBar = toolbar;
507
508 if ( (m_frameToolBar != NULL) != hadBar )
509 {
510 PositionToolBar();
511
512 DoLayout();
513 }
514 }
515
516 #endif // wxUSE_TOOLBAR
517
518 // ----------------------------------------------------------------------------
519 // menus
520 // ----------------------------------------------------------------------------
521
522 #if wxUSE_MENUS
523
524 // update all menus
525 void wxFrameBase::DoMenuUpdates(wxMenu* menu)
526 {
527 if (menu)
528 {
529 wxEvtHandler* source = GetEventHandler();
530 menu->UpdateUI(source);
531 }
532 else
533 {
534 wxMenuBar* bar = GetMenuBar();
535 if (bar != NULL)
536 bar->UpdateMenus();
537 }
538 }
539
540 void wxFrameBase::DetachMenuBar()
541 {
542 if ( m_frameMenuBar )
543 {
544 m_frameMenuBar->Detach();
545 m_frameMenuBar = NULL;
546 }
547 }
548
549 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
550 {
551 if ( menubar )
552 {
553 menubar->Attach((wxFrame *)this);
554 m_frameMenuBar = menubar;
555 }
556 }
557
558 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
559 {
560 if ( menubar == GetMenuBar() )
561 {
562 // nothing to do
563 return;
564 }
565
566 DetachMenuBar();
567
568 this->AttachMenuBar(menubar);
569 }
570
571 #endif // wxUSE_MENUS