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