]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
added SetString(), deprecated SetLabel()
[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 wxFrameBase::~wxFrameBase()
82 {
83 // this destructor is required for Darwin
84 }
85
86 wxFrame *wxFrameBase::New(wxWindow *parent,
87 wxWindowID id,
88 const wxString& title,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 return new wxFrame(parent, id, title, pos, size, style, name);
95 }
96
97 void wxFrameBase::DeleteAllBars()
98 {
99 #if wxUSE_MENUS
100 if ( m_frameMenuBar )
101 {
102 delete m_frameMenuBar;
103 m_frameMenuBar = (wxMenuBar *) NULL;
104 }
105 #endif // wxUSE_MENUS
106
107 #if wxUSE_STATUSBAR
108 if ( m_frameStatusBar )
109 {
110 delete m_frameStatusBar;
111 m_frameStatusBar = (wxStatusBar *) NULL;
112 }
113 #endif // wxUSE_STATUSBAR
114
115 #if wxUSE_TOOLBAR
116 if ( m_frameToolBar )
117 {
118 delete m_frameToolBar;
119 m_frameToolBar = (wxToolBar *) NULL;
120 }
121 #endif // wxUSE_TOOLBAR
122 }
123
124 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
125 {
126 #if wxUSE_MENUS
127 if ( win == GetMenuBar() )
128 return TRUE;
129 #endif // wxUSE_MENUS
130
131 #if wxUSE_STATUSBAR
132 if ( win == GetStatusBar() )
133 return TRUE;
134 #endif // wxUSE_STATUSBAR
135
136 #if wxUSE_TOOLBAR
137 if ( win == GetToolBar() )
138 return TRUE;
139 #endif // wxUSE_TOOLBAR
140
141 return FALSE;
142 }
143
144 // ----------------------------------------------------------------------------
145 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
146 // from the client area, so the client area is what's really available for the
147 // frame contents
148 // ----------------------------------------------------------------------------
149
150 // get the origin of the client area in the client coordinates
151 wxPoint wxFrameBase::GetClientAreaOrigin() const
152 {
153 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
154
155 #if wxUSE_TOOLBAR
156 wxToolBar *toolbar = GetToolBar();
157 if ( toolbar && toolbar->IsShown() )
158 {
159 int w, h;
160 toolbar->GetSize(&w, &h);
161
162 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
163 {
164 pt.x += w;
165 }
166 else
167 {
168 pt.y += h;
169 }
170 }
171 #endif // wxUSE_TOOLBAR
172
173 return pt;
174 }
175
176 // ----------------------------------------------------------------------------
177 // misc
178 // ----------------------------------------------------------------------------
179
180 bool wxFrameBase::ProcessCommand(int id)
181 {
182 #if wxUSE_MENUS
183 wxMenuBar *bar = GetMenuBar();
184 if ( !bar )
185 return FALSE;
186
187 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
188 commandEvent.SetEventObject(this);
189
190 wxMenuItem *item = bar->FindItem(id);
191 if (item)
192 {
193 if (!item->IsEnabled())
194 return TRUE;
195
196 if (item->IsCheckable())
197 {
198 item->Toggle();
199 // use the new value
200 commandEvent.SetInt(item->IsChecked());
201 }
202 }
203
204 return GetEventHandler()->ProcessEvent(commandEvent);
205 #else // !wxUSE_MENUS
206 return FALSE;
207 #endif // wxUSE_MENUS/!wxUSE_MENUS
208 }
209
210 // ----------------------------------------------------------------------------
211 // event handlers
212 // ----------------------------------------------------------------------------
213
214 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
215 {
216 #if wxUSE_STATUSBAR
217 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
218 #endif // wxUSE_STATUSBAR
219 }
220
221 void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
222 {
223 #if wxUSE_MENUS
224 DoMenuUpdates();
225 #endif // wxUSE_MENUS
226 }
227
228 // ----------------------------------------------------------------------------
229 // status bar stuff
230 // ----------------------------------------------------------------------------
231
232 #if wxUSE_STATUSBAR
233
234 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
235 long style,
236 wxWindowID id,
237 const wxString& name)
238 {
239 // the main status bar can only be created once (or else it should be
240 // deleted before calling CreateStatusBar() again)
241 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
242 wxT("recreating status bar in wxFrame") );
243
244 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
245 if ( m_frameStatusBar )
246 PositionStatusBar();
247
248 return m_frameStatusBar;
249 }
250
251 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
252 long style,
253 wxWindowID id,
254 const wxString& name)
255 {
256 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
257
258 statusBar->SetFieldsCount(number);
259
260 return statusBar;
261 }
262
263 void wxFrameBase::SetStatusText(const wxString& text, int number)
264 {
265 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
266
267 m_frameStatusBar->SetStatusText(text, number);
268 }
269
270 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
271 {
272 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
273
274 m_frameStatusBar->SetStatusWidths(n, widths_field);
275
276 PositionStatusBar();
277 }
278
279 bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId)
280 {
281 #if wxUSE_MENUS
282 if ( !statbar )
283 return FALSE;
284
285 // if no help string found, we will clear the status bar text
286 wxString helpString;
287
288 if ( menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */ )
289 {
290 wxMenuBar *menuBar = GetMenuBar();
291 if ( menuBar )
292 {
293 // it's ok if we don't find the item because it might belong
294 // to the popup menu
295 wxMenuItem *item = menuBar->FindItem(menuId);
296 if ( item )
297 helpString = item->GetHelp();
298 }
299 }
300
301 // set status text even if the string is empty - this will at least
302 // remove the string from the item which was previously selected
303 statbar->SetStatusText(helpString);
304
305 return !helpString.IsEmpty();
306 #else // !wxUSE_MENUS
307 return FALSE;
308 #endif // wxUSE_MENUS/!wxUSE_MENUS
309 }
310
311 #endif // wxUSE_STATUSBAR
312
313 // ----------------------------------------------------------------------------
314 // toolbar stuff
315 // ----------------------------------------------------------------------------
316
317 #if wxUSE_TOOLBAR
318
319 wxToolBar* wxFrameBase::CreateToolBar(long style,
320 wxWindowID id,
321 const wxString& name)
322 {
323 // the main toolbar can't be recreated (unless it was explicitly deeleted
324 // before)
325 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
326 wxT("recreating toolbar in wxFrame") );
327
328 m_frameToolBar = OnCreateToolBar(style, id, name);
329
330 return m_frameToolBar;
331 }
332
333 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
334 wxWindowID id,
335 const wxString& name)
336 {
337 return new wxToolBar(this, id,
338 wxDefaultPosition, wxDefaultSize,
339 style, name);
340 }
341
342 #endif // wxUSE_TOOLBAR
343
344 // ----------------------------------------------------------------------------
345 // menus
346 // ----------------------------------------------------------------------------
347
348 #if wxUSE_MENUS
349
350 // update all menus
351 void wxFrameBase::DoMenuUpdates()
352 {
353 wxMenuBar* bar = GetMenuBar();
354
355 #ifdef __WXMSW__
356 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
357 #else
358 wxWindow* focusWin = (wxWindow*) NULL;
359 #endif
360 if ( bar != NULL )
361 {
362 int nCount = bar->GetMenuCount();
363 for (int n = 0; n < nCount; n++)
364 DoMenuUpdates(bar->GetMenu(n), focusWin);
365 }
366 }
367
368 // update a menu and all submenus recursively
369 void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
370 {
371 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
372 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
373 while (node)
374 {
375 wxMenuItem* item = node->GetData();
376 if ( !item->IsSeparator() )
377 {
378 wxWindowID id = item->GetId();
379 wxUpdateUIEvent event(id);
380 event.SetEventObject( this );
381
382 if (evtHandler->ProcessEvent(event))
383 {
384 if (event.GetSetText())
385 menu->SetLabel(id, event.GetText());
386 if (event.GetSetChecked())
387 menu->Check(id, event.GetChecked());
388 if (event.GetSetEnabled())
389 menu->Enable(id, event.GetEnabled());
390 }
391
392 if (item->GetSubMenu())
393 DoMenuUpdates(item->GetSubMenu(), (wxWindow*) NULL);
394 }
395 node = node->GetNext();
396 }
397 }
398
399 void wxFrameBase::DetachMenuBar()
400 {
401 if ( m_frameMenuBar )
402 {
403 m_frameMenuBar->Detach();
404 m_frameMenuBar = NULL;
405 }
406 }
407
408 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
409 {
410 if ( menubar )
411 {
412 menubar->Attach((wxFrame *)this);
413 m_frameMenuBar = menubar;
414 }
415 }
416
417 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
418 {
419 if ( menubar == GetMenuBar() )
420 {
421 // nothing to do
422 return;
423 }
424
425 DetachMenuBar();
426
427 AttachMenuBar(menubar);
428 }
429
430 #endif // wxUSE_MENUS