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/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/statusbr.h"
48 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
52 #if wxUSE_DRAG_AND_DROP
54 #endif // wxUSE_DRAG_AND_DROP
57 #include "wx/cshelp.h"
61 #include "wx/tooltip.h"
62 #endif // wxUSE_TOOLTIPS
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 int wxWindowBase::ms_lastControlId
= -200;
74 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
81 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
82 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
83 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
86 EVT_HELP(-1, wxWindowBase::OnHelp
)
91 // ============================================================================
92 // implementation of the common functionality of the wxWindow class
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // the default initialization
100 void wxWindowBase::InitBase()
102 // no window yet, no parent nor children
103 m_parent
= (wxWindow
*)NULL
;
105 m_children
.DeleteContents( FALSE
); // don't auto delete node data
107 // no constraints on the minimal window size
113 // window is created enabled but it's not visible yet
117 // no client data (yet)
119 m_clientDataType
= ClientData_None
;
121 // the default event handler is just this window
122 m_eventHandler
= this;
126 m_windowValidator
= (wxValidator
*) NULL
;
127 #endif // wxUSE_VALIDATORS
129 // use the system default colours
130 wxSystemSettings settings
;
132 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
133 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
134 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
136 // GRG, changed Mar/2000
137 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
142 // an optimization for the event processing: checking this flag is much
143 // faster than using IsKindOf(CLASSINFO(wxWindow))
146 #if wxUSE_CONSTRAINTS
147 // no constraints whatsoever
148 m_constraints
= (wxLayoutConstraints
*) NULL
;
149 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
150 m_windowSizer
= (wxSizer
*) NULL
;
151 m_autoLayout
= FALSE
;
152 #endif // wxUSE_CONSTRAINTS
154 #if wxUSE_DRAG_AND_DROP
155 m_dropTarget
= (wxDropTarget
*)NULL
;
156 #endif // wxUSE_DRAG_AND_DROP
159 m_tooltip
= (wxToolTip
*)NULL
;
160 #endif // wxUSE_TOOLTIPS
163 m_caret
= (wxCaret
*)NULL
;
164 #endif // wxUSE_CARET
167 // common part of window creation process
168 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
170 const wxPoint
& WXUNUSED(pos
),
171 const wxSize
& WXUNUSED(size
),
173 const wxValidator
& validator
,
174 const wxString
& name
)
176 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
177 // member variables - check that it has been called (will catch the case
178 // when a new ctor is added which doesn't call InitWindow)
179 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
181 // generate a new id if the user doesn't care about it
182 m_windowId
= id
== -1 ? NewControlId() : id
;
185 SetWindowStyleFlag(style
);
189 SetValidator(validator
);
190 #endif // wxUSE_VALIDATORS
192 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
193 // have it too - like this it's possible to set it only in the top level
194 // dialog/frame and all children will inherit it by defult
195 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
197 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
208 wxWindowBase::~wxWindowBase()
210 // FIXME if these 2 cases result from programming errors in the user code
211 // we should probably assert here instead of silently fixing them
213 // Just in case the window has been Closed, but we're then deleting
214 // immediately: don't leave dangling pointers.
215 wxPendingDelete
.DeleteObject(this);
217 // Just in case we've loaded a top-level window via LoadNativeDialog but
218 // we weren't a dialog class
219 wxTopLevelWindows
.DeleteObject(this);
221 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
223 // make sure that there are no dangling pointers left pointing to us
224 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
227 if ( panel
->GetLastFocus() == this )
229 panel
->SetLastFocus((wxWindow
*)NULL
);
236 #endif // wxUSE_CARET
239 if ( m_windowValidator
)
240 delete m_windowValidator
;
241 #endif // wxUSE_VALIDATORS
243 // we only delete object data, not untyped
244 if ( m_clientDataType
== ClientData_Object
)
245 delete m_clientObject
;
247 #if wxUSE_CONSTRAINTS
248 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
249 // at deleted windows as they delete themselves.
250 DeleteRelatedConstraints();
254 // This removes any dangling pointers to this window in other windows'
255 // constraintsInvolvedIn lists.
256 UnsetConstraints(m_constraints
);
257 delete m_constraints
;
258 m_constraints
= NULL
;
262 delete m_windowSizer
;
264 #endif // wxUSE_CONSTRAINTS
266 #if wxUSE_DRAG_AND_DROP
269 #endif // wxUSE_DRAG_AND_DROP
274 #endif // wxUSE_TOOLTIPS
277 bool wxWindowBase::Destroy()
284 bool wxWindowBase::Close(bool force
)
286 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
287 event
.SetEventObject(this);
288 #if WXWIN_COMPATIBILITY
289 event
.SetForce(force
);
290 #endif // WXWIN_COMPATIBILITY
291 event
.SetCanVeto(!force
);
293 // return FALSE if window wasn't closed because the application vetoed the
295 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
298 bool wxWindowBase::DestroyChildren()
300 wxWindowList::Node
*node
;
303 // we iterate until the list becomes empty
304 node
= GetChildren().GetFirst();
308 wxWindow
*child
= node
->GetData();
310 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
314 wxASSERT_MSG( !GetChildren().Find(child
),
315 wxT("child didn't remove itself using RemoveChild()") );
321 // ----------------------------------------------------------------------------
322 // size/position related methods
323 // ----------------------------------------------------------------------------
325 // centre the window with respect to its parent in either (or both) directions
326 void wxWindowBase::Centre(int direction
)
328 // the position/size of the parent window or of the entire screen
330 int widthParent
, heightParent
;
332 wxWindow
*parent
= NULL
;
334 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
336 // find the parent to centre this window on: it should be the
337 // immediate parent for the controls but the top level parent for the
338 // top level windows (like dialogs)
339 parent
= GetParent();
342 while ( parent
&& !parent
->IsTopLevel() )
344 parent
= parent
->GetParent();
348 // did we find the parent?
352 direction
|= wxCENTRE_ON_SCREEN
;
356 if ( direction
& wxCENTRE_ON_SCREEN
)
358 // centre with respect to the whole screen
359 wxDisplaySize(&widthParent
, &heightParent
);
365 // centre on the parent
366 parent
->GetSize(&widthParent
, &heightParent
);
368 // adjust to the parents position
369 posParent
= parent
->GetPosition();
373 // centre inside the parents client rectangle
374 parent
->GetClientSize(&widthParent
, &heightParent
);
379 GetSize(&width
, &height
);
384 if ( direction
& wxHORIZONTAL
)
385 xNew
= (widthParent
- width
)/2;
387 if ( direction
& wxVERTICAL
)
388 yNew
= (heightParent
- height
)/2;
393 // move the centre of this window to this position
397 // fits the window around the children
398 void wxWindowBase::Fit()
400 if ( GetChildren().GetCount() > 0 )
402 SetClientSize(DoGetBestSize());
404 //else: do nothing if we have no children
407 // return the size best suited for the current window
408 wxSize
wxWindowBase::DoGetBestSize() const
410 if ( GetChildren().GetCount() > 0 )
412 // our minimal acceptable size is such that all our windows fit inside
416 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
418 node
= node
->GetNext() )
420 wxWindow
*win
= node
->GetData();
421 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) )
423 // dialogs and frames lie in different top level windows -
424 // don't deal with them here; as for the status bars, they
425 // don't lie in the client area at all
430 win
->GetPosition(&wx
, &wy
);
432 // if the window hadn't been positioned yet, assume that it is in
439 win
->GetSize(&ww
, &wh
);
440 if ( wx
+ ww
> maxX
)
442 if ( wy
+ wh
> maxY
)
447 return wxSize(maxX
+ 7, maxY
+ 14);
451 // for a generic window there is no natural best size - just use the
457 // set the min/max size of the window
458 void wxWindowBase::SetSizeHints(int minW
, int minH
,
460 int WXUNUSED(incW
), int WXUNUSED(incH
))
468 // ----------------------------------------------------------------------------
469 // show/hide/enable/disable the window
470 // ----------------------------------------------------------------------------
472 bool wxWindowBase::Show(bool show
)
474 if ( show
!= m_isShown
)
486 bool wxWindowBase::Enable(bool enable
)
488 if ( enable
!= m_isEnabled
)
490 m_isEnabled
= enable
;
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 bool wxWindowBase::IsTopLevel() const
508 // ----------------------------------------------------------------------------
509 // reparenting the window
510 // ----------------------------------------------------------------------------
512 void wxWindowBase::AddChild(wxWindowBase
*child
)
514 wxCHECK_RET( child
, wxT("can't add a NULL child") );
516 // this should never happen and it will lead to a crash later if it does
517 // because RemoveChild() will remove only one node from the children list
518 // and the other(s) one(s) will be left with dangling pointers in them
519 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
521 GetChildren().Append(child
);
522 child
->SetParent(this);
525 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
527 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
529 GetChildren().DeleteObject(child
);
530 child
->SetParent((wxWindow
*)NULL
);
533 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
535 wxWindow
*oldParent
= GetParent();
536 if ( newParent
== oldParent
)
542 // unlink this window from the existing parent.
545 oldParent
->RemoveChild(this);
549 wxTopLevelWindows
.DeleteObject(this);
552 // add it to the new one
555 newParent
->AddChild(this);
559 wxTopLevelWindows
.Append(this);
565 // ----------------------------------------------------------------------------
566 // event handler stuff
567 // ----------------------------------------------------------------------------
569 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
571 handler
->SetNextHandler(GetEventHandler());
572 SetEventHandler(handler
);
575 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
577 wxEvtHandler
*handlerA
= GetEventHandler();
580 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
581 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
582 SetEventHandler(handlerB
);
586 handlerA
= (wxEvtHandler
*)NULL
;
593 // ----------------------------------------------------------------------------
595 // ----------------------------------------------------------------------------
597 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
599 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
602 m_backgroundColour
= colour
;
607 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
609 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
612 m_foregroundColour
= colour
;
617 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
619 // setting an invalid cursor is ok, it means that we don't have any special
621 if ( m_cursor
== cursor
)
632 bool wxWindowBase::SetFont(const wxFont
& font
)
634 // don't try to set invalid font, always fall back to the default
635 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
637 if ( (wxFont
&)fontOk
== m_font
)
649 void wxWindowBase::SetCaret(wxCaret
*caret
)
660 wxASSERT_MSG( m_caret
->GetWindow() == this,
661 wxT("caret should be created associated to this window") );
664 #endif // wxUSE_CARET
667 // ----------------------------------------------------------------------------
669 // ----------------------------------------------------------------------------
671 void wxWindowBase::SetValidator(const wxValidator
& validator
)
673 if ( m_windowValidator
)
674 delete m_windowValidator
;
676 m_windowValidator
= (wxValidator
*)validator
.Clone();
678 if ( m_windowValidator
)
679 m_windowValidator
->SetWindow(this) ;
681 #endif // wxUSE_VALIDATORS
683 // ----------------------------------------------------------------------------
684 // update region testing
685 // ----------------------------------------------------------------------------
687 bool wxWindowBase::IsExposed(int x
, int y
) const
689 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
692 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
694 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
697 // ----------------------------------------------------------------------------
698 // find window by id or name
699 // ----------------------------------------------------------------------------
701 wxWindow
*wxWindowBase::FindWindow( long id
)
703 if ( id
== m_windowId
)
704 return (wxWindow
*)this;
706 wxWindowBase
*res
= (wxWindow
*)NULL
;
707 wxWindowList::Node
*node
;
708 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
710 wxWindowBase
*child
= node
->GetData();
711 res
= child
->FindWindow( id
);
714 return (wxWindow
*)res
;
717 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
719 if ( name
== m_windowName
)
720 return (wxWindow
*)this;
722 wxWindowBase
*res
= (wxWindow
*)NULL
;
723 wxWindowList::Node
*node
;
724 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
726 wxWindow
*child
= node
->GetData();
727 res
= child
->FindWindow(name
);
730 return (wxWindow
*)res
;
733 // ----------------------------------------------------------------------------
734 // dialog oriented functions
735 // ----------------------------------------------------------------------------
737 void wxWindowBase::MakeModal(bool modal
)
739 // Disable all other windows
742 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
745 wxWindow
*win
= node
->GetData();
749 node
= node
->GetNext();
754 bool wxWindowBase::Validate()
757 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
759 wxWindowList::Node
*node
;
760 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
762 wxWindowBase
*child
= node
->GetData();
763 wxValidator
*validator
= child
->GetValidator();
764 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
769 if ( recurse
&& !child
->Validate() )
774 #endif // wxUSE_VALIDATORS
779 bool wxWindowBase::TransferDataToWindow()
782 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
784 wxWindowList::Node
*node
;
785 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
787 wxWindowBase
*child
= node
->GetData();
788 wxValidator
*validator
= child
->GetValidator();
789 if ( validator
&& !validator
->TransferToWindow() )
791 wxLogWarning(_("Could not transfer data to window"));
792 wxLog::FlushActive();
799 if ( !child
->TransferDataToWindow() )
801 // warning already given
806 #endif // wxUSE_VALIDATORS
811 bool wxWindowBase::TransferDataFromWindow()
814 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
816 wxWindowList::Node
*node
;
817 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
819 wxWindow
*child
= node
->GetData();
820 wxValidator
*validator
= child
->GetValidator();
821 if ( validator
&& !validator
->TransferFromWindow() )
823 // nop warning here because the application is supposed to give
824 // one itself - we don't know here what might have gone wrongly
831 if ( !child
->TransferDataFromWindow() )
833 // warning already given
838 #endif // wxUSE_VALIDATORS
843 void wxWindowBase::InitDialog()
845 wxInitDialogEvent
event(GetId());
846 event
.SetEventObject( this );
847 GetEventHandler()->ProcessEvent(event
);
850 // ----------------------------------------------------------------------------
851 // context-sensitive help support
852 // ----------------------------------------------------------------------------
856 // associate this help text with this window
857 void wxWindowBase::SetHelpText(const wxString
& text
)
859 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
862 helpProvider
->AddHelp(this, text
);
866 // associate this help text with all windows with the same id as this
868 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
870 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
873 helpProvider
->AddHelp(GetId(), text
);
877 // get the help string associated with this window (may be empty)
878 wxString
wxWindowBase::GetHelpText() const
881 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
884 text
= helpProvider
->GetHelp(this);
890 // show help for this window
891 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
893 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
896 if ( helpProvider
->ShowHelp(this) )
898 // skip the event.Skip() below
908 // ----------------------------------------------------------------------------
910 // ----------------------------------------------------------------------------
914 void wxWindowBase::SetToolTip( const wxString
&tip
)
916 // don't create the new tooltip if we already have one
919 m_tooltip
->SetTip( tip
);
923 SetToolTip( new wxToolTip( tip
) );
926 // setting empty tooltip text does not remove the tooltip any more - use
927 // SetToolTip((wxToolTip *)NULL) for this
930 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
938 #endif // wxUSE_TOOLTIPS
940 // ----------------------------------------------------------------------------
941 // constraints and sizers
942 // ----------------------------------------------------------------------------
944 #if wxUSE_CONSTRAINTS
946 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
950 UnsetConstraints(m_constraints
);
951 delete m_constraints
;
953 m_constraints
= constraints
;
956 // Make sure other windows know they're part of a 'meaningful relationship'
957 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
958 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
959 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
960 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
961 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
962 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
963 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
964 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
965 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
966 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
967 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
968 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
969 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
970 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
971 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
972 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
976 // This removes any dangling pointers to this window in other windows'
977 // constraintsInvolvedIn lists.
978 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
982 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
983 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
984 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
985 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
986 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
987 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
988 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
989 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
990 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
991 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
992 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
993 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
994 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
995 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
996 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
997 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1001 // Back-pointer to other windows we're involved with, so if we delete this
1002 // window, we must delete any constraints we're involved with.
1003 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1005 if ( !m_constraintsInvolvedIn
)
1006 m_constraintsInvolvedIn
= new wxWindowList
;
1007 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1008 m_constraintsInvolvedIn
->Append(otherWin
);
1011 // REMOVE back-pointer to other windows we're involved with.
1012 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1014 if ( m_constraintsInvolvedIn
)
1015 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1018 // Reset any constraints that mention this window
1019 void wxWindowBase::DeleteRelatedConstraints()
1021 if ( m_constraintsInvolvedIn
)
1023 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1026 wxWindow
*win
= node
->GetData();
1027 wxLayoutConstraints
*constr
= win
->GetConstraints();
1029 // Reset any constraints involving this window
1032 constr
->left
.ResetIfWin(this);
1033 constr
->top
.ResetIfWin(this);
1034 constr
->right
.ResetIfWin(this);
1035 constr
->bottom
.ResetIfWin(this);
1036 constr
->width
.ResetIfWin(this);
1037 constr
->height
.ResetIfWin(this);
1038 constr
->centreX
.ResetIfWin(this);
1039 constr
->centreY
.ResetIfWin(this);
1042 wxWindowList::Node
*next
= node
->GetNext();
1047 delete m_constraintsInvolvedIn
;
1048 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1052 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1054 if (m_windowSizer
) delete m_windowSizer
;
1056 m_windowSizer
= sizer
;
1059 bool wxWindowBase::Layout()
1061 // If there is a sizer, use it instead of the constraints
1065 GetClientSize(&w
, &h
);
1067 GetSizer()->SetDimension( 0, 0, w
, h
);
1071 wxLayoutConstraints
*constr
= GetConstraints();
1072 bool wasOk
= constr
&& constr
->AreSatisfied();
1074 ResetConstraints(); // Mark all constraints as unevaluated
1076 // if we're a top level panel (i.e. our parent is frame/dialog), our
1077 // own constraints will never be satisfied any more unless we do it
1082 while ( noChanges
> 0 )
1084 constr
->SatisfyConstraints(this, &noChanges
);
1088 DoPhase(1); // Layout children
1089 DoPhase(2); // Layout grand children
1090 SetConstraintSizes(); // Recursively set the real window sizes
1097 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1098 // do a similar thing, but also impose their own 'constraints' and order the
1099 // evaluation differently.
1100 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1102 wxLayoutConstraints
*constr
= GetConstraints();
1105 return constr
->SatisfyConstraints(this, noChanges
);
1111 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1121 // Do a phase of evaluating child constraints
1122 bool wxWindowBase::DoPhase(int phase
)
1124 int noIterations
= 0;
1125 int maxIterations
= 500;
1128 wxWindowList succeeded
;
1129 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1133 wxWindowList::Node
*node
= GetChildren().GetFirst();
1136 wxWindow
*child
= node
->GetData();
1137 if ( !child
->IsTopLevel() )
1139 wxLayoutConstraints
*constr
= child
->GetConstraints();
1142 if ( !succeeded
.Find(child
) )
1144 int tempNoChanges
= 0;
1145 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1146 noChanges
+= tempNoChanges
;
1149 succeeded
.Append(child
);
1154 node
= node
->GetNext();
1163 void wxWindowBase::ResetConstraints()
1165 wxLayoutConstraints
*constr
= GetConstraints();
1168 constr
->left
.SetDone(FALSE
);
1169 constr
->top
.SetDone(FALSE
);
1170 constr
->right
.SetDone(FALSE
);
1171 constr
->bottom
.SetDone(FALSE
);
1172 constr
->width
.SetDone(FALSE
);
1173 constr
->height
.SetDone(FALSE
);
1174 constr
->centreX
.SetDone(FALSE
);
1175 constr
->centreY
.SetDone(FALSE
);
1178 wxWindowList::Node
*node
= GetChildren().GetFirst();
1181 wxWindow
*win
= node
->GetData();
1182 if ( !win
->IsTopLevel() )
1183 win
->ResetConstraints();
1184 node
= node
->GetNext();
1188 // Need to distinguish between setting the 'fake' size for windows and sizers,
1189 // and setting the real values.
1190 void wxWindowBase::SetConstraintSizes(bool recurse
)
1192 wxLayoutConstraints
*constr
= GetConstraints();
1193 if ( constr
&& constr
->AreSatisfied() )
1195 int x
= constr
->left
.GetValue();
1196 int y
= constr
->top
.GetValue();
1197 int w
= constr
->width
.GetValue();
1198 int h
= constr
->height
.GetValue();
1200 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1201 (constr
->height
.GetRelationship() != wxAsIs
) )
1203 SetSize(x
, y
, w
, h
);
1207 // If we don't want to resize this window, just move it...
1213 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1214 GetClassInfo()->GetClassName(),
1220 wxWindowList::Node
*node
= GetChildren().GetFirst();
1223 wxWindow
*win
= node
->GetData();
1224 if ( !win
->IsTopLevel() )
1225 win
->SetConstraintSizes();
1226 node
= node
->GetNext();
1231 // Only set the size/position of the constraint (if any)
1232 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1234 wxLayoutConstraints
*constr
= GetConstraints();
1239 constr
->left
.SetValue(x
);
1240 constr
->left
.SetDone(TRUE
);
1244 constr
->top
.SetValue(y
);
1245 constr
->top
.SetDone(TRUE
);
1249 constr
->width
.SetValue(w
);
1250 constr
->width
.SetDone(TRUE
);
1254 constr
->height
.SetValue(h
);
1255 constr
->height
.SetDone(TRUE
);
1260 void wxWindowBase::MoveConstraint(int x
, int y
)
1262 wxLayoutConstraints
*constr
= GetConstraints();
1267 constr
->left
.SetValue(x
);
1268 constr
->left
.SetDone(TRUE
);
1272 constr
->top
.SetValue(y
);
1273 constr
->top
.SetDone(TRUE
);
1278 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1280 wxLayoutConstraints
*constr
= GetConstraints();
1283 *w
= constr
->width
.GetValue();
1284 *h
= constr
->height
.GetValue();
1290 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1292 wxLayoutConstraints
*constr
= GetConstraints();
1295 *w
= constr
->width
.GetValue();
1296 *h
= constr
->height
.GetValue();
1299 GetClientSize(w
, h
);
1302 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1304 wxLayoutConstraints
*constr
= GetConstraints();
1307 *x
= constr
->left
.GetValue();
1308 *y
= constr
->top
.GetValue();
1314 #endif // wxUSE_CONSTRAINTS
1316 // ----------------------------------------------------------------------------
1317 // do Update UI processing for child controls
1318 // ----------------------------------------------------------------------------
1320 // TODO: should this be implemented for the child window rather
1321 // than the parent? Then you can override it e.g. for wxCheckBox
1322 // to do the Right Thing rather than having to assume a fixed number
1323 // of control classes.
1324 void wxWindowBase::UpdateWindowUI()
1326 wxUpdateUIEvent
event(GetId());
1327 event
.m_eventObject
= this;
1329 if ( GetEventHandler()->ProcessEvent(event
) )
1331 if ( event
.GetSetEnabled() )
1332 Enable(event
.GetEnabled());
1334 if ( event
.GetSetText() )
1336 wxControl
*control
= wxDynamicCast(this, wxControl
);
1339 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1341 text
->SetValue(event
.GetText());
1343 control
->SetLabel(event
.GetText());
1348 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1351 if ( event
.GetSetChecked() )
1352 checkbox
->SetValue(event
.GetChecked());
1354 #endif // wxUSE_CHECKBOX
1357 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1360 if ( event
.GetSetChecked() )
1361 radiobtn
->SetValue(event
.GetChecked());
1363 #endif // wxUSE_RADIOBTN
1367 // ----------------------------------------------------------------------------
1368 // dialog units translations
1369 // ----------------------------------------------------------------------------
1371 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1373 int charWidth
= GetCharWidth();
1374 int charHeight
= GetCharHeight();
1375 wxPoint
pt2(-1, -1);
1377 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1379 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1384 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1386 int charWidth
= GetCharWidth();
1387 int charHeight
= GetCharHeight();
1388 wxPoint
pt2(-1, -1);
1390 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1392 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1397 // ----------------------------------------------------------------------------
1399 // ----------------------------------------------------------------------------
1401 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1403 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1404 wxT("can't have both object and void client data") );
1406 if ( m_clientObject
)
1407 delete m_clientObject
;
1409 m_clientObject
= data
;
1410 m_clientDataType
= ClientData_Object
;
1413 wxClientData
*wxWindowBase::DoGetClientObject() const
1415 // it's not an error to call GetClientObject() on a window which doesn't
1416 // have client data at all - NULL will be returned
1417 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1418 wxT("this window doesn't have object client data") );
1420 return m_clientObject
;
1423 void wxWindowBase::DoSetClientData( void *data
)
1425 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1426 wxT("can't have both object and void client data") );
1428 m_clientData
= data
;
1429 m_clientDataType
= ClientData_Void
;
1432 void *wxWindowBase::DoGetClientData() const
1434 // it's not an error to call GetClientData() on a window which doesn't have
1435 // client data at all - NULL will be returned
1436 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1437 wxT("this window doesn't have void client data") );
1439 return m_clientData
;
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 // propagate the colour change event to the subwindows
1447 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1449 wxWindowList::Node
*node
= GetChildren().GetFirst();
1452 // Only propagate to non-top-level windows
1453 wxWindow
*win
= node
->GetData();
1454 if ( !win
->IsTopLevel() )
1456 wxSysColourChangedEvent event2
;
1457 event
.m_eventObject
= win
;
1458 win
->GetEventHandler()->ProcessEvent(event2
);
1461 node
= node
->GetNext();
1465 // the default action is to populate dialog with data when it's created
1466 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1468 TransferDataToWindow();
1471 // process Ctrl-Alt-mclick
1472 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1474 if ( event
.ControlDown() && event
.AltDown() )
1476 // don't translate these strings
1478 switch ( wxGetOsVersion() )
1480 case wxMOTIF_X
: port
= _T("Motif"); break;
1481 case wxMACINTOSH
: port
= _T("Mac"); break;
1482 case wxBEOS
: port
= _T("BeOS"); break;
1486 case wxGTK_BEOS
: port
= _T("GTK"); break;
1492 case wxWIN386
: port
= _T("MS Windows"); break;
1496 case wxMGL_OS2
: port
= _T("MGL"); break;
1498 case wxOS2_PM
: port
= _T("OS/2"); break;
1499 default: port
= _T("unknown"); break;
1502 wxMessageBox(wxString::Format(
1504 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1513 _T("wxWindows information"),
1514 wxICON_INFORMATION
| wxOK
,
1523 // ----------------------------------------------------------------------------
1524 // list classes implementation
1525 // ----------------------------------------------------------------------------
1527 void wxWindowListNode::DeleteData()
1529 delete (wxWindow
*)GetData();