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"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // the default event handler is just this window
119 m_eventHandler
= this;
123 m_windowValidator
= (wxValidator
*) NULL
;
124 #endif // wxUSE_VALIDATORS
126 // use the system default colours
127 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
128 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
130 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
131 // so the font is *not* really set - but calls to SetFont() later won't do
132 // anything because m_font appears to be already set!
134 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
137 // the colours/fonts are default for now
146 // an optimization for the event processing: checking this flag is much
147 // faster than using IsKindOf(CLASSINFO(wxWindow))
150 #if wxUSE_CONSTRAINTS
151 // no constraints whatsoever
152 m_constraints
= (wxLayoutConstraints
*) NULL
;
153 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
154 m_windowSizer
= (wxSizer
*) NULL
;
155 m_autoLayout
= FALSE
;
156 #endif // wxUSE_CONSTRAINTS
158 #if wxUSE_DRAG_AND_DROP
159 m_dropTarget
= (wxDropTarget
*)NULL
;
160 #endif // wxUSE_DRAG_AND_DROP
163 m_tooltip
= (wxToolTip
*)NULL
;
164 #endif // wxUSE_TOOLTIPS
167 m_caret
= (wxCaret
*)NULL
;
168 #endif // wxUSE_CARET
171 m_hasCustomPalette
= FALSE
;
172 #endif // wxUSE_PALETTE
174 // Whether we're using the current theme for this window (wxGTK only for now)
175 m_themeEnabled
= FALSE
;
178 // common part of window creation process
179 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
181 const wxPoint
& WXUNUSED(pos
),
182 const wxSize
& WXUNUSED(size
),
184 const wxValidator
& validator
,
185 const wxString
& name
)
187 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
188 // member variables - check that it has been called (will catch the case
189 // when a new ctor is added which doesn't call InitWindow)
190 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
192 // generate a new id if the user doesn't care about it
193 m_windowId
= id
== -1 ? NewControlId() : id
;
196 SetWindowStyleFlag(style
);
200 SetValidator(validator
);
201 #endif // wxUSE_VALIDATORS
203 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
204 // have it too - like this it's possible to set it only in the top level
205 // dialog/frame and all children will inherit it by defult
206 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
208 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
219 wxWindowBase::~wxWindowBase()
221 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
223 // FIXME if these 2 cases result from programming errors in the user code
224 // we should probably assert here instead of silently fixing them
226 // Just in case the window has been Closed, but we're then deleting
227 // immediately: don't leave dangling pointers.
228 wxPendingDelete
.DeleteObject(this);
230 // Just in case we've loaded a top-level window via LoadNativeDialog but
231 // we weren't a dialog class
232 wxTopLevelWindows
.DeleteObject(this);
234 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
239 #endif // wxUSE_CARET
242 if ( m_windowValidator
)
243 delete m_windowValidator
;
244 #endif // wxUSE_VALIDATORS
246 #if wxUSE_CONSTRAINTS
247 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
248 // at deleted windows as they delete themselves.
249 DeleteRelatedConstraints();
253 // This removes any dangling pointers to this window in other windows'
254 // constraintsInvolvedIn lists.
255 UnsetConstraints(m_constraints
);
256 delete m_constraints
;
257 m_constraints
= NULL
;
261 delete m_windowSizer
;
263 #endif // wxUSE_CONSTRAINTS
265 #if wxUSE_DRAG_AND_DROP
268 #endif // wxUSE_DRAG_AND_DROP
273 #endif // wxUSE_TOOLTIPS
275 // reset the dangling pointer our parent window may keep to us
276 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
278 m_parent
->SetDefaultItem(NULL
);
282 bool wxWindowBase::Destroy()
289 bool wxWindowBase::Close(bool force
)
291 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
292 event
.SetEventObject(this);
293 #if WXWIN_COMPATIBILITY
294 event
.SetForce(force
);
295 #endif // WXWIN_COMPATIBILITY
296 event
.SetCanVeto(!force
);
298 // return FALSE if window wasn't closed because the application vetoed the
300 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
303 bool wxWindowBase::DestroyChildren()
305 wxWindowList::Node
*node
;
308 // we iterate until the list becomes empty
309 node
= GetChildren().GetFirst();
313 wxWindow
*child
= node
->GetData();
315 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
320 wxASSERT_MSG( !GetChildren().Find(child
),
321 wxT("child didn't remove itself using RemoveChild()") );
327 // ----------------------------------------------------------------------------
328 // size/position related methods
329 // ----------------------------------------------------------------------------
331 // centre the window with respect to its parent in either (or both) directions
332 void wxWindowBase::Centre(int direction
)
334 // the position/size of the parent window or of the entire screen
336 int widthParent
, heightParent
;
338 wxWindow
*parent
= NULL
;
340 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
342 // find the parent to centre this window on: it should be the
343 // immediate parent for the controls but the top level parent for the
344 // top level windows (like dialogs)
345 parent
= GetParent();
348 while ( parent
&& !parent
->IsTopLevel() )
350 parent
= parent
->GetParent();
354 // did we find the parent?
358 direction
|= wxCENTRE_ON_SCREEN
;
362 if ( direction
& wxCENTRE_ON_SCREEN
)
364 // centre with respect to the whole screen
365 wxDisplaySize(&widthParent
, &heightParent
);
371 // centre on the parent
372 parent
->GetSize(&widthParent
, &heightParent
);
374 // adjust to the parents position
375 posParent
= parent
->GetPosition();
379 // centre inside the parents client rectangle
380 parent
->GetClientSize(&widthParent
, &heightParent
);
385 GetSize(&width
, &height
);
390 if ( direction
& wxHORIZONTAL
)
391 xNew
= (widthParent
- width
)/2;
393 if ( direction
& wxVERTICAL
)
394 yNew
= (heightParent
- height
)/2;
399 // Base size of the visible dimensions of the display
400 // to take into account the taskbar
401 wxRect rect
= wxGetClientDisplayRect();
402 wxSize
size (rect
.width
,rect
.height
);
404 if (posParent
.x
>= 0) // if parent is on the main display
408 else if (xNew
+width
> size
.x
)
409 xNew
= size
.x
-width
-1;
411 if (posParent
.y
>= 0) // if parent is on the main display
413 if (yNew
+height
> size
.y
)
414 yNew
= size
.y
-height
-1;
416 // Make certain that the title bar is initially visible
417 // always, even if this would push the bottom of the
418 // dialog of the visible area of the display
423 // move the window to this position (keeping the old size but using
424 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
425 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
428 // fits the window around the children
429 void wxWindowBase::Fit()
431 if ( GetChildren().GetCount() > 0 )
433 wxSize size
= DoGetBestSize();
435 // for compatibility with the old versions and because it really looks
436 // slightly more pretty like this, add a pad
442 //else: do nothing if we have no children
445 // return the size best suited for the current window
446 wxSize
wxWindowBase::DoGetBestSize() const
448 if ( GetChildren().GetCount() > 0 )
450 // our minimal acceptable size is such that all our windows fit inside
454 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
456 node
= node
->GetNext() )
458 wxWindow
*win
= node
->GetData();
459 if ( win
->IsTopLevel()
461 || wxDynamicCast(win
, wxStatusBar
)
462 #endif // wxUSE_STATUSBAR
465 // dialogs and frames lie in different top level windows -
466 // don't deal with them here; as for the status bars, they
467 // don't lie in the client area at all
472 win
->GetPosition(&wx
, &wy
);
474 // if the window hadn't been positioned yet, assume that it is in
481 win
->GetSize(&ww
, &wh
);
482 if ( wx
+ ww
> maxX
)
484 if ( wy
+ wh
> maxY
)
488 return wxSize(maxX
, maxY
);
492 // for a generic window there is no natural best size - just use the
498 // by default the origin is not shifted
499 wxPoint
wxWindowBase::GetClientAreaOrigin() const
501 return wxPoint(0, 0);
504 // set the min/max size of the window
505 void wxWindowBase::SetSizeHints(int minW
, int minH
,
507 int WXUNUSED(incW
), int WXUNUSED(incH
))
515 // ----------------------------------------------------------------------------
516 // show/hide/enable/disable the window
517 // ----------------------------------------------------------------------------
519 bool wxWindowBase::Show(bool show
)
521 if ( show
!= m_isShown
)
533 bool wxWindowBase::Enable(bool enable
)
535 if ( enable
!= m_isEnabled
)
537 m_isEnabled
= enable
;
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
550 bool wxWindowBase::IsTopLevel() const
555 // ----------------------------------------------------------------------------
556 // reparenting the window
557 // ----------------------------------------------------------------------------
559 void wxWindowBase::AddChild(wxWindowBase
*child
)
561 wxCHECK_RET( child
, wxT("can't add a NULL child") );
563 // this should never happen and it will lead to a crash later if it does
564 // because RemoveChild() will remove only one node from the children list
565 // and the other(s) one(s) will be left with dangling pointers in them
566 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
568 GetChildren().Append(child
);
569 child
->SetParent(this);
572 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
574 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
576 GetChildren().DeleteObject(child
);
577 child
->SetParent((wxWindow
*)NULL
);
580 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
582 wxWindow
*oldParent
= GetParent();
583 if ( newParent
== oldParent
)
589 // unlink this window from the existing parent.
592 oldParent
->RemoveChild(this);
596 wxTopLevelWindows
.DeleteObject(this);
599 // add it to the new one
602 newParent
->AddChild(this);
606 wxTopLevelWindows
.Append(this);
612 // ----------------------------------------------------------------------------
613 // event handler stuff
614 // ----------------------------------------------------------------------------
616 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
618 handler
->SetNextHandler(GetEventHandler());
619 SetEventHandler(handler
);
622 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
624 wxEvtHandler
*handlerA
= GetEventHandler();
627 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
628 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
629 SetEventHandler(handlerB
);
633 handlerA
= (wxEvtHandler
*)NULL
;
640 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
642 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
644 wxEvtHandler
*handlerPrev
= NULL
,
645 *handlerCur
= GetEventHandler();
648 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
650 if ( handlerCur
== handler
)
654 handlerPrev
->SetNextHandler(handlerNext
);
658 SetEventHandler(handlerNext
);
661 handler
->SetNextHandler(NULL
);
666 handlerPrev
= handlerCur
;
667 handlerCur
= handlerNext
;
670 wxFAIL_MSG( _T("where has the event handler gone?") );
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
681 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
684 m_backgroundColour
= colour
;
691 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
693 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
696 m_foregroundColour
= colour
;
703 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
705 // setting an invalid cursor is ok, it means that we don't have any special
707 if ( m_cursor
== cursor
)
718 bool wxWindowBase::SetFont(const wxFont
& font
)
720 // don't try to set invalid font, always fall back to the default
721 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
723 if ( fontOk
== m_font
)
738 void wxWindowBase::SetPalette(const wxPalette
& pal
)
740 m_hasCustomPalette
= TRUE
;
743 // VZ: can anyone explain me what do we do here?
744 wxWindowDC
d((wxWindow
*) this);
748 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
750 wxWindow
*win
= (wxWindow
*)this;
751 while ( win
&& !win
->HasCustomPalette() )
753 win
= win
->GetParent();
759 #endif // wxUSE_PALETTE
762 void wxWindowBase::SetCaret(wxCaret
*caret
)
773 wxASSERT_MSG( m_caret
->GetWindow() == this,
774 wxT("caret should be created associated to this window") );
777 #endif // wxUSE_CARET
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 void wxWindowBase::SetValidator(const wxValidator
& validator
)
786 if ( m_windowValidator
)
787 delete m_windowValidator
;
789 m_windowValidator
= (wxValidator
*)validator
.Clone();
791 if ( m_windowValidator
)
792 m_windowValidator
->SetWindow(this) ;
794 #endif // wxUSE_VALIDATORS
796 // ----------------------------------------------------------------------------
797 // update region stuff
798 // ----------------------------------------------------------------------------
800 wxRect
wxWindowBase::GetUpdateClientRect() const
802 wxRegion rgnUpdate
= GetUpdateRegion();
803 rgnUpdate
.Intersect(GetClientRect());
804 wxRect rectUpdate
= rgnUpdate
.GetBox();
805 wxPoint ptOrigin
= GetClientAreaOrigin();
806 rectUpdate
.x
-= ptOrigin
.x
;
807 rectUpdate
.y
-= ptOrigin
.y
;
812 bool wxWindowBase::IsExposed(int x
, int y
) const
814 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
817 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
819 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
822 // ----------------------------------------------------------------------------
823 // find window by id or name
824 // ----------------------------------------------------------------------------
826 wxWindow
*wxWindowBase::FindWindow( long id
)
828 if ( id
== m_windowId
)
829 return (wxWindow
*)this;
831 wxWindowBase
*res
= (wxWindow
*)NULL
;
832 wxWindowList::Node
*node
;
833 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
835 wxWindowBase
*child
= node
->GetData();
836 res
= child
->FindWindow( id
);
839 return (wxWindow
*)res
;
842 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
844 if ( name
== m_windowName
)
845 return (wxWindow
*)this;
847 wxWindowBase
*res
= (wxWindow
*)NULL
;
848 wxWindowList::Node
*node
;
849 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
851 wxWindow
*child
= node
->GetData();
852 res
= child
->FindWindow(name
);
855 return (wxWindow
*)res
;
858 // ----------------------------------------------------------------------------
859 // dialog oriented functions
860 // ----------------------------------------------------------------------------
862 void wxWindowBase::MakeModal(bool modal
)
864 // Disable all other windows
867 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
870 wxWindow
*win
= node
->GetData();
874 node
= node
->GetNext();
879 bool wxWindowBase::Validate()
882 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
884 wxWindowList::Node
*node
;
885 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
887 wxWindowBase
*child
= node
->GetData();
888 wxValidator
*validator
= child
->GetValidator();
889 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
894 if ( recurse
&& !child
->Validate() )
899 #endif // wxUSE_VALIDATORS
904 bool wxWindowBase::TransferDataToWindow()
907 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
909 wxWindowList::Node
*node
;
910 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
912 wxWindowBase
*child
= node
->GetData();
913 wxValidator
*validator
= child
->GetValidator();
914 if ( validator
&& !validator
->TransferToWindow() )
916 wxLogWarning(_("Could not transfer data to window"));
917 wxLog::FlushActive();
924 if ( !child
->TransferDataToWindow() )
926 // warning already given
931 #endif // wxUSE_VALIDATORS
936 bool wxWindowBase::TransferDataFromWindow()
939 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
941 wxWindowList::Node
*node
;
942 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
944 wxWindow
*child
= node
->GetData();
945 wxValidator
*validator
= child
->GetValidator();
946 if ( validator
&& !validator
->TransferFromWindow() )
948 // nop warning here because the application is supposed to give
949 // one itself - we don't know here what might have gone wrongly
956 if ( !child
->TransferDataFromWindow() )
958 // warning already given
963 #endif // wxUSE_VALIDATORS
968 void wxWindowBase::InitDialog()
970 wxInitDialogEvent
event(GetId());
971 event
.SetEventObject( this );
972 GetEventHandler()->ProcessEvent(event
);
975 // ----------------------------------------------------------------------------
976 // context-sensitive help support
977 // ----------------------------------------------------------------------------
981 // associate this help text with this window
982 void wxWindowBase::SetHelpText(const wxString
& text
)
984 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
987 helpProvider
->AddHelp(this, text
);
991 // associate this help text with all windows with the same id as this
993 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
995 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
998 helpProvider
->AddHelp(GetId(), text
);
1002 // get the help string associated with this window (may be empty)
1003 wxString
wxWindowBase::GetHelpText() const
1006 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1009 text
= helpProvider
->GetHelp(this);
1015 // show help for this window
1016 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1018 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1021 if ( helpProvider
->ShowHelp(this) )
1023 // skip the event.Skip() below
1031 #endif // wxUSE_HELP
1033 // ----------------------------------------------------------------------------
1034 // tooltipsroot.Replace("\\", "/");
1035 // ----------------------------------------------------------------------------
1039 void wxWindowBase::SetToolTip( const wxString
&tip
)
1041 // don't create the new tooltip if we already have one
1044 m_tooltip
->SetTip( tip
);
1048 SetToolTip( new wxToolTip( tip
) );
1051 // setting empty tooltip text does not remove the tooltip any more - use
1052 // SetToolTip((wxToolTip *)NULL) for this
1055 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1060 m_tooltip
= tooltip
;
1063 #endif // wxUSE_TOOLTIPS
1065 // ----------------------------------------------------------------------------
1066 // constraints and sizers
1067 // ----------------------------------------------------------------------------
1069 #if wxUSE_CONSTRAINTS
1071 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1073 if ( m_constraints
)
1075 UnsetConstraints(m_constraints
);
1076 delete m_constraints
;
1078 m_constraints
= constraints
;
1079 if ( m_constraints
)
1081 // Make sure other windows know they're part of a 'meaningful relationship'
1082 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1083 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1084 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1085 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1086 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1087 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1088 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1089 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1090 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1091 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1092 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1093 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1094 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1095 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1096 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1097 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1101 // This removes any dangling pointers to this window in other windows'
1102 // constraintsInvolvedIn lists.
1103 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1107 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1108 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1109 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1110 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1111 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1112 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1113 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1114 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1115 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1116 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1117 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1118 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1119 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1120 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1121 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1122 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1126 // Back-pointer to other windows we're involved with, so if we delete this
1127 // window, we must delete any constraints we're involved with.
1128 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1130 if ( !m_constraintsInvolvedIn
)
1131 m_constraintsInvolvedIn
= new wxWindowList
;
1132 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1133 m_constraintsInvolvedIn
->Append(otherWin
);
1136 // REMOVE back-pointer to other windows we're involved with.
1137 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1139 if ( m_constraintsInvolvedIn
)
1140 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1143 // Reset any constraints that mention this window
1144 void wxWindowBase::DeleteRelatedConstraints()
1146 if ( m_constraintsInvolvedIn
)
1148 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1151 wxWindow
*win
= node
->GetData();
1152 wxLayoutConstraints
*constr
= win
->GetConstraints();
1154 // Reset any constraints involving this window
1157 constr
->left
.ResetIfWin(this);
1158 constr
->top
.ResetIfWin(this);
1159 constr
->right
.ResetIfWin(this);
1160 constr
->bottom
.ResetIfWin(this);
1161 constr
->width
.ResetIfWin(this);
1162 constr
->height
.ResetIfWin(this);
1163 constr
->centreX
.ResetIfWin(this);
1164 constr
->centreY
.ResetIfWin(this);
1167 wxWindowList::Node
*next
= node
->GetNext();
1172 delete m_constraintsInvolvedIn
;
1173 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1177 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1179 if (m_windowSizer
) delete m_windowSizer
;
1181 m_windowSizer
= sizer
;
1184 bool wxWindowBase::Layout()
1186 // If there is a sizer, use it instead of the constraints
1190 GetClientSize(&w
, &h
);
1192 GetSizer()->SetDimension( 0, 0, w
, h
);
1193 #if defined __WXPM__
1199 wxLayoutConstraints
*constr
= GetConstraints();
1200 bool wasOk
= constr
&& constr
->AreSatisfied();
1202 ResetConstraints(); // Mark all constraints as unevaluated
1204 // if we're a top level panel (i.e. our parent is frame/dialog), our
1205 // own constraints will never be satisfied any more unless we do it
1210 while ( noChanges
> 0 )
1212 constr
->SatisfyConstraints(this, &noChanges
);
1216 DoPhase(1); // Layout children
1217 DoPhase(2); // Layout grand children
1218 SetConstraintSizes(); // Recursively set the real window sizes
1225 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1226 // do a similar thing, but also impose their own 'constraints' and order the
1227 // evaluation differently.
1228 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1230 wxLayoutConstraints
*constr
= GetConstraints();
1233 return constr
->SatisfyConstraints(this, noChanges
);
1239 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1249 // Do a phase of evaluating child constraints
1250 bool wxWindowBase::DoPhase(int phase
)
1252 int noIterations
= 0;
1253 int maxIterations
= 500;
1256 wxWindowList succeeded
;
1257 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1261 wxWindowList::Node
*node
= GetChildren().GetFirst();
1264 wxWindow
*child
= node
->GetData();
1265 if ( !child
->IsTopLevel() )
1267 wxLayoutConstraints
*constr
= child
->GetConstraints();
1270 if ( !succeeded
.Find(child
) )
1272 int tempNoChanges
= 0;
1273 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1274 noChanges
+= tempNoChanges
;
1277 succeeded
.Append(child
);
1282 node
= node
->GetNext();
1291 void wxWindowBase::ResetConstraints()
1293 wxLayoutConstraints
*constr
= GetConstraints();
1296 constr
->left
.SetDone(FALSE
);
1297 constr
->top
.SetDone(FALSE
);
1298 constr
->right
.SetDone(FALSE
);
1299 constr
->bottom
.SetDone(FALSE
);
1300 constr
->width
.SetDone(FALSE
);
1301 constr
->height
.SetDone(FALSE
);
1302 constr
->centreX
.SetDone(FALSE
);
1303 constr
->centreY
.SetDone(FALSE
);
1306 wxWindowList::Node
*node
= GetChildren().GetFirst();
1309 wxWindow
*win
= node
->GetData();
1310 if ( !win
->IsTopLevel() )
1311 win
->ResetConstraints();
1312 node
= node
->GetNext();
1316 // Need to distinguish between setting the 'fake' size for windows and sizers,
1317 // and setting the real values.
1318 void wxWindowBase::SetConstraintSizes(bool recurse
)
1320 wxLayoutConstraints
*constr
= GetConstraints();
1321 if ( constr
&& constr
->AreSatisfied() )
1323 int x
= constr
->left
.GetValue();
1324 int y
= constr
->top
.GetValue();
1325 int w
= constr
->width
.GetValue();
1326 int h
= constr
->height
.GetValue();
1328 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1329 (constr
->height
.GetRelationship() != wxAsIs
) )
1331 SetSize(x
, y
, w
, h
);
1335 // If we don't want to resize this window, just move it...
1341 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1342 GetClassInfo()->GetClassName(),
1348 wxWindowList::Node
*node
= GetChildren().GetFirst();
1351 wxWindow
*win
= node
->GetData();
1352 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1353 win
->SetConstraintSizes();
1354 node
= node
->GetNext();
1359 // Only set the size/position of the constraint (if any)
1360 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1362 wxLayoutConstraints
*constr
= GetConstraints();
1367 constr
->left
.SetValue(x
);
1368 constr
->left
.SetDone(TRUE
);
1372 constr
->top
.SetValue(y
);
1373 constr
->top
.SetDone(TRUE
);
1377 constr
->width
.SetValue(w
);
1378 constr
->width
.SetDone(TRUE
);
1382 constr
->height
.SetValue(h
);
1383 constr
->height
.SetDone(TRUE
);
1388 void wxWindowBase::MoveConstraint(int x
, int y
)
1390 wxLayoutConstraints
*constr
= GetConstraints();
1395 constr
->left
.SetValue(x
);
1396 constr
->left
.SetDone(TRUE
);
1400 constr
->top
.SetValue(y
);
1401 constr
->top
.SetDone(TRUE
);
1406 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1408 wxLayoutConstraints
*constr
= GetConstraints();
1411 *w
= constr
->width
.GetValue();
1412 *h
= constr
->height
.GetValue();
1418 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1420 wxLayoutConstraints
*constr
= GetConstraints();
1423 *w
= constr
->width
.GetValue();
1424 *h
= constr
->height
.GetValue();
1427 GetClientSize(w
, h
);
1430 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1432 // don't do it for the dialogs/frames - they float independently of their
1434 if ( !IsTopLevel() )
1436 wxWindow
*parent
= GetParent();
1437 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1439 wxPoint
pt(parent
->GetClientAreaOrigin());
1447 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1449 wxLayoutConstraints
*constr
= GetConstraints();
1452 *x
= constr
->left
.GetValue();
1453 *y
= constr
->top
.GetValue();
1459 #endif // wxUSE_CONSTRAINTS
1461 // ----------------------------------------------------------------------------
1462 // do Update UI processing for child controls
1463 // ----------------------------------------------------------------------------
1465 // TODO: should this be implemented for the child window rather
1466 // than the parent? Then you can override it e.g. for wxCheckBox
1467 // to do the Right Thing rather than having to assume a fixed number
1468 // of control classes.
1469 void wxWindowBase::UpdateWindowUI()
1472 wxUpdateUIEvent
event(GetId());
1473 event
.m_eventObject
= this;
1475 if ( GetEventHandler()->ProcessEvent(event
) )
1477 if ( event
.GetSetEnabled() )
1478 Enable(event
.GetEnabled());
1480 if ( event
.GetSetText() )
1482 wxControl
*control
= wxDynamicCastThis(wxControl
);
1486 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1488 text
->SetValue(event
.GetText());
1490 #endif // wxUSE_TEXTCTRL
1491 control
->SetLabel(event
.GetText());
1496 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1499 if ( event
.GetSetChecked() )
1500 checkbox
->SetValue(event
.GetChecked());
1502 #endif // wxUSE_CHECKBOX
1505 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1508 if ( event
.GetSetChecked() )
1509 radiobtn
->SetValue(event
.GetChecked());
1511 #endif // wxUSE_RADIOBTN
1513 #endif // wxUSE_CONTROLS
1516 // ----------------------------------------------------------------------------
1517 // dialog units translations
1518 // ----------------------------------------------------------------------------
1520 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1522 int charWidth
= GetCharWidth();
1523 int charHeight
= GetCharHeight();
1524 wxPoint
pt2(-1, -1);
1526 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1528 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1533 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1535 int charWidth
= GetCharWidth();
1536 int charHeight
= GetCharHeight();
1537 wxPoint
pt2(-1, -1);
1539 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1541 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1546 // ----------------------------------------------------------------------------
1548 // ----------------------------------------------------------------------------
1550 // propagate the colour change event to the subwindows
1551 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1553 wxWindowList::Node
*node
= GetChildren().GetFirst();
1556 // Only propagate to non-top-level windows
1557 wxWindow
*win
= node
->GetData();
1558 if ( !win
->IsTopLevel() )
1560 wxSysColourChangedEvent event2
;
1561 event
.m_eventObject
= win
;
1562 win
->GetEventHandler()->ProcessEvent(event2
);
1565 node
= node
->GetNext();
1569 // the default action is to populate dialog with data when it's created
1570 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1572 TransferDataToWindow();
1575 // process Ctrl-Alt-mclick
1576 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1579 if ( event
.ControlDown() && event
.AltDown() )
1581 // don't translate these strings
1584 #ifdef __WXUNIVERSAL__
1586 #endif // __WXUNIVERSAL__
1588 switch ( wxGetOsVersion() )
1590 case wxMOTIF_X
: port
= _T("Motif"); break;
1592 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1593 case wxBEOS
: port
= _T("BeOS"); break;
1597 case wxGTK_BEOS
: port
= _T("GTK"); break;
1603 case wxWIN386
: port
= _T("MS Windows"); break;
1607 case wxMGL_OS2
: port
= _T("MGL"); break;
1609 case wxOS2_PM
: port
= _T("OS/2"); break;
1610 default: port
= _T("unknown"); break;
1613 wxMessageBox(wxString::Format(
1615 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1629 _T("wxWindows information"),
1630 wxICON_INFORMATION
| wxOK
,
1634 #endif // wxUSE_MSGDLG
1640 // ----------------------------------------------------------------------------
1641 // list classes implementation
1642 // ----------------------------------------------------------------------------
1644 void wxWindowListNode::DeleteData()
1646 delete (wxWindow
*)GetData();
1649 // ----------------------------------------------------------------------------
1651 // ----------------------------------------------------------------------------
1653 wxBorder
wxWindowBase::GetBorder() const
1655 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1656 if ( border
== wxBORDER_DEFAULT
)
1658 border
= GetDefaultBorder();
1664 wxBorder
wxWindowBase::GetDefaultBorder() const
1666 return wxBORDER_NONE
;
1669 // ----------------------------------------------------------------------------
1671 // ----------------------------------------------------------------------------
1673 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1675 // here we just check if the point is inside the window or not
1677 // check the top and left border first
1678 bool outside
= x
< 0 || y
< 0;
1681 // check the right and bottom borders too
1682 wxSize size
= GetSize();
1683 outside
= x
>= size
.x
|| y
>= size
.y
;
1686 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1689 // ----------------------------------------------------------------------------
1691 // ----------------------------------------------------------------------------
1693 struct WXDLLEXPORT wxWindowNext
1697 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1699 void wxWindowBase::CaptureMouse()
1701 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1703 wxWindow
*winOld
= GetCapture();
1707 wxWindowNext
*item
= new wxWindowNext
;
1709 item
->next
= ms_winCaptureNext
;
1710 ms_winCaptureNext
= item
;
1712 //else: no mouse capture to save
1717 void wxWindowBase::ReleaseMouse()
1719 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1721 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1725 if ( ms_winCaptureNext
)
1727 ms_winCaptureNext
->win
->CaptureMouse();
1729 wxWindowNext
*item
= ms_winCaptureNext
;
1730 ms_winCaptureNext
= item
->next
;
1733 //else: stack is empty, no previous capture
1735 wxLogTrace(_T("mousecapture"),
1736 _T("After ReleaseMouse() mouse is captured by 0x%08x"),