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 #ifndef __WXMGL__ // FIXME - temporary dirty hack!!
405 if (posParent
.x
>= 0) // if parent is on the main display
410 else if (xNew
+width
> size
.x
)
411 xNew
= size
.x
-width
-1;
413 #ifndef __WXMGL__ // FIXME - temporary dirty hack!!
414 if (posParent
.y
>= 0) // if parent is on the main display
417 if (yNew
+height
> size
.y
)
418 yNew
= size
.y
-height
-1;
420 // Make certain that the title bar is initially visible
421 // always, even if this would push the bottom of the
422 // dialog of the visible area of the display
427 // move the window to this position (keeping the old size but using
428 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
429 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
432 // fits the window around the children
433 void wxWindowBase::Fit()
435 if ( GetChildren().GetCount() > 0 )
437 wxSize size
= DoGetBestSize();
439 // for compatibility with the old versions and because it really looks
440 // slightly more pretty like this, add a pad
446 //else: do nothing if we have no children
449 // return the size best suited for the current window
450 wxSize
wxWindowBase::DoGetBestSize() const
452 if ( GetChildren().GetCount() > 0 )
454 // our minimal acceptable size is such that all our windows fit inside
458 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
460 node
= node
->GetNext() )
462 wxWindow
*win
= node
->GetData();
463 if ( win
->IsTopLevel()
465 || wxDynamicCast(win
, wxStatusBar
)
466 #endif // wxUSE_STATUSBAR
469 // dialogs and frames lie in different top level windows -
470 // don't deal with them here; as for the status bars, they
471 // don't lie in the client area at all
476 win
->GetPosition(&wx
, &wy
);
478 // if the window hadn't been positioned yet, assume that it is in
485 win
->GetSize(&ww
, &wh
);
486 if ( wx
+ ww
> maxX
)
488 if ( wy
+ wh
> maxY
)
492 return wxSize(maxX
, maxY
);
496 // for a generic window there is no natural best size - just use the
502 // by default the origin is not shifted
503 wxPoint
wxWindowBase::GetClientAreaOrigin() const
505 return wxPoint(0, 0);
508 // set the min/max size of the window
509 void wxWindowBase::SetSizeHints(int minW
, int minH
,
511 int WXUNUSED(incW
), int WXUNUSED(incH
))
519 // ----------------------------------------------------------------------------
520 // show/hide/enable/disable the window
521 // ----------------------------------------------------------------------------
523 bool wxWindowBase::Show(bool show
)
525 if ( show
!= m_isShown
)
537 bool wxWindowBase::Enable(bool enable
)
539 if ( enable
!= m_isEnabled
)
541 m_isEnabled
= enable
;
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 bool wxWindowBase::IsTopLevel() const
559 // ----------------------------------------------------------------------------
560 // reparenting the window
561 // ----------------------------------------------------------------------------
563 void wxWindowBase::AddChild(wxWindowBase
*child
)
565 wxCHECK_RET( child
, wxT("can't add a NULL child") );
567 // this should never happen and it will lead to a crash later if it does
568 // because RemoveChild() will remove only one node from the children list
569 // and the other(s) one(s) will be left with dangling pointers in them
570 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
572 GetChildren().Append(child
);
573 child
->SetParent(this);
576 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
578 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
580 GetChildren().DeleteObject(child
);
581 child
->SetParent((wxWindow
*)NULL
);
584 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
586 wxWindow
*oldParent
= GetParent();
587 if ( newParent
== oldParent
)
593 // unlink this window from the existing parent.
596 oldParent
->RemoveChild(this);
600 wxTopLevelWindows
.DeleteObject(this);
603 // add it to the new one
606 newParent
->AddChild(this);
610 wxTopLevelWindows
.Append(this);
616 // ----------------------------------------------------------------------------
617 // event handler stuff
618 // ----------------------------------------------------------------------------
620 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
622 handler
->SetNextHandler(GetEventHandler());
623 SetEventHandler(handler
);
626 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
628 wxEvtHandler
*handlerA
= GetEventHandler();
631 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
632 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
633 SetEventHandler(handlerB
);
637 handlerA
= (wxEvtHandler
*)NULL
;
644 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
646 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
648 wxEvtHandler
*handlerPrev
= NULL
,
649 *handlerCur
= GetEventHandler();
652 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
654 if ( handlerCur
== handler
)
658 handlerPrev
->SetNextHandler(handlerNext
);
662 SetEventHandler(handlerNext
);
665 handler
->SetNextHandler(NULL
);
670 handlerPrev
= handlerCur
;
671 handlerCur
= handlerNext
;
674 wxFAIL_MSG( _T("where has the event handler gone?") );
679 // ----------------------------------------------------------------------------
681 // ----------------------------------------------------------------------------
683 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
685 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
688 m_backgroundColour
= colour
;
695 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
697 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
700 m_foregroundColour
= colour
;
707 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
709 // setting an invalid cursor is ok, it means that we don't have any special
711 if ( m_cursor
== cursor
)
722 bool wxWindowBase::SetFont(const wxFont
& font
)
724 // don't try to set invalid font, always fall back to the default
725 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
727 if ( fontOk
== m_font
)
742 void wxWindowBase::SetPalette(const wxPalette
& pal
)
744 m_hasCustomPalette
= TRUE
;
747 // VZ: can anyone explain me what do we do here?
748 wxWindowDC
d((wxWindow
*) this);
752 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
754 wxWindow
*win
= (wxWindow
*)this;
755 while ( win
&& !win
->HasCustomPalette() )
757 win
= win
->GetParent();
763 #endif // wxUSE_PALETTE
766 void wxWindowBase::SetCaret(wxCaret
*caret
)
777 wxASSERT_MSG( m_caret
->GetWindow() == this,
778 wxT("caret should be created associated to this window") );
781 #endif // wxUSE_CARET
784 // ----------------------------------------------------------------------------
786 // ----------------------------------------------------------------------------
788 void wxWindowBase::SetValidator(const wxValidator
& validator
)
790 if ( m_windowValidator
)
791 delete m_windowValidator
;
793 m_windowValidator
= (wxValidator
*)validator
.Clone();
795 if ( m_windowValidator
)
796 m_windowValidator
->SetWindow(this) ;
798 #endif // wxUSE_VALIDATORS
800 // ----------------------------------------------------------------------------
801 // update region stuff
802 // ----------------------------------------------------------------------------
804 wxRect
wxWindowBase::GetUpdateClientRect() const
806 wxRegion rgnUpdate
= GetUpdateRegion();
807 rgnUpdate
.Intersect(GetClientRect());
808 wxRect rectUpdate
= rgnUpdate
.GetBox();
809 wxPoint ptOrigin
= GetClientAreaOrigin();
810 rectUpdate
.x
-= ptOrigin
.x
;
811 rectUpdate
.y
-= ptOrigin
.y
;
816 bool wxWindowBase::IsExposed(int x
, int y
) const
818 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
821 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
823 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
826 // ----------------------------------------------------------------------------
827 // find window by id or name
828 // ----------------------------------------------------------------------------
830 wxWindow
*wxWindowBase::FindWindow( long id
)
832 if ( id
== m_windowId
)
833 return (wxWindow
*)this;
835 wxWindowBase
*res
= (wxWindow
*)NULL
;
836 wxWindowList::Node
*node
;
837 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
839 wxWindowBase
*child
= node
->GetData();
840 res
= child
->FindWindow( id
);
843 return (wxWindow
*)res
;
846 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
848 if ( name
== m_windowName
)
849 return (wxWindow
*)this;
851 wxWindowBase
*res
= (wxWindow
*)NULL
;
852 wxWindowList::Node
*node
;
853 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
855 wxWindow
*child
= node
->GetData();
856 res
= child
->FindWindow(name
);
859 return (wxWindow
*)res
;
862 // ----------------------------------------------------------------------------
863 // dialog oriented functions
864 // ----------------------------------------------------------------------------
866 void wxWindowBase::MakeModal(bool modal
)
868 // Disable all other windows
871 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
874 wxWindow
*win
= node
->GetData();
878 node
= node
->GetNext();
883 bool wxWindowBase::Validate()
886 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
888 wxWindowList::Node
*node
;
889 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
891 wxWindowBase
*child
= node
->GetData();
892 wxValidator
*validator
= child
->GetValidator();
893 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
898 if ( recurse
&& !child
->Validate() )
903 #endif // wxUSE_VALIDATORS
908 bool wxWindowBase::TransferDataToWindow()
911 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
913 wxWindowList::Node
*node
;
914 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
916 wxWindowBase
*child
= node
->GetData();
917 wxValidator
*validator
= child
->GetValidator();
918 if ( validator
&& !validator
->TransferToWindow() )
920 wxLogWarning(_("Could not transfer data to window"));
921 wxLog::FlushActive();
928 if ( !child
->TransferDataToWindow() )
930 // warning already given
935 #endif // wxUSE_VALIDATORS
940 bool wxWindowBase::TransferDataFromWindow()
943 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
945 wxWindowList::Node
*node
;
946 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
948 wxWindow
*child
= node
->GetData();
949 wxValidator
*validator
= child
->GetValidator();
950 if ( validator
&& !validator
->TransferFromWindow() )
952 // nop warning here because the application is supposed to give
953 // one itself - we don't know here what might have gone wrongly
960 if ( !child
->TransferDataFromWindow() )
962 // warning already given
967 #endif // wxUSE_VALIDATORS
972 void wxWindowBase::InitDialog()
974 wxInitDialogEvent
event(GetId());
975 event
.SetEventObject( this );
976 GetEventHandler()->ProcessEvent(event
);
979 // ----------------------------------------------------------------------------
980 // context-sensitive help support
981 // ----------------------------------------------------------------------------
985 // associate this help text with this window
986 void wxWindowBase::SetHelpText(const wxString
& text
)
988 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
991 helpProvider
->AddHelp(this, text
);
995 // associate this help text with all windows with the same id as this
997 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
999 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1002 helpProvider
->AddHelp(GetId(), text
);
1006 // get the help string associated with this window (may be empty)
1007 wxString
wxWindowBase::GetHelpText() const
1010 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1013 text
= helpProvider
->GetHelp(this);
1019 // show help for this window
1020 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1022 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1025 if ( helpProvider
->ShowHelp(this) )
1027 // skip the event.Skip() below
1035 #endif // wxUSE_HELP
1037 // ----------------------------------------------------------------------------
1038 // tooltipsroot.Replace("\\", "/");
1039 // ----------------------------------------------------------------------------
1043 void wxWindowBase::SetToolTip( const wxString
&tip
)
1045 // don't create the new tooltip if we already have one
1048 m_tooltip
->SetTip( tip
);
1052 SetToolTip( new wxToolTip( tip
) );
1055 // setting empty tooltip text does not remove the tooltip any more - use
1056 // SetToolTip((wxToolTip *)NULL) for this
1059 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1064 m_tooltip
= tooltip
;
1067 #endif // wxUSE_TOOLTIPS
1069 // ----------------------------------------------------------------------------
1070 // constraints and sizers
1071 // ----------------------------------------------------------------------------
1073 #if wxUSE_CONSTRAINTS
1075 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1077 if ( m_constraints
)
1079 UnsetConstraints(m_constraints
);
1080 delete m_constraints
;
1082 m_constraints
= constraints
;
1083 if ( m_constraints
)
1085 // Make sure other windows know they're part of a 'meaningful relationship'
1086 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1087 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1088 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1089 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1090 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1091 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1092 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1093 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1094 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1095 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1096 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1097 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1098 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1099 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1100 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1101 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1105 // This removes any dangling pointers to this window in other windows'
1106 // constraintsInvolvedIn lists.
1107 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1111 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1112 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1113 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1114 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1115 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1116 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1117 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1118 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1119 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1120 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1121 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1122 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1123 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1124 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1125 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1126 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1130 // Back-pointer to other windows we're involved with, so if we delete this
1131 // window, we must delete any constraints we're involved with.
1132 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1134 if ( !m_constraintsInvolvedIn
)
1135 m_constraintsInvolvedIn
= new wxWindowList
;
1136 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1137 m_constraintsInvolvedIn
->Append(otherWin
);
1140 // REMOVE back-pointer to other windows we're involved with.
1141 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1143 if ( m_constraintsInvolvedIn
)
1144 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1147 // Reset any constraints that mention this window
1148 void wxWindowBase::DeleteRelatedConstraints()
1150 if ( m_constraintsInvolvedIn
)
1152 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1155 wxWindow
*win
= node
->GetData();
1156 wxLayoutConstraints
*constr
= win
->GetConstraints();
1158 // Reset any constraints involving this window
1161 constr
->left
.ResetIfWin(this);
1162 constr
->top
.ResetIfWin(this);
1163 constr
->right
.ResetIfWin(this);
1164 constr
->bottom
.ResetIfWin(this);
1165 constr
->width
.ResetIfWin(this);
1166 constr
->height
.ResetIfWin(this);
1167 constr
->centreX
.ResetIfWin(this);
1168 constr
->centreY
.ResetIfWin(this);
1171 wxWindowList::Node
*next
= node
->GetNext();
1176 delete m_constraintsInvolvedIn
;
1177 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1181 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1183 if (m_windowSizer
) delete m_windowSizer
;
1185 m_windowSizer
= sizer
;
1188 bool wxWindowBase::Layout()
1190 // If there is a sizer, use it instead of the constraints
1194 GetClientSize(&w
, &h
);
1196 GetSizer()->SetDimension( 0, 0, w
, h
);
1200 wxLayoutConstraints
*constr
= GetConstraints();
1201 bool wasOk
= constr
&& constr
->AreSatisfied();
1203 ResetConstraints(); // Mark all constraints as unevaluated
1205 // if we're a top level panel (i.e. our parent is frame/dialog), our
1206 // own constraints will never be satisfied any more unless we do it
1211 while ( noChanges
> 0 )
1213 constr
->SatisfyConstraints(this, &noChanges
);
1217 DoPhase(1); // Layout children
1218 DoPhase(2); // Layout grand children
1219 SetConstraintSizes(); // Recursively set the real window sizes
1226 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1227 // do a similar thing, but also impose their own 'constraints' and order the
1228 // evaluation differently.
1229 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1231 wxLayoutConstraints
*constr
= GetConstraints();
1234 return constr
->SatisfyConstraints(this, noChanges
);
1240 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1250 // Do a phase of evaluating child constraints
1251 bool wxWindowBase::DoPhase(int phase
)
1253 int noIterations
= 0;
1254 int maxIterations
= 500;
1257 wxWindowList succeeded
;
1258 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1262 wxWindowList::Node
*node
= GetChildren().GetFirst();
1265 wxWindow
*child
= node
->GetData();
1266 if ( !child
->IsTopLevel() )
1268 wxLayoutConstraints
*constr
= child
->GetConstraints();
1271 if ( !succeeded
.Find(child
) )
1273 int tempNoChanges
= 0;
1274 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1275 noChanges
+= tempNoChanges
;
1278 succeeded
.Append(child
);
1283 node
= node
->GetNext();
1292 void wxWindowBase::ResetConstraints()
1294 wxLayoutConstraints
*constr
= GetConstraints();
1297 constr
->left
.SetDone(FALSE
);
1298 constr
->top
.SetDone(FALSE
);
1299 constr
->right
.SetDone(FALSE
);
1300 constr
->bottom
.SetDone(FALSE
);
1301 constr
->width
.SetDone(FALSE
);
1302 constr
->height
.SetDone(FALSE
);
1303 constr
->centreX
.SetDone(FALSE
);
1304 constr
->centreY
.SetDone(FALSE
);
1307 wxWindowList::Node
*node
= GetChildren().GetFirst();
1310 wxWindow
*win
= node
->GetData();
1311 if ( !win
->IsTopLevel() )
1312 win
->ResetConstraints();
1313 node
= node
->GetNext();
1317 // Need to distinguish between setting the 'fake' size for windows and sizers,
1318 // and setting the real values.
1319 void wxWindowBase::SetConstraintSizes(bool recurse
)
1321 wxLayoutConstraints
*constr
= GetConstraints();
1322 if ( constr
&& constr
->AreSatisfied() )
1324 int x
= constr
->left
.GetValue();
1325 int y
= constr
->top
.GetValue();
1326 int w
= constr
->width
.GetValue();
1327 int h
= constr
->height
.GetValue();
1329 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1330 (constr
->height
.GetRelationship() != wxAsIs
) )
1332 SetSize(x
, y
, w
, h
);
1336 // If we don't want to resize this window, just move it...
1342 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1343 GetClassInfo()->GetClassName(),
1349 wxWindowList::Node
*node
= GetChildren().GetFirst();
1352 wxWindow
*win
= node
->GetData();
1353 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1354 win
->SetConstraintSizes();
1355 node
= node
->GetNext();
1360 // Only set the size/position of the constraint (if any)
1361 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1363 wxLayoutConstraints
*constr
= GetConstraints();
1368 constr
->left
.SetValue(x
);
1369 constr
->left
.SetDone(TRUE
);
1373 constr
->top
.SetValue(y
);
1374 constr
->top
.SetDone(TRUE
);
1378 constr
->width
.SetValue(w
);
1379 constr
->width
.SetDone(TRUE
);
1383 constr
->height
.SetValue(h
);
1384 constr
->height
.SetDone(TRUE
);
1389 void wxWindowBase::MoveConstraint(int x
, int y
)
1391 wxLayoutConstraints
*constr
= GetConstraints();
1396 constr
->left
.SetValue(x
);
1397 constr
->left
.SetDone(TRUE
);
1401 constr
->top
.SetValue(y
);
1402 constr
->top
.SetDone(TRUE
);
1407 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1409 wxLayoutConstraints
*constr
= GetConstraints();
1412 *w
= constr
->width
.GetValue();
1413 *h
= constr
->height
.GetValue();
1419 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1421 wxLayoutConstraints
*constr
= GetConstraints();
1424 *w
= constr
->width
.GetValue();
1425 *h
= constr
->height
.GetValue();
1428 GetClientSize(w
, h
);
1431 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1433 // don't do it for the dialogs/frames - they float independently of their
1435 if ( !IsTopLevel() )
1437 wxWindow
*parent
= GetParent();
1438 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1440 wxPoint
pt(parent
->GetClientAreaOrigin());
1448 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1450 wxLayoutConstraints
*constr
= GetConstraints();
1453 *x
= constr
->left
.GetValue();
1454 *y
= constr
->top
.GetValue();
1460 #endif // wxUSE_CONSTRAINTS
1462 // ----------------------------------------------------------------------------
1463 // do Update UI processing for child controls
1464 // ----------------------------------------------------------------------------
1466 // TODO: should this be implemented for the child window rather
1467 // than the parent? Then you can override it e.g. for wxCheckBox
1468 // to do the Right Thing rather than having to assume a fixed number
1469 // of control classes.
1470 void wxWindowBase::UpdateWindowUI()
1473 wxUpdateUIEvent
event(GetId());
1474 event
.m_eventObject
= this;
1476 if ( GetEventHandler()->ProcessEvent(event
) )
1478 if ( event
.GetSetEnabled() )
1479 Enable(event
.GetEnabled());
1481 if ( event
.GetSetText() )
1483 wxControl
*control
= wxDynamicCastThis(wxControl
);
1487 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1489 text
->SetValue(event
.GetText());
1491 #endif // wxUSE_TEXTCTRL
1492 control
->SetLabel(event
.GetText());
1497 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1500 if ( event
.GetSetChecked() )
1501 checkbox
->SetValue(event
.GetChecked());
1503 #endif // wxUSE_CHECKBOX
1506 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1509 if ( event
.GetSetChecked() )
1510 radiobtn
->SetValue(event
.GetChecked());
1512 #endif // wxUSE_RADIOBTN
1514 #endif // wxUSE_CONTROLS
1517 // ----------------------------------------------------------------------------
1518 // dialog units translations
1519 // ----------------------------------------------------------------------------
1521 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1523 int charWidth
= GetCharWidth();
1524 int charHeight
= GetCharHeight();
1525 wxPoint
pt2(-1, -1);
1527 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1529 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1534 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1536 int charWidth
= GetCharWidth();
1537 int charHeight
= GetCharHeight();
1538 wxPoint
pt2(-1, -1);
1540 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1542 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1547 // ----------------------------------------------------------------------------
1549 // ----------------------------------------------------------------------------
1551 // propagate the colour change event to the subwindows
1552 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1554 wxWindowList::Node
*node
= GetChildren().GetFirst();
1557 // Only propagate to non-top-level windows
1558 wxWindow
*win
= node
->GetData();
1559 if ( !win
->IsTopLevel() )
1561 wxSysColourChangedEvent event2
;
1562 event
.m_eventObject
= win
;
1563 win
->GetEventHandler()->ProcessEvent(event2
);
1566 node
= node
->GetNext();
1570 // the default action is to populate dialog with data when it's created
1571 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1573 TransferDataToWindow();
1576 // process Ctrl-Alt-mclick
1577 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1580 if ( event
.ControlDown() && event
.AltDown() )
1582 // don't translate these strings
1585 #ifdef __WXUNIVERSAL__
1587 #endif // __WXUNIVERSAL__
1589 switch ( wxGetOsVersion() )
1591 case wxMOTIF_X
: port
= _T("Motif"); break;
1593 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1594 case wxBEOS
: port
= _T("BeOS"); break;
1598 case wxGTK_BEOS
: port
= _T("GTK"); break;
1604 case wxWIN386
: port
= _T("MS Windows"); break;
1608 case wxMGL_OS2
: port
= _T("MGL"); break;
1610 case wxOS2_PM
: port
= _T("OS/2"); break;
1611 default: port
= _T("unknown"); break;
1614 wxMessageBox(wxString::Format(
1616 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1630 _T("wxWindows information"),
1631 wxICON_INFORMATION
| wxOK
,
1635 #endif // wxUSE_MSGDLG
1641 // ----------------------------------------------------------------------------
1642 // list classes implementation
1643 // ----------------------------------------------------------------------------
1645 void wxWindowListNode::DeleteData()
1647 delete (wxWindow
*)GetData();
1650 // ----------------------------------------------------------------------------
1652 // ----------------------------------------------------------------------------
1654 wxBorder
wxWindowBase::GetBorder() const
1656 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1657 if ( border
== wxBORDER_DEFAULT
)
1659 border
= GetDefaultBorder();
1665 wxBorder
wxWindowBase::GetDefaultBorder() const
1667 return wxBORDER_NONE
;
1670 // ----------------------------------------------------------------------------
1672 // ----------------------------------------------------------------------------
1674 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1676 // here we just check if the point is inside the window or not
1678 // check the top and left border first
1679 bool outside
= x
< 0 || y
< 0;
1682 // check the right and bottom borders too
1683 wxSize size
= GetSize();
1684 outside
= x
>= size
.x
|| y
>= size
.y
;
1687 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1690 // ----------------------------------------------------------------------------
1692 // ----------------------------------------------------------------------------
1694 struct WXDLLEXPORT wxWindowNext
1698 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1700 void wxWindowBase::CaptureMouse()
1702 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1704 wxWindow
*winOld
= GetCapture();
1707 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
1709 wxWindowNext
*item
= new wxWindowNext
;
1711 item
->next
= ms_winCaptureNext
;
1712 ms_winCaptureNext
= item
;
1714 //else: no mouse capture to save
1719 void wxWindowBase::ReleaseMouse()
1721 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1723 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1727 if ( ms_winCaptureNext
)
1729 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
1731 wxWindowNext
*item
= ms_winCaptureNext
;
1732 ms_winCaptureNext
= item
->next
;
1735 //else: stack is empty, no previous capture
1737 wxLogTrace(_T("mousecapture"),
1738 _T("After ReleaseMouse() mouse is captured by 0x%08x"),