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"
33 #include "wx/menuitem.h"
34 #include "wx/dcclient.h"
38 #include "wx/toolbar.h"
41 #include "wx/statusbr.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(wxFrameBase
, wxWindow
)
49 EVT_IDLE(wxFrameBase::OnIdle
)
50 EVT_CLOSE(wxFrameBase::OnCloseWindow
)
51 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
52 EVT_SIZE(wxFrameBase::OnSize
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // construction/destruction
61 // ----------------------------------------------------------------------------
63 wxFrameBase::wxFrameBase()
66 m_frameMenuBar
= NULL
;
70 m_frameToolBar
= NULL
;
71 #endif // wxUSE_TOOLBAR
74 m_frameStatusBar
= NULL
;
75 #endif // wxUSE_STATUSBAR
78 bool wxFrameBase::Destroy()
80 // delayed destruction: the frame will be deleted during the next idle
82 if ( !wxPendingDelete
.Member(this) )
83 wxPendingDelete
.Append(this);
88 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
90 const wxString
& title
,
96 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
99 void wxFrameBase::DeleteAllBars()
102 if ( m_frameMenuBar
)
104 delete m_frameMenuBar
;
105 m_frameMenuBar
= (wxMenuBar
*) NULL
;
107 #endif // wxUSE_MENUS
110 if ( m_frameStatusBar
)
112 delete m_frameStatusBar
;
113 m_frameStatusBar
= (wxStatusBar
*) NULL
;
115 #endif // wxUSE_STATUSBAR
118 if ( m_frameToolBar
)
120 delete m_frameToolBar
;
121 m_frameToolBar
= (wxToolBar
*) NULL
;
123 #endif // wxUSE_TOOLBAR
126 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
129 if ( win
== GetMenuBar() )
131 #endif // wxUSE_MENUS
134 if ( win
== GetStatusBar() )
136 #endif // wxUSE_STATUSBAR
139 if ( win
== GetToolBar() )
141 #endif // wxUSE_TOOLBAR
146 // ----------------------------------------------------------------------------
147 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
148 // from the client area, so the client area is what's really available for the
150 // ----------------------------------------------------------------------------
152 // get the origin of the client area in the client coordinates
153 wxPoint
wxFrameBase::GetClientAreaOrigin() const
158 if ( GetToolBar() && GetToolBar()->IsShown() )
161 GetToolBar()->GetSize(& w
, & h
);
163 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
172 #endif // wxUSE_TOOLBAR
177 void wxFrameBase::DoScreenToClient(int *x
, int *y
) const
179 wxWindow::DoScreenToClient(x
, y
);
181 // We may be faking the client origin.
182 // So a window that's really at (0, 30) may appear
183 // (to wxWin apps) to be at (0, 0).
184 wxPoint
pt(GetClientAreaOrigin());
189 void wxFrameBase::DoClientToScreen(int *x
, int *y
) const
191 // We may be faking the client origin.
192 // So a window that's really at (0, 30) may appear
193 // (to wxWin apps) to be at (0, 0).
194 wxPoint
pt1(GetClientAreaOrigin());
198 wxWindow::DoClientToScreen(x
, y
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 // make the window modal (all other windows unresponsive)
206 void wxFrameBase::MakeModal(bool modal
)
210 wxEnableTopLevelWindows(FALSE
);
211 Enable(TRUE
); // keep this window enabled
215 wxEnableTopLevelWindows(TRUE
);
219 bool wxFrameBase::ProcessCommand(int id
)
222 wxMenuBar
*bar
= GetMenuBar();
226 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
227 commandEvent
.SetEventObject(this);
229 wxMenuItem
*item
= bar
->FindItem(id
);
230 if ( item
&& item
->IsCheckable() )
235 commandEvent
.SetInt(item
->IsChecked());
238 return GetEventHandler()->ProcessEvent(commandEvent
);
239 #else // !wxUSE_MENUS
241 #endif // wxUSE_MENUS/!wxUSE_MENUS
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 // default resizing behaviour - if only ONE subwindow, resize to fill the
250 void wxFrameBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
252 // if we're using constraints - do use them
253 #if wxUSE_CONSTRAINTS
254 if ( GetAutoLayout() )
259 #endif // wxUSE_CONSTRAINTS
261 // do we have _exactly_ one child?
262 wxWindow
*child
= (wxWindow
*)NULL
;
263 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
265 node
= node
->GetNext() )
267 wxWindow
*win
= node
->GetData();
269 // exclude top level and managed windows (status bar isn't
270 // currently in the children list except under wxMac anyhow, but
271 // it makes no harm to test for it)
272 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
276 return; // it's our second subwindow - nothing to do
283 // do we have any children at all?
286 // exactly one child - set it's size to fill the whole frame
287 int clientW
, clientH
;
288 DoGetClientSize(&clientW
, &clientH
);
290 // for whatever reasons, wxGTK wants to have a small offset - it
291 // probably looks better with it?
293 static const int ofs
= 1;
295 static const int ofs
= 0;
298 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
303 // The default implementation for the close window event.
304 void wxFrameBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
309 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
312 (void)ShowMenuHelp(GetStatusBar(), event
.GetMenuId());
313 #endif // wxUSE_STATUSBAR
316 bool wxFrameBase::SendIconizeEvent(bool iconized
)
318 wxIconizeEvent
event(GetId(), iconized
);
319 event
.SetEventObject(this);
321 return GetEventHandler()->ProcessEvent(event
);
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
330 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
333 const wxString
& name
)
335 // the main status bar can only be created once (or else it should be
336 // deleted before calling CreateStatusBar() again)
337 wxCHECK_MSG( !m_frameStatusBar
, (wxStatusBar
*)NULL
,
338 wxT("recreating status bar in wxFrame") );
340 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
341 if ( m_frameStatusBar
)
344 return m_frameStatusBar
;
347 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
350 const wxString
& name
)
352 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
354 // Set the height according to the font and the border size
355 wxClientDC
dc(statusBar
);
356 dc
.SetFont(statusBar
->GetFont());
359 dc
.GetTextExtent( "X", NULL
, &y
);
361 int height
= (int)( (11*y
)/10 + 2*statusBar
->GetBorderY());
363 statusBar
->SetSize( -1, -1, -1, height
);
365 statusBar
->SetFieldsCount(number
);
370 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
372 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
374 m_frameStatusBar
->SetStatusText(text
, number
);
377 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
379 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
381 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
386 bool wxFrameBase::ShowMenuHelp(wxStatusBar
*statbar
, int menuId
)
391 // if no help string found, we will clear the status bar text
394 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= -2 /* wxID_TITLE */ )
396 wxMenuBar
*menuBar
= GetMenuBar();
399 // it's ok if we don't find the item because it might belong
401 wxMenuItem
*item
= menuBar
->FindItem(menuId
);
403 helpString
= item
->GetHelp();
407 // set status text even if the string is empty - this will at least
408 // remove the string from the item which was previously selected
409 statbar
->SetStatusText(helpString
);
411 return !helpString
.IsEmpty();
414 #endif // wxUSE_STATUSBAR
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
422 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
424 const wxString
& name
)
426 // the main toolbar can't be recreated (unless it was explicitly deeleted
428 wxCHECK_MSG( !m_frameToolBar
, (wxToolBar
*)NULL
,
429 wxT("recreating toolbar in wxFrame") );
431 m_frameToolBar
= OnCreateToolBar(style
, id
, name
);
433 return m_frameToolBar
;
436 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
438 const wxString
& name
)
440 return new wxToolBar(this, id
,
441 wxDefaultPosition
, wxDefaultSize
,
445 #endif // wxUSE_TOOLBAR
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 void wxFrameBase::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
455 #endif // wxUSE_MENUS
461 void wxFrameBase::DoMenuUpdates()
463 wxMenuBar
* bar
= GetMenuBar();
466 wxWindow
* focusWin
= wxFindFocusDescendant((wxWindow
*) this);
468 wxWindow
* focusWin
= (wxWindow
*) NULL
;
472 int nCount
= bar
->GetMenuCount();
473 for (int n
= 0; n
< nCount
; n
++)
474 DoMenuUpdates(bar
->GetMenu(n
), focusWin
);
478 // update a menu and all submenus recursively
479 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
, wxWindow
* focusWin
)
481 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler();
482 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
485 wxMenuItem
* item
= node
->GetData();
486 if ( !item
->IsSeparator() )
488 wxWindowID id
= item
->GetId();
489 wxUpdateUIEvent
event(id
);
490 event
.SetEventObject( this );
492 if (evtHandler
->ProcessEvent(event
))
494 if (event
.GetSetText())
495 menu
->SetLabel(id
, event
.GetText());
496 if (event
.GetSetChecked())
497 menu
->Check(id
, event
.GetChecked());
498 if (event
.GetSetEnabled())
499 menu
->Enable(id
, event
.GetEnabled());
502 if (item
->GetSubMenu())
503 DoMenuUpdates(item
->GetSubMenu(), (wxWindow
*) NULL
);
505 node
= node
->GetNext();
509 #endif // wxUSE_MENUS