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