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()
417 bool wxWindowBase::Close(bool force
)
419 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
420 event
.SetEventObject(this);
421 event
.SetCanVeto(!force
);
423 // return false if window wasn't closed because the application vetoed the
425 return HandleWindowEvent(event
) && !event
.GetVeto();
428 bool wxWindowBase::DestroyChildren()
430 wxWindowList::compatibility_iterator node
;
433 // we iterate until the list becomes empty
434 node
= GetChildren().GetFirst();
438 wxWindow
*child
= node
->GetData();
440 // note that we really want to delete it immediately so don't call the
441 // possible overridden Destroy() version which might not delete the
442 // child immediately resulting in problems with our (top level) child
443 // outliving its parent
444 child
->wxWindowBase::Destroy();
446 wxASSERT_MSG( !GetChildren().Find(child
),
447 wxT("child didn't remove itself using RemoveChild()") );
453 // ----------------------------------------------------------------------------
454 // size/position related methods
455 // ----------------------------------------------------------------------------
457 // centre the window with respect to its parent in either (or both) directions
458 void wxWindowBase::DoCentre(int dir
)
460 wxCHECK_RET( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent(),
461 _T("this method only implements centering child windows") );
463 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir
));
466 // fits the window around the children
467 void wxWindowBase::Fit()
469 if ( !GetChildren().empty() )
471 SetSize(GetBestSize());
473 //else: do nothing if we have no children
476 // fits virtual size (ie. scrolled area etc.) around children
477 void wxWindowBase::FitInside()
479 if ( GetChildren().GetCount() > 0 )
481 SetVirtualSize( GetBestVirtualSize() );
485 // On Mac, scrollbars are explicitly children.
487 static bool wxHasRealChildren(const wxWindowBase
* win
)
489 int realChildCount
= 0;
491 for ( wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
493 node
= node
->GetNext() )
495 wxWindow
*win
= node
->GetData();
496 if ( !win
->IsTopLevel() && win
->IsShown() && !win
->IsKindOf(CLASSINFO(wxScrollBar
)))
499 return (realChildCount
> 0);
503 void wxWindowBase::InvalidateBestSize()
505 m_bestSizeCache
= wxDefaultSize
;
507 // parent's best size calculation may depend on its children's
508 // as long as child window we are in is not top level window itself
509 // (because the TLW size is never resized automatically)
510 // so let's invalidate it as well to be safe:
511 if (m_parent
&& !IsTopLevel())
512 m_parent
->InvalidateBestSize();
515 // return the size best suited for the current window
516 wxSize
wxWindowBase::DoGetBestSize() const
522 best
= m_windowSizer
->GetMinSize();
524 #if wxUSE_CONSTRAINTS
525 else if ( m_constraints
)
527 wxConstCast(this, wxWindowBase
)->SatisfyConstraints();
529 // our minimal acceptable size is such that all our windows fit inside
533 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
535 node
= node
->GetNext() )
537 wxLayoutConstraints
*c
= node
->GetData()->GetConstraints();
540 // it's not normal that we have an unconstrained child, but
541 // what can we do about it?
545 int x
= c
->right
.GetValue(),
546 y
= c
->bottom
.GetValue();
554 // TODO: we must calculate the overlaps somehow, otherwise we
555 // will never return a size bigger than the current one :-(
558 best
= wxSize(maxX
, maxY
);
560 #endif // wxUSE_CONSTRAINTS
561 else if ( !GetChildren().empty()
563 && wxHasRealChildren(this)
567 // our minimal acceptable size is such that all our visible child
568 // windows fit inside
572 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
574 node
= node
->GetNext() )
576 wxWindow
*win
= node
->GetData();
577 if ( win
->IsTopLevel()
580 || wxDynamicCast(win
, wxStatusBar
)
581 #endif // wxUSE_STATUSBAR
584 // dialogs and frames lie in different top level windows -
585 // don't deal with them here; as for the status bars, they
586 // don't lie in the client area at all
591 win
->GetPosition(&wx
, &wy
);
593 // if the window hadn't been positioned yet, assume that it is in
595 if ( wx
== wxDefaultCoord
)
597 if ( wy
== wxDefaultCoord
)
600 win
->GetSize(&ww
, &wh
);
601 if ( wx
+ ww
> maxX
)
603 if ( wy
+ wh
> maxY
)
607 best
= wxSize(maxX
, maxY
);
609 else // ! has children
611 // for a generic window there is no natural best size so, if the
612 // minimal size is not set, use the current size but take care to
613 // remember it as minimal size for the next time because our best size
614 // should be constant: otherwise we could get into a situation when the
615 // window is initially at some size, then expanded to a larger size and
616 // then, when the containing window is shrunk back (because our initial
617 // best size had been used for computing the parent min size), we can't
618 // be shrunk back any more because our best size is now bigger
619 wxSize size
= GetMinSize();
620 if ( !size
.IsFullySpecified() )
622 size
.SetDefaults(GetSize());
623 wxConstCast(this, wxWindowBase
)->SetMinSize(size
);
626 // return as-is, unadjusted by the client size difference.
630 // Add any difference between size and client size
631 wxSize diff
= GetSize() - GetClientSize();
632 best
.x
+= wxMax(0, diff
.x
);
633 best
.y
+= wxMax(0, diff
.y
);
638 // helper of GetWindowBorderSize(): as many ports don't implement support for
639 // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
640 // fallbacks in this case
641 static int wxGetMetricOrDefault(wxSystemMetric what
, const wxWindowBase
* win
)
643 int rc
= wxSystemSettings::GetMetric(
644 what
, static_cast<wxWindow
*>(const_cast<wxWindowBase
*>(win
)));
651 // 2D border is by default 1 pixel wide
657 // 3D borders are by default 2 pixels
662 wxFAIL_MSG( _T("unexpected wxGetMetricOrDefault() argument") );
670 wxSize
wxWindowBase::GetWindowBorderSize() const
674 switch ( GetBorder() )
677 // nothing to do, size is already (0, 0)
680 case wxBORDER_SIMPLE
:
681 case wxBORDER_STATIC
:
682 size
.x
= wxGetMetricOrDefault(wxSYS_BORDER_X
, this);
683 size
.y
= wxGetMetricOrDefault(wxSYS_BORDER_Y
, this);
686 case wxBORDER_SUNKEN
:
687 case wxBORDER_RAISED
:
688 size
.x
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X
, this),
689 wxGetMetricOrDefault(wxSYS_BORDER_X
, this));
690 size
.y
= wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y
, this),
691 wxGetMetricOrDefault(wxSYS_BORDER_Y
, this));
694 case wxBORDER_DOUBLE
:
695 size
.x
= wxGetMetricOrDefault(wxSYS_EDGE_X
, this) +
696 wxGetMetricOrDefault(wxSYS_BORDER_X
, this);
697 size
.y
= wxGetMetricOrDefault(wxSYS_EDGE_Y
, this) +
698 wxGetMetricOrDefault(wxSYS_BORDER_Y
, this);
702 wxFAIL_MSG(_T("Unknown border style."));
706 // we have borders on both sides
710 wxSize
wxWindowBase::GetEffectiveMinSize() const
712 // merge the best size with the min size, giving priority to the min size
713 wxSize min
= GetMinSize();
714 if (min
.x
== wxDefaultCoord
|| min
.y
== wxDefaultCoord
)
716 wxSize best
= GetBestSize();
717 if (min
.x
== wxDefaultCoord
) min
.x
= best
.x
;
718 if (min
.y
== wxDefaultCoord
) min
.y
= best
.y
;
724 void wxWindowBase::SetInitialSize(const wxSize
& size
)
726 // Set the min size to the size passed in. This will usually either be
727 // wxDefaultSize or the size passed to this window's ctor/Create function.
730 // Merge the size with the best size if needed
731 wxSize best
= GetEffectiveMinSize();
733 // If the current size doesn't match then change it
734 if (GetSize() != best
)
739 // by default the origin is not shifted
740 wxPoint
wxWindowBase::GetClientAreaOrigin() const
745 wxSize
wxWindowBase::ClientToWindowSize(const wxSize
& size
) const
747 const wxSize
diff(GetSize() - GetClientSize());
749 return wxSize(size
.x
== -1 ? -1 : size
.x
+ diff
.x
,
750 size
.y
== -1 ? -1 : size
.y
+ diff
.y
);
753 wxSize
wxWindowBase::WindowToClientSize(const wxSize
& size
) const
755 const wxSize
diff(GetSize() - GetClientSize());
757 return wxSize(size
.x
== -1 ? -1 : size
.x
- diff
.x
,
758 size
.y
== -1 ? -1 : size
.y
- diff
.y
);
761 void wxWindowBase::SetWindowVariant( wxWindowVariant variant
)
763 if ( m_windowVariant
!= variant
)
765 m_windowVariant
= variant
;
767 DoSetWindowVariant(variant
);
771 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant
)
773 // adjust the font height to correspond to our new variant (notice that
774 // we're only called if something really changed)
775 wxFont font
= GetFont();
776 int size
= font
.GetPointSize();
779 case wxWINDOW_VARIANT_NORMAL
:
782 case wxWINDOW_VARIANT_SMALL
:
787 case wxWINDOW_VARIANT_MINI
:
792 case wxWINDOW_VARIANT_LARGE
:
798 wxFAIL_MSG(_T("unexpected window variant"));
802 font
.SetPointSize(size
);
806 void wxWindowBase::DoSetSizeHints( int minW
, int minH
,
808 int WXUNUSED(incW
), int WXUNUSED(incH
) )
810 wxCHECK_RET( (minW
== wxDefaultCoord
|| maxW
== wxDefaultCoord
|| minW
<= maxW
) &&
811 (minH
== wxDefaultCoord
|| maxH
== wxDefaultCoord
|| minH
<= maxH
),
812 _T("min width/height must be less than max width/height!") );
821 #if WXWIN_COMPATIBILITY_2_8
822 void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW
), int WXUNUSED(minH
),
823 int WXUNUSED(maxW
), int WXUNUSED(maxH
))
827 void wxWindowBase::SetVirtualSizeHints(const wxSize
& WXUNUSED(minsize
),
828 const wxSize
& WXUNUSED(maxsize
))
831 #endif // WXWIN_COMPATIBILITY_2_8
833 void wxWindowBase::DoSetVirtualSize( int x
, int y
)
835 m_virtualSize
= wxSize(x
, y
);
838 wxSize
wxWindowBase::DoGetVirtualSize() const
840 // we should use the entire client area so if it is greater than our
841 // virtual size, expand it to fit (otherwise if the window is big enough we
842 // wouldn't be using parts of it)
843 wxSize size
= GetClientSize();
844 if ( m_virtualSize
.x
> size
.x
)
845 size
.x
= m_virtualSize
.x
;
847 if ( m_virtualSize
.y
>= size
.y
)
848 size
.y
= m_virtualSize
.y
;
853 void wxWindowBase::DoGetScreenPosition(int *x
, int *y
) const
855 // screen position is the same as (0, 0) in client coords for non TLWs (and
856 // TLWs override this method)
862 ClientToScreen(x
, y
);
865 void wxWindowBase::SendSizeEvent(int flags
)
867 wxSizeEvent
event(GetSize(), GetId());
868 event
.SetEventObject(this);
869 if ( flags
& wxSEND_EVENT_POST
)
870 wxPostEvent(this, event
);
872 HandleWindowEvent(event
);
875 void wxWindowBase::SendSizeEventToParent(int flags
)
877 wxWindow
* const parent
= GetParent();
878 if ( parent
&& !parent
->IsBeingDeleted() )
879 parent
->SendSizeEvent(flags
);
882 // ----------------------------------------------------------------------------
883 // show/hide/enable/disable the window
884 // ----------------------------------------------------------------------------
886 bool wxWindowBase::Show(bool show
)
888 if ( show
!= m_isShown
)
900 bool wxWindowBase::IsEnabled() const
902 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
905 void wxWindowBase::NotifyWindowOnEnableChange(bool enabled
)
907 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
909 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
913 // If we are top-level then the logic doesn't apply - otherwise
914 // showing a modal dialog would result in total greying out (and ungreying
915 // out later) of everything which would be really ugly
919 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
921 node
= node
->GetNext() )
923 wxWindowBase
* const child
= node
->GetData();
924 if ( !child
->IsTopLevel() && child
->IsThisEnabled() )
925 child
->NotifyWindowOnEnableChange(enabled
);
929 bool wxWindowBase::Enable(bool enable
)
931 if ( enable
== IsThisEnabled() )
934 m_isEnabled
= enable
;
936 #ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
938 #else // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
939 wxWindowBase
* const parent
= GetParent();
940 if( !IsTopLevel() && parent
&& !parent
->IsEnabled() )
944 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
946 NotifyWindowOnEnableChange(enable
);
951 bool wxWindowBase::IsShownOnScreen() const
953 // A window is shown on screen if it itself is shown and so are all its
954 // parents. But if a window is toplevel one, then its always visible on
955 // screen if IsShown() returns true, even if it has a hidden parent.
957 (IsTopLevel() || GetParent() == NULL
|| GetParent()->IsShownOnScreen());
960 // ----------------------------------------------------------------------------
962 // ----------------------------------------------------------------------------
964 bool wxWindowBase::IsTopLevel() const
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
973 void wxWindowBase::Freeze()
975 if ( !m_freezeCount
++ )
977 // physically freeze this window:
980 // and recursively freeze all children:
981 for ( wxWindowList::iterator i
= GetChildren().begin();
982 i
!= GetChildren().end(); ++i
)
984 wxWindow
*child
= *i
;
985 if ( child
->IsTopLevel() )
993 void wxWindowBase::Thaw()
995 wxASSERT_MSG( m_freezeCount
, "Thaw() without matching Freeze()" );
997 if ( !--m_freezeCount
)
999 // recursively thaw all children:
1000 for ( wxWindowList::iterator i
= GetChildren().begin();
1001 i
!= GetChildren().end(); ++i
)
1003 wxWindow
*child
= *i
;
1004 if ( child
->IsTopLevel() )
1010 // physically thaw this window:
1015 // ----------------------------------------------------------------------------
1016 // reparenting the window
1017 // ----------------------------------------------------------------------------
1019 void wxWindowBase::AddChild(wxWindowBase
*child
)
1021 wxCHECK_RET( child
, wxT("can't add a NULL child") );
1023 // this should never happen and it will lead to a crash later if it does
1024 // because RemoveChild() will remove only one node from the children list
1025 // and the other(s) one(s) will be left with dangling pointers in them
1026 wxASSERT_MSG( !GetChildren().Find((wxWindow
*)child
), _T("AddChild() called twice") );
1028 GetChildren().Append((wxWindow
*)child
);
1029 child
->SetParent(this);
1031 // adding a child while frozen will assert when thawed, so freeze it as if
1032 // it had been already present when we were frozen
1033 if ( IsFrozen() && !child
->IsTopLevel() )
1037 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
1039 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
1041 // removing a child while frozen may result in permanently frozen window
1042 // if used e.g. from Reparent(), so thaw it
1044 // NB: IsTopLevel() doesn't return true any more when a TLW child is being
1045 // removed from its ~wxWindowBase, so check for IsBeingDeleted() too
1046 if ( IsFrozen() && !child
->IsBeingDeleted() && !child
->IsTopLevel() )
1049 GetChildren().DeleteObject((wxWindow
*)child
);
1050 child
->SetParent(NULL
);
1053 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
1055 wxWindow
*oldParent
= GetParent();
1056 if ( newParent
== oldParent
)
1062 const bool oldEnabledState
= IsEnabled();
1064 // unlink this window from the existing parent.
1067 oldParent
->RemoveChild(this);
1071 wxTopLevelWindows
.DeleteObject((wxWindow
*)this);
1074 // add it to the new one
1077 newParent
->AddChild(this);
1081 wxTopLevelWindows
.Append((wxWindow
*)this);
1084 // We need to notify window (and its subwindows) if by changing the parent
1085 // we also change our enabled/disabled status.
1086 const bool newEnabledState
= IsEnabled();
1087 if ( newEnabledState
!= oldEnabledState
)
1089 NotifyWindowOnEnableChange(newEnabledState
);
1095 // ----------------------------------------------------------------------------
1096 // event handler stuff
1097 // ----------------------------------------------------------------------------
1099 void wxWindowBase::SetEventHandler(wxEvtHandler
*handler
)
1101 wxCHECK_RET(handler
!= NULL
, "SetEventHandler(NULL) called");
1103 m_eventHandler
= handler
;
1106 void wxWindowBase::SetNextHandler(wxEvtHandler
*WXUNUSED(handler
))
1108 // disable wxEvtHandler chain mechanism for wxWindows:
1109 // wxWindow uses its own stack mechanism which doesn't mix well with wxEvtHandler's one
1111 wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1113 void wxWindowBase::SetPreviousHandler(wxEvtHandler
*WXUNUSED(handler
))
1115 // we can't simply wxFAIL here as in SetNextHandler: in fact the last
1116 // handler of our stack when is destroyed will be Unlink()ed and thus
1117 // will call this function to update the pointer of this window...
1119 //wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1122 void wxWindowBase::PushEventHandler(wxEvtHandler
*handlerToPush
)
1124 wxCHECK_RET( handlerToPush
!= NULL
, "PushEventHandler(NULL) called" );
1126 // the new handler is going to be part of the wxWindow stack of event handlers:
1127 // it can't be part also of an event handler double-linked chain:
1128 wxASSERT_MSG(handlerToPush
->IsUnlinked(),
1129 "The handler being pushed in the wxWindow stack shouldn't be part of "
1130 "a wxEvtHandler chain; call Unlink() on it first");
1132 wxEvtHandler
*handlerOld
= GetEventHandler();
1133 wxCHECK_RET( handlerOld
, "an old event handler is NULL?" );
1135 // now use wxEvtHandler double-linked list to implement a stack:
1136 handlerToPush
->SetNextHandler(handlerOld
);
1138 if (handlerOld
!= this)
1139 handlerOld
->SetPreviousHandler(handlerToPush
);
1141 SetEventHandler(handlerToPush
);
1144 // final checks of the operations done above:
1145 wxASSERT_MSG( handlerToPush
->GetPreviousHandler() == NULL
,
1146 "the first handler of the wxWindow stack should have no previous handlers set" );
1147 wxASSERT_MSG( handlerToPush
->GetNextHandler() != NULL
,
1148 "the first handler of the wxWindow stack should have non-NULL next handler" );
1150 wxEvtHandler
* pLast
= handlerToPush
;
1151 while (pLast
&& pLast
!= this)
1152 pLast
= pLast
->GetNextHandler();
1153 wxASSERT_MSG( pLast
->GetNextHandler() == NULL
,
1154 "the last handler of the wxWindow stack should have this window as next handler" );
1158 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
1160 // we need to pop the wxWindow stack, i.e. we need to remove the first handler
1162 wxEvtHandler
*firstHandler
= GetEventHandler();
1163 wxCHECK_MSG( firstHandler
!= NULL
, NULL
, "wxWindow cannot have a NULL event handler" );
1164 wxCHECK_MSG( firstHandler
!= this, NULL
, "cannot pop the wxWindow itself" );
1165 wxCHECK_MSG( firstHandler
->GetPreviousHandler() == NULL
, NULL
,
1166 "the first handler of the wxWindow stack should have no previous handlers set" );
1168 wxEvtHandler
*secondHandler
= firstHandler
->GetNextHandler();
1169 wxCHECK_MSG( secondHandler
!= NULL
, NULL
,
1170 "the first handler of the wxWindow stack should have non-NULL next handler" );
1172 firstHandler
->SetNextHandler(NULL
);
1173 secondHandler
->SetPreviousHandler(NULL
);
1175 // now firstHandler is completely unlinked; set secondHandler as the new window event handler
1176 SetEventHandler(secondHandler
);
1178 if ( deleteHandler
)
1180 delete firstHandler
;
1181 firstHandler
= NULL
;
1184 return firstHandler
;
1187 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handlerToRemove
)
1189 wxCHECK_MSG( handlerToRemove
!= NULL
, false, "RemoveEventHandler(NULL) called" );
1190 wxCHECK_MSG( handlerToRemove
!= this, false, "Cannot remove the window itself" );
1192 if (handlerToRemove
== GetEventHandler())
1194 // removing the first event handler is equivalent to "popping" the stack
1195 PopEventHandler(false);
1199 // NOTE: the wxWindow event handler list is always terminated with "this" handler
1200 wxEvtHandler
*handlerCur
= GetEventHandler()->GetNextHandler();
1201 while ( handlerCur
!= this )
1203 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
1205 if ( handlerCur
== handlerToRemove
)
1207 handlerCur
->Unlink();
1209 wxASSERT_MSG( handlerCur
!= GetEventHandler(),
1210 "the case Remove == Pop should was already handled" );
1214 handlerCur
= handlerNext
;
1217 wxFAIL_MSG( _T("where has the event handler gone?") );
1222 bool wxWindowBase::HandleWindowEvent(wxEvent
& event
) const
1224 // SafelyProcessEvent() will handle exceptions nicely
1225 return GetEventHandler()->SafelyProcessEvent(event
);
1228 // ----------------------------------------------------------------------------
1229 // colours, fonts &c
1230 // ----------------------------------------------------------------------------
1232 void wxWindowBase::InheritAttributes()
1234 const wxWindowBase
* const parent
= GetParent();
1238 // we only inherit attributes which had been explicitly set for the parent
1239 // which ensures that this only happens if the user really wants it and
1240 // not by default which wouldn't make any sense in modern GUIs where the
1241 // controls don't all use the same fonts (nor colours)
1242 if ( parent
->m_inheritFont
&& !m_hasFont
)
1243 SetFont(parent
->GetFont());
1245 // in addition, there is a possibility to explicitly forbid inheriting
1246 // colours at each class level by overriding ShouldInheritColours()
1247 if ( ShouldInheritColours() )
1249 if ( parent
->m_inheritFgCol
&& !m_hasFgCol
)
1250 SetForegroundColour(parent
->GetForegroundColour());
1252 // inheriting (solid) background colour is wrong as it totally breaks
1253 // any kind of themed backgrounds
1255 // instead, the controls should use the same background as their parent
1256 // (ideally by not drawing it at all)
1258 if ( parent
->m_inheritBgCol
&& !m_hasBgCol
)
1259 SetBackgroundColour(parent
->GetBackgroundColour());
1264 /* static */ wxVisualAttributes
1265 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1267 // it is important to return valid values for all attributes from here,
1268 // GetXXX() below rely on this
1269 wxVisualAttributes attrs
;
1270 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
1271 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
1273 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1274 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1275 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1276 // colour on other platforms.
1278 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1279 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1281 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1286 wxColour
wxWindowBase::GetBackgroundColour() const
1288 if ( !m_backgroundColour
.IsOk() )
1290 wxASSERT_MSG( !m_hasBgCol
, _T("we have invalid explicit bg colour?") );
1292 // get our default background colour
1293 wxColour colBg
= GetDefaultAttributes().colBg
;
1295 // we must return some valid colour to avoid redoing this every time
1296 // and also to avoid surprizing the applications written for older
1297 // wxWidgets versions where GetBackgroundColour() always returned
1298 // something -- so give them something even if it doesn't make sense
1299 // for this window (e.g. it has a themed background)
1301 colBg
= GetClassDefaultAttributes().colBg
;
1306 return m_backgroundColour
;
1309 wxColour
wxWindowBase::GetForegroundColour() const
1311 // logic is the same as above
1312 if ( !m_hasFgCol
&& !m_foregroundColour
.Ok() )
1314 wxColour colFg
= GetDefaultAttributes().colFg
;
1316 if ( !colFg
.IsOk() )
1317 colFg
= GetClassDefaultAttributes().colFg
;
1322 return m_foregroundColour
;
1325 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
1327 if ( colour
== m_backgroundColour
)
1330 m_hasBgCol
= colour
.IsOk();
1331 if ( m_backgroundStyle
!= wxBG_STYLE_CUSTOM
)
1332 m_backgroundStyle
= m_hasBgCol
? wxBG_STYLE_COLOUR
: wxBG_STYLE_SYSTEM
;
1334 m_inheritBgCol
= m_hasBgCol
;
1335 m_backgroundColour
= colour
;
1336 SetThemeEnabled( !m_hasBgCol
&& !m_foregroundColour
.Ok() );
1340 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
1342 if (colour
== m_foregroundColour
)
1345 m_hasFgCol
= colour
.IsOk();
1346 m_inheritFgCol
= m_hasFgCol
;
1347 m_foregroundColour
= colour
;
1348 SetThemeEnabled( !m_hasFgCol
&& !m_backgroundColour
.Ok() );
1352 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
1354 // setting an invalid cursor is ok, it means that we don't have any special
1356 if ( m_cursor
.IsSameAs(cursor
) )
1367 wxFont
wxWindowBase::GetFont() const
1369 // logic is the same as in GetBackgroundColour()
1370 if ( !m_font
.IsOk() )
1372 wxASSERT_MSG( !m_hasFont
, _T("we have invalid explicit font?") );
1374 wxFont font
= GetDefaultAttributes().font
;
1376 font
= GetClassDefaultAttributes().font
;
1384 bool wxWindowBase::SetFont(const wxFont
& font
)
1386 if ( font
== m_font
)
1393 m_hasFont
= font
.IsOk();
1394 m_inheritFont
= m_hasFont
;
1396 InvalidateBestSize();
1403 void wxWindowBase::SetPalette(const wxPalette
& pal
)
1405 m_hasCustomPalette
= true;
1408 // VZ: can anyone explain me what do we do here?
1409 wxWindowDC
d((wxWindow
*) this);
1413 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
1415 wxWindow
*win
= (wxWindow
*)this;
1416 while ( win
&& !win
->HasCustomPalette() )
1418 win
= win
->GetParent();
1424 #endif // wxUSE_PALETTE
1427 void wxWindowBase::SetCaret(wxCaret
*caret
)
1438 wxASSERT_MSG( m_caret
->GetWindow() == this,
1439 wxT("caret should be created associated to this window") );
1442 #endif // wxUSE_CARET
1444 #if wxUSE_VALIDATORS
1445 // ----------------------------------------------------------------------------
1447 // ----------------------------------------------------------------------------
1449 void wxWindowBase::SetValidator(const wxValidator
& validator
)
1451 if ( m_windowValidator
)
1452 delete m_windowValidator
;
1454 m_windowValidator
= (wxValidator
*)validator
.Clone();
1456 if ( m_windowValidator
)
1457 m_windowValidator
->SetWindow(this);
1459 #endif // wxUSE_VALIDATORS
1461 // ----------------------------------------------------------------------------
1462 // update region stuff
1463 // ----------------------------------------------------------------------------
1465 wxRect
wxWindowBase::GetUpdateClientRect() const
1467 wxRegion rgnUpdate
= GetUpdateRegion();
1468 rgnUpdate
.Intersect(GetClientRect());
1469 wxRect rectUpdate
= rgnUpdate
.GetBox();
1470 wxPoint ptOrigin
= GetClientAreaOrigin();
1471 rectUpdate
.x
-= ptOrigin
.x
;
1472 rectUpdate
.y
-= ptOrigin
.y
;
1477 bool wxWindowBase::DoIsExposed(int x
, int y
) const
1479 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
1482 bool wxWindowBase::DoIsExposed(int x
, int y
, int w
, int h
) const
1484 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
1487 void wxWindowBase::ClearBackground()
1489 // wxGTK uses its own version, no need to add never used code
1491 wxClientDC
dc((wxWindow
*)this);
1492 wxBrush
brush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID
);
1493 dc
.SetBackground(brush
);
1498 // ----------------------------------------------------------------------------
1499 // find child window by id or name
1500 // ----------------------------------------------------------------------------
1502 wxWindow
*wxWindowBase::FindWindow(long id
) const
1504 if ( id
== m_windowId
)
1505 return (wxWindow
*)this;
1507 wxWindowBase
*res
= NULL
;
1508 wxWindowList::compatibility_iterator node
;
1509 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1511 wxWindowBase
*child
= node
->GetData();
1512 res
= child
->FindWindow( id
);
1515 return (wxWindow
*)res
;
1518 wxWindow
*wxWindowBase::FindWindow(const wxString
& name
) const
1520 if ( name
== m_windowName
)
1521 return (wxWindow
*)this;
1523 wxWindowBase
*res
= NULL
;
1524 wxWindowList::compatibility_iterator node
;
1525 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
1527 wxWindow
*child
= node
->GetData();
1528 res
= child
->FindWindow(name
);
1531 return (wxWindow
*)res
;
1535 // find any window by id or name or label: If parent is non-NULL, look through
1536 // children for a label or title matching the specified string. If NULL, look
1537 // through all top-level windows.
1539 // to avoid duplicating code we reuse the same helper function but with
1540 // different comparators
1542 typedef bool (*wxFindWindowCmp
)(const wxWindow
*win
,
1543 const wxString
& label
, long id
);
1546 bool wxFindWindowCmpLabels(const wxWindow
*win
, const wxString
& label
,
1549 return win
->GetLabel() == label
;
1553 bool wxFindWindowCmpNames(const wxWindow
*win
, const wxString
& label
,
1556 return win
->GetName() == label
;
1560 bool wxFindWindowCmpIds(const wxWindow
*win
, const wxString
& WXUNUSED(label
),
1563 return win
->GetId() == id
;
1566 // recursive helper for the FindWindowByXXX() functions
1568 wxWindow
*wxFindWindowRecursively(const wxWindow
*parent
,
1569 const wxString
& label
,
1571 wxFindWindowCmp cmp
)
1575 // see if this is the one we're looking for
1576 if ( (*cmp
)(parent
, label
, id
) )
1577 return (wxWindow
*)parent
;
1579 // It wasn't, so check all its children
1580 for ( wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
1582 node
= node
->GetNext() )
1584 // recursively check each child
1585 wxWindow
*win
= (wxWindow
*)node
->GetData();
1586 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1596 // helper for FindWindowByXXX()
1598 wxWindow
*wxFindWindowHelper(const wxWindow
*parent
,
1599 const wxString
& label
,
1601 wxFindWindowCmp cmp
)
1605 // just check parent and all its children
1606 return wxFindWindowRecursively(parent
, label
, id
, cmp
);
1609 // start at very top of wx's windows
1610 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1612 node
= node
->GetNext() )
1614 // recursively check each window & its children
1615 wxWindow
*win
= node
->GetData();
1616 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1626 wxWindowBase::FindWindowByLabel(const wxString
& title
, const wxWindow
*parent
)
1628 return wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpLabels
);
1633 wxWindowBase::FindWindowByName(const wxString
& title
, const wxWindow
*parent
)
1635 wxWindow
*win
= wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpNames
);
1639 // fall back to the label
1640 win
= FindWindowByLabel(title
, parent
);
1648 wxWindowBase::FindWindowById( long id
, const wxWindow
* parent
)
1650 return wxFindWindowHelper(parent
, wxEmptyString
, id
, wxFindWindowCmpIds
);
1653 // ----------------------------------------------------------------------------
1654 // dialog oriented functions
1655 // ----------------------------------------------------------------------------
1657 void wxWindowBase::MakeModal(bool modal
)
1659 // Disable all other windows
1662 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
1665 wxWindow
*win
= node
->GetData();
1667 win
->Enable(!modal
);
1669 node
= node
->GetNext();
1674 bool wxWindowBase::Validate()
1676 #if wxUSE_VALIDATORS
1677 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1679 wxWindowList::compatibility_iterator node
;
1680 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1682 wxWindowBase
*child
= node
->GetData();
1683 wxValidator
*validator
= child
->GetValidator();
1684 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
1689 if ( recurse
&& !child
->Validate() )
1694 #endif // wxUSE_VALIDATORS
1699 bool wxWindowBase::TransferDataToWindow()
1701 #if wxUSE_VALIDATORS
1702 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1704 wxWindowList::compatibility_iterator node
;
1705 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1707 wxWindowBase
*child
= node
->GetData();
1708 wxValidator
*validator
= child
->GetValidator();
1709 if ( validator
&& !validator
->TransferToWindow() )
1711 wxLogWarning(_("Could not transfer data to window"));
1713 wxLog::FlushActive();
1721 if ( !child
->TransferDataToWindow() )
1723 // warning already given
1728 #endif // wxUSE_VALIDATORS
1733 bool wxWindowBase::TransferDataFromWindow()
1735 #if wxUSE_VALIDATORS
1736 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1738 wxWindowList::compatibility_iterator node
;
1739 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1741 wxWindow
*child
= node
->GetData();
1742 wxValidator
*validator
= child
->GetValidator();
1743 if ( validator
&& !validator
->TransferFromWindow() )
1745 // nop warning here because the application is supposed to give
1746 // one itself - we don't know here what might have gone wrongly
1753 if ( !child
->TransferDataFromWindow() )
1755 // warning already given
1760 #endif // wxUSE_VALIDATORS
1765 void wxWindowBase::InitDialog()
1767 wxInitDialogEvent
event(GetId());
1768 event
.SetEventObject( this );
1769 GetEventHandler()->ProcessEvent(event
);
1772 // ----------------------------------------------------------------------------
1773 // context-sensitive help support
1774 // ----------------------------------------------------------------------------
1778 // associate this help text with this window
1779 void wxWindowBase::SetHelpText(const wxString
& text
)
1781 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1784 helpProvider
->AddHelp(this, text
);
1788 #if WXWIN_COMPATIBILITY_2_8
1789 // associate this help text with all windows with the same id as this
1791 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1793 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1796 helpProvider
->AddHelp(GetId(), text
);
1799 #endif // WXWIN_COMPATIBILITY_2_8
1801 // get the help string associated with this window (may be empty)
1802 // default implementation forwards calls to the help provider
1804 wxWindowBase::GetHelpTextAtPoint(const wxPoint
& WXUNUSED(pt
),
1805 wxHelpEvent::Origin
WXUNUSED(origin
)) const
1808 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1811 text
= helpProvider
->GetHelp(this);
1817 // show help for this window
1818 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1820 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1823 wxPoint pos
= event
.GetPosition();
1824 const wxHelpEvent::Origin origin
= event
.GetOrigin();
1825 if ( origin
== wxHelpEvent::Origin_Keyboard
)
1827 // if the help event was generated from keyboard it shouldn't
1828 // appear at the mouse position (which is still the only position
1829 // associated with help event) if the mouse is far away, although
1830 // we still do use the mouse position if it's over the window
1831 // because we suppose the user looks approximately at the mouse
1832 // already and so it would be more convenient than showing tooltip
1833 // at some arbitrary position which can be quite far from it
1834 const wxRect rectClient
= GetClientRect();
1835 if ( !rectClient
.Contains(ScreenToClient(pos
)) )
1837 // position help slightly under and to the right of this window
1838 pos
= ClientToScreen(wxPoint(
1840 rectClient
.height
+ GetCharHeight()
1845 if ( helpProvider
->ShowHelpAtPoint(this, pos
, origin
) )
1847 // skip the event.Skip() below
1855 #endif // wxUSE_HELP
1857 // ----------------------------------------------------------------------------
1859 // ----------------------------------------------------------------------------
1863 void wxWindowBase::SetToolTip( const wxString
&tip
)
1865 // don't create the new tooltip if we already have one
1868 m_tooltip
->SetTip( tip
);
1872 SetToolTip( new wxToolTip( tip
) );
1875 // setting empty tooltip text does not remove the tooltip any more - use
1876 // SetToolTip(NULL) for this
1879 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1881 if ( m_tooltip
!= tooltip
)
1886 m_tooltip
= tooltip
;
1890 #endif // wxUSE_TOOLTIPS
1892 // ----------------------------------------------------------------------------
1893 // constraints and sizers
1894 // ----------------------------------------------------------------------------
1896 #if wxUSE_CONSTRAINTS
1898 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1900 if ( m_constraints
)
1902 UnsetConstraints(m_constraints
);
1903 delete m_constraints
;
1905 m_constraints
= constraints
;
1906 if ( m_constraints
)
1908 // Make sure other windows know they're part of a 'meaningful relationship'
1909 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1910 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1911 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1912 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1913 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1914 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1915 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1916 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1917 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1918 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1919 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1920 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1921 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1922 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1923 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1924 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1928 // This removes any dangling pointers to this window in other windows'
1929 // constraintsInvolvedIn lists.
1930 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1934 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1935 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1936 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1937 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1938 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1939 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1940 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1941 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1942 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1943 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1944 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1945 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1946 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1947 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1948 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1949 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1953 // Back-pointer to other windows we're involved with, so if we delete this
1954 // window, we must delete any constraints we're involved with.
1955 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1957 if ( !m_constraintsInvolvedIn
)
1958 m_constraintsInvolvedIn
= new wxWindowList
;
1959 if ( !m_constraintsInvolvedIn
->Find((wxWindow
*)otherWin
) )
1960 m_constraintsInvolvedIn
->Append((wxWindow
*)otherWin
);
1963 // REMOVE back-pointer to other windows we're involved with.
1964 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1966 if ( m_constraintsInvolvedIn
)
1967 m_constraintsInvolvedIn
->DeleteObject((wxWindow
*)otherWin
);
1970 // Reset any constraints that mention this window
1971 void wxWindowBase::DeleteRelatedConstraints()
1973 if ( m_constraintsInvolvedIn
)
1975 wxWindowList::compatibility_iterator node
= m_constraintsInvolvedIn
->GetFirst();
1978 wxWindow
*win
= node
->GetData();
1979 wxLayoutConstraints
*constr
= win
->GetConstraints();
1981 // Reset any constraints involving this window
1984 constr
->left
.ResetIfWin(this);
1985 constr
->top
.ResetIfWin(this);
1986 constr
->right
.ResetIfWin(this);
1987 constr
->bottom
.ResetIfWin(this);
1988 constr
->width
.ResetIfWin(this);
1989 constr
->height
.ResetIfWin(this);
1990 constr
->centreX
.ResetIfWin(this);
1991 constr
->centreY
.ResetIfWin(this);
1994 wxWindowList::compatibility_iterator next
= node
->GetNext();
1995 m_constraintsInvolvedIn
->Erase(node
);
1999 delete m_constraintsInvolvedIn
;
2000 m_constraintsInvolvedIn
= NULL
;
2004 #endif // wxUSE_CONSTRAINTS
2006 void wxWindowBase::SetSizer(wxSizer
*sizer
, bool deleteOld
)
2008 if ( sizer
== m_windowSizer
)
2011 if ( m_windowSizer
)
2013 m_windowSizer
->SetContainingWindow(NULL
);
2016 delete m_windowSizer
;
2019 m_windowSizer
= sizer
;
2020 if ( m_windowSizer
)
2022 m_windowSizer
->SetContainingWindow((wxWindow
*)this);
2025 SetAutoLayout(m_windowSizer
!= NULL
);
2028 void wxWindowBase::SetSizerAndFit(wxSizer
*sizer
, bool deleteOld
)
2030 SetSizer( sizer
, deleteOld
);
2031 sizer
->SetSizeHints( (wxWindow
*) this );
2035 void wxWindowBase::SetContainingSizer(wxSizer
* sizer
)
2037 // adding a window to a sizer twice is going to result in fatal and
2038 // hard to debug problems later because when deleting the second
2039 // associated wxSizerItem we're going to dereference a dangling
2040 // pointer; so try to detect this as early as possible
2041 wxASSERT_MSG( !sizer
|| m_containingSizer
!= sizer
,
2042 _T("Adding a window to the same sizer twice?") );
2044 m_containingSizer
= sizer
;
2047 #if wxUSE_CONSTRAINTS
2049 void wxWindowBase::SatisfyConstraints()
2051 wxLayoutConstraints
*constr
= GetConstraints();
2052 bool wasOk
= constr
&& constr
->AreSatisfied();
2054 ResetConstraints(); // Mark all constraints as unevaluated
2058 // if we're a top level panel (i.e. our parent is frame/dialog), our
2059 // own constraints will never be satisfied any more unless we do it
2063 while ( noChanges
> 0 )
2065 LayoutPhase1(&noChanges
);
2069 LayoutPhase2(&noChanges
);
2072 #endif // wxUSE_CONSTRAINTS
2074 bool wxWindowBase::Layout()
2076 // If there is a sizer, use it instead of the constraints
2080 GetVirtualSize(&w
, &h
);
2081 GetSizer()->SetDimension( 0, 0, w
, h
);
2083 #if wxUSE_CONSTRAINTS
2086 SatisfyConstraints(); // Find the right constraints values
2087 SetConstraintSizes(); // Recursively set the real window sizes
2094 #if wxUSE_CONSTRAINTS
2096 // first phase of the constraints evaluation: set our own constraints
2097 bool wxWindowBase::LayoutPhase1(int *noChanges
)
2099 wxLayoutConstraints
*constr
= GetConstraints();
2101 return !constr
|| constr
->SatisfyConstraints(this, noChanges
);
2104 // second phase: set the constraints for our children
2105 bool wxWindowBase::LayoutPhase2(int *noChanges
)
2112 // Layout grand children
2118 // Do a phase of evaluating child constraints
2119 bool wxWindowBase::DoPhase(int phase
)
2121 // the list containing the children for which the constraints are already
2123 wxWindowList succeeded
;
2125 // the max number of iterations we loop before concluding that we can't set
2127 static const int maxIterations
= 500;
2129 for ( int noIterations
= 0; noIterations
< maxIterations
; noIterations
++ )
2133 // loop over all children setting their constraints
2134 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2136 node
= node
->GetNext() )
2138 wxWindow
*child
= node
->GetData();
2139 if ( child
->IsTopLevel() )
2141 // top level children are not inside our client area
2145 if ( !child
->GetConstraints() || succeeded
.Find(child
) )
2147 // this one is either already ok or nothing we can do about it
2151 int tempNoChanges
= 0;
2152 bool success
= phase
== 1 ? child
->LayoutPhase1(&tempNoChanges
)
2153 : child
->LayoutPhase2(&tempNoChanges
);
2154 noChanges
+= tempNoChanges
;
2158 succeeded
.Append(child
);
2164 // constraints are set
2172 void wxWindowBase::ResetConstraints()
2174 wxLayoutConstraints
*constr
= GetConstraints();
2177 constr
->left
.SetDone(false);
2178 constr
->top
.SetDone(false);
2179 constr
->right
.SetDone(false);
2180 constr
->bottom
.SetDone(false);
2181 constr
->width
.SetDone(false);
2182 constr
->height
.SetDone(false);
2183 constr
->centreX
.SetDone(false);
2184 constr
->centreY
.SetDone(false);
2187 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2190 wxWindow
*win
= node
->GetData();
2191 if ( !win
->IsTopLevel() )
2192 win
->ResetConstraints();
2193 node
= node
->GetNext();
2197 // Need to distinguish between setting the 'fake' size for windows and sizers,
2198 // and setting the real values.
2199 void wxWindowBase::SetConstraintSizes(bool recurse
)
2201 wxLayoutConstraints
*constr
= GetConstraints();
2202 if ( constr
&& constr
->AreSatisfied() )
2204 int x
= constr
->left
.GetValue();
2205 int y
= constr
->top
.GetValue();
2206 int w
= constr
->width
.GetValue();
2207 int h
= constr
->height
.GetValue();
2209 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
2210 (constr
->height
.GetRelationship() != wxAsIs
) )
2212 SetSize(x
, y
, w
, h
);
2216 // If we don't want to resize this window, just move it...
2222 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2223 GetClassInfo()->GetClassName(),
2229 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2232 wxWindow
*win
= node
->GetData();
2233 if ( !win
->IsTopLevel() && win
->GetConstraints() )
2234 win
->SetConstraintSizes();
2235 node
= node
->GetNext();
2240 // Only set the size/position of the constraint (if any)
2241 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
2243 wxLayoutConstraints
*constr
= GetConstraints();
2246 if ( x
!= wxDefaultCoord
)
2248 constr
->left
.SetValue(x
);
2249 constr
->left
.SetDone(true);
2251 if ( y
!= wxDefaultCoord
)
2253 constr
->top
.SetValue(y
);
2254 constr
->top
.SetDone(true);
2256 if ( w
!= wxDefaultCoord
)
2258 constr
->width
.SetValue(w
);
2259 constr
->width
.SetDone(true);
2261 if ( h
!= wxDefaultCoord
)
2263 constr
->height
.SetValue(h
);
2264 constr
->height
.SetDone(true);
2269 void wxWindowBase::MoveConstraint(int x
, int y
)
2271 wxLayoutConstraints
*constr
= GetConstraints();
2274 if ( x
!= wxDefaultCoord
)
2276 constr
->left
.SetValue(x
);
2277 constr
->left
.SetDone(true);
2279 if ( y
!= wxDefaultCoord
)
2281 constr
->top
.SetValue(y
);
2282 constr
->top
.SetDone(true);
2287 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
2289 wxLayoutConstraints
*constr
= GetConstraints();
2292 *w
= constr
->width
.GetValue();
2293 *h
= constr
->height
.GetValue();
2299 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
2301 wxLayoutConstraints
*constr
= GetConstraints();
2304 *w
= constr
->width
.GetValue();
2305 *h
= constr
->height
.GetValue();
2308 GetClientSize(w
, h
);
2311 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
2313 wxLayoutConstraints
*constr
= GetConstraints();
2316 *x
= constr
->left
.GetValue();
2317 *y
= constr
->top
.GetValue();
2323 #endif // wxUSE_CONSTRAINTS
2325 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
) const
2327 // don't do it for the dialogs/frames - they float independently of their
2329 if ( !IsTopLevel() )
2331 wxWindow
*parent
= GetParent();
2332 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
2334 wxPoint
pt(parent
->GetClientAreaOrigin());
2341 // ----------------------------------------------------------------------------
2342 // Update UI processing
2343 // ----------------------------------------------------------------------------
2345 void wxWindowBase::UpdateWindowUI(long flags
)
2347 wxUpdateUIEvent
event(GetId());
2348 event
.SetEventObject(this);
2350 if ( GetEventHandler()->ProcessEvent(event
) )
2352 DoUpdateWindowUI(event
);
2355 if (flags
& wxUPDATE_UI_RECURSE
)
2357 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2360 wxWindow
* child
= (wxWindow
*) node
->GetData();
2361 child
->UpdateWindowUI(flags
);
2362 node
= node
->GetNext();
2367 // do the window-specific processing after processing the update event
2368 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
2370 if ( event
.GetSetEnabled() )
2371 Enable(event
.GetEnabled());
2373 if ( event
.GetSetShown() )
2374 Show(event
.GetShown());
2377 // ----------------------------------------------------------------------------
2378 // dialog units translations
2379 // ----------------------------------------------------------------------------
2381 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
2383 int charWidth
= GetCharWidth();
2384 int charHeight
= GetCharHeight();
2385 wxPoint pt2
= wxDefaultPosition
;
2386 if (pt
.x
!= wxDefaultCoord
)
2387 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
);
2388 if (pt
.y
!= wxDefaultCoord
)
2389 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
);
2394 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
2396 int charWidth
= GetCharWidth();
2397 int charHeight
= GetCharHeight();
2398 wxPoint pt2
= wxDefaultPosition
;
2399 if (pt
.x
!= wxDefaultCoord
)
2400 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4);
2401 if (pt
.y
!= wxDefaultCoord
)
2402 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8);
2407 // ----------------------------------------------------------------------------
2409 // ----------------------------------------------------------------------------
2411 // propagate the colour change event to the subwindows
2412 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
2414 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
2417 // Only propagate to non-top-level windows
2418 wxWindow
*win
= node
->GetData();
2419 if ( !win
->IsTopLevel() )
2421 wxSysColourChangedEvent event2
;
2422 event
.SetEventObject(win
);
2423 win
->GetEventHandler()->ProcessEvent(event2
);
2426 node
= node
->GetNext();
2432 // the default action is to populate dialog with data when it's created,
2433 // and nudge the UI into displaying itself correctly in case
2434 // we've turned the wxUpdateUIEvents frequency down low.
2435 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
2437 TransferDataToWindow();
2439 // Update the UI at this point
2440 UpdateWindowUI(wxUPDATE_UI_RECURSE
);
2443 // ----------------------------------------------------------------------------
2444 // menu-related functions
2445 // ----------------------------------------------------------------------------
2449 bool wxWindowBase::PopupMenu(wxMenu
*menu
, int x
, int y
)
2451 wxCHECK_MSG( menu
, false, "can't popup NULL menu" );
2453 wxCurrentPopupMenu
= menu
;
2454 const bool rc
= DoPopupMenu(menu
, x
, y
);
2455 wxCurrentPopupMenu
= NULL
;
2460 // this is used to pass the id of the selected item from the menu event handler
2461 // to the main function itself
2463 // it's ok to use a global here as there can be at most one popup menu shown at
2465 static int gs_popupMenuSelection
= wxID_NONE
;
2467 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent
& event
)
2469 // store the id in a global variable where we'll retrieve it from later
2470 gs_popupMenuSelection
= event
.GetId();
2473 void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent
& WXUNUSED(event
))
2475 // nothing to do but do not skip it
2479 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu
& menu
, int x
, int y
)
2481 gs_popupMenuSelection
= wxID_NONE
;
2483 Connect(wxEVT_COMMAND_MENU_SELECTED
,
2484 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2488 // it is common to construct the menu passed to this function dynamically
2489 // using some fixed range of ids which could clash with the ids used
2490 // elsewhere in the program, which could result in some menu items being
2491 // unintentionally disabled or otherwise modified by update UI handlers
2492 // elsewhere in the program code and this is difficult to avoid in the
2493 // program itself, so instead we just temporarily suspend UI updating while
2494 // this menu is shown
2495 Connect(wxEVT_UPDATE_UI
,
2496 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate
),
2500 PopupMenu(&menu
, x
, y
);
2502 Disconnect(wxEVT_UPDATE_UI
,
2503 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate
),
2506 Disconnect(wxEVT_COMMAND_MENU_SELECTED
,
2507 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu
),
2511 return gs_popupMenuSelection
;
2514 #endif // wxUSE_MENUS
2516 // methods for drawing the sizers in a visible way
2519 static void DrawSizers(wxWindowBase
*win
);
2521 static void DrawBorder(wxWindowBase
*win
, const wxRect
& rect
, bool fill
= false)
2523 wxClientDC
dc((wxWindow
*)win
);
2524 dc
.SetPen(*wxRED_PEN
);
2525 dc
.SetBrush(fill
? wxBrush(*wxRED
, wxBRUSHSTYLE_CROSSDIAG_HATCH
) : *wxTRANSPARENT_BRUSH
);
2526 dc
.DrawRectangle(rect
.Deflate(1, 1));
2529 static void DrawSizer(wxWindowBase
*win
, wxSizer
*sizer
)
2531 const wxSizerItemList
& items
= sizer
->GetChildren();
2532 for ( wxSizerItemList::const_iterator i
= items
.begin(),
2537 wxSizerItem
*item
= *i
;
2538 if ( item
->IsSizer() )
2540 DrawBorder(win
, item
->GetRect().Deflate(2));
2541 DrawSizer(win
, item
->GetSizer());
2543 else if ( item
->IsSpacer() )
2545 DrawBorder(win
, item
->GetRect().Deflate(2), true);
2547 else if ( item
->IsWindow() )
2549 DrawSizers(item
->GetWindow());
2554 static void DrawSizers(wxWindowBase
*win
)
2556 wxSizer
*sizer
= win
->GetSizer();
2559 DrawBorder(win
, win
->GetClientSize());
2560 DrawSizer(win
, sizer
);
2562 else // no sizer, still recurse into the children
2564 const wxWindowList
& children
= win
->GetChildren();
2565 for ( wxWindowList::const_iterator i
= children
.begin(),
2566 end
= children
.end();
2575 #endif // __WXDEBUG__
2577 // process special middle clicks
2578 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
2580 if ( event
.ControlDown() && event
.AltDown() )
2583 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2584 if ( event
.ShiftDown() )
2589 #endif // __WXDEBUG__
2590 ::wxInfoMessageBox((wxWindow
*)this);
2598 // ----------------------------------------------------------------------------
2600 // ----------------------------------------------------------------------------
2602 #if wxUSE_ACCESSIBILITY
2603 void wxWindowBase::SetAccessible(wxAccessible
* accessible
)
2605 if (m_accessible
&& (accessible
!= m_accessible
))
2606 delete m_accessible
;
2607 m_accessible
= accessible
;
2609 m_accessible
->SetWindow((wxWindow
*) this);
2612 // Returns the accessible object, creating if necessary.
2613 wxAccessible
* wxWindowBase::GetOrCreateAccessible()
2616 m_accessible
= CreateAccessible();
2617 return m_accessible
;
2620 // Override to create a specific accessible object.
2621 wxAccessible
* wxWindowBase::CreateAccessible()
2623 return new wxWindowAccessible((wxWindow
*) this);
2628 // ----------------------------------------------------------------------------
2629 // list classes implementation
2630 // ----------------------------------------------------------------------------
2634 #include "wx/listimpl.cpp"
2635 WX_DEFINE_LIST(wxWindowList
)
2639 void wxWindowListNode::DeleteData()
2641 delete (wxWindow
*)GetData();
2644 #endif // wxUSE_STL/!wxUSE_STL
2646 // ----------------------------------------------------------------------------
2648 // ----------------------------------------------------------------------------
2650 wxBorder
wxWindowBase::GetBorder(long flags
) const
2652 wxBorder border
= (wxBorder
)(flags
& wxBORDER_MASK
);
2653 if ( border
== wxBORDER_DEFAULT
)
2655 border
= GetDefaultBorder();
2657 else if ( border
== wxBORDER_THEME
)
2659 border
= GetDefaultBorderForControl();
2665 wxBorder
wxWindowBase::GetDefaultBorder() const
2667 return wxBORDER_NONE
;
2670 // ----------------------------------------------------------------------------
2672 // ----------------------------------------------------------------------------
2674 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
2676 // here we just check if the point is inside the window or not
2678 // check the top and left border first
2679 bool outside
= x
< 0 || y
< 0;
2682 // check the right and bottom borders too
2683 wxSize size
= GetSize();
2684 outside
= x
>= size
.x
|| y
>= size
.y
;
2687 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
2690 // ----------------------------------------------------------------------------
2692 // ----------------------------------------------------------------------------
2694 struct WXDLLEXPORT wxWindowNext
2698 } *wxWindowBase::ms_winCaptureNext
= NULL
;
2699 wxWindow
*wxWindowBase::ms_winCaptureCurrent
= NULL
;
2700 bool wxWindowBase::ms_winCaptureChanging
= false;
2702 void wxWindowBase::CaptureMouse()
2704 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), static_cast<void*>(this));
2706 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive CaptureMouse call?") );
2708 ms_winCaptureChanging
= true;
2710 wxWindow
*winOld
= GetCapture();
2713 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
2716 wxWindowNext
*item
= new wxWindowNext
;
2718 item
->next
= ms_winCaptureNext
;
2719 ms_winCaptureNext
= item
;
2721 //else: no mouse capture to save
2724 ms_winCaptureCurrent
= (wxWindow
*)this;
2726 ms_winCaptureChanging
= false;
2729 void wxWindowBase::ReleaseMouse()
2731 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), static_cast<void*>(this));
2733 wxASSERT_MSG( !ms_winCaptureChanging
, _T("recursive ReleaseMouse call?") );
2735 wxASSERT_MSG( GetCapture() == this,
2736 "attempt to release mouse, but this window hasn't captured it" );
2737 wxASSERT_MSG( ms_winCaptureCurrent
== this,
2738 "attempt to release mouse, but this window hasn't captured it" );
2740 ms_winCaptureChanging
= true;
2743 ms_winCaptureCurrent
= NULL
;
2745 if ( ms_winCaptureNext
)
2747 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
2748 ms_winCaptureCurrent
= ms_winCaptureNext
->win
;
2750 wxWindowNext
*item
= ms_winCaptureNext
;
2751 ms_winCaptureNext
= item
->next
;
2754 //else: stack is empty, no previous capture
2756 ms_winCaptureChanging
= false;
2758 wxLogTrace(_T("mousecapture"),
2759 (const wxChar
*) _T("After ReleaseMouse() mouse is captured by %p"),
2760 static_cast<void*>(GetCapture()));
2763 static void DoNotifyWindowAboutCaptureLost(wxWindow
*win
)
2765 wxMouseCaptureLostEvent
event(win
->GetId());
2766 event
.SetEventObject(win
);
2767 if ( !win
->GetEventHandler()->ProcessEvent(event
) )
2769 // windows must handle this event, otherwise the app wouldn't behave
2770 // correctly if it loses capture unexpectedly; see the discussion here:
2771 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2772 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
2773 wxFAIL_MSG( _T("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2778 void wxWindowBase::NotifyCaptureLost()
2780 // don't do anything if capture lost was expected, i.e. resulted from
2781 // a wx call to ReleaseMouse or CaptureMouse:
2782 if ( ms_winCaptureChanging
)
2785 // if the capture was lost unexpectedly, notify every window that has
2786 // capture (on stack or current) about it and clear the stack:
2788 if ( ms_winCaptureCurrent
)
2790 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent
);
2791 ms_winCaptureCurrent
= NULL
;
2794 while ( ms_winCaptureNext
)
2796 wxWindowNext
*item
= ms_winCaptureNext
;
2797 ms_winCaptureNext
= item
->next
;
2799 DoNotifyWindowAboutCaptureLost(item
->win
);
2808 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId
),
2809 int WXUNUSED(modifiers
),
2810 int WXUNUSED(keycode
))
2816 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId
))
2822 #endif // wxUSE_HOTKEY
2824 // ----------------------------------------------------------------------------
2826 // ----------------------------------------------------------------------------
2828 bool wxWindowBase::TryValidator(wxEvent
& wxVALIDATOR_PARAM(event
))
2830 #if wxUSE_VALIDATORS
2831 // Can only use the validator of the window which
2832 // is receiving the event
2833 if ( event
.GetEventObject() == this )
2835 wxValidator
* const validator
= GetValidator();
2836 if ( validator
&& validator
->ProcessEventHere(event
) )
2841 #endif // wxUSE_VALIDATORS
2846 bool wxWindowBase::TryParent(wxEvent
& event
)
2848 // carry on up the parent-child hierarchy if the propagation count hasn't
2850 if ( event
.ShouldPropagate() )
2852 // honour the requests to stop propagation at this window: this is
2853 // used by the dialogs, for example, to prevent processing the events
2854 // from the dialog controls in the parent frame which rarely, if ever,
2856 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
2858 wxWindow
*parent
= GetParent();
2859 if ( parent
&& !parent
->IsBeingDeleted() )
2861 wxPropagateOnce
propagateOnce(event
);
2863 return parent
->GetEventHandler()->ProcessEvent(event
);
2868 return wxEvtHandler::TryParent(event
);
2871 // ----------------------------------------------------------------------------
2872 // window relationships
2873 // ----------------------------------------------------------------------------
2875 wxWindow
*wxWindowBase::DoGetSibling(WindowOrder order
) const
2877 wxCHECK_MSG( GetParent(), NULL
,
2878 _T("GetPrev/NextSibling() don't work for TLWs!") );
2880 wxWindowList
& siblings
= GetParent()->GetChildren();
2881 wxWindowList::compatibility_iterator i
= siblings
.Find((wxWindow
*)this);
2882 wxCHECK_MSG( i
, NULL
, _T("window not a child of its parent?") );
2884 if ( order
== OrderBefore
)
2885 i
= i
->GetPrevious();
2889 return i
? i
->GetData() : NULL
;
2892 // ----------------------------------------------------------------------------
2893 // keyboard navigation
2894 // ----------------------------------------------------------------------------
2896 // Navigates in the specified direction inside this window
2897 bool wxWindowBase::DoNavigateIn(int flags
)
2899 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2900 // native code doesn't process our wxNavigationKeyEvents anyhow
2903 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
2904 wxNavigationKeyEvent eventNav
;
2905 wxWindow
*focused
= FindFocus();
2906 eventNav
.SetCurrentFocus(focused
);
2907 eventNav
.SetEventObject(focused
);
2908 eventNav
.SetFlags(flags
);
2909 return GetEventHandler()->ProcessEvent(eventNav
);
2910 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
2913 bool wxWindowBase::HandleAsNavigationKey(const wxKeyEvent
& event
)
2915 if ( event
.GetKeyCode() != WXK_TAB
)
2918 int flags
= wxNavigationKeyEvent::FromTab
;
2920 if ( event
.ShiftDown() )
2921 flags
|= wxNavigationKeyEvent::IsBackward
;
2923 flags
|= wxNavigationKeyEvent::IsForward
;
2925 if ( event
.ControlDown() )
2926 flags
|= wxNavigationKeyEvent::WinChange
;
2932 void wxWindowBase::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
2934 // check that we're not a top level window
2935 wxCHECK_RET( GetParent(),
2936 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2938 // detect the special case when we have nothing to do anyhow and when the
2939 // code below wouldn't work
2943 // find the target window in the siblings list
2944 wxWindowList
& siblings
= GetParent()->GetChildren();
2945 wxWindowList::compatibility_iterator i
= siblings
.Find(win
);
2946 wxCHECK_RET( i
, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2948 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2949 // can't just move the node around
2950 wxWindow
*self
= (wxWindow
*)this;
2951 siblings
.DeleteObject(self
);
2952 if ( move
== OrderAfter
)
2959 siblings
.Insert(i
, self
);
2961 else // OrderAfter and win was the last sibling
2963 siblings
.Append(self
);
2967 // ----------------------------------------------------------------------------
2969 // ----------------------------------------------------------------------------
2971 /*static*/ wxWindow
* wxWindowBase::FindFocus()
2973 wxWindowBase
*win
= DoFindFocus();
2974 return win
? win
->GetMainWindowOfCompositeControl() : NULL
;
2977 bool wxWindowBase::HasFocus() const
2979 wxWindowBase
*win
= DoFindFocus();
2980 return win
== this ||
2981 win
== wxConstCast(this, wxWindowBase
)->GetMainWindowOfCompositeControl();
2984 // ----------------------------------------------------------------------------
2986 // ----------------------------------------------------------------------------
2988 #if wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
2993 class DragAcceptFilesTarget
: public wxFileDropTarget
2996 DragAcceptFilesTarget(wxWindowBase
*win
) : m_win(win
) {}
2998 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
2999 const wxArrayString
& filenames
)
3001 wxDropFilesEvent
event(wxEVT_DROP_FILES
,
3003 wxCArrayString(filenames
).Release());
3004 event
.SetEventObject(m_win
);
3008 return m_win
->HandleWindowEvent(event
);
3012 wxWindowBase
* const m_win
;
3014 DECLARE_NO_COPY_CLASS(DragAcceptFilesTarget
)
3018 } // anonymous namespace
3020 // Generic version of DragAcceptFiles(). It works by installing a simple
3021 // wxFileDropTarget-to-EVT_DROP_FILES adaptor and therefore cannot be used
3022 // together with explicit SetDropTarget() calls.
3023 void wxWindowBase::DragAcceptFiles(bool accept
)
3027 wxASSERT_MSG( !GetDropTarget(),
3028 "cannot use DragAcceptFiles() and SetDropTarget() together" );
3029 SetDropTarget(new DragAcceptFilesTarget(this));
3033 SetDropTarget(NULL
);
3037 #endif // wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3039 // ----------------------------------------------------------------------------
3041 // ----------------------------------------------------------------------------
3043 wxWindow
* wxGetTopLevelParent(wxWindow
*win
)
3045 while ( win
&& !win
->IsTopLevel() )
3046 win
= win
->GetParent();
3051 #if wxUSE_ACCESSIBILITY
3052 // ----------------------------------------------------------------------------
3053 // accessible object for windows
3054 // ----------------------------------------------------------------------------
3056 // Can return either a child object, or an integer
3057 // representing the child element, starting from 1.
3058 wxAccStatus
wxWindowAccessible::HitTest(const wxPoint
& WXUNUSED(pt
), int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(childObject
))
3060 wxASSERT( GetWindow() != NULL
);
3064 return wxACC_NOT_IMPLEMENTED
;
3067 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
3068 wxAccStatus
wxWindowAccessible::GetLocation(wxRect
& rect
, int elementId
)
3070 wxASSERT( GetWindow() != NULL
);
3074 wxWindow
* win
= NULL
;
3081 if (elementId
<= (int) GetWindow()->GetChildren().GetCount())
3083 win
= GetWindow()->GetChildren().Item(elementId
-1)->GetData();
3090 rect
= win
->GetRect();
3091 if (win
->GetParent() && !win
->IsKindOf(CLASSINFO(wxTopLevelWindow
)))
3092 rect
.SetPosition(win
->GetParent()->ClientToScreen(rect
.GetPosition()));
3096 return wxACC_NOT_IMPLEMENTED
;
3099 // Navigates from fromId to toId/toObject.
3100 wxAccStatus
wxWindowAccessible::Navigate(wxNavDir navDir
, int fromId
,
3101 int* WXUNUSED(toId
), wxAccessible
** toObject
)
3103 wxASSERT( GetWindow() != NULL
);
3109 case wxNAVDIR_FIRSTCHILD
:
3111 if (GetWindow()->GetChildren().GetCount() == 0)
3113 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetFirst()->GetData();
3114 *toObject
= childWindow
->GetOrCreateAccessible();
3118 case wxNAVDIR_LASTCHILD
:
3120 if (GetWindow()->GetChildren().GetCount() == 0)
3122 wxWindow
* childWindow
= (wxWindow
*) GetWindow()->GetChildren().GetLast()->GetData();
3123 *toObject
= childWindow
->GetOrCreateAccessible();
3127 case wxNAVDIR_RIGHT
:
3131 wxWindowList::compatibility_iterator node
=
3132 wxWindowList::compatibility_iterator();
3135 // Can't navigate to sibling of this window
3136 // if we're a top-level window.
3137 if (!GetWindow()->GetParent())
3138 return wxACC_NOT_IMPLEMENTED
;
3140 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3144 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
3145 node
= GetWindow()->GetChildren().Item(fromId
-1);
3148 if (node
&& node
->GetNext())
3150 wxWindow
* nextWindow
= node
->GetNext()->GetData();
3151 *toObject
= nextWindow
->GetOrCreateAccessible();
3159 case wxNAVDIR_PREVIOUS
:
3161 wxWindowList::compatibility_iterator node
=
3162 wxWindowList::compatibility_iterator();
3165 // Can't navigate to sibling of this window
3166 // if we're a top-level window.
3167 if (!GetWindow()->GetParent())
3168 return wxACC_NOT_IMPLEMENTED
;
3170 node
= GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3174 if (fromId
<= (int) GetWindow()->GetChildren().GetCount())
3175 node
= GetWindow()->GetChildren().Item(fromId
-1);
3178 if (node
&& node
->GetPrevious())
3180 wxWindow
* previousWindow
= node
->GetPrevious()->GetData();
3181 *toObject
= previousWindow
->GetOrCreateAccessible();
3189 return wxACC_NOT_IMPLEMENTED
;
3192 // Gets the name of the specified object.
3193 wxAccStatus
wxWindowAccessible::GetName(int childId
, wxString
* name
)
3195 wxASSERT( GetWindow() != NULL
);
3201 // If a child, leave wxWidgets to call the function on the actual
3204 return wxACC_NOT_IMPLEMENTED
;
3206 // This will eventually be replaced by specialised
3207 // accessible classes, one for each kind of wxWidgets
3208 // control or window.
3210 if (GetWindow()->IsKindOf(CLASSINFO(wxButton
)))
3211 title
= ((wxButton
*) GetWindow())->GetLabel();
3214 title
= GetWindow()->GetName();
3222 return wxACC_NOT_IMPLEMENTED
;
3225 // Gets the number of children.
3226 wxAccStatus
wxWindowAccessible::GetChildCount(int* childId
)
3228 wxASSERT( GetWindow() != NULL
);
3232 *childId
= (int) GetWindow()->GetChildren().GetCount();
3236 // Gets the specified child (starting from 1).
3237 // If *child is NULL and return value is wxACC_OK,
3238 // this means that the child is a simple element and
3239 // not an accessible object.
3240 wxAccStatus
wxWindowAccessible::GetChild(int childId
, wxAccessible
** child
)
3242 wxASSERT( GetWindow() != NULL
);
3252 if (childId
> (int) GetWindow()->GetChildren().GetCount())
3255 wxWindow
* childWindow
= GetWindow()->GetChildren().Item(childId
-1)->GetData();
3256 *child
= childWindow
->GetOrCreateAccessible();
3263 // Gets the parent, or NULL.
3264 wxAccStatus
wxWindowAccessible::GetParent(wxAccessible
** parent
)
3266 wxASSERT( GetWindow() != NULL
);
3270 wxWindow
* parentWindow
= GetWindow()->GetParent();
3278 *parent
= parentWindow
->GetOrCreateAccessible();
3286 // Performs the default action. childId is 0 (the action for this object)
3287 // or > 0 (the action for a child).
3288 // Return wxACC_NOT_SUPPORTED if there is no default action for this
3289 // window (e.g. an edit control).
3290 wxAccStatus
wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId
))
3292 wxASSERT( GetWindow() != NULL
);
3296 return wxACC_NOT_IMPLEMENTED
;
3299 // Gets the default action for this object (0) or > 0 (the action for a child).
3300 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
3301 // string if there is no action.
3302 // The retrieved string describes the action that is performed on an object,
3303 // not what the object does as a result. For example, a toolbar button that prints
3304 // a document has a default action of "Press" rather than "Prints the current document."
3305 wxAccStatus
wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId
), wxString
* WXUNUSED(actionName
))
3307 wxASSERT( GetWindow() != NULL
);
3311 return wxACC_NOT_IMPLEMENTED
;
3314 // Returns the description for this object or a child.
3315 wxAccStatus
wxWindowAccessible::GetDescription(int WXUNUSED(childId
), wxString
* description
)
3317 wxASSERT( GetWindow() != NULL
);
3321 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3327 return wxACC_NOT_IMPLEMENTED
;
3330 // Returns help text for this object or a child, similar to tooltip text.
3331 wxAccStatus
wxWindowAccessible::GetHelpText(int WXUNUSED(childId
), wxString
* helpText
)
3333 wxASSERT( GetWindow() != NULL
);
3337 wxString
ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition
, wxHelpEvent::Origin_Keyboard
));
3343 return wxACC_NOT_IMPLEMENTED
;
3346 // Returns the keyboard shortcut for this object or child.
3347 // Return e.g. ALT+K
3348 wxAccStatus
wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId
), wxString
* WXUNUSED(shortcut
))
3350 wxASSERT( GetWindow() != NULL
);
3354 return wxACC_NOT_IMPLEMENTED
;
3357 // Returns a role constant.
3358 wxAccStatus
wxWindowAccessible::GetRole(int childId
, wxAccRole
* role
)
3360 wxASSERT( GetWindow() != NULL
);
3364 // If a child, leave wxWidgets to call the function on the actual
3367 return wxACC_NOT_IMPLEMENTED
;
3369 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3370 return wxACC_NOT_IMPLEMENTED
;
3372 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3373 return wxACC_NOT_IMPLEMENTED
;
3376 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3377 return wxACC_NOT_IMPLEMENTED
;
3380 //*role = wxROLE_SYSTEM_CLIENT;
3381 *role
= wxROLE_SYSTEM_CLIENT
;
3385 return wxACC_NOT_IMPLEMENTED
;
3389 // Returns a state constant.
3390 wxAccStatus
wxWindowAccessible::GetState(int childId
, long* state
)
3392 wxASSERT( GetWindow() != NULL
);
3396 // If a child, leave wxWidgets to call the function on the actual
3399 return wxACC_NOT_IMPLEMENTED
;
3401 if (GetWindow()->IsKindOf(CLASSINFO(wxControl
)))
3402 return wxACC_NOT_IMPLEMENTED
;
3405 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar
)))
3406 return wxACC_NOT_IMPLEMENTED
;
3409 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar
)))
3410 return wxACC_NOT_IMPLEMENTED
;
3417 return wxACC_NOT_IMPLEMENTED
;
3421 // Returns a localized string representing the value for the object
3423 wxAccStatus
wxWindowAccessible::GetValue(int WXUNUSED(childId
), wxString
* WXUNUSED(strValue
))
3425 wxASSERT( GetWindow() != NULL
);
3429 return wxACC_NOT_IMPLEMENTED
;
3432 // Selects the object or child.
3433 wxAccStatus
wxWindowAccessible::Select(int WXUNUSED(childId
), wxAccSelectionFlags
WXUNUSED(selectFlags
))
3435 wxASSERT( GetWindow() != NULL
);
3439 return wxACC_NOT_IMPLEMENTED
;
3442 // Gets the window with the keyboard focus.
3443 // If childId is 0 and child is NULL, no object in
3444 // this subhierarchy has the focus.
3445 // If this object has the focus, child should be 'this'.
3446 wxAccStatus
wxWindowAccessible::GetFocus(int* WXUNUSED(childId
), wxAccessible
** WXUNUSED(child
))
3448 wxASSERT( GetWindow() != NULL
);
3452 return wxACC_NOT_IMPLEMENTED
;
3456 // Gets a variant representing the selected children
3458 // Acceptable values:
3459 // - a null variant (IsNull() returns true)
3460 // - a list variant (GetType() == wxT("list")
3461 // - an integer representing the selected child element,
3462 // or 0 if this object is selected (GetType() == wxT("long")
3463 // - a "void*" pointer to a wxAccessible child object
3464 wxAccStatus
wxWindowAccessible::GetSelections(wxVariant
* WXUNUSED(selections
))
3466 wxASSERT( GetWindow() != NULL
);
3470 return wxACC_NOT_IMPLEMENTED
;
3472 #endif // wxUSE_VARIANT
3474 #endif // wxUSE_ACCESSIBILITY
3476 // ----------------------------------------------------------------------------
3478 // ----------------------------------------------------------------------------
3481 wxWindowBase::AdjustForLayoutDirection(wxCoord x
,
3483 wxCoord widthTotal
) const
3485 if ( GetLayoutDirection() == wxLayout_RightToLeft
)
3487 x
= widthTotal
- x
- width
;