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