]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Linux/AXP compilation fixed.
[wxWidgets.git] / src / common / framecmn.cpp
CommitLineData
63fec618 1/////////////////////////////////////////////////////////////////////////////
7c0ea335 2// Name: common/framecmn.cpp
63fec618
VZ
3// Purpose: common (for all platforms) wxFrame functions
4// Author: Julian Smart, Vadim Zeitlin
5// Created: 01/02/97
439b3bf1 6// Id: $Id$
63fec618
VZ
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
7c0ea335
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#ifdef __GNUG__
20 #pragma implementation "framebase.h"
21#endif
22
f701d7ab
JS
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
7c0ea335 27 #pragma hdrstop
f701d7ab
JS
28#endif
29
439b3bf1 30#include "wx/frame.h"
f701d7ab 31#include "wx/menu.h"
e52f60e6 32#include "wx/menuitem.h"
7c0ea335
VZ
33#include "wx/dcclient.h"
34
35#if wxUSE_TOOLBAR
36 #include "wx/toolbar.h"
37#endif
38#if wxUSE_STATUSBAR
39 #include "wx/statusbr.h"
40#endif
41
42// ----------------------------------------------------------------------------
43// event table
44// ----------------------------------------------------------------------------
45
46BEGIN_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)
51END_EVENT_TABLE()
52
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ----------------------------------------------------------------------------
58// construction/destruction
59// ----------------------------------------------------------------------------
60
61wxFrameBase::wxFrameBase()
62{
63 m_frameMenuBar = NULL;
64
65#if wxUSE_TOOLBAR
66 m_frameToolBar = NULL;
67#endif // wxUSE_TOOLBAR
68
69#if wxUSE_STATUSBAR
70 m_frameStatusBar = NULL;
71#endif // wxUSE_STATUSBAR
72}
73
74bool wxFrameBase::Destroy()
75{
76 // delayed destruction: the frame will be deleted during the next idle
77 // loop iteration
78 if ( !wxPendingDelete.Member(this) )
79 wxPendingDelete.Append(this);
80
81 return TRUE;
82}
83
84wxFrame *wxFrameBase::New(wxWindow *parent,
85 wxWindowID id,
86 const wxString& title,
87 const wxPoint& pos,
88 const wxSize& size,
89 long style,
90 const wxString& name)
91{
92 return new wxFrame(parent, id, title, pos, size, style, name);
93}
94
95void wxFrameBase::DeleteAllBars()
96{
97 if ( m_frameMenuBar )
98 {
99 delete m_frameMenuBar;
100 m_frameMenuBar = (wxMenuBar *) NULL;
101 }
102
103#if wxUSE_STATUSBAR
104 if ( m_frameStatusBar )
105 {
106 delete m_frameStatusBar;
107 m_frameStatusBar = (wxStatusBar *) NULL;
108 }
109#endif // wxUSE_STATUSBAR
110
111#if wxUSE_TOOLBAR
112 if ( m_frameToolBar )
113 {
114 delete m_frameToolBar;
115 m_frameToolBar = (wxToolBar *) NULL;
116 }
117#endif // wxUSE_TOOLBAR
118}
119
120// ----------------------------------------------------------------------------
121// misc
122// ----------------------------------------------------------------------------
123
124// make the window modal (all other windows unresponsive)
125void wxFrameBase::MakeModal(bool modal)
126{
127 if ( modal )
128 {
129 wxEnableTopLevelWindows(FALSE);
130 Enable(TRUE); // keep this window enabled
131 }
132 else
133 {
134 wxEnableTopLevelWindows(TRUE);
135 }
136}
137
138bool wxFrameBase::ProcessCommand(int id)
139{
140 wxMenuBar *bar = GetMenuBar();
141 if ( !bar )
142 return FALSE;
143
144 wxMenuItem *item = bar->FindItem(id);
145 if ( item && item->IsCheckable() )
146 {
147 item->Toggle();
148 }
149
150 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
151 commandEvent.SetInt(id);
152 commandEvent.SetEventObject(this);
153
154 return GetEventHandler()->ProcessEvent(commandEvent);
155}
156
157// ----------------------------------------------------------------------------
158// event handlers
159// ----------------------------------------------------------------------------
160
161// default resizing behaviour - if only ONE subwindow, resize to fill the
162// whole client area
163void wxFrameBase::OnSize(wxSizeEvent& event)
164{
165 // if we're using constraints - do use them
166#if wxUSE_CONSTRAINTS
167 if ( GetAutoLayout() )
168 {
169 Layout();
170 }
171 else
172#endif
173 {
174 // do we have _exactly_ one child?
175 wxWindow *child = (wxWindow *)NULL;
176 for ( wxWindowList::Node *node = GetChildren().GetFirst();
177 node;
178 node = node->GetNext() )
179 {
180 wxWindow *win = node->GetData();
181
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()
186#if wxUSE_STATUSBAR
187 && (win != GetStatusBar())
188#endif // wxUSE_STATUSBAR
189#if wxUSE_TOOLBAR
190 && (win != GetToolBar())
191#endif // wxUSE_TOOLBAR
192 )
193 {
194 if ( child )
195 {
196 return; // it's our second subwindow - nothing to do
197 }
198
199 child = win;
200 }
201 }
e6688c3f 202
7c0ea335
VZ
203 // do we have any children at all?
204 if ( child )
205 {
206 // exactly one child - set it's size to fill the whole frame
207 int clientW, clientH;
208 DoGetClientSize(&clientW, &clientH);
209
210 // for whatever reasons, wxGTK wants to have a small offset - it
211 // probably looks better with it?
212#ifdef __WXGTK__
213 static const int ofs = 0;
214#else
215 static const int ofs = 1;
216#endif
217
218 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
219 }
220 }
221}
222
223// The default implementation for the close window event.
224void wxFrameBase::OnCloseWindow(wxCloseEvent& event)
225{
226 Destroy();
227}
228
229void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
230{
231#if wxUSE_STATUSBAR
232 if ( GetStatusBar() )
233 {
234 // if no help string found, we will clear the status bar text
235 wxString helpString;
236
237 int menuId = event.GetMenuId();
238 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
239 {
240 wxMenuBar *menuBar = GetMenuBar();
241 if ( menuBar )
242 {
243 // it's ok if we don't find the item because it might belong
244 // to the popup menu
245 wxMenuItem *item = menuBar->FindItem(menuId);
246 if ( item )
247 helpString = item->GetHelp();
248 }
249 }
250
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);
254 }
255#endif // wxUSE_STATUSBAR
256}
257
258// ----------------------------------------------------------------------------
259// status bar stuff
260// ----------------------------------------------------------------------------
261
262#if wxUSE_STATUSBAR
263
264wxStatusBar* wxFrameBase::CreateStatusBar(int number,
265 long style,
266 wxWindowID id,
267 const wxString& name)
268{
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") );
273
274 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
275 if ( m_frameStatusBar )
276 PositionStatusBar();
277
278 return m_frameStatusBar;
279}
280
281wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
282 long style,
283 wxWindowID id,
284 const wxString& name)
285{
286 wxStatusBar *statusBar = new wxStatusBar(this, id,
287 wxPoint(0, 0), wxSize(100, 20),
288 style, name);
289
290 // Set the height according to the font and the border size
291 wxClientDC dc(statusBar);
292 dc.SetFont(statusBar->GetFont());
293
294 long y;
295 dc.GetTextExtent( "X", NULL, &y );
296
297 int height = (int)( (11*y)/10 + 2*statusBar->GetBorderY());
298
299 statusBar->SetSize( -1, -1, 100, height );
300
301 statusBar->SetFieldsCount(number);
302
303 return statusBar;
304}
305
306void wxFrameBase::SetStatusText(const wxString& text, int number)
307{
7c0ea335
VZ
308 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
309
310 m_frameStatusBar->SetStatusText(text, number);
311}
312
313void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
314{
7c0ea335
VZ
315 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
316
317 m_frameStatusBar->SetStatusWidths(n, widths_field);
318
319 PositionStatusBar();
320}
321
322#endif // wxUSE_STATUSBAR
323
324// ----------------------------------------------------------------------------
325// toolbar stuff
326// ----------------------------------------------------------------------------
327
328#if wxUSE_TOOLBAR
329
330wxToolBar* wxFrameBase::CreateToolBar(long style,
331 wxWindowID id,
332 const wxString& name)
333{
334 // the main toolbar can't be recreated (unless it was explicitly deeleted
335 // before)
336 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
337 wxT("recreating toolbar in wxFrame") );
338
339 m_frameToolBar = OnCreateToolBar(style, id, name);
340
341 return m_frameToolBar;
342}
343
344wxToolBar* wxFrameBase::OnCreateToolBar(long style,
345 wxWindowID id,
346 const wxString& name)
347{
348 return new wxToolBar(this, id,
349 wxDefaultPosition, wxDefaultSize,
350 style, name);
351}
352
353#endif // wxUSE_TOOLBAR
354
355// ----------------------------------------------------------------------------
356// Menu UI updating
357// ----------------------------------------------------------------------------
358
359void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
63fec618 360{
54517652 361 DoMenuUpdates();
63fec618
VZ
362}
363
364// update all menus
7c0ea335 365void wxFrameBase::DoMenuUpdates()
63fec618 366{
54517652 367 wxMenuBar* bar = GetMenuBar();
e702ff0f 368
7c0ea335 369 if ( bar != NULL )
54517652
RR
370 {
371 int nCount = bar->GetMenuCount();
372 for (int n = 0; n < nCount; n++)
373 DoMenuUpdates(bar->GetMenu(n), (wxWindow*) NULL);
374 }
63fec618
VZ
375}
376
377// update a menu and all submenus recursively
7c0ea335
VZ
378void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* WXUNUSED(focusWin))
379{
380 wxEvtHandler* evtHandler = GetEventHandler();
381 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
382 while (node)
63fec618 383 {
7c0ea335
VZ
384 wxMenuItem* item = node->GetData();
385 if ( !item->IsSeparator() )
386 {
387 wxWindowID id = item->GetId();
388 wxUpdateUIEvent event(id);
389 event.SetEventObject( this );
390
391 if (evtHandler->ProcessEvent(event))
392 {
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());
399 }
400
401 if (item->GetSubMenu())
402 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
403 }
404 node = node->GetNext();
63fec618 405 }
63fec618 406}