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 // ----------------------------------------------------------------------------
121 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
122 // from the client area, so the client area is what's really available for the
124 // ----------------------------------------------------------------------------
126 // get the origin of the client area in the client coordinates
127 wxPoint
wxFrameBase::GetClientAreaOrigin() const
132 if ( GetToolBar() && GetToolBar()->IsShown() )
135 GetToolBar()->GetSize(& w
, & h
);
137 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
146 #endif // wxUSE_TOOLBAR
151 void wxFrameBase::DoScreenToClient(int *x
, int *y
) const
153 wxWindow::DoScreenToClient(x
, y
);
155 // We may be faking the client origin.
156 // So a window that's really at (0, 30) may appear
157 // (to wxWin apps) to be at (0, 0).
158 wxPoint
pt(GetClientAreaOrigin());
163 void wxFrameBase::DoClientToScreen(int *x
, int *y
) const
165 // We may be faking the client origin.
166 // So a window that's really at (0, 30) may appear
167 // (to wxWin apps) to be at (0, 0).
168 wxPoint
pt1(GetClientAreaOrigin());
172 wxWindow::DoClientToScreen(x
, y
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 // make the window modal (all other windows unresponsive)
180 void wxFrameBase::MakeModal(bool modal
)
184 wxEnableTopLevelWindows(FALSE
);
185 Enable(TRUE
); // keep this window enabled
189 wxEnableTopLevelWindows(TRUE
);
193 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
);
203 if ( item
&& item
->IsCheckable() )
208 commandEvent
.SetInt(item
->IsChecked());
211 return GetEventHandler()->ProcessEvent(commandEvent
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 // default resizing behaviour - if only ONE subwindow, resize to fill the
220 void wxFrameBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
222 // if we're using constraints - do use them
223 #if wxUSE_CONSTRAINTS
224 if ( GetAutoLayout() )
231 // do we have _exactly_ one child?
232 wxWindow
*child
= (wxWindow
*)NULL
;
233 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
235 node
= node
->GetNext() )
237 wxWindow
*win
= node
->GetData();
239 // exclude top level and managed windows (status bar isn't
240 // currently in the children list except under wxMac anyhow, but
241 // it makes no harm to test for it)
242 if ( !win
->IsTopLevel()
244 && (win
!= GetStatusBar())
245 #endif // wxUSE_STATUSBAR
247 && (win
!= GetToolBar())
248 #endif // wxUSE_TOOLBAR
253 return; // it's our second subwindow - nothing to do
260 // do we have any children at all?
263 // exactly one child - set it's size to fill the whole frame
264 int clientW
, clientH
;
265 DoGetClientSize(&clientW
, &clientH
);
267 // for whatever reasons, wxGTK wants to have a small offset - it
268 // probably looks better with it?
270 static const int ofs
= 1;
272 static const int ofs
= 0;
275 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
280 // The default implementation for the close window event.
281 void wxFrameBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
286 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
289 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
290 #endif // wxUSE_STATUSBAR
293 bool wxFrameBase::SendIconizeEvent(bool iconized
)
295 wxIconizeEvent
event(GetId(), iconized
);
296 event
.SetEventObject(this);
298 return GetEventHandler()->ProcessEvent(event
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
307 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
310 const wxString
& name
)
312 // the main status bar can only be created once (or else it should be
313 // deleted before calling CreateStatusBar() again)
314 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
315 wxT("recreating status bar in wxFrame") );
317 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
318 if ( m_frameStatusBar
)
321 return m_frameStatusBar
;
324 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
327 const wxString
& name
)
329 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
331 // Set the height according to the font and the border size
332 wxClientDC
dc(statusBar
);
333 dc
.SetFont(statusBar
->GetFont());
336 dc
.GetTextExtent( "X", NULL
, &y
);
338 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
340 statusBar
->SetSize( -1, -1, -1, height
);
342 statusBar
->SetFieldsCount(number
);
347 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
349 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
351 m_frameStatusBar
->SetStatusText(text
, number
);
354 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
356 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
358 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
363 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*statbar
, int menuId
)
368 // if no help string found, we will clear the status bar text
371 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */ )
373 wxMenuBar
*menuBar
= GetMenuBar();
376 // it's ok if we don't find the item because it might belong
378 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
380 helpString
= item
->GetHelp();
384 // set status text even if the string is empty - this will at least
385 // remove the string from the item which was previously selected
386 statbar
->SetStatusText(helpString
);
388 return !helpString
.IsEmpty();
391 #endif // wxUSE_STATUSBAR
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
399 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
401 const wxString
& name
)
403 // the main toolbar can't be recreated (unless it was explicitly deeleted
405 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
406 wxT("recreating toolbar in wxFrame") );
408 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
410 return m_frameToolBar
;
413 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
415 const wxString
& name
)
417 return new wxToolBar(this, id
,
418 wxDefaultPosition
, wxDefaultSize
,
422 #endif // wxUSE_TOOLBAR
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 void wxFrameBase::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
434 void wxFrameBase::DoMenuUpdates()
436 wxMenuBar
* bar
= GetMenuBar();
439 wxWindow
* focusWin
= wxFindFocusDescendant((wxWindow
*) this);
441 wxWindow
* focusWin
= (wxWindow
*) NULL
;
445 int nCount
= bar
->GetMenuCount();
446 for (int n
= 0; n
< nCount
; n
++)
447 DoMenuUpdates(bar
->GetMenu(n
), focusWin
);
451 // update a menu and all submenus recursively
452 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
, wxWindow
* focusWin
)
454 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler();
455 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
458 wxMenuItem
* item
= node
->GetData();
459 if ( !item
->IsSeparator() )
461 wxWindowID id
= item
->GetId();
462 wxUpdateUIEvent
event(id
);
463 event
.SetEventObject( this );
465 if (evtHandler
->ProcessEvent(event
))
467 if (event
.GetSetText())
468 menu
->SetLabel(id
, event
.GetText());
469 if (event
.GetSetChecked())
470 menu
->Check(id
, event
.GetChecked());
471 if (event
.GetSetEnabled())
472 menu
->Enable(id
, event
.GetEnabled());
475 if (item
->GetSubMenu())
476 DoMenuUpdates(item
->GetSubMenu(), (wxWindow
*) NULL
);
478 node
= node
->GetNext();