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