1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // no client data (yet)
120 m_clientDataType
= wxClientData_None
;
122 // the default event handler is just this window
123 m_eventHandler
= this;
127 m_windowValidator
= (wxValidator
*) NULL
;
128 #endif // wxUSE_VALIDATORS
130 // use the system default colours
131 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
132 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
134 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
135 // so the font is *not* really set - but calls to SetFont() later won't do
136 // anything because m_font appears to be already set!
138 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
145 // an optimization for the event processing: checking this flag is much
146 // faster than using IsKindOf(CLASSINFO(wxWindow))
149 #if wxUSE_CONSTRAINTS
150 // no constraints whatsoever
151 m_constraints
= (wxLayoutConstraints
*) NULL
;
152 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
153 m_windowSizer
= (wxSizer
*) NULL
;
154 m_autoLayout
= FALSE
;
155 #endif // wxUSE_CONSTRAINTS
157 #if wxUSE_DRAG_AND_DROP
158 m_dropTarget
= (wxDropTarget
*)NULL
;
159 #endif // wxUSE_DRAG_AND_DROP
162 m_tooltip
= (wxToolTip
*)NULL
;
163 #endif // wxUSE_TOOLTIPS
166 m_caret
= (wxCaret
*)NULL
;
167 #endif // wxUSE_CARET
169 // Whether we're using the current theme for this window (wxGTK only for now)
170 m_themeEnabled
= FALSE
;
173 // common part of window creation process
174 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
176 const wxPoint
& WXUNUSED(pos
),
177 const wxSize
& WXUNUSED(size
),
179 const wxValidator
& validator
,
180 const wxString
& name
)
182 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
183 // member variables - check that it has been called (will catch the case
184 // when a new ctor is added which doesn't call InitWindow)
185 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
187 // generate a new id if the user doesn't care about it
188 m_windowId
= id
== -1 ? NewControlId() : id
;
191 SetWindowStyleFlag(style
);
195 SetValidator(validator
);
196 #endif // wxUSE_VALIDATORS
198 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
199 // have it too - like this it's possible to set it only in the top level
200 // dialog/frame and all children will inherit it by defult
201 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
203 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 wxWindowBase::~wxWindowBase()
216 // FIXME if these 2 cases result from programming errors in the user code
217 // we should probably assert here instead of silently fixing them
219 // Just in case the window has been Closed, but we're then deleting
220 // immediately: don't leave dangling pointers.
221 wxPendingDelete
.DeleteObject(this);
223 // Just in case we've loaded a top-level window via LoadNativeDialog but
224 // we weren't a dialog class
225 wxTopLevelWindows
.DeleteObject(this);
227 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
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
== wxClientData_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
272 // reset the dangling pointer our parent window may keep to us
273 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
275 m_parent
->SetDefaultItem(NULL
);
279 bool wxWindowBase::Destroy()
286 bool wxWindowBase::Close(bool force
)
288 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
289 event
.SetEventObject(this);
290 #if WXWIN_COMPATIBILITY
291 event
.SetForce(force
);
292 #endif // WXWIN_COMPATIBILITY
293 event
.SetCanVeto(!force
);
295 // return FALSE if window wasn't closed because the application vetoed the
297 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
300 bool wxWindowBase::DestroyChildren()
302 wxWindowList::Node
*node
;
305 // we iterate until the list becomes empty
306 node
= GetChildren().GetFirst();
310 wxWindow
*child
= node
->GetData();
312 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
317 wxASSERT_MSG( !GetChildren().Find(child
),
318 wxT("child didn't remove itself using RemoveChild()") );
324 // ----------------------------------------------------------------------------
325 // size/position related methods
326 // ----------------------------------------------------------------------------
328 // centre the window with respect to its parent in either (or both) directions
329 void wxWindowBase::Centre(int direction
)
331 // the position/size of the parent window or of the entire screen
333 int widthParent
, heightParent
;
335 wxWindow
*parent
= NULL
;
337 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
339 // find the parent to centre this window on: it should be the
340 // immediate parent for the controls but the top level parent for the
341 // top level windows (like dialogs)
342 parent
= GetParent();
345 while ( parent
&& !parent
->IsTopLevel() )
347 parent
= parent
->GetParent();
351 // did we find the parent?
355 direction
|= wxCENTRE_ON_SCREEN
;
359 if ( direction
& wxCENTRE_ON_SCREEN
)
361 // centre with respect to the whole screen
362 wxDisplaySize(&widthParent
, &heightParent
);
368 // centre on the parent
369 parent
->GetSize(&widthParent
, &heightParent
);
371 // adjust to the parents position
372 posParent
= parent
->GetPosition();
376 // centre inside the parents client rectangle
377 parent
->GetClientSize(&widthParent
, &heightParent
);
382 GetSize(&width
, &height
);
387 if ( direction
& wxHORIZONTAL
)
388 xNew
= (widthParent
- width
)/2;
390 if ( direction
& wxVERTICAL
)
391 yNew
= (heightParent
- height
)/2;
396 // Base size of the visible dimensions of the display
397 // to take into account the taskbar
398 wxRect rect
= wxGetClientDisplayRect();
399 wxSize
size (rect
.width
,rect
.height
);
401 if (posParent
.x
>= 0) // if parent is on the main display
405 else if (xNew
+width
> size
.x
)
406 xNew
= size
.x
-width
-1;
408 if (posParent
.y
>= 0) // if parent is on the main display
410 if (yNew
+height
> size
.y
)
411 yNew
= size
.y
-height
-1;
413 // Make certain that the title bar is initially visible
414 // always, even if this would push the bottom of the
415 // dialog of the visible area of the display
420 // move the window to this position (keeping the old size but using
421 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
422 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
425 // fits the window around the children
426 void wxWindowBase::Fit()
428 if ( GetChildren().GetCount() > 0 )
430 wxSize size
= DoGetBestSize();
432 // for compatibility with the old versions and because it really looks
433 // slightly more pretty like this, add a pad
439 //else: do nothing if we have no children
442 // return the size best suited for the current window
443 wxSize
wxWindowBase::DoGetBestSize() const
445 if ( GetChildren().GetCount() > 0 )
447 // our minimal acceptable size is such that all our windows fit inside
451 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
453 node
= node
->GetNext() )
455 wxWindow
*win
= node
->GetData();
456 if ( win
->IsTopLevel()
458 || wxDynamicCast(win
, wxStatusBar
)
459 #endif // wxUSE_STATUSBAR
462 // dialogs and frames lie in different top level windows -
463 // don't deal with them here; as for the status bars, they
464 // don't lie in the client area at all
469 win
->GetPosition(&wx
, &wy
);
471 // if the window hadn't been positioned yet, assume that it is in
478 win
->GetSize(&ww
, &wh
);
479 if ( wx
+ ww
> maxX
)
481 if ( wy
+ wh
> maxY
)
485 return wxSize(maxX
, maxY
);
489 // for a generic window there is no natural best size - just use the
495 // by default the origin is not shifted
496 wxPoint
wxWindowBase::GetClientAreaOrigin() const
498 return wxPoint(0, 0);
501 // set the min/max size of the window
502 void wxWindowBase::SetSizeHints(int minW
, int minH
,
504 int WXUNUSED(incW
), int WXUNUSED(incH
))
512 // ----------------------------------------------------------------------------
513 // show/hide/enable/disable the window
514 // ----------------------------------------------------------------------------
516 bool wxWindowBase::Show(bool show
)
518 if ( show
!= m_isShown
)
530 bool wxWindowBase::Enable(bool enable
)
532 if ( enable
!= m_isEnabled
)
534 m_isEnabled
= enable
;
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 bool wxWindowBase::IsTopLevel() const
552 // ----------------------------------------------------------------------------
553 // reparenting the window
554 // ----------------------------------------------------------------------------
556 void wxWindowBase::AddChild(wxWindowBase
*child
)
558 wxCHECK_RET( child
, wxT("can't add a NULL child") );
560 // this should never happen and it will lead to a crash later if it does
561 // because RemoveChild() will remove only one node from the children list
562 // and the other(s) one(s) will be left with dangling pointers in them
563 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
565 GetChildren().Append(child
);
566 child
->SetParent(this);
569 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
571 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
573 GetChildren().DeleteObject(child
);
574 child
->SetParent((wxWindow
*)NULL
);
577 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
579 wxWindow
*oldParent
= GetParent();
580 if ( newParent
== oldParent
)
586 // unlink this window from the existing parent.
589 oldParent
->RemoveChild(this);
593 wxTopLevelWindows
.DeleteObject(this);
596 // add it to the new one
599 newParent
->AddChild(this);
603 wxTopLevelWindows
.Append(this);
609 // ----------------------------------------------------------------------------
610 // event handler stuff
611 // ----------------------------------------------------------------------------
613 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
615 handler
->SetNextHandler(GetEventHandler());
616 SetEventHandler(handler
);
619 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
621 wxEvtHandler
*handlerA
= GetEventHandler();
624 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
625 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
626 SetEventHandler(handlerB
);
630 handlerA
= (wxEvtHandler
*)NULL
;
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
641 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
643 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
646 m_backgroundColour
= colour
;
651 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
653 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
656 m_foregroundColour
= colour
;
661 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
663 // setting an invalid cursor is ok, it means that we don't have any special
665 if ( m_cursor
== cursor
)
676 bool wxWindowBase::SetFont(const wxFont
& font
)
678 // don't try to set invalid font, always fall back to the default
679 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
681 if ( fontOk
== m_font
)
693 void wxWindowBase::SetCaret(wxCaret
*caret
)
704 wxASSERT_MSG( m_caret
->GetWindow() == this,
705 wxT("caret should be created associated to this window") );
708 #endif // wxUSE_CARET
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
715 void wxWindowBase::SetValidator(const wxValidator
& validator
)
717 if ( m_windowValidator
)
718 delete m_windowValidator
;
720 m_windowValidator
= (wxValidator
*)validator
.Clone();
722 if ( m_windowValidator
)
723 m_windowValidator
->SetWindow(this) ;
725 #endif // wxUSE_VALIDATORS
727 // ----------------------------------------------------------------------------
728 // update region stuff
729 // ----------------------------------------------------------------------------
731 wxRect
wxWindowBase::GetUpdateClientRect() const
733 wxRegion rgnUpdate
= GetUpdateRegion();
734 rgnUpdate
.Intersect(GetClientRect());
735 wxRect rectUpdate
= rgnUpdate
.GetBox();
736 wxPoint ptOrigin
= GetClientAreaOrigin();
737 rectUpdate
.x
-= ptOrigin
.x
;
738 rectUpdate
.y
-= ptOrigin
.y
;
743 bool wxWindowBase::IsExposed(int x
, int y
) const
745 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
748 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
750 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
753 // ----------------------------------------------------------------------------
754 // find window by id or name
755 // ----------------------------------------------------------------------------
757 wxWindow
*wxWindowBase::FindWindow( long id
)
759 if ( id
== m_windowId
)
760 return (wxWindow
*)this;
762 wxWindowBase
*res
= (wxWindow
*)NULL
;
763 wxWindowList::Node
*node
;
764 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
766 wxWindowBase
*child
= node
->GetData();
767 res
= child
->FindWindow( id
);
770 return (wxWindow
*)res
;
773 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
775 if ( name
== m_windowName
)
776 return (wxWindow
*)this;
778 wxWindowBase
*res
= (wxWindow
*)NULL
;
779 wxWindowList::Node
*node
;
780 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
782 wxWindow
*child
= node
->GetData();
783 res
= child
->FindWindow(name
);
786 return (wxWindow
*)res
;
789 // ----------------------------------------------------------------------------
790 // dialog oriented functions
791 // ----------------------------------------------------------------------------
793 void wxWindowBase::MakeModal(bool modal
)
795 // Disable all other windows
798 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
801 wxWindow
*win
= node
->GetData();
805 node
= node
->GetNext();
810 bool wxWindowBase::Validate()
813 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
815 wxWindowList::Node
*node
;
816 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
818 wxWindowBase
*child
= node
->GetData();
819 wxValidator
*validator
= child
->GetValidator();
820 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
825 if ( recurse
&& !child
->Validate() )
830 #endif // wxUSE_VALIDATORS
835 bool wxWindowBase::TransferDataToWindow()
838 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
840 wxWindowList::Node
*node
;
841 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
843 wxWindowBase
*child
= node
->GetData();
844 wxValidator
*validator
= child
->GetValidator();
845 if ( validator
&& !validator
->TransferToWindow() )
847 wxLogWarning(_("Could not transfer data to window"));
848 wxLog::FlushActive();
855 if ( !child
->TransferDataToWindow() )
857 // warning already given
862 #endif // wxUSE_VALIDATORS
867 bool wxWindowBase::TransferDataFromWindow()
870 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
872 wxWindowList::Node
*node
;
873 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
875 wxWindow
*child
= node
->GetData();
876 wxValidator
*validator
= child
->GetValidator();
877 if ( validator
&& !validator
->TransferFromWindow() )
879 // nop warning here because the application is supposed to give
880 // one itself - we don't know here what might have gone wrongly
887 if ( !child
->TransferDataFromWindow() )
889 // warning already given
894 #endif // wxUSE_VALIDATORS
899 void wxWindowBase::InitDialog()
901 wxInitDialogEvent
event(GetId());
902 event
.SetEventObject( this );
903 GetEventHandler()->ProcessEvent(event
);
906 // ----------------------------------------------------------------------------
907 // context-sensitive help support
908 // ----------------------------------------------------------------------------
912 // associate this help text with this window
913 void wxWindowBase::SetHelpText(const wxString
& text
)
915 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
918 helpProvider
->AddHelp(this, text
);
922 // associate this help text with all windows with the same id as this
924 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
926 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
929 helpProvider
->AddHelp(GetId(), text
);
933 // get the help string associated with this window (may be empty)
934 wxString
wxWindowBase::GetHelpText() const
937 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
940 text
= helpProvider
->GetHelp(this);
946 // show help for this window
947 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
949 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
952 if ( helpProvider
->ShowHelp(this) )
954 // skip the event.Skip() below
964 // ----------------------------------------------------------------------------
966 // ----------------------------------------------------------------------------
970 void wxWindowBase::SetToolTip( const wxString
&tip
)
972 // don't create the new tooltip if we already have one
975 m_tooltip
->SetTip( tip
);
979 SetToolTip( new wxToolTip( tip
) );
982 // setting empty tooltip text does not remove the tooltip any more - use
983 // SetToolTip((wxToolTip *)NULL) for this
986 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
994 #endif // wxUSE_TOOLTIPS
996 // ----------------------------------------------------------------------------
997 // constraints and sizers
998 // ----------------------------------------------------------------------------
1000 #if wxUSE_CONSTRAINTS
1002 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1004 if ( m_constraints
)
1006 UnsetConstraints(m_constraints
);
1007 delete m_constraints
;
1009 m_constraints
= constraints
;
1010 if ( m_constraints
)
1012 // Make sure other windows know they're part of a 'meaningful relationship'
1013 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1014 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1015 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1016 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1017 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1018 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1019 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1020 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1021 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1022 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1023 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1024 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1025 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1026 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1027 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1028 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1032 // This removes any dangling pointers to this window in other windows'
1033 // constraintsInvolvedIn lists.
1034 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1038 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1039 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1040 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1041 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1042 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1043 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1044 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1045 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1046 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1047 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1048 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1049 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1050 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1051 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1052 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1053 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1057 // Back-pointer to other windows we're involved with, so if we delete this
1058 // window, we must delete any constraints we're involved with.
1059 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1061 if ( !m_constraintsInvolvedIn
)
1062 m_constraintsInvolvedIn
= new wxWindowList
;
1063 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1064 m_constraintsInvolvedIn
->Append(otherWin
);
1067 // REMOVE back-pointer to other windows we're involved with.
1068 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1070 if ( m_constraintsInvolvedIn
)
1071 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1074 // Reset any constraints that mention this window
1075 void wxWindowBase::DeleteRelatedConstraints()
1077 if ( m_constraintsInvolvedIn
)
1079 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1082 wxWindow
*win
= node
->GetData();
1083 wxLayoutConstraints
*constr
= win
->GetConstraints();
1085 // Reset any constraints involving this window
1088 constr
->left
.ResetIfWin(this);
1089 constr
->top
.ResetIfWin(this);
1090 constr
->right
.ResetIfWin(this);
1091 constr
->bottom
.ResetIfWin(this);
1092 constr
->width
.ResetIfWin(this);
1093 constr
->height
.ResetIfWin(this);
1094 constr
->centreX
.ResetIfWin(this);
1095 constr
->centreY
.ResetIfWin(this);
1098 wxWindowList::Node
*next
= node
->GetNext();
1103 delete m_constraintsInvolvedIn
;
1104 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1108 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1110 if (m_windowSizer
) delete m_windowSizer
;
1112 m_windowSizer
= sizer
;
1115 bool wxWindowBase::Layout()
1117 // If there is a sizer, use it instead of the constraints
1121 GetClientSize(&w
, &h
);
1123 GetSizer()->SetDimension( 0, 0, w
, h
);
1127 wxLayoutConstraints
*constr
= GetConstraints();
1128 bool wasOk
= constr
&& constr
->AreSatisfied();
1130 ResetConstraints(); // Mark all constraints as unevaluated
1132 // if we're a top level panel (i.e. our parent is frame/dialog), our
1133 // own constraints will never be satisfied any more unless we do it
1138 while ( noChanges
> 0 )
1140 constr
->SatisfyConstraints(this, &noChanges
);
1144 DoPhase(1); // Layout children
1145 DoPhase(2); // Layout grand children
1146 SetConstraintSizes(); // Recursively set the real window sizes
1153 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1154 // do a similar thing, but also impose their own 'constraints' and order the
1155 // evaluation differently.
1156 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1158 wxLayoutConstraints
*constr
= GetConstraints();
1161 return constr
->SatisfyConstraints(this, noChanges
);
1167 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1177 // Do a phase of evaluating child constraints
1178 bool wxWindowBase::DoPhase(int phase
)
1180 int noIterations
= 0;
1181 int maxIterations
= 500;
1184 wxWindowList succeeded
;
1185 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1189 wxWindowList::Node
*node
= GetChildren().GetFirst();
1192 wxWindow
*child
= node
->GetData();
1193 if ( !child
->IsTopLevel() )
1195 wxLayoutConstraints
*constr
= child
->GetConstraints();
1198 if ( !succeeded
.Find(child
) )
1200 int tempNoChanges
= 0;
1201 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1202 noChanges
+= tempNoChanges
;
1205 succeeded
.Append(child
);
1210 node
= node
->GetNext();
1219 void wxWindowBase::ResetConstraints()
1221 wxLayoutConstraints
*constr
= GetConstraints();
1224 constr
->left
.SetDone(FALSE
);
1225 constr
->top
.SetDone(FALSE
);
1226 constr
->right
.SetDone(FALSE
);
1227 constr
->bottom
.SetDone(FALSE
);
1228 constr
->width
.SetDone(FALSE
);
1229 constr
->height
.SetDone(FALSE
);
1230 constr
->centreX
.SetDone(FALSE
);
1231 constr
->centreY
.SetDone(FALSE
);
1234 wxWindowList::Node
*node
= GetChildren().GetFirst();
1237 wxWindow
*win
= node
->GetData();
1238 if ( !win
->IsTopLevel() )
1239 win
->ResetConstraints();
1240 node
= node
->GetNext();
1244 // Need to distinguish between setting the 'fake' size for windows and sizers,
1245 // and setting the real values.
1246 void wxWindowBase::SetConstraintSizes(bool recurse
)
1248 wxLayoutConstraints
*constr
= GetConstraints();
1249 if ( constr
&& constr
->AreSatisfied() )
1251 int x
= constr
->left
.GetValue();
1252 int y
= constr
->top
.GetValue();
1253 int w
= constr
->width
.GetValue();
1254 int h
= constr
->height
.GetValue();
1256 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1257 (constr
->height
.GetRelationship() != wxAsIs
) )
1259 SetSize(x
, y
, w
, h
);
1263 // If we don't want to resize this window, just move it...
1269 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1270 GetClassInfo()->GetClassName(),
1276 wxWindowList::Node
*node
= GetChildren().GetFirst();
1279 wxWindow
*win
= node
->GetData();
1280 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1281 win
->SetConstraintSizes();
1282 node
= node
->GetNext();
1287 // Only set the size/position of the constraint (if any)
1288 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1290 wxLayoutConstraints
*constr
= GetConstraints();
1295 constr
->left
.SetValue(x
);
1296 constr
->left
.SetDone(TRUE
);
1300 constr
->top
.SetValue(y
);
1301 constr
->top
.SetDone(TRUE
);
1305 constr
->width
.SetValue(w
);
1306 constr
->width
.SetDone(TRUE
);
1310 constr
->height
.SetValue(h
);
1311 constr
->height
.SetDone(TRUE
);
1316 void wxWindowBase::MoveConstraint(int x
, int y
)
1318 wxLayoutConstraints
*constr
= GetConstraints();
1323 constr
->left
.SetValue(x
);
1324 constr
->left
.SetDone(TRUE
);
1328 constr
->top
.SetValue(y
);
1329 constr
->top
.SetDone(TRUE
);
1334 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1336 wxLayoutConstraints
*constr
= GetConstraints();
1339 *w
= constr
->width
.GetValue();
1340 *h
= constr
->height
.GetValue();
1346 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1348 wxLayoutConstraints
*constr
= GetConstraints();
1351 *w
= constr
->width
.GetValue();
1352 *h
= constr
->height
.GetValue();
1355 GetClientSize(w
, h
);
1358 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1360 // don't do it for the dialogs/frames - they float independently of their
1362 if ( !IsTopLevel() )
1364 wxWindow
*parent
= GetParent();
1365 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1367 wxPoint
pt(parent
->GetClientAreaOrigin());
1375 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1377 wxLayoutConstraints
*constr
= GetConstraints();
1380 *x
= constr
->left
.GetValue();
1381 *y
= constr
->top
.GetValue();
1387 #endif // wxUSE_CONSTRAINTS
1389 // ----------------------------------------------------------------------------
1390 // do Update UI processing for child controls
1391 // ----------------------------------------------------------------------------
1393 // TODO: should this be implemented for the child window rather
1394 // than the parent? Then you can override it e.g. for wxCheckBox
1395 // to do the Right Thing rather than having to assume a fixed number
1396 // of control classes.
1397 void wxWindowBase::UpdateWindowUI()
1400 wxUpdateUIEvent
event(GetId());
1401 event
.m_eventObject
= this;
1403 if ( GetEventHandler()->ProcessEvent(event
) )
1405 if ( event
.GetSetEnabled() )
1406 Enable(event
.GetEnabled());
1408 if ( event
.GetSetText() )
1410 wxControl
*control
= wxDynamicCastThis(wxControl
);
1414 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1416 text
->SetValue(event
.GetText());
1418 #endif // wxUSE_TEXTCTRL
1419 control
->SetLabel(event
.GetText());
1424 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1427 if ( event
.GetSetChecked() )
1428 checkbox
->SetValue(event
.GetChecked());
1430 #endif // wxUSE_CHECKBOX
1433 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1436 if ( event
.GetSetChecked() )
1437 radiobtn
->SetValue(event
.GetChecked());
1439 #endif // wxUSE_RADIOBTN
1441 #endif // wxUSE_CONTROLS
1444 // ----------------------------------------------------------------------------
1445 // dialog units translations
1446 // ----------------------------------------------------------------------------
1448 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1450 int charWidth
= GetCharWidth();
1451 int charHeight
= GetCharHeight();
1452 wxPoint
pt2(-1, -1);
1454 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1456 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1461 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1463 int charWidth
= GetCharWidth();
1464 int charHeight
= GetCharHeight();
1465 wxPoint
pt2(-1, -1);
1467 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1469 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1474 // ----------------------------------------------------------------------------
1476 // ----------------------------------------------------------------------------
1478 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1480 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1481 wxT("can't have both object and void client data") );
1483 if ( m_clientObject
)
1484 delete m_clientObject
;
1486 m_clientObject
= data
;
1487 m_clientDataType
= wxClientData_Object
;
1490 wxClientData
*wxWindowBase::DoGetClientObject() const
1492 // it's not an error to call GetClientObject() on a window which doesn't
1493 // have client data at all - NULL will be returned
1494 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1495 wxT("this window doesn't have object client data") );
1497 return m_clientObject
;
1500 void wxWindowBase::DoSetClientData( void *data
)
1502 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1503 wxT("can't have both object and void client data") );
1505 m_clientData
= data
;
1506 m_clientDataType
= wxClientData_Void
;
1509 void *wxWindowBase::DoGetClientData() const
1511 // it's not an error to call GetClientData() on a window which doesn't have
1512 // client data at all - NULL will be returned
1513 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1514 wxT("this window doesn't have void client data") );
1516 return m_clientData
;
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1523 // propagate the colour change event to the subwindows
1524 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1526 wxWindowList::Node
*node
= GetChildren().GetFirst();
1529 // Only propagate to non-top-level windows
1530 wxWindow
*win
= node
->GetData();
1531 if ( !win
->IsTopLevel() )
1533 wxSysColourChangedEvent event2
;
1534 event
.m_eventObject
= win
;
1535 win
->GetEventHandler()->ProcessEvent(event2
);
1538 node
= node
->GetNext();
1542 // the default action is to populate dialog with data when it's created
1543 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1545 TransferDataToWindow();
1548 // process Ctrl-Alt-mclick
1549 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1552 if ( event
.ControlDown() && event
.AltDown() )
1554 // don't translate these strings
1556 switch ( wxGetOsVersion() )
1558 case wxMOTIF_X
: port
= _T("Motif"); break;
1559 case wxMACINTOSH
: port
= _T("Mac"); break;
1560 case wxBEOS
: port
= _T("BeOS"); break;
1564 case wxGTK_BEOS
: port
= _T("GTK"); break;
1570 case wxWIN386
: port
= _T("MS Windows"); break;
1574 case wxMGL_OS2
: port
= _T("MGL"); break;
1576 case wxOS2_PM
: port
= _T("OS/2"); break;
1577 default: port
= _T("unknown"); break;
1580 wxMessageBox(wxString::Format(
1582 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1591 _T("wxWindows information"),
1592 wxICON_INFORMATION
| wxOK
,
1596 #endif // wxUSE_MSGDLG
1602 // ----------------------------------------------------------------------------
1603 // list classes implementation
1604 // ----------------------------------------------------------------------------
1606 void wxWindowListNode::DeleteData()
1608 delete (wxWindow
*)GetData();
1611 // ----------------------------------------------------------------------------
1613 // ----------------------------------------------------------------------------
1615 wxBorder
wxWindowBase::GetBorder() const
1617 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1618 if ( border
== wxBORDER_DEFAULT
)
1620 border
= GetDefaultBorder();
1626 wxBorder
wxWindowBase::GetDefaultBorder() const
1628 return wxBORDER_NONE
;
1631 // ----------------------------------------------------------------------------
1633 // ----------------------------------------------------------------------------
1635 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1637 // here we just check if the point is inside the window or not
1639 // check the top and left border first
1640 bool outside
= x
< 0 || y
< 0;
1643 // check the right and bottom borders too
1644 wxSize size
= GetSize();
1645 outside
= x
>= size
.x
|| y
>= size
.y
;
1648 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;