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"
50 #if wxUSE_DRAG_AND_DROP
52 #endif // wxUSE_DRAG_AND_DROP
54 #if wxUSE_ACCESSIBILITY
55 #include "wx/access.h"
59 #include "wx/cshelp.h"
63 #include "wx/tooltip.h"
64 #endif // wxUSE_TOOLTIPS
70 #if wxUSE_SYSTEM_OPTIONS
71 #include "wx/sysopt.h"
74 // For reporting compile- and runtime version of GTK+ in the ctrl+alt+mclick dialog.
75 // The gtk includes don't pull any other headers in, at least not on my system - MR
78 #include <gtk/gtkversion.h>
80 #include <gtk/gtkfeatures.h>
84 #include "wx/platinfo.h"
87 WXDLLIMPEXP_DATA_CORE(wxWindowList
) wxTopLevelWindows
;
91 wxMenu
*wxCurrentPopupMenu
= NULL
;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
99 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
106 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
107 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
108 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
111 EVT_HELP(wxID_ANY
, wxWindowBase::OnHelp
)
116 // ============================================================================
117 // implementation of the common functionality of the wxWindow class
118 // ============================================================================
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // the default initialization
125 wxWindowBase::wxWindowBase()
127 // no window yet, no parent nor children
129 m_windowId
= wxID_ANY
;
131 // no constraints on the minimal window size
133 m_maxWidth
= wxDefaultCoord
;
135 m_maxHeight
= wxDefaultCoord
;
137 // invalidiated cache value
138 m_bestSizeCache
= wxDefaultSize
;
140 // window are created enabled and visible by default
144 // the default event handler is just this window
145 m_eventHandler
= this;
149 m_windowValidator
= NULL
;
150 #endif // wxUSE_VALIDATORS
152 // the colours/fonts are default for now, so leave m_font,
153 // m_backgroundColour and m_foregroundColour uninitialized and set those
159 m_inheritFont
= false;
165 m_backgroundStyle
= wxBG_STYLE_SYSTEM
;
167 #if wxUSE_CONSTRAINTS
168 // no constraints whatsoever
169 m_constraints
= NULL
;
170 m_constraintsInvolvedIn
= NULL
;
171 #endif // wxUSE_CONSTRAINTS
173 m_windowSizer
= NULL
;
174 m_containingSizer
= NULL
;
175 m_autoLayout
= false;
177 #if wxUSE_DRAG_AND_DROP
179 #endif // wxUSE_DRAG_AND_DROP
183 #endif // wxUSE_TOOLTIPS
187 #endif // wxUSE_CARET
190 m_hasCustomPalette
= false;
191 #endif // wxUSE_PALETTE
193 #if wxUSE_ACCESSIBILITY
197 m_virtualSize
= wxDefaultSize
;
199 m_scrollHelper
= NULL
;
201 m_windowVariant
= wxWINDOW_VARIANT_NORMAL
;
202 #if wxUSE_SYSTEM_OPTIONS
203 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT
) )
205 m_windowVariant
= (wxWindowVariant
) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT
) ;
209 // Whether we're using the current theme for this window (wxGTK only for now)
210 m_themeEnabled
= false;
212 // This is set to true by SendDestroyEvent() which should be called by the
213 // most derived class to ensure that the destruction event is sent as soon
214 // as possible to allow its handlers to still see the undestroyed window
215 m_isBeingDeleted
= false;
220 // common part of window creation process
221 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
223 const wxPoint
& WXUNUSED(pos
),
224 const wxSize
& WXUNUSED(size
),
226 const wxValidator
& wxVALIDATOR_PARAM(validator
),
227 const wxString
& name
)
230 // wxGTK doesn't allow to create controls with static box as the parent so
231 // this will result in a crash when the program is ported to wxGTK so warn
234 // if you get this assert, the correct solution is to create the controls
235 // as siblings of the static box
236 wxASSERT_MSG( !parent
|| !wxDynamicCast(parent
, wxStaticBox
),
237 _T("wxStaticBox can't be used as a window parent!") );
238 #endif // wxUSE_STATBOX
240 // ids are limited to 16 bits under MSW so if you care about portability,
241 // it's not a good idea to use ids out of this range (and negative ids are
242 // reserved for wxWidgets own usage)
243 wxASSERT_MSG( id
== wxID_ANY
|| (id
>= 0 && id
< 32767) ||
244 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
245 _T("invalid id value") );
247 // generate a new id if the user doesn't care about it
248 if ( id
== wxID_ANY
)
250 m_windowId
= NewControlId();
252 else // valid id specified
257 // don't use SetWindowStyleFlag() here, this function should only be called
258 // to change the flag after creation as it tries to reflect the changes in
259 // flags by updating the window dynamically and we don't need this here
260 m_windowStyle
= style
;
266 SetValidator(validator
);
267 #endif // wxUSE_VALIDATORS
269 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
270 // have it too - like this it's possible to set it only in the top level
271 // dialog/frame and all children will inherit it by defult
272 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
274 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY
);
280 bool wxWindowBase::ToggleWindowStyle(int flag
)
282 wxASSERT_MSG( flag
, _T("flags with 0 value can't be toggled") );
285 long style
= GetWindowStyleFlag();
291 else // currently off
297 SetWindowStyleFlag(style
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
307 wxWindowBase::~wxWindowBase()
309 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
311 // FIXME if these 2 cases result from programming errors in the user code
312 // we should probably assert here instead of silently fixing them
314 // Just in case the window has been Closed, but we're then deleting
315 // immediately: don't leave dangling pointers.
316 wxPendingDelete
.DeleteObject(this);
318 // Just in case we've loaded a top-level window via LoadNativeDialog but
319 // we weren't a dialog class
320 wxTopLevelWindows
.DeleteObject((wxWindow
*)this);
323 // The associated popup menu can still be alive, disassociate from it in
325 if ( wxCurrentPopupMenu
&& wxCurrentPopupMenu
->GetInvokingWindow() == this )
326 wxCurrentPopupMenu
->SetInvokingWindow(NULL
);
327 #endif // wxUSE_MENUS
329 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
331 // notify the parent about this window destruction
333 m_parent
->RemoveChild(this);
337 #endif // wxUSE_CARET
340 delete m_windowValidator
;
341 #endif // wxUSE_VALIDATORS
343 #if wxUSE_CONSTRAINTS
344 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
345 // at deleted windows as they delete themselves.
346 DeleteRelatedConstraints();
350 // This removes any dangling pointers to this window in other windows'
351 // constraintsInvolvedIn lists.
352 UnsetConstraints(m_constraints
);
353 delete m_constraints
;
354 m_constraints
= NULL
;
356 #endif // wxUSE_CONSTRAINTS
358 if ( m_containingSizer
)
359 m_containingSizer
->Detach( (wxWindow
*)this );
361 delete m_windowSizer
;
363 #if wxUSE_DRAG_AND_DROP
365 #endif // wxUSE_DRAG_AND_DROP
369 #endif // wxUSE_TOOLTIPS
371 #if wxUSE_ACCESSIBILITY
376 // NB: this has to be called unconditionally, because we don't know
377 // whether this window has associated help text or not
378 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
380 helpProvider
->RemoveHelp(this);
384 bool wxWindowBase::IsBeingDeleted() const
386 return m_isBeingDeleted
||
387 (!IsTopLevel() && m_parent
&& m_parent
->IsBeingDeleted());
390 void wxWindowBase::SendDestroyEvent()
392 if ( m_isBeingDeleted
)
394 // we could have been already called from a more derived class dtor,
395 // e.g. ~wxTLW calls us and so does ~wxWindow and the latter call
396 // should be simply ignored
400 m_isBeingDeleted
= true;
402 wxWindowDestroyEvent event
;
403 event
.SetEventObject(this);
404 event
.SetId(GetId());
405 GetEventHandler()->ProcessEvent(event
);
408 bool wxWindowBase::Destroy()
415 bool wxWindowBase::Close(bool force
)
417 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
418 event
.SetEventObject(this);
419 event
.SetCanVeto(!force
);
421 // return false if window wasn't closed because the application vetoed the
423 return HandleWindowEvent(event
) && !event
.GetVeto();
426 bool wxWindowBase::DestroyChildren()
428 wxWindowList::compatibility_iterator node
;
431 // we iterate until the list becomes empty
432 node
= GetChildren().GetFirst();
436 wxWindow
*child
= node
->GetData();
438 // note that we really want to call delete and not ->Destroy() here
439 // because we want to delete the child immediately, before we are
440 // deleted, and delayed deletion would result in problems as our (top
441 // level) child could outlive its parent
444 wxASSERT_MSG( !GetChildren().Find(child
),
445 wxT("child didn't remove itself using RemoveChild()") );
451 // ----------------------------------------------------------------------------
452 // size/position related methods
453 // ----------------------------------------------------------------------------
455 // centre the window with respect to its parent in either (or both) directions
456 void wxWindowBase::DoCentre(int dir
)
458 wxCHECK_RET( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent(),
459 _T("this method only implements centering child windows") );
461 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir
));
464 // fits the window around the children
465 void wxWindowBase::Fit()
467 if ( !GetChildren().empty() )
469 SetSize(GetBestSize());
471 //else: do nothing if we have no children
474 // fits virtual size (ie. scrolled area etc.) around children
475 void wxWindowBase::FitInside()
477 if ( GetChildren().GetCount() > 0 )
479 SetVirtualSize( GetBestVirtualSize() );
483 // On Mac, scrollbars are explicitly children.
485 static bool wxHasRealChildren(const wxWindowBase
* win
)
487 int realChildCount
= 0;
489 for ( wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
491 node
= node
->GetNext() )
493 wxWindow
*win
= node
->GetData();
494 if ( !win
->IsTopLevel() && win
->IsShown() && !win
->IsKindOf(CLASSINFO(wxScrollBar
)))
497 return (realChildCount
> 0);
501 void wxWindowBase::InvalidateBestSize()
503 m_bestSizeCache
= wxDefaultSize
;
505 // parent's best size calculation may depend on its children's
506 // as long as child window we are in is not top level window itself
507 // (because the TLW size is never resized automatically)
508 // so let's invalidate it as well to be safe:
509 if (m_parent
&& !IsTopLevel())
510 m_parent
->InvalidateBestSize();
513 // return the size best suited for the current window
514 wxSize
wxWindowBase::DoGetBestSize() const
520 best
= m_windowSizer
->GetMinSize();
522 #if wxUSE_CONSTRAINTS
523 else if ( m_constraints
)
525 wxConstCast(this, wxWindowBase
)->SatisfyConstraints();
527 // our minimal acceptable size is such that all our windows fit inside
531 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
533 node
= node
->GetNext() )
535 wxLayoutConstraints
*c
= node
->GetData()->GetConstraints();
538 // it's not normal that we have an unconstrained child, but
539 // what can we do about it?
543 int x
= c
->right
.GetValue(),
544 y
= c
->bottom
.GetValue();
552 // TODO: we must calculate the overlaps somehow, otherwise we
553 // will never return a size bigger than the current one :-(
556 best
= wxSize(maxX
, maxY
);
558 #endif // wxUSE_CONSTRAINTS
559 else if ( !GetChildren().empty()
561 && wxHasRealChildren(this)
565 // our minimal acceptable size is such that all our visible child
566 // windows fit inside
570 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
572 node
= node
->GetNext() )
574 wxWindow
*win
= node
->GetData();
575 if ( win
->IsTopLevel()
578 || wxDynamicCast(win
, wxStatusBar
)
579 #endif // wxUSE_STATUSBAR
582 // dialogs and frames lie in different top level windows -
583 // don't deal with them here; as for the status bars, they
584 // don't lie in the client area at all
589 win
->GetPosition(&wx
, &wy
);
591 // if the window hadn't been positioned yet, assume that it is in
593 if ( wx
== wxDefaultCoord
)
595 if ( wy
== wxDefaultCoord
)
598 win
->GetSize(&ww
, &wh
);
599 if ( wx
+ ww
> maxX
)
601 if ( wy
+ wh
> maxY
)
605 best
= wxSize(maxX
, maxY
);
607 else // ! has children
609 // for a generic window there is no natural best size so, if the
610 // minimal size is not set, use the current size but take care to
611 // remember it as minimal size for the next time because our best size
612 // should be constant: otherwise we could get into a situation when the
613 // window is initially at some size, then expanded to a larger size and
614 // then, when the containing window is shrunk back (because our initial
615 // best size had been used for computing the parent min size), we can't
616 // be shrunk back any more because our best size is now bigger
617 wxSize size
= GetMinSize();
618 if ( !size
.IsFullySpecified() )
620 size
.SetDefaults(GetSize());
621 wxConstCast(this, wxWindowBase
)->SetMinSize(size
);
624 // return as-is, unadjusted by the client size difference.
628 // Add any difference between size and client size
629 wxSize diff
= GetSize() - GetClientSize();
630 best
.x
+= wxMax(0, diff
.x
);
631 best
.y
+= wxMax(0, diff
.y
);
636 // helper of GetWindowBorderSize(): as many ports don't implement support for
637 // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
638 // fallbacks in this case
639 static int wxGetMetricOrDefault(wxSystemMetric what
, const wxWindowBase
* win
)
641 int rc
= wxSystemSettings::GetMetric(
642 what
, static_cast<wxWindow
*>(const_cast<wxWindowBase
*>(win
)));
649 // 2D border is by default 1 pixel wide
655 // 3D borders are by default 2 pixels
660 wxFAIL_MSG( _T("unexpected wxGetMetricOrDefault() argument") );
668 wxSize
wxWindowBase::GetWindowBorderSize() const
672 switch ( GetBorder() )
675 // nothing to do, size is already (0, 0)
678 case wxBORDER_SIMPLE
:
679 case wxBORDER_STATIC
:
680 size
.x
= wxGetMetricOrDefault(wxSYS_BORDER_X
, this);
681 size
.y
= wxGetMetricOrDefault(wxSYS_BORDER_Y
, this);
684 case wxBORDER_SUNKEN
:
685 case wxBORDER_RAISED
:
686 size
.x
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X
, this),
687 wxGetMetricOrDefault(wxSYS_BORDER_X
, this));
688 size
.y
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y
, this),
689 wxGetMetricOrDefault(wxSYS_BORDER_Y
, this));
692 case wxBORDER_DOUBLE
:
693 size
.x
= wxGetMetricOrDefault(wxSYS_EDGE_X
, this) +
694 wxGetMetricOrDefault(wxSYS_BORDER_X
, this);
695 size
.y
= wxGetMetricOrDefault(wxSYS_EDGE_Y
, this) +
696 wxGetMetricOrDefault(wxSYS_BORDER_Y
, this);
700 wxFAIL_MSG(_T("Unknown border style."));
704 // we have borders on both sides
708 wxSize
wxWindowBase::GetEffectiveMinSize() const
710 // merge the best size with the min size, giving priority to the min size
711 wxSize min
= GetMinSize();
712 if (min
.x
== wxDefaultCoord
|| min
.y
== wxDefaultCoord
)
714 wxSize best
= GetBestSize();
715 if (min
.x
== wxDefaultCoord
) min
.x
= best
.x
;
716 if (min
.y
== wxDefaultCoord
) min
.y
= best
.y
;
722 void wxWindowBase::SetInitialSize(const wxSize
& size
)
724 // Set the min size to the size passed in. This will usually either be
725 // wxDefaultSize or the size passed to this window's ctor/Create function.
728 // Merge the size with the best size if needed
729 wxSize best
= GetEffectiveMinSize();
731 // If the current size doesn't match then change it
732 if (GetSize() != best
)
737 // by default the origin is not shifted
738 wxPoint
wxWindowBase::GetClientAreaOrigin() const
743 wxSize
wxWindowBase::ClientToWindowSize(const wxSize
& size
) const
745 const wxSize
diff(GetSize() - GetClientSize());
747 return wxSize(size
.x
== -1 ? -1 : size
.x
+ diff
.x
,
748 size
.y
== -1 ? -1 : size
.y
+ diff
.y
);
751 wxSize
wxWindowBase::WindowToClientSize(const wxSize
& size
) const
753 const wxSize
diff(GetSize() - GetClientSize());
755 return wxSize(size
.x
== -1 ? -1 : size
.x
- diff
.x
,
756 size
.y
== -1 ? -1 : size
.y
- diff
.y
);
759 void wxWindowBase::SetWindowVariant( wxWindowVariant variant
)
761 if ( m_windowVariant
!= variant
)
763 m_windowVariant
= variant
;
765 DoSetWindowVariant(variant
);
769 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant
)
771 // adjust the font height to correspond to our new variant (notice that
772 // we're only called if something really changed)
773 wxFont font
= GetFont();
774 int size
= font
.GetPointSize();
777 case wxWINDOW_VARIANT_NORMAL
:
780 case wxWINDOW_VARIANT_SMALL
:
785 case wxWINDOW_VARIANT_MINI
:
790 case wxWINDOW_VARIANT_LARGE
:
796 wxFAIL_MSG(_T("unexpected window variant"));
800 font
.SetPointSize(size
);
804 void wxWindowBase::DoSetSizeHints( int minW
, int minH
,
806 int WXUNUSED(incW
), int WXUNUSED(incH
) )
808 wxCHECK_RET( (minW
== wxDefaultCoord
|| maxW
== wxDefaultCoord
|| minW
<= maxW
) &&
809 (minH
== wxDefaultCoord
|| maxH
== wxDefaultCoord
|| minH
<= maxH
),
810 _T("min width/height must be less than max width/height!") );
819 #if WXWIN_COMPATIBILITY_2_8
820 void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW
), int WXUNUSED(minH
),
821 int WXUNUSED(maxW
), int WXUNUSED(maxH
))
825 void wxWindowBase::SetVirtualSizeHints(const wxSize
& WXUNUSED(minsize
),
826 const wxSize
& WXUNUSED(maxsize
))
829 #endif // WXWIN_COMPATIBILITY_2_8
831 void wxWindowBase::DoSetVirtualSize( int x
, int y
)
833 m_virtualSize
= wxSize(x
, y
);
836 wxSize
wxWindowBase::DoGetVirtualSize() const
838 // we should use the entire client area so if it is greater than our
839 // virtual size, expand it to fit (otherwise if the window is big enough we
840 // wouldn't be using parts of it)
841 wxSize size
= GetClientSize();
842 if ( m_virtualSize
.x
> size
.x
)
843 size
.x
= m_virtualSize
.x
;
845 if ( m_virtualSize
.y
>= size
.y
)
846 size
.y
= m_virtualSize
.y
;
851 void wxWindowBase::DoGetScreenPosition(int *x
, int *y
) const
853 // screen position is the same as (0, 0) in client coords for non TLWs (and
854 // TLWs override this method)
860 ClientToScreen(x
, y
);
863 void wxWindowBase::SendSizeEvent(int flags
)
865 wxSizeEvent
event(GetSize(), GetId());
866 event
.SetEventObject(this);
867 if ( flags
& wxSEND_EVENT_POST
)
868 wxPostEvent(this, event
);
870 HandleWindowEvent(event
);
873 void wxWindowBase::SendSizeEventToParent(int flags
)
875 wxWindow
* const parent
= GetParent();
876 if ( parent
&& !parent
->IsBeingDeleted() )
877 parent
->SendSizeEvent(flags
);
880 // ----------------------------------------------------------------------------
881 // show/hide/enable/disable the window
882 // ----------------------------------------------------------------------------
884 bool wxWindowBase::Show(bool show
)
886 if ( show
!= m_isShown
)
898 bool wxWindowBase::IsEnabled() const
900 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
903 void wxWindowBase::NotifyWindowOnEnableChange(bool enabled
)
905 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
907 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
911 // If we are top-level then the logic doesn't apply - otherwise
912 // showing a modal dialog would result in total greying out (and ungreying
913 // out later) of everything which would be really ugly
917 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
919 node
= node
->GetNext() )
921 wxWindowBase
* const child
= node
->GetData();
922 if ( !child
->IsTopLevel() && child
->IsThisEnabled() )
923 child
->NotifyWindowOnEnableChange(enabled
);
927 bool wxWindowBase::Enable(bool enable
)
929 if ( enable
== IsThisEnabled() )
932 m_isEnabled
= enable
;
934 #ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
936 #else // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
937 wxWindowBase
* const parent
= GetParent();
938 if( !IsTopLevel() && parent
&& !parent
->IsEnabled() )
942 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
944 NotifyWindowOnEnableChange(enable
);
949 bool wxWindowBase::IsShownOnScreen() const
951 // A window is shown on screen if it itself is shown and so are all its
952 // parents. But if a window is toplevel one, then its always visible on
953 // screen if IsShown() returns true, even if it has a hidden parent.
955 (IsTopLevel() || GetParent() == NULL
|| GetParent()->IsShownOnScreen());
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 bool wxWindowBase::IsTopLevel() const
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
971 void wxWindowBase::Freeze()
973 if ( !m_freezeCount
++ )
975 // physically freeze this window:
978 // and recursively freeze all children:
979 for ( wxWindowList::iterator i
= GetChildren().begin();
980 i
!= GetChildren().end(); ++i
)
982 wxWindow
*child
= *i
;
983 if ( child
->IsTopLevel() )
991 void wxWindowBase::Thaw()
993 wxASSERT_MSG( m_freezeCount
, "Thaw() without matching Freeze()" );
995 if ( !--m_freezeCount
)
997 // recursively thaw all children:
998 for ( wxWindowList::iterator i
= GetChildren().begin();
999 i
!= GetChildren().end(); ++i
)
1001 wxWindow
*child
= *i
;
1002 if ( child
->IsTopLevel() )
1008 // physically thaw this window:
1013 // ----------------------------------------------------------------------------
1014 // reparenting the window
1015 // ----------------------------------------------------------------------------
1017 void wxWindowBase::AddChild(wxWindowBase
*child
)
1019 wxCHECK_RET( child
, wxT("can't add a NULL child") );
1021 // this should never happen and it will lead to a crash later if it does
1022 // because RemoveChild() will remove only one node from the children list
1023 // and the other(s) one(s) will be left with dangling pointers in them
1024 wxASSERT_MSG( !GetChildren().Find((wxWindow
*)child
), _T("AddChild() called twice") );
1026 GetChildren().Append((wxWindow
*)child
);
1027 child
->SetParent(this);
1029 // adding a child while frozen will assert when thawed, so freeze it as if
1030 // it had been already present when we were frozen
1031 if ( IsFrozen() && !child
->IsTopLevel() )
1035 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
1037 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
1039 // removing a child while frozen may result in permanently frozen window
1040 // if used e.g. from Reparent(), so thaw it
1042 // NB: IsTopLevel() doesn't return true any more when a TLW child is being
1043 // removed from its ~wxWindowBase, so check for IsBeingDeleted() too
1044 if ( IsFrozen() && !child
->IsBeingDeleted() && !child
->IsTopLevel() )
1047 GetChildren().DeleteObject((wxWindow
*)child
);
1048 child
->SetParent(NULL
);
1051 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
1053 wxWindow
*oldParent
= GetParent();
1054 if ( newParent
== oldParent
)
1060 const bool oldEnabledState
= IsEnabled();
1062 // unlink this window from the existing parent.
1065 oldParent
->RemoveChild(this);
1069 wxTopLevelWindows
.DeleteObject((wxWindow
*)this);
1072 // add it to the new one
1075 newParent
->AddChild(this);
1079 wxTopLevelWindows
.Append((wxWindow
*)this);
1082 // We need to notify window (and its subwindows) if by changing the parent
1083 // we also change our enabled/disabled status.
1084 const bool newEnabledState
= IsEnabled();
1085 if ( newEnabledState
!= oldEnabledState
)
1087 NotifyWindowOnEnableChange(newEnabledState
);
1093 // ----------------------------------------------------------------------------
1094 // event handler stuff
1095 // ----------------------------------------------------------------------------
1097 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
1099 wxEvtHandler
*handlerOld
= GetEventHandler();
1101 handler
->SetNextHandler(handlerOld
);
1104 GetEventHandler()->SetPreviousHandler(handler
);
1106 SetEventHandler(handler
);
1109 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
1111 wxEvtHandler
*handlerA
= GetEventHandler();
1114 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
1115 handlerA
->SetNextHandler(NULL
);
1118 handlerB
->SetPreviousHandler(NULL
);
1119 SetEventHandler(handlerB
);
1121 if ( deleteHandler
)
1131 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
1133 wxCHECK_MSG( handler
, false, _T("RemoveEventHandler(NULL) called") );
1135 wxEvtHandler
*handlerPrev
= NULL
,
1136 *handlerCur
= GetEventHandler();
1137 while ( handlerCur
)
1139 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
1141 if ( handlerCur
== handler
)
1145 handlerPrev
->SetNextHandler(handlerNext
);
1149 SetEventHandler(handlerNext
);
1154 handlerNext
->SetPreviousHandler ( handlerPrev
);
1157 handler
->SetNextHandler(NULL
);
1158 handler
->SetPreviousHandler(NULL
);
1163 handlerPrev
= handlerCur
;
1164 handlerCur
= handlerNext
;
1167 wxFAIL_MSG( _T("where has the event handler gone?") );
1172 bool wxWindowBase::HandleWindowEvent(wxEvent
& event
) const
1174 return GetEventHandler()->SafelyProcessEvent(event
);
1177 // ----------------------------------------------------------------------------
1178 // colours, fonts &c
1179 // ----------------------------------------------------------------------------
1181 void wxWindowBase::InheritAttributes()
1183 const wxWindowBase
* const parent
= GetParent();
1187 // we only inherit attributes which had been explicitly set for the parent
1188 // which ensures that this only happens if the user really wants it and
1189 // not by default which wouldn't make any sense in modern GUIs where the
1190 // controls don't all use the same fonts (nor colours)
1191 if ( parent
->m_inheritFont
&& !m_hasFont
)
1192 SetFont(parent
->GetFont());
1194 // in addition, there is a possibility to explicitly forbid inheriting
1195 // colours at each class level by overriding ShouldInheritColours()
1196 if ( ShouldInheritColours() )
1198 if ( parent
->m_inheritFgCol
&& !m_hasFgCol
)
1199 SetForegroundColour(parent
->GetForegroundColour());
1201 // inheriting (solid) background colour is wrong as it totally breaks
1202 // any kind of themed backgrounds
1204 // instead, the controls should use the same background as their parent
1205 // (ideally by not drawing it at all)
1207 if ( parent
->m_inheritBgCol
&& !m_hasBgCol
)
1208 SetBackgroundColour(parent
->GetBackgroundColour());
1213 /* static */ wxVisualAttributes
1214 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1216 // it is important to return valid values for all attributes from here,
1217 // GetXXX() below rely on this
1218 wxVisualAttributes attrs
;
1219 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
1220 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1222 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1223 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1224 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1225 // colour on other platforms.
1227 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1228 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1230 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1235 wxColour
wxWindowBase::GetBackgroundColour() const
1237 if ( !m_backgroundColour
.IsOk() )
1239 wxASSERT_MSG( !m_hasBgCol
, _T("we have invalid explicit bg colour?") );
1241 // get our default background colour
1242 wxColour colBg
= GetDefaultAttributes().colBg
;
1244 // we must return some valid colour to avoid redoing this every time
1245 // and also to avoid surprizing the applications written for older
1246 // wxWidgets versions where GetBackgroundColour() always returned
1247 // something -- so give them something even if it doesn't make sense
1248 // for this window (e.g. it has a themed background)
1250 colBg
= GetClassDefaultAttributes().colBg
;
1255 return m_backgroundColour
;
1258 wxColour
wxWindowBase::GetForegroundColour() const
1260 // logic is the same as above
1261 if ( !m_hasFgCol
&& !m_foregroundColour
.Ok() )
1263 wxColour colFg
= GetDefaultAttributes().colFg
;
1265 if ( !colFg
.IsOk() )
1266 colFg
= GetClassDefaultAttributes().colFg
;
1271 return m_foregroundColour
;
1274 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
1276 if ( colour
== m_backgroundColour
)
1279 m_hasBgCol
= colour
.IsOk();
1280 if ( m_backgroundStyle
!= wxBG_STYLE_CUSTOM
)
1281 m_backgroundStyle
= m_hasBgCol
? wxBG_STYLE_COLOUR
: wxBG_STYLE_SYSTEM
;
1283 m_inheritBgCol
= m_hasBgCol
;
1284 m_backgroundColour
= colour
;
1285 SetThemeEnabled( !m_hasBgCol
&& !m_foregroundColour
.Ok() );
1289 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
1291 if (colour
== m_foregroundColour
)
1294 m_hasFgCol
= colour
.IsOk();
1295 m_inheritFgCol
= m_hasFgCol
;
1296 m_foregroundColour
= colour
;
1297 SetThemeEnabled( !m_hasFgCol
&& !m_backgroundColour
.Ok() );
1301 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
1303 // setting an invalid cursor is ok, it means that we don't have any special
1305 if ( m_cursor
.IsSameAs(cursor
) )
1316 wxFont
wxWindowBase::GetFont() const
1318 // logic is the same as in GetBackgroundColour()
1319 if ( !m_font
.IsOk() )
1321 wxASSERT_MSG( !m_hasFont
, _T("we have invalid explicit font?") );
1323 wxFont font
= GetDefaultAttributes().font
;
1325 font
= GetClassDefaultAttributes().font
;
1333 bool wxWindowBase::SetFont(const wxFont
& font
)
1335 if ( font
== m_font
)
1342 m_hasFont
= font
.IsOk();
1343 m_inheritFont
= m_hasFont
;
1345 InvalidateBestSize();
1352 void wxWindowBase::SetPalette(const wxPalette
& pal
)
1354 m_hasCustomPalette
= true;
1357 // VZ: can anyone explain me what do we do here?
1358 wxWindowDC
d((wxWindow
*) this);
1362 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
1364 wxWindow
*win
= (wxWindow
*)this;
1365 while ( win
&& !win
->HasCustomPalette() )
1367 win
= win
->GetParent();
1373 #endif // wxUSE_PALETTE
1376 void wxWindowBase::SetCaret(wxCaret
*caret
)
1387 wxASSERT_MSG( m_caret
->GetWindow() == this,
1388 wxT("caret should be created associated to this window") );
1391 #endif // wxUSE_CARET
1393 #if wxUSE_VALIDATORS
1394 // ----------------------------------------------------------------------------
1396 // ----------------------------------------------------------------------------
1398 void wxWindowBase::SetValidator(const wxValidator
& validator
)
1400 if ( m_windowValidator
)
1401 delete m_windowValidator
;
1403 m_windowValidator
= (wxValidator
*)validator
.Clone();
1405 if ( m_windowValidator
)
1406 m_windowValidator
->SetWindow(this);
1408 #endif // wxUSE_VALIDATORS
1410 // ----------------------------------------------------------------------------
1411 // update region stuff
1412 // ----------------------------------------------------------------------------
1414 wxRect
wxWindowBase::GetUpdateClientRect() const
1416 wxRegion rgnUpdate
= GetUpdateRegion();
1417 rgnUpdate
.Intersect(GetClientRect());
1418 wxRect rectUpdate
= rgnUpdate
.GetBox();
1419 wxPoint ptOrigin
= GetClientAreaOrigin();
1420 rectUpdate
.x
-= ptOrigin
.x
;
1421 rectUpdate
.y
-= ptOrigin
.y
;
1426 bool wxWindowBase::DoIsExposed(int x
, int y
) const
1428 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
1431 bool wxWindowBase::DoIsExposed(int x
, int y
, int w
, int h
) const
1433 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
1436 void wxWindowBase::ClearBackground()
1438 // wxGTK uses its own version, no need to add never used code
1440 wxClientDC
dc((wxWindow
*)this);
1441 wxBrush
brush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID
);
1442 dc
.SetBackground(brush
);
1447 // ----------------------------------------------------------------------------
1448 // find child window by id or name
1449 // ----------------------------------------------------------------------------
1451 wxWindow
*wxWindowBase::FindWindow(long id
) const
1453 if ( id
== m_windowId
)
1454 return (wxWindow
*)this;
1456 wxWindowBase
*res
= NULL
;
1457 wxWindowList::compatibility_iterator node
;
1458 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1460 wxWindowBase
*child
= node
->GetData();
1461 res
= child
->FindWindow( id
);
1464 return (wxWindow
*)res
;
1467 wxWindow
*wxWindowBase::FindWindow(const wxString
& name
) const
1469 if ( name
== m_windowName
)
1470 return (wxWindow
*)this;
1472 wxWindowBase
*res
= NULL
;
1473 wxWindowList::compatibility_iterator node
;
1474 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1476 wxWindow
*child
= node
->GetData();
1477 res
= child
->FindWindow(name
);
1480 return (wxWindow
*)res
;
1484 // find any window by id or name or label: If parent is non-NULL, look through
1485 // children for a label or title matching the specified string. If NULL, look
1486 // through all top-level windows.
1488 // to avoid duplicating code we reuse the same helper function but with
1489 // different comparators
1491 typedef bool (*wxFindWindowCmp
)(const wxWindow
*win
,
1492 const wxString
& label
, long id
);
1495 bool wxFindWindowCmpLabels(const wxWindow
*win
, const wxString
& label
,
1498 return win
->GetLabel() == label
;
1502 bool wxFindWindowCmpNames(const wxWindow
*win
, const wxString
& label
,
1505 return win
->GetName() == label
;
1509 bool wxFindWindowCmpIds(const wxWindow
*win
, const wxString
& WXUNUSED(label
),
1512 return win
->GetId() == id
;
1515 // recursive helper for the FindWindowByXXX() functions
1517 wxWindow
*wxFindWindowRecursively(const wxWindow
*parent
,
1518 const wxString
& label
,
1520 wxFindWindowCmp cmp
)
1524 // see if this is the one we're looking for
1525 if ( (*cmp
)(parent
, label
, id
) )
1526 return (wxWindow
*)parent
;
1528 // It wasn't, so check all its children
1529 for ( wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
1531 node
= node
->GetNext() )
1533 // recursively check each child
1534 wxWindow
*win
= (wxWindow
*)node
->GetData();
1535 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1545 // helper for FindWindowByXXX()
1547 wxWindow
*wxFindWindowHelper(const wxWindow
*parent
,
1548 const wxString
& label
,
1550 wxFindWindowCmp cmp
)
1554 // just check parent and all its children
1555 return wxFindWindowRecursively(parent
, label
, id
, cmp
);
1558 // start at very top of wx's windows
1559 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1561 node
= node
->GetNext() )
1563 // recursively check each window & its children
1564 wxWindow
*win
= node
->GetData();
1565 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1575 wxWindowBase::FindWindowByLabel(const wxString
& title
, const wxWindow
*parent
)
1577 return wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpLabels
);
1582 wxWindowBase::FindWindowByName(const wxString
& title
, const wxWindow
*parent
)
1584 wxWindow
*win
= wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpNames
);
1588 // fall back to the label
1589 win
= FindWindowByLabel(title
, parent
);
1597 wxWindowBase::FindWindowById( long id
, const wxWindow
* parent
)
1599 return wxFindWindowHelper(parent
, wxEmptyString
, id
, wxFindWindowCmpIds
);
1602 // ----------------------------------------------------------------------------
1603 // dialog oriented functions
1604 // ----------------------------------------------------------------------------
1606 void wxWindowBase::MakeModal(bool modal
)
1608 // Disable all other windows
1611 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1614 wxWindow
*win
= node
->GetData();
1616 win
->Enable(!modal
);
1618 node
= node
->GetNext();
1623 bool wxWindowBase::Validate()
1625 #if wxUSE_VALIDATORS
1626 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1628 wxWindowList::compatibility_iterator node
;
1629 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1631 wxWindowBase
*child
= node
->GetData();
1632 wxValidator
*validator
= child
->GetValidator();
1633 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
1638 if ( recurse
&& !child
->Validate() )
1643 #endif // wxUSE_VALIDATORS
1648 bool wxWindowBase::TransferDataToWindow()
1650 #if wxUSE_VALIDATORS
1651 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1653 wxWindowList::compatibility_iterator node
;
1654 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1656 wxWindowBase
*child
= node
->GetData();
1657 wxValidator
*validator
= child
->GetValidator();
1658 if ( validator
&& !validator
->TransferToWindow() )
1660 wxLogWarning(_("Could not transfer data to window"));
1662 wxLog::FlushActive();
1670 if ( !child
->TransferDataToWindow() )
1672 // warning already given
1677 #endif // wxUSE_VALIDATORS
1682 bool wxWindowBase::TransferDataFromWindow()
1684 #if wxUSE_VALIDATORS
1685 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1687 wxWindowList::compatibility_iterator node
;
1688 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1690 wxWindow
*child
= node
->GetData();
1691 wxValidator
*validator
= child
->GetValidator();
1692 if ( validator
&& !validator
->TransferFromWindow() )
1694 // nop warning here because the application is supposed to give
1695 // one itself - we don't know here what might have gone wrongly
1702 if ( !child
->TransferDataFromWindow() )
1704 // warning already given
1709 #endif // wxUSE_VALIDATORS
1714 void wxWindowBase::InitDialog()
1716 wxInitDialogEvent
event(GetId());
1717 event
.SetEventObject( this );
1718 GetEventHandler()->ProcessEvent(event
);
1721 // ----------------------------------------------------------------------------
1722 // context-sensitive help support
1723 // ----------------------------------------------------------------------------
1727 // associate this help text with this window
1728 void wxWindowBase::SetHelpText(const wxString
& text
)
1730 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1733 helpProvider
->AddHelp(this, text
);
1737 #if WXWIN_COMPATIBILITY_2_8
1738 // associate this help text with all windows with the same id as this
1740 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1742 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1745 helpProvider
->AddHelp(GetId(), text
);
1748 #endif // WXWIN_COMPATIBILITY_2_8
1750 // get the help string associated with this window (may be empty)
1751 // default implementation forwards calls to the help provider
1753 wxWindowBase::GetHelpTextAtPoint(const wxPoint
& WXUNUSED(pt
),
1754 wxHelpEvent::Origin
WXUNUSED(origin
)) const
1757 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1760 text
= helpProvider
->GetHelp(this);
1766 // show help for this window
1767 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1769 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1772 wxPoint pos
= event
.GetPosition();
1773 const wxHelpEvent::Origin origin
= event
.GetOrigin();
1774 if ( origin
== wxHelpEvent::Origin_Keyboard
)
1776 // if the help event was generated from keyboard it shouldn't
1777 // appear at the mouse position (which is still the only position
1778 // associated with help event) if the mouse is far away, although
1779 // we still do use the mouse position if it's over the window
1780 // because we suppose the user looks approximately at the mouse
1781 // already and so it would be more convenient than showing tooltip
1782 // at some arbitrary position which can be quite far from it
1783 const wxRect rectClient
= GetClientRect();
1784 if ( !rectClient
.Contains(ScreenToClient(pos
)) )
1786 // position help slightly under and to the right of this window
1787 pos
= ClientToScreen(wxPoint(
1789 rectClient
.height
+ GetCharHeight()
1794 if ( helpProvider
->ShowHelpAtPoint(this, pos
, origin
) )
1796 // skip the event.Skip() below
1804 #endif // wxUSE_HELP
1806 // ----------------------------------------------------------------------------
1808 // ----------------------------------------------------------------------------
1812 void wxWindowBase::SetToolTip( const wxString
&tip
)
1814 // don't create the new tooltip if we already have one
1817 m_tooltip
->SetTip( tip
);
1821 SetToolTip( new wxToolTip( tip
) );
1824 // setting empty tooltip text does not remove the tooltip any more - use
1825 // SetToolTip(NULL) for this
1828 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1830 if ( m_tooltip
!= tooltip
)
1835 m_tooltip
= tooltip
;
1839 #endif // wxUSE_TOOLTIPS
1841 // ----------------------------------------------------------------------------
1842 // constraints and sizers
1843 // ----------------------------------------------------------------------------
1845 #if wxUSE_CONSTRAINTS
1847 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1849 if ( m_constraints
)
1851 UnsetConstraints(m_constraints
);
1852 delete m_constraints
;
1854 m_constraints
= constraints
;
1855 if ( m_constraints
)
1857 // Make sure other windows know they're part of a 'meaningful relationship'
1858 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1859 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1860 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1861 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1862 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1863 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1864 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1865 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1866 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1867 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1868 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1869 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1870 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1871 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1872 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1873 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1877 // This removes any dangling pointers to this window in other windows'
1878 // constraintsInvolvedIn lists.
1879 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1883 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1884 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1885 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1886 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1887 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1888 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1889 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1890 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1891 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1892 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1893 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1894 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1895 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1896 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1897 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1898 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1902 // Back-pointer to other windows we're involved with, so if we delete this
1903 // window, we must delete any constraints we're involved with.
1904 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1906 if ( !m_constraintsInvolvedIn
)
1907 m_constraintsInvolvedIn
= new wxWindowList
;
1908 if ( !m_constraintsInvolvedIn
->Find((wxWindow
*)otherWin
) )
1909 m_constraintsInvolvedIn
->Append((wxWindow
*)otherWin
);
1912 // REMOVE back-pointer to other windows we're involved with.
1913 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1915 if ( m_constraintsInvolvedIn
)
1916 m_constraintsInvolvedIn
->DeleteObject((wxWindow
*)otherWin
);
1919 // Reset any constraints that mention this window
1920 void wxWindowBase::DeleteRelatedConstraints()
1922 if ( m_constraintsInvolvedIn
)
1924 wxWindowList::compatibility_iterator node
= m_constraintsInvolvedIn
->GetFirst();
1927 wxWindow
*win
= node
->GetData();
1928 wxLayoutConstraints
*constr
= win
->GetConstraints();
1930 // Reset any constraints involving this window
1933 constr
->left
.ResetIfWin(this);
1934 constr
->top
.ResetIfWin(this);
1935 constr
->right
.ResetIfWin(this);
1936 constr
->bottom
.ResetIfWin(this);
1937 constr
->width
.ResetIfWin(this);
1938 constr
->height
.ResetIfWin(this);
1939 constr
->centreX
.ResetIfWin(this);
1940 constr
->centreY
.ResetIfWin(this);
1943 wxWindowList::compatibility_iterator next
= node
->GetNext();
1944 m_constraintsInvolvedIn
->Erase(node
);
1948 delete m_constraintsInvolvedIn
;
1949 m_constraintsInvolvedIn
= NULL
;
1953 #endif // wxUSE_CONSTRAINTS
1955 void wxWindowBase::SetSizer(wxSizer
*sizer
, bool deleteOld
)
1957 if ( sizer
== m_windowSizer
)
1960 if ( m_windowSizer
)
1962 m_windowSizer
->SetContainingWindow(NULL
);
1965 delete m_windowSizer
;
1968 m_windowSizer
= sizer
;
1969 if ( m_windowSizer
)
1971 m_windowSizer
->SetContainingWindow((wxWindow
*)this);
1974 SetAutoLayout(m_windowSizer
!= NULL
);
1977 void wxWindowBase::SetSizerAndFit(wxSizer
*sizer
, bool deleteOld
)
1979 SetSizer( sizer
, deleteOld
);
1980 sizer
->SetSizeHints( (wxWindow
*) this );
1984 void wxWindowBase::SetContainingSizer(wxSizer
* sizer
)
1986 // adding a window to a sizer twice is going to result in fatal and
1987 // hard to debug problems later because when deleting the second
1988 // associated wxSizerItem we're going to dereference a dangling
1989 // pointer; so try to detect this as early as possible
1990 wxASSERT_MSG( !sizer
|| m_containingSizer
!= sizer
,
1991 _T("Adding a window to the same sizer twice?") );
1993 m_containingSizer
= sizer
;
1996 #if wxUSE_CONSTRAINTS
1998 void wxWindowBase::SatisfyConstraints()
2000 wxLayoutConstraints
*constr
= GetConstraints();
2001 bool wasOk
= constr
&& constr
->AreSatisfied();
2003 ResetConstraints(); // Mark all constraints as unevaluated
2007 // if we're a top level panel (i.e. our parent is frame/dialog), our
2008 // own constraints will never be satisfied any more unless we do it
2012 while ( noChanges
> 0 )
2014 LayoutPhase1(&noChanges
);
2018 LayoutPhase2(&noChanges
);
2021 #endif // wxUSE_CONSTRAINTS
2023 bool wxWindowBase::Layout()
2025 // If there is a sizer, use it instead of the constraints
2029 GetVirtualSize(&w
, &h
);
2030 GetSizer()->SetDimension( 0, 0, w
, h
);
2032 #if wxUSE_CONSTRAINTS
2035 SatisfyConstraints(); // Find the right constraints values
2036 SetConstraintSizes(); // Recursively set the real window sizes
2043 #if wxUSE_CONSTRAINTS
2045 // first phase of the constraints evaluation: set our own constraints
2046 bool wxWindowBase::LayoutPhase1(int *noChanges
)
2048 wxLayoutConstraints
*constr
= GetConstraints();
2050 return !constr
|| constr
->SatisfyConstraints(this, noChanges
);
2053 // second phase: set the constraints for our children
2054 bool wxWindowBase::LayoutPhase2(int *noChanges
)
2061 // Layout grand children
2067 // Do a phase of evaluating child constraints
2068 bool wxWindowBase::DoPhase(int phase
)
2070 // the list containing the children for which the constraints are already
2072 wxWindowList succeeded
;
2074 // the max number of iterations we loop before concluding that we can't set
2076 static const int maxIterations
= 500;
2078 for ( int noIterations
= 0; noIterations
< maxIterations
; noIterations
++ )
2082 // loop over all children setting their constraints
2083 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2085 node
= node
->GetNext() )
2087 wxWindow
*child
= node
->GetData();
2088 if ( child
->IsTopLevel() )
2090 // top level children are not inside our client area
2094 if ( !child
->GetConstraints() || succeeded
.Find(child
) )
2096 // this one is either already ok or nothing we can do about it
2100 int tempNoChanges
= 0;
2101 bool success
= phase
== 1 ? child
->LayoutPhase1(&tempNoChanges
)
2102 : child
->LayoutPhase2(&tempNoChanges
);
2103 noChanges
+= tempNoChanges
;
2107 succeeded
.Append(child
);
2113 // constraints are set
2121 void wxWindowBase::ResetConstraints()
2123 wxLayoutConstraints
*constr
= GetConstraints();
2126 constr
->left
.SetDone(false);
2127 constr
->top
.SetDone(false);
2128 constr
->right
.SetDone(false);
2129 constr
->bottom
.SetDone(false);
2130 constr
->width
.SetDone(false);
2131 constr
->height
.SetDone(false);
2132 constr
->centreX
.SetDone(false);
2133 constr
->centreY
.SetDone(false);
2136 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2139 wxWindow
*win
= node
->GetData();
2140 if ( !win
->IsTopLevel() )
2141 win
->ResetConstraints();
2142 node
= node
->GetNext();
2146 // Need to distinguish between setting the 'fake' size for windows and sizers,
2147 // and setting the real values.
2148 void wxWindowBase::SetConstraintSizes(bool recurse
)
2150 wxLayoutConstraints
*constr
= GetConstraints();
2151 if ( constr
&& constr
->AreSatisfied() )
2153 int x
= constr
->left
.GetValue();
2154 int y
= constr
->top
.GetValue();
2155 int w
= constr
->width
.GetValue();
2156 int h
= constr
->height
.GetValue();
2158 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
2159 (constr
->height
.GetRelationship() != wxAsIs
) )
2161 SetSize(x
, y
, w
, h
);
2165 // If we don't want to resize this window, just move it...
2171 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2172 GetClassInfo()->GetClassName(),
2178 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2181 wxWindow
*win
= node
->GetData();
2182 if ( !win
->IsTopLevel() && win
->GetConstraints() )
2183 win
->SetConstraintSizes();
2184 node
= node
->GetNext();
2189 // Only set the size/position of the constraint (if any)
2190 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
2192 wxLayoutConstraints
*constr
= GetConstraints();
2195 if ( x
!= wxDefaultCoord
)
2197 constr
->left
.SetValue(x
);
2198 constr
->left
.SetDone(true);
2200 if ( y
!= wxDefaultCoord
)
2202 constr
->top
.SetValue(y
);
2203 constr
->top
.SetDone(true);
2205 if ( w
!= wxDefaultCoord
)
2207 constr
->width
.SetValue(w
);
2208 constr
->width
.SetDone(true);
2210 if ( h
!= wxDefaultCoord
)
2212 constr
->height
.SetValue(h
);
2213 constr
->height
.SetDone(true);
2218 void wxWindowBase::MoveConstraint(int x
, int y
)
2220 wxLayoutConstraints
*constr
= GetConstraints();
2223 if ( x
!= wxDefaultCoord
)
2225 constr
->left
.SetValue(x
);
2226 constr
->left
.SetDone(true);
2228 if ( y
!= wxDefaultCoord
)
2230 constr
->top
.SetValue(y
);
2231 constr
->top
.SetDone(true);
2236 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
2238 wxLayoutConstraints
*constr
= GetConstraints();
2241 *w
= constr
->width
.GetValue();
2242 *h
= constr
->height
.GetValue();
2248 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
2250 wxLayoutConstraints
*constr
= GetConstraints();
2253 *w
= constr
->width
.GetValue();
2254 *h
= constr
->height
.GetValue();
2257 GetClientSize(w
, h
);
2260 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
2262 wxLayoutConstraints
*constr
= GetConstraints();
2265 *x
= constr
->left
.GetValue();
2266 *y
= constr
->top
.GetValue();
2272 #endif // wxUSE_CONSTRAINTS
2274 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
) const
2276 // don't do it for the dialogs/frames - they float independently of their
2278 if ( !IsTopLevel() )
2280 wxWindow
*parent
= GetParent();
2281 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
2283 wxPoint
pt(parent
->GetClientAreaOrigin());
2290 // ----------------------------------------------------------------------------
2291 // Update UI processing
2292 // ----------------------------------------------------------------------------
2294 void wxWindowBase::UpdateWindowUI(long flags
)
2296 wxUpdateUIEvent
event(GetId());
2297 event
.SetEventObject(this);
2299 if ( GetEventHandler()->ProcessEvent(event
) )
2301 DoUpdateWindowUI(event
);
2304 if (flags
& wxUPDATE_UI_RECURSE
)
2306 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2309 wxWindow
* child
= (wxWindow
*) node
->GetData();
2310 child
->UpdateWindowUI(flags
);
2311 node
= node
->GetNext();
2316 // do the window-specific processing after processing the update event
2317 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
2319 if ( event
.GetSetEnabled() )
2320 Enable(event
.GetEnabled());
2322 if ( event
.GetSetShown() )
2323 Show(event
.GetShown());
2326 // ----------------------------------------------------------------------------
2327 // dialog units translations
2328 // ----------------------------------------------------------------------------
2330 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
2332 int charWidth
= GetCharWidth();
2333 int charHeight
= GetCharHeight();
2334 wxPoint pt2
= wxDefaultPosition
;
2335 if (pt
.x
!= wxDefaultCoord
)
2336 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
);
2337 if (pt
.y
!= wxDefaultCoord
)
2338 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
);
2343 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
2345 int charWidth
= GetCharWidth();
2346 int charHeight
= GetCharHeight();
2347 wxPoint pt2
= wxDefaultPosition
;
2348 if (pt
.x
!= wxDefaultCoord
)
2349 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4);
2350 if (pt
.y
!= wxDefaultCoord
)
2351 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8);
2356 // ----------------------------------------------------------------------------
2358 // ----------------------------------------------------------------------------
2360 // propagate the colour change event to the subwindows
2361 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
2363 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2366 // Only propagate to non-top-level windows
2367 wxWindow
*win
= node
->GetData();
2368 if ( !win
->IsTopLevel() )
2370 wxSysColourChangedEvent event2
;
2371 event
.SetEventObject(win
);
2372 win
->GetEventHandler()->ProcessEvent(event2
);
2375 node
= node
->GetNext();
2381 // the default action is to populate dialog with data when it's created,
2382 // and nudge the UI into displaying itself correctly in case
2383 // we've turned the wxUpdateUIEvents frequency down low.
2384 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
2386 TransferDataToWindow();
2388 // Update the UI at this point
2389 UpdateWindowUI(wxUPDATE_UI_RECURSE
);
2392 // ----------------------------------------------------------------------------
2393 // menu-related functions
2394 // ----------------------------------------------------------------------------
2398 bool wxWindowBase::PopupMenu(wxMenu
*menu
, int x
, int y
)
2400 wxCHECK_MSG( menu
, false, "can't popup NULL menu" );
2402 wxCurrentPopupMenu
= menu
;
2403 const bool rc
= DoPopupMenu(menu
, x
, y
);
2404 wxCurrentPopupMenu
= NULL
;
2409 // this is used to pass the id of the selected item from the menu event handler
2410 // to the main function itself
2412 // it's ok to use a global here as there can be at most one popup menu shown at
2414 static int gs_popupMenuSelection
= wxID_NONE
;
2416 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent
& event
)
2418 // store the id in a global variable where we'll retrieve it from later
2419 gs_popupMenuSelection
= event
.GetId();
2422 void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent
& WXUNUSED(event
))
2424 // nothing to do but do not skip it
2428 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu
& menu
, int x
, int y
)
2430 gs_popupMenuSelection
= wxID_NONE
;
2432 Connect(wxEVT_COMMAND_MENU_SELECTED
,
2433 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2437 // it is common to construct the menu passed to this function dynamically
2438 // using some fixed range of ids which could clash with the ids used
2439 // elsewhere in the program, which could result in some menu items being
2440 // unintentionally disabled or otherwise modified by update UI handlers
2441 // elsewhere in the program code and this is difficult to avoid in the
2442 // program itself, so instead we just temporarily suspend UI updating while
2443 // this menu is shown
2444 Connect(wxEVT_UPDATE_UI
,
2445 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate
),
2449 PopupMenu(&menu
, x
, y
);
2451 Disconnect(wxEVT_UPDATE_UI
,
2452 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate
),
2455 Disconnect(wxEVT_COMMAND_MENU_SELECTED
,
2456 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2460 return gs_popupMenuSelection
;
2463 #endif // wxUSE_MENUS
2465 // methods for drawing the sizers in a visible way
2468 static void DrawSizers(wxWindowBase
*win
);
2470 static void DrawBorder(wxWindowBase
*win
, const wxRect
& rect
, bool fill
= false)
2472 wxClientDC
dc((wxWindow
*)win
);
2473 dc
.SetPen(*wxRED_PEN
);
2474 dc
.SetBrush(fill
? wxBrush(*wxRED
, wxBRUSHSTYLE_CROSSDIAG_HATCH
) : *wxTRANSPARENT_BRUSH
);
2475 dc
.DrawRectangle(rect
.Deflate(1, 1));
2478 static void DrawSizer(wxWindowBase
*win
, wxSizer
*sizer
)
2480 const wxSizerItemList
& items
= sizer
->GetChildren();
2481 for ( wxSizerItemList::const_iterator i
= items
.begin(),
2486 wxSizerItem
*item
= *i
;
2487 if ( item
->IsSizer() )
2489 DrawBorder(win
, item
->GetRect().Deflate(2));
2490 DrawSizer(win
, item
->GetSizer());
2492 else if ( item
->IsSpacer() )
2494 DrawBorder(win
, item
->GetRect().Deflate(2), true);
2496 else if ( item
->IsWindow() )
2498 DrawSizers(item
->GetWindow());
2503 static void DrawSizers(wxWindowBase
*win
)
2505 wxSizer
*sizer
= win
->GetSizer();
2508 DrawBorder(win
, win
->GetClientSize());
2509 DrawSizer(win
, sizer
);
2511 else // no sizer, still recurse into the children
2513 const wxWindowList
& children
= win
->GetChildren();
2514 for ( wxWindowList::const_iterator i
= children
.begin(),
2515 end
= children
.end();
2524 #endif // __WXDEBUG__
2526 // process special middle clicks
2527 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
2529 if ( event
.ControlDown() && event
.AltDown() )
2532 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2533 if ( event
.ShiftDown() )
2538 #endif // __WXDEBUG__
2539 ::wxInfoMessageBox((wxWindow
*)this);
2547 // ----------------------------------------------------------------------------
2549 // ----------------------------------------------------------------------------
2551 #if wxUSE_ACCESSIBILITY
2552 void wxWindowBase::SetAccessible(wxAccessible
* accessible
)
2554 if (m_accessible
&& (accessible
!= m_accessible
))
2555 delete m_accessible
;
2556 m_accessible
= accessible
;
2558 m_accessible
->SetWindow((wxWindow
*) this);
2561 // Returns the accessible object, creating if necessary.
2562 wxAccessible
* wxWindowBase::GetOrCreateAccessible()
2565 m_accessible
= CreateAccessible();
2566 return m_accessible
;
2569 // Override to create a specific accessible object.
2570 wxAccessible
* wxWindowBase::CreateAccessible()
2572 return new wxWindowAccessible((wxWindow
*) this);
2577 // ----------------------------------------------------------------------------
2578 // list classes implementation
2579 // ----------------------------------------------------------------------------
2583 #include "wx/listimpl.cpp"
2584 WX_DEFINE_LIST(wxWindowList
)
2588 void wxWindowListNode::DeleteData()
2590 delete (wxWindow
*)GetData();
2593 #endif // wxUSE_STL/!wxUSE_STL
2595 // ----------------------------------------------------------------------------
2597 // ----------------------------------------------------------------------------
2599 wxBorder
wxWindowBase::GetBorder(long flags
) const
2601 wxBorder border
= (wxBorder
)(flags
& wxBORDER_MASK
);
2602 if ( border
== wxBORDER_DEFAULT
)
2604 border
= GetDefaultBorder();
2606 else if ( border
== wxBORDER_THEME
)
2608 border
= GetDefaultBorderForControl();
2614 wxBorder
wxWindowBase::GetDefaultBorder() const
2616 return wxBORDER_NONE
;
2619 // ----------------------------------------------------------------------------
2621 // ----------------------------------------------------------------------------
2623 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
2625 // here we just check if the point is inside the window or not
2627 // check the top and left border first
2628 bool outside
= x
< 0 || y
< 0;
2631 // check the right and bottom borders too
2632 wxSize size
= GetSize();
2633 outside
= x
>= size
.x
|| y
>= size
.y
;
2636 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
2639 // ----------------------------------------------------------------------------
2641 // ----------------------------------------------------------------------------
2643 struct WXDLLEXPORT wxWindowNext
2647 } *wxWindowBase::ms_winCaptureNext
= NULL
;
2648 wxWindow
*wxWindowBase::ms_winCaptureCurrent
= NULL
;
2649 bool wxWindowBase::ms_winCaptureChanging
= false;
2651 void wxWindowBase::CaptureMouse()
2653 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), static_cast<void*>(this));
2655 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive CaptureMouse call?") );
2657 ms_winCaptureChanging
= true;
2659 wxWindow
*winOld
= GetCapture();
2662 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
2665 wxWindowNext
*item
= new wxWindowNext
;
2667 item
->next
= ms_winCaptureNext
;
2668 ms_winCaptureNext
= item
;
2670 //else: no mouse capture to save
2673 ms_winCaptureCurrent
= (wxWindow
*)this;
2675 ms_winCaptureChanging
= false;
2678 void wxWindowBase::ReleaseMouse()
2680 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), static_cast<void*>(this));
2682 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive ReleaseMouse call?") );
2684 wxASSERT_MSG( GetCapture() == this,
2685 "attempt to release mouse, but this window hasn't captured it" );
2686 wxASSERT_MSG( ms_winCaptureCurrent
== this,
2687 "attempt to release mouse, but this window hasn't captured it" );
2689 ms_winCaptureChanging
= true;
2692 ms_winCaptureCurrent
= NULL
;
2694 if ( ms_winCaptureNext
)
2696 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
2697 ms_winCaptureCurrent
= ms_winCaptureNext
->win
;
2699 wxWindowNext
*item
= ms_winCaptureNext
;
2700 ms_winCaptureNext
= item
->next
;
2703 //else: stack is empty, no previous capture
2705 ms_winCaptureChanging
= false;
2707 wxLogTrace(_T("mousecapture"),
2708 (const wxChar
*) _T("After ReleaseMouse() mouse is captured by %p"),
2709 static_cast<void*>(GetCapture()));
2712 static void DoNotifyWindowAboutCaptureLost(wxWindow
*win
)
2714 wxMouseCaptureLostEvent
event(win
->GetId());
2715 event
.SetEventObject(win
);
2716 if ( !win
->GetEventHandler()->ProcessEvent(event
) )
2718 // windows must handle this event, otherwise the app wouldn't behave
2719 // correctly if it loses capture unexpectedly; see the discussion here:
2720 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2721 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
2722 wxFAIL_MSG( _T("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2727 void wxWindowBase::NotifyCaptureLost()
2729 // don't do anything if capture lost was expected, i.e. resulted from
2730 // a wx call to ReleaseMouse or CaptureMouse:
2731 if ( ms_winCaptureChanging
)
2734 // if the capture was lost unexpectedly, notify every window that has
2735 // capture (on stack or current) about it and clear the stack:
2737 if ( ms_winCaptureCurrent
)
2739 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent
);
2740 ms_winCaptureCurrent
= NULL
;
2743 while ( ms_winCaptureNext
)
2745 wxWindowNext
*item
= ms_winCaptureNext
;
2746 ms_winCaptureNext
= item
->next
;
2748 DoNotifyWindowAboutCaptureLost(item
->win
);
2757 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId
),
2758 int WXUNUSED(modifiers
),
2759 int WXUNUSED(keycode
))
2765 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId
))
2771 #endif // wxUSE_HOTKEY
2773 // ----------------------------------------------------------------------------
2775 // ----------------------------------------------------------------------------
2777 bool wxWindowBase::TryValidator(wxEvent
& wxVALIDATOR_PARAM(event
))
2779 #if wxUSE_VALIDATORS
2780 // Can only use the validator of the window which
2781 // is receiving the event
2782 if ( event
.GetEventObject() == this )
2784 wxValidator
* const validator
= GetValidator();
2785 if ( validator
&& validator
->ProcessEventHere(event
) )
2790 #endif // wxUSE_VALIDATORS
2795 bool wxWindowBase::TryParent(wxEvent
& event
)
2797 // carry on up the parent-child hierarchy if the propagation count hasn't
2799 if ( event
.ShouldPropagate() )
2801 // honour the requests to stop propagation at this window: this is
2802 // used by the dialogs, for example, to prevent processing the events
2803 // from the dialog controls in the parent frame which rarely, if ever,
2805 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
2807 wxWindow
*parent
= GetParent();
2808 if ( parent
&& !parent
->IsBeingDeleted() )
2810 wxPropagateOnce
propagateOnce(event
);
2812 return parent
->GetEventHandler()->ProcessEvent(event
);
2817 return wxEvtHandler::TryParent(event
);
2820 // ----------------------------------------------------------------------------
2821 // window relationships
2822 // ----------------------------------------------------------------------------
2824 wxWindow
*wxWindowBase::DoGetSibling(WindowOrder order
) const
2826 wxCHECK_MSG( GetParent(), NULL
,
2827 _T("GetPrev/NextSibling() don't work for TLWs!") );
2829 wxWindowList
& siblings
= GetParent()->GetChildren();
2830 wxWindowList::compatibility_iterator i
= siblings
.Find((wxWindow
*)this);
2831 wxCHECK_MSG( i
, NULL
, _T("window not a child of its parent?") );
2833 if ( order
== OrderBefore
)
2834 i
= i
->GetPrevious();
2838 return i
? i
->GetData() : NULL
;
2841 // ----------------------------------------------------------------------------
2842 // keyboard navigation
2843 // ----------------------------------------------------------------------------
2845 // Navigates in the specified direction inside this window
2846 bool wxWindowBase::DoNavigateIn(int flags
)
2848 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2849 // native code doesn't process our wxNavigationKeyEvents anyhow
2852 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
2853 wxNavigationKeyEvent eventNav
;
2854 wxWindow
*focused
= FindFocus();
2855 eventNav
.SetCurrentFocus(focused
);
2856 eventNav
.SetEventObject(focused
);
2857 eventNav
.SetFlags(flags
);
2858 return GetEventHandler()->ProcessEvent(eventNav
);
2859 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
2862 bool wxWindowBase::HandleAsNavigationKey(const wxKeyEvent
& event
)
2864 if ( event
.GetKeyCode() != WXK_TAB
)
2867 int flags
= wxNavigationKeyEvent::FromTab
;
2869 if ( event
.ShiftDown() )
2870 flags
|= wxNavigationKeyEvent::IsBackward
;
2872 flags
|= wxNavigationKeyEvent::IsForward
;
2874 if ( event
.ControlDown() )
2875 flags
|= wxNavigationKeyEvent::WinChange
;
2881 void wxWindowBase::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
2883 // check that we're not a top level window
2884 wxCHECK_RET( GetParent(),
2885 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2887 // detect the special case when we have nothing to do anyhow and when the
2888 // code below wouldn't work
2892 // find the target window in the siblings list
2893 wxWindowList
& siblings
= GetParent()->GetChildren();
2894 wxWindowList::compatibility_iterator i
= siblings
.Find(win
);
2895 wxCHECK_RET( i
, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2897 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2898 // can't just move the node around
2899 wxWindow
*self
= (wxWindow
*)this;
2900 siblings
.DeleteObject(self
);
2901 if ( move
== OrderAfter
)
2908 siblings
.Insert(i
, self
);
2910 else // OrderAfter and win was the last sibling
2912 siblings
.Append(self
);
2916 // ----------------------------------------------------------------------------
2918 // ----------------------------------------------------------------------------
2920 /*static*/ wxWindow
* wxWindowBase::FindFocus()
2922 wxWindowBase
*win
= DoFindFocus();
2923 return win
? win
->GetMainWindowOfCompositeControl() : NULL
;
2926 bool wxWindowBase::HasFocus() const
2928 wxWindowBase
*win
= DoFindFocus();
2929 return win
== this ||
2930 win
== wxConstCast(this, wxWindowBase
)->GetMainWindowOfCompositeControl();
2933 // ----------------------------------------------------------------------------
2935 // ----------------------------------------------------------------------------
2937 #if wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
2942 class DragAcceptFilesTarget
: public wxFileDropTarget
2945 DragAcceptFilesTarget(wxWindowBase
*win
) : m_win(win
) {}
2947 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
2948 const wxArrayString
& filenames
)
2950 wxDropFilesEvent
event(wxEVT_DROP_FILES
,
2952 wxCArrayString(filenames
).Release());
2953 event
.SetEventObject(m_win
);
2957 return m_win
->HandleWindowEvent(event
);
2961 wxWindowBase
* const m_win
;
2963 DECLARE_NO_COPY_CLASS(DragAcceptFilesTarget
)
2967 } // anonymous namespace
2969 // Generic version of DragAcceptFiles(). It works by installing a simple
2970 // wxFileDropTarget-to-EVT_DROP_FILES adaptor and therefore cannot be used
2971 // together with explicit SetDropTarget() calls.
2972 void wxWindowBase::DragAcceptFiles(bool accept
)
2976 wxASSERT_MSG( !GetDropTarget(),
2977 "cannot use DragAcceptFiles() and SetDropTarget() together" );
2978 SetDropTarget(new DragAcceptFilesTarget(this));
2982 SetDropTarget(NULL
);
2986 #endif // wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
2988 // ----------------------------------------------------------------------------
2990 // ----------------------------------------------------------------------------
2992 wxWindow
* wxGetTopLevelParent(wxWindow
*win
)
2994 while ( win
&& !win
->IsTopLevel() )
2995 win
= win
->GetParent();
3000 #if wxUSE_ACCESSIBILITY
3001 // ----------------------------------------------------------------------------
3002 // accessible object for windows
3003 // ----------------------------------------------------------------------------
3005 // Can return either a child object, or an integer
3006 // representing the child element, starting from 1.
3007 wxAccStatus
wxWindowAccessible::HitTest(const wxPoint
& WXUNUSED(pt
), int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(childObject
))
3009 wxASSERT( GetWindow() != NULL
);
3013 return wxACC_NOT_IMPLEMENTED
;
3016 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
3017 wxAccStatus
wxWindowAccessible::GetLocation(wxRect
& rect
, int elementId
)
3019 wxASSERT( GetWindow() != NULL
);
3023 wxWindow
* win
= NULL
;
3030 if (elementId
<= (int) GetWindow()->GetChildren().GetCount())
3032 win
= GetWindow()->GetChildren().Item(elementId
-1)->GetData();
3039 rect
= win
->GetRect();
3040 if (win
->GetParent() && !win
->IsKindOf(CLASSINFO(wxTopLevelWindow
)))
3041 rect
.SetPosition(win
->GetParent()->ClientToScreen(rect
.GetPosition()));
3045 return wxACC_NOT_IMPLEMENTED
;
3048 // Navigates from fromId to toId/toObject.
3049 wxAccStatus
wxWindowAccessible::Navigate(wxNavDir navDir
, int fromId
,
3050 int* WXUNUSED(toId
), wxAccessible
** toObject
)
3052 wxASSERT( GetWindow() != NULL
);
3058 case wxNAVDIR_FIRSTCHILD
:
3060 if (GetWindow()->GetChildren().GetCount() == 0)
3062 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetFirst()->GetData();
3063 *toObject
= childWindow
->GetOrCreateAccessible();
3067 case wxNAVDIR_LASTCHILD
:
3069 if (GetWindow()->GetChildren().GetCount() == 0)
3071 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetLast()->GetData();
3072 *toObject
= childWindow
->GetOrCreateAccessible();
3076 case wxNAVDIR_RIGHT
:
3080 wxWindowList::compatibility_iterator node
=
3081 wxWindowList::compatibility_iterator();
3084 // Can't navigate to sibling of this window
3085 // if we're a top-level window.
3086 if (!GetWindow()->GetParent())
3087 return wxACC_NOT_IMPLEMENTED
;
3089 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3093 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
3094 node
= GetWindow()->GetChildren().Item(fromId
-1);
3097 if (node
&& node
->GetNext())
3099 wxWindow
* nextWindow
= node
->GetNext()->GetData();
3100 *toObject
= nextWindow
->GetOrCreateAccessible();
3108 case wxNAVDIR_PREVIOUS
:
3110 wxWindowList::compatibility_iterator node
=
3111 wxWindowList::compatibility_iterator();
3114 // Can't navigate to sibling of this window
3115 // if we're a top-level window.
3116 if (!GetWindow()->GetParent())
3117 return wxACC_NOT_IMPLEMENTED
;
3119 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3123 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
3124 node
= GetWindow()->GetChildren().Item(fromId
-1);
3127 if (node
&& node
->GetPrevious())
3129 wxWindow
* previousWindow
= node
->GetPrevious()->GetData();
3130 *toObject
= previousWindow
->GetOrCreateAccessible();
3138 return wxACC_NOT_IMPLEMENTED
;
3141 // Gets the name of the specified object.
3142 wxAccStatus
wxWindowAccessible::GetName(int childId
, wxString
* name
)
3144 wxASSERT( GetWindow() != NULL
);
3150 // If a child, leave wxWidgets to call the function on the actual
3153 return wxACC_NOT_IMPLEMENTED
;
3155 // This will eventually be replaced by specialised
3156 // accessible classes, one for each kind of wxWidgets
3157 // control or window.
3159 if (GetWindow()->IsKindOf(CLASSINFO(wxButton
)))
3160 title
= ((wxButton
*) GetWindow())->GetLabel();
3163 title
= GetWindow()->GetName();
3171 return wxACC_NOT_IMPLEMENTED
;
3174 // Gets the number of children.
3175 wxAccStatus
wxWindowAccessible::GetChildCount(int* childId
)
3177 wxASSERT( GetWindow() != NULL
);
3181 *childId
= (int) GetWindow()->GetChildren().GetCount();
3185 // Gets the specified child (starting from 1).
3186 // If *child is NULL and return value is wxACC_OK,
3187 // this means that the child is a simple element and
3188 // not an accessible object.
3189 wxAccStatus
wxWindowAccessible::GetChild(int childId
, wxAccessible
** child
)
3191 wxASSERT( GetWindow() != NULL
);
3201 if (childId
> (int) GetWindow()->GetChildren().GetCount())
3204 wxWindow
* childWindow
= GetWindow()->GetChildren().Item(childId
-1)->GetData();
3205 *child
= childWindow
->GetOrCreateAccessible();
3212 // Gets the parent, or NULL.
3213 wxAccStatus
wxWindowAccessible::GetParent(wxAccessible
** parent
)
3215 wxASSERT( GetWindow() != NULL
);
3219 wxWindow
* parentWindow
= GetWindow()->GetParent();
3227 *parent
= parentWindow
->GetOrCreateAccessible();
3235 // Performs the default action. childId is 0 (the action for this object)
3236 // or > 0 (the action for a child).
3237 // Return wxACC_NOT_SUPPORTED if there is no default action for this
3238 // window (e.g. an edit control).
3239 wxAccStatus
wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId
))
3241 wxASSERT( GetWindow() != NULL
);
3245 return wxACC_NOT_IMPLEMENTED
;
3248 // Gets the default action for this object (0) or > 0 (the action for a child).
3249 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
3250 // string if there is no action.
3251 // The retrieved string describes the action that is performed on an object,
3252 // not what the object does as a result. For example, a toolbar button that prints
3253 // a document has a default action of "Press" rather than "Prints the current document."
3254 wxAccStatus
wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId
), wxString
* WXUNUSED(actionName
))
3256 wxASSERT( GetWindow() != NULL
);
3260 return wxACC_NOT_IMPLEMENTED
;
3263 // Returns the description for this object or a child.
3264 wxAccStatus
wxWindowAccessible::GetDescription(int WXUNUSED(childId
), wxString
* description
)
3266 wxASSERT( GetWindow() != NULL
);
3270 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3276 return wxACC_NOT_IMPLEMENTED
;
3279 // Returns help text for this object or a child, similar to tooltip text.
3280 wxAccStatus
wxWindowAccessible::GetHelpText(int WXUNUSED(childId
), wxString
* helpText
)
3282 wxASSERT( GetWindow() != NULL
);
3286 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3292 return wxACC_NOT_IMPLEMENTED
;
3295 // Returns the keyboard shortcut for this object or child.
3296 // Return e.g. ALT+K
3297 wxAccStatus
wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId
), wxString
* WXUNUSED(shortcut
))
3299 wxASSERT( GetWindow() != NULL
);
3303 return wxACC_NOT_IMPLEMENTED
;
3306 // Returns a role constant.
3307 wxAccStatus
wxWindowAccessible::GetRole(int childId
, wxAccRole
* role
)
3309 wxASSERT( GetWindow() != NULL
);
3313 // If a child, leave wxWidgets to call the function on the actual
3316 return wxACC_NOT_IMPLEMENTED
;
3318 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3319 return wxACC_NOT_IMPLEMENTED
;
3321 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3322 return wxACC_NOT_IMPLEMENTED
;
3325 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3326 return wxACC_NOT_IMPLEMENTED
;
3329 //*role = wxROLE_SYSTEM_CLIENT;
3330 *role
= wxROLE_SYSTEM_CLIENT
;
3334 return wxACC_NOT_IMPLEMENTED
;
3338 // Returns a state constant.
3339 wxAccStatus
wxWindowAccessible::GetState(int childId
, long* state
)
3341 wxASSERT( GetWindow() != NULL
);
3345 // If a child, leave wxWidgets to call the function on the actual
3348 return wxACC_NOT_IMPLEMENTED
;
3350 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3351 return wxACC_NOT_IMPLEMENTED
;
3354 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3355 return wxACC_NOT_IMPLEMENTED
;
3358 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3359 return wxACC_NOT_IMPLEMENTED
;
3366 return wxACC_NOT_IMPLEMENTED
;
3370 // Returns a localized string representing the value for the object
3372 wxAccStatus
wxWindowAccessible::GetValue(int WXUNUSED(childId
), wxString
* WXUNUSED(strValue
))
3374 wxASSERT( GetWindow() != NULL
);
3378 return wxACC_NOT_IMPLEMENTED
;
3381 // Selects the object or child.
3382 wxAccStatus
wxWindowAccessible::Select(int WXUNUSED(childId
), wxAccSelectionFlags
WXUNUSED(selectFlags
))
3384 wxASSERT( GetWindow() != NULL
);
3388 return wxACC_NOT_IMPLEMENTED
;
3391 // Gets the window with the keyboard focus.
3392 // If childId is 0 and child is NULL, no object in
3393 // this subhierarchy has the focus.
3394 // If this object has the focus, child should be 'this'.
3395 wxAccStatus
wxWindowAccessible::GetFocus(int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(child
))
3397 wxASSERT( GetWindow() != NULL
);
3401 return wxACC_NOT_IMPLEMENTED
;
3405 // Gets a variant representing the selected children
3407 // Acceptable values:
3408 // - a null variant (IsNull() returns true)
3409 // - a list variant (GetType() == wxT("list")
3410 // - an integer representing the selected child element,
3411 // or 0 if this object is selected (GetType() == wxT("long")
3412 // - a "void*" pointer to a wxAccessible child object
3413 wxAccStatus
wxWindowAccessible::GetSelections(wxVariant
* WXUNUSED(selections
))
3415 wxASSERT( GetWindow() != NULL
);
3419 return wxACC_NOT_IMPLEMENTED
;
3421 #endif // wxUSE_VARIANT
3423 #endif // wxUSE_ACCESSIBILITY
3425 // ----------------------------------------------------------------------------
3427 // ----------------------------------------------------------------------------
3430 wxWindowBase::AdjustForLayoutDirection(wxCoord x
,
3432 wxCoord widthTotal
) const
3434 if ( GetLayoutDirection() == wxLayout_RightToLeft
)
3436 x
= widthTotal
- x
- width
;