]> git.saurik.com Git - wxWidgets.git/blame - src/common/wincmn.cpp
use of div_t.quot and div_t.rem were changed to / and %
[wxWidgets.git] / src / common / wincmn.cpp
CommitLineData
7ec1983b 1/////////////////////////////////////////////////////////////////////////////
f03fc89f 2// Name: common/window.cpp
7ec1983b
VZ
3// Purpose: common (to all ports) wxWindow functions
4// Author: Julian Smart, Vadim Zeitlin
5// Modified by:
6// Created: 13/07/98
7// RCS-ID: $Id$
f03fc89f 8// Copyright: (c) wxWindows team
7ec1983b
VZ
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
f03fc89f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
f701d7ab 19
58654ed0
VZ
20#ifdef __GNUG__
21 #pragma implementation "windowbase.h"
22#endif
23
341287bf
JS
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
f701d7ab 27#ifdef __BORLANDC__
f03fc89f 28 #pragma hdrstop
f701d7ab
JS
29#endif
30
f03fc89f
VZ
31#ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/frame.h"
36 #include "wx/defs.h"
37 #include "wx/window.h"
38 #include "wx/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/settings.h"
41 #include "wx/dialog.h"
42#endif //WX_PRECOMP
43
44#if wxUSE_CONSTRAINTS
45 #include "wx/layout.h"
46#endif // wxUSE_CONSTRAINTS
47
48#if wxUSE_DRAG_AND_DROP
49 #include "wx/dnd.h"
50#endif // wxUSE_DRAG_AND_DROP
51
52#if wxUSE_TOOLTIPS
53 #include "wx/tooltip.h"
54#endif // wxUSE_TOOLTIPS
55
789295bf
VZ
56#if wxUSE_CARET
57 #include "wx/caret.h"
58#endif // wxUSE_CARET
59
f03fc89f
VZ
60// ----------------------------------------------------------------------------
61// static data
62// ----------------------------------------------------------------------------
63
42e69d6b 64int wxWindowBase::ms_lastControlId = -200;
f03fc89f
VZ
65
66IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
67
68// ----------------------------------------------------------------------------
69// event table
70// ----------------------------------------------------------------------------
71
72BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
73 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
74 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
75END_EVENT_TABLE()
76
77// ============================================================================
78// implementation of the common functionality of the wxWindow class
79// ============================================================================
80
81// ----------------------------------------------------------------------------
82// initialization
83// ----------------------------------------------------------------------------
84
85// the default initialization
86void wxWindowBase::InitBase()
87{
88 // no window yet, no parent nor children
f03fc89f
VZ
89 m_parent = (wxWindow *)NULL;
90 m_windowId = -1;
91 m_children.DeleteContents( FALSE ); // don't auto delete node data
92
93 // no constraints on the minimal window size
94 m_minWidth =
95 m_minHeight =
96 m_maxWidth =
97 m_maxHeight = -1;
98
99 // window is created enabled but it's not visible yet
100 m_isShown = FALSE;
101 m_isEnabled = TRUE;
102
103 // no client data
104 m_clientObject = (wxClientData *)NULL;
105 m_clientData = NULL;
106
107 // the default event handler is just this window
108 m_eventHandler = this;
109
88ac883a 110#if wxUSE_VALIDATORS
f03fc89f
VZ
111 // no validator
112 m_windowValidator = (wxValidator *) NULL;
88ac883a 113#endif // wxUSE_VALIDATORS
f03fc89f
VZ
114
115 // use the system default colours
116 wxSystemSettings settings;
117
118 m_backgroundColour = settings.GetSystemColour(wxSYS_COLOUR_BTNFACE);
119 m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
120 m_font = *wxSWISS_FONT; // and this?
121
122 // no style bits
123 m_windowStyle = 0;
124
125 // an optimization for the event processing: checking this flag is much
126 // faster than using IsKindOf(CLASSINFO(wxWindow))
127 m_isWindow = TRUE;
128
129#if wxUSE_CONSTRAINTS
130 // no constraints whatsoever
131 m_constraints = (wxLayoutConstraints *) NULL;
132 m_constraintsInvolvedIn = (wxWindowList *) NULL;
133 m_windowSizer = (wxSizer *) NULL;
134 m_sizerParent = (wxWindowBase *) NULL;
135 m_autoLayout = FALSE;
136#endif // wxUSE_CONSTRAINTS
137
138#if wxUSE_DRAG_AND_DROP
139 m_dropTarget = (wxDropTarget *)NULL;
140#endif // wxUSE_DRAG_AND_DROP
141
142#if wxUSE_TOOLTIPS
143 m_tooltip = (wxToolTip *)NULL;
144#endif // wxUSE_TOOLTIPS
789295bf
VZ
145
146#if wxUSE_CARET
147 m_caret = (wxCaret *)NULL;
148#endif // wxUSE_CARET
f03fc89f
VZ
149}
150
151// common part of window creation process
152bool wxWindowBase::CreateBase(wxWindowBase *parent,
153 wxWindowID id,
74e3313b
VZ
154 const wxPoint& WXUNUSED(pos),
155 const wxSize& WXUNUSED(size),
f03fc89f
VZ
156 long style,
157 const wxString& name)
158{
159 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
160 // member variables - check that it has been called (will catch the case
161 // when a new ctor is added which doesn't call InitWindow)
162 wxASSERT_MSG( m_isWindow, _T("Init() must have been called before!") );
163
164 // generate a new id if the user doesn't care about it
69418a8e 165 m_windowId = id == -1 ? NewControlId() : id;
f03fc89f
VZ
166
167 SetName(name);
168 SetWindowStyleFlag(style);
169 SetParent(parent);
170
171 return TRUE;
172}
173
174// ----------------------------------------------------------------------------
175// destruction
176// ----------------------------------------------------------------------------
177
178// common clean up
179wxWindowBase::~wxWindowBase()
180{
181 // FIXME if these 2 cases result from programming errors in the user code
182 // we should probably assert here instead of silently fixing them
183
184 // Just in case the window has been Closed, but we're then deleting
185 // immediately: don't leave dangling pointers.
186 wxPendingDelete.DeleteObject(this);
187
188 // Just in case we've loaded a top-level window via LoadNativeDialog but
189 // we weren't a dialog class
190 wxTopLevelWindows.DeleteObject(this);
a23fd0e1 191
9111db68 192 wxASSERT_MSG( GetChildren().GetCount() == 0, _T("children not destroyed") );
f03fc89f 193
319fefa9
VZ
194 // make sure that there are no dangling pointers left pointing to us
195 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
196 if ( panel )
197 {
198 if ( panel->GetLastFocus() == this )
199 {
200 panel->SetLastFocus((wxWindow *)NULL);
201 }
202 }
203
789295bf
VZ
204#if wxUSE_CARET
205 if ( m_caret )
206 delete m_caret;
207#endif // wxUSE_CARET
208
88ac883a 209#if wxUSE_VALIDATORS
f03fc89f
VZ
210 if ( m_windowValidator )
211 delete m_windowValidator;
88ac883a 212#endif // wxUSE_VALIDATORS
f03fc89f
VZ
213
214 if ( m_clientObject )
215 delete m_clientObject;
216
217#if wxUSE_CONSTRAINTS
218 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
219 // at deleted windows as they delete themselves.
220 DeleteRelatedConstraints();
221
222 if ( m_constraints )
223 {
224 // This removes any dangling pointers to this window in other windows'
225 // constraintsInvolvedIn lists.
226 UnsetConstraints(m_constraints);
227 delete m_constraints;
228 m_constraints = NULL;
229 }
230
231 if ( m_windowSizer )
232 delete m_windowSizer;
233
234 // If this is a child of a sizer, remove self from parent
235 if ( m_sizerParent )
236 m_sizerParent->RemoveChild(this);
237#endif // wxUSE_CONSTRAINTS
238
239#if wxUSE_DRAG_AND_DROP
240 if ( m_dropTarget )
241 delete m_dropTarget;
242#endif // wxUSE_DRAG_AND_DROP
243
244#if wxUSE_TOOLTIPS
245 if ( m_tooltip )
246 delete m_tooltip;
247#endif // wxUSE_TOOLTIPS
248}
249
250bool wxWindowBase::Destroy()
251{
252 delete this;
253
254 return TRUE;
255}
256
257bool wxWindowBase::Close(bool force)
258{
259 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
260 event.SetEventObject(this);
261#if WXWIN_COMPATIBILITY
262 event.SetForce(force);
263#endif // WXWIN_COMPATIBILITY
264 event.SetCanVeto(!force);
265
266 // return FALSE if window wasn't closed because the application vetoed the
267 // close event
268 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
269}
270
271bool wxWindowBase::DestroyChildren()
272{
273 wxWindowList::Node *node;
a23fd0e1 274 for ( ;; )
f03fc89f 275 {
a23fd0e1
VZ
276 // we iterate until the list becomes empty
277 node = GetChildren().GetFirst();
278 if ( !node )
279 break;
280
f03fc89f 281 wxWindow *child = node->GetData();
a23fd0e1 282
9111db68 283 wxASSERT_MSG( child, _T("children list contains empty nodes") );
a23fd0e1 284
eb082a08 285 delete child;
a23fd0e1
VZ
286
287 wxASSERT_MSG( !GetChildren().Find(child),
9111db68 288 _T("child didn't remove itself using RemoveChild()") );
f03fc89f
VZ
289 }
290
291 return TRUE;
292}
293
294// ----------------------------------------------------------------------------
295// centre/fit the window
296// ----------------------------------------------------------------------------
297
298// centre the window with respect to its parent in either (or both) directions
299void wxWindowBase::Centre(int direction)
300{
301 int widthParent, heightParent;
302
303 wxWindow *parent = GetParent();
304 if ( parent )
305 {
306 parent->GetClientSize(&widthParent, &heightParent);
307 }
308 else
309 {
310 // centre with respect to the whole screen
311 wxDisplaySize(&widthParent, &heightParent);
312 }
313
314 int width, height;
315 GetSize(&width, &height);
316
c39eda94
VZ
317 int xNew = -1,
318 yNew = -1;
f03fc89f
VZ
319
320 if ( direction & wxHORIZONTAL )
c39eda94 321 xNew = (widthParent - width)/2;
f03fc89f
VZ
322
323 if ( direction & wxVERTICAL )
c39eda94 324 yNew = (heightParent - height)/2;
f03fc89f 325
c39eda94
VZ
326 // controls are always centered on their parent because it doesn't make
327 // sense to centre them on the screen
328 if ( (direction & wxCENTER_FRAME) || wxDynamicCast(this, wxControl) )
329 {
330 // adjust to the parents client area origin
331 wxPoint posParent = parent->ClientToScreen(wxPoint(0, 0));
f03fc89f 332
c39eda94
VZ
333 xNew += posParent.x;
334 yNew += posParent.y;
7631a292
RD
335 }
336
c39eda94 337 Move(xNew, yNew);
7631a292
RD
338}
339
f03fc89f
VZ
340// fits the window around the children
341void wxWindowBase::Fit()
342{
343 int maxX = 0,
344 maxY = 0;
345
bfac8499
VZ
346 for ( wxWindowList::Node *node = GetChildren().GetFirst();
347 node;
348 node = node->GetNext() )
f03fc89f
VZ
349 {
350 wxWindow *win = node->GetData();
34636400 351 if ( win->IsTopLevel() )
42e69d6b 352 {
34636400 353 // dialogs and frames lie in different top level windows - don't
42e69d6b
VZ
354 // deal with them here
355 continue;
356 }
357
f03fc89f
VZ
358 int wx, wy, ww, wh;
359 win->GetPosition(&wx, &wy);
360 win->GetSize(&ww, &wh);
361 if ( wx + ww > maxX )
362 maxX = wx + ww;
363 if ( wy + wh > maxY )
364 maxY = wy + wh;
f03fc89f
VZ
365 }
366
367 // leave a margin
368 SetClientSize(maxX + 7, maxY + 14);
369}
370
371// set the min/max size of the window
372
373void wxWindowBase::SetSizeHints(int minW, int minH,
374 int maxW, int maxH,
375 int WXUNUSED(incW), int WXUNUSED(incH))
376{
377 m_minWidth = minW;
378 m_maxWidth = maxW;
379 m_minHeight = minH;
380 m_maxHeight = maxH;
381}
382
383// ----------------------------------------------------------------------------
384// show/hide/enable/disable the window
385// ----------------------------------------------------------------------------
386
387bool wxWindowBase::Show(bool show)
388{
389 if ( show != m_isShown )
390 {
391 m_isShown = show;
392
393 return TRUE;
394 }
395 else
396 {
397 return FALSE;
398 }
399}
400
401bool wxWindowBase::Enable(bool enable)
402{
403 if ( enable != m_isEnabled )
404 {
405 m_isEnabled = enable;
406
407 return TRUE;
408 }
409 else
410 {
411 return FALSE;
412 }
413}
34636400
VZ
414// ----------------------------------------------------------------------------
415// RTTI
416// ----------------------------------------------------------------------------
417
418bool wxWindowBase::IsTopLevel() const
419{
420 return wxDynamicCast(this, wxFrame) || wxDynamicCast(this, wxDialog);
421}
f03fc89f
VZ
422
423// ----------------------------------------------------------------------------
424// reparenting the window
425// ----------------------------------------------------------------------------
426
427void wxWindowBase::AddChild(wxWindowBase *child)
428{
429 wxCHECK_RET( child, _T("can't add a NULL child") );
430
431 GetChildren().Append(child);
432 child->SetParent(this);
433}
434
435void wxWindowBase::RemoveChild(wxWindowBase *child)
436{
437 wxCHECK_RET( child, _T("can't remove a NULL child") );
438
439 GetChildren().DeleteObject(child);
440 child->SetParent((wxWindow *)NULL);
441}
439b3bf1 442
f03fc89f
VZ
443bool wxWindowBase::Reparent(wxWindowBase *newParent)
444{
445 wxWindow *oldParent = GetParent();
446 if ( newParent == oldParent )
447 {
448 // nothing done
449 return FALSE;
450 }
451
452 // unlink this window from the existing parent.
453 if ( oldParent )
454 {
455 oldParent->RemoveChild(this);
456 }
457 else
458 {
459 wxTopLevelWindows.DeleteObject(this);
460 }
461
462 // add it to the new one
463 if ( newParent )
464 {
465 newParent->AddChild(this);
466 }
467 else
468 {
469 wxTopLevelWindows.Append(this);
470 }
471
472 return TRUE;
473}
474
475// ----------------------------------------------------------------------------
476// event handler stuff
477// ----------------------------------------------------------------------------
478
479void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
480{
481 handler->SetNextHandler(GetEventHandler());
482 SetEventHandler(handler);
483}
484
485wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
486{
487 wxEvtHandler *handlerA = GetEventHandler();
488 if ( handlerA )
489 {
490 wxEvtHandler *handlerB = handlerA->GetNextHandler();
491 handlerA->SetNextHandler((wxEvtHandler *)NULL);
492 SetEventHandler(handlerB);
493 if ( deleteHandler )
494 {
495 delete handlerA;
496 handlerA = (wxEvtHandler *)NULL;
497 }
498 }
499
500 return handlerA;
501}
502
503// ----------------------------------------------------------------------------
504// cursors, fonts &c
505// ----------------------------------------------------------------------------
506
507bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
508{
509 if ( !colour.Ok() || (colour == m_backgroundColour) )
510 return FALSE;
511
512 m_backgroundColour = colour;
513
514 return TRUE;
515}
516
517bool wxWindowBase::SetForegroundColour( const wxColour &colour )
518{
519 if ( !colour.Ok() || (colour == m_foregroundColour) )
520 return FALSE;
521
522 m_foregroundColour = colour;
523
524 return TRUE;
525}
526
527bool wxWindowBase::SetCursor(const wxCursor& cursor)
528{
529 // don't try to set invalid cursor, always fall back to the default
530 const wxCursor& cursorOk = cursor.Ok() ? cursor : *wxSTANDARD_CURSOR;
531
532 if ( cursorOk == m_cursor )
533 {
534 // no change
535 return FALSE;
536 }
537
538 m_cursor = cursorOk;
539
540 return TRUE;
541}
542
543bool wxWindowBase::SetFont(const wxFont& font)
544{
545 // don't try to set invalid font, always fall back to the default
546 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
547
548 if ( fontOk == m_font )
549 {
550 // no change
551 return FALSE;
552 }
553
554 m_font = fontOk;
555
556 return TRUE;
557}
558
789295bf
VZ
559#if wxUSE_CARET
560void wxWindowBase::SetCaret(wxCaret *caret)
561{
562 if ( m_caret )
563 {
564 delete m_caret;
565 }
566
567 m_caret = caret;
568
569 if ( m_caret )
570 {
571 wxASSERT_MSG( m_caret->GetWindow() == this,
bdb9dffb 572 _T("caret should be created associated to this window") );
789295bf
VZ
573 }
574}
575#endif // wxUSE_CARET
576
88ac883a 577#if wxUSE_VALIDATORS
f03fc89f
VZ
578// ----------------------------------------------------------------------------
579// validators
580// ----------------------------------------------------------------------------
581
582void wxWindowBase::SetValidator(const wxValidator& validator)
583{
584 if ( m_windowValidator )
585 delete m_windowValidator;
586
587 m_windowValidator = (wxValidator *)validator.Clone();
588
589 if ( m_windowValidator )
590 m_windowValidator->SetWindow(this) ;
591}
88ac883a 592#endif // wxUSE_VALIDATORS
f03fc89f
VZ
593
594// ----------------------------------------------------------------------------
595// update region testing
596// ----------------------------------------------------------------------------
597
598bool wxWindowBase::IsExposed(int x, int y) const
599{
600 return m_updateRegion.Contains(x, y) != wxOutRegion;
601}
602
603bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
604{
605 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
606}
607
608// ----------------------------------------------------------------------------
609// find window by id or name
610// ----------------------------------------------------------------------------
611
612wxWindow *wxWindowBase::FindWindow( long id )
613{
614 if ( id == m_windowId )
615 return (wxWindow *)this;
616
617 wxWindowBase *res = (wxWindow *)NULL;
618 wxWindowList::Node *node;
619 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
620 {
621 wxWindowBase *child = node->GetData();
622 res = child->FindWindow( id );
623 }
624
625 return (wxWindow *)res;
626}
627
628wxWindow *wxWindowBase::FindWindow( const wxString& name )
629{
630 if ( name == m_windowName )
631 return (wxWindow *)this;
632
633 wxWindowBase *res = (wxWindow *)NULL;
634 wxWindowList::Node *node;
635 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
636 {
637 wxWindow *child = node->GetData();
638 res = child->FindWindow(name);
639 }
640
641 return (wxWindow *)res;
642}
643
644// ----------------------------------------------------------------------------
645// dialog oriented functions
646// ----------------------------------------------------------------------------
647
34636400 648void wxWindowBase::MakeModal(bool modal)
f03fc89f 649{
34636400
VZ
650 // Disable all other windows
651 if ( IsTopLevel() )
652 {
653 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
654 while (node)
655 {
656 wxWindow *win = node->GetData();
657 if (win != this)
658 win->Enable(!modal);
659
660 node = node->GetNext();
661 }
662 }
f03fc89f
VZ
663}
664
665bool wxWindowBase::Validate()
666{
88ac883a 667#if wxUSE_VALIDATORS
f03fc89f
VZ
668 wxWindowList::Node *node;
669 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
670 {
671 wxWindowBase *child = node->GetData();
672 wxValidator *validator = child->GetValidator();
dcd6b914 673 if ( validator && !validator->Validate((wxWindow *)this) )
f03fc89f
VZ
674 {
675 return FALSE;
676 }
677 }
88ac883a 678#endif // wxUSE_VALIDATORS
f03fc89f
VZ
679
680 return TRUE;
681}
682
683bool wxWindowBase::TransferDataToWindow()
684{
88ac883a 685#if wxUSE_VALIDATORS
f03fc89f
VZ
686 wxWindowList::Node *node;
687 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
688 {
689 wxWindowBase *child = node->GetData();
690 wxValidator *validator = child->GetValidator();
691 if ( validator && !validator->TransferToWindow() )
692 {
693 wxLog *log = wxLog::GetActiveTarget();
694 if ( log )
695 {
696 wxLogWarning(_("Could not transfer data to window"));
697 log->Flush();
698 }
699
700 return FALSE;
701 }
702 }
88ac883a 703#endif // wxUSE_VALIDATORS
f03fc89f
VZ
704
705 return TRUE;
706}
707
708bool wxWindowBase::TransferDataFromWindow()
709{
88ac883a 710#if wxUSE_VALIDATORS
f03fc89f
VZ
711 wxWindowList::Node *node;
712 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
713 {
714 wxWindow *child = node->GetData();
715 if ( child->GetValidator() &&
716 !child->GetValidator()->TransferFromWindow() )
717 {
718 return FALSE;
719 }
720 }
88ac883a 721#endif // wxUSE_VALIDATORS
f03fc89f
VZ
722
723 return TRUE;
724}
725
726void wxWindowBase::InitDialog()
727{
728 wxInitDialogEvent event(GetId());
729 event.SetEventObject( this );
730 GetEventHandler()->ProcessEvent(event);
731}
732
733// ----------------------------------------------------------------------------
734// tooltips
735// ----------------------------------------------------------------------------
736
737#if wxUSE_TOOLTIPS
738
739void wxWindowBase::SetToolTip( const wxString &tip )
740{
741 // don't create the new tooltip if we already have one
742 if ( m_tooltip )
743 {
744 m_tooltip->SetTip( tip );
745 }
746 else
747 {
748 SetToolTip( new wxToolTip( tip ) );
749 }
750
751 // setting empty tooltip text does not remove the tooltip any more - use
752 // SetToolTip((wxToolTip *)NULL) for this
753}
754
755void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
756{
757 if ( m_tooltip )
758 delete m_tooltip;
759
760 m_tooltip = tooltip;
761}
762
763#endif // wxUSE_TOOLTIPS
764
765// ----------------------------------------------------------------------------
766// constraints and sizers
767// ----------------------------------------------------------------------------
768
769#if wxUSE_CONSTRAINTS
770
771void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
772{
773 if ( m_constraints )
774 {
775 UnsetConstraints(m_constraints);
776 delete m_constraints;
777 }
778 m_constraints = constraints;
779 if ( m_constraints )
780 {
781 // Make sure other windows know they're part of a 'meaningful relationship'
782 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
783 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
784 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
785 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
786 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
787 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
788 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
789 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
790 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
791 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
792 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
793 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
794 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
795 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
796 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
797 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
798 }
799}
800
801// This removes any dangling pointers to this window in other windows'
802// constraintsInvolvedIn lists.
803void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
804{
805 if ( c )
806 {
807 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
808 c->left.GetOtherWindow()->RemoveConstraintReference(this);
809 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
810 c->top.GetOtherWindow()->RemoveConstraintReference(this);
811 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
812 c->right.GetOtherWindow()->RemoveConstraintReference(this);
813 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
814 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
815 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
816 c->width.GetOtherWindow()->RemoveConstraintReference(this);
817 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
818 c->height.GetOtherWindow()->RemoveConstraintReference(this);
819 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
820 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
821 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
822 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
823 }
824}
825
826// Back-pointer to other windows we're involved with, so if we delete this
827// window, we must delete any constraints we're involved with.
828void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
829{
830 if ( !m_constraintsInvolvedIn )
831 m_constraintsInvolvedIn = new wxWindowList;
832 if ( !m_constraintsInvolvedIn->Find(otherWin) )
833 m_constraintsInvolvedIn->Append(otherWin);
834}
835
836// REMOVE back-pointer to other windows we're involved with.
837void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
838{
839 if ( m_constraintsInvolvedIn )
840 m_constraintsInvolvedIn->DeleteObject(otherWin);
841}
842
843// Reset any constraints that mention this window
844void wxWindowBase::DeleteRelatedConstraints()
845{
846 if ( m_constraintsInvolvedIn )
847 {
848 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
849 while (node)
850 {
851 wxWindow *win = node->GetData();
852 wxLayoutConstraints *constr = win->GetConstraints();
853
854 // Reset any constraints involving this window
855 if ( constr )
856 {
857 constr->left.ResetIfWin(this);
858 constr->top.ResetIfWin(this);
859 constr->right.ResetIfWin(this);
860 constr->bottom.ResetIfWin(this);
861 constr->width.ResetIfWin(this);
862 constr->height.ResetIfWin(this);
863 constr->centreX.ResetIfWin(this);
864 constr->centreY.ResetIfWin(this);
865 }
866
867 wxWindowList::Node *next = node->GetNext();
868 delete node;
869 node = next;
870 }
871
872 delete m_constraintsInvolvedIn;
873 m_constraintsInvolvedIn = (wxWindowList *) NULL;
874 }
875}
876
877void wxWindowBase::SetSizer(wxSizer *sizer)
878{
879 m_windowSizer = sizer;
880 if ( sizer )
881 sizer->SetSizerParent(this);
882}
883
884bool wxWindowBase::Layout()
885{
886 if ( GetConstraints() )
887 {
888 int w, h;
889 GetClientSize(&w, &h);
890 GetConstraints()->width.SetValue(w);
891 GetConstraints()->height.SetValue(h);
892 }
893
894 // If top level (one sizer), evaluate the sizer's constraints.
895 if ( GetSizer() )
896 {
897 int noChanges;
898 GetSizer()->ResetConstraints(); // Mark all constraints as unevaluated
899 GetSizer()->LayoutPhase1(&noChanges);
900 GetSizer()->LayoutPhase2(&noChanges);
901 GetSizer()->SetConstraintSizes(); // Recursively set the real window sizes
902 return TRUE;
903 }
904 else
905 {
906 // Otherwise, evaluate child constraints
907 ResetConstraints(); // Mark all constraints as unevaluated
908 DoPhase(1); // Just one phase need if no sizers involved
909 DoPhase(2);
910 SetConstraintSizes(); // Recursively set the real window sizes
911 }
912 return TRUE;
913}
914
915
916// Do a phase of evaluating constraints: the default behaviour. wxSizers may
917// do a similar thing, but also impose their own 'constraints' and order the
918// evaluation differently.
919bool wxWindowBase::LayoutPhase1(int *noChanges)
920{
921 wxLayoutConstraints *constr = GetConstraints();
922 if ( constr )
923 {
924 return constr->SatisfyConstraints(this, noChanges);
925 }
926 else
927 return TRUE;
928}
929
930bool wxWindowBase::LayoutPhase2(int *noChanges)
931{
932 *noChanges = 0;
933
934 // Layout children
935 DoPhase(1);
936 DoPhase(2);
937 return TRUE;
938}
939
940// Do a phase of evaluating child constraints
941bool wxWindowBase::DoPhase(int phase)
942{
943 int noIterations = 0;
944 int maxIterations = 500;
945 int noChanges = 1;
946 int noFailures = 0;
947 wxWindowList succeeded;
948 while ((noChanges > 0) && (noIterations < maxIterations))
949 {
950 noChanges = 0;
951 noFailures = 0;
952 wxWindowList::Node *node = GetChildren().GetFirst();
953 while (node)
954 {
955 wxWindow *child = node->GetData();
34636400 956 if ( !child->IsTopLevel() )
f03fc89f
VZ
957 {
958 wxLayoutConstraints *constr = child->GetConstraints();
959 if ( constr )
960 {
961 if ( !succeeded.Find(child) )
962 {
963 int tempNoChanges = 0;
964 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
965 noChanges += tempNoChanges;
966 if ( success )
967 {
968 succeeded.Append(child);
969 }
970 }
971 }
972 }
973 node = node->GetNext();
974 }
975
976 noIterations++;
977 }
978
979 return TRUE;
980}
981
982void wxWindowBase::ResetConstraints()
983{
984 wxLayoutConstraints *constr = GetConstraints();
985 if ( constr )
986 {
987 constr->left.SetDone(FALSE);
988 constr->top.SetDone(FALSE);
989 constr->right.SetDone(FALSE);
990 constr->bottom.SetDone(FALSE);
991 constr->width.SetDone(FALSE);
992 constr->height.SetDone(FALSE);
993 constr->centreX.SetDone(FALSE);
994 constr->centreY.SetDone(FALSE);
995 }
996 wxWindowList::Node *node = GetChildren().GetFirst();
997 while (node)
998 {
999 wxWindow *win = node->GetData();
34636400 1000 if ( !win->IsTopLevel() )
f03fc89f
VZ
1001 win->ResetConstraints();
1002 node = node->GetNext();
1003 }
1004}
1005
1006// Need to distinguish between setting the 'fake' size for windows and sizers,
1007// and setting the real values.
1008void wxWindowBase::SetConstraintSizes(bool recurse)
1009{
1010 wxLayoutConstraints *constr = GetConstraints();
1011 if ( constr && constr->left.GetDone() && constr->right.GetDone( ) &&
1012 constr->width.GetDone() && constr->height.GetDone())
1013 {
1014 int x = constr->left.GetValue();
1015 int y = constr->top.GetValue();
1016 int w = constr->width.GetValue();
1017 int h = constr->height.GetValue();
1018
1019 // If we don't want to resize this window, just move it...
1020 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1021 (constr->height.GetRelationship() != wxAsIs))
1022 {
1023 // Calls Layout() recursively. AAAGH. How can we stop that.
1024 // Simply take Layout() out of non-top level OnSizes.
1025 SizerSetSize(x, y, w, h);
1026 }
1027 else
1028 {
1029 SizerMove(x, y);
1030 }
1031 }
1032 else if ( constr )
1033 {
1034 wxChar *windowClass = GetClassInfo()->GetClassName();
1035
1036 wxString winName;
1037 if ( GetName() == _T("") )
1038 winName = _T("unnamed");
1039 else
1040 winName = GetName();
1041 wxLogDebug( _T("Constraint(s) not satisfied for window of type %s, name %s:\n"),
1042 (const wxChar *)windowClass,
1043 (const wxChar *)winName);
1044 if ( !constr->left.GetDone()) wxLogDebug( _T(" unsatisfied 'left' constraint.\n") );
1045 if ( !constr->right.GetDone()) wxLogDebug( _T(" unsatisfied 'right' constraint.\n") );
1046 if ( !constr->width.GetDone()) wxLogDebug( _T(" unsatisfied 'width' constraint.\n") );
1047 if ( !constr->height.GetDone()) wxLogDebug( _T(" unsatisfied 'height' constraint.\n") );
1048 wxLogDebug( _T("Please check constraints: try adding AsIs() constraints.\n") );
1049 }
1050
1051 if ( recurse )
1052 {
1053 wxWindowList::Node *node = GetChildren().GetFirst();
1054 while (node)
1055 {
1056 wxWindow *win = node->GetData();
34636400 1057 if ( !win->IsTopLevel() )
f03fc89f
VZ
1058 win->SetConstraintSizes();
1059 node = node->GetNext();
1060 }
1061 }
1062}
1063
1064// This assumes that all sizers are 'on' the same window, i.e. the parent of
1065// this window.
1066void wxWindowBase::TransformSizerToActual(int *x, int *y) const
1067{
34636400 1068 if ( !m_sizerParent || m_sizerParent->IsTopLevel() )
f03fc89f
VZ
1069 return;
1070
1071 int xp, yp;
1072 m_sizerParent->GetPosition(&xp, &yp);
1073 m_sizerParent->TransformSizerToActual(&xp, &yp);
1074 *x += xp;
1075 *y += yp;
1076}
1077
1078void wxWindowBase::SizerSetSize(int x, int y, int w, int h)
1079{
1080 int xx = x;
1081 int yy = y;
1082 TransformSizerToActual(&xx, &yy);
1083 SetSize(xx, yy, w, h);
1084}
1085
1086void wxWindowBase::SizerMove(int x, int y)
1087{
1088 int xx = x;
1089 int yy = y;
1090 TransformSizerToActual(&xx, &yy);
1091 Move(xx, yy);
1092}
1093
1094// Only set the size/position of the constraint (if any)
1095void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1096{
1097 wxLayoutConstraints *constr = GetConstraints();
1098 if ( constr )
1099 {
1100 if ( x != -1 )
1101 {
1102 constr->left.SetValue(x);
1103 constr->left.SetDone(TRUE);
1104 }
1105 if ( y != -1 )
1106 {
1107 constr->top.SetValue(y);
1108 constr->top.SetDone(TRUE);
1109 }
1110 if ( w != -1 )
1111 {
1112 constr->width.SetValue(w);
1113 constr->width.SetDone(TRUE);
1114 }
1115 if ( h != -1 )
1116 {
1117 constr->height.SetValue(h);
1118 constr->height.SetDone(TRUE);
1119 }
1120 }
1121}
1122
1123void wxWindowBase::MoveConstraint(int x, int y)
1124{
1125 wxLayoutConstraints *constr = GetConstraints();
1126 if ( constr )
1127 {
1128 if ( x != -1 )
1129 {
1130 constr->left.SetValue(x);
1131 constr->left.SetDone(TRUE);
1132 }
1133 if ( y != -1 )
1134 {
1135 constr->top.SetValue(y);
1136 constr->top.SetDone(TRUE);
1137 }
1138 }
1139}
1140
1141void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1142{
1143 wxLayoutConstraints *constr = GetConstraints();
1144 if ( constr )
1145 {
1146 *w = constr->width.GetValue();
1147 *h = constr->height.GetValue();
1148 }
1149 else
1150 GetSize(w, h);
1151}
1152
1153void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1154{
1155 wxLayoutConstraints *constr = GetConstraints();
1156 if ( constr )
1157 {
1158 *w = constr->width.GetValue();
1159 *h = constr->height.GetValue();
1160 }
1161 else
1162 GetClientSize(w, h);
1163}
1164
1165void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1166{
1167 wxLayoutConstraints *constr = GetConstraints();
1168 if ( constr )
1169 {
1170 *x = constr->left.GetValue();
1171 *y = constr->top.GetValue();
1172 }
1173 else
1174 GetPosition(x, y);
1175}
1176
1177#endif // wxUSE_CONSTRAINTS
1178
1179// ----------------------------------------------------------------------------
1180// do Update UI processing for child controls
1181// ----------------------------------------------------------------------------
7ec1983b
VZ
1182
1183// TODO: should this be implemented for the child window rather
1184// than the parent? Then you can override it e.g. for wxCheckBox
1185// to do the Right Thing rather than having to assume a fixed number
1186// of control classes.
f03fc89f 1187void wxWindowBase::UpdateWindowUI()
7ec1983b 1188{
f03fc89f
VZ
1189 wxWindowID id = GetId();
1190 if ( id > 0 )
7ec1983b 1191 {
f03fc89f
VZ
1192 wxUpdateUIEvent event(id);
1193 event.m_eventObject = this;
7ec1983b 1194
f03fc89f
VZ
1195 if ( GetEventHandler()->ProcessEvent(event) )
1196 {
1197 if ( event.GetSetEnabled() )
1198 Enable(event.GetEnabled());
7ec1983b 1199
34636400
VZ
1200 if ( event.GetSetText() )
1201 {
1202 wxControl *control = wxDynamicCast(this, wxControl);
1203 if ( control )
1204 control->SetLabel(event.GetText());
1205 }
f03fc89f 1206
88ac883a 1207#if wxUSE_CHECKBOX
34636400
VZ
1208 wxCheckBox *checkbox = wxDynamicCast(this, wxCheckBox);
1209 if ( checkbox )
f03fc89f
VZ
1210 {
1211 if ( event.GetSetChecked() )
34636400 1212 checkbox->SetValue(event.GetChecked());
f03fc89f 1213 }
88ac883a
VZ
1214#endif // wxUSE_CHECKBOX
1215
1216#if wxUSE_RADIOBUTTON
34636400
VZ
1217 wxRadioButton *radiobtn = wxDynamicCast(this, wxRadioButton);
1218 if ( radiobtn )
f03fc89f
VZ
1219 {
1220 if ( event.GetSetChecked() )
34636400 1221 radiobtn->SetValue(event.GetChecked());
f03fc89f 1222 }
88ac883a 1223#endif // wxUSE_RADIOBUTTON
f03fc89f 1224 }
7ec1983b 1225 }
7ec1983b 1226}
fd71308f 1227
f03fc89f
VZ
1228// ----------------------------------------------------------------------------
1229// dialog units translations
1230// ----------------------------------------------------------------------------
1231
1232wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
fd71308f
JS
1233{
1234 int charWidth = GetCharWidth();
1235 int charHeight = GetCharHeight();
5d9c2818
RD
1236 wxPoint pt2(-1, -1);
1237 if (pt.x != -1)
1238 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1239 if (pt.y != -1)
1240 pt2.y = (int) ((pt.y * 8) / charHeight) ;
fd71308f
JS
1241
1242 return pt2;
1243}
1244
f03fc89f 1245wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
fd71308f
JS
1246{
1247 int charWidth = GetCharWidth();
1248 int charHeight = GetCharHeight();
5d9c2818
RD
1249 wxPoint pt2(-1, -1);
1250 if (pt.x != -1)
1251 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1252 if (pt.y != -1)
1253 pt2.y = (int) ((pt.y * charHeight) / 8) ;
fd71308f
JS
1254
1255 return pt2;
1256}
1257
f03fc89f
VZ
1258// ----------------------------------------------------------------------------
1259// event handlers
1260// ----------------------------------------------------------------------------
1261
1262// propagate the colour change event to the subwindows
1263void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1264{
1265 wxWindowList::Node *node = GetChildren().GetFirst();
1266 while ( node )
1267 {
1268 // Only propagate to non-top-level windows
1269 wxWindow *win = node->GetData();
1270 if ( !win->IsTopLevel() )
1271 {
1272 wxSysColourChangedEvent event2;
1273 event.m_eventObject = win;
1274 win->GetEventHandler()->ProcessEvent(event2);
1275 }
1276
1277 node = node->GetNext();
1278 }
1279}
1280
1281// the default action is to populate dialog with data when it's created
1282void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1283{
1284 TransferDataToWindow();
1285}
1286
1287// ----------------------------------------------------------------------------
1288// list classes implementation
1289// ----------------------------------------------------------------------------
1290
1291void wxWindowListNode::DeleteData()
1292{
1293 delete (wxWindow *)GetData();
1294}
1295