]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
Removed a line of debug code that got left in
[wxWidgets.git] / src / common / framecmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Created: 01/02/97
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma implementation "framebase.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/frame.h"
32 #include "wx/menu.h"
33 #include "wx/menuitem.h"
34 #include "wx/dcclient.h"
35 #endif // WX_PRECOMP
36
37 #if wxUSE_TOOLBAR
38 #include "wx/toolbar.h"
39 #endif
40 #if wxUSE_STATUSBAR
41 #include "wx/statusbr.h"
42 #endif
43
44 // FIXME - temporary hack in absence of wxTLW in all ports!
45 #ifndef wxTopLevelWindowNative
46 #define wxTopLevelWindow wxTopLevelWindowBase
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // event table
51 // ----------------------------------------------------------------------------
52
53 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
54 EVT_IDLE(wxFrameBase::OnIdle)
55 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
56 END_EVENT_TABLE()
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // construction/destruction
64 // ----------------------------------------------------------------------------
65
66 wxFrameBase::wxFrameBase()
67 {
68 #if wxUSE_MENUS
69 m_frameMenuBar = NULL;
70 #endif // wxUSE_MENUS
71
72 #if wxUSE_TOOLBAR
73 m_frameToolBar = NULL;
74 #endif // wxUSE_TOOLBAR
75
76 #if wxUSE_STATUSBAR
77 m_frameStatusBar = NULL;
78 #endif // wxUSE_STATUSBAR
79 }
80
81 wxFrame *wxFrameBase::New(wxWindow *parent,
82 wxWindowID id,
83 const wxString& title,
84 const wxPoint& pos,
85 const wxSize& size,
86 long style,
87 const wxString& name)
88 {
89 return new wxFrame(parent, id, title, pos, size, style, name);
90 }
91
92 void wxFrameBase::DeleteAllBars()
93 {
94 #if wxUSE_MENUS
95 if ( m_frameMenuBar )
96 {
97 delete m_frameMenuBar;
98 m_frameMenuBar = (wxMenuBar *) NULL;
99 }
100 #endif // wxUSE_MENUS
101
102 #if wxUSE_STATUSBAR
103 if ( m_frameStatusBar )
104 {
105 delete m_frameStatusBar;
106 m_frameStatusBar = (wxStatusBar *) NULL;
107 }
108 #endif // wxUSE_STATUSBAR
109
110 #if wxUSE_TOOLBAR
111 if ( m_frameToolBar )
112 {
113 delete m_frameToolBar;
114 m_frameToolBar = (wxToolBar *) NULL;
115 }
116 #endif // wxUSE_TOOLBAR
117 }
118
119 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
120 {
121 #if wxUSE_MENUS
122 if ( win == GetMenuBar() )
123 return TRUE;
124 #endif // wxUSE_MENUS
125
126 #if wxUSE_STATUSBAR
127 if ( win == GetStatusBar() )
128 return TRUE;
129 #endif // wxUSE_STATUSBAR
130
131 #if wxUSE_TOOLBAR
132 if ( win == GetToolBar() )
133 return TRUE;
134 #endif // wxUSE_TOOLBAR
135
136 return FALSE;
137 }
138
139 // ----------------------------------------------------------------------------
140 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
141 // from the client area, so the client area is what's really available for the
142 // frame contents
143 // ----------------------------------------------------------------------------
144
145 // get the origin of the client area in the client coordinates
146 wxPoint wxFrameBase::GetClientAreaOrigin() const
147 {
148 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
149
150 #if wxUSE_TOOLBAR
151 if ( GetToolBar() && GetToolBar()->IsShown() )
152 {
153 int w, h;
154 GetToolBar()->GetSize(& w, & h);
155
156 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
157 {
158 pt.x += w;
159 }
160 else
161 {
162 pt.y += h;
163 }
164 }
165 #endif // wxUSE_TOOLBAR
166
167 return pt;
168 }
169
170 // ----------------------------------------------------------------------------
171 // misc
172 // ----------------------------------------------------------------------------
173
174 bool wxFrameBase::ProcessCommand(int id)
175 {
176 #if wxUSE_MENUS
177 wxMenuBar *bar = GetMenuBar();
178 if ( !bar )
179 return FALSE;
180
181 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
182 commandEvent.SetEventObject(this);
183
184 wxMenuItem *item = bar->FindItem(id);
185 if ( item && item->IsCheckable() )
186 {
187 item->Toggle();
188
189 // use the new value
190 commandEvent.SetInt(item->IsChecked());
191 }
192
193 return GetEventHandler()->ProcessEvent(commandEvent);
194 #else // !wxUSE_MENUS
195 return FALSE;
196 #endif // wxUSE_MENUS/!wxUSE_MENUS
197 }
198
199 // ----------------------------------------------------------------------------
200 // event handlers
201 // ----------------------------------------------------------------------------
202
203 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
204 {
205 #if wxUSE_STATUSBAR
206 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
207 #endif // wxUSE_STATUSBAR
208 }
209
210 void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
211 {
212 #if wxUSE_MENUS
213 DoMenuUpdates();
214 #endif // wxUSE_MENUS
215 }
216
217 // ----------------------------------------------------------------------------
218 // status bar stuff
219 // ----------------------------------------------------------------------------
220
221 #if wxUSE_STATUSBAR
222
223 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
224 long style,
225 wxWindowID id,
226 const wxString& name)
227 {
228 // the main status bar can only be created once (or else it should be
229 // deleted before calling CreateStatusBar() again)
230 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
231 wxT("recreating status bar in wxFrame") );
232
233 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
234 if ( m_frameStatusBar )
235 PositionStatusBar();
236
237 return m_frameStatusBar;
238 }
239
240 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
241 long style,
242 wxWindowID id,
243 const wxString& name)
244 {
245 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
246
247 statusBar->SetFieldsCount(number);
248
249 return statusBar;
250 }
251
252 void wxFrameBase::SetStatusText(const wxString& text, int number)
253 {
254 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
255
256 m_frameStatusBar->SetStatusText(text, number);
257 }
258
259 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
260 {
261 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
262
263 m_frameStatusBar->SetStatusWidths(n, widths_field);
264
265 PositionStatusBar();
266 }
267
268 bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId)
269 {
270 #if wxUSE_MENUS
271 if ( !statbar )
272 return FALSE;
273
274 // if no help string found, we will clear the status bar text
275 wxString helpString;
276
277 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
278 {
279 wxMenuBar *menuBar = GetMenuBar();
280 if ( menuBar )
281 {
282 // it's ok if we don't find the item because it might belong
283 // to the popup menu
284 wxMenuItem *item = menuBar->FindItem(menuId);
285 if ( item )
286 helpString = item->GetHelp();
287 }
288 }
289
290 // set status text even if the string is empty - this will at least
291 // remove the string from the item which was previously selected
292 statbar->SetStatusText(helpString);
293
294 return !helpString.IsEmpty();
295 #else // !wxUSE_MENUS
296 return FALSE;
297 #endif // wxUSE_MENUS/!wxUSE_MENUS
298 }
299
300 #endif // wxUSE_STATUSBAR
301
302 // ----------------------------------------------------------------------------
303 // toolbar stuff
304 // ----------------------------------------------------------------------------
305
306 #if wxUSE_TOOLBAR
307
308 wxToolBar* wxFrameBase::CreateToolBar(long style,
309 wxWindowID id,
310 const wxString& name)
311 {
312 // the main toolbar can't be recreated (unless it was explicitly deeleted
313 // before)
314 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
315 wxT("recreating toolbar in wxFrame") );
316
317 m_frameToolBar = OnCreateToolBar(style, id, name);
318
319 return m_frameToolBar;
320 }
321
322 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
323 wxWindowID id,
324 const wxString& name)
325 {
326 return new wxToolBar(this, id,
327 wxDefaultPosition, wxDefaultSize,
328 style, name);
329 }
330
331 #endif // wxUSE_TOOLBAR
332
333 // ----------------------------------------------------------------------------
334 // menus
335 // ----------------------------------------------------------------------------
336
337 #if wxUSE_MENUS
338
339 // update all menus
340 void wxFrameBase::DoMenuUpdates()
341 {
342 wxMenuBar* bar = GetMenuBar();
343
344 #ifdef __WXMSW__
345 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
346 #else
347 wxWindow* focusWin = (wxWindow*) NULL;
348 #endif
349 if ( bar != NULL )
350 {
351 int nCount = bar->GetMenuCount();
352 for (int n = 0; n < nCount; n++)
353 DoMenuUpdates(bar->GetMenu(n), focusWin);
354 }
355 }
356
357 // update a menu and all submenus recursively
358 void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
359 {
360 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
361 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
362 while (node)
363 {
364 wxMenuItem* item = node->GetData();
365 if ( !item->IsSeparator() )
366 {
367 wxWindowID id = item->GetId();
368 wxUpdateUIEvent event(id);
369 event.SetEventObject( this );
370
371 if (evtHandler->ProcessEvent(event))
372 {
373 if (event.GetSetText())
374 menu->SetLabel(id, event.GetText());
375 if (event.GetSetChecked())
376 menu->Check(id, event.GetChecked());
377 if (event.GetSetEnabled())
378 menu->Enable(id, event.GetEnabled());
379 }
380
381 if (item->GetSubMenu())
382 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
383 }
384 node = node->GetNext();
385 }
386 }
387
388 void wxFrameBase::DetachMenuBar()
389 {
390 if ( m_frameMenuBar )
391 {
392 m_frameMenuBar->Detach();
393 m_frameMenuBar = NULL;
394 }
395 }
396
397 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
398 {
399 if ( menubar )
400 {
401 m_frameMenuBar = menubar;
402 menubar->Attach((wxFrame *)this);
403 }
404 }
405
406 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
407 {
408 if ( menubar == GetMenuBar() )
409 {
410 // nothing to do
411 return;
412 }
413
414 DetachMenuBar();
415
416 AttachMenuBar(menubar);
417 }
418
419 #endif // wxUSE_MENUS