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 (!defined(__WXWINCE__) || (_WIN32_WCE >= 400 && !defined(WIN32_PLATFORM_PSPC) && !defined(WIN32_PLATFORM_WFSP)))
160 wxToolBar
*toolbar
= GetToolBar();
161 if ( toolbar
&& toolbar
->IsShown() )
164 toolbar
->GetSize(&w
, &h
);
166 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
175 #endif // wxUSE_TOOLBAR
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 bool wxFrameBase::ProcessCommand(int id
)
187 wxMenuBar
*bar
= GetMenuBar();
191 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
192 commandEvent
.SetEventObject(this);
194 wxMenuItem
*item
= bar
->FindItem(id
);
197 if (!item
->IsEnabled())
200 if (item
->IsCheckable())
205 commandEvent
.SetInt(item
->IsChecked());
209 GetEventHandler()->ProcessEvent(commandEvent
);
211 #else // !wxUSE_MENUS
213 #endif // wxUSE_MENUS/!wxUSE_MENUS
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
)
221 wxWindowBase::UpdateWindowUI(flags
);
225 GetToolBar()->UpdateWindowUI(flags
);
231 if ((flags
& wxUPDATE_UI_FROMIDLE
) && !wxUSE_IDLEMENUUPDATES
)
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
241 #endif // wxUSE_MENUS
244 // ----------------------------------------------------------------------------
245 // event handlers for status bar updates from menus
246 // ----------------------------------------------------------------------------
248 #if wxUSE_MENUS && wxUSE_STATUSBAR
250 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
253 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
254 #endif // wxUSE_STATUSBAR
257 #if !wxUSE_IDLEMENUUPDATES
258 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
260 void wxFrameBase::OnMenuOpen(wxMenuEvent
& WXUNUSED(event
))
263 #if !wxUSE_IDLEMENUUPDATES
264 DoMenuUpdates(event
.GetMenu());
265 #endif // !wxUSE_IDLEMENUUPDATES
268 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
270 // do we have real status text to restore?
271 if ( m_oldStatusText
.length() > 1 || m_oldStatusText
[0u] )
273 if ( m_statusBarPane
>= 0 )
275 wxStatusBar
*statbar
= GetStatusBar();
277 statbar
->SetStatusText(m_oldStatusText
, m_statusBarPane
);
280 m_oldStatusText
.clear();
284 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
286 // Implement internal behaviour (menu updating on some platforms)
287 void wxFrameBase::OnInternalIdle()
289 wxTopLevelWindow::OnInternalIdle();
291 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
292 if (wxUpdateUIEvent::CanUpdate(this))
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
303 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
306 const wxString
& name
)
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") );
313 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
314 if ( m_frameStatusBar
)
317 return m_frameStatusBar
;
320 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
323 const wxString
& name
)
325 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
327 statusBar
->SetFieldsCount(number
);
332 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
334 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
336 m_frameStatusBar
->SetStatusText(text
, number
);
339 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
341 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
343 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
348 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
350 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
352 m_frameStatusBar
->PushStatusText(text
, number
);
355 void wxFrameBase::PopStatusText(int number
)
357 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
359 m_frameStatusBar
->PopStatusText(number
);
362 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*WXUNUSED(statbar
), int menuId
)
365 // if no help string found, we will clear the status bar text
367 bool show
= menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */;
371 wxMenuBar
*menuBar
= GetMenuBar();
374 // it's ok if we don't find the item because it might belong
376 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
378 helpString
= item
->GetHelp();
382 DoGiveHelp(helpString
, show
);
384 return !helpString
.IsEmpty();
385 #else // !wxUSE_MENUS
387 #endif // wxUSE_MENUS/!wxUSE_MENUS
390 #endif // wxUSE_STATUSBAR
392 void wxFrameBase::DoGiveHelp(const wxString
& text
, bool show
)
395 if ( m_statusBarPane
< 0 )
397 // status bar messages disabled
401 wxStatusBar
*statbar
= GetStatusBar();
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
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
419 if ( m_oldStatusText
.empty() )
421 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
422 if ( m_oldStatusText
.empty() )
424 // use special value to prevent us from doing this the next time
425 m_oldStatusText
+= _T('\0');
429 statbar
->SetStatusText(help
, m_statusBarPane
);
430 #endif // wxUSE_STATUSBAR
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
440 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
442 const wxString
& name
)
444 // the main toolbar can't be recreated (unless it was explicitly deeleted
446 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
447 wxT("recreating toolbar in wxFrame") );
453 // NB: we don't specify the default value in the method declaration
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
;
461 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
463 return m_frameToolBar
;
466 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
468 const wxString
& name
)
470 return new wxToolBar(this, id
,
471 wxDefaultPosition
, wxDefaultSize
,
475 #endif // wxUSE_TOOLBAR
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
484 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
486 wxEvtHandler
* source
= GetEventHandler();
487 wxMenuBar
* bar
= GetMenuBar();
490 menu
->UpdateUI(source
);
491 else if ( bar
!= NULL
)
493 int nCount
= bar
->GetMenuCount();
494 for (int n
= 0; n
< nCount
; n
++)
495 bar
->GetMenu(n
)->UpdateUI(source
);
499 void wxFrameBase::DetachMenuBar()
501 if ( m_frameMenuBar
)
503 m_frameMenuBar
->Detach();
504 m_frameMenuBar
= NULL
;
508 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
512 menubar
->Attach((wxFrame
*)this);
513 m_frameMenuBar
= menubar
;
517 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
519 if ( menubar
== GetMenuBar() )
527 this->AttachMenuBar(menubar
);
530 #endif // wxUSE_MENUS