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