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