1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
54 #if wxUSE_DRAG_AND_DROP
56 #endif // wxUSE_DRAG_AND_DROP
59 #include "wx/cshelp.h"
63 #include "wx/tooltip.h"
64 #endif // wxUSE_TOOLTIPS
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 int wxWindowBase::ms_lastControlId
= -200;
76 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
83 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
84 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
85 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
88 EVT_HELP(-1, wxWindowBase::OnHelp
)
93 // ============================================================================
94 // implementation of the common functionality of the wxWindow class
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // the default initialization
102 void wxWindowBase::InitBase()
104 // no window yet, no parent nor children
105 m_parent
= (wxWindow
*)NULL
;
107 m_children
.DeleteContents( FALSE
); // don't auto delete node data
109 // no constraints on the minimal window size
115 // window is created enabled but it's not visible yet
119 // the default event handler is just this window
120 m_eventHandler
= this;
124 m_windowValidator
= (wxValidator
*) NULL
;
125 #endif // wxUSE_VALIDATORS
127 // use the system default colours
128 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
129 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
131 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
132 // so the font is *not* really set - but calls to SetFont() later won't do
133 // anything because m_font appears to be already set!
135 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
138 // the colours/fonts are default for now
147 // an optimization for the event processing: checking this flag is much
148 // faster than using IsKindOf(CLASSINFO(wxWindow))
151 #if wxUSE_CONSTRAINTS
152 // no constraints whatsoever
153 m_constraints
= (wxLayoutConstraints
*) NULL
;
154 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
155 #endif // wxUSE_CONSTRAINTS
157 m_windowSizer
= (wxSizer
*) NULL
;
158 m_containingSizer
= (wxSizer
*) NULL
;
159 m_autoLayout
= FALSE
;
161 #if wxUSE_DRAG_AND_DROP
162 m_dropTarget
= (wxDropTarget
*)NULL
;
163 #endif // wxUSE_DRAG_AND_DROP
166 m_tooltip
= (wxToolTip
*)NULL
;
167 #endif // wxUSE_TOOLTIPS
170 m_caret
= (wxCaret
*)NULL
;
171 #endif // wxUSE_CARET
174 m_hasCustomPalette
= FALSE
;
175 #endif // wxUSE_PALETTE
177 m_virtualSize
= wxDefaultSize
;
178 m_minVirtualWidth
= -1;
179 m_minVirtualHeight
= -1;
180 m_maxVirtualWidth
= -1;
181 m_maxVirtualHeight
= -1;
183 // Whether we're using the current theme for this window (wxGTK only for now)
184 m_themeEnabled
= FALSE
;
187 // common part of window creation process
188 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
190 const wxPoint
& WXUNUSED(pos
),
191 const wxSize
& WXUNUSED(size
),
193 const wxValidator
& validator
,
194 const wxString
& name
)
196 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
197 // member variables - check that it has been called (will catch the case
198 // when a new ctor is added which doesn't call InitWindow)
199 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
201 // generate a new id if the user doesn't care about it
202 m_windowId
= id
== -1 ? NewControlId() : id
;
205 SetWindowStyleFlag(style
);
209 SetValidator(validator
);
210 #endif // wxUSE_VALIDATORS
212 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
213 // have it too - like this it's possible to set it only in the top level
214 // dialog/frame and all children will inherit it by defult
215 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
217 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
228 wxWindowBase::~wxWindowBase()
230 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
232 // FIXME if these 2 cases result from programming errors in the user code
233 // we should probably assert here instead of silently fixing them
235 // Just in case the window has been Closed, but we're then deleting
236 // immediately: don't leave dangling pointers.
237 wxPendingDelete
.DeleteObject(this);
239 // Just in case we've loaded a top-level window via LoadNativeDialog but
240 // we weren't a dialog class
241 wxTopLevelWindows
.DeleteObject(this);
243 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
248 #endif // wxUSE_CARET
251 if ( m_windowValidator
)
252 delete m_windowValidator
;
253 #endif // wxUSE_VALIDATORS
255 #if wxUSE_CONSTRAINTS
256 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
257 // at deleted windows as they delete themselves.
258 DeleteRelatedConstraints();
262 // This removes any dangling pointers to this window in other windows'
263 // constraintsInvolvedIn lists.
264 UnsetConstraints(m_constraints
);
265 delete m_constraints
;
266 m_constraints
= NULL
;
269 #endif // wxUSE_CONSTRAINTS
271 if ( m_containingSizer
)
272 m_containingSizer
->Remove((wxWindow
*)this);
275 delete m_windowSizer
;
277 #if wxUSE_DRAG_AND_DROP
280 #endif // wxUSE_DRAG_AND_DROP
285 #endif // wxUSE_TOOLTIPS
287 // reset the dangling pointer our parent window may keep to us
288 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
290 m_parent
->SetDefaultItem(NULL
);
294 bool wxWindowBase::Destroy()
301 bool wxWindowBase::Close(bool force
)
303 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
304 event
.SetEventObject(this);
305 #if WXWIN_COMPATIBILITY
306 event
.SetForce(force
);
307 #endif // WXWIN_COMPATIBILITY
308 event
.SetCanVeto(!force
);
310 // return FALSE if window wasn't closed because the application vetoed the
312 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
315 bool wxWindowBase::DestroyChildren()
317 wxWindowList::Node
*node
;
320 // we iterate until the list becomes empty
321 node
= GetChildren().GetFirst();
325 wxWindow
*child
= node
->GetData();
327 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
332 wxASSERT_MSG( !GetChildren().Find(child
),
333 wxT("child didn't remove itself using RemoveChild()") );
339 // ----------------------------------------------------------------------------
340 // size/position related methods
341 // ----------------------------------------------------------------------------
343 // centre the window with respect to its parent in either (or both) directions
344 void wxWindowBase::Centre(int direction
)
346 // the position/size of the parent window or of the entire screen
348 int widthParent
, heightParent
;
350 wxWindow
*parent
= NULL
;
352 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
354 // find the parent to centre this window on: it should be the
355 // immediate parent for the controls but the top level parent for the
356 // top level windows (like dialogs)
357 parent
= GetParent();
360 while ( parent
&& !parent
->IsTopLevel() )
362 parent
= parent
->GetParent();
366 // did we find the parent?
370 direction
|= wxCENTRE_ON_SCREEN
;
374 if ( direction
& wxCENTRE_ON_SCREEN
)
376 // centre with respect to the whole screen
377 wxDisplaySize(&widthParent
, &heightParent
);
383 // centre on the parent
384 parent
->GetSize(&widthParent
, &heightParent
);
386 // adjust to the parents position
387 posParent
= parent
->GetPosition();
391 // centre inside the parents client rectangle
392 parent
->GetClientSize(&widthParent
, &heightParent
);
397 GetSize(&width
, &height
);
402 if ( direction
& wxHORIZONTAL
)
403 xNew
= (widthParent
- width
)/2;
405 if ( direction
& wxVERTICAL
)
406 yNew
= (heightParent
- height
)/2;
411 // Base size of the visible dimensions of the display
412 // to take into account the taskbar
413 wxRect rect
= wxGetClientDisplayRect();
414 wxSize
size (rect
.width
,rect
.height
);
416 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
417 // but it may mean that the window is placed on other than the main
418 // display. Therefore we only make sure centered window is on the main display
419 // if the parent is at least partially present here.
420 if (posParent
.x
+ widthParent
>= 0) // if parent is (partially) on the main display
424 else if (xNew
+width
> size
.x
)
425 xNew
= size
.x
-width
-1;
427 if (posParent
.y
+ heightParent
>= 0) // if parent is (partially) on the main display
429 if (yNew
+height
> size
.y
)
430 yNew
= size
.y
-height
-1;
432 // Make certain that the title bar is initially visible
433 // always, even if this would push the bottom of the
434 // dialog of the visible area of the display
439 // move the window to this position (keeping the old size but using
440 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
441 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
444 // fits the window around the children
445 void wxWindowBase::Fit()
447 if ( GetChildren().GetCount() > 0 )
449 wxSize size
= DoGetBestSize();
451 // for compatibility with the old versions and because it really looks
452 // slightly more pretty like this, add a pad
458 //else: do nothing if we have no children
461 // return the size best suited for the current window
462 wxSize
wxWindowBase::DoGetBestSize() const
464 if ( GetChildren().GetCount() > 0 )
466 // our minimal acceptable size is such that all our windows fit inside
470 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
472 node
= node
->GetNext() )
474 wxWindow
*win
= node
->GetData();
475 if ( win
->IsTopLevel()
477 || wxDynamicCast(win
, wxStatusBar
)
478 #endif // wxUSE_STATUSBAR
481 // dialogs and frames lie in different top level windows -
482 // don't deal with them here; as for the status bars, they
483 // don't lie in the client area at all
488 win
->GetPosition(&wx
, &wy
);
490 // if the window hadn't been positioned yet, assume that it is in
497 win
->GetSize(&ww
, &wh
);
498 if ( wx
+ ww
> maxX
)
500 if ( wy
+ wh
> maxY
)
504 return wxSize(maxX
, maxY
);
508 // for a generic window there is no natural best size - just use the
514 // by default the origin is not shifted
515 wxPoint
wxWindowBase::GetClientAreaOrigin() const
517 return wxPoint(0, 0);
520 // set the min/max size of the window
521 void wxWindowBase::SetSizeHints(int minW
, int minH
,
523 int WXUNUSED(incW
), int WXUNUSED(incH
))
531 void wxWindowBase::SetVirtualSizeHints( int minW
, int minH
,
534 m_minVirtualWidth
= minW
;
535 m_maxVirtualWidth
= maxW
;
536 m_minVirtualHeight
= minH
;
537 m_maxVirtualHeight
= maxH
;
539 SetVirtualSize( GetClientSize() );
542 void wxWindowBase::DoSetVirtualSize( int x
, int y
)
544 if( m_minVirtualWidth
!= -1 && m_minVirtualWidth
> x
) x
= m_minVirtualWidth
;
545 if( m_maxVirtualWidth
!= -1 && m_maxVirtualWidth
< x
) x
= m_maxVirtualWidth
;
546 if( m_minVirtualHeight
!= -1 && m_minVirtualHeight
> y
) y
= m_minVirtualHeight
;
547 if( m_maxVirtualHeight
!= -1 && m_maxVirtualHeight
< y
) y
= m_maxVirtualHeight
;
549 m_virtualSize
.SetWidth( x
);
550 m_virtualSize
.SetHeight( y
);
553 wxSize
wxWindowBase::DoGetVirtualSize() const
555 wxSize
s( GetClientSize() );
557 if( m_virtualSize
.GetWidth() != -1 )
558 s
.SetWidth( m_virtualSize
.GetWidth() );
559 if( m_virtualSize
.GetHeight() != -1 )
560 s
.SetHeight( m_virtualSize
.GetHeight() );
565 // ----------------------------------------------------------------------------
566 // show/hide/enable/disable the window
567 // ----------------------------------------------------------------------------
569 bool wxWindowBase::Show(bool show
)
571 if ( show
!= m_isShown
)
583 bool wxWindowBase::Enable(bool enable
)
585 if ( enable
!= m_isEnabled
)
587 m_isEnabled
= enable
;
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
600 bool wxWindowBase::IsTopLevel() const
605 // ----------------------------------------------------------------------------
606 // reparenting the window
607 // ----------------------------------------------------------------------------
609 void wxWindowBase::AddChild(wxWindowBase
*child
)
611 wxCHECK_RET( child
, wxT("can't add a NULL child") );
613 // this should never happen and it will lead to a crash later if it does
614 // because RemoveChild() will remove only one node from the children list
615 // and the other(s) one(s) will be left with dangling pointers in them
616 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
618 GetChildren().Append(child
);
619 child
->SetParent(this);
622 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
624 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
626 GetChildren().DeleteObject(child
);
627 child
->SetParent((wxWindow
*)NULL
);
630 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
632 wxWindow
*oldParent
= GetParent();
633 if ( newParent
== oldParent
)
639 // unlink this window from the existing parent.
642 oldParent
->RemoveChild(this);
646 wxTopLevelWindows
.DeleteObject(this);
649 // add it to the new one
652 newParent
->AddChild(this);
656 wxTopLevelWindows
.Append(this);
662 // ----------------------------------------------------------------------------
663 // event handler stuff
664 // ----------------------------------------------------------------------------
666 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
668 handler
->SetNextHandler(GetEventHandler());
669 SetEventHandler(handler
);
672 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
674 wxEvtHandler
*handlerA
= GetEventHandler();
677 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
678 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
679 SetEventHandler(handlerB
);
683 handlerA
= (wxEvtHandler
*)NULL
;
690 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
692 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
694 wxEvtHandler
*handlerPrev
= NULL
,
695 *handlerCur
= GetEventHandler();
698 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
700 if ( handlerCur
== handler
)
704 handlerPrev
->SetNextHandler(handlerNext
);
708 SetEventHandler(handlerNext
);
711 handler
->SetNextHandler(NULL
);
716 handlerPrev
= handlerCur
;
717 handlerCur
= handlerNext
;
720 wxFAIL_MSG( _T("where has the event handler gone?") );
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
731 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
734 m_backgroundColour
= colour
;
741 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
743 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
746 m_foregroundColour
= colour
;
753 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
755 // setting an invalid cursor is ok, it means that we don't have any special
757 if ( m_cursor
== cursor
)
768 bool wxWindowBase::SetFont(const wxFont
& font
)
770 // don't try to set invalid font, always fall back to the default
771 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
773 if ( fontOk
== m_font
)
788 void wxWindowBase::SetPalette(const wxPalette
& pal
)
790 m_hasCustomPalette
= TRUE
;
793 // VZ: can anyone explain me what do we do here?
794 wxWindowDC
d((wxWindow
*) this);
798 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
800 wxWindow
*win
= (wxWindow
*)this;
801 while ( win
&& !win
->HasCustomPalette() )
803 win
= win
->GetParent();
809 #endif // wxUSE_PALETTE
812 void wxWindowBase::SetCaret(wxCaret
*caret
)
823 wxASSERT_MSG( m_caret
->GetWindow() == this,
824 wxT("caret should be created associated to this window") );
827 #endif // wxUSE_CARET
830 // ----------------------------------------------------------------------------
832 // ----------------------------------------------------------------------------
834 void wxWindowBase::SetValidator(const wxValidator
& validator
)
836 if ( m_windowValidator
)
837 delete m_windowValidator
;
839 m_windowValidator
= (wxValidator
*)validator
.Clone();
841 if ( m_windowValidator
)
842 m_windowValidator
->SetWindow(this) ;
844 #endif // wxUSE_VALIDATORS
846 // ----------------------------------------------------------------------------
847 // update region stuff
848 // ----------------------------------------------------------------------------
850 wxRect
wxWindowBase::GetUpdateClientRect() const
852 wxRegion rgnUpdate
= GetUpdateRegion();
853 rgnUpdate
.Intersect(GetClientRect());
854 wxRect rectUpdate
= rgnUpdate
.GetBox();
855 wxPoint ptOrigin
= GetClientAreaOrigin();
856 rectUpdate
.x
-= ptOrigin
.x
;
857 rectUpdate
.y
-= ptOrigin
.y
;
862 bool wxWindowBase::IsExposed(int x
, int y
) const
864 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
867 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
869 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
872 // ----------------------------------------------------------------------------
873 // find child window by id or name
874 // ----------------------------------------------------------------------------
876 wxWindow
*wxWindowBase::FindWindow( long id
)
878 if ( id
== m_windowId
)
879 return (wxWindow
*)this;
881 wxWindowBase
*res
= (wxWindow
*)NULL
;
882 wxWindowList::Node
*node
;
883 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
885 wxWindowBase
*child
= node
->GetData();
886 res
= child
->FindWindow( id
);
889 return (wxWindow
*)res
;
892 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
894 if ( name
== m_windowName
)
895 return (wxWindow
*)this;
897 wxWindowBase
*res
= (wxWindow
*)NULL
;
898 wxWindowList::Node
*node
;
899 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
901 wxWindow
*child
= node
->GetData();
902 res
= child
->FindWindow(name
);
905 return (wxWindow
*)res
;
909 // find any window by id or name or label: If parent is non-NULL, look through
910 // children for a label or title matching the specified string. If NULL, look
911 // through all top-level windows.
913 // to avoid duplicating code we reuse the same helper function but with
914 // different comparators
916 typedef bool (*wxFindWindowCmp
)(const wxWindow
*win
,
917 const wxString
& label
, long id
);
920 bool wxFindWindowCmpLabels(const wxWindow
*win
, const wxString
& label
,
923 return win
->GetLabel() == label
;
927 bool wxFindWindowCmpNames(const wxWindow
*win
, const wxString
& label
,
930 return win
->GetName() == label
;
934 bool wxFindWindowCmpIds(const wxWindow
*win
, const wxString
& WXUNUSED(label
),
937 return win
->GetId() == id
;
940 // recursive helper for the FindWindowByXXX() functions
942 wxWindow
*wxFindWindowRecursively(const wxWindow
*parent
,
943 const wxString
& label
,
949 // see if this is the one we're looking for
950 if ( (*cmp
)(parent
, label
, id
) )
951 return (wxWindow
*)parent
;
953 // It wasn't, so check all its children
954 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
956 node
= node
->GetNext() )
958 // recursively check each child
959 wxWindow
*win
= (wxWindow
*)node
->GetData();
960 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
970 // helper for FindWindowByXXX()
972 wxWindow
*wxFindWindowHelper(const wxWindow
*parent
,
973 const wxString
& label
,
979 // just check parent and all its children
980 return wxFindWindowRecursively(parent
, label
, id
, cmp
);
983 // start at very top of wx's windows
984 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
986 node
= node
->GetNext() )
988 // recursively check each window & its children
989 wxWindow
*win
= node
->GetData();
990 wxWindow
*retwin
= wxFindWindowRecursively(win
, label
, id
, cmp
);
1000 wxWindowBase::FindWindowByLabel(const wxString
& title
, const wxWindow
*parent
)
1002 return wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpLabels
);
1007 wxWindowBase::FindWindowByName(const wxString
& title
, const wxWindow
*parent
)
1009 wxWindow
*win
= wxFindWindowHelper(parent
, title
, 0, wxFindWindowCmpNames
);
1013 // fall back to the label
1014 win
= FindWindowByLabel(title
, parent
);
1022 wxWindowBase::FindWindowById( long id
, const wxWindow
* parent
)
1024 return wxFindWindowHelper(parent
, _T(""), id
, wxFindWindowCmpIds
);
1027 // ----------------------------------------------------------------------------
1028 // dialog oriented functions
1029 // ----------------------------------------------------------------------------
1031 void wxWindowBase::MakeModal(bool modal
)
1033 // Disable all other windows
1036 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
1039 wxWindow
*win
= node
->GetData();
1041 win
->Enable(!modal
);
1043 node
= node
->GetNext();
1048 bool wxWindowBase::Validate()
1050 #if wxUSE_VALIDATORS
1051 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1053 wxWindowList::Node
*node
;
1054 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1056 wxWindowBase
*child
= node
->GetData();
1057 wxValidator
*validator
= child
->GetValidator();
1058 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
1063 if ( recurse
&& !child
->Validate() )
1068 #endif // wxUSE_VALIDATORS
1073 bool wxWindowBase::TransferDataToWindow()
1075 #if wxUSE_VALIDATORS
1076 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1078 wxWindowList::Node
*node
;
1079 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1081 wxWindowBase
*child
= node
->GetData();
1082 wxValidator
*validator
= child
->GetValidator();
1083 if ( validator
&& !validator
->TransferToWindow() )
1085 wxLogWarning(_("Could not transfer data to window"));
1086 wxLog::FlushActive();
1093 if ( !child
->TransferDataToWindow() )
1095 // warning already given
1100 #endif // wxUSE_VALIDATORS
1105 bool wxWindowBase::TransferDataFromWindow()
1107 #if wxUSE_VALIDATORS
1108 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
1110 wxWindowList::Node
*node
;
1111 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
1113 wxWindow
*child
= node
->GetData();
1114 wxValidator
*validator
= child
->GetValidator();
1115 if ( validator
&& !validator
->TransferFromWindow() )
1117 // nop warning here because the application is supposed to give
1118 // one itself - we don't know here what might have gone wrongly
1125 if ( !child
->TransferDataFromWindow() )
1127 // warning already given
1132 #endif // wxUSE_VALIDATORS
1137 void wxWindowBase::InitDialog()
1139 wxInitDialogEvent
event(GetId());
1140 event
.SetEventObject( this );
1141 GetEventHandler()->ProcessEvent(event
);
1144 // ----------------------------------------------------------------------------
1145 // context-sensitive help support
1146 // ----------------------------------------------------------------------------
1150 // associate this help text with this window
1151 void wxWindowBase::SetHelpText(const wxString
& text
)
1153 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1156 helpProvider
->AddHelp(this, text
);
1160 // associate this help text with all windows with the same id as this
1162 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1164 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1167 helpProvider
->AddHelp(GetId(), text
);
1171 // get the help string associated with this window (may be empty)
1172 wxString
wxWindowBase::GetHelpText() const
1175 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1178 text
= helpProvider
->GetHelp(this);
1184 // show help for this window
1185 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1187 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1190 if ( helpProvider
->ShowHelp(this) )
1192 // skip the event.Skip() below
1200 #endif // wxUSE_HELP
1202 // ----------------------------------------------------------------------------
1203 // tooltipsroot.Replace("\\", "/");
1204 // ----------------------------------------------------------------------------
1208 void wxWindowBase::SetToolTip( const wxString
&tip
)
1210 // don't create the new tooltip if we already have one
1213 m_tooltip
->SetTip( tip
);
1217 SetToolTip( new wxToolTip( tip
) );
1220 // setting empty tooltip text does not remove the tooltip any more - use
1221 // SetToolTip((wxToolTip *)NULL) for this
1224 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1229 m_tooltip
= tooltip
;
1232 #endif // wxUSE_TOOLTIPS
1234 // ----------------------------------------------------------------------------
1235 // constraints and sizers
1236 // ----------------------------------------------------------------------------
1238 #if wxUSE_CONSTRAINTS
1240 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1242 if ( m_constraints
)
1244 UnsetConstraints(m_constraints
);
1245 delete m_constraints
;
1247 m_constraints
= constraints
;
1248 if ( m_constraints
)
1250 // Make sure other windows know they're part of a 'meaningful relationship'
1251 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1252 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1253 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1254 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1255 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1256 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1257 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1258 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1259 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1260 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1261 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1262 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1263 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1264 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1265 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1266 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1270 // This removes any dangling pointers to this window in other windows'
1271 // constraintsInvolvedIn lists.
1272 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1276 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1277 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1278 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1279 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1280 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1281 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1282 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1283 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1284 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1285 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1286 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1287 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1288 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1289 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1290 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1291 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1295 // Back-pointer to other windows we're involved with, so if we delete this
1296 // window, we must delete any constraints we're involved with.
1297 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1299 if ( !m_constraintsInvolvedIn
)
1300 m_constraintsInvolvedIn
= new wxWindowList
;
1301 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1302 m_constraintsInvolvedIn
->Append(otherWin
);
1305 // REMOVE back-pointer to other windows we're involved with.
1306 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1308 if ( m_constraintsInvolvedIn
)
1309 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1312 // Reset any constraints that mention this window
1313 void wxWindowBase::DeleteRelatedConstraints()
1315 if ( m_constraintsInvolvedIn
)
1317 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1320 wxWindow
*win
= node
->GetData();
1321 wxLayoutConstraints
*constr
= win
->GetConstraints();
1323 // Reset any constraints involving this window
1326 constr
->left
.ResetIfWin(this);
1327 constr
->top
.ResetIfWin(this);
1328 constr
->right
.ResetIfWin(this);
1329 constr
->bottom
.ResetIfWin(this);
1330 constr
->width
.ResetIfWin(this);
1331 constr
->height
.ResetIfWin(this);
1332 constr
->centreX
.ResetIfWin(this);
1333 constr
->centreY
.ResetIfWin(this);
1336 wxWindowList::Node
*next
= node
->GetNext();
1341 delete m_constraintsInvolvedIn
;
1342 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1347 void wxWindowBase::SetSizer(wxSizer
*sizer
, bool deleteOld
)
1349 if (m_windowSizer
&& deleteOld
) delete m_windowSizer
;
1351 m_windowSizer
= sizer
;
1353 SetAutoLayout( sizer
!= 0 );
1356 void wxWindowBase::SetSizerAndFit(wxSizer
*sizer
, bool deleteOld
)
1358 SetSizer( sizer
, deleteOld
);
1359 sizer
->SetSizeHints( (wxWindow
*) this );
1362 bool wxWindowBase::Layout()
1364 // If there is a sizer, use it instead of the constraints
1368 GetVirtualSize(&w
, &h
);
1369 GetSizer()->SetDimension( 0, 0, w
, h
);
1371 #if wxUSE_CONSTRAINTS
1374 wxLayoutConstraints
*constr
= GetConstraints();
1375 bool wasOk
= constr
&& constr
->AreSatisfied();
1377 ResetConstraints(); // Mark all constraints as unevaluated
1379 // if we're a top level panel (i.e. our parent is frame/dialog), our
1380 // own constraints will never be satisfied any more unless we do it
1385 while ( noChanges
> 0 )
1387 constr
->SatisfyConstraints(this, &noChanges
);
1391 DoPhase(1); // Layout children
1392 DoPhase(2); // Layout grand children
1393 SetConstraintSizes(); // Recursively set the real window sizes
1400 #if wxUSE_CONSTRAINTS
1401 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1402 // do a similar thing, but also impose their own 'constraints' and order the
1403 // evaluation differently.
1404 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1406 wxLayoutConstraints
*constr
= GetConstraints();
1409 return constr
->SatisfyConstraints(this, noChanges
);
1415 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1425 // Do a phase of evaluating child constraints
1426 bool wxWindowBase::DoPhase(int phase
)
1428 int noIterations
= 0;
1429 int maxIterations
= 500;
1432 wxWindowList succeeded
;
1433 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1437 wxWindowList::Node
*node
= GetChildren().GetFirst();
1440 wxWindow
*child
= node
->GetData();
1441 if ( !child
->IsTopLevel() )
1443 wxLayoutConstraints
*constr
= child
->GetConstraints();
1446 if ( !succeeded
.Find(child
) )
1448 int tempNoChanges
= 0;
1449 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1450 noChanges
+= tempNoChanges
;
1453 succeeded
.Append(child
);
1458 node
= node
->GetNext();
1467 void wxWindowBase::ResetConstraints()
1469 wxLayoutConstraints
*constr
= GetConstraints();
1472 constr
->left
.SetDone(FALSE
);
1473 constr
->top
.SetDone(FALSE
);
1474 constr
->right
.SetDone(FALSE
);
1475 constr
->bottom
.SetDone(FALSE
);
1476 constr
->width
.SetDone(FALSE
);
1477 constr
->height
.SetDone(FALSE
);
1478 constr
->centreX
.SetDone(FALSE
);
1479 constr
->centreY
.SetDone(FALSE
);
1482 wxWindowList::Node
*node
= GetChildren().GetFirst();
1485 wxWindow
*win
= node
->GetData();
1486 if ( !win
->IsTopLevel() )
1487 win
->ResetConstraints();
1488 node
= node
->GetNext();
1492 // Need to distinguish between setting the 'fake' size for windows and sizers,
1493 // and setting the real values.
1494 void wxWindowBase::SetConstraintSizes(bool recurse
)
1496 wxLayoutConstraints
*constr
= GetConstraints();
1497 if ( constr
&& constr
->AreSatisfied() )
1499 int x
= constr
->left
.GetValue();
1500 int y
= constr
->top
.GetValue();
1501 int w
= constr
->width
.GetValue();
1502 int h
= constr
->height
.GetValue();
1504 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1505 (constr
->height
.GetRelationship() != wxAsIs
) )
1507 SetSize(x
, y
, w
, h
);
1511 // If we don't want to resize this window, just move it...
1517 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1518 GetClassInfo()->GetClassName(),
1524 wxWindowList::Node
*node
= GetChildren().GetFirst();
1527 wxWindow
*win
= node
->GetData();
1528 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1529 win
->SetConstraintSizes();
1530 node
= node
->GetNext();
1535 // Only set the size/position of the constraint (if any)
1536 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1538 wxLayoutConstraints
*constr
= GetConstraints();
1543 constr
->left
.SetValue(x
);
1544 constr
->left
.SetDone(TRUE
);
1548 constr
->top
.SetValue(y
);
1549 constr
->top
.SetDone(TRUE
);
1553 constr
->width
.SetValue(w
);
1554 constr
->width
.SetDone(TRUE
);
1558 constr
->height
.SetValue(h
);
1559 constr
->height
.SetDone(TRUE
);
1564 void wxWindowBase::MoveConstraint(int x
, int y
)
1566 wxLayoutConstraints
*constr
= GetConstraints();
1571 constr
->left
.SetValue(x
);
1572 constr
->left
.SetDone(TRUE
);
1576 constr
->top
.SetValue(y
);
1577 constr
->top
.SetDone(TRUE
);
1582 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1584 wxLayoutConstraints
*constr
= GetConstraints();
1587 *w
= constr
->width
.GetValue();
1588 *h
= constr
->height
.GetValue();
1594 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1596 wxLayoutConstraints
*constr
= GetConstraints();
1599 *w
= constr
->width
.GetValue();
1600 *h
= constr
->height
.GetValue();
1603 GetClientSize(w
, h
);
1606 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1608 wxLayoutConstraints
*constr
= GetConstraints();
1611 *x
= constr
->left
.GetValue();
1612 *y
= constr
->top
.GetValue();
1618 #endif // wxUSE_CONSTRAINTS
1620 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
) const
1622 // don't do it for the dialogs/frames - they float independently of their
1624 if ( !IsTopLevel() )
1626 wxWindow
*parent
= GetParent();
1627 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1629 wxPoint
pt(parent
->GetClientAreaOrigin());
1636 // ----------------------------------------------------------------------------
1637 // do Update UI processing for child controls
1638 // ----------------------------------------------------------------------------
1640 // TODO: should this be implemented for the child window rather
1641 // than the parent? Then you can override it e.g. for wxCheckBox
1642 // to do the Right Thing rather than having to assume a fixed number
1643 // of control classes.
1644 void wxWindowBase::UpdateWindowUI()
1647 wxUpdateUIEvent
event(GetId());
1648 event
.m_eventObject
= this;
1650 if ( GetEventHandler()->ProcessEvent(event
) )
1652 if ( event
.GetSetEnabled() )
1653 Enable(event
.GetEnabled());
1655 if ( event
.GetSetText() )
1657 wxControl
*control
= wxDynamicCastThis(wxControl
);
1661 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1663 text
->SetValue(event
.GetText());
1665 #endif // wxUSE_TEXTCTRL
1666 control
->SetLabel(event
.GetText());
1671 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1674 if ( event
.GetSetChecked() )
1675 checkbox
->SetValue(event
.GetChecked());
1677 #endif // wxUSE_CHECKBOX
1680 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1683 if ( event
.GetSetChecked() )
1684 radiobtn
->SetValue(event
.GetChecked());
1686 #endif // wxUSE_RADIOBTN
1688 #endif // wxUSE_CONTROLS
1691 // ----------------------------------------------------------------------------
1692 // dialog units translations
1693 // ----------------------------------------------------------------------------
1695 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1697 int charWidth
= GetCharWidth();
1698 int charHeight
= GetCharHeight();
1699 wxPoint
pt2(-1, -1);
1701 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1703 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1708 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1710 int charWidth
= GetCharWidth();
1711 int charHeight
= GetCharHeight();
1712 wxPoint
pt2(-1, -1);
1714 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1716 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1721 // ----------------------------------------------------------------------------
1723 // ----------------------------------------------------------------------------
1725 // propagate the colour change event to the subwindows
1726 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1728 wxWindowList::Node
*node
= GetChildren().GetFirst();
1731 // Only propagate to non-top-level windows
1732 wxWindow
*win
= node
->GetData();
1733 if ( !win
->IsTopLevel() )
1735 wxSysColourChangedEvent event2
;
1736 event
.m_eventObject
= win
;
1737 win
->GetEventHandler()->ProcessEvent(event2
);
1740 node
= node
->GetNext();
1744 // the default action is to populate dialog with data when it's created
1745 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1747 TransferDataToWindow();
1750 // process Ctrl-Alt-mclick
1751 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1754 if ( event
.ControlDown() && event
.AltDown() )
1756 // don't translate these strings
1759 #ifdef __WXUNIVERSAL__
1761 #endif // __WXUNIVERSAL__
1763 switch ( wxGetOsVersion() )
1765 case wxMOTIF_X
: port
= _T("Motif"); break;
1767 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1768 case wxBEOS
: port
= _T("BeOS"); break;
1772 case wxGTK_BEOS
: port
= _T("GTK"); break;
1778 case wxWIN386
: port
= _T("MS Windows"); break;
1782 case wxMGL_OS2
: port
= _T("MGL"); break;
1784 case wxOS2_PM
: port
= _T("OS/2"); break;
1785 default: port
= _T("unknown"); break;
1788 wxMessageBox(wxString::Format(
1790 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1804 _T("wxWindows information"),
1805 wxICON_INFORMATION
| wxOK
,
1809 #endif // wxUSE_MSGDLG
1815 // ----------------------------------------------------------------------------
1816 // list classes implementation
1817 // ----------------------------------------------------------------------------
1819 void wxWindowListNode::DeleteData()
1821 delete (wxWindow
*)GetData();
1824 // ----------------------------------------------------------------------------
1826 // ----------------------------------------------------------------------------
1828 wxBorder
wxWindowBase::GetBorder() const
1830 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1831 if ( border
== wxBORDER_DEFAULT
)
1833 border
= GetDefaultBorder();
1839 wxBorder
wxWindowBase::GetDefaultBorder() const
1841 return wxBORDER_NONE
;
1844 // ----------------------------------------------------------------------------
1846 // ----------------------------------------------------------------------------
1848 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1850 // here we just check if the point is inside the window or not
1852 // check the top and left border first
1853 bool outside
= x
< 0 || y
< 0;
1856 // check the right and bottom borders too
1857 wxSize size
= GetSize();
1858 outside
= x
>= size
.x
|| y
>= size
.y
;
1861 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1864 // ----------------------------------------------------------------------------
1866 // ----------------------------------------------------------------------------
1868 struct WXDLLEXPORT wxWindowNext
1872 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1874 void wxWindowBase::CaptureMouse()
1876 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1878 wxWindow
*winOld
= GetCapture();
1881 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
1884 wxWindowNext
*item
= new wxWindowNext
;
1886 item
->next
= ms_winCaptureNext
;
1887 ms_winCaptureNext
= item
;
1889 //else: no mouse capture to save
1894 void wxWindowBase::ReleaseMouse()
1896 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1898 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1902 if ( ms_winCaptureNext
)
1904 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
1906 wxWindowNext
*item
= ms_winCaptureNext
;
1907 ms_winCaptureNext
= item
->next
;
1910 //else: stack is empty, no previous capture
1912 wxLogTrace(_T("mousecapture"),
1913 _T("After ReleaseMouse() mouse is captured by 0x%08x"),
1917 // ----------------------------------------------------------------------------
1919 // ----------------------------------------------------------------------------
1921 wxWindow
* wxGetTopLevelParent(wxWindow
*win
)
1923 while ( win
&& !win
->IsTopLevel() )
1924 win
= win
->GetParent();