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