]>
git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/menuitem.h"
32 #include "wx/dcclient.h"
33 #include "wx/toolbar.h"
34 #include "wx/statusbr.h"
37 extern WXDLLEXPORT_DATA(const char) wxFrameNameStr
[] = "frame";
38 extern WXDLLEXPORT_DATA(const char) wxStatusLineNameStr
[] = "status_line";
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(wxFrameBase
, wxTopLevelWindow
)
48 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen
)
49 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose
)
51 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight
)
53 #endif // wxUSE_STATUSBAR
56 bool wxFrameBase::ShouldUpdateMenuFromIdle()
58 // Usually this is determined at compile time and is determined by whether
59 // the platform supports wxEVT_MENU_OPEN, however in wxGTK we need to also
60 // check if we're using the global menu bar as we don't get EVT_MENU_OPEN
61 // for it and need to fall back to idle time updating even if normally
62 // wxUSE_IDLEMENUUPDATES is set to 0 for wxGTK.
64 if ( wxApp::GTKIsUsingGlobalMenu() )
68 return wxUSE_IDLEMENUUPDATES
!= 0;
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 wxDEFINE_FLAGS( wxFrameStyle
)
82 wxBEGIN_FLAGS( wxFrameStyle
)
83 // new style border flags, we put them first to
84 // use them for streaming out
85 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
86 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
87 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
88 wxFLAGS_MEMBER(wxBORDER_RAISED
)
89 wxFLAGS_MEMBER(wxBORDER_STATIC
)
90 wxFLAGS_MEMBER(wxBORDER_NONE
)
92 // old style border flags
93 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
94 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
95 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
96 wxFLAGS_MEMBER(wxRAISED_BORDER
)
97 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
98 wxFLAGS_MEMBER(wxBORDER
)
100 // standard window styles
101 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
102 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
103 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
104 wxFLAGS_MEMBER(wxWANTS_CHARS
)
105 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
106 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
107 wxFLAGS_MEMBER(wxVSCROLL
)
108 wxFLAGS_MEMBER(wxHSCROLL
)
111 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
112 wxFLAGS_MEMBER(wxCAPTION
)
113 #if WXWIN_COMPATIBILITY_2_6
114 wxFLAGS_MEMBER(wxTHICK_FRAME
)
115 #endif // WXWIN_COMPATIBILITY_2_6
116 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
117 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
118 #if WXWIN_COMPATIBILITY_2_6
119 wxFLAGS_MEMBER(wxRESIZE_BOX
)
120 #endif // WXWIN_COMPATIBILITY_2_6
121 wxFLAGS_MEMBER(wxCLOSE_BOX
)
122 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
123 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
125 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW
)
126 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT
)
128 wxFLAGS_MEMBER(wxFRAME_SHAPED
)
129 wxEND_FLAGS( wxFrameStyle
)
131 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame
, wxTopLevelWindow
, "wx/frame.h")
133 wxBEGIN_PROPERTIES_TABLE(wxFrame
)
134 wxEVENT_PROPERTY( Menu
, wxEVT_MENU
, wxCommandEvent
)
136 wxPROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/, \
137 wxT("Helpstring"), wxT("group"))
138 wxPROPERTY_FLAGS( WindowStyle
, wxFrameStyle
, long, SetWindowStyleFlag
, \
139 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
140 wxT("Helpstring"), wxT("group")) // style
142 wxPROPERTY( MenuBar
, wxMenuBar
*, SetMenuBar
, GetMenuBar
, wxEMPTY_PARAMETER_VALUE
, \
143 0 /*flags*/, wxT("Helpstring"), wxT("group"))
145 wxEND_PROPERTIES_TABLE()
147 wxEMPTY_HANDLERS_TABLE(wxFrame
)
149 wxCONSTRUCTOR_6( wxFrame
, wxWindow
*, Parent
, wxWindowID
, Id
, wxString
, Title
, \
150 wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
152 // ----------------------------------------------------------------------------
153 // construction/destruction
154 // ----------------------------------------------------------------------------
156 wxFrameBase::wxFrameBase()
159 m_frameMenuBar
= NULL
;
160 #endif // wxUSE_MENUS
163 m_frameToolBar
= NULL
;
164 #endif // wxUSE_TOOLBAR
167 m_frameStatusBar
= NULL
;
168 #endif // wxUSE_STATUSBAR
173 wxFrameBase::~wxFrameBase()
175 // this destructor is required for Darwin
178 wxFrame
*wxFrameBase::New(wxWindow
*parent
,
180 const wxString
& title
,
184 const wxString
& name
)
186 return new wxFrame(parent
, id
, title
, pos
, size
, style
, name
);
189 void wxFrameBase::DeleteAllBars()
192 wxDELETE(m_frameMenuBar
);
193 #endif // wxUSE_MENUS
196 wxDELETE(m_frameStatusBar
);
197 #endif // wxUSE_STATUSBAR
200 wxDELETE(m_frameToolBar
);
201 #endif // wxUSE_TOOLBAR
204 bool wxFrameBase::IsOneOfBars(const wxWindow
*win
) const
207 if ( win
== GetMenuBar() )
209 #endif // wxUSE_MENUS
212 if ( win
== GetStatusBar() )
214 #endif // wxUSE_STATUSBAR
217 if ( win
== GetToolBar() )
219 #endif // wxUSE_TOOLBAR
226 // ----------------------------------------------------------------------------
227 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
228 // from the client area, so the client area is what's really available for the
230 // ----------------------------------------------------------------------------
232 // get the origin of the client area in the client coordinates
233 wxPoint
wxFrameBase::GetClientAreaOrigin() const
235 wxPoint pt
= wxTopLevelWindow::GetClientAreaOrigin();
237 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
238 wxToolBar
*toolbar
= GetToolBar();
239 if ( toolbar
&& toolbar
->IsShown() )
242 toolbar
->GetSize(&w
, &h
);
244 if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
253 #endif // wxUSE_TOOLBAR
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
264 bool wxFrameBase::ProcessCommand(int id
)
266 wxMenuItem
* const item
= FindItemInMenuBar(id
);
270 return ProcessCommand(item
);
273 bool wxFrameBase::ProcessCommand(wxMenuItem
*item
)
275 wxCHECK_MSG( item
, false, wxS("Menu item can't be NULL") );
277 if (!item
->IsEnabled())
280 if ((item
->GetKind() == wxITEM_RADIO
) && item
->IsChecked() )
284 if (item
->IsCheckable())
289 checked
= item
->IsChecked();
291 else // Uncheckable item.
296 wxMenu
* const menu
= item
->GetMenu();
297 wxCHECK_MSG( menu
, false, wxS("Menu item should be attached to a menu") );
299 return menu
->SendEvent(item
->GetId(), checked
);
302 #endif // wxUSE_MENUS
304 // Do the UI update processing for this window. This is
305 // provided for the application to call if it wants to
306 // force a UI update, particularly for the menus and toolbar.
307 void wxFrameBase::UpdateWindowUI(long flags
)
309 wxWindowBase::UpdateWindowUI(flags
);
313 GetToolBar()->UpdateWindowUI(flags
);
319 // If coming from an idle event, we only want to update the menus if
320 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
321 // will be update when the menu is opened later
322 if ( !(flags
& wxUPDATE_UI_FROMIDLE
) || ShouldUpdateMenuFromIdle() )
325 #endif // wxUSE_MENUS
328 // ----------------------------------------------------------------------------
329 // event handlers for status bar updates from menus
330 // ----------------------------------------------------------------------------
332 #if wxUSE_MENUS && wxUSE_STATUSBAR
334 void wxFrameBase::OnMenuHighlight(wxMenuEvent
& event
)
337 (void)ShowMenuHelp(event
.GetMenuId());
338 #endif // wxUSE_STATUSBAR
341 void wxFrameBase::OnMenuOpen(wxMenuEvent
& event
)
343 if ( !ShouldUpdateMenuFromIdle() )
345 // as we didn't update the menus from idle time, do it now
346 DoMenuUpdates(event
.GetMenu());
350 void wxFrameBase::OnMenuClose(wxMenuEvent
& WXUNUSED(event
))
352 DoGiveHelp(wxEmptyString
, false);
355 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
357 // Implement internal behaviour (menu updating on some platforms)
358 void wxFrameBase::OnInternalIdle()
360 wxTopLevelWindow::OnInternalIdle();
363 if ( ShouldUpdateMenuFromIdle() && wxUpdateUIEvent::CanUpdate(this) )
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
374 wxStatusBar
* wxFrameBase::CreateStatusBar(int number
,
377 const wxString
& name
)
379 // the main status bar can only be created once (or else it should be
380 // deleted before calling CreateStatusBar() again)
381 wxCHECK_MSG( !m_frameStatusBar
, NULL
,
382 wxT("recreating status bar in wxFrame") );
384 SetStatusBar(OnCreateStatusBar(number
, style
, id
, name
));
386 return m_frameStatusBar
;
389 wxStatusBar
*wxFrameBase::OnCreateStatusBar(int number
,
392 const wxString
& name
)
394 wxStatusBar
*statusBar
= new wxStatusBar(this, id
, style
, name
);
396 statusBar
->SetFieldsCount(number
);
401 void wxFrameBase::SetStatusText(const wxString
& text
, int number
)
403 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
405 m_frameStatusBar
->SetStatusText(text
, number
);
408 void wxFrameBase::SetStatusWidths(int n
, const int widths_field
[] )
410 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set widths for") );
412 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
417 void wxFrameBase::PushStatusText(const wxString
& text
, int number
)
419 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
421 m_frameStatusBar
->PushStatusText(text
, number
);
424 void wxFrameBase::PopStatusText(int number
)
426 wxCHECK_RET( m_frameStatusBar
!= NULL
, wxT("no statusbar to set text for") );
428 m_frameStatusBar
->PopStatusText(number
);
431 bool wxFrameBase::ShowMenuHelp(int menuId
)
434 // if no help string found, we will clear the status bar text
436 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
438 if ( menuId
!= wxID_SEPARATOR
&& menuId
!= wxID_NONE
)
440 const wxMenuItem
* const item
= FindItemInMenuBar(menuId
);
441 if ( item
&& !item
->IsSeparator() )
442 helpString
= item
->GetHelp();
444 // notice that it's ok if we don't find the item because it might
445 // belong to the popup menu, so don't assert here
448 DoGiveHelp(helpString
, true);
450 return !helpString
.empty();
451 #else // !wxUSE_MENUS
453 #endif // wxUSE_MENUS/!wxUSE_MENUS
456 void wxFrameBase::SetStatusBar(wxStatusBar
*statBar
)
458 bool hadBar
= m_frameStatusBar
!= NULL
;
459 m_frameStatusBar
= statBar
;
461 if ( (m_frameStatusBar
!= NULL
) != hadBar
)
469 #endif // wxUSE_STATUSBAR
471 #if wxUSE_MENUS || wxUSE_TOOLBAR
472 void wxFrameBase::DoGiveHelp(const wxString
& help
, bool show
)
475 if ( m_statusBarPane
< 0 )
477 // status bar messages disabled
481 wxStatusBar
*statbar
= GetStatusBar();
488 // remember the old status bar text if this is the first time we're
489 // called since the menu has been opened as we're going to overwrite it
490 // in our DoGiveHelp() and we want to restore it when the menu is
493 // note that it would be logical to do this in OnMenuOpen() but under
494 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
495 // enough, and so this doesn't work and instead we use the ugly trick
496 // with using special m_oldStatusText value as "menu opened" (but it is
497 // arguably better than adding yet another member variable to wxFrame
499 if ( m_oldStatusText
.empty() )
501 m_oldStatusText
= statbar
->GetStatusText(m_statusBarPane
);
502 if ( m_oldStatusText
.empty() )
504 // use special value to prevent us from doing this the next time
505 m_oldStatusText
+= wxT('\0');
512 else // hide help, restore the original text
514 // clear the last shown help string but remember its value
515 wxString lastHelpShown
;
516 lastHelpShown
.swap(m_lastHelpShown
);
518 // also clear the old status text but remember it too to restore it
520 text
.swap(m_oldStatusText
);
522 if ( statbar
->GetStatusText(m_statusBarPane
) != lastHelpShown
)
524 // if the text was changed with an explicit SetStatusText() call
525 // from the user code in the meanwhile, do not overwrite it with
526 // the old status bar contents -- this is almost certainly not what
527 // the user expects and would be very hard to avoid from user code
532 statbar
->SetStatusText(text
, m_statusBarPane
);
536 #endif // wxUSE_STATUSBAR
538 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
547 wxToolBar
* wxFrameBase::CreateToolBar(long style
,
549 const wxString
& name
)
551 // the main toolbar can't be recreated (unless it was explicitly deleted
553 wxCHECK_MSG( !m_frameToolBar
, NULL
,
554 wxT("recreating toolbar in wxFrame") );
560 // NB: we don't specify the default value in the method declaration
562 // a) this allows us to have different defaults for different
563 // platforms (even if we don't have them right now)
564 // b) we don't need to include wx/toolbar.h in the header then
565 style
= wxTB_DEFAULT_STYLE
;
568 SetToolBar(OnCreateToolBar(style
, id
, name
));
570 return m_frameToolBar
;
573 wxToolBar
* wxFrameBase::OnCreateToolBar(long style
,
575 const wxString
& name
)
577 #if defined(__WXWINCE__) && defined(__POCKETPC__)
578 return new wxToolMenuBar(this, id
,
579 wxDefaultPosition
, wxDefaultSize
,
582 return new wxToolBar(this, id
,
583 wxDefaultPosition
, wxDefaultSize
,
588 void wxFrameBase::SetToolBar(wxToolBar
*toolbar
)
590 if ( (toolbar
!= NULL
) != (m_frameToolBar
!= NULL
) )
592 // the toolbar visibility must have changed so we need to both position
593 // the toolbar itself (if it appeared) and to relayout the frame
594 // contents in any case
598 // we need to assign it to m_frameToolBar for PositionToolBar() to
600 m_frameToolBar
= toolbar
;
603 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
604 // wouldn't recognize the (still existing) toolbar as one of our
605 // bars and wouldn't layout the single child of the frame correctly
608 // and this is even more tricky: we want DoLayout() to recognize the
609 // old toolbar for the purpose of not counting it among our non-bar
610 // children but we don't want to reserve any more space for it so we
611 // temporarily hide it
612 if ( m_frameToolBar
)
613 m_frameToolBar
->Hide();
617 if ( m_frameToolBar
)
618 m_frameToolBar
->Show();
621 // this might have been already done above but it's simpler to just always
622 // do it unconditionally instead of testing for whether we already did it
623 m_frameToolBar
= toolbar
;
626 #endif // wxUSE_TOOLBAR
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
635 void wxFrameBase::DoMenuUpdates(wxMenu
* menu
)
639 wxEvtHandler
* source
= GetEventHandler();
640 menu
->UpdateUI(source
);
644 wxMenuBar
* bar
= GetMenuBar();
650 void wxFrameBase::DetachMenuBar()
652 if ( m_frameMenuBar
)
654 m_frameMenuBar
->Detach();
655 m_frameMenuBar
= NULL
;
659 void wxFrameBase::AttachMenuBar(wxMenuBar
*menubar
)
663 menubar
->Attach((wxFrame
*)this);
664 m_frameMenuBar
= menubar
;
668 void wxFrameBase::SetMenuBar(wxMenuBar
*menubar
)
670 if ( menubar
== GetMenuBar() )
678 this->AttachMenuBar(menubar
);
681 wxMenuItem
*wxFrameBase::FindItemInMenuBar(int menuId
) const
683 const wxMenuBar
* const menuBar
= GetMenuBar();
685 return menuBar
? menuBar
->FindItem(menuId
) : NULL
;
688 #endif // wxUSE_MENUS