]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
Allow building wxProtocol without wxUSE_SOCKETS
[wxWidgets.git] / src / common / framecmn.cpp
CommitLineData
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 53BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
7c0ea335 54 EVT_IDLE(wxFrameBase::OnIdle)
7c0ea335 55 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
56END_EVENT_TABLE()
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// construction/destruction
64// ----------------------------------------------------------------------------
65
66wxFrameBase::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
1f361cdd
MB
79
80 m_statusBarPane = 0;
7c0ea335
VZ
81}
82
799ea011
GD
83wxFrameBase::~wxFrameBase()
84{
85 // this destructor is required for Darwin
86}
87
7c0ea335
VZ
88wxFrame *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
99void wxFrameBase::DeleteAllBars()
100{
1e6feb95 101#if wxUSE_MENUS
7c0ea335
VZ
102 if ( m_frameMenuBar )
103 {
104 delete m_frameMenuBar;
105 m_frameMenuBar = (wxMenuBar *) NULL;
106 }
1e6feb95 107#endif // wxUSE_MENUS
7c0ea335
VZ
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
1e6feb95
VZ
126bool 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
1c4f8f8d
VZ
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
153wxPoint wxFrameBase::GetClientAreaOrigin() const
154{
7d9f12f3 155 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 156
16c9a425 157#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
158 wxToolBar *toolbar = GetToolBar();
159 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
160 {
161 int w, h;
d4597e13 162 toolbar->GetSize(&w, &h);
1c4f8f8d 163
d4597e13 164 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
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
7c0ea335
VZ
178// ----------------------------------------------------------------------------
179// misc
180// ----------------------------------------------------------------------------
181
7c0ea335
VZ
182bool wxFrameBase::ProcessCommand(int id)
183{
1e6feb95 184#if wxUSE_MENUS
7c0ea335
VZ
185 wxMenuBar *bar = GetMenuBar();
186 if ( !bar )
187 return FALSE;
188
3ca6a5f0
BP
189 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
190 commandEvent.SetEventObject(this);
191
7c0ea335 192 wxMenuItem *item = bar->FindItem(id);
84f7908b 193 if (item)
7c0ea335 194 {
84f7908b
RR
195 if (!item->IsEnabled())
196 return TRUE;
d4597e13 197
84f7908b
RR
198 if (item->IsCheckable())
199 {
200 item->Toggle();
0472ece7 201
84f7908b
RR
202 // use the new value
203 commandEvent.SetInt(item->IsChecked());
204 }
3ca6a5f0 205 }
7c0ea335
VZ
206
207 return GetEventHandler()->ProcessEvent(commandEvent);
1e6feb95
VZ
208#else // !wxUSE_MENUS
209 return FALSE;
210#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
211}
212
213// ----------------------------------------------------------------------------
214// event handlers
215// ----------------------------------------------------------------------------
216
7c0ea335
VZ
217void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
218{
219#if wxUSE_STATUSBAR
f6bcfd97 220 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
221#endif // wxUSE_STATUSBAR
222}
223
6522713c
VZ
224void wxFrameBase::OnIdle(wxIdleEvent& WXUNUSED(event) )
225{
226#if wxUSE_MENUS
227 DoMenuUpdates();
228#endif // wxUSE_MENUS
229}
230
7c0ea335
VZ
231// ----------------------------------------------------------------------------
232// status bar stuff
233// ----------------------------------------------------------------------------
234
235#if wxUSE_STATUSBAR
236
237wxStatusBar* 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
254wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
255 long style,
256 wxWindowID id,
257 const wxString& name)
258{
ed791986 259 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 260
7c0ea335
VZ
261 statusBar->SetFieldsCount(number);
262
263 return statusBar;
264}
265
266void wxFrameBase::SetStatusText(const wxString& text, int number)
267{
7c0ea335
VZ
268 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
269
270 m_frameStatusBar->SetStatusText(text, number);
271}
272
273void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
274{
7c0ea335
VZ
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
1f361cdd 282void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 283{
1f361cdd
MB
284 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
285
286 m_frameStatusBar->PushStatusText(text, number);
287}
f6bcfd97 288
1f361cdd
MB
289void 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
296void 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
308bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
309{
310#if wxUSE_MENUS
f6bcfd97
BP
311 // if no help string found, we will clear the status bar text
312 wxString helpString;
1f361cdd 313 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 314
1f361cdd 315 if ( show )
f6bcfd97
BP
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
1f361cdd 328 DoGiveHelp(helpString, show);
f6bcfd97
BP
329
330 return !helpString.IsEmpty();
3379ed37
VZ
331#else // !wxUSE_MENUS
332 return FALSE;
333#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
334}
335
7c0ea335
VZ
336#endif // wxUSE_STATUSBAR
337
338// ----------------------------------------------------------------------------
339// toolbar stuff
340// ----------------------------------------------------------------------------
341
342#if wxUSE_TOOLBAR
343
344wxToolBar* 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
358wxToolBar* 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// ----------------------------------------------------------------------------
6522713c 370// menus
7c0ea335
VZ
371// ----------------------------------------------------------------------------
372
1e6feb95
VZ
373#if wxUSE_MENUS
374
63fec618 375// update all menus
7c0ea335 376void wxFrameBase::DoMenuUpdates()
63fec618 377{
54517652 378 wxMenuBar* bar = GetMenuBar();
e702ff0f 379
f0e9f0d3 380#ifdef __WXMSW__
e63fdcd6 381 wxWindow* focusWin = wxFindFocusDescendant((wxWindow*) this);
f0e9f0d3
JS
382#else
383 wxWindow* focusWin = (wxWindow*) NULL;
384#endif
7c0ea335 385 if ( bar != NULL )
54517652
RR
386 {
387 int nCount = bar->GetMenuCount();
388 for (int n = 0; n < nCount; n++)
e63fdcd6 389 DoMenuUpdates(bar->GetMenu(n), focusWin);
54517652 390 }
63fec618
VZ
391}
392
393// update a menu and all submenus recursively
e63fdcd6 394void wxFrameBase::DoMenuUpdates(wxMenu* menu, wxWindow* focusWin)
7c0ea335 395{
e63fdcd6 396 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler();
7c0ea335
VZ
397 wxMenuItemList::Node* node = menu->GetMenuItems().GetFirst();
398 while (node)
63fec618 399 {
7c0ea335
VZ
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();
63fec618 421 }
63fec618 422}
1e6feb95 423
6522713c
VZ
424void wxFrameBase::DetachMenuBar()
425{
426 if ( m_frameMenuBar )
427 {
428 m_frameMenuBar->Detach();
429 m_frameMenuBar = NULL;
430 }
431}
432
433void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
434{
435 if ( menubar )
436 {
6522713c 437 menubar->Attach((wxFrame *)this);
3dbe38c3 438 m_frameMenuBar = menubar;
6522713c
VZ
439 }
440}
441
442void 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
1e6feb95 455#endif // wxUSE_MENUS