1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // the default event handler is just this window
119 m_eventHandler
= this;
123 m_windowValidator
= (wxValidator
*) NULL
;
124 #endif // wxUSE_VALIDATORS
126 // use the system default colours
127 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
128 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
130 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
131 // so the font is *not* really set - but calls to SetFont() later won't do
132 // anything because m_font appears to be already set!
134 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
137 // the colours/fonts are default for now
146 // an optimization for the event processing: checking this flag is much
147 // faster than using IsKindOf(CLASSINFO(wxWindow))
150 #if wxUSE_CONSTRAINTS
151 // no constraints whatsoever
152 m_constraints
= (wxLayoutConstraints
*) NULL
;
153 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
154 m_windowSizer
= (wxSizer
*) NULL
;
155 m_autoLayout
= FALSE
;
156 #endif // wxUSE_CONSTRAINTS
158 #if wxUSE_DRAG_AND_DROP
159 m_dropTarget
= (wxDropTarget
*)NULL
;
160 #endif // wxUSE_DRAG_AND_DROP
163 m_tooltip
= (wxToolTip
*)NULL
;
164 #endif // wxUSE_TOOLTIPS
167 m_caret
= (wxCaret
*)NULL
;
168 #endif // wxUSE_CARET
171 m_custompalette
= false;
172 #endif // wxUSE_PALETTE
174 // Whether we're using the current theme for this window (wxGTK only for now)
175 m_themeEnabled
= FALSE
;
178 // common part of window creation process
179 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
181 const wxPoint
& WXUNUSED(pos
),
182 const wxSize
& WXUNUSED(size
),
184 const wxValidator
& validator
,
185 const wxString
& name
)
187 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
188 // member variables - check that it has been called (will catch the case
189 // when a new ctor is added which doesn't call InitWindow)
190 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
192 // generate a new id if the user doesn't care about it
193 m_windowId
= id
== -1 ? NewControlId() : id
;
196 SetWindowStyleFlag(style
);
200 SetValidator(validator
);
201 #endif // wxUSE_VALIDATORS
203 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
204 // have it too - like this it's possible to set it only in the top level
205 // dialog/frame and all children will inherit it by defult
206 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
208 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
219 wxWindowBase::~wxWindowBase()
221 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
223 // FIXME if these 2 cases result from programming errors in the user code
224 // we should probably assert here instead of silently fixing them
226 // Just in case the window has been Closed, but we're then deleting
227 // immediately: don't leave dangling pointers.
228 wxPendingDelete
.DeleteObject(this);
230 // Just in case we've loaded a top-level window via LoadNativeDialog but
231 // we weren't a dialog class
232 wxTopLevelWindows
.DeleteObject(this);
234 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
239 #endif // wxUSE_CARET
242 if ( m_windowValidator
)
243 delete m_windowValidator
;
244 #endif // wxUSE_VALIDATORS
246 #if wxUSE_CONSTRAINTS
247 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
248 // at deleted windows as they delete themselves.
249 DeleteRelatedConstraints();
253 // This removes any dangling pointers to this window in other windows'
254 // constraintsInvolvedIn lists.
255 UnsetConstraints(m_constraints
);
256 delete m_constraints
;
257 m_constraints
= NULL
;
261 delete m_windowSizer
;
263 #endif // wxUSE_CONSTRAINTS
265 #if wxUSE_DRAG_AND_DROP
268 #endif // wxUSE_DRAG_AND_DROP
273 #endif // wxUSE_TOOLTIPS
275 // reset the dangling pointer our parent window may keep to us
276 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
278 m_parent
->SetDefaultItem(NULL
);
282 bool wxWindowBase::Destroy()
289 bool wxWindowBase::Close(bool force
)
291 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
292 event
.SetEventObject(this);
293 #if WXWIN_COMPATIBILITY
294 event
.SetForce(force
);
295 #endif // WXWIN_COMPATIBILITY
296 event
.SetCanVeto(!force
);
298 // return FALSE if window wasn't closed because the application vetoed the
300 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
303 bool wxWindowBase::DestroyChildren()
305 wxWindowList::Node
*node
;
308 // we iterate until the list becomes empty
309 node
= GetChildren().GetFirst();
313 wxWindow
*child
= node
->GetData();
315 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
320 wxASSERT_MSG( !GetChildren().Find(child
),
321 wxT("child didn't remove itself using RemoveChild()") );
327 // ----------------------------------------------------------------------------
328 // size/position related methods
329 // ----------------------------------------------------------------------------
331 // centre the window with respect to its parent in either (or both) directions
332 void wxWindowBase::Centre(int direction
)
334 // the position/size of the parent window or of the entire screen
336 int widthParent
, heightParent
;
338 wxWindow
*parent
= NULL
;
340 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
342 // find the parent to centre this window on: it should be the
343 // immediate parent for the controls but the top level parent for the
344 // top level windows (like dialogs)
345 parent
= GetParent();
348 while ( parent
&& !parent
->IsTopLevel() )
350 parent
= parent
->GetParent();
354 // did we find the parent?
358 direction
|= wxCENTRE_ON_SCREEN
;
362 if ( direction
& wxCENTRE_ON_SCREEN
)
364 // centre with respect to the whole screen
365 wxDisplaySize(&widthParent
, &heightParent
);
371 // centre on the parent
372 parent
->GetSize(&widthParent
, &heightParent
);
374 // adjust to the parents position
375 posParent
= parent
->GetPosition();
379 // centre inside the parents client rectangle
380 parent
->GetClientSize(&widthParent
, &heightParent
);
385 GetSize(&width
, &height
);
390 if ( direction
& wxHORIZONTAL
)
391 xNew
= (widthParent
- width
)/2;
393 if ( direction
& wxVERTICAL
)
394 yNew
= (heightParent
- height
)/2;
399 // Base size of the visible dimensions of the display
400 // to take into account the taskbar
401 wxRect rect
= wxGetClientDisplayRect();
402 wxSize
size (rect
.width
,rect
.height
);
404 if (posParent
.x
>= 0) // if parent is on the main display
408 else if (xNew
+width
> size
.x
)
409 xNew
= size
.x
-width
-1;
411 if (posParent
.y
>= 0) // if parent is on the main display
413 if (yNew
+height
> size
.y
)
414 yNew
= size
.y
-height
-1;
416 // Make certain that the title bar is initially visible
417 // always, even if this would push the bottom of the
418 // dialog of the visible area of the display
423 // move the window to this position (keeping the old size but using
424 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
425 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
428 // fits the window around the children
429 void wxWindowBase::Fit()
431 if ( GetChildren().GetCount() > 0 )
433 wxSize size
= DoGetBestSize();
435 // for compatibility with the old versions and because it really looks
436 // slightly more pretty like this, add a pad
442 //else: do nothing if we have no children
445 // return the size best suited for the current window
446 wxSize
wxWindowBase::DoGetBestSize() const
448 if ( GetChildren().GetCount() > 0 )
450 // our minimal acceptable size is such that all our windows fit inside
454 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
456 node
= node
->GetNext() )
458 wxWindow
*win
= node
->GetData();
459 if ( win
->IsTopLevel()
461 || wxDynamicCast(win
, wxStatusBar
)
462 #endif // wxUSE_STATUSBAR
465 // dialogs and frames lie in different top level windows -
466 // don't deal with them here; as for the status bars, they
467 // don't lie in the client area at all
472 win
->GetPosition(&wx
, &wy
);
474 // if the window hadn't been positioned yet, assume that it is in
481 win
->GetSize(&ww
, &wh
);
482 if ( wx
+ ww
> maxX
)
484 if ( wy
+ wh
> maxY
)
488 return wxSize(maxX
, maxY
);
492 // for a generic window there is no natural best size - just use the
498 // by default the origin is not shifted
499 wxPoint
wxWindowBase::GetClientAreaOrigin() const
501 return wxPoint(0, 0);
504 // set the min/max size of the window
505 void wxWindowBase::SetSizeHints(int minW
, int minH
,
507 int WXUNUSED(incW
), int WXUNUSED(incH
))
515 // ----------------------------------------------------------------------------
516 // show/hide/enable/disable the window
517 // ----------------------------------------------------------------------------
519 bool wxWindowBase::Show(bool show
)
521 if ( show
!= m_isShown
)
533 bool wxWindowBase::Enable(bool enable
)
535 if ( enable
!= m_isEnabled
)
537 m_isEnabled
= enable
;
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
550 bool wxWindowBase::IsTopLevel() const
555 // ----------------------------------------------------------------------------
556 // reparenting the window
557 // ----------------------------------------------------------------------------
559 void wxWindowBase::AddChild(wxWindowBase
*child
)
561 wxCHECK_RET( child
, wxT("can't add a NULL child") );
563 // this should never happen and it will lead to a crash later if it does
564 // because RemoveChild() will remove only one node from the children list
565 // and the other(s) one(s) will be left with dangling pointers in them
566 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
568 GetChildren().Append(child
);
569 child
->SetParent(this);
572 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
574 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
576 GetChildren().DeleteObject(child
);
577 child
->SetParent((wxWindow
*)NULL
);
580 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
582 wxWindow
*oldParent
= GetParent();
583 if ( newParent
== oldParent
)
589 // unlink this window from the existing parent.
592 oldParent
->RemoveChild(this);
596 wxTopLevelWindows
.DeleteObject(this);
599 // add it to the new one
602 newParent
->AddChild(this);
606 wxTopLevelWindows
.Append(this);
612 // ----------------------------------------------------------------------------
613 // event handler stuff
614 // ----------------------------------------------------------------------------
616 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
618 handler
->SetNextHandler(GetEventHandler());
619 SetEventHandler(handler
);
622 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
624 wxEvtHandler
*handlerA
= GetEventHandler();
627 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
628 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
629 SetEventHandler(handlerB
);
633 handlerA
= (wxEvtHandler
*)NULL
;
640 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
642 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
644 wxEvtHandler
*handlerPrev
= NULL
,
645 *handlerCur
= GetEventHandler();
648 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
650 if ( handlerCur
== handler
)
654 handlerPrev
->SetNextHandler(handlerNext
);
658 SetEventHandler(handlerNext
);
661 handler
->SetNextHandler(NULL
);
666 handlerPrev
= handlerCur
;
667 handlerCur
= handlerNext
;
670 wxFAIL_MSG( _T("where has the event handler gone?") );
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
681 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
684 m_backgroundColour
= colour
;
691 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
693 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
696 m_foregroundColour
= colour
;
703 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
705 // setting an invalid cursor is ok, it means that we don't have any special
707 if ( m_cursor
== cursor
)
718 bool wxWindowBase::SetFont(const wxFont
& font
)
720 // don't try to set invalid font, always fall back to the default
721 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
723 if ( fontOk
== m_font
)
737 void wxWindowBase::SetCaret(wxCaret
*caret
)
748 wxASSERT_MSG( m_caret
->GetWindow() == this,
749 wxT("caret should be created associated to this window") );
752 #endif // wxUSE_CARET
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 void wxWindowBase::SetValidator(const wxValidator
& validator
)
761 if ( m_windowValidator
)
762 delete m_windowValidator
;
764 m_windowValidator
= (wxValidator
*)validator
.Clone();
766 if ( m_windowValidator
)
767 m_windowValidator
->SetWindow(this) ;
769 #endif // wxUSE_VALIDATORS
771 // ----------------------------------------------------------------------------
772 // update region stuff
773 // ----------------------------------------------------------------------------
775 wxRect
wxWindowBase::GetUpdateClientRect() const
777 wxRegion rgnUpdate
= GetUpdateRegion();
778 rgnUpdate
.Intersect(GetClientRect());
779 wxRect rectUpdate
= rgnUpdate
.GetBox();
780 wxPoint ptOrigin
= GetClientAreaOrigin();
781 rectUpdate
.x
-= ptOrigin
.x
;
782 rectUpdate
.y
-= ptOrigin
.y
;
787 bool wxWindowBase::IsExposed(int x
, int y
) const
789 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
792 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
794 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
797 // ----------------------------------------------------------------------------
798 // find window by id or name
799 // ----------------------------------------------------------------------------
801 wxWindow
*wxWindowBase::FindWindow( long id
)
803 if ( id
== m_windowId
)
804 return (wxWindow
*)this;
806 wxWindowBase
*res
= (wxWindow
*)NULL
;
807 wxWindowList::Node
*node
;
808 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
810 wxWindowBase
*child
= node
->GetData();
811 res
= child
->FindWindow( id
);
814 return (wxWindow
*)res
;
817 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
819 if ( name
== m_windowName
)
820 return (wxWindow
*)this;
822 wxWindowBase
*res
= (wxWindow
*)NULL
;
823 wxWindowList::Node
*node
;
824 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
826 wxWindow
*child
= node
->GetData();
827 res
= child
->FindWindow(name
);
830 return (wxWindow
*)res
;
833 // ----------------------------------------------------------------------------
834 // dialog oriented functions
835 // ----------------------------------------------------------------------------
837 void wxWindowBase::MakeModal(bool modal
)
839 // Disable all other windows
842 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
845 wxWindow
*win
= node
->GetData();
849 node
= node
->GetNext();
854 bool wxWindowBase::Validate()
857 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
859 wxWindowList::Node
*node
;
860 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
862 wxWindowBase
*child
= node
->GetData();
863 wxValidator
*validator
= child
->GetValidator();
864 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
869 if ( recurse
&& !child
->Validate() )
874 #endif // wxUSE_VALIDATORS
879 bool wxWindowBase::TransferDataToWindow()
882 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
884 wxWindowList::Node
*node
;
885 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
887 wxWindowBase
*child
= node
->GetData();
888 wxValidator
*validator
= child
->GetValidator();
889 if ( validator
&& !validator
->TransferToWindow() )
891 wxLogWarning(_("Could not transfer data to window"));
892 wxLog::FlushActive();
899 if ( !child
->TransferDataToWindow() )
901 // warning already given
906 #endif // wxUSE_VALIDATORS
911 bool wxWindowBase::TransferDataFromWindow()
914 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
916 wxWindowList::Node
*node
;
917 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
919 wxWindow
*child
= node
->GetData();
920 wxValidator
*validator
= child
->GetValidator();
921 if ( validator
&& !validator
->TransferFromWindow() )
923 // nop warning here because the application is supposed to give
924 // one itself - we don't know here what might have gone wrongly
931 if ( !child
->TransferDataFromWindow() )
933 // warning already given
938 #endif // wxUSE_VALIDATORS
943 void wxWindowBase::InitDialog()
945 wxInitDialogEvent
event(GetId());
946 event
.SetEventObject( this );
947 GetEventHandler()->ProcessEvent(event
);
950 // ----------------------------------------------------------------------------
951 // context-sensitive help support
952 // ----------------------------------------------------------------------------
956 // associate this help text with this window
957 void wxWindowBase::SetHelpText(const wxString
& text
)
959 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
962 helpProvider
->AddHelp(this, text
);
966 // associate this help text with all windows with the same id as this
968 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
970 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
973 helpProvider
->AddHelp(GetId(), text
);
977 // get the help string associated with this window (may be empty)
978 wxString
wxWindowBase::GetHelpText() const
981 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
984 text
= helpProvider
->GetHelp(this);
990 // show help for this window
991 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
993 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
996 if ( helpProvider
->ShowHelp(this) )
998 // skip the event.Skip() below
1006 #endif // wxUSE_HELP
1008 // ----------------------------------------------------------------------------
1009 // tooltipsroot.Replace("\\", "/");
1010 // ----------------------------------------------------------------------------
1014 void wxWindowBase::SetToolTip( const wxString
&tip
)
1016 // don't create the new tooltip if we already have one
1019 m_tooltip
->SetTip( tip
);
1023 SetToolTip( new wxToolTip( tip
) );
1026 // setting empty tooltip text does not remove the tooltip any more - use
1027 // SetToolTip((wxToolTip *)NULL) for this
1030 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1035 m_tooltip
= tooltip
;
1038 #endif // wxUSE_TOOLTIPS
1040 // ----------------------------------------------------------------------------
1041 // constraints and sizers
1042 // ----------------------------------------------------------------------------
1044 #if wxUSE_CONSTRAINTS
1046 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1048 if ( m_constraints
)
1050 UnsetConstraints(m_constraints
);
1051 delete m_constraints
;
1053 m_constraints
= constraints
;
1054 if ( m_constraints
)
1056 // Make sure other windows know they're part of a 'meaningful relationship'
1057 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1058 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1059 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1060 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1061 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1062 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1063 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1064 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1065 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1066 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1067 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1068 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1069 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1070 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1071 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1072 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1076 // This removes any dangling pointers to this window in other windows'
1077 // constraintsInvolvedIn lists.
1078 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1082 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1083 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1084 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1085 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1086 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1087 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1088 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1089 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1090 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1091 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1092 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1093 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1094 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1095 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1096 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1097 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1101 // Back-pointer to other windows we're involved with, so if we delete this
1102 // window, we must delete any constraints we're involved with.
1103 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1105 if ( !m_constraintsInvolvedIn
)
1106 m_constraintsInvolvedIn
= new wxWindowList
;
1107 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1108 m_constraintsInvolvedIn
->Append(otherWin
);
1111 // REMOVE back-pointer to other windows we're involved with.
1112 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1114 if ( m_constraintsInvolvedIn
)
1115 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1118 // Reset any constraints that mention this window
1119 void wxWindowBase::DeleteRelatedConstraints()
1121 if ( m_constraintsInvolvedIn
)
1123 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1126 wxWindow
*win
= node
->GetData();
1127 wxLayoutConstraints
*constr
= win
->GetConstraints();
1129 // Reset any constraints involving this window
1132 constr
->left
.ResetIfWin(this);
1133 constr
->top
.ResetIfWin(this);
1134 constr
->right
.ResetIfWin(this);
1135 constr
->bottom
.ResetIfWin(this);
1136 constr
->width
.ResetIfWin(this);
1137 constr
->height
.ResetIfWin(this);
1138 constr
->centreX
.ResetIfWin(this);
1139 constr
->centreY
.ResetIfWin(this);
1142 wxWindowList::Node
*next
= node
->GetNext();
1147 delete m_constraintsInvolvedIn
;
1148 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1152 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1154 if (m_windowSizer
) delete m_windowSizer
;
1156 m_windowSizer
= sizer
;
1159 bool wxWindowBase::Layout()
1161 // If there is a sizer, use it instead of the constraints
1165 GetClientSize(&w
, &h
);
1167 GetSizer()->SetDimension( 0, 0, w
, h
);
1168 #if defined __WXPM__
1174 wxLayoutConstraints
*constr
= GetConstraints();
1175 bool wasOk
= constr
&& constr
->AreSatisfied();
1177 ResetConstraints(); // Mark all constraints as unevaluated
1179 // if we're a top level panel (i.e. our parent is frame/dialog), our
1180 // own constraints will never be satisfied any more unless we do it
1185 while ( noChanges
> 0 )
1187 constr
->SatisfyConstraints(this, &noChanges
);
1191 DoPhase(1); // Layout children
1192 DoPhase(2); // Layout grand children
1193 SetConstraintSizes(); // Recursively set the real window sizes
1200 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1201 // do a similar thing, but also impose their own 'constraints' and order the
1202 // evaluation differently.
1203 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1205 wxLayoutConstraints
*constr
= GetConstraints();
1208 return constr
->SatisfyConstraints(this, noChanges
);
1214 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1224 // Do a phase of evaluating child constraints
1225 bool wxWindowBase::DoPhase(int phase
)
1227 int noIterations
= 0;
1228 int maxIterations
= 500;
1231 wxWindowList succeeded
;
1232 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1236 wxWindowList::Node
*node
= GetChildren().GetFirst();
1239 wxWindow
*child
= node
->GetData();
1240 if ( !child
->IsTopLevel() )
1242 wxLayoutConstraints
*constr
= child
->GetConstraints();
1245 if ( !succeeded
.Find(child
) )
1247 int tempNoChanges
= 0;
1248 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1249 noChanges
+= tempNoChanges
;
1252 succeeded
.Append(child
);
1257 node
= node
->GetNext();
1266 void wxWindowBase::ResetConstraints()
1268 wxLayoutConstraints
*constr
= GetConstraints();
1271 constr
->left
.SetDone(FALSE
);
1272 constr
->top
.SetDone(FALSE
);
1273 constr
->right
.SetDone(FALSE
);
1274 constr
->bottom
.SetDone(FALSE
);
1275 constr
->width
.SetDone(FALSE
);
1276 constr
->height
.SetDone(FALSE
);
1277 constr
->centreX
.SetDone(FALSE
);
1278 constr
->centreY
.SetDone(FALSE
);
1281 wxWindowList::Node
*node
= GetChildren().GetFirst();
1284 wxWindow
*win
= node
->GetData();
1285 if ( !win
->IsTopLevel() )
1286 win
->ResetConstraints();
1287 node
= node
->GetNext();
1291 // Need to distinguish between setting the 'fake' size for windows and sizers,
1292 // and setting the real values.
1293 void wxWindowBase::SetConstraintSizes(bool recurse
)
1295 wxLayoutConstraints
*constr
= GetConstraints();
1296 if ( constr
&& constr
->AreSatisfied() )
1298 int x
= constr
->left
.GetValue();
1299 int y
= constr
->top
.GetValue();
1300 int w
= constr
->width
.GetValue();
1301 int h
= constr
->height
.GetValue();
1303 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1304 (constr
->height
.GetRelationship() != wxAsIs
) )
1306 SetSize(x
, y
, w
, h
);
1310 // If we don't want to resize this window, just move it...
1316 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1317 GetClassInfo()->GetClassName(),
1323 wxWindowList::Node
*node
= GetChildren().GetFirst();
1326 wxWindow
*win
= node
->GetData();
1327 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1328 win
->SetConstraintSizes();
1329 node
= node
->GetNext();
1334 // Only set the size/position of the constraint (if any)
1335 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1337 wxLayoutConstraints
*constr
= GetConstraints();
1342 constr
->left
.SetValue(x
);
1343 constr
->left
.SetDone(TRUE
);
1347 constr
->top
.SetValue(y
);
1348 constr
->top
.SetDone(TRUE
);
1352 constr
->width
.SetValue(w
);
1353 constr
->width
.SetDone(TRUE
);
1357 constr
->height
.SetValue(h
);
1358 constr
->height
.SetDone(TRUE
);
1363 void wxWindowBase::MoveConstraint(int x
, int y
)
1365 wxLayoutConstraints
*constr
= GetConstraints();
1370 constr
->left
.SetValue(x
);
1371 constr
->left
.SetDone(TRUE
);
1375 constr
->top
.SetValue(y
);
1376 constr
->top
.SetDone(TRUE
);
1381 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1383 wxLayoutConstraints
*constr
= GetConstraints();
1386 *w
= constr
->width
.GetValue();
1387 *h
= constr
->height
.GetValue();
1393 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1395 wxLayoutConstraints
*constr
= GetConstraints();
1398 *w
= constr
->width
.GetValue();
1399 *h
= constr
->height
.GetValue();
1402 GetClientSize(w
, h
);
1405 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1407 // don't do it for the dialogs/frames - they float independently of their
1409 if ( !IsTopLevel() )
1411 wxWindow
*parent
= GetParent();
1412 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1414 wxPoint
pt(parent
->GetClientAreaOrigin());
1422 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1424 wxLayoutConstraints
*constr
= GetConstraints();
1427 *x
= constr
->left
.GetValue();
1428 *y
= constr
->top
.GetValue();
1434 #endif // wxUSE_CONSTRAINTS
1436 // ----------------------------------------------------------------------------
1437 // do Update UI processing for child controls
1438 // ----------------------------------------------------------------------------
1440 // TODO: should this be implemented for the child window rather
1441 // than the parent? Then you can override it e.g. for wxCheckBox
1442 // to do the Right Thing rather than having to assume a fixed number
1443 // of control classes.
1444 void wxWindowBase::UpdateWindowUI()
1447 wxUpdateUIEvent
event(GetId());
1448 event
.m_eventObject
= this;
1450 if ( GetEventHandler()->ProcessEvent(event
) )
1452 if ( event
.GetSetEnabled() )
1453 Enable(event
.GetEnabled());
1455 if ( event
.GetSetText() )
1457 wxControl
*control
= wxDynamicCastThis(wxControl
);
1461 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1463 text
->SetValue(event
.GetText());
1465 #endif // wxUSE_TEXTCTRL
1466 control
->SetLabel(event
.GetText());
1471 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1474 if ( event
.GetSetChecked() )
1475 checkbox
->SetValue(event
.GetChecked());
1477 #endif // wxUSE_CHECKBOX
1480 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1483 if ( event
.GetSetChecked() )
1484 radiobtn
->SetValue(event
.GetChecked());
1486 #endif // wxUSE_RADIOBTN
1488 #endif // wxUSE_CONTROLS
1491 // ----------------------------------------------------------------------------
1492 // dialog units translations
1493 // ----------------------------------------------------------------------------
1495 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1497 int charWidth
= GetCharWidth();
1498 int charHeight
= GetCharHeight();
1499 wxPoint
pt2(-1, -1);
1501 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1503 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1508 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1510 int charWidth
= GetCharWidth();
1511 int charHeight
= GetCharHeight();
1512 wxPoint
pt2(-1, -1);
1514 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1516 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1521 // ----------------------------------------------------------------------------
1523 // ----------------------------------------------------------------------------
1525 // propagate the colour change event to the subwindows
1526 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1528 wxWindowList::Node
*node
= GetChildren().GetFirst();
1531 // Only propagate to non-top-level windows
1532 wxWindow
*win
= node
->GetData();
1533 if ( !win
->IsTopLevel() )
1535 wxSysColourChangedEvent event2
;
1536 event
.m_eventObject
= win
;
1537 win
->GetEventHandler()->ProcessEvent(event2
);
1540 node
= node
->GetNext();
1544 // the default action is to populate dialog with data when it's created
1545 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1547 TransferDataToWindow();
1550 // process Ctrl-Alt-mclick
1551 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1554 if ( event
.ControlDown() && event
.AltDown() )
1556 // don't translate these strings
1559 #ifdef __WXUNIVERSAL__
1561 #endif // __WXUNIVERSAL__
1563 switch ( wxGetOsVersion() )
1565 case wxMOTIF_X
: port
= _T("Motif"); break;
1567 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1568 case wxBEOS
: port
= _T("BeOS"); break;
1572 case wxGTK_BEOS
: port
= _T("GTK"); break;
1578 case wxWIN386
: port
= _T("MS Windows"); break;
1582 case wxMGL_OS2
: port
= _T("MGL"); break;
1584 case wxOS2_PM
: port
= _T("OS/2"); break;
1585 default: port
= _T("unknown"); break;
1588 wxMessageBox(wxString::Format(
1590 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1604 _T("wxWindows information"),
1605 wxICON_INFORMATION
| wxOK
,
1609 #endif // wxUSE_MSGDLG
1615 // ----------------------------------------------------------------------------
1616 // list classes implementation
1617 // ----------------------------------------------------------------------------
1619 void wxWindowListNode::DeleteData()
1621 delete (wxWindow
*)GetData();
1624 // ----------------------------------------------------------------------------
1626 // ----------------------------------------------------------------------------
1628 wxBorder
wxWindowBase::GetBorder() const
1630 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1631 if ( border
== wxBORDER_DEFAULT
)
1633 border
= GetDefaultBorder();
1639 wxBorder
wxWindowBase::GetDefaultBorder() const
1641 return wxBORDER_NONE
;
1644 // ----------------------------------------------------------------------------
1646 // ----------------------------------------------------------------------------
1648 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1650 // here we just check if the point is inside the window or not
1652 // check the top and left border first
1653 bool outside
= x
< 0 || y
< 0;
1656 // check the right and bottom borders too
1657 wxSize size
= GetSize();
1658 outside
= x
>= size
.x
|| y
>= size
.y
;
1661 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1664 // ----------------------------------------------------------------------------
1666 // ----------------------------------------------------------------------------
1668 struct WXDLLEXPORT wxWindowNext
1672 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1674 void wxWindowBase::CaptureMouse()
1676 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1678 wxWindow
*winOld
= GetCapture();
1682 wxWindowNext
*item
= new wxWindowNext
;
1684 item
->next
= ms_winCaptureNext
;
1685 ms_winCaptureNext
= item
;
1687 //else: no mouse capture to save
1692 void wxWindowBase::ReleaseMouse()
1694 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1696 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1700 if ( ms_winCaptureNext
)
1702 ms_winCaptureNext
->win
->CaptureMouse();
1704 wxWindowNext
*item
= ms_winCaptureNext
;
1705 ms_winCaptureNext
= item
->next
;
1708 //else: stack is empty, no previous capture
1710 wxLogTrace(_T("mousecapture"),
1711 _T("After ReleaseMouse() mouse is captured by 0x%08x"),