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 // Whether we're using the current theme for this window (wxGTK only for now)
178 m_themeEnabled
= FALSE
;
181 // common part of window creation process
182 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
184 const wxPoint
& WXUNUSED(pos
),
185 const wxSize
& WXUNUSED(size
),
187 const wxValidator
& validator
,
188 const wxString
& name
)
190 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
191 // member variables - check that it has been called (will catch the case
192 // when a new ctor is added which doesn't call InitWindow)
193 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
195 // generate a new id if the user doesn't care about it
196 m_windowId
= id
== -1 ? NewControlId() : id
;
199 SetWindowStyleFlag(style
);
203 SetValidator(validator
);
204 #endif // wxUSE_VALIDATORS
206 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
207 // have it too - like this it's possible to set it only in the top level
208 // dialog/frame and all children will inherit it by defult
209 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
211 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 wxWindowBase::~wxWindowBase()
224 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
226 // FIXME if these 2 cases result from programming errors in the user code
227 // we should probably assert here instead of silently fixing them
229 // Just in case the window has been Closed, but we're then deleting
230 // immediately: don't leave dangling pointers.
231 wxPendingDelete
.DeleteObject(this);
233 // Just in case we've loaded a top-level window via LoadNativeDialog but
234 // we weren't a dialog class
235 wxTopLevelWindows
.DeleteObject(this);
237 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
242 #endif // wxUSE_CARET
245 if ( m_windowValidator
)
246 delete m_windowValidator
;
247 #endif // wxUSE_VALIDATORS
249 #if wxUSE_CONSTRAINTS
250 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
251 // at deleted windows as they delete themselves.
252 DeleteRelatedConstraints();
256 // This removes any dangling pointers to this window in other windows'
257 // constraintsInvolvedIn lists.
258 UnsetConstraints(m_constraints
);
259 delete m_constraints
;
260 m_constraints
= NULL
;
263 #endif // wxUSE_CONSTRAINTS
265 if ( m_containingSizer
)
266 m_containingSizer
->Remove((wxWindow
*)this);
269 delete m_windowSizer
;
271 #if wxUSE_DRAG_AND_DROP
274 #endif // wxUSE_DRAG_AND_DROP
279 #endif // wxUSE_TOOLTIPS
281 // reset the dangling pointer our parent window may keep to us
282 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
284 m_parent
->SetDefaultItem(NULL
);
288 bool wxWindowBase::Destroy()
295 bool wxWindowBase::Close(bool force
)
297 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
298 event
.SetEventObject(this);
299 #if WXWIN_COMPATIBILITY
300 event
.SetForce(force
);
301 #endif // WXWIN_COMPATIBILITY
302 event
.SetCanVeto(!force
);
304 // return FALSE if window wasn't closed because the application vetoed the
306 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
309 bool wxWindowBase::DestroyChildren()
311 wxWindowList::Node
*node
;
314 // we iterate until the list becomes empty
315 node
= GetChildren().GetFirst();
319 wxWindow
*child
= node
->GetData();
321 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
326 wxASSERT_MSG( !GetChildren().Find(child
),
327 wxT("child didn't remove itself using RemoveChild()") );
333 // ----------------------------------------------------------------------------
334 // size/position related methods
335 // ----------------------------------------------------------------------------
337 // centre the window with respect to its parent in either (or both) directions
338 void wxWindowBase::Centre(int direction
)
340 // the position/size of the parent window or of the entire screen
342 int widthParent
, heightParent
;
344 wxWindow
*parent
= NULL
;
346 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
348 // find the parent to centre this window on: it should be the
349 // immediate parent for the controls but the top level parent for the
350 // top level windows (like dialogs)
351 parent
= GetParent();
354 while ( parent
&& !parent
->IsTopLevel() )
356 parent
= parent
->GetParent();
360 // did we find the parent?
364 direction
|= wxCENTRE_ON_SCREEN
;
368 if ( direction
& wxCENTRE_ON_SCREEN
)
370 // centre with respect to the whole screen
371 wxDisplaySize(&widthParent
, &heightParent
);
377 // centre on the parent
378 parent
->GetSize(&widthParent
, &heightParent
);
380 // adjust to the parents position
381 posParent
= parent
->GetPosition();
385 // centre inside the parents client rectangle
386 parent
->GetClientSize(&widthParent
, &heightParent
);
391 GetSize(&width
, &height
);
396 if ( direction
& wxHORIZONTAL
)
397 xNew
= (widthParent
- width
)/2;
399 if ( direction
& wxVERTICAL
)
400 yNew
= (heightParent
- height
)/2;
405 // Base size of the visible dimensions of the display
406 // to take into account the taskbar
407 wxRect rect
= wxGetClientDisplayRect();
408 wxSize
size (rect
.width
,rect
.height
);
410 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
411 // but it may mean that the window is placed on other than the main
412 // display. Therefore we only make sure centered window is on the main display
413 // if the parent is at least partially present here.
414 if (posParent
.x
+ widthParent
>= 0) // if parent is (partially) on the main display
418 else if (xNew
+width
> size
.x
)
419 xNew
= size
.x
-width
-1;
421 if (posParent
.y
+ heightParent
>= 0) // if parent is (partially) on the main display
423 if (yNew
+height
> size
.y
)
424 yNew
= size
.y
-height
-1;
426 // Make certain that the title bar is initially visible
427 // always, even if this would push the bottom of the
428 // dialog of the visible area of the display
433 // move the window to this position (keeping the old size but using
434 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
435 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
438 // fits the window around the children
439 void wxWindowBase::Fit()
441 if ( GetChildren().GetCount() > 0 )
443 wxSize size
= DoGetBestSize();
445 // for compatibility with the old versions and because it really looks
446 // slightly more pretty like this, add a pad
452 //else: do nothing if we have no children
455 // return the size best suited for the current window
456 wxSize
wxWindowBase::DoGetBestSize() const
458 if ( GetChildren().GetCount() > 0 )
460 // our minimal acceptable size is such that all our windows fit inside
464 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
466 node
= node
->GetNext() )
468 wxWindow
*win
= node
->GetData();
469 if ( win
->IsTopLevel()
471 || wxDynamicCast(win
, wxStatusBar
)
472 #endif // wxUSE_STATUSBAR
475 // dialogs and frames lie in different top level windows -
476 // don't deal with them here; as for the status bars, they
477 // don't lie in the client area at all
482 win
->GetPosition(&wx
, &wy
);
484 // if the window hadn't been positioned yet, assume that it is in
491 win
->GetSize(&ww
, &wh
);
492 if ( wx
+ ww
> maxX
)
494 if ( wy
+ wh
> maxY
)
498 return wxSize(maxX
, maxY
);
502 // for a generic window there is no natural best size - just use the
508 // by default the origin is not shifted
509 wxPoint
wxWindowBase::GetClientAreaOrigin() const
511 return wxPoint(0, 0);
514 // set the min/max size of the window
515 void wxWindowBase::SetSizeHints(int minW
, int minH
,
517 int WXUNUSED(incW
), int WXUNUSED(incH
))
525 // ----------------------------------------------------------------------------
526 // show/hide/enable/disable the window
527 // ----------------------------------------------------------------------------
529 bool wxWindowBase::Show(bool show
)
531 if ( show
!= m_isShown
)
543 bool wxWindowBase::Enable(bool enable
)
545 if ( enable
!= m_isEnabled
)
547 m_isEnabled
= enable
;
556 // ----------------------------------------------------------------------------
558 // ----------------------------------------------------------------------------
560 bool wxWindowBase::IsTopLevel() const
565 // ----------------------------------------------------------------------------
566 // reparenting the window
567 // ----------------------------------------------------------------------------
569 void wxWindowBase::AddChild(wxWindowBase
*child
)
571 wxCHECK_RET( child
, wxT("can't add a NULL child") );
573 // this should never happen and it will lead to a crash later if it does
574 // because RemoveChild() will remove only one node from the children list
575 // and the other(s) one(s) will be left with dangling pointers in them
576 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
578 GetChildren().Append(child
);
579 child
->SetParent(this);
582 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
584 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
586 GetChildren().DeleteObject(child
);
587 child
->SetParent((wxWindow
*)NULL
);
590 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
592 wxWindow
*oldParent
= GetParent();
593 if ( newParent
== oldParent
)
599 // unlink this window from the existing parent.
602 oldParent
->RemoveChild(this);
606 wxTopLevelWindows
.DeleteObject(this);
609 // add it to the new one
612 newParent
->AddChild(this);
616 wxTopLevelWindows
.Append(this);
622 // ----------------------------------------------------------------------------
623 // event handler stuff
624 // ----------------------------------------------------------------------------
626 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
628 handler
->SetNextHandler(GetEventHandler());
629 SetEventHandler(handler
);
632 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
634 wxEvtHandler
*handlerA
= GetEventHandler();
637 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
638 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
639 SetEventHandler(handlerB
);
643 handlerA
= (wxEvtHandler
*)NULL
;
650 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
652 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
654 wxEvtHandler
*handlerPrev
= NULL
,
655 *handlerCur
= GetEventHandler();
658 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
660 if ( handlerCur
== handler
)
664 handlerPrev
->SetNextHandler(handlerNext
);
668 SetEventHandler(handlerNext
);
671 handler
->SetNextHandler(NULL
);
676 handlerPrev
= handlerCur
;
677 handlerCur
= handlerNext
;
680 wxFAIL_MSG( _T("where has the event handler gone?") );
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
689 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
691 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
694 m_backgroundColour
= colour
;
701 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
703 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
706 m_foregroundColour
= colour
;
713 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
715 // setting an invalid cursor is ok, it means that we don't have any special
717 if ( m_cursor
== cursor
)
728 bool wxWindowBase::SetFont(const wxFont
& font
)
730 // don't try to set invalid font, always fall back to the default
731 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
733 if ( fontOk
== m_font
)
748 void wxWindowBase::SetPalette(const wxPalette
& pal
)
750 m_hasCustomPalette
= TRUE
;
753 // VZ: can anyone explain me what do we do here?
754 wxWindowDC
d((wxWindow
*) this);
758 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
760 wxWindow
*win
= (wxWindow
*)this;
761 while ( win
&& !win
->HasCustomPalette() )
763 win
= win
->GetParent();
769 #endif // wxUSE_PALETTE
772 void wxWindowBase::SetCaret(wxCaret
*caret
)
783 wxASSERT_MSG( m_caret
->GetWindow() == this,
784 wxT("caret should be created associated to this window") );
787 #endif // wxUSE_CARET
790 // ----------------------------------------------------------------------------
792 // ----------------------------------------------------------------------------
794 void wxWindowBase::SetValidator(const wxValidator
& validator
)
796 if ( m_windowValidator
)
797 delete m_windowValidator
;
799 m_windowValidator
= (wxValidator
*)validator
.Clone();
801 if ( m_windowValidator
)
802 m_windowValidator
->SetWindow(this) ;
804 #endif // wxUSE_VALIDATORS
806 // ----------------------------------------------------------------------------
807 // update region stuff
808 // ----------------------------------------------------------------------------
810 wxRect
wxWindowBase::GetUpdateClientRect() const
812 wxRegion rgnUpdate
= GetUpdateRegion();
813 rgnUpdate
.Intersect(GetClientRect());
814 wxRect rectUpdate
= rgnUpdate
.GetBox();
815 wxPoint ptOrigin
= GetClientAreaOrigin();
816 rectUpdate
.x
-= ptOrigin
.x
;
817 rectUpdate
.y
-= ptOrigin
.y
;
822 bool wxWindowBase::IsExposed(int x
, int y
) const
824 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
827 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
829 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
832 // ----------------------------------------------------------------------------
833 // find window by id or name
834 // ----------------------------------------------------------------------------
836 wxWindow
*wxWindowBase::FindWindow( long id
)
838 if ( id
== m_windowId
)
839 return (wxWindow
*)this;
841 wxWindowBase
*res
= (wxWindow
*)NULL
;
842 wxWindowList::Node
*node
;
843 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
845 wxWindowBase
*child
= node
->GetData();
846 res
= child
->FindWindow( id
);
849 return (wxWindow
*)res
;
852 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
854 if ( name
== m_windowName
)
855 return (wxWindow
*)this;
857 wxWindowBase
*res
= (wxWindow
*)NULL
;
858 wxWindowList::Node
*node
;
859 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
861 wxWindow
*child
= node
->GetData();
862 res
= child
->FindWindow(name
);
865 return (wxWindow
*)res
;
868 // ----------------------------------------------------------------------------
869 // dialog oriented functions
870 // ----------------------------------------------------------------------------
872 void wxWindowBase::MakeModal(bool modal
)
874 // Disable all other windows
877 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
880 wxWindow
*win
= node
->GetData();
884 node
= node
->GetNext();
889 bool wxWindowBase::Validate()
892 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
894 wxWindowList::Node
*node
;
895 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
897 wxWindowBase
*child
= node
->GetData();
898 wxValidator
*validator
= child
->GetValidator();
899 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
904 if ( recurse
&& !child
->Validate() )
909 #endif // wxUSE_VALIDATORS
914 bool wxWindowBase::TransferDataToWindow()
917 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
919 wxWindowList::Node
*node
;
920 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
922 wxWindowBase
*child
= node
->GetData();
923 wxValidator
*validator
= child
->GetValidator();
924 if ( validator
&& !validator
->TransferToWindow() )
926 wxLogWarning(_("Could not transfer data to window"));
927 wxLog::FlushActive();
934 if ( !child
->TransferDataToWindow() )
936 // warning already given
941 #endif // wxUSE_VALIDATORS
946 bool wxWindowBase::TransferDataFromWindow()
949 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
951 wxWindowList::Node
*node
;
952 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
954 wxWindow
*child
= node
->GetData();
955 wxValidator
*validator
= child
->GetValidator();
956 if ( validator
&& !validator
->TransferFromWindow() )
958 // nop warning here because the application is supposed to give
959 // one itself - we don't know here what might have gone wrongly
966 if ( !child
->TransferDataFromWindow() )
968 // warning already given
973 #endif // wxUSE_VALIDATORS
978 void wxWindowBase::InitDialog()
980 wxInitDialogEvent
event(GetId());
981 event
.SetEventObject( this );
982 GetEventHandler()->ProcessEvent(event
);
985 // ----------------------------------------------------------------------------
986 // context-sensitive help support
987 // ----------------------------------------------------------------------------
991 // associate this help text with this window
992 void wxWindowBase::SetHelpText(const wxString
& text
)
994 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
997 helpProvider
->AddHelp(this, text
);
1001 // associate this help text with all windows with the same id as this
1003 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1005 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1008 helpProvider
->AddHelp(GetId(), text
);
1012 // get the help string associated with this window (may be empty)
1013 wxString
wxWindowBase::GetHelpText() const
1016 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1019 text
= helpProvider
->GetHelp(this);
1025 // show help for this window
1026 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1028 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1031 if ( helpProvider
->ShowHelp(this) )
1033 // skip the event.Skip() below
1041 #endif // wxUSE_HELP
1043 // ----------------------------------------------------------------------------
1044 // tooltipsroot.Replace("\\", "/");
1045 // ----------------------------------------------------------------------------
1049 void wxWindowBase::SetToolTip( const wxString
&tip
)
1051 // don't create the new tooltip if we already have one
1054 m_tooltip
->SetTip( tip
);
1058 SetToolTip( new wxToolTip( tip
) );
1061 // setting empty tooltip text does not remove the tooltip any more - use
1062 // SetToolTip((wxToolTip *)NULL) for this
1065 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1070 m_tooltip
= tooltip
;
1073 #endif // wxUSE_TOOLTIPS
1075 // ----------------------------------------------------------------------------
1076 // constraints and sizers
1077 // ----------------------------------------------------------------------------
1079 #if wxUSE_CONSTRAINTS
1081 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1083 if ( m_constraints
)
1085 UnsetConstraints(m_constraints
);
1086 delete m_constraints
;
1088 m_constraints
= constraints
;
1089 if ( m_constraints
)
1091 // Make sure other windows know they're part of a 'meaningful relationship'
1092 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1093 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1094 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1095 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1096 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1097 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1098 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1099 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1100 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1101 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1102 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1103 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1104 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1105 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1106 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1107 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1111 // This removes any dangling pointers to this window in other windows'
1112 // constraintsInvolvedIn lists.
1113 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1117 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1118 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1119 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1120 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1121 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1122 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1123 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1124 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1125 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1126 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1127 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1128 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1129 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1130 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1131 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1132 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1136 // Back-pointer to other windows we're involved with, so if we delete this
1137 // window, we must delete any constraints we're involved with.
1138 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1140 if ( !m_constraintsInvolvedIn
)
1141 m_constraintsInvolvedIn
= new wxWindowList
;
1142 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1143 m_constraintsInvolvedIn
->Append(otherWin
);
1146 // REMOVE back-pointer to other windows we're involved with.
1147 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1149 if ( m_constraintsInvolvedIn
)
1150 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1153 // Reset any constraints that mention this window
1154 void wxWindowBase::DeleteRelatedConstraints()
1156 if ( m_constraintsInvolvedIn
)
1158 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1161 wxWindow
*win
= node
->GetData();
1162 wxLayoutConstraints
*constr
= win
->GetConstraints();
1164 // Reset any constraints involving this window
1167 constr
->left
.ResetIfWin(this);
1168 constr
->top
.ResetIfWin(this);
1169 constr
->right
.ResetIfWin(this);
1170 constr
->bottom
.ResetIfWin(this);
1171 constr
->width
.ResetIfWin(this);
1172 constr
->height
.ResetIfWin(this);
1173 constr
->centreX
.ResetIfWin(this);
1174 constr
->centreY
.ResetIfWin(this);
1177 wxWindowList::Node
*next
= node
->GetNext();
1182 delete m_constraintsInvolvedIn
;
1183 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1188 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1190 if (m_windowSizer
) delete m_windowSizer
;
1192 m_windowSizer
= sizer
;
1195 bool wxWindowBase::Layout()
1197 // If there is a sizer, use it instead of the constraints
1201 GetClientSize(&w
, &h
);
1203 GetSizer()->SetDimension( 0, 0, w
, h
);
1205 #if wxUSE_CONSTRAINTS
1208 wxLayoutConstraints
*constr
= GetConstraints();
1209 bool wasOk
= constr
&& constr
->AreSatisfied();
1211 ResetConstraints(); // Mark all constraints as unevaluated
1213 // if we're a top level panel (i.e. our parent is frame/dialog), our
1214 // own constraints will never be satisfied any more unless we do it
1219 while ( noChanges
> 0 )
1221 constr
->SatisfyConstraints(this, &noChanges
);
1225 DoPhase(1); // Layout children
1226 DoPhase(2); // Layout grand children
1227 SetConstraintSizes(); // Recursively set the real window sizes
1234 #if wxUSE_CONSTRAINTS
1235 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1236 // do a similar thing, but also impose their own 'constraints' and order the
1237 // evaluation differently.
1238 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1240 wxLayoutConstraints
*constr
= GetConstraints();
1243 return constr
->SatisfyConstraints(this, noChanges
);
1249 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1259 // Do a phase of evaluating child constraints
1260 bool wxWindowBase::DoPhase(int phase
)
1262 int noIterations
= 0;
1263 int maxIterations
= 500;
1266 wxWindowList succeeded
;
1267 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1271 wxWindowList::Node
*node
= GetChildren().GetFirst();
1274 wxWindow
*child
= node
->GetData();
1275 if ( !child
->IsTopLevel() )
1277 wxLayoutConstraints
*constr
= child
->GetConstraints();
1280 if ( !succeeded
.Find(child
) )
1282 int tempNoChanges
= 0;
1283 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1284 noChanges
+= tempNoChanges
;
1287 succeeded
.Append(child
);
1292 node
= node
->GetNext();
1301 void wxWindowBase::ResetConstraints()
1303 wxLayoutConstraints
*constr
= GetConstraints();
1306 constr
->left
.SetDone(FALSE
);
1307 constr
->top
.SetDone(FALSE
);
1308 constr
->right
.SetDone(FALSE
);
1309 constr
->bottom
.SetDone(FALSE
);
1310 constr
->width
.SetDone(FALSE
);
1311 constr
->height
.SetDone(FALSE
);
1312 constr
->centreX
.SetDone(FALSE
);
1313 constr
->centreY
.SetDone(FALSE
);
1316 wxWindowList::Node
*node
= GetChildren().GetFirst();
1319 wxWindow
*win
= node
->GetData();
1320 if ( !win
->IsTopLevel() )
1321 win
->ResetConstraints();
1322 node
= node
->GetNext();
1326 // Need to distinguish between setting the 'fake' size for windows and sizers,
1327 // and setting the real values.
1328 void wxWindowBase::SetConstraintSizes(bool recurse
)
1330 wxLayoutConstraints
*constr
= GetConstraints();
1331 if ( constr
&& constr
->AreSatisfied() )
1333 int x
= constr
->left
.GetValue();
1334 int y
= constr
->top
.GetValue();
1335 int w
= constr
->width
.GetValue();
1336 int h
= constr
->height
.GetValue();
1338 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1339 (constr
->height
.GetRelationship() != wxAsIs
) )
1341 SetSize(x
, y
, w
, h
);
1345 // If we don't want to resize this window, just move it...
1351 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1352 GetClassInfo()->GetClassName(),
1358 wxWindowList::Node
*node
= GetChildren().GetFirst();
1361 wxWindow
*win
= node
->GetData();
1362 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1363 win
->SetConstraintSizes();
1364 node
= node
->GetNext();
1369 // Only set the size/position of the constraint (if any)
1370 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1372 wxLayoutConstraints
*constr
= GetConstraints();
1377 constr
->left
.SetValue(x
);
1378 constr
->left
.SetDone(TRUE
);
1382 constr
->top
.SetValue(y
);
1383 constr
->top
.SetDone(TRUE
);
1387 constr
->width
.SetValue(w
);
1388 constr
->width
.SetDone(TRUE
);
1392 constr
->height
.SetValue(h
);
1393 constr
->height
.SetDone(TRUE
);
1398 void wxWindowBase::MoveConstraint(int x
, int y
)
1400 wxLayoutConstraints
*constr
= GetConstraints();
1405 constr
->left
.SetValue(x
);
1406 constr
->left
.SetDone(TRUE
);
1410 constr
->top
.SetValue(y
);
1411 constr
->top
.SetDone(TRUE
);
1416 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1418 wxLayoutConstraints
*constr
= GetConstraints();
1421 *w
= constr
->width
.GetValue();
1422 *h
= constr
->height
.GetValue();
1428 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1430 wxLayoutConstraints
*constr
= GetConstraints();
1433 *w
= constr
->width
.GetValue();
1434 *h
= constr
->height
.GetValue();
1437 GetClientSize(w
, h
);
1440 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1442 wxLayoutConstraints
*constr
= GetConstraints();
1445 *x
= constr
->left
.GetValue();
1446 *y
= constr
->top
.GetValue();
1452 #endif // wxUSE_CONSTRAINTS
1454 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
) const
1456 // don't do it for the dialogs/frames - they float independently of their
1458 if ( !IsTopLevel() )
1460 wxWindow
*parent
= GetParent();
1461 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1463 wxPoint
pt(parent
->GetClientAreaOrigin());
1470 // ----------------------------------------------------------------------------
1471 // do Update UI processing for child controls
1472 // ----------------------------------------------------------------------------
1474 // TODO: should this be implemented for the child window rather
1475 // than the parent? Then you can override it e.g. for wxCheckBox
1476 // to do the Right Thing rather than having to assume a fixed number
1477 // of control classes.
1478 void wxWindowBase::UpdateWindowUI()
1481 wxUpdateUIEvent
event(GetId());
1482 event
.m_eventObject
= this;
1484 if ( GetEventHandler()->ProcessEvent(event
) )
1486 if ( event
.GetSetEnabled() )
1487 Enable(event
.GetEnabled());
1489 if ( event
.GetSetText() )
1491 wxControl
*control
= wxDynamicCastThis(wxControl
);
1495 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1497 text
->SetValue(event
.GetText());
1499 #endif // wxUSE_TEXTCTRL
1500 control
->SetLabel(event
.GetText());
1505 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1508 if ( event
.GetSetChecked() )
1509 checkbox
->SetValue(event
.GetChecked());
1511 #endif // wxUSE_CHECKBOX
1514 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1517 if ( event
.GetSetChecked() )
1518 radiobtn
->SetValue(event
.GetChecked());
1520 #endif // wxUSE_RADIOBTN
1522 #endif // wxUSE_CONTROLS
1525 // ----------------------------------------------------------------------------
1526 // dialog units translations
1527 // ----------------------------------------------------------------------------
1529 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1531 int charWidth
= GetCharWidth();
1532 int charHeight
= GetCharHeight();
1533 wxPoint
pt2(-1, -1);
1535 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1537 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1542 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1544 int charWidth
= GetCharWidth();
1545 int charHeight
= GetCharHeight();
1546 wxPoint
pt2(-1, -1);
1548 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1550 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1555 // ----------------------------------------------------------------------------
1557 // ----------------------------------------------------------------------------
1559 // propagate the colour change event to the subwindows
1560 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1562 wxWindowList::Node
*node
= GetChildren().GetFirst();
1565 // Only propagate to non-top-level windows
1566 wxWindow
*win
= node
->GetData();
1567 if ( !win
->IsTopLevel() )
1569 wxSysColourChangedEvent event2
;
1570 event
.m_eventObject
= win
;
1571 win
->GetEventHandler()->ProcessEvent(event2
);
1574 node
= node
->GetNext();
1578 // the default action is to populate dialog with data when it's created
1579 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1581 TransferDataToWindow();
1584 // process Ctrl-Alt-mclick
1585 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1588 if ( event
.ControlDown() && event
.AltDown() )
1590 // don't translate these strings
1593 #ifdef __WXUNIVERSAL__
1595 #endif // __WXUNIVERSAL__
1597 switch ( wxGetOsVersion() )
1599 case wxMOTIF_X
: port
= _T("Motif"); break;
1601 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1602 case wxBEOS
: port
= _T("BeOS"); break;
1606 case wxGTK_BEOS
: port
= _T("GTK"); break;
1612 case wxWIN386
: port
= _T("MS Windows"); break;
1616 case wxMGL_OS2
: port
= _T("MGL"); break;
1618 case wxOS2_PM
: port
= _T("OS/2"); break;
1619 default: port
= _T("unknown"); break;
1622 wxMessageBox(wxString::Format(
1624 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1638 _T("wxWindows information"),
1639 wxICON_INFORMATION
| wxOK
,
1643 #endif // wxUSE_MSGDLG
1649 // ----------------------------------------------------------------------------
1650 // list classes implementation
1651 // ----------------------------------------------------------------------------
1653 void wxWindowListNode::DeleteData()
1655 delete (wxWindow
*)GetData();
1658 // ----------------------------------------------------------------------------
1660 // ----------------------------------------------------------------------------
1662 wxBorder
wxWindowBase::GetBorder() const
1664 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1665 if ( border
== wxBORDER_DEFAULT
)
1667 border
= GetDefaultBorder();
1673 wxBorder
wxWindowBase::GetDefaultBorder() const
1675 return wxBORDER_NONE
;
1678 // ----------------------------------------------------------------------------
1680 // ----------------------------------------------------------------------------
1682 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1684 // here we just check if the point is inside the window or not
1686 // check the top and left border first
1687 bool outside
= x
< 0 || y
< 0;
1690 // check the right and bottom borders too
1691 wxSize size
= GetSize();
1692 outside
= x
>= size
.x
|| y
>= size
.y
;
1695 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1698 // ----------------------------------------------------------------------------
1700 // ----------------------------------------------------------------------------
1702 struct WXDLLEXPORT wxWindowNext
1706 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1708 void wxWindowBase::CaptureMouse()
1710 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1712 wxWindow
*winOld
= GetCapture();
1715 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
1718 wxWindowNext
*item
= new wxWindowNext
;
1720 item
->next
= ms_winCaptureNext
;
1721 ms_winCaptureNext
= item
;
1723 //else: no mouse capture to save
1728 void wxWindowBase::ReleaseMouse()
1730 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1732 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1736 if ( ms_winCaptureNext
)
1738 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
1740 wxWindowNext
*item
= ms_winCaptureNext
;
1741 ms_winCaptureNext
= item
->next
;
1744 //else: stack is empty, no previous capture
1746 wxLogTrace(_T("mousecapture"),
1747 _T("After ReleaseMouse() mouse is captured by 0x%08x"),