]> git.saurik.com Git - wxWidgets.git/blame - src/common/framecmn.cpp
cocoa doesn't need system framework - string conversion for cocoa - focus for cocoa...
[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$
55d99c7a 7// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 8// Licence: wxWindows licence
63fec618
VZ
9/////////////////////////////////////////////////////////////////////////////
10
7c0ea335
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
14f355c2 19#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7c0ea335
VZ
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
44// ----------------------------------------------------------------------------
45// event table
46// ----------------------------------------------------------------------------
47
96ac065f
VZ
48#if wxUSE_MENUS && wxUSE_STATUSBAR
49
7d9f12f3 50BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
0b30bb0b 51 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
96ac065f
VZ
52 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
53
7c0ea335 54 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
7c0ea335
VZ
55END_EVENT_TABLE()
56
96ac065f
VZ
57#endif // wxUSE_MENUS && wxUSE_STATUSBAR
58
7c0ea335
VZ
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// construction/destruction
65// ----------------------------------------------------------------------------
66
67wxFrameBase::wxFrameBase()
68{
1e6feb95 69#if wxUSE_MENUS
7c0ea335 70 m_frameMenuBar = NULL;
1e6feb95 71#endif // wxUSE_MENUS
7c0ea335
VZ
72
73#if wxUSE_TOOLBAR
74 m_frameToolBar = NULL;
75#endif // wxUSE_TOOLBAR
76
77#if wxUSE_STATUSBAR
78 m_frameStatusBar = NULL;
79#endif // wxUSE_STATUSBAR
1f361cdd
MB
80
81 m_statusBarPane = 0;
7c0ea335
VZ
82}
83
799ea011
GD
84wxFrameBase::~wxFrameBase()
85{
86 // this destructor is required for Darwin
87}
88
7c0ea335
VZ
89wxFrame *wxFrameBase::New(wxWindow *parent,
90 wxWindowID id,
91 const wxString& title,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96{
97 return new wxFrame(parent, id, title, pos, size, style, name);
98}
99
100void wxFrameBase::DeleteAllBars()
101{
1e6feb95 102#if wxUSE_MENUS
7c0ea335
VZ
103 if ( m_frameMenuBar )
104 {
105 delete m_frameMenuBar;
106 m_frameMenuBar = (wxMenuBar *) NULL;
107 }
1e6feb95 108#endif // wxUSE_MENUS
7c0ea335
VZ
109
110#if wxUSE_STATUSBAR
111 if ( m_frameStatusBar )
112 {
113 delete m_frameStatusBar;
114 m_frameStatusBar = (wxStatusBar *) NULL;
115 }
116#endif // wxUSE_STATUSBAR
117
118#if wxUSE_TOOLBAR
119 if ( m_frameToolBar )
120 {
121 delete m_frameToolBar;
122 m_frameToolBar = (wxToolBar *) NULL;
123 }
124#endif // wxUSE_TOOLBAR
125}
126
1e6feb95
VZ
127bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
128{
129#if wxUSE_MENUS
130 if ( win == GetMenuBar() )
d1b20379 131 return true;
1e6feb95
VZ
132#endif // wxUSE_MENUS
133
134#if wxUSE_STATUSBAR
135 if ( win == GetStatusBar() )
d1b20379 136 return true;
1e6feb95
VZ
137#endif // wxUSE_STATUSBAR
138
139#if wxUSE_TOOLBAR
140 if ( win == GetToolBar() )
d1b20379 141 return true;
1e6feb95
VZ
142#endif // wxUSE_TOOLBAR
143
d1b20379 144 return false;
1e6feb95
VZ
145}
146
1c4f8f8d
VZ
147// ----------------------------------------------------------------------------
148// wxFrame size management: we exclude the areas taken by menu/status/toolbars
149// from the client area, so the client area is what's really available for the
150// frame contents
151// ----------------------------------------------------------------------------
152
153// get the origin of the client area in the client coordinates
154wxPoint wxFrameBase::GetClientAreaOrigin() const
155{
7d9f12f3 156 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1c4f8f8d 157
a9928e9d 158#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
d4597e13
VZ
159 wxToolBar *toolbar = GetToolBar();
160 if ( toolbar && toolbar->IsShown() )
1c4f8f8d
VZ
161 {
162 int w, h;
d4597e13 163 toolbar->GetSize(&w, &h);
1c4f8f8d 164
d4597e13 165 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
1c4f8f8d
VZ
166 {
167 pt.x += w;
168 }
169 else
170 {
171 pt.y += h;
172 }
173 }
174#endif // wxUSE_TOOLBAR
175
176 return pt;
177}
178
7c0ea335
VZ
179// ----------------------------------------------------------------------------
180// misc
181// ----------------------------------------------------------------------------
182
7c0ea335
VZ
183bool wxFrameBase::ProcessCommand(int id)
184{
1e6feb95 185#if wxUSE_MENUS
7c0ea335
VZ
186 wxMenuBar *bar = GetMenuBar();
187 if ( !bar )
d1b20379 188 return false;
7c0ea335 189
3ca6a5f0
BP
190 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
191 commandEvent.SetEventObject(this);
192
7c0ea335 193 wxMenuItem *item = bar->FindItem(id);
84f7908b 194 if (item)
7c0ea335 195 {
84f7908b 196 if (!item->IsEnabled())
d1b20379 197 return true;
d4597e13 198
84f7908b
RR
199 if (item->IsCheckable())
200 {
201 item->Toggle();
0472ece7 202
84f7908b
RR
203 // use the new value
204 commandEvent.SetInt(item->IsChecked());
205 }
3ca6a5f0 206 }
7c0ea335 207
a01fe3d6 208 GetEventHandler()->ProcessEvent(commandEvent);
d1b20379 209 return true;
1e6feb95 210#else // !wxUSE_MENUS
d1b20379 211 return false;
1e6feb95 212#endif // wxUSE_MENUS/!wxUSE_MENUS
7c0ea335
VZ
213}
214
e39af974
JS
215// Do the UI update processing for this window. This is
216// provided for the application to call if it wants to
217// force a UI update, particularly for the menus and toolbar.
218void wxFrameBase::UpdateWindowUI(long flags)
219{
220 wxWindowBase::UpdateWindowUI(flags);
a62848fd 221
e39af974
JS
222#if wxUSE_TOOLBAR
223 if (GetToolBar())
224 GetToolBar()->UpdateWindowUI(flags);
225#endif
226
227#if wxUSE_MENUS
228 if (GetMenuBar())
229 {
230 if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES)
231 {
232 // If coming from an idle event, we only
233 // want to update the menus if we're
234 // in the wxUSE_IDLEMENUUPDATES configuration:
235 // so if we're not, do nothing
236 }
237 else
238 DoMenuUpdates();
239 }
96ac065f 240#endif // wxUSE_MENUS
e39af974
JS
241}
242
7c0ea335 243// ----------------------------------------------------------------------------
96ac065f 244// event handlers for status bar updates from menus
7c0ea335
VZ
245// ----------------------------------------------------------------------------
246
96ac065f
VZ
247#if wxUSE_MENUS && wxUSE_STATUSBAR
248
7c0ea335
VZ
249void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
250{
251#if wxUSE_STATUSBAR
f6bcfd97 252 (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId());
7c0ea335
VZ
253#endif // wxUSE_STATUSBAR
254}
255
d1b20379 256#if !wxUSE_IDLEMENUUPDATES
96ac065f 257void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
d1b20379
DS
258#else
259void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event))
260#endif
96ac065f
VZ
261{
262#if !wxUSE_IDLEMENUUPDATES
263 DoMenuUpdates(event.GetMenu());
264#endif // !wxUSE_IDLEMENUUPDATES
265}
266
267void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
268{
269 // do we have real status text to restore?
270 if ( m_oldStatusText.length() > 1 || m_oldStatusText[0u] )
271 {
272 if ( m_statusBarPane >= 0 )
273 {
274 wxStatusBar *statbar = GetStatusBar();
275 if ( statbar )
276 statbar->SetStatusText(m_oldStatusText, m_statusBarPane);
277 }
278
279 m_oldStatusText.clear();
280 }
281}
282
283#endif // wxUSE_MENUS && wxUSE_STATUSBAR
284
e39af974
JS
285// Implement internal behaviour (menu updating on some platforms)
286void wxFrameBase::OnInternalIdle()
6522713c 287{
e2b6d07d 288 wxTopLevelWindow::OnInternalIdle();
a62848fd 289
0b30bb0b 290#if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
e39af974 291 if (wxUpdateUIEvent::CanUpdate(this))
0b30bb0b
JS
292 DoMenuUpdates();
293#endif
294}
295
7c0ea335
VZ
296// ----------------------------------------------------------------------------
297// status bar stuff
298// ----------------------------------------------------------------------------
299
300#if wxUSE_STATUSBAR
301
302wxStatusBar* wxFrameBase::CreateStatusBar(int number,
303 long style,
304 wxWindowID id,
305 const wxString& name)
306{
307 // the main status bar can only be created once (or else it should be
308 // deleted before calling CreateStatusBar() again)
309 wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL,
310 wxT("recreating status bar in wxFrame") );
311
a4f01f05 312 SetStatusBar(OnCreateStatusBar(number, style, id, name));
7c0ea335
VZ
313
314 return m_frameStatusBar;
315}
316
317wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
318 long style,
319 wxWindowID id,
320 const wxString& name)
321{
ed791986 322 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
7c0ea335 323
7c0ea335
VZ
324 statusBar->SetFieldsCount(number);
325
326 return statusBar;
327}
328
329void wxFrameBase::SetStatusText(const wxString& text, int number)
330{
7c0ea335
VZ
331 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
332
333 m_frameStatusBar->SetStatusText(text, number);
334}
335
336void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
337{
7c0ea335
VZ
338 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
339
340 m_frameStatusBar->SetStatusWidths(n, widths_field);
341
342 PositionStatusBar();
343}
344
1f361cdd 345void wxFrameBase::PushStatusText(const wxString& text, int number)
f6bcfd97 346{
1f361cdd
MB
347 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
348
349 m_frameStatusBar->PushStatusText(text, number);
350}
f6bcfd97 351
1f361cdd
MB
352void wxFrameBase::PopStatusText(int number)
353{
354 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
355
356 m_frameStatusBar->PopStatusText(number);
357}
358
1f361cdd
MB
359bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId)
360{
361#if wxUSE_MENUS
f6bcfd97
BP
362 // if no help string found, we will clear the status bar text
363 wxString helpString;
1f361cdd 364 bool show = menuId != wxID_SEPARATOR && menuId != -2 /* wxID_TITLE */;
f6bcfd97 365
1f361cdd 366 if ( show )
f6bcfd97
BP
367 {
368 wxMenuBar *menuBar = GetMenuBar();
369 if ( menuBar )
370 {
371 // it's ok if we don't find the item because it might belong
372 // to the popup menu
373 wxMenuItem *item = menuBar->FindItem(menuId);
374 if ( item )
375 helpString = item->GetHelp();
376 }
377 }
378
1f361cdd 379 DoGiveHelp(helpString, show);
f6bcfd97
BP
380
381 return !helpString.IsEmpty();
3379ed37 382#else // !wxUSE_MENUS
d1b20379 383 return false;
3379ed37 384#endif // wxUSE_MENUS/!wxUSE_MENUS
f6bcfd97
BP
385}
386
a4f01f05
VZ
387void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
388{
389 bool hadBar = m_frameStatusBar != NULL;
390 m_frameStatusBar = statBar;
391
392 if ( (m_frameStatusBar != NULL) != hadBar )
393 {
394 PositionStatusBar();
395
396 DoLayout();
397 }
398}
399
7c0ea335
VZ
400#endif // wxUSE_STATUSBAR
401
c60a36d5
VZ
402void wxFrameBase::DoGiveHelp(const wxString& text, bool show)
403{
404#if wxUSE_STATUSBAR
96ac065f
VZ
405 if ( m_statusBarPane < 0 )
406 {
407 // status bar messages disabled
408 return;
409 }
410
411 wxStatusBar *statbar = GetStatusBar();
412 if ( !statbar )
413 return;
414
415 wxString help;
416 if ( show )
417 help = text;
418
419 // remember the old status bar text if this is the first time we're called
420 // since the menu has been opened as we're going to overwrite it in our
421 // DoGiveHelp() and we want to restore it when the menu is closed
422 //
423 // note that it would be logical to do this in OnMenuOpen() but under MSW
424 // we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely enough, and
425 // so this doesn't work and instead we use the ugly trick with using
426 // special m_oldStatusText value as "menu opened" (but it is arguably
427 // better than adding yet another member variable to wxFrame on all
428 // platforms)
429 if ( m_oldStatusText.empty() )
430 {
431 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
432 if ( m_oldStatusText.empty() )
433 {
434 // use special value to prevent us from doing this the next time
435 m_oldStatusText += _T('\0');
436 }
437 }
c60a36d5 438
96ac065f 439 statbar->SetStatusText(help, m_statusBarPane);
f428e6c5
WS
440#else
441 wxUnusedVar(text);
442 wxUnusedVar(show);
c60a36d5
VZ
443#endif // wxUSE_STATUSBAR
444}
445
446
7c0ea335
VZ
447// ----------------------------------------------------------------------------
448// toolbar stuff
449// ----------------------------------------------------------------------------
450
451#if wxUSE_TOOLBAR
452
453wxToolBar* wxFrameBase::CreateToolBar(long style,
454 wxWindowID id,
455 const wxString& name)
456{
457 // the main toolbar can't be recreated (unless it was explicitly deeleted
458 // before)
459 wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL,
460 wxT("recreating toolbar in wxFrame") );
461
f9dae779
VZ
462 if ( style == -1 )
463 {
464 // use default style
465 //
466 // NB: we don't specify the default value in the method declaration
467 // because
468 // a) this allows us to have different defaults for different
469 // platforms (even if we don't have them right now)
470 // b) we don't need to include wx/toolbar.h in the header then
471 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
472 }
473
a4f01f05 474 SetToolBar(OnCreateToolBar(style, id, name));
7c0ea335
VZ
475
476 return m_frameToolBar;
477}
478
479wxToolBar* wxFrameBase::OnCreateToolBar(long style,
480 wxWindowID id,
481 const wxString& name)
482{
483 return new wxToolBar(this, id,
484 wxDefaultPosition, wxDefaultSize,
485 style, name);
486}
487
a4f01f05
VZ
488void wxFrameBase::SetToolBar(wxToolBar *toolbar)
489{
490 bool hadBar = m_frameToolBar != NULL;
491 m_frameToolBar = toolbar;
492
493 if ( (m_frameToolBar != NULL) != hadBar )
494 {
495 PositionToolBar();
496
497 DoLayout();
498 }
499}
500
7c0ea335
VZ
501#endif // wxUSE_TOOLBAR
502
503// ----------------------------------------------------------------------------
6522713c 504// menus
7c0ea335
VZ
505// ----------------------------------------------------------------------------
506
1e6feb95
VZ
507#if wxUSE_MENUS
508
63fec618 509// update all menus
92f1a59c 510void wxFrameBase::DoMenuUpdates(wxMenu* menu)
63fec618 511{
92f1a59c 512 wxEvtHandler* source = GetEventHandler();
54517652 513 wxMenuBar* bar = GetMenuBar();
e702ff0f 514
92f1a59c
JS
515 if (menu)
516 menu->UpdateUI(source);
517 else if ( bar != NULL )
54517652
RR
518 {
519 int nCount = bar->GetMenuCount();
520 for (int n = 0; n < nCount; n++)
18afa2ac 521 bar->GetMenu(n)->UpdateUI(source);
63fec618 522 }
63fec618 523}
1e6feb95 524
6522713c
VZ
525void wxFrameBase::DetachMenuBar()
526{
527 if ( m_frameMenuBar )
528 {
529 m_frameMenuBar->Detach();
530 m_frameMenuBar = NULL;
531 }
532}
533
534void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
535{
536 if ( menubar )
537 {
6522713c 538 menubar->Attach((wxFrame *)this);
3dbe38c3 539 m_frameMenuBar = menubar;
6522713c
VZ
540 }
541}
542
543void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
544{
545 if ( menubar == GetMenuBar() )
546 {
547 // nothing to do
548 return;
549 }
550
551 DetachMenuBar();
552
a96b4743 553 this->AttachMenuBar(menubar);
6522713c
VZ
554}
555
1e6feb95 556#endif // wxUSE_MENUS