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/tooltip.h"
58 #endif // wxUSE_TOOLTIPS
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 int wxWindowBase::ms_lastControlId
= -200;
70 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
77 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
78 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
79 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
82 // ============================================================================
83 // implementation of the common functionality of the wxWindow class
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // the default initialization
91 void wxWindowBase::InitBase()
93 // no window yet, no parent nor children
94 m_parent
= (wxWindow
*)NULL
;
96 m_children
.DeleteContents( FALSE
); // don't auto delete node data
98 // no constraints on the minimal window size
104 // window is created enabled but it's not visible yet
108 // no client data (yet)
110 m_clientDataType
= ClientData_None
;
112 // the default event handler is just this window
113 m_eventHandler
= this;
117 m_windowValidator
= (wxValidator
*) NULL
;
118 #endif // wxUSE_VALIDATORS
120 // use the system default colours
121 wxSystemSettings settings
;
123 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
124 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
125 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
127 // GRG, changed Mar/2000
128 #if defined(__VISAGECPP__) && __IBMCPP__ < 0x400
129 // For now VisualAge 3.0 needs it this way
130 m_font
= *wxSWISS_FONT
; // and this?
132 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
138 // an optimization for the event processing: checking this flag is much
139 // faster than using IsKindOf(CLASSINFO(wxWindow))
142 #if wxUSE_CONSTRAINTS
143 // no constraints whatsoever
144 m_constraints
= (wxLayoutConstraints
*) NULL
;
145 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
146 m_windowSizer
= (wxSizer
*) NULL
;
147 m_autoLayout
= FALSE
;
148 #endif // wxUSE_CONSTRAINTS
150 #if wxUSE_DRAG_AND_DROP
151 m_dropTarget
= (wxDropTarget
*)NULL
;
152 #endif // wxUSE_DRAG_AND_DROP
155 m_tooltip
= (wxToolTip
*)NULL
;
156 #endif // wxUSE_TOOLTIPS
159 m_caret
= (wxCaret
*)NULL
;
160 #endif // wxUSE_CARET
163 // common part of window creation process
164 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
166 const wxPoint
& WXUNUSED(pos
),
167 const wxSize
& WXUNUSED(size
),
169 const wxValidator
& validator
,
170 const wxString
& name
)
172 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
173 // member variables - check that it has been called (will catch the case
174 // when a new ctor is added which doesn't call InitWindow)
175 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
177 // generate a new id if the user doesn't care about it
178 m_windowId
= id
== -1 ? NewControlId() : id
;
181 SetWindowStyleFlag(style
);
185 SetValidator(validator
);
186 #endif // wxUSE_VALIDATORS
188 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
189 // have it too - like this it's possible to set it only in the top level
190 // dialog/frame and all children will inherit it by defult
191 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
193 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
204 wxWindowBase::~wxWindowBase()
206 // FIXME if these 2 cases result from programming errors in the user code
207 // we should probably assert here instead of silently fixing them
209 // Just in case the window has been Closed, but we're then deleting
210 // immediately: don't leave dangling pointers.
211 wxPendingDelete
.DeleteObject(this);
213 // Just in case we've loaded a top-level window via LoadNativeDialog but
214 // we weren't a dialog class
215 wxTopLevelWindows
.DeleteObject(this);
217 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
219 // make sure that there are no dangling pointers left pointing to us
220 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
223 if ( panel
->GetLastFocus() == this )
225 panel
->SetLastFocus((wxWindow
*)NULL
);
232 #endif // wxUSE_CARET
235 if ( m_windowValidator
)
236 delete m_windowValidator
;
237 #endif // wxUSE_VALIDATORS
239 // we only delete object data, not untyped
240 if ( m_clientDataType
== ClientData_Object
)
241 delete m_clientObject
;
243 #if wxUSE_CONSTRAINTS
244 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
245 // at deleted windows as they delete themselves.
246 DeleteRelatedConstraints();
250 // This removes any dangling pointers to this window in other windows'
251 // constraintsInvolvedIn lists.
252 UnsetConstraints(m_constraints
);
253 delete m_constraints
;
254 m_constraints
= NULL
;
258 delete m_windowSizer
;
260 #endif // wxUSE_CONSTRAINTS
262 #if wxUSE_DRAG_AND_DROP
265 #endif // wxUSE_DRAG_AND_DROP
270 #endif // wxUSE_TOOLTIPS
273 bool wxWindowBase::Destroy()
280 bool wxWindowBase::Close(bool force
)
282 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
283 event
.SetEventObject(this);
284 #if WXWIN_COMPATIBILITY
285 event
.SetForce(force
);
286 #endif // WXWIN_COMPATIBILITY
287 event
.SetCanVeto(!force
);
289 // return FALSE if window wasn't closed because the application vetoed the
291 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
294 bool wxWindowBase::DestroyChildren()
296 wxWindowList::Node
*node
;
299 // we iterate until the list becomes empty
300 node
= GetChildren().GetFirst();
304 wxWindow
*child
= node
->GetData();
306 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
310 wxASSERT_MSG( !GetChildren().Find(child
),
311 wxT("child didn't remove itself using RemoveChild()") );
317 // ----------------------------------------------------------------------------
318 // size/position related methods
319 // ----------------------------------------------------------------------------
321 // centre the window with respect to its parent in either (or both) directions
322 void wxWindowBase::Centre(int direction
)
324 int widthParent
, heightParent
;
326 wxWindow
*parent
= GetParent();
330 direction
|= wxCENTRE_ON_SCREEN
;
333 if ( direction
& wxCENTRE_ON_SCREEN
)
335 // centre with respect to the whole screen
336 wxDisplaySize(&widthParent
, &heightParent
);
340 // centre inside the parents rectangle
341 parent
->GetClientSize(&widthParent
, &heightParent
);
345 GetSize(&width
, &height
);
350 if ( direction
& wxHORIZONTAL
)
351 xNew
= (widthParent
- width
)/2;
353 if ( direction
& wxVERTICAL
)
354 yNew
= (heightParent
- height
)/2;
356 // controls are always centered on their parent because it doesn't make
357 // sense to centre them on the screen
358 if ( !(direction
& wxCENTRE_ON_SCREEN
) || !IsTopLevel() )
360 // the only chance to get this is to have a not top level window
361 // without parent which shouldn't happen
362 wxCHECK_RET( parent
, wxT("this window must have a parent") );
364 // adjust to the parents client area origin
365 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
371 // move the centre of this window to this position
375 // fits the window around the children
376 void wxWindowBase::Fit()
378 if ( GetChildren().GetCount() > 0 )
380 SetClientSize(DoGetBestSize());
382 //else: do nothing if we have no children
385 // return the size best suited for the current window
386 wxSize
wxWindowBase::DoGetBestSize() const
388 if ( GetChildren().GetCount() > 0 )
390 // our minimal acceptable size is such that all our windows fit inside
394 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
396 node
= node
->GetNext() )
398 wxWindow
*win
= node
->GetData();
399 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) )
401 // dialogs and frames lie in different top level windows -
402 // don't deal with them here; as for the status bars, they
403 // don't lie in the client area at all
408 win
->GetPosition(&wx
, &wy
);
410 // if the window hadn't been positioned yet, assume that it is in
417 win
->GetSize(&ww
, &wh
);
418 if ( wx
+ ww
> maxX
)
420 if ( wy
+ wh
> maxY
)
425 return wxSize(maxX
+ 7, maxY
+ 14);
429 // for a generic window there is no natural best size - just use the
435 // set the min/max size of the window
436 void wxWindowBase::SetSizeHints(int minW
, int minH
,
438 int WXUNUSED(incW
), int WXUNUSED(incH
))
446 // ----------------------------------------------------------------------------
447 // show/hide/enable/disable the window
448 // ----------------------------------------------------------------------------
450 bool wxWindowBase::Show(bool show
)
452 if ( show
!= m_isShown
)
464 bool wxWindowBase::Enable(bool enable
)
466 if ( enable
!= m_isEnabled
)
468 m_isEnabled
= enable
;
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 bool wxWindowBase::IsTopLevel() const
486 // ----------------------------------------------------------------------------
487 // reparenting the window
488 // ----------------------------------------------------------------------------
490 void wxWindowBase::AddChild(wxWindowBase
*child
)
492 wxCHECK_RET( child
, wxT("can't add a NULL child") );
494 GetChildren().Append(child
);
495 child
->SetParent(this);
498 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
500 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
502 GetChildren().DeleteObject(child
);
503 child
->SetParent((wxWindow
*)NULL
);
506 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
508 wxWindow
*oldParent
= GetParent();
509 if ( newParent
== oldParent
)
515 // unlink this window from the existing parent.
518 oldParent
->RemoveChild(this);
522 wxTopLevelWindows
.DeleteObject(this);
525 // add it to the new one
528 newParent
->AddChild(this);
532 wxTopLevelWindows
.Append(this);
538 // ----------------------------------------------------------------------------
539 // event handler stuff
540 // ----------------------------------------------------------------------------
542 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
544 handler
->SetNextHandler(GetEventHandler());
545 SetEventHandler(handler
);
548 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
550 wxEvtHandler
*handlerA
= GetEventHandler();
553 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
554 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
555 SetEventHandler(handlerB
);
559 handlerA
= (wxEvtHandler
*)NULL
;
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
572 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
575 m_backgroundColour
= colour
;
580 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
582 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
585 m_foregroundColour
= colour
;
590 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
592 // setting an invalid cursor is ok, it means that we don't have any special
594 if ( m_cursor
== cursor
)
605 bool wxWindowBase::SetFont(const wxFont
& font
)
607 // don't try to set invalid font, always fall back to the default
608 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
610 if ( (wxFont
&)fontOk
== m_font
)
622 void wxWindowBase::SetCaret(wxCaret
*caret
)
633 wxASSERT_MSG( m_caret
->GetWindow() == this,
634 wxT("caret should be created associated to this window") );
637 #endif // wxUSE_CARET
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 void wxWindowBase::SetValidator(const wxValidator
& validator
)
646 if ( m_windowValidator
)
647 delete m_windowValidator
;
649 m_windowValidator
= (wxValidator
*)validator
.Clone();
651 if ( m_windowValidator
)
652 m_windowValidator
->SetWindow(this) ;
654 #endif // wxUSE_VALIDATORS
656 // ----------------------------------------------------------------------------
657 // update region testing
658 // ----------------------------------------------------------------------------
660 bool wxWindowBase::IsExposed(int x
, int y
) const
662 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
665 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
667 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
670 // ----------------------------------------------------------------------------
671 // find window by id or name
672 // ----------------------------------------------------------------------------
674 wxWindow
*wxWindowBase::FindWindow( long id
)
676 if ( id
== m_windowId
)
677 return (wxWindow
*)this;
679 wxWindowBase
*res
= (wxWindow
*)NULL
;
680 wxWindowList::Node
*node
;
681 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
683 wxWindowBase
*child
= node
->GetData();
684 res
= child
->FindWindow( id
);
687 return (wxWindow
*)res
;
690 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
692 if ( name
== m_windowName
)
693 return (wxWindow
*)this;
695 wxWindowBase
*res
= (wxWindow
*)NULL
;
696 wxWindowList::Node
*node
;
697 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
699 wxWindow
*child
= node
->GetData();
700 res
= child
->FindWindow(name
);
703 return (wxWindow
*)res
;
706 // ----------------------------------------------------------------------------
707 // dialog oriented functions
708 // ----------------------------------------------------------------------------
710 void wxWindowBase::MakeModal(bool modal
)
712 // Disable all other windows
715 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
718 wxWindow
*win
= node
->GetData();
722 node
= node
->GetNext();
727 bool wxWindowBase::Validate()
730 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
732 wxWindowList::Node
*node
;
733 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
735 wxWindowBase
*child
= node
->GetData();
736 wxValidator
*validator
= child
->GetValidator();
737 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
742 if ( recurse
&& !child
->Validate() )
747 #endif // wxUSE_VALIDATORS
752 bool wxWindowBase::TransferDataToWindow()
755 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
757 wxWindowList::Node
*node
;
758 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
760 wxWindowBase
*child
= node
->GetData();
761 wxValidator
*validator
= child
->GetValidator();
762 if ( validator
&& !validator
->TransferToWindow() )
764 wxLogWarning(_("Could not transfer data to window"));
765 wxLog::FlushActive();
772 if ( !child
->TransferDataToWindow() )
774 // warning already given
779 #endif // wxUSE_VALIDATORS
784 bool wxWindowBase::TransferDataFromWindow()
787 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
789 wxWindowList::Node
*node
;
790 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
792 wxWindow
*child
= node
->GetData();
793 wxValidator
*validator
= child
->GetValidator();
794 if ( validator
&& !validator
->TransferFromWindow() )
796 // nop warning here because the application is supposed to give
797 // one itself - we don't know here what might have gone wrongly
804 if ( !child
->TransferDataFromWindow() )
806 // warning already given
811 #endif // wxUSE_VALIDATORS
816 void wxWindowBase::InitDialog()
818 wxInitDialogEvent
event(GetId());
819 event
.SetEventObject( this );
820 GetEventHandler()->ProcessEvent(event
);
823 // ----------------------------------------------------------------------------
825 // ----------------------------------------------------------------------------
829 void wxWindowBase::SetToolTip( const wxString
&tip
)
831 // don't create the new tooltip if we already have one
834 m_tooltip
->SetTip( tip
);
838 SetToolTip( new wxToolTip( tip
) );
841 // setting empty tooltip text does not remove the tooltip any more - use
842 // SetToolTip((wxToolTip *)NULL) for this
845 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
853 #endif // wxUSE_TOOLTIPS
855 // ----------------------------------------------------------------------------
856 // constraints and sizers
857 // ----------------------------------------------------------------------------
859 #if wxUSE_CONSTRAINTS
861 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
865 UnsetConstraints(m_constraints
);
866 delete m_constraints
;
868 m_constraints
= constraints
;
871 // Make sure other windows know they're part of a 'meaningful relationship'
872 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
873 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
874 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
875 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
876 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
877 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
878 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
879 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
880 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
881 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
882 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
883 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
884 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
885 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
886 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
887 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
891 // This removes any dangling pointers to this window in other windows'
892 // constraintsInvolvedIn lists.
893 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
897 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
898 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
899 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
900 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
901 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
902 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
903 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
904 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
905 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
906 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
907 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
908 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
909 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
910 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
911 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
912 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
916 // Back-pointer to other windows we're involved with, so if we delete this
917 // window, we must delete any constraints we're involved with.
918 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
920 if ( !m_constraintsInvolvedIn
)
921 m_constraintsInvolvedIn
= new wxWindowList
;
922 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
923 m_constraintsInvolvedIn
->Append(otherWin
);
926 // REMOVE back-pointer to other windows we're involved with.
927 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
929 if ( m_constraintsInvolvedIn
)
930 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
933 // Reset any constraints that mention this window
934 void wxWindowBase::DeleteRelatedConstraints()
936 if ( m_constraintsInvolvedIn
)
938 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
941 wxWindow
*win
= node
->GetData();
942 wxLayoutConstraints
*constr
= win
->GetConstraints();
944 // Reset any constraints involving this window
947 constr
->left
.ResetIfWin(this);
948 constr
->top
.ResetIfWin(this);
949 constr
->right
.ResetIfWin(this);
950 constr
->bottom
.ResetIfWin(this);
951 constr
->width
.ResetIfWin(this);
952 constr
->height
.ResetIfWin(this);
953 constr
->centreX
.ResetIfWin(this);
954 constr
->centreY
.ResetIfWin(this);
957 wxWindowList::Node
*next
= node
->GetNext();
962 delete m_constraintsInvolvedIn
;
963 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
967 void wxWindowBase::SetSizer(wxSizer
*sizer
)
969 if (m_windowSizer
) delete m_windowSizer
;
971 m_windowSizer
= sizer
;
974 bool wxWindowBase::Layout()
976 // If there is a sizer, use it instead of the constraints
980 GetClientSize(&w
, &h
);
982 GetSizer()->SetDimension( 0, 0, w
, h
);
986 wxLayoutConstraints
*constr
= GetConstraints();
987 bool wasOk
= constr
&& constr
->AreSatisfied();
989 ResetConstraints(); // Mark all constraints as unevaluated
991 // if we're a top level panel (i.e. our parent is frame/dialog), our
992 // own constraints will never be satisfied any more unless we do it
997 while ( noChanges
> 0 )
999 constr
->SatisfyConstraints(this, &noChanges
);
1003 DoPhase(1); // Layout children
1004 DoPhase(2); // Layout grand children
1005 SetConstraintSizes(); // Recursively set the real window sizes
1012 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1013 // do a similar thing, but also impose their own 'constraints' and order the
1014 // evaluation differently.
1015 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1017 wxLayoutConstraints
*constr
= GetConstraints();
1020 return constr
->SatisfyConstraints(this, noChanges
);
1026 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1036 // Do a phase of evaluating child constraints
1037 bool wxWindowBase::DoPhase(int phase
)
1039 int noIterations
= 0;
1040 int maxIterations
= 500;
1043 wxWindowList succeeded
;
1044 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1048 wxWindowList::Node
*node
= GetChildren().GetFirst();
1051 wxWindow
*child
= node
->GetData();
1052 if ( !child
->IsTopLevel() )
1054 wxLayoutConstraints
*constr
= child
->GetConstraints();
1057 if ( !succeeded
.Find(child
) )
1059 int tempNoChanges
= 0;
1060 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1061 noChanges
+= tempNoChanges
;
1064 succeeded
.Append(child
);
1069 node
= node
->GetNext();
1078 void wxWindowBase::ResetConstraints()
1080 wxLayoutConstraints
*constr
= GetConstraints();
1083 constr
->left
.SetDone(FALSE
);
1084 constr
->top
.SetDone(FALSE
);
1085 constr
->right
.SetDone(FALSE
);
1086 constr
->bottom
.SetDone(FALSE
);
1087 constr
->width
.SetDone(FALSE
);
1088 constr
->height
.SetDone(FALSE
);
1089 constr
->centreX
.SetDone(FALSE
);
1090 constr
->centreY
.SetDone(FALSE
);
1093 wxWindowList::Node
*node
= GetChildren().GetFirst();
1096 wxWindow
*win
= node
->GetData();
1097 if ( !win
->IsTopLevel() )
1098 win
->ResetConstraints();
1099 node
= node
->GetNext();
1103 // Need to distinguish between setting the 'fake' size for windows and sizers,
1104 // and setting the real values.
1105 void wxWindowBase::SetConstraintSizes(bool recurse
)
1107 wxLayoutConstraints
*constr
= GetConstraints();
1108 if ( constr
&& constr
->AreSatisfied() )
1110 int x
= constr
->left
.GetValue();
1111 int y
= constr
->top
.GetValue();
1112 int w
= constr
->width
.GetValue();
1113 int h
= constr
->height
.GetValue();
1115 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1116 (constr
->height
.GetRelationship() != wxAsIs
) )
1118 SetSize(x
, y
, w
, h
);
1122 // If we don't want to resize this window, just move it...
1128 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1129 GetClassInfo()->GetClassName(),
1135 wxWindowList::Node
*node
= GetChildren().GetFirst();
1138 wxWindow
*win
= node
->GetData();
1139 if ( !win
->IsTopLevel() )
1140 win
->SetConstraintSizes();
1141 node
= node
->GetNext();
1146 // Only set the size/position of the constraint (if any)
1147 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1149 wxLayoutConstraints
*constr
= GetConstraints();
1154 constr
->left
.SetValue(x
);
1155 constr
->left
.SetDone(TRUE
);
1159 constr
->top
.SetValue(y
);
1160 constr
->top
.SetDone(TRUE
);
1164 constr
->width
.SetValue(w
);
1165 constr
->width
.SetDone(TRUE
);
1169 constr
->height
.SetValue(h
);
1170 constr
->height
.SetDone(TRUE
);
1175 void wxWindowBase::MoveConstraint(int x
, int y
)
1177 wxLayoutConstraints
*constr
= GetConstraints();
1182 constr
->left
.SetValue(x
);
1183 constr
->left
.SetDone(TRUE
);
1187 constr
->top
.SetValue(y
);
1188 constr
->top
.SetDone(TRUE
);
1193 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1195 wxLayoutConstraints
*constr
= GetConstraints();
1198 *w
= constr
->width
.GetValue();
1199 *h
= constr
->height
.GetValue();
1205 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1207 wxLayoutConstraints
*constr
= GetConstraints();
1210 *w
= constr
->width
.GetValue();
1211 *h
= constr
->height
.GetValue();
1214 GetClientSize(w
, h
);
1217 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1219 wxLayoutConstraints
*constr
= GetConstraints();
1222 *x
= constr
->left
.GetValue();
1223 *y
= constr
->top
.GetValue();
1229 #endif // wxUSE_CONSTRAINTS
1231 // ----------------------------------------------------------------------------
1232 // do Update UI processing for child controls
1233 // ----------------------------------------------------------------------------
1235 // TODO: should this be implemented for the child window rather
1236 // than the parent? Then you can override it e.g. for wxCheckBox
1237 // to do the Right Thing rather than having to assume a fixed number
1238 // of control classes.
1239 void wxWindowBase::UpdateWindowUI()
1241 wxUpdateUIEvent
event(GetId());
1242 event
.m_eventObject
= this;
1244 if ( GetEventHandler()->ProcessEvent(event
) )
1246 if ( event
.GetSetEnabled() )
1247 Enable(event
.GetEnabled());
1249 if ( event
.GetSetText() )
1251 wxControl
*control
= wxDynamicCast(this, wxControl
);
1254 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1256 text
->SetValue(event
.GetText());
1258 control
->SetLabel(event
.GetText());
1263 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1266 if ( event
.GetSetChecked() )
1267 checkbox
->SetValue(event
.GetChecked());
1269 #endif // wxUSE_CHECKBOX
1272 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1275 if ( event
.GetSetChecked() )
1276 radiobtn
->SetValue(event
.GetChecked());
1278 #endif // wxUSE_RADIOBTN
1282 // ----------------------------------------------------------------------------
1283 // dialog units translations
1284 // ----------------------------------------------------------------------------
1286 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1288 int charWidth
= GetCharWidth();
1289 int charHeight
= GetCharHeight();
1290 wxPoint
pt2(-1, -1);
1292 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1294 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1299 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1301 int charWidth
= GetCharWidth();
1302 int charHeight
= GetCharHeight();
1303 wxPoint
pt2(-1, -1);
1305 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1307 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1312 // ----------------------------------------------------------------------------
1314 // ----------------------------------------------------------------------------
1316 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1318 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1319 wxT("can't have both object and void client data") );
1321 if ( m_clientObject
)
1322 delete m_clientObject
;
1324 m_clientObject
= data
;
1325 m_clientDataType
= ClientData_Object
;
1328 wxClientData
*wxWindowBase::DoGetClientObject() const
1330 // it's not an error to call GetClientObject() on a window which doesn't
1331 // have client data at all - NULL will be returned
1332 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1333 wxT("this window doesn't have object client data") );
1335 return m_clientObject
;
1338 void wxWindowBase::DoSetClientData( void *data
)
1340 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1341 wxT("can't have both object and void client data") );
1343 m_clientData
= data
;
1344 m_clientDataType
= ClientData_Void
;
1347 void *wxWindowBase::DoGetClientData() const
1349 // it's not an error to call GetClientData() on a window which doesn't have
1350 // client data at all - NULL will be returned
1351 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1352 wxT("this window doesn't have void client data") );
1354 return m_clientData
;
1357 // ----------------------------------------------------------------------------
1359 // ----------------------------------------------------------------------------
1361 // propagate the colour change event to the subwindows
1362 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1364 wxWindowList::Node
*node
= GetChildren().GetFirst();
1367 // Only propagate to non-top-level windows
1368 wxWindow
*win
= node
->GetData();
1369 if ( !win
->IsTopLevel() )
1371 wxSysColourChangedEvent event2
;
1372 event
.m_eventObject
= win
;
1373 win
->GetEventHandler()->ProcessEvent(event2
);
1376 node
= node
->GetNext();
1380 // the default action is to populate dialog with data when it's created
1381 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1383 TransferDataToWindow();
1386 // process Ctrl-Alt-mclick
1387 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1389 if ( event
.ControlDown() && event
.AltDown() )
1391 // don't translate these strings
1393 switch ( wxGetOsVersion() )
1395 case wxMOTIF_X
: port
= _T("Motif"); break;
1396 case wxMACINTOSH
: port
= _T("Mac"); break;
1397 case wxBEOS
: port
= _T("BeOS"); break;
1401 case wxGTK_BEOS
: port
= _T("GTK"); break;
1407 case wxWIN386
: port
= _T("MS Windows"); break;
1411 case wxMGL_OS2
: port
= _T("MGL"); break;
1413 case wxOS2_PM
: port
= _T("OS/2"); break;
1414 default: port
= _T("unknown"); break;
1417 wxMessageBox(wxString::Format(
1419 " wxWindows Library (%s port)\n"
1420 "Version %u.%u.%u, compiled at %s %s\n"
1421 " Copyright (c) 1995-2000 wxWindows team"
1430 _T("wxWindows information"),
1431 wxICON_INFORMATION
| wxOK
,
1440 // ----------------------------------------------------------------------------
1441 // list classes implementation
1442 // ----------------------------------------------------------------------------
1444 void wxWindowListNode::DeleteData()
1446 delete (wxWindow
*)GetData();