1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "framebase.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
33 #include "wx/menuitem.h"
34 #include "wx/dcclient.h"
38 #include "wx/toolbar.h"
41 #include "wx/statusbr.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 #if wxUSE_MENUS && wxUSE_STATUSBAR
50 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
51 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
52 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose
)
54 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
57 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // construction/destruction
65 // ----------------------------------------------------------------------------
67 wxFrameBase::wxFrameBase()
70 m_frameMenuBar
= NULL
;
74 m_frameToolBar
= NULL
;
75 #endif // wxUSE_TOOLBAR
78 m_frameStatusBar
= NULL
;
79 #endif // wxUSE_STATUSBAR
84 wxFrameBase::~wxFrameBase()
86 // this destructor is required for Darwin
89 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
91 const wxString
& title
,
97 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
100 void wxFrameBase::DeleteAllBars()
103 if ( m_frameMenuBar
)
105 delete m_frameMenuBar
;
106 m_frameMenuBar
= (wxMenuBar
*) NULL
;
108 #endif // wxUSE_MENUS
111 if ( m_frameStatusBar
)
113 delete m_frameStatusBar
;
114 m_frameStatusBar
= (wxStatusBar
*) NULL
;
116 #endif // wxUSE_STATUSBAR
119 if ( m_frameToolBar
)
121 delete m_frameToolBar
;
122 m_frameToolBar
= (wxToolBar
*) NULL
;
124 #endif // wxUSE_TOOLBAR
127 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
130 if ( win
== GetMenuBar() )
132 #endif // wxUSE_MENUS
135 if ( win
== GetStatusBar() )
137 #endif // wxUSE_STATUSBAR
140 if ( win
== GetToolBar() )
142 #endif // wxUSE_TOOLBAR
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
151 // ----------------------------------------------------------------------------
153 // get the origin of the client area in the client coordinates
154 wxPoint
wxFrameBase::GetClientAreaOrigin() const
156 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
158 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
159 wxToolBar
*toolbar
= GetToolBar();
160 if ( toolbar
&& toolbar
->IsShown() )
163 toolbar
->GetSize(&w
, &h
);
165 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
174 #endif // wxUSE_TOOLBAR
180 void wxFrameBase::SendSizeEvent()
182 wxSizeEvent
event( GetSize(), GetId() );
183 event
.SetEventObject( this );
184 GetEventHandler()->AddPendingEvent( event
);
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 bool wxFrameBase::ProcessCommand(int id
)
195 wxMenuBar
*bar
= GetMenuBar();
199 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
200 commandEvent
.SetEventObject(this);
202 wxMenuItem
*item
= bar
->FindItem(id
);
205 if (!item
->IsEnabled())
208 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
211 if (item
->IsCheckable())
216 commandEvent
.SetInt(item
->IsChecked());
220 GetEventHandler()->ProcessEvent(commandEvent
);
222 #else // !wxUSE_MENUS
224 #endif // wxUSE_MENUS/!wxUSE_MENUS
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
)
232 wxWindowBase::UpdateWindowUI(flags
);
236 GetToolBar()->UpdateWindowUI(flags
);
242 if ((flags
& wxUPDATE_UI_FROMIDLE
) && !wxUSE_IDLEMENUUPDATES
)
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
252 #endif // wxUSE_MENUS
255 // ----------------------------------------------------------------------------
256 // event handlers for status bar updates from menus
257 // ----------------------------------------------------------------------------
259 #if wxUSE_MENUS && wxUSE_STATUSBAR
261 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
264 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
265 #endif // wxUSE_STATUSBAR
268 #if !wxUSE_IDLEMENUUPDATES
269 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
271 void wxFrameBase::OnMenuOpen(wxMenuEvent
& WXUNUSED(event
))
274 #if !wxUSE_IDLEMENUUPDATES
275 DoMenuUpdates(event
.GetMenu());
276 #endif // !wxUSE_IDLEMENUUPDATES
279 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
281 // do we have real status text to restore?
282 if ( !m_oldStatusText
.empty() )
284 if ( m_statusBarPane
>= 0 )
286 wxStatusBar
*statbar
= GetStatusBar();
288 statbar
->SetStatusText(m_oldStatusText
, m_statusBarPane
);
291 m_oldStatusText
.clear();
295 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
297 // Implement internal behaviour (menu updating on some platforms)
298 void wxFrameBase::OnInternalIdle()
300 wxTopLevelWindow::OnInternalIdle();
302 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
303 if (wxUpdateUIEvent::CanUpdate(this))
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
314 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
317 const wxString
& name
)
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") );
324 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
326 return m_frameStatusBar
;
329 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
332 const wxString
& name
)
334 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
336 statusBar
->SetFieldsCount(number
);
341 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
343 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
345 m_frameStatusBar
->SetStatusText(text
, number
);
348 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
350 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
352 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
357 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
359 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
361 m_frameStatusBar
->PushStatusText(text
, number
);
364 void wxFrameBase::PopStatusText(int number
)
366 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
368 m_frameStatusBar
->PopStatusText(number
);
371 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*WXUNUSED(statbar
), int menuId
)
374 // if no help string found, we will clear the status bar text
376 bool show
= menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */;
380 wxMenuBar
*menuBar
= GetMenuBar();
383 // it's ok if we don't find the item because it might belong
385 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
387 helpString
= item
->GetHelp();
391 DoGiveHelp(helpString
, show
);
393 return !helpString
.empty();
394 #else // !wxUSE_MENUS
396 #endif // wxUSE_MENUS/!wxUSE_MENUS
399 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
401 bool hadBar
= m_frameStatusBar
!= NULL
;
402 m_frameStatusBar
= statBar
;
404 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
412 #endif // wxUSE_STATUSBAR
414 void wxFrameBase::DoGiveHelp(const wxString
& text
, bool show
)
417 if ( m_statusBarPane
< 0 )
419 // status bar messages disabled
423 wxStatusBar
*statbar
= GetStatusBar();
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
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
441 if ( m_oldStatusText
.empty() )
443 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
444 if ( m_oldStatusText
.empty() )
446 // use special value to prevent us from doing this the next time
447 m_oldStatusText
+= _T('\0');
451 statbar
->SetStatusText(help
, m_statusBarPane
);
455 #endif // wxUSE_STATUSBAR
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
465 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
467 const wxString
& name
)
469 // the main toolbar can't be recreated (unless it was explicitly deeleted
471 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
472 wxT("recreating toolbar in wxFrame") );
478 // NB: we don't specify the default value in the method declaration
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
;
486 SetToolBar(OnCreateToolBar(style
, id
, name
));
488 return m_frameToolBar
;
491 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
493 const wxString
& name
)
495 #if defined(__WXWINCE__) && defined(__POCKETPC__)
496 return new wxToolMenuBar(this, id
,
497 wxDefaultPosition
, wxDefaultSize
,
500 return new wxToolBar(this, id
,
501 wxDefaultPosition
, wxDefaultSize
,
506 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
508 bool hadBar
= m_frameToolBar
!= NULL
;
509 m_frameToolBar
= toolbar
;
511 if ( (m_frameToolBar
!= NULL
) != hadBar
)
519 #endif // wxUSE_TOOLBAR
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
528 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
530 wxEvtHandler
* source
= GetEventHandler();
531 wxMenuBar
* bar
= GetMenuBar();
534 menu
->UpdateUI(source
);
535 else if ( bar
!= NULL
)
537 int nCount
= bar
->GetMenuCount();
538 for (int n
= 0; n
< nCount
; n
++)
539 bar
->GetMenu(n
)->UpdateUI(source
);
543 void wxFrameBase::DetachMenuBar()
545 if ( m_frameMenuBar
)
547 m_frameMenuBar
->Detach();
548 m_frameMenuBar
= NULL
;
552 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
556 menubar
->Attach((wxFrame
*)this);
557 m_frameMenuBar
= menubar
;
561 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
563 if ( menubar
== GetMenuBar() )
571 this->AttachMenuBar(menubar
);
574 #endif // wxUSE_MENUS
576 #if WXWIN_COMPATIBILITY_2_2
578 bool wxFrameBase::Command(int winid
)
580 return ProcessCommand(winid
);
583 #endif // WXWIN_COMPATIBILITY_2_2