1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
32 #include "wx/window.h"
33 #include "wx/control.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobut.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
38 #include "wx/settings.h"
39 #include "wx/dialog.h"
40 #include "wx/msgdlg.h"
41 #include "wx/statusbr.h"
42 #include "wx/toolbar.h"
43 #include "wx/dcclient.h"
44 #include "wx/scrolbar.h"
45 #include "wx/layout.h"
49 #if wxUSE_DRAG_AND_DROP
51 #endif // wxUSE_DRAG_AND_DROP
53 #if wxUSE_ACCESSIBILITY
54 #include "wx/access.h"
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 #if wxUSE_SYSTEM_OPTIONS
70 #include "wx/sysopt.h"
73 // For reporting compile- and runtime version of GTK+ in the ctrl+alt+mclick dialog.
74 // The gtk includes don't pull any other headers in, at least not on my system - MR
77 #include <gtk/gtkversion.h>
79 #include <gtk/gtkfeatures.h>
83 #include "wx/platinfo.h"
86 WXDLLIMPEXP_DATA_CORE(wxWindowList
) wxTopLevelWindows
;
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
93 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
100 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
101 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
102 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
105 EVT_HELP(wxID_ANY
, wxWindowBase::OnHelp
)
110 // ============================================================================
111 // implementation of the common functionality of the wxWindow class
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // the default initialization
119 wxWindowBase::wxWindowBase()
121 // no window yet, no parent nor children
122 m_parent
= (wxWindow
*)NULL
;
123 m_windowId
= wxID_ANY
;
125 // no constraints on the minimal window size
127 m_maxWidth
= wxDefaultCoord
;
129 m_maxHeight
= wxDefaultCoord
;
131 // invalidiated cache value
132 m_bestSizeCache
= wxDefaultSize
;
134 // window are created enabled and visible by default
138 // the default event handler is just this window
139 m_eventHandler
= this;
143 m_windowValidator
= (wxValidator
*) NULL
;
144 #endif // wxUSE_VALIDATORS
146 // the colours/fonts are default for now, so leave m_font,
147 // m_backgroundColour and m_foregroundColour uninitialized and set those
153 m_inheritFont
= false;
159 m_backgroundStyle
= wxBG_STYLE_SYSTEM
;
161 #if wxUSE_CONSTRAINTS
162 // no constraints whatsoever
163 m_constraints
= (wxLayoutConstraints
*) NULL
;
164 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
165 #endif // wxUSE_CONSTRAINTS
167 m_windowSizer
= (wxSizer
*) NULL
;
168 m_containingSizer
= (wxSizer
*) NULL
;
169 m_autoLayout
= false;
172 #if wxUSE_DRAG_AND_DROP
173 m_dropTarget
= (wxDropTarget
*)NULL
;
174 #endif // wxUSE_DRAG_AND_DROP
177 m_tooltip
= (wxToolTip
*)NULL
;
178 #endif // wxUSE_TOOLTIPS
181 m_caret
= (wxCaret
*)NULL
;
182 #endif // wxUSE_CARET
185 m_hasCustomPalette
= false;
186 #endif // wxUSE_PALETTE
188 #if wxUSE_ACCESSIBILITY
192 m_virtualSize
= wxDefaultSize
;
194 m_scrollHelper
= (wxScrollHelper
*) NULL
;
196 m_windowVariant
= wxWINDOW_VARIANT_NORMAL
;
197 #if wxUSE_SYSTEM_OPTIONS
198 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT
) )
200 m_windowVariant
= (wxWindowVariant
) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT
) ;
204 // Whether we're using the current theme for this window (wxGTK only for now)
205 m_themeEnabled
= false;
207 // VZ: this one shouldn't exist...
208 m_isBeingDeleted
= false;
211 // common part of window creation process
212 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
214 const wxPoint
& WXUNUSED(pos
),
215 const wxSize
& WXUNUSED(size
),
217 const wxValidator
& wxVALIDATOR_PARAM(validator
),
218 const wxString
& name
)
221 // wxGTK doesn't allow to create controls with static box as the parent so
222 // this will result in a crash when the program is ported to wxGTK so warn
225 // if you get this assert, the correct solution is to create the controls
226 // as siblings of the static box
227 wxASSERT_MSG( !parent
|| !wxDynamicCast(parent
, wxStaticBox
),
228 _T("wxStaticBox can't be used as a window parent!") );
229 #endif // wxUSE_STATBOX
231 // ids are limited to 16 bits under MSW so if you care about portability,
232 // it's not a good idea to use ids out of this range (and negative ids are
233 // reserved for wxWidgets own usage)
234 wxASSERT_MSG( id
== wxID_ANY
|| (id
>= 0 && id
< 32767) ||
235 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
236 _T("invalid id value") );
238 // generate a new id if the user doesn't care about it
239 if ( id
== wxID_ANY
)
241 m_windowId
= NewControlId();
243 // remember to call ReleaseControlId() when this window is destroyed
246 else // valid id specified
251 // don't use SetWindowStyleFlag() here, this function should only be called
252 // to change the flag after creation as it tries to reflect the changes in
253 // flags by updating the window dynamically and we don't need this here
254 m_windowStyle
= style
;
260 SetValidator(validator
);
261 #endif // wxUSE_VALIDATORS
263 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
264 // have it too - like this it's possible to set it only in the top level
265 // dialog/frame and all children will inherit it by defult
266 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
268 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY
);
274 bool wxWindowBase::ToggleWindowStyle(int flag
)
276 wxASSERT_MSG( flag
, _T("flags with 0 value can't be toggled") );
279 long style
= GetWindowStyleFlag();
285 else // currently off
291 SetWindowStyleFlag(style
);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
301 wxWindowBase::~wxWindowBase()
303 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
305 // mark the id as unused if we allocated it for this control
307 ReleaseControlId(m_windowId
);
309 // FIXME if these 2 cases result from programming errors in the user code
310 // we should probably assert here instead of silently fixing them
312 // Just in case the window has been Closed, but we're then deleting
313 // immediately: don't leave dangling pointers.
314 wxPendingDelete
.DeleteObject(this);
316 // Just in case we've loaded a top-level window via LoadNativeDialog but
317 // we weren't a dialog class
318 wxTopLevelWindows
.DeleteObject((wxWindow
*)this);
320 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
322 // notify the parent about this window destruction
324 m_parent
->RemoveChild(this);
328 #endif // wxUSE_CARET
331 delete m_windowValidator
;
332 #endif // wxUSE_VALIDATORS
334 #if wxUSE_CONSTRAINTS
335 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
336 // at deleted windows as they delete themselves.
337 DeleteRelatedConstraints();
341 // This removes any dangling pointers to this window in other windows'
342 // constraintsInvolvedIn lists.
343 UnsetConstraints(m_constraints
);
344 delete m_constraints
;
345 m_constraints
= NULL
;
347 #endif // wxUSE_CONSTRAINTS
349 if ( m_containingSizer
)
350 m_containingSizer
->Detach( (wxWindow
*)this );
352 delete m_windowSizer
;
354 #if wxUSE_DRAG_AND_DROP
356 #endif // wxUSE_DRAG_AND_DROP
360 #endif // wxUSE_TOOLTIPS
362 #if wxUSE_ACCESSIBILITY
367 void wxWindowBase::SendDestroyEvent()
369 wxWindowDestroyEvent event
;
370 event
.SetEventObject(this);
371 event
.SetId(GetId());
372 GetEventHandler()->ProcessEvent(event
);
375 bool wxWindowBase::Destroy()
382 bool wxWindowBase::Close(bool force
)
384 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
385 event
.SetEventObject(this);
386 event
.SetCanVeto(!force
);
388 // return false if window wasn't closed because the application vetoed the
390 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
393 bool wxWindowBase::DestroyChildren()
395 wxWindowList::compatibility_iterator node
;
398 // we iterate until the list becomes empty
399 node
= GetChildren().GetFirst();
403 wxWindow
*child
= node
->GetData();
405 // note that we really want to call delete and not ->Destroy() here
406 // because we want to delete the child immediately, before we are
407 // deleted, and delayed deletion would result in problems as our (top
408 // level) child could outlive its parent
411 wxASSERT_MSG( !GetChildren().Find(child
),
412 wxT("child didn't remove itself using RemoveChild()") );
418 // ----------------------------------------------------------------------------
419 // size/position related methods
420 // ----------------------------------------------------------------------------
422 // centre the window with respect to its parent in either (or both) directions
423 void wxWindowBase::DoCentre(int dir
)
425 wxCHECK_RET( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent(),
426 _T("this method only implements centering child windows") );
428 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir
));
431 // fits the window around the children
432 void wxWindowBase::Fit()
434 if ( !GetChildren().empty() )
436 SetSize(GetBestSize());
438 //else: do nothing if we have no children
441 // fits virtual size (ie. scrolled area etc.) around children
442 void wxWindowBase::FitInside()
444 if ( GetChildren().GetCount() > 0 )
446 SetVirtualSize( GetBestVirtualSize() );
450 // On Mac, scrollbars are explicitly children.
452 static bool wxHasRealChildren(const wxWindowBase
* win
)
454 int realChildCount
= 0;
456 for ( wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
458 node
= node
->GetNext() )
460 wxWindow
*win
= node
->GetData();
461 if ( !win
->IsTopLevel() && win
->IsShown() && !win
->IsKindOf(CLASSINFO(wxScrollBar
)))
464 return (realChildCount
> 0);
468 void wxWindowBase::InvalidateBestSize()
470 m_bestSizeCache
= wxDefaultSize
;
472 // parent's best size calculation may depend on its children's
473 // as long as child window we are in is not top level window itself
474 // (because the TLW size is never resized automatically)
475 // so let's invalidate it as well to be safe:
476 if (m_parent
&& !IsTopLevel())
477 m_parent
->InvalidateBestSize();
480 // return the size best suited for the current window
481 wxSize
wxWindowBase::DoGetBestSize() const
487 best
= m_windowSizer
->GetMinSize();
489 #if wxUSE_CONSTRAINTS
490 else if ( m_constraints
)
492 wxConstCast(this, wxWindowBase
)->SatisfyConstraints();
494 // our minimal acceptable size is such that all our windows fit inside
498 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
500 node
= node
->GetNext() )
502 wxLayoutConstraints
*c
= node
->GetData()->GetConstraints();
505 // it's not normal that we have an unconstrained child, but
506 // what can we do about it?
510 int x
= c
->right
.GetValue(),
511 y
= c
->bottom
.GetValue();
519 // TODO: we must calculate the overlaps somehow, otherwise we
520 // will never return a size bigger than the current one :-(
523 best
= wxSize(maxX
, maxY
);
525 #endif // wxUSE_CONSTRAINTS
526 else if ( !GetChildren().empty()
528 && wxHasRealChildren(this)
532 // our minimal acceptable size is such that all our visible child
533 // windows fit inside
537 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
539 node
= node
->GetNext() )
541 wxWindow
*win
= node
->GetData();
542 if ( win
->IsTopLevel()
545 || wxDynamicCast(win
, wxStatusBar
)
546 #endif // wxUSE_STATUSBAR
549 // dialogs and frames lie in different top level windows -
550 // don't deal with them here; as for the status bars, they
551 // don't lie in the client area at all
556 win
->GetPosition(&wx
, &wy
);
558 // if the window hadn't been positioned yet, assume that it is in
560 if ( wx
== wxDefaultCoord
)
562 if ( wy
== wxDefaultCoord
)
565 win
->GetSize(&ww
, &wh
);
566 if ( wx
+ ww
> maxX
)
568 if ( wy
+ wh
> maxY
)
572 best
= wxSize(maxX
, maxY
);
574 else // ! has children
576 // for a generic window there is no natural best size so, if the
577 // minimal size is not set, use the current size but take care to
578 // remember it as minimal size for the next time because our best size
579 // should be constant: otherwise we could get into a situation when the
580 // window is initially at some size, then expanded to a larger size and
581 // then, when the containing window is shrunk back (because our initial
582 // best size had been used for computing the parent min size), we can't
583 // be shrunk back any more because our best size is now bigger
584 wxSize size
= GetMinSize();
585 if ( !size
.IsFullySpecified() )
587 size
.SetDefaults(GetSize());
588 wxConstCast(this, wxWindowBase
)->SetMinSize(size
);
591 // return as-is, unadjusted by the client size difference.
595 // Add any difference between size and client size
596 wxSize diff
= GetSize() - GetClientSize();
597 best
.x
+= wxMax(0, diff
.x
);
598 best
.y
+= wxMax(0, diff
.y
);
603 // helper of GetWindowBorderSize(): as many ports don't implement support for
604 // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
605 // fallbacks in this case
606 static int wxGetMetricOrDefault(wxSystemMetric what
)
608 int rc
= wxSystemSettings::GetMetric(what
);
615 // 2D border is by default 1 pixel wide
621 // 3D borders are by default 2 pixels
626 wxFAIL_MSG( _T("unexpected wxGetMetricOrDefault() argument") );
634 wxSize
wxWindowBase::GetWindowBorderSize() const
638 switch ( GetBorder() )
641 // nothing to do, size is already (0, 0)
644 case wxBORDER_SIMPLE
:
645 case wxBORDER_STATIC
:
646 size
.x
= wxGetMetricOrDefault(wxSYS_BORDER_X
);
647 size
.y
= wxGetMetricOrDefault(wxSYS_BORDER_Y
);
650 case wxBORDER_SUNKEN
:
651 case wxBORDER_RAISED
:
652 size
.x
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X
),
653 wxGetMetricOrDefault(wxSYS_BORDER_X
));
654 size
.y
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y
),
655 wxGetMetricOrDefault(wxSYS_BORDER_Y
));
658 case wxBORDER_DOUBLE
:
659 size
.x
= wxGetMetricOrDefault(wxSYS_EDGE_X
) +
660 wxGetMetricOrDefault(wxSYS_BORDER_X
);
661 size
.y
= wxGetMetricOrDefault(wxSYS_EDGE_Y
) +
662 wxGetMetricOrDefault(wxSYS_BORDER_Y
);
666 wxFAIL_MSG(_T("Unknown border style."));
670 // we have borders on both sides
674 wxSize
wxWindowBase::GetEffectiveMinSize() const
676 // merge the best size with the min size, giving priority to the min size
677 wxSize min
= GetMinSize();
678 if (min
.x
== wxDefaultCoord
|| min
.y
== wxDefaultCoord
)
680 wxSize best
= GetBestSize();
681 if (min
.x
== wxDefaultCoord
) min
.x
= best
.x
;
682 if (min
.y
== wxDefaultCoord
) min
.y
= best
.y
;
688 void wxWindowBase::SetInitialSize(const wxSize
& size
)
690 // Set the min size to the size passed in. This will usually either be
691 // wxDefaultSize or the size passed to this window's ctor/Create function.
694 // Merge the size with the best size if needed
695 wxSize best
= GetEffectiveMinSize();
697 // If the current size doesn't match then change it
698 if (GetSize() != best
)
703 // by default the origin is not shifted
704 wxPoint
wxWindowBase::GetClientAreaOrigin() const
709 void wxWindowBase::SetWindowVariant( wxWindowVariant variant
)
711 if ( m_windowVariant
!= variant
)
713 m_windowVariant
= variant
;
715 DoSetWindowVariant(variant
);
719 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant
)
721 // adjust the font height to correspond to our new variant (notice that
722 // we're only called if something really changed)
723 wxFont font
= GetFont();
724 int size
= font
.GetPointSize();
727 case wxWINDOW_VARIANT_NORMAL
:
730 case wxWINDOW_VARIANT_SMALL
:
735 case wxWINDOW_VARIANT_MINI
:
740 case wxWINDOW_VARIANT_LARGE
:
746 wxFAIL_MSG(_T("unexpected window variant"));
750 font
.SetPointSize(size
);
754 void wxWindowBase::DoSetSizeHints( int minW
, int minH
,
756 int WXUNUSED(incW
), int WXUNUSED(incH
) )
758 wxCHECK_RET( (minW
== wxDefaultCoord
|| maxW
== wxDefaultCoord
|| minW
<= maxW
) &&
759 (minH
== wxDefaultCoord
|| maxH
== wxDefaultCoord
|| minH
<= maxH
),
760 _T("min width/height must be less than max width/height!") );
769 #if WXWIN_COMPATIBILITY_2_8
770 void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW
), int WXUNUSED(minH
),
771 int WXUNUSED(maxW
), int WXUNUSED(maxH
))
775 void wxWindowBase::SetVirtualSizeHints(const wxSize
& WXUNUSED(minsize
),
776 const wxSize
& WXUNUSED(maxsize
))
779 #endif // WXWIN_COMPATIBILITY_2_8
781 void wxWindowBase::DoSetVirtualSize( int x
, int y
)
783 m_virtualSize
= wxSize(x
, y
);
786 wxSize
wxWindowBase::DoGetVirtualSize() const
788 // we should use the entire client area so if it is greater than our
789 // virtual size, expand it to fit (otherwise if the window is big enough we
790 // wouldn't be using parts of it)
791 wxSize size
= GetClientSize();
792 if ( m_virtualSize
.x
> size
.x
)
793 size
.x
= m_virtualSize
.x
;
795 if ( m_virtualSize
.y
>= size
.y
)
796 size
.y
= m_virtualSize
.y
;
801 void wxWindowBase::DoGetScreenPosition(int *x
, int *y
) const
803 // screen position is the same as (0, 0) in client coords for non TLWs (and
804 // TLWs override this method)
810 ClientToScreen(x
, y
);
813 // ----------------------------------------------------------------------------
814 // show/hide/enable/disable the window
815 // ----------------------------------------------------------------------------
817 bool wxWindowBase::Show(bool show
)
819 if ( show
!= m_isShown
)
831 bool wxWindowBase::IsEnabled() const
833 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
836 void wxWindowBase::NotifyWindowOnEnableChange(bool enabled
)
838 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
840 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
844 // If we are top-level then the logic doesn't apply - otherwise
845 // showing a modal dialog would result in total greying out (and ungreying
846 // out later) of everything which would be really ugly
850 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
852 node
= node
->GetNext() )
854 wxWindowBase
* const child
= node
->GetData();
855 if ( !child
->IsTopLevel() && child
->IsThisEnabled() )
856 child
->NotifyWindowOnEnableChange(enabled
);
860 bool wxWindowBase::Enable(bool enable
)
862 if ( enable
== IsThisEnabled() )
865 m_isEnabled
= enable
;
867 #ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
869 #else // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
870 wxWindowBase
* const parent
= GetParent();
871 if( !IsTopLevel() && parent
&& !parent
->IsEnabled() )
875 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
877 NotifyWindowOnEnableChange(enable
);
882 bool wxWindowBase::IsShownOnScreen() const
885 (GetParent() == NULL
|| GetParent()->IsShownOnScreen());
888 // ----------------------------------------------------------------------------
890 // ----------------------------------------------------------------------------
892 bool wxWindowBase::IsTopLevel() const
897 // ----------------------------------------------------------------------------
898 // reparenting the window
899 // ----------------------------------------------------------------------------
901 void wxWindowBase::AddChild(wxWindowBase
*child
)
903 wxCHECK_RET( child
, wxT("can't add a NULL child") );
905 // this should never happen and it will lead to a crash later if it does
906 // because RemoveChild() will remove only one node from the children list
907 // and the other(s) one(s) will be left with dangling pointers in them
908 wxASSERT_MSG( !GetChildren().Find((wxWindow
*)child
), _T("AddChild() called twice") );
910 GetChildren().Append((wxWindow
*)child
);
911 child
->SetParent(this);
914 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
916 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
918 GetChildren().DeleteObject((wxWindow
*)child
);
919 child
->SetParent(NULL
);
922 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
924 wxWindow
*oldParent
= GetParent();
925 if ( newParent
== oldParent
)
931 const bool oldEnabledState
= IsEnabled();
933 // unlink this window from the existing parent.
936 oldParent
->RemoveChild(this);
940 wxTopLevelWindows
.DeleteObject((wxWindow
*)this);
943 // add it to the new one
946 newParent
->AddChild(this);
950 wxTopLevelWindows
.Append((wxWindow
*)this);
953 // We need to notify window (and its subwindows) if by changing the parent
954 // we also change our enabled/disabled status.
955 const bool newEnabledState
= IsEnabled();
956 if ( newEnabledState
!= oldEnabledState
)
958 NotifyWindowOnEnableChange(newEnabledState
);
964 // ----------------------------------------------------------------------------
965 // event handler stuff
966 // ----------------------------------------------------------------------------
968 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
970 wxEvtHandler
*handlerOld
= GetEventHandler();
972 handler
->SetNextHandler(handlerOld
);
975 GetEventHandler()->SetPreviousHandler(handler
);
977 SetEventHandler(handler
);
980 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
982 wxEvtHandler
*handlerA
= GetEventHandler();
985 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
986 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
989 handlerB
->SetPreviousHandler((wxEvtHandler
*)NULL
);
990 SetEventHandler(handlerB
);
995 handlerA
= (wxEvtHandler
*)NULL
;
1002 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
1004 wxCHECK_MSG( handler
, false, _T("RemoveEventHandler(NULL) called") );
1006 wxEvtHandler
*handlerPrev
= NULL
,
1007 *handlerCur
= GetEventHandler();
1008 while ( handlerCur
)
1010 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
1012 if ( handlerCur
== handler
)
1016 handlerPrev
->SetNextHandler(handlerNext
);
1020 SetEventHandler(handlerNext
);
1025 handlerNext
->SetPreviousHandler ( handlerPrev
);
1028 handler
->SetNextHandler(NULL
);
1029 handler
->SetPreviousHandler(NULL
);
1034 handlerPrev
= handlerCur
;
1035 handlerCur
= handlerNext
;
1038 wxFAIL_MSG( _T("where has the event handler gone?") );
1043 bool wxWindowBase::HandleWindowEvent(wxEvent
& event
) const
1045 return GetEventHandler()->SafelyProcessEvent(event
);
1048 // ----------------------------------------------------------------------------
1049 // colours, fonts &c
1050 // ----------------------------------------------------------------------------
1052 void wxWindowBase::InheritAttributes()
1054 const wxWindowBase
* const parent
= GetParent();
1058 // we only inherit attributes which had been explicitly set for the parent
1059 // which ensures that this only happens if the user really wants it and
1060 // not by default which wouldn't make any sense in modern GUIs where the
1061 // controls don't all use the same fonts (nor colours)
1062 if ( parent
->m_inheritFont
&& !m_hasFont
)
1063 SetFont(parent
->GetFont());
1065 // in addition, there is a possibility to explicitly forbid inheriting
1066 // colours at each class level by overriding ShouldInheritColours()
1067 if ( ShouldInheritColours() )
1069 if ( parent
->m_inheritFgCol
&& !m_hasFgCol
)
1070 SetForegroundColour(parent
->GetForegroundColour());
1072 // inheriting (solid) background colour is wrong as it totally breaks
1073 // any kind of themed backgrounds
1075 // instead, the controls should use the same background as their parent
1076 // (ideally by not drawing it at all)
1078 if ( parent
->m_inheritBgCol
&& !m_hasBgCol
)
1079 SetBackgroundColour(parent
->GetBackgroundColour());
1084 /* static */ wxVisualAttributes
1085 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1087 // it is important to return valid values for all attributes from here,
1088 // GetXXX() below rely on this
1089 wxVisualAttributes attrs
;
1090 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
1091 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1093 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1094 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1095 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1096 // colour on other platforms.
1098 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1099 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1101 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1106 wxColour
wxWindowBase::GetBackgroundColour() const
1108 if ( !m_backgroundColour
.IsOk() )
1110 wxASSERT_MSG( !m_hasBgCol
, _T("we have invalid explicit bg colour?") );
1112 // get our default background colour
1113 wxColour colBg
= GetDefaultAttributes().colBg
;
1115 // we must return some valid colour to avoid redoing this every time
1116 // and also to avoid surprizing the applications written for older
1117 // wxWidgets versions where GetBackgroundColour() always returned
1118 // something -- so give them something even if it doesn't make sense
1119 // for this window (e.g. it has a themed background)
1121 colBg
= GetClassDefaultAttributes().colBg
;
1126 return m_backgroundColour
;
1129 wxColour
wxWindowBase::GetForegroundColour() const
1131 // logic is the same as above
1132 if ( !m_hasFgCol
&& !m_foregroundColour
.Ok() )
1134 wxColour colFg
= GetDefaultAttributes().colFg
;
1136 if ( !colFg
.IsOk() )
1137 colFg
= GetClassDefaultAttributes().colFg
;
1142 return m_foregroundColour
;
1145 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
1147 if ( colour
== m_backgroundColour
)
1150 m_hasBgCol
= colour
.IsOk();
1151 if ( m_backgroundStyle
!= wxBG_STYLE_CUSTOM
)
1152 m_backgroundStyle
= m_hasBgCol
? wxBG_STYLE_COLOUR
: wxBG_STYLE_SYSTEM
;
1154 m_inheritBgCol
= m_hasBgCol
;
1155 m_backgroundColour
= colour
;
1156 SetThemeEnabled( !m_hasBgCol
&& !m_foregroundColour
.Ok() );
1160 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
1162 if (colour
== m_foregroundColour
)
1165 m_hasFgCol
= colour
.IsOk();
1166 m_inheritFgCol
= m_hasFgCol
;
1167 m_foregroundColour
= colour
;
1168 SetThemeEnabled( !m_hasFgCol
&& !m_backgroundColour
.Ok() );
1172 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
1174 // setting an invalid cursor is ok, it means that we don't have any special
1176 if ( m_cursor
.IsSameAs(cursor
) )
1187 wxFont
wxWindowBase::GetFont() const
1189 // logic is the same as in GetBackgroundColour()
1190 if ( !m_font
.IsOk() )
1192 wxASSERT_MSG( !m_hasFont
, _T("we have invalid explicit font?") );
1194 wxFont font
= GetDefaultAttributes().font
;
1196 font
= GetClassDefaultAttributes().font
;
1204 bool wxWindowBase::SetFont(const wxFont
& font
)
1206 if ( font
== m_font
)
1213 m_hasFont
= font
.IsOk();
1214 m_inheritFont
= m_hasFont
;
1216 InvalidateBestSize();
1223 void wxWindowBase::SetPalette(const wxPalette
& pal
)
1225 m_hasCustomPalette
= true;
1228 // VZ: can anyone explain me what do we do here?
1229 wxWindowDC
d((wxWindow
*) this);
1233 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
1235 wxWindow
*win
= (wxWindow
*)this;
1236 while ( win
&& !win
->HasCustomPalette() )
1238 win
= win
->GetParent();
1244 #endif // wxUSE_PALETTE
1247 void wxWindowBase::SetCaret(wxCaret
*caret
)
1258 wxASSERT_MSG( m_caret
->GetWindow() == this,
1259 wxT("caret should be created associated to this window") );
1262 #endif // wxUSE_CARET
1264 #if wxUSE_VALIDATORS
1265 // ----------------------------------------------------------------------------
1267 // ----------------------------------------------------------------------------
1269 void wxWindowBase::SetValidator(const wxValidator
& validator
)
1271 if ( m_windowValidator
)
1272 delete m_windowValidator
;
1274 m_windowValidator
= (wxValidator
*)validator
.Clone();
1276 if ( m_windowValidator
)
1277 m_windowValidator
->SetWindow(this);
1279 #endif // wxUSE_VALIDATORS
1281 // ----------------------------------------------------------------------------
1282 // update region stuff
1283 // ----------------------------------------------------------------------------
1285 wxRect
wxWindowBase::GetUpdateClientRect() const
1287 wxRegion rgnUpdate
= GetUpdateRegion();
1288 rgnUpdate
.Intersect(GetClientRect());
1289 wxRect rectUpdate
= rgnUpdate
.GetBox();
1290 wxPoint ptOrigin
= GetClientAreaOrigin();
1291 rectUpdate
.x
-= ptOrigin
.x
;
1292 rectUpdate
.y
-= ptOrigin
.y
;
1297 bool wxWindowBase::DoIsExposed(int x
, int y
) const
1299 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
1302 bool wxWindowBase::DoIsExposed(int x
, int y
, int w
, int h
) const
1304 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
1307 void wxWindowBase::ClearBackground()
1309 // wxGTK uses its own version, no need to add never used code
1311 wxClientDC
dc((wxWindow
*)this);
1312 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
1313 dc
.SetBackground(brush
);
1318 // ----------------------------------------------------------------------------
1319 // find child window by id or name
1320 // ----------------------------------------------------------------------------
1322 wxWindow
*wxWindowBase::FindWindow(long id
) const
1324 if ( id
== m_windowId
)
1325 return (wxWindow
*)this;
1327 wxWindowBase
*res
= (wxWindow
*)NULL
;
1328 wxWindowList::compatibility_iterator node
;
1329 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1331 wxWindowBase
*child
= node
->GetData();
1332 res
= child
->FindWindow( id
);
1335 return (wxWindow
*)res
;
1338 wxWindow
*wxWindowBase::FindWindow(const wxString
& name
) const
1340 if ( name
== m_windowName
)
1341 return (wxWindow
*)this;
1343 wxWindowBase
*res
= (wxWindow
*)NULL
;
1344 wxWindowList::compatibility_iterator node
;
1345 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1347 wxWindow
*child
= node
->GetData();
1348 res
= child
->FindWindow(name
);
1351 return (wxWindow
*)res
;
1355 // find any window by id or name or label: If parent is non-NULL, look through
1356 // children for a label or title matching the specified string. If NULL, look
1357 // through all top-level windows.
1359 // to avoid duplicating code we reuse the same helper function but with
1360 // different comparators
1362 typedef bool (*wxFindWindowCmp
)(const wxWindow
*win
,
1363 const wxString
& label
, long id
);
1366 bool wxFindWindowCmpLabels(const wxWindow
*win
, const wxString
& label
,
1369 return win
->GetLabel() == label
;
1373 bool wxFindWindowCmpNames(const wxWindow
*win
, const wxString
& label
,
1376 return win
->GetName() == label
;
1380 bool wxFindWindowCmpIds(const wxWindow
*win
, const wxString
& WXUNUSED(label
),
1383 return win
->GetId() == id
;
1386 // recursive helper for the FindWindowByXXX() functions
1388 wxWindow
*wxFindWindowRecursively(const wxWindow
*parent
,
1389 const wxString
& label
,
1391 wxFindWindowCmp cmp
)
1395 // see if this is the one we're looking for
1396 if ( (*cmp
)(parent
, label
, id
) )
1397 return (wxWindow
*)parent
;
1399 // It wasn't, so check all its children
1400 for ( wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
1402 node
= node
->GetNext() )
1404 // recursively check each child
1405 wxWindow
*win
= (wxWindow
*)node
->GetData();
1406 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1416 // helper for FindWindowByXXX()
1418 wxWindow
*wxFindWindowHelper(const wxWindow
*parent
,
1419 const wxString
& label
,
1421 wxFindWindowCmp cmp
)
1425 // just check parent and all its children
1426 return wxFindWindowRecursively(parent
, label
, id
, cmp
);
1429 // start at very top of wx's windows
1430 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1432 node
= node
->GetNext() )
1434 // recursively check each window & its children
1435 wxWindow
*win
= node
->GetData();
1436 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1446 wxWindowBase::FindWindowByLabel(const wxString
& title
, const wxWindow
*parent
)
1448 return wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpLabels
);
1453 wxWindowBase::FindWindowByName(const wxString
& title
, const wxWindow
*parent
)
1455 wxWindow
*win
= wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpNames
);
1459 // fall back to the label
1460 win
= FindWindowByLabel(title
, parent
);
1468 wxWindowBase::FindWindowById( long id
, const wxWindow
* parent
)
1470 return wxFindWindowHelper(parent
, wxEmptyString
, id
, wxFindWindowCmpIds
);
1473 // ----------------------------------------------------------------------------
1474 // dialog oriented functions
1475 // ----------------------------------------------------------------------------
1477 void wxWindowBase::MakeModal(bool modal
)
1479 // Disable all other windows
1482 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1485 wxWindow
*win
= node
->GetData();
1487 win
->Enable(!modal
);
1489 node
= node
->GetNext();
1494 bool wxWindowBase::Validate()
1496 #if wxUSE_VALIDATORS
1497 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1499 wxWindowList::compatibility_iterator node
;
1500 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1502 wxWindowBase
*child
= node
->GetData();
1503 wxValidator
*validator
= child
->GetValidator();
1504 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
1509 if ( recurse
&& !child
->Validate() )
1514 #endif // wxUSE_VALIDATORS
1519 bool wxWindowBase::TransferDataToWindow()
1521 #if wxUSE_VALIDATORS
1522 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1524 wxWindowList::compatibility_iterator node
;
1525 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1527 wxWindowBase
*child
= node
->GetData();
1528 wxValidator
*validator
= child
->GetValidator();
1529 if ( validator
&& !validator
->TransferToWindow() )
1531 wxLogWarning(_("Could not transfer data to window"));
1533 wxLog::FlushActive();
1541 if ( !child
->TransferDataToWindow() )
1543 // warning already given
1548 #endif // wxUSE_VALIDATORS
1553 bool wxWindowBase::TransferDataFromWindow()
1555 #if wxUSE_VALIDATORS
1556 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1558 wxWindowList::compatibility_iterator node
;
1559 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1561 wxWindow
*child
= node
->GetData();
1562 wxValidator
*validator
= child
->GetValidator();
1563 if ( validator
&& !validator
->TransferFromWindow() )
1565 // nop warning here because the application is supposed to give
1566 // one itself - we don't know here what might have gone wrongly
1573 if ( !child
->TransferDataFromWindow() )
1575 // warning already given
1580 #endif // wxUSE_VALIDATORS
1585 void wxWindowBase::InitDialog()
1587 wxInitDialogEvent
event(GetId());
1588 event
.SetEventObject( this );
1589 GetEventHandler()->ProcessEvent(event
);
1592 // ----------------------------------------------------------------------------
1593 // context-sensitive help support
1594 // ----------------------------------------------------------------------------
1598 // associate this help text with this window
1599 void wxWindowBase::SetHelpText(const wxString
& text
)
1601 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1604 helpProvider
->AddHelp(this, text
);
1608 // associate this help text with all windows with the same id as this
1610 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1612 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1615 helpProvider
->AddHelp(GetId(), text
);
1619 // get the help string associated with this window (may be empty)
1620 // default implementation forwards calls to the help provider
1622 wxWindowBase::GetHelpTextAtPoint(const wxPoint
& WXUNUSED(pt
),
1623 wxHelpEvent::Origin
WXUNUSED(origin
)) const
1626 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1629 text
= helpProvider
->GetHelp(this);
1635 // show help for this window
1636 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1638 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1641 if ( helpProvider
->ShowHelpAtPoint(this, event
.GetPosition(), event
.GetOrigin()) )
1643 // skip the event.Skip() below
1651 #endif // wxUSE_HELP
1653 // ----------------------------------------------------------------------------
1655 // ----------------------------------------------------------------------------
1659 void wxWindowBase::SetToolTip( const wxString
&tip
)
1661 // don't create the new tooltip if we already have one
1664 m_tooltip
->SetTip( tip
);
1668 SetToolTip( new wxToolTip( tip
) );
1671 // setting empty tooltip text does not remove the tooltip any more - use
1672 // SetToolTip((wxToolTip *)NULL) for this
1675 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1677 if ( m_tooltip
!= tooltip
)
1682 m_tooltip
= tooltip
;
1686 #endif // wxUSE_TOOLTIPS
1688 // ----------------------------------------------------------------------------
1689 // constraints and sizers
1690 // ----------------------------------------------------------------------------
1692 #if wxUSE_CONSTRAINTS
1694 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1696 if ( m_constraints
)
1698 UnsetConstraints(m_constraints
);
1699 delete m_constraints
;
1701 m_constraints
= constraints
;
1702 if ( m_constraints
)
1704 // Make sure other windows know they're part of a 'meaningful relationship'
1705 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1706 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1707 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1708 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1709 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1710 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1711 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1712 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1713 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1714 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1715 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1716 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1717 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1718 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1719 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1720 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1724 // This removes any dangling pointers to this window in other windows'
1725 // constraintsInvolvedIn lists.
1726 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1730 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1731 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1732 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1733 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1734 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1735 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1736 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1737 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1738 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1739 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1740 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1741 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1742 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1743 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1744 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1745 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1749 // Back-pointer to other windows we're involved with, so if we delete this
1750 // window, we must delete any constraints we're involved with.
1751 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1753 if ( !m_constraintsInvolvedIn
)
1754 m_constraintsInvolvedIn
= new wxWindowList
;
1755 if ( !m_constraintsInvolvedIn
->Find((wxWindow
*)otherWin
) )
1756 m_constraintsInvolvedIn
->Append((wxWindow
*)otherWin
);
1759 // REMOVE back-pointer to other windows we're involved with.
1760 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1762 if ( m_constraintsInvolvedIn
)
1763 m_constraintsInvolvedIn
->DeleteObject((wxWindow
*)otherWin
);
1766 // Reset any constraints that mention this window
1767 void wxWindowBase::DeleteRelatedConstraints()
1769 if ( m_constraintsInvolvedIn
)
1771 wxWindowList::compatibility_iterator node
= m_constraintsInvolvedIn
->GetFirst();
1774 wxWindow
*win
= node
->GetData();
1775 wxLayoutConstraints
*constr
= win
->GetConstraints();
1777 // Reset any constraints involving this window
1780 constr
->left
.ResetIfWin(this);
1781 constr
->top
.ResetIfWin(this);
1782 constr
->right
.ResetIfWin(this);
1783 constr
->bottom
.ResetIfWin(this);
1784 constr
->width
.ResetIfWin(this);
1785 constr
->height
.ResetIfWin(this);
1786 constr
->centreX
.ResetIfWin(this);
1787 constr
->centreY
.ResetIfWin(this);
1790 wxWindowList::compatibility_iterator next
= node
->GetNext();
1791 m_constraintsInvolvedIn
->Erase(node
);
1795 delete m_constraintsInvolvedIn
;
1796 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1800 #endif // wxUSE_CONSTRAINTS
1802 void wxWindowBase::SetSizer(wxSizer
*sizer
, bool deleteOld
)
1804 if ( sizer
== m_windowSizer
)
1807 if ( m_windowSizer
)
1809 m_windowSizer
->SetContainingWindow(NULL
);
1812 delete m_windowSizer
;
1815 m_windowSizer
= sizer
;
1816 if ( m_windowSizer
)
1818 m_windowSizer
->SetContainingWindow((wxWindow
*)this);
1821 SetAutoLayout(m_windowSizer
!= NULL
);
1824 void wxWindowBase::SetSizerAndFit(wxSizer
*sizer
, bool deleteOld
)
1826 SetSizer( sizer
, deleteOld
);
1827 sizer
->SetSizeHints( (wxWindow
*) this );
1831 void wxWindowBase::SetContainingSizer(wxSizer
* sizer
)
1833 // adding a window to a sizer twice is going to result in fatal and
1834 // hard to debug problems later because when deleting the second
1835 // associated wxSizerItem we're going to dereference a dangling
1836 // pointer; so try to detect this as early as possible
1837 wxASSERT_MSG( !sizer
|| m_containingSizer
!= sizer
,
1838 _T("Adding a window to the same sizer twice?") );
1840 m_containingSizer
= sizer
;
1843 #if wxUSE_CONSTRAINTS
1845 void wxWindowBase::SatisfyConstraints()
1847 wxLayoutConstraints
*constr
= GetConstraints();
1848 bool wasOk
= constr
&& constr
->AreSatisfied();
1850 ResetConstraints(); // Mark all constraints as unevaluated
1854 // if we're a top level panel (i.e. our parent is frame/dialog), our
1855 // own constraints will never be satisfied any more unless we do it
1859 while ( noChanges
> 0 )
1861 LayoutPhase1(&noChanges
);
1865 LayoutPhase2(&noChanges
);
1868 #endif // wxUSE_CONSTRAINTS
1870 bool wxWindowBase::Layout()
1872 // If there is a sizer, use it instead of the constraints
1876 GetVirtualSize(&w
, &h
);
1877 GetSizer()->SetDimension( 0, 0, w
, h
);
1879 #if wxUSE_CONSTRAINTS
1882 SatisfyConstraints(); // Find the right constraints values
1883 SetConstraintSizes(); // Recursively set the real window sizes
1890 #if wxUSE_CONSTRAINTS
1892 // first phase of the constraints evaluation: set our own constraints
1893 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1895 wxLayoutConstraints
*constr
= GetConstraints();
1897 return !constr
|| constr
->SatisfyConstraints(this, noChanges
);
1900 // second phase: set the constraints for our children
1901 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1908 // Layout grand children
1914 // Do a phase of evaluating child constraints
1915 bool wxWindowBase::DoPhase(int phase
)
1917 // the list containing the children for which the constraints are already
1919 wxWindowList succeeded
;
1921 // the max number of iterations we loop before concluding that we can't set
1923 static const int maxIterations
= 500;
1925 for ( int noIterations
= 0; noIterations
< maxIterations
; noIterations
++ )
1929 // loop over all children setting their constraints
1930 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1932 node
= node
->GetNext() )
1934 wxWindow
*child
= node
->GetData();
1935 if ( child
->IsTopLevel() )
1937 // top level children are not inside our client area
1941 if ( !child
->GetConstraints() || succeeded
.Find(child
) )
1943 // this one is either already ok or nothing we can do about it
1947 int tempNoChanges
= 0;
1948 bool success
= phase
== 1 ? child
->LayoutPhase1(&tempNoChanges
)
1949 : child
->LayoutPhase2(&tempNoChanges
);
1950 noChanges
+= tempNoChanges
;
1954 succeeded
.Append(child
);
1960 // constraints are set
1968 void wxWindowBase::ResetConstraints()
1970 wxLayoutConstraints
*constr
= GetConstraints();
1973 constr
->left
.SetDone(false);
1974 constr
->top
.SetDone(false);
1975 constr
->right
.SetDone(false);
1976 constr
->bottom
.SetDone(false);
1977 constr
->width
.SetDone(false);
1978 constr
->height
.SetDone(false);
1979 constr
->centreX
.SetDone(false);
1980 constr
->centreY
.SetDone(false);
1983 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1986 wxWindow
*win
= node
->GetData();
1987 if ( !win
->IsTopLevel() )
1988 win
->ResetConstraints();
1989 node
= node
->GetNext();
1993 // Need to distinguish between setting the 'fake' size for windows and sizers,
1994 // and setting the real values.
1995 void wxWindowBase::SetConstraintSizes(bool recurse
)
1997 wxLayoutConstraints
*constr
= GetConstraints();
1998 if ( constr
&& constr
->AreSatisfied() )
2000 int x
= constr
->left
.GetValue();
2001 int y
= constr
->top
.GetValue();
2002 int w
= constr
->width
.GetValue();
2003 int h
= constr
->height
.GetValue();
2005 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
2006 (constr
->height
.GetRelationship() != wxAsIs
) )
2008 SetSize(x
, y
, w
, h
);
2012 // If we don't want to resize this window, just move it...
2018 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2019 GetClassInfo()->GetClassName(),
2025 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2028 wxWindow
*win
= node
->GetData();
2029 if ( !win
->IsTopLevel() && win
->GetConstraints() )
2030 win
->SetConstraintSizes();
2031 node
= node
->GetNext();
2036 // Only set the size/position of the constraint (if any)
2037 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
2039 wxLayoutConstraints
*constr
= GetConstraints();
2042 if ( x
!= wxDefaultCoord
)
2044 constr
->left
.SetValue(x
);
2045 constr
->left
.SetDone(true);
2047 if ( y
!= wxDefaultCoord
)
2049 constr
->top
.SetValue(y
);
2050 constr
->top
.SetDone(true);
2052 if ( w
!= wxDefaultCoord
)
2054 constr
->width
.SetValue(w
);
2055 constr
->width
.SetDone(true);
2057 if ( h
!= wxDefaultCoord
)
2059 constr
->height
.SetValue(h
);
2060 constr
->height
.SetDone(true);
2065 void wxWindowBase::MoveConstraint(int x
, int y
)
2067 wxLayoutConstraints
*constr
= GetConstraints();
2070 if ( x
!= wxDefaultCoord
)
2072 constr
->left
.SetValue(x
);
2073 constr
->left
.SetDone(true);
2075 if ( y
!= wxDefaultCoord
)
2077 constr
->top
.SetValue(y
);
2078 constr
->top
.SetDone(true);
2083 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
2085 wxLayoutConstraints
*constr
= GetConstraints();
2088 *w
= constr
->width
.GetValue();
2089 *h
= constr
->height
.GetValue();
2095 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
2097 wxLayoutConstraints
*constr
= GetConstraints();
2100 *w
= constr
->width
.GetValue();
2101 *h
= constr
->height
.GetValue();
2104 GetClientSize(w
, h
);
2107 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
2109 wxLayoutConstraints
*constr
= GetConstraints();
2112 *x
= constr
->left
.GetValue();
2113 *y
= constr
->top
.GetValue();
2119 #endif // wxUSE_CONSTRAINTS
2121 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
) const
2123 // don't do it for the dialogs/frames - they float independently of their
2125 if ( !IsTopLevel() )
2127 wxWindow
*parent
= GetParent();
2128 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
2130 wxPoint
pt(parent
->GetClientAreaOrigin());
2137 // ----------------------------------------------------------------------------
2138 // Update UI processing
2139 // ----------------------------------------------------------------------------
2141 void wxWindowBase::UpdateWindowUI(long flags
)
2143 wxUpdateUIEvent
event(GetId());
2144 event
.SetEventObject(this);
2146 if ( GetEventHandler()->ProcessEvent(event
) )
2148 DoUpdateWindowUI(event
);
2151 if (flags
& wxUPDATE_UI_RECURSE
)
2153 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2156 wxWindow
* child
= (wxWindow
*) node
->GetData();
2157 child
->UpdateWindowUI(flags
);
2158 node
= node
->GetNext();
2163 // do the window-specific processing after processing the update event
2164 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
2166 if ( event
.GetSetEnabled() )
2167 Enable(event
.GetEnabled());
2169 if ( event
.GetSetShown() )
2170 Show(event
.GetShown());
2173 // ----------------------------------------------------------------------------
2174 // dialog units translations
2175 // ----------------------------------------------------------------------------
2177 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
2179 int charWidth
= GetCharWidth();
2180 int charHeight
= GetCharHeight();
2181 wxPoint pt2
= wxDefaultPosition
;
2182 if (pt
.x
!= wxDefaultCoord
)
2183 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
);
2184 if (pt
.y
!= wxDefaultCoord
)
2185 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
);
2190 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
2192 int charWidth
= GetCharWidth();
2193 int charHeight
= GetCharHeight();
2194 wxPoint pt2
= wxDefaultPosition
;
2195 if (pt
.x
!= wxDefaultCoord
)
2196 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4);
2197 if (pt
.y
!= wxDefaultCoord
)
2198 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8);
2203 // ----------------------------------------------------------------------------
2205 // ----------------------------------------------------------------------------
2207 // propagate the colour change event to the subwindows
2208 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
2210 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2213 // Only propagate to non-top-level windows
2214 wxWindow
*win
= node
->GetData();
2215 if ( !win
->IsTopLevel() )
2217 wxSysColourChangedEvent event2
;
2218 event
.SetEventObject(win
);
2219 win
->GetEventHandler()->ProcessEvent(event2
);
2222 node
= node
->GetNext();
2228 // the default action is to populate dialog with data when it's created,
2229 // and nudge the UI into displaying itself correctly in case
2230 // we've turned the wxUpdateUIEvents frequency down low.
2231 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
2233 TransferDataToWindow();
2235 // Update the UI at this point
2236 UpdateWindowUI(wxUPDATE_UI_RECURSE
);
2239 // ----------------------------------------------------------------------------
2240 // menu-related functions
2241 // ----------------------------------------------------------------------------
2245 // this is used to pass the id of the selected item from the menu event handler
2246 // to the main function itself
2248 // it's ok to use a global here as there can be at most one popup menu shown at
2250 static int gs_popupMenuSelection
= wxID_NONE
;
2252 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent
& event
)
2254 // store the id in a global variable where we'll retrieve it from later
2255 gs_popupMenuSelection
= event
.GetId();
2259 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu
& menu
, int x
, int y
)
2261 gs_popupMenuSelection
= wxID_NONE
;
2263 Connect(wxEVT_COMMAND_MENU_SELECTED
,
2264 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2268 PopupMenu(&menu
, x
, y
);
2270 Disconnect(wxEVT_COMMAND_MENU_SELECTED
,
2271 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2275 return gs_popupMenuSelection
;
2278 #endif // wxUSE_MENUS
2280 // methods for drawing the sizers in a visible way
2283 static void DrawSizers(wxWindowBase
*win
);
2285 static void DrawBorder(wxWindowBase
*win
, const wxRect
& rect
, bool fill
= false)
2287 wxClientDC
dc((wxWindow
*)win
);
2288 dc
.SetPen(*wxRED_PEN
);
2289 dc
.SetBrush(fill
? wxBrush(*wxRED
, wxCROSSDIAG_HATCH
): *wxTRANSPARENT_BRUSH
);
2290 dc
.DrawRectangle(rect
.Deflate(1, 1));
2293 static void DrawSizer(wxWindowBase
*win
, wxSizer
*sizer
)
2295 const wxSizerItemList
& items
= sizer
->GetChildren();
2296 for ( wxSizerItemList::const_iterator i
= items
.begin(),
2301 wxSizerItem
*item
= *i
;
2302 if ( item
->IsSizer() )
2304 DrawBorder(win
, item
->GetRect().Deflate(2));
2305 DrawSizer(win
, item
->GetSizer());
2307 else if ( item
->IsSpacer() )
2309 DrawBorder(win
, item
->GetRect().Deflate(2), true);
2311 else if ( item
->IsWindow() )
2313 DrawSizers(item
->GetWindow());
2318 static void DrawSizers(wxWindowBase
*win
)
2320 wxSizer
*sizer
= win
->GetSizer();
2323 DrawBorder(win
, win
->GetClientSize());
2324 DrawSizer(win
, sizer
);
2326 else // no sizer, still recurse into the children
2328 const wxWindowList
& children
= win
->GetChildren();
2329 for ( wxWindowList::const_iterator i
= children
.begin(),
2330 end
= children
.end();
2339 #endif // __WXDEBUG__
2341 // process special middle clicks
2342 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
2344 if ( event
.ControlDown() && event
.AltDown() )
2347 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2348 if ( event
.ShiftDown() )
2353 #endif // __WXDEBUG__
2354 ::wxInfoMessageBox((wxWindow
*)this);
2362 // ----------------------------------------------------------------------------
2364 // ----------------------------------------------------------------------------
2366 #if wxUSE_ACCESSIBILITY
2367 void wxWindowBase::SetAccessible(wxAccessible
* accessible
)
2369 if (m_accessible
&& (accessible
!= m_accessible
))
2370 delete m_accessible
;
2371 m_accessible
= accessible
;
2373 m_accessible
->SetWindow((wxWindow
*) this);
2376 // Returns the accessible object, creating if necessary.
2377 wxAccessible
* wxWindowBase::GetOrCreateAccessible()
2380 m_accessible
= CreateAccessible();
2381 return m_accessible
;
2384 // Override to create a specific accessible object.
2385 wxAccessible
* wxWindowBase::CreateAccessible()
2387 return new wxWindowAccessible((wxWindow
*) this);
2392 // ----------------------------------------------------------------------------
2393 // list classes implementation
2394 // ----------------------------------------------------------------------------
2398 #include "wx/listimpl.cpp"
2399 WX_DEFINE_LIST(wxWindowList
)
2403 void wxWindowListNode::DeleteData()
2405 delete (wxWindow
*)GetData();
2408 #endif // wxUSE_STL/!wxUSE_STL
2410 // ----------------------------------------------------------------------------
2412 // ----------------------------------------------------------------------------
2414 wxBorder
wxWindowBase::GetBorder(long flags
) const
2416 wxBorder border
= (wxBorder
)(flags
& wxBORDER_MASK
);
2417 if ( border
== wxBORDER_DEFAULT
)
2419 border
= GetDefaultBorder();
2421 else if ( border
== wxBORDER_THEME
)
2423 border
= GetDefaultBorderForControl();
2429 wxBorder
wxWindowBase::GetDefaultBorder() const
2431 return wxBORDER_NONE
;
2434 // ----------------------------------------------------------------------------
2436 // ----------------------------------------------------------------------------
2438 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
2440 // here we just check if the point is inside the window or not
2442 // check the top and left border first
2443 bool outside
= x
< 0 || y
< 0;
2446 // check the right and bottom borders too
2447 wxSize size
= GetSize();
2448 outside
= x
>= size
.x
|| y
>= size
.y
;
2451 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
2454 // ----------------------------------------------------------------------------
2456 // ----------------------------------------------------------------------------
2458 struct WXDLLEXPORT wxWindowNext
2462 } *wxWindowBase::ms_winCaptureNext
= NULL
;
2463 wxWindow
*wxWindowBase::ms_winCaptureCurrent
= NULL
;
2464 bool wxWindowBase::ms_winCaptureChanging
= false;
2466 void wxWindowBase::CaptureMouse()
2468 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), wx_static_cast(void*, this));
2470 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive CaptureMouse call?") );
2472 ms_winCaptureChanging
= true;
2474 wxWindow
*winOld
= GetCapture();
2477 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
2480 wxWindowNext
*item
= new wxWindowNext
;
2482 item
->next
= ms_winCaptureNext
;
2483 ms_winCaptureNext
= item
;
2485 //else: no mouse capture to save
2488 ms_winCaptureCurrent
= (wxWindow
*)this;
2490 ms_winCaptureChanging
= false;
2493 void wxWindowBase::ReleaseMouse()
2495 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), wx_static_cast(void*, this));
2497 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive ReleaseMouse call?") );
2499 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2501 ms_winCaptureChanging
= true;
2504 ms_winCaptureCurrent
= NULL
;
2506 if ( ms_winCaptureNext
)
2508 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
2509 ms_winCaptureCurrent
= ms_winCaptureNext
->win
;
2511 wxWindowNext
*item
= ms_winCaptureNext
;
2512 ms_winCaptureNext
= item
->next
;
2515 //else: stack is empty, no previous capture
2517 ms_winCaptureChanging
= false;
2519 wxLogTrace(_T("mousecapture"),
2520 (const wxChar
*) _T("After ReleaseMouse() mouse is captured by %p"),
2521 wx_static_cast(void*, GetCapture()));
2524 static void DoNotifyWindowAboutCaptureLost(wxWindow
*win
)
2526 wxMouseCaptureLostEvent
event(win
->GetId());
2527 event
.SetEventObject(win
);
2528 if ( !win
->GetEventHandler()->ProcessEvent(event
) )
2530 // windows must handle this event, otherwise the app wouldn't behave
2531 // correctly if it loses capture unexpectedly; see the discussion here:
2532 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2533 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
2534 wxFAIL_MSG( _T("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2539 void wxWindowBase::NotifyCaptureLost()
2541 // don't do anything if capture lost was expected, i.e. resulted from
2542 // a wx call to ReleaseMouse or CaptureMouse:
2543 if ( ms_winCaptureChanging
)
2546 // if the capture was lost unexpectedly, notify every window that has
2547 // capture (on stack or current) about it and clear the stack:
2549 if ( ms_winCaptureCurrent
)
2551 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent
);
2552 ms_winCaptureCurrent
= NULL
;
2555 while ( ms_winCaptureNext
)
2557 wxWindowNext
*item
= ms_winCaptureNext
;
2558 ms_winCaptureNext
= item
->next
;
2560 DoNotifyWindowAboutCaptureLost(item
->win
);
2569 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId
),
2570 int WXUNUSED(modifiers
),
2571 int WXUNUSED(keycode
))
2577 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId
))
2583 #endif // wxUSE_HOTKEY
2585 // ----------------------------------------------------------------------------
2587 // ----------------------------------------------------------------------------
2589 bool wxWindowBase::TryValidator(wxEvent
& wxVALIDATOR_PARAM(event
))
2591 #if wxUSE_VALIDATORS
2592 // Can only use the validator of the window which
2593 // is receiving the event
2594 if ( event
.GetEventObject() == this )
2596 wxValidator
*validator
= GetValidator();
2597 if ( validator
&& validator
->ProcessEvent(event
) )
2602 #endif // wxUSE_VALIDATORS
2607 bool wxWindowBase::TryParent(wxEvent
& event
)
2609 // carry on up the parent-child hierarchy if the propagation count hasn't
2611 if ( event
.ShouldPropagate() )
2613 // honour the requests to stop propagation at this window: this is
2614 // used by the dialogs, for example, to prevent processing the events
2615 // from the dialog controls in the parent frame which rarely, if ever,
2617 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
2619 wxWindow
*parent
= GetParent();
2620 if ( parent
&& !parent
->IsBeingDeleted() )
2622 wxPropagateOnce
propagateOnce(event
);
2624 return parent
->GetEventHandler()->ProcessEvent(event
);
2629 return wxEvtHandler::TryParent(event
);
2632 // ----------------------------------------------------------------------------
2633 // window relationships
2634 // ----------------------------------------------------------------------------
2636 wxWindow
*wxWindowBase::DoGetSibling(WindowOrder order
) const
2638 wxCHECK_MSG( GetParent(), NULL
,
2639 _T("GetPrev/NextSibling() don't work for TLWs!") );
2641 wxWindowList
& siblings
= GetParent()->GetChildren();
2642 wxWindowList::compatibility_iterator i
= siblings
.Find((wxWindow
*)this);
2643 wxCHECK_MSG( i
, NULL
, _T("window not a child of its parent?") );
2645 if ( order
== OrderBefore
)
2646 i
= i
->GetPrevious();
2650 return i
? i
->GetData() : NULL
;
2653 // ----------------------------------------------------------------------------
2654 // keyboard navigation
2655 // ----------------------------------------------------------------------------
2657 // Navigates in the specified direction inside this window
2658 bool wxWindowBase::DoNavigateIn(int flags
)
2660 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2661 // native code doesn't process our wxNavigationKeyEvents anyhow
2664 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
2665 wxNavigationKeyEvent eventNav
;
2666 eventNav
.SetFlags(flags
);
2667 eventNav
.SetEventObject(FindFocus());
2668 return GetEventHandler()->ProcessEvent(eventNav
);
2669 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
2672 void wxWindowBase::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
2674 // check that we're not a top level window
2675 wxCHECK_RET( GetParent(),
2676 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2678 // detect the special case when we have nothing to do anyhow and when the
2679 // code below wouldn't work
2683 // find the target window in the siblings list
2684 wxWindowList
& siblings
= GetParent()->GetChildren();
2685 wxWindowList::compatibility_iterator i
= siblings
.Find(win
);
2686 wxCHECK_RET( i
, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2688 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2689 // can't just move the node around
2690 wxWindow
*self
= (wxWindow
*)this;
2691 siblings
.DeleteObject(self
);
2692 if ( move
== OrderAfter
)
2699 siblings
.Insert(i
, self
);
2701 else // OrderAfter and win was the last sibling
2703 siblings
.Append(self
);
2707 // ----------------------------------------------------------------------------
2709 // ----------------------------------------------------------------------------
2711 /*static*/ wxWindow
* wxWindowBase::FindFocus()
2713 wxWindowBase
*win
= DoFindFocus();
2714 return win
? win
->GetMainWindowOfCompositeControl() : NULL
;
2717 // ----------------------------------------------------------------------------
2719 // ----------------------------------------------------------------------------
2721 wxWindow
* wxGetTopLevelParent(wxWindow
*win
)
2723 while ( win
&& !win
->IsTopLevel() )
2724 win
= win
->GetParent();
2729 #if wxUSE_ACCESSIBILITY
2730 // ----------------------------------------------------------------------------
2731 // accessible object for windows
2732 // ----------------------------------------------------------------------------
2734 // Can return either a child object, or an integer
2735 // representing the child element, starting from 1.
2736 wxAccStatus
wxWindowAccessible::HitTest(const wxPoint
& WXUNUSED(pt
), int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(childObject
))
2738 wxASSERT( GetWindow() != NULL
);
2742 return wxACC_NOT_IMPLEMENTED
;
2745 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2746 wxAccStatus
wxWindowAccessible::GetLocation(wxRect
& rect
, int elementId
)
2748 wxASSERT( GetWindow() != NULL
);
2752 wxWindow
* win
= NULL
;
2759 if (elementId
<= (int) GetWindow()->GetChildren().GetCount())
2761 win
= GetWindow()->GetChildren().Item(elementId
-1)->GetData();
2768 rect
= win
->GetRect();
2769 if (win
->GetParent() && !win
->IsKindOf(CLASSINFO(wxTopLevelWindow
)))
2770 rect
.SetPosition(win
->GetParent()->ClientToScreen(rect
.GetPosition()));
2774 return wxACC_NOT_IMPLEMENTED
;
2777 // Navigates from fromId to toId/toObject.
2778 wxAccStatus
wxWindowAccessible::Navigate(wxNavDir navDir
, int fromId
,
2779 int* WXUNUSED(toId
), wxAccessible
** toObject
)
2781 wxASSERT( GetWindow() != NULL
);
2787 case wxNAVDIR_FIRSTCHILD
:
2789 if (GetWindow()->GetChildren().GetCount() == 0)
2791 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetFirst()->GetData();
2792 *toObject
= childWindow
->GetOrCreateAccessible();
2796 case wxNAVDIR_LASTCHILD
:
2798 if (GetWindow()->GetChildren().GetCount() == 0)
2800 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetLast()->GetData();
2801 *toObject
= childWindow
->GetOrCreateAccessible();
2805 case wxNAVDIR_RIGHT
:
2809 wxWindowList::compatibility_iterator node
=
2810 wxWindowList::compatibility_iterator();
2813 // Can't navigate to sibling of this window
2814 // if we're a top-level window.
2815 if (!GetWindow()->GetParent())
2816 return wxACC_NOT_IMPLEMENTED
;
2818 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2822 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
2823 node
= GetWindow()->GetChildren().Item(fromId
-1);
2826 if (node
&& node
->GetNext())
2828 wxWindow
* nextWindow
= node
->GetNext()->GetData();
2829 *toObject
= nextWindow
->GetOrCreateAccessible();
2837 case wxNAVDIR_PREVIOUS
:
2839 wxWindowList::compatibility_iterator node
=
2840 wxWindowList::compatibility_iterator();
2843 // Can't navigate to sibling of this window
2844 // if we're a top-level window.
2845 if (!GetWindow()->GetParent())
2846 return wxACC_NOT_IMPLEMENTED
;
2848 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2852 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
2853 node
= GetWindow()->GetChildren().Item(fromId
-1);
2856 if (node
&& node
->GetPrevious())
2858 wxWindow
* previousWindow
= node
->GetPrevious()->GetData();
2859 *toObject
= previousWindow
->GetOrCreateAccessible();
2867 return wxACC_NOT_IMPLEMENTED
;
2870 // Gets the name of the specified object.
2871 wxAccStatus
wxWindowAccessible::GetName(int childId
, wxString
* name
)
2873 wxASSERT( GetWindow() != NULL
);
2879 // If a child, leave wxWidgets to call the function on the actual
2882 return wxACC_NOT_IMPLEMENTED
;
2884 // This will eventually be replaced by specialised
2885 // accessible classes, one for each kind of wxWidgets
2886 // control or window.
2888 if (GetWindow()->IsKindOf(CLASSINFO(wxButton
)))
2889 title
= ((wxButton
*) GetWindow())->GetLabel();
2892 title
= GetWindow()->GetName();
2900 return wxACC_NOT_IMPLEMENTED
;
2903 // Gets the number of children.
2904 wxAccStatus
wxWindowAccessible::GetChildCount(int* childId
)
2906 wxASSERT( GetWindow() != NULL
);
2910 *childId
= (int) GetWindow()->GetChildren().GetCount();
2914 // Gets the specified child (starting from 1).
2915 // If *child is NULL and return value is wxACC_OK,
2916 // this means that the child is a simple element and
2917 // not an accessible object.
2918 wxAccStatus
wxWindowAccessible::GetChild(int childId
, wxAccessible
** child
)
2920 wxASSERT( GetWindow() != NULL
);
2930 if (childId
> (int) GetWindow()->GetChildren().GetCount())
2933 wxWindow
* childWindow
= GetWindow()->GetChildren().Item(childId
-1)->GetData();
2934 *child
= childWindow
->GetOrCreateAccessible();
2941 // Gets the parent, or NULL.
2942 wxAccStatus
wxWindowAccessible::GetParent(wxAccessible
** parent
)
2944 wxASSERT( GetWindow() != NULL
);
2948 wxWindow
* parentWindow
= GetWindow()->GetParent();
2956 *parent
= parentWindow
->GetOrCreateAccessible();
2964 // Performs the default action. childId is 0 (the action for this object)
2965 // or > 0 (the action for a child).
2966 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2967 // window (e.g. an edit control).
2968 wxAccStatus
wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId
))
2970 wxASSERT( GetWindow() != NULL
);
2974 return wxACC_NOT_IMPLEMENTED
;
2977 // Gets the default action for this object (0) or > 0 (the action for a child).
2978 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2979 // string if there is no action.
2980 // The retrieved string describes the action that is performed on an object,
2981 // not what the object does as a result. For example, a toolbar button that prints
2982 // a document has a default action of "Press" rather than "Prints the current document."
2983 wxAccStatus
wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId
), wxString
* WXUNUSED(actionName
))
2985 wxASSERT( GetWindow() != NULL
);
2989 return wxACC_NOT_IMPLEMENTED
;
2992 // Returns the description for this object or a child.
2993 wxAccStatus
wxWindowAccessible::GetDescription(int WXUNUSED(childId
), wxString
* description
)
2995 wxASSERT( GetWindow() != NULL
);
2999 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3005 return wxACC_NOT_IMPLEMENTED
;
3008 // Returns help text for this object or a child, similar to tooltip text.
3009 wxAccStatus
wxWindowAccessible::GetHelpText(int WXUNUSED(childId
), wxString
* helpText
)
3011 wxASSERT( GetWindow() != NULL
);
3015 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3021 return wxACC_NOT_IMPLEMENTED
;
3024 // Returns the keyboard shortcut for this object or child.
3025 // Return e.g. ALT+K
3026 wxAccStatus
wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId
), wxString
* WXUNUSED(shortcut
))
3028 wxASSERT( GetWindow() != NULL
);
3032 return wxACC_NOT_IMPLEMENTED
;
3035 // Returns a role constant.
3036 wxAccStatus
wxWindowAccessible::GetRole(int childId
, wxAccRole
* role
)
3038 wxASSERT( GetWindow() != NULL
);
3042 // If a child, leave wxWidgets to call the function on the actual
3045 return wxACC_NOT_IMPLEMENTED
;
3047 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3048 return wxACC_NOT_IMPLEMENTED
;
3050 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3051 return wxACC_NOT_IMPLEMENTED
;
3054 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3055 return wxACC_NOT_IMPLEMENTED
;
3058 //*role = wxROLE_SYSTEM_CLIENT;
3059 *role
= wxROLE_SYSTEM_CLIENT
;
3063 return wxACC_NOT_IMPLEMENTED
;
3067 // Returns a state constant.
3068 wxAccStatus
wxWindowAccessible::GetState(int childId
, long* state
)
3070 wxASSERT( GetWindow() != NULL
);
3074 // If a child, leave wxWidgets to call the function on the actual
3077 return wxACC_NOT_IMPLEMENTED
;
3079 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3080 return wxACC_NOT_IMPLEMENTED
;
3083 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3084 return wxACC_NOT_IMPLEMENTED
;
3087 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3088 return wxACC_NOT_IMPLEMENTED
;
3095 return wxACC_NOT_IMPLEMENTED
;
3099 // Returns a localized string representing the value for the object
3101 wxAccStatus
wxWindowAccessible::GetValue(int WXUNUSED(childId
), wxString
* WXUNUSED(strValue
))
3103 wxASSERT( GetWindow() != NULL
);
3107 return wxACC_NOT_IMPLEMENTED
;
3110 // Selects the object or child.
3111 wxAccStatus
wxWindowAccessible::Select(int WXUNUSED(childId
), wxAccSelectionFlags
WXUNUSED(selectFlags
))
3113 wxASSERT( GetWindow() != NULL
);
3117 return wxACC_NOT_IMPLEMENTED
;
3120 // Gets the window with the keyboard focus.
3121 // If childId is 0 and child is NULL, no object in
3122 // this subhierarchy has the focus.
3123 // If this object has the focus, child should be 'this'.
3124 wxAccStatus
wxWindowAccessible::GetFocus(int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(child
))
3126 wxASSERT( GetWindow() != NULL
);
3130 return wxACC_NOT_IMPLEMENTED
;
3134 // Gets a variant representing the selected children
3136 // Acceptable values:
3137 // - a null variant (IsNull() returns true)
3138 // - a list variant (GetType() == wxT("list")
3139 // - an integer representing the selected child element,
3140 // or 0 if this object is selected (GetType() == wxT("long")
3141 // - a "void*" pointer to a wxAccessible child object
3142 wxAccStatus
wxWindowAccessible::GetSelections(wxVariant
* WXUNUSED(selections
))
3144 wxASSERT( GetWindow() != NULL
);
3148 return wxACC_NOT_IMPLEMENTED
;
3150 #endif // wxUSE_VARIANT
3152 #endif // wxUSE_ACCESSIBILITY
3154 // ----------------------------------------------------------------------------
3156 // ----------------------------------------------------------------------------
3159 wxWindowBase::AdjustForLayoutDirection(wxCoord x
,
3161 wxCoord widthTotal
) const
3163 if ( GetLayoutDirection() == wxLayout_RightToLeft
)
3165 x
= widthTotal
- x
- width
;
3171 // ----------------------------------------------------------------------------
3172 // Window (and menu items) identifiers management
3173 // ----------------------------------------------------------------------------
3178 // this array contains, in packed form, the "in use" flags for the entire
3179 // auto-generated ids range: N-th element of the array contains the flags for
3180 // ids in [wxID_AUTO_LOWEST + 8*N, wxID_AUTO_LOWEST + 8*N + 7] range
3182 // initially no ids are in use and we allocate them consecutively, but after we
3183 // exhaust the entire range, we wrap around and reuse the ids freed in the
3185 wxByte gs_autoIdsInUse
[(wxID_AUTO_HIGHEST
- wxID_AUTO_LOWEST
+ 1)/8 + 1] = { 0 };
3187 // this is an optimization used until we wrap around wxID_AUTO_HIGHEST: if this
3188 // value is < wxID_AUTO_HIGHEST we know that we haven't wrapped yet and so can
3189 // allocate the ids simply by incrementing it
3190 static wxWindowID gs_nextControlId
= wxID_AUTO_LOWEST
;
3192 void MarkAutoIdUsed(wxWindowID id
)
3194 id
-= wxID_AUTO_LOWEST
;
3196 const int theByte
= id
/ 8;
3197 const int theBit
= id
% 8;
3199 gs_autoIdsInUse
[theByte
] |= 1 << theBit
;
3202 void FreeAutoId(wxWindowID id
)
3204 id
-= wxID_AUTO_LOWEST
;
3206 const int theByte
= id
/ 8;
3207 const int theBit
= id
% 8;
3209 gs_autoIdsInUse
[theByte
] &= ~(1 << theBit
);
3212 bool IsAutoIdInUse(wxWindowID id
)
3214 id
-= wxID_AUTO_LOWEST
;
3216 const int theByte
= id
/ 8;
3217 const int theBit
= id
% 8;
3219 return (gs_autoIdsInUse
[theByte
] & (1 << theBit
)) != 0;
3222 } // anonymous namespace
3226 bool wxWindowBase::IsAutoGeneratedId(wxWindowID id
)
3228 if ( id
< wxID_AUTO_LOWEST
|| id
> wxID_AUTO_HIGHEST
)
3231 // we shouldn't have any stray ids in this range
3232 wxASSERT_MSG( IsAutoIdInUse(id
), "unused automatically generated id?" );
3237 wxWindowID
wxWindowBase::NewControlId(int count
)
3239 wxASSERT_MSG( count
> 0, "can't allocate less than 1 id" );
3241 if ( gs_nextControlId
+ count
- 1 <= wxID_AUTO_HIGHEST
)
3243 // we haven't wrapped yet, so we can just grab the next count ids
3244 wxWindowID id
= gs_nextControlId
;
3247 MarkAutoIdUsed(gs_nextControlId
++);
3251 else // we've already wrapped or are now going to
3253 // brute-force search for the id values
3255 // number of consecutive free ids found so far
3258 for ( wxWindowID id
= wxID_AUTO_LOWEST
; id
<= wxID_AUTO_HIGHEST
; id
++ )
3260 if ( !IsAutoIdInUse(id
) )
3262 // found another consecutive available id
3264 if ( found
== count
)
3266 // mark all count consecutive free ids we found as being in
3267 // use now and rewind back to the start of available range
3270 MarkAutoIdUsed(id
--);
3275 else // this id is in use
3277 // reset the number of consecutive free values found
3283 // if we get here, there are not enough consecutive free ids
3287 void wxWindowBase::ReleaseControlId(wxWindowID id
)
3289 wxCHECK_RET( IsAutoGeneratedId(id
), "can't release non auto-generated id" );