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, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma implementation "framebase.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
32 #include "wx/menuitem.h"
33 #include "wx/dcclient.h"
36 #include "wx/toolbar.h"
39 #include "wx/statusbr.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(wxFrameBase
, wxWindow
)
47 EVT_IDLE(wxFrameBase::OnIdle
)
48 EVT_CLOSE(wxFrameBase::OnCloseWindow
)
49 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
50 EVT_SIZE(wxFrameBase::OnSize
)
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
58 // construction/destruction
59 // ----------------------------------------------------------------------------
61 wxFrameBase::wxFrameBase()
63 m_frameMenuBar
= NULL
;
66 m_frameToolBar
= NULL
;
67 #endif // wxUSE_TOOLBAR
70 m_frameStatusBar
= NULL
;
71 #endif // wxUSE_STATUSBAR
74 bool wxFrameBase::Destroy()
76 // delayed destruction: the frame will be deleted during the next idle
78 if ( !wxPendingDelete
.Member(this) )
79 wxPendingDelete
.Append(this);
84 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
86 const wxString
& title
,
92 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
95 void wxFrameBase::DeleteAllBars()
99 delete m_frameMenuBar
;
100 m_frameMenuBar
= (wxMenuBar
*) NULL
;
104 if ( m_frameStatusBar
)
106 delete m_frameStatusBar
;
107 m_frameStatusBar
= (wxStatusBar
*) NULL
;
109 #endif // wxUSE_STATUSBAR
112 if ( m_frameToolBar
)
114 delete m_frameToolBar
;
115 m_frameToolBar
= (wxToolBar
*) NULL
;
117 #endif // wxUSE_TOOLBAR
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // make the window modal (all other windows unresponsive)
125 void wxFrameBase::MakeModal(bool modal
)
129 wxEnableTopLevelWindows(FALSE
);
130 Enable(TRUE
); // keep this window enabled
134 wxEnableTopLevelWindows(TRUE
);
138 bool wxFrameBase::ProcessCommand(int id
)
140 wxMenuBar
*bar
= GetMenuBar();
144 wxMenuItem
*item
= bar
->FindItem(id
);
145 if ( item
&& item
->IsCheckable() )
150 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
151 commandEvent
.SetInt(id
);
152 commandEvent
.SetEventObject(this);
154 return GetEventHandler()->ProcessEvent(commandEvent
);
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // default resizing behaviour - if only ONE subwindow, resize to fill the
163 void wxFrameBase::OnSize(wxSizeEvent
& event
)
165 // if we're using constraints - do use them
166 #if wxUSE_CONSTRAINTS
167 if ( GetAutoLayout() )
174 // do we have _exactly_ one child?
175 wxWindow
*child
= (wxWindow
*)NULL
;
176 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
178 node
= node
->GetNext() )
180 wxWindow
*win
= node
->GetData();
182 // exclude top level and managed windows (status bar isn't
183 // currently in the children list except under wxMac anyhow, but
184 // it makes no harm to test for it)
185 if ( !win
->IsTopLevel()
187 && (win
!= GetStatusBar())
188 #endif // wxUSE_STATUSBAR
190 && (win
!= GetToolBar())
191 #endif // wxUSE_TOOLBAR
196 return; // it's our second subwindow - nothing to do
203 // do we have any children at all?
206 // exactly one child - set it's size to fill the whole frame
207 int clientW
, clientH
;
208 DoGetClientSize(&clientW
, &clientH
);
210 // for whatever reasons, wxGTK wants to have a small offset - it
211 // probably looks better with it?
213 static const int ofs
= 0;
215 static const int ofs
= 1;
218 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
223 // The default implementation for the close window event.
224 void wxFrameBase::OnCloseWindow(wxCloseEvent
& event
)
229 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
232 if ( GetStatusBar() )
234 // if no help string found, we will clear the status bar text
237 int menuId
= event
.GetMenuId();
238 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */ )
240 wxMenuBar
*menuBar
= GetMenuBar();
243 // it's ok if we don't find the item because it might belong
245 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
247 helpString
= item
->GetHelp();
251 // set status text even if the string is empty - this will at least
252 // remove the string from the item which was previously selected
253 SetStatusText(helpString
);
255 #endif // wxUSE_STATUSBAR
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
264 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
267 const wxString
& name
)
269 // the main status bar can only be created once (or else it should be
270 // deleted before calling CreateStatusBar() again)
271 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
272 wxT("recreating status bar in wxFrame") );
274 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
275 if ( m_frameStatusBar
)
278 return m_frameStatusBar
;
281 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
284 const wxString
& name
)
286 wxStatusBar
*statusBar
= new wxStatusBar(this, id
,
287 wxPoint(0, 0), wxSize(100, 20),
290 // Set the height according to the font and the border size
291 wxClientDC
dc(statusBar
);
292 dc
.SetFont(statusBar
->GetFont());
295 dc
.GetTextExtent( "X", NULL
, &y
);
297 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
299 statusBar
->SetSize( -1, -1, 100, height
);
301 statusBar
->SetFieldsCount(number
);
306 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
308 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
310 m_frameStatusBar
->SetStatusText(text
, number
);
313 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
315 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
317 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
322 #endif // wxUSE_STATUSBAR
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
330 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
332 const wxString
& name
)
334 // the main toolbar can't be recreated (unless it was explicitly deeleted
336 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
337 wxT("recreating toolbar in wxFrame") );
339 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
341 return m_frameToolBar
;
344 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
346 const wxString
& name
)
348 return new wxToolBar(this, id
,
349 wxDefaultPosition
, wxDefaultSize
,
353 #endif // wxUSE_TOOLBAR
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 void wxFrameBase::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
365 void wxFrameBase::DoMenuUpdates()
367 wxMenuBar
* bar
= GetMenuBar();
371 int nCount
= bar
->GetMenuCount();
372 for (int n
= 0; n
< nCount
; n
++)
373 DoMenuUpdates(bar
->GetMenu(n
), (wxWindow
*) NULL
);
377 // update a menu and all submenus recursively
378 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
, wxWindow
* WXUNUSED(focusWin
))
380 wxEvtHandler
* evtHandler
= GetEventHandler();
381 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
384 wxMenuItem
* item
= node
->GetData();
385 if ( !item
->IsSeparator() )
387 wxWindowID id
= item
->GetId();
388 wxUpdateUIEvent
event(id
);
389 event
.SetEventObject( this );
391 if (evtHandler
->ProcessEvent(event
))
393 if (event
.GetSetText())
394 menu
->SetLabel(id
, event
.GetText());
395 if (event
.GetSetChecked())
396 menu
->Check(id
, event
.GetChecked());
397 if (event
.GetSetEnabled())
398 menu
->Enable(id
, event
.GetEnabled());
401 if (item
->GetSubMenu())
402 DoMenuUpdates(item
->GetSubMenu(), (wxWindow
*) NULL
);
404 node
= node
->GetNext();