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"
47 #include "wx/layout.h"
49 #endif // wxUSE_CONSTRAINTS
51 #if wxUSE_DRAG_AND_DROP
53 #endif // wxUSE_DRAG_AND_DROP
56 #include "wx/tooltip.h"
57 #endif // wxUSE_TOOLTIPS
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 int wxWindowBase::ms_lastControlId
= -200;
69 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
76 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
77 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
78 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
81 // ============================================================================
82 // implementation of the common functionality of the wxWindow class
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the default initialization
90 void wxWindowBase::InitBase()
92 // no window yet, no parent nor children
93 m_parent
= (wxWindow
*)NULL
;
95 m_children
.DeleteContents( FALSE
); // don't auto delete node data
97 // no constraints on the minimal window size
103 // window is created enabled but it's not visible yet
107 // no client data (yet)
109 m_clientDataType
= ClientData_None
;
111 // the default event handler is just this window
112 m_eventHandler
= this;
116 m_windowValidator
= (wxValidator
*) NULL
;
117 #endif // wxUSE_VALIDATORS
119 // use the system default colours
120 wxSystemSettings settings
;
122 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
123 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
124 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
126 #if !defined(__WXMAC__) && !defined(__WXGTK__)
127 m_font
= *wxSWISS_FONT
; // and this?
129 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
136 // an optimization for the event processing: checking this flag is much
137 // faster than using IsKindOf(CLASSINFO(wxWindow))
140 #if wxUSE_CONSTRAINTS
141 // no constraints whatsoever
142 m_constraints
= (wxLayoutConstraints
*) NULL
;
143 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
144 m_windowSizer
= (wxSizer
*) NULL
;
145 m_autoLayout
= FALSE
;
146 #endif // wxUSE_CONSTRAINTS
148 #if wxUSE_DRAG_AND_DROP
149 m_dropTarget
= (wxDropTarget
*)NULL
;
150 #endif // wxUSE_DRAG_AND_DROP
153 m_tooltip
= (wxToolTip
*)NULL
;
154 #endif // wxUSE_TOOLTIPS
157 m_caret
= (wxCaret
*)NULL
;
158 #endif // wxUSE_CARET
161 // common part of window creation process
162 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
164 const wxPoint
& WXUNUSED(pos
),
165 const wxSize
& WXUNUSED(size
),
167 const wxValidator
& validator
,
168 const wxString
& name
)
170 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
171 // member variables - check that it has been called (will catch the case
172 // when a new ctor is added which doesn't call InitWindow)
173 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
175 // generate a new id if the user doesn't care about it
176 m_windowId
= id
== -1 ? NewControlId() : id
;
180 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
181 // have it too - like this it's possible to set it only in the top level
182 // dialog/frame and all children will inherit it by defult
183 if ( parent
&& (parent
->GetWindowStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
185 style
|= wxWS_EX_VALIDATE_RECURSIVELY
;
188 SetWindowStyleFlag(style
);
192 SetValidator(validator
);
193 #endif // wxUSE_VALIDATORS
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
203 wxWindowBase::~wxWindowBase()
205 // FIXME if these 2 cases result from programming errors in the user code
206 // we should probably assert here instead of silently fixing them
208 // Just in case the window has been Closed, but we're then deleting
209 // immediately: don't leave dangling pointers.
210 wxPendingDelete
.DeleteObject(this);
212 // Just in case we've loaded a top-level window via LoadNativeDialog but
213 // we weren't a dialog class
214 wxTopLevelWindows
.DeleteObject(this);
216 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
218 // make sure that there are no dangling pointers left pointing to us
219 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
222 if ( panel
->GetLastFocus() == this )
224 panel
->SetLastFocus((wxWindow
*)NULL
);
231 #endif // wxUSE_CARET
234 if ( m_windowValidator
)
235 delete m_windowValidator
;
236 #endif // wxUSE_VALIDATORS
238 // we only delete object data, not untyped
239 if ( m_clientDataType
== ClientData_Object
)
240 delete m_clientObject
;
242 #if wxUSE_CONSTRAINTS
243 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
244 // at deleted windows as they delete themselves.
245 DeleteRelatedConstraints();
249 // This removes any dangling pointers to this window in other windows'
250 // constraintsInvolvedIn lists.
251 UnsetConstraints(m_constraints
);
252 delete m_constraints
;
253 m_constraints
= NULL
;
257 delete m_windowSizer
;
259 #endif // wxUSE_CONSTRAINTS
261 #if wxUSE_DRAG_AND_DROP
264 #endif // wxUSE_DRAG_AND_DROP
269 #endif // wxUSE_TOOLTIPS
272 bool wxWindowBase::Destroy()
279 bool wxWindowBase::Close(bool force
)
281 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
282 event
.SetEventObject(this);
283 #if WXWIN_COMPATIBILITY
284 event
.SetForce(force
);
285 #endif // WXWIN_COMPATIBILITY
286 event
.SetCanVeto(!force
);
288 // return FALSE if window wasn't closed because the application vetoed the
290 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
293 bool wxWindowBase::DestroyChildren()
295 wxWindowList::Node
*node
;
298 // we iterate until the list becomes empty
299 node
= GetChildren().GetFirst();
303 wxWindow
*child
= node
->GetData();
305 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
309 wxASSERT_MSG( !GetChildren().Find(child
),
310 wxT("child didn't remove itself using RemoveChild()") );
316 // ----------------------------------------------------------------------------
317 // size/position related methods
318 // ----------------------------------------------------------------------------
320 // centre the window with respect to its parent in either (or both) directions
321 void wxWindowBase::Centre(int direction
)
323 int widthParent
, heightParent
;
325 wxWindow
*parent
= GetParent();
329 direction
|= wxCENTRE_ON_SCREEN
;
332 if ( direction
& wxCENTRE_ON_SCREEN
)
334 // centre with respect to the whole screen
335 wxDisplaySize(&widthParent
, &heightParent
);
339 // centre inside the parents rectangle
340 parent
->GetClientSize(&widthParent
, &heightParent
);
344 GetSize(&width
, &height
);
349 if ( direction
& wxHORIZONTAL
)
350 xNew
= (widthParent
- width
)/2;
352 if ( direction
& wxVERTICAL
)
353 yNew
= (heightParent
- height
)/2;
355 // controls are always centered on their parent because it doesn't make
356 // sense to centre them on the screen
357 if ( !(direction
& wxCENTRE_ON_SCREEN
) || wxDynamicCast(this, wxControl
) )
359 // theo nly chance to get this is to have a wxControl without parent
360 wxCHECK_RET( parent
, wxT("a control must have a parent") );
362 // adjust to the parents client area origin
363 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
369 // move the centre of this window to this position
373 // fits the window around the children
374 void wxWindowBase::Fit()
376 if ( GetChildren().GetCount() > 0 )
378 SetClientSize(DoGetBestSize());
380 //else: do nothing if we have no children
383 // return the size best suited for the current window
384 wxSize
wxWindowBase::DoGetBestSize() const
386 if ( GetChildren().GetCount() > 0 )
388 // our minimal acceptable size is such that all our windows fit inside
392 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
394 node
= node
->GetNext() )
396 wxWindow
*win
= node
->GetData();
397 if ( win
->IsTopLevel() )
399 // dialogs and frames lie in different top level windows -
400 // don't deal with them here
405 win
->GetPosition(&wx
, &wy
);
406 win
->GetSize(&ww
, &wh
);
407 if ( wx
+ ww
> maxX
)
409 if ( wy
+ wh
> maxY
)
414 return wxSize(maxX
+ 7, maxY
+ 14);
418 // for a generic window there is no natural best size - just use the
424 // set the min/max size of the window
425 void wxWindowBase::SetSizeHints(int minW
, int minH
,
427 int WXUNUSED(incW
), int WXUNUSED(incH
))
435 // ----------------------------------------------------------------------------
436 // show/hide/enable/disable the window
437 // ----------------------------------------------------------------------------
439 bool wxWindowBase::Show(bool show
)
441 if ( show
!= m_isShown
)
453 bool wxWindowBase::Enable(bool enable
)
455 if ( enable
!= m_isEnabled
)
457 m_isEnabled
= enable
;
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 bool wxWindowBase::IsTopLevel() const
475 // ----------------------------------------------------------------------------
476 // reparenting the window
477 // ----------------------------------------------------------------------------
479 void wxWindowBase::AddChild(wxWindowBase
*child
)
481 wxCHECK_RET( child
, wxT("can't add a NULL child") );
483 GetChildren().Append(child
);
484 child
->SetParent(this);
487 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
489 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
491 GetChildren().DeleteObject(child
);
492 child
->SetParent((wxWindow
*)NULL
);
495 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
497 wxWindow
*oldParent
= GetParent();
498 if ( newParent
== oldParent
)
504 // unlink this window from the existing parent.
507 oldParent
->RemoveChild(this);
511 wxTopLevelWindows
.DeleteObject(this);
514 // add it to the new one
517 newParent
->AddChild(this);
521 wxTopLevelWindows
.Append(this);
527 // ----------------------------------------------------------------------------
528 // event handler stuff
529 // ----------------------------------------------------------------------------
531 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
533 handler
->SetNextHandler(GetEventHandler());
534 SetEventHandler(handler
);
537 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
539 wxEvtHandler
*handlerA
= GetEventHandler();
542 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
543 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
544 SetEventHandler(handlerB
);
548 handlerA
= (wxEvtHandler
*)NULL
;
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
561 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
564 m_backgroundColour
= colour
;
569 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
571 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
574 m_foregroundColour
= colour
;
579 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
581 // don't try to set invalid cursor, always fall back to the default
582 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
584 if ( (wxCursor
&)cursorOk
== m_cursor
)
595 bool wxWindowBase::SetFont(const wxFont
& font
)
597 // don't try to set invalid font, always fall back to the default
598 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
600 if ( (wxFont
&)fontOk
== m_font
)
612 void wxWindowBase::SetCaret(wxCaret
*caret
)
623 wxASSERT_MSG( m_caret
->GetWindow() == this,
624 wxT("caret should be created associated to this window") );
627 #endif // wxUSE_CARET
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 void wxWindowBase::SetValidator(const wxValidator
& validator
)
636 if ( m_windowValidator
)
637 delete m_windowValidator
;
639 m_windowValidator
= (wxValidator
*)validator
.Clone();
641 if ( m_windowValidator
)
642 m_windowValidator
->SetWindow(this) ;
644 #endif // wxUSE_VALIDATORS
646 // ----------------------------------------------------------------------------
647 // update region testing
648 // ----------------------------------------------------------------------------
650 bool wxWindowBase::IsExposed(int x
, int y
) const
652 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
655 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
657 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
660 // ----------------------------------------------------------------------------
661 // find window by id or name
662 // ----------------------------------------------------------------------------
664 wxWindow
*wxWindowBase::FindWindow( long id
)
666 if ( id
== m_windowId
)
667 return (wxWindow
*)this;
669 wxWindowBase
*res
= (wxWindow
*)NULL
;
670 wxWindowList::Node
*node
;
671 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
673 wxWindowBase
*child
= node
->GetData();
674 res
= child
->FindWindow( id
);
677 return (wxWindow
*)res
;
680 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
682 if ( name
== m_windowName
)
683 return (wxWindow
*)this;
685 wxWindowBase
*res
= (wxWindow
*)NULL
;
686 wxWindowList::Node
*node
;
687 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
689 wxWindow
*child
= node
->GetData();
690 res
= child
->FindWindow(name
);
693 return (wxWindow
*)res
;
696 // ----------------------------------------------------------------------------
697 // dialog oriented functions
698 // ----------------------------------------------------------------------------
700 void wxWindowBase::MakeModal(bool modal
)
702 // Disable all other windows
705 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
708 wxWindow
*win
= node
->GetData();
712 node
= node
->GetNext();
717 bool wxWindowBase::Validate()
720 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
722 wxWindowList::Node
*node
;
723 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
725 wxWindowBase
*child
= node
->GetData();
726 wxValidator
*validator
= child
->GetValidator();
727 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
732 if ( recurse
&& !child
->Validate() )
737 #endif // wxUSE_VALIDATORS
742 bool wxWindowBase::TransferDataToWindow()
745 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
747 wxWindowList::Node
*node
;
748 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
750 wxWindowBase
*child
= node
->GetData();
751 wxValidator
*validator
= child
->GetValidator();
752 if ( validator
&& !validator
->TransferToWindow() )
754 wxLogWarning(_("Could not transfer data to window"));
755 wxLog::FlushActive();
762 if ( !child
->TransferDataToWindow() )
764 // warning already given
769 #endif // wxUSE_VALIDATORS
774 bool wxWindowBase::TransferDataFromWindow()
777 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
779 wxWindowList::Node
*node
;
780 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
782 wxWindow
*child
= node
->GetData();
783 wxValidator
*validator
= child
->GetValidator();
784 if ( validator
&& !validator
->TransferFromWindow() )
786 // nop warning here because the application is supposed to give
787 // one itself - we don't know here what might have gone wrongly
794 if ( !child
->TransferDataFromWindow() )
796 // warning already given
801 #endif // wxUSE_VALIDATORS
806 void wxWindowBase::InitDialog()
808 wxInitDialogEvent
event(GetId());
809 event
.SetEventObject( this );
810 GetEventHandler()->ProcessEvent(event
);
813 // ----------------------------------------------------------------------------
815 // ----------------------------------------------------------------------------
819 void wxWindowBase::SetToolTip( const wxString
&tip
)
821 // don't create the new tooltip if we already have one
824 m_tooltip
->SetTip( tip
);
828 SetToolTip( new wxToolTip( tip
) );
831 // setting empty tooltip text does not remove the tooltip any more - use
832 // SetToolTip((wxToolTip *)NULL) for this
835 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
843 #endif // wxUSE_TOOLTIPS
845 // ----------------------------------------------------------------------------
846 // constraints and sizers
847 // ----------------------------------------------------------------------------
849 #if wxUSE_CONSTRAINTS
851 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
855 UnsetConstraints(m_constraints
);
856 delete m_constraints
;
858 m_constraints
= constraints
;
861 // Make sure other windows know they're part of a 'meaningful relationship'
862 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
863 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
864 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
865 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
866 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
867 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
868 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
869 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
870 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
871 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
872 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
873 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
874 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
875 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
876 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
877 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
881 // This removes any dangling pointers to this window in other windows'
882 // constraintsInvolvedIn lists.
883 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
887 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
888 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
889 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
890 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
891 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
892 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
893 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
894 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
895 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
896 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
897 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
898 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
899 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
900 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
901 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
902 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
906 // Back-pointer to other windows we're involved with, so if we delete this
907 // window, we must delete any constraints we're involved with.
908 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
910 if ( !m_constraintsInvolvedIn
)
911 m_constraintsInvolvedIn
= new wxWindowList
;
912 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
913 m_constraintsInvolvedIn
->Append(otherWin
);
916 // REMOVE back-pointer to other windows we're involved with.
917 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
919 if ( m_constraintsInvolvedIn
)
920 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
923 // Reset any constraints that mention this window
924 void wxWindowBase::DeleteRelatedConstraints()
926 if ( m_constraintsInvolvedIn
)
928 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
931 wxWindow
*win
= node
->GetData();
932 wxLayoutConstraints
*constr
= win
->GetConstraints();
934 // Reset any constraints involving this window
937 constr
->left
.ResetIfWin(this);
938 constr
->top
.ResetIfWin(this);
939 constr
->right
.ResetIfWin(this);
940 constr
->bottom
.ResetIfWin(this);
941 constr
->width
.ResetIfWin(this);
942 constr
->height
.ResetIfWin(this);
943 constr
->centreX
.ResetIfWin(this);
944 constr
->centreY
.ResetIfWin(this);
947 wxWindowList::Node
*next
= node
->GetNext();
952 delete m_constraintsInvolvedIn
;
953 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
957 void wxWindowBase::SetSizer(wxSizer
*sizer
)
959 if (m_windowSizer
) delete m_windowSizer
;
961 m_windowSizer
= sizer
;
964 bool wxWindowBase::Layout()
966 // If there is a sizer, use it instead of the constraints
970 GetClientSize(&w
, &h
);
972 GetSizer()->SetDimension( 0, 0, w
, h
);
976 wxLayoutConstraints
*constr
= GetConstraints();
977 bool wasOk
= constr
&& constr
->AreSatisfied();
979 ResetConstraints(); // Mark all constraints as unevaluated
981 // if we're a top level panel (i.e. our parent is frame/dialog), our
982 // own constraints will never be satisfied any more unless we do it
987 while ( noChanges
> 0 )
989 constr
->SatisfyConstraints(this, &noChanges
);
993 DoPhase(1); // Layout children
994 DoPhase(2); // Layout grand children
995 SetConstraintSizes(); // Recursively set the real window sizes
1002 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1003 // do a similar thing, but also impose their own 'constraints' and order the
1004 // evaluation differently.
1005 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1007 wxLayoutConstraints
*constr
= GetConstraints();
1010 return constr
->SatisfyConstraints(this, noChanges
);
1016 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1026 // Do a phase of evaluating child constraints
1027 bool wxWindowBase::DoPhase(int phase
)
1029 int noIterations
= 0;
1030 int maxIterations
= 500;
1033 wxWindowList succeeded
;
1034 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1038 wxWindowList::Node
*node
= GetChildren().GetFirst();
1041 wxWindow
*child
= node
->GetData();
1042 if ( !child
->IsTopLevel() )
1044 wxLayoutConstraints
*constr
= child
->GetConstraints();
1047 if ( !succeeded
.Find(child
) )
1049 int tempNoChanges
= 0;
1050 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1051 noChanges
+= tempNoChanges
;
1054 succeeded
.Append(child
);
1059 node
= node
->GetNext();
1068 void wxWindowBase::ResetConstraints()
1070 wxLayoutConstraints
*constr
= GetConstraints();
1073 constr
->left
.SetDone(FALSE
);
1074 constr
->top
.SetDone(FALSE
);
1075 constr
->right
.SetDone(FALSE
);
1076 constr
->bottom
.SetDone(FALSE
);
1077 constr
->width
.SetDone(FALSE
);
1078 constr
->height
.SetDone(FALSE
);
1079 constr
->centreX
.SetDone(FALSE
);
1080 constr
->centreY
.SetDone(FALSE
);
1083 wxWindowList::Node
*node
= GetChildren().GetFirst();
1086 wxWindow
*win
= node
->GetData();
1087 if ( !win
->IsTopLevel() )
1088 win
->ResetConstraints();
1089 node
= node
->GetNext();
1093 // Need to distinguish between setting the 'fake' size for windows and sizers,
1094 // and setting the real values.
1095 void wxWindowBase::SetConstraintSizes(bool recurse
)
1097 wxLayoutConstraints
*constr
= GetConstraints();
1098 if ( constr
&& constr
->AreSatisfied() )
1100 int x
= constr
->left
.GetValue();
1101 int y
= constr
->top
.GetValue();
1102 int w
= constr
->width
.GetValue();
1103 int h
= constr
->height
.GetValue();
1105 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1106 (constr
->height
.GetRelationship() != wxAsIs
) )
1108 SetSize(x
, y
, w
, h
);
1112 // If we don't want to resize this window, just move it...
1118 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1119 GetClassInfo()->GetClassName(),
1125 wxWindowList::Node
*node
= GetChildren().GetFirst();
1128 wxWindow
*win
= node
->GetData();
1129 if ( !win
->IsTopLevel() )
1130 win
->SetConstraintSizes();
1131 node
= node
->GetNext();
1136 // Only set the size/position of the constraint (if any)
1137 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1139 wxLayoutConstraints
*constr
= GetConstraints();
1144 constr
->left
.SetValue(x
);
1145 constr
->left
.SetDone(TRUE
);
1149 constr
->top
.SetValue(y
);
1150 constr
->top
.SetDone(TRUE
);
1154 constr
->width
.SetValue(w
);
1155 constr
->width
.SetDone(TRUE
);
1159 constr
->height
.SetValue(h
);
1160 constr
->height
.SetDone(TRUE
);
1165 void wxWindowBase::MoveConstraint(int x
, int y
)
1167 wxLayoutConstraints
*constr
= GetConstraints();
1172 constr
->left
.SetValue(x
);
1173 constr
->left
.SetDone(TRUE
);
1177 constr
->top
.SetValue(y
);
1178 constr
->top
.SetDone(TRUE
);
1183 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1185 wxLayoutConstraints
*constr
= GetConstraints();
1188 *w
= constr
->width
.GetValue();
1189 *h
= constr
->height
.GetValue();
1195 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1197 wxLayoutConstraints
*constr
= GetConstraints();
1200 *w
= constr
->width
.GetValue();
1201 *h
= constr
->height
.GetValue();
1204 GetClientSize(w
, h
);
1207 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1209 wxLayoutConstraints
*constr
= GetConstraints();
1212 *x
= constr
->left
.GetValue();
1213 *y
= constr
->top
.GetValue();
1219 #endif // wxUSE_CONSTRAINTS
1221 // ----------------------------------------------------------------------------
1222 // do Update UI processing for child controls
1223 // ----------------------------------------------------------------------------
1225 // TODO: should this be implemented for the child window rather
1226 // than the parent? Then you can override it e.g. for wxCheckBox
1227 // to do the Right Thing rather than having to assume a fixed number
1228 // of control classes.
1229 void wxWindowBase::UpdateWindowUI()
1231 wxUpdateUIEvent
event(GetId());
1232 event
.m_eventObject
= this;
1234 if ( GetEventHandler()->ProcessEvent(event
) )
1236 if ( event
.GetSetEnabled() )
1237 Enable(event
.GetEnabled());
1239 if ( event
.GetSetText() )
1241 wxControl
*control
= wxDynamicCast(this, wxControl
);
1244 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1246 text
->SetValue(event
.GetText());
1248 control
->SetLabel(event
.GetText());
1253 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1256 if ( event
.GetSetChecked() )
1257 checkbox
->SetValue(event
.GetChecked());
1259 #endif // wxUSE_CHECKBOX
1262 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1265 if ( event
.GetSetChecked() )
1266 radiobtn
->SetValue(event
.GetChecked());
1268 #endif // wxUSE_RADIOBTN
1272 // ----------------------------------------------------------------------------
1273 // dialog units translations
1274 // ----------------------------------------------------------------------------
1276 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1278 int charWidth
= GetCharWidth();
1279 int charHeight
= GetCharHeight();
1280 wxPoint
pt2(-1, -1);
1282 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1284 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1289 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1291 int charWidth
= GetCharWidth();
1292 int charHeight
= GetCharHeight();
1293 wxPoint
pt2(-1, -1);
1295 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1297 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1302 // ----------------------------------------------------------------------------
1304 // ----------------------------------------------------------------------------
1306 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1308 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1309 wxT("can't have both object and void client data") );
1311 if ( m_clientObject
)
1312 delete m_clientObject
;
1314 m_clientObject
= data
;
1315 m_clientDataType
= ClientData_Object
;
1318 wxClientData
*wxWindowBase::DoGetClientObject() const
1320 // it's not an error to call GetClientObject() on a window which doesn't
1321 // have client data at all - NULL will be returned
1322 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1323 wxT("this window doesn't have object client data") );
1325 return m_clientObject
;
1328 void wxWindowBase::DoSetClientData( void *data
)
1330 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1331 wxT("can't have both object and void client data") );
1333 m_clientData
= data
;
1334 m_clientDataType
= ClientData_Void
;
1337 void *wxWindowBase::DoGetClientData() const
1339 // it's not an error to call GetClientData() on a window which doesn't have
1340 // client data at all - NULL will be returned
1341 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1342 wxT("this window doesn't have void client data") );
1344 return m_clientData
;
1347 // ----------------------------------------------------------------------------
1349 // ----------------------------------------------------------------------------
1351 // propagate the colour change event to the subwindows
1352 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1354 wxWindowList::Node
*node
= GetChildren().GetFirst();
1357 // Only propagate to non-top-level windows
1358 wxWindow
*win
= node
->GetData();
1359 if ( !win
->IsTopLevel() )
1361 wxSysColourChangedEvent event2
;
1362 event
.m_eventObject
= win
;
1363 win
->GetEventHandler()->ProcessEvent(event2
);
1366 node
= node
->GetNext();
1370 // the default action is to populate dialog with data when it's created
1371 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1373 TransferDataToWindow();
1376 // process Ctrl-Alt-mclick
1377 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1379 if ( event
.ControlDown() && event
.AltDown() )
1381 // don't translate these strings
1383 switch ( wxGetOsVersion() )
1385 case wxMOTIF_X
: port
= _T("Motif"); break;
1386 case wxMACINTOSH
: port
= _T("Mac"); break;
1387 case wxBEOS
: port
= _T("BeOS"); break;
1391 case wxGTK_BEOS
: port
= _T("GTK"); break;
1397 case wxWIN386
: port
= _T("MS Windows"); break;
1401 case wxMGL_OS2
: port
= _T("MGL"); break;
1403 case wxOS2_PM
: port
= _T("OS/2"); break;
1404 default: port
= _T("unknown"); break;
1407 wxMessageBox(wxString::Format(
1409 " wxWindows Library (%s port)\n"
1410 "Version %u.%u.%u, compiled at %s %s\n"
1411 " Copyright (c) 1995-2000 wxWindows team"
1420 _T("wxWindows information"),
1421 wxICON_INFORMATION
| wxOK
,
1430 // ----------------------------------------------------------------------------
1431 // list classes implementation
1432 // ----------------------------------------------------------------------------
1434 void wxWindowListNode::DeleteData()
1436 delete (wxWindow
*)GetData();