]>
Commit | Line | Data |
---|---|---|
63fec618 | 1 | ///////////////////////////////////////////////////////////////////////////// |
7c0ea335 | 2 | // Name: common/framecmn.cpp |
63fec618 VZ |
3 | // Purpose: common (for all platforms) wxFrame functions |
4 | // Author: Julian Smart, Vadim Zeitlin | |
5 | // Created: 01/02/97 | |
439b3bf1 | 6 | // Id: $Id$ |
63fec618 VZ |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
7c0ea335 VZ |
11 | // ============================================================================ |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "framebase.h" | |
21 | #endif | |
22 | ||
f701d7ab JS |
23 | // For compilers that support precompilation, includes "wx.h". |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
7c0ea335 | 27 | #pragma hdrstop |
f701d7ab JS |
28 | #endif |
29 | ||
1e6feb95 VZ |
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 | |
7c0ea335 VZ |
36 | |
37 | #if wxUSE_TOOLBAR | |
38 | #include "wx/toolbar.h" | |
39 | #endif | |
40 | #if wxUSE_STATUSBAR | |
41 | #include "wx/statusbr.h" | |
42 | #endif | |
43 | ||
7d9f12f3 VS |
44 | // FIXME - temporary hack in absence of wxTLW in all ports! |
45 | #ifndef wxTopLevelWindowNative | |
46 | #define wxTopLevelWindow wxTopLevelWindowBase | |
47 | #endif | |
48 | ||
7c0ea335 VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // event table | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
7d9f12f3 | 53 | BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow) |
7c0ea335 | 54 | EVT_IDLE(wxFrameBase::OnIdle) |
7c0ea335 | 55 | EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight) |
7c0ea335 VZ |
56 | END_EVENT_TABLE() |
57 | ||
58 | // ============================================================================ | |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // construction/destruction | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | wxFrameBase::wxFrameBase() | |
67 | { | |
1e6feb95 | 68 | #if wxUSE_MENUS |
7c0ea335 | 69 | m_frameMenuBar = NULL; |
1e6feb95 | 70 | #endif // wxUSE_MENUS |
7c0ea335 VZ |
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 | ||
799ea011 GD |
81 | wxFrameBase::~wxFrameBase() |
82 | { | |
83 | // this destructor is required for Darwin | |
84 | } | |
85 | ||
7c0ea335 VZ |
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 | { | |
1e6feb95 | 99 | #if wxUSE_MENUS |
7c0ea335 VZ |
100 | if ( m_frameMenuBar ) |
101 | { | |
102 | delete m_frameMenuBar; | |
103 | m_frameMenuBar = (wxMenuBar *) NULL; | |
104 | } | |
1e6feb95 | 105 | #endif // wxUSE_MENUS |
7c0ea335 VZ |
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 | ||
1e6feb95 VZ |
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 | ||
1c4f8f8d VZ |
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 | { | |
7d9f12f3 | 153 | wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); |
1c4f8f8d VZ |
154 | |
155 | #if wxUSE_TOOLBAR | |
d4597e13 VZ |
156 | wxToolBar *toolbar = GetToolBar(); |
157 | if ( toolbar && toolbar->IsShown() ) | |
1c4f8f8d VZ |
158 | { |
159 | int w, h; | |
d4597e13 | 160 | toolbar->GetSize(&w, &h); |
1c4f8f8d | 161 | |
d4597e13 | 162 | if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) |
1c4f8f8d VZ |
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 | ||
7c0ea335 VZ |
176 | // ---------------------------------------------------------------------------- |
177 | // misc | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
7c0ea335 VZ |
180 | bool wxFrameBase::ProcessCommand(int id) |
181 | { | |
1e6feb95 | 182 | #if wxUSE_MENUS |
7c0ea335 VZ |
183 | wxMenuBar *bar = GetMenuBar(); |
184 | if ( !bar ) | |
185 | return FALSE; | |
186 | ||
3ca6a5f0 BP |
187 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); |
188 | commandEvent.SetEventObject(this); | |
189 | ||
7c0ea335 | 190 | wxMenuItem *item = bar->FindItem(id); |
84f7908b | 191 | if (item) |
7c0ea335 | 192 | { |
84f7908b RR |
193 | if (!item->IsEnabled()) |
194 | return TRUE; | |
d4597e13 | 195 | |
84f7908b RR |
196 | if (item->IsCheckable()) |
197 | { | |
198 | item->Toggle(); | |
199 | // use the new value | |
200 | commandEvent.SetInt(item->IsChecked()); | |
201 | } | |
3ca6a5f0 | 202 | } |
7c0ea335 VZ |
203 | |
204 | return GetEventHandler()->ProcessEvent(commandEvent); | |
1e6feb95 VZ |
205 | #else // !wxUSE_MENUS |
206 | return FALSE; | |
207 | #endif // wxUSE_MENUS/!wxUSE_MENUS | |
7c0ea335 VZ |
208 | } |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // event handlers | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
7c0ea335 VZ |
214 | void wxFrameBase::OnMenuHighlight(wxMenuEvent& event) |
215 | { | |
216 | #if wxUSE_STATUSBAR | |
f6bcfd97 | 217 | (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId()); |
7c0ea335 VZ |
218 | #endif // wxUSE_STATUSBAR |
219 | } | |
220 | ||
6522713c VZ |
221 | void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) ) |
222 | { | |
223 | #if wxUSE_MENUS | |
224 | DoMenuUpdates(); | |
225 | #endif // wxUSE_MENUS | |
226 | } | |
227 | ||
7c0ea335 VZ |
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 | { | |
ed791986 | 256 | wxStatusBar *statusBar = new wxStatusBar(this, id, style, name); |
7c0ea335 | 257 | |
7c0ea335 VZ |
258 | statusBar->SetFieldsCount(number); |
259 | ||
260 | return statusBar; | |
261 | } | |
262 | ||
263 | void wxFrameBase::SetStatusText(const wxString& text, int number) | |
264 | { | |
7c0ea335 VZ |
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 | { | |
7c0ea335 VZ |
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 | ||
f6bcfd97 BP |
279 | bool wxFrameBase::ShowMenuHelp(wxStatusBar *statbar, int menuId) |
280 | { | |
3379ed37 | 281 | #if wxUSE_MENUS |
f6bcfd97 BP |
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(); | |
3379ed37 VZ |
306 | #else // !wxUSE_MENUS |
307 | return FALSE; | |
308 | #endif // wxUSE_MENUS/!wxUSE_MENUS | |
f6bcfd97 BP |
309 | } |
310 | ||
7c0ea335 VZ |
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 | // ---------------------------------------------------------------------------- | |
6522713c | 345 | // menus |
7c0ea335 VZ |
346 | // ---------------------------------------------------------------------------- |
347 | ||
1e6feb95 VZ |
348 | #if wxUSE_MENUS |
349 | ||
63fec618 | 350 | // update all menus |
7c0ea335 | 351 | void wxFrameBase::DoMenuUpdates() |
63fec618 | 352 | { |
54517652 | 353 | wxMenuBar* bar = GetMenuBar(); |
e702ff0f | 354 | |
f0e9f0d3 | 355 | #ifdef __WXMSW__ |
e63fdcd6 | 356 | wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this); |
f0e9f0d3 JS |
357 | #else |
358 | wxWindow* focusWin = (wxWindow*) NULL; | |
359 | #endif | |
7c0ea335 | 360 | if ( bar != NULL ) |
54517652 RR |
361 | { |
362 | int nCount = bar->GetMenuCount(); | |
363 | for (int n = 0; n < nCount; n++) | |
e63fdcd6 | 364 | DoMenuUpdates(bar->GetMenu(n), focusWin); |
54517652 | 365 | } |
63fec618 VZ |
366 | } |
367 | ||
368 | // update a menu and all submenus recursively | |
e63fdcd6 | 369 | void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin) |
7c0ea335 | 370 | { |
e63fdcd6 | 371 | wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler(); |
7c0ea335 VZ |
372 | wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst(); |
373 | while (node) | |
63fec618 | 374 | { |
7c0ea335 VZ |
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(); | |
63fec618 | 396 | } |
63fec618 | 397 | } |
1e6feb95 | 398 | |
6522713c VZ |
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 | { | |
6522713c | 412 | menubar->Attach((wxFrame *)this); |
3dbe38c3 | 413 | m_frameMenuBar = menubar; |
6522713c VZ |
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 | ||
1e6feb95 | 430 | #endif // wxUSE_MENUS |