]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
wxWindow::GetBestSize() added
[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 // size/position related methods
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 if ( GetChildren().GetCount() > 0 )
365 {
366 SetClientSize(DoGetBestSize());
367 }
368 //else: do nothing if we have no children
369 }
370
371 // return the size best suited for the current window
372 wxSize wxWindowBase::DoGetBestSize() const
373 {
374 if ( GetChildren().GetCount() > 0 )
375 {
376 // our minimal acceptable size is such that all our windows fit inside
377 int maxX = 0,
378 maxY = 0;
379
380 for ( wxWindowList::Node *node = GetChildren().GetFirst();
381 node;
382 node = node->GetNext() )
383 {
384 wxWindow *win = node->GetData();
385 if ( win->IsTopLevel() )
386 {
387 // dialogs and frames lie in different top level windows -
388 // don't deal with them here
389 continue;
390 }
391
392 int wx, wy, ww, wh;
393 win->GetPosition(&wx, &wy);
394 win->GetSize(&ww, &wh);
395 if ( wx + ww > maxX )
396 maxX = wx + ww;
397 if ( wy + wh > maxY )
398 maxY = wy + wh;
399 }
400
401 // leave a margin
402 return wxSize(maxX + 7, maxY + 14);
403 }
404 else
405 {
406 // for a generic window there is no natural best size - just use the
407 // current one
408 return GetSize();
409 }
410 }
411
412 // set the min/max size of the window
413 void wxWindowBase::SetSizeHints(int minW, int minH,
414 int maxW, int maxH,
415 int WXUNUSED(incW), int WXUNUSED(incH))
416 {
417 m_minWidth = minW;
418 m_maxWidth = maxW;
419 m_minHeight = minH;
420 m_maxHeight = maxH;
421 }
422
423 // ----------------------------------------------------------------------------
424 // show/hide/enable/disable the window
425 // ----------------------------------------------------------------------------
426
427 bool wxWindowBase::Show(bool show)
428 {
429 if ( show != m_isShown )
430 {
431 m_isShown = show;
432
433 return TRUE;
434 }
435 else
436 {
437 return FALSE;
438 }
439 }
440
441 bool wxWindowBase::Enable(bool enable)
442 {
443 if ( enable != m_isEnabled )
444 {
445 m_isEnabled = enable;
446
447 return TRUE;
448 }
449 else
450 {
451 return FALSE;
452 }
453 }
454 // ----------------------------------------------------------------------------
455 // RTTI
456 // ----------------------------------------------------------------------------
457
458 bool wxWindowBase::IsTopLevel() const
459 {
460 return FALSE;
461 }
462
463 // ----------------------------------------------------------------------------
464 // reparenting the window
465 // ----------------------------------------------------------------------------
466
467 void wxWindowBase::AddChild(wxWindowBase *child)
468 {
469 wxCHECK_RET( child, wxT("can't add a NULL child") );
470
471 GetChildren().Append(child);
472 child->SetParent(this);
473 }
474
475 void wxWindowBase::RemoveChild(wxWindowBase *child)
476 {
477 wxCHECK_RET( child, wxT("can't remove a NULL child") );
478
479 GetChildren().DeleteObject(child);
480 child->SetParent((wxWindow *)NULL);
481 }
482
483 bool wxWindowBase::Reparent(wxWindowBase *newParent)
484 {
485 wxWindow *oldParent = GetParent();
486 if ( newParent == oldParent )
487 {
488 // nothing done
489 return FALSE;
490 }
491
492 // unlink this window from the existing parent.
493 if ( oldParent )
494 {
495 oldParent->RemoveChild(this);
496 }
497 else
498 {
499 wxTopLevelWindows.DeleteObject(this);
500 }
501
502 // add it to the new one
503 if ( newParent )
504 {
505 newParent->AddChild(this);
506 }
507 else
508 {
509 wxTopLevelWindows.Append(this);
510 }
511
512 return TRUE;
513 }
514
515 // ----------------------------------------------------------------------------
516 // event handler stuff
517 // ----------------------------------------------------------------------------
518
519 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
520 {
521 handler->SetNextHandler(GetEventHandler());
522 SetEventHandler(handler);
523 }
524
525 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
526 {
527 wxEvtHandler *handlerA = GetEventHandler();
528 if ( handlerA )
529 {
530 wxEvtHandler *handlerB = handlerA->GetNextHandler();
531 handlerA->SetNextHandler((wxEvtHandler *)NULL);
532 SetEventHandler(handlerB);
533 if ( deleteHandler )
534 {
535 delete handlerA;
536 handlerA = (wxEvtHandler *)NULL;
537 }
538 }
539
540 return handlerA;
541 }
542
543 // ----------------------------------------------------------------------------
544 // cursors, fonts &c
545 // ----------------------------------------------------------------------------
546
547 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
548 {
549 if ( !colour.Ok() || (colour == m_backgroundColour) )
550 return FALSE;
551
552 m_backgroundColour = colour;
553
554 return TRUE;
555 }
556
557 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
558 {
559 if ( !colour.Ok() || (colour == m_foregroundColour) )
560 return FALSE;
561
562 m_foregroundColour = colour;
563
564 return TRUE;
565 }
566
567 bool wxWindowBase::SetCursor(const wxCursor& cursor)
568 {
569 // don't try to set invalid cursor, always fall back to the default
570 const wxCursor& cursorOk = cursor.Ok() ? cursor : *wxSTANDARD_CURSOR;
571
572 if ( (wxCursor&)cursorOk == m_cursor )
573 {
574 // no change
575 return FALSE;
576 }
577
578 m_cursor = cursorOk;
579
580 return TRUE;
581 }
582
583 bool wxWindowBase::SetFont(const wxFont& font)
584 {
585 // don't try to set invalid font, always fall back to the default
586 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
587
588 if ( (wxFont&)fontOk == m_font )
589 {
590 // no change
591 return FALSE;
592 }
593
594 m_font = fontOk;
595
596 return TRUE;
597 }
598
599 #if wxUSE_CARET
600 void wxWindowBase::SetCaret(wxCaret *caret)
601 {
602 if ( m_caret )
603 {
604 delete m_caret;
605 }
606
607 m_caret = caret;
608
609 if ( m_caret )
610 {
611 wxASSERT_MSG( m_caret->GetWindow() == this,
612 wxT("caret should be created associated to this window") );
613 }
614 }
615 #endif // wxUSE_CARET
616
617 #if wxUSE_VALIDATORS
618 // ----------------------------------------------------------------------------
619 // validators
620 // ----------------------------------------------------------------------------
621
622 # if defined(__VISAGECPP__)
623 void wxWindowBase::SetValidator(const wxValidator* validator)
624 {
625 if ( m_windowValidator )
626 delete m_windowValidator;
627
628 m_windowValidator = (wxValidator *)validator->Clone();
629
630 if ( m_windowValidator )
631 m_windowValidator->SetWindow(this) ;
632 }
633 # else
634 void wxWindowBase::SetValidator(const wxValidator& validator)
635 {
636 if ( m_windowValidator )
637 delete m_windowValidator;
638
639 m_windowValidator = (wxValidator *)validator.Clone();
640
641 if ( m_windowValidator )
642 m_windowValidator->SetWindow(this) ;
643 }
644 # endif // __VISAGECPP__
645 #endif // wxUSE_VALIDATORS
646
647 // ----------------------------------------------------------------------------
648 // update region testing
649 // ----------------------------------------------------------------------------
650
651 bool wxWindowBase::IsExposed(int x, int y) const
652 {
653 return m_updateRegion.Contains(x, y) != wxOutRegion;
654 }
655
656 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
657 {
658 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
659 }
660
661 // ----------------------------------------------------------------------------
662 // find window by id or name
663 // ----------------------------------------------------------------------------
664
665 wxWindow *wxWindowBase::FindWindow( long id )
666 {
667 if ( id == m_windowId )
668 return (wxWindow *)this;
669
670 wxWindowBase *res = (wxWindow *)NULL;
671 wxWindowList::Node *node;
672 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
673 {
674 wxWindowBase *child = node->GetData();
675 res = child->FindWindow( id );
676 }
677
678 return (wxWindow *)res;
679 }
680
681 wxWindow *wxWindowBase::FindWindow( const wxString& name )
682 {
683 if ( name == m_windowName )
684 return (wxWindow *)this;
685
686 wxWindowBase *res = (wxWindow *)NULL;
687 wxWindowList::Node *node;
688 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
689 {
690 wxWindow *child = node->GetData();
691 res = child->FindWindow(name);
692 }
693
694 return (wxWindow *)res;
695 }
696
697 // ----------------------------------------------------------------------------
698 // dialog oriented functions
699 // ----------------------------------------------------------------------------
700
701 void wxWindowBase::MakeModal(bool modal)
702 {
703 // Disable all other windows
704 if ( IsTopLevel() )
705 {
706 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
707 while (node)
708 {
709 wxWindow *win = node->GetData();
710 if (win != this)
711 win->Enable(!modal);
712
713 node = node->GetNext();
714 }
715 }
716 }
717
718 bool wxWindowBase::Validate()
719 {
720 #if wxUSE_VALIDATORS
721 wxWindowList::Node *node;
722 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
723 {
724 wxWindowBase *child = node->GetData();
725 wxValidator *validator = child->GetValidator();
726 if ( validator && !validator->Validate((wxWindow *)this) )
727 {
728 return FALSE;
729 }
730 }
731 #endif // wxUSE_VALIDATORS
732
733 return TRUE;
734 }
735
736 bool wxWindowBase::TransferDataToWindow()
737 {
738 #if wxUSE_VALIDATORS
739 wxWindowList::Node *node;
740 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
741 {
742 wxWindowBase *child = node->GetData();
743 wxValidator *validator = child->GetValidator();
744 if ( validator && !validator->TransferToWindow() )
745 {
746 wxLog *log = wxLog::GetActiveTarget();
747 if ( log )
748 {
749 wxLogWarning(_("Could not transfer data to window"));
750 log->Flush();
751 }
752
753 return FALSE;
754 }
755 }
756 #endif // wxUSE_VALIDATORS
757
758 return TRUE;
759 }
760
761 bool wxWindowBase::TransferDataFromWindow()
762 {
763 #if wxUSE_VALIDATORS
764 wxWindowList::Node *node;
765 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
766 {
767 wxWindow *child = node->GetData();
768 if ( child->GetValidator() &&
769 !child->GetValidator()->TransferFromWindow() )
770 {
771 return FALSE;
772 }
773 }
774 #endif // wxUSE_VALIDATORS
775
776 return TRUE;
777 }
778
779 void wxWindowBase::InitDialog()
780 {
781 wxInitDialogEvent event(GetId());
782 event.SetEventObject( this );
783 GetEventHandler()->ProcessEvent(event);
784 }
785
786 // ----------------------------------------------------------------------------
787 // tooltips
788 // ----------------------------------------------------------------------------
789
790 #if wxUSE_TOOLTIPS
791
792 void wxWindowBase::SetToolTip( const wxString &tip )
793 {
794 // don't create the new tooltip if we already have one
795 if ( m_tooltip )
796 {
797 m_tooltip->SetTip( tip );
798 }
799 else
800 {
801 SetToolTip( new wxToolTip( tip ) );
802 }
803
804 // setting empty tooltip text does not remove the tooltip any more - use
805 // SetToolTip((wxToolTip *)NULL) for this
806 }
807
808 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
809 {
810 if ( m_tooltip )
811 delete m_tooltip;
812
813 m_tooltip = tooltip;
814 }
815
816 #endif // wxUSE_TOOLTIPS
817
818 // ----------------------------------------------------------------------------
819 // constraints and sizers
820 // ----------------------------------------------------------------------------
821
822 #if wxUSE_CONSTRAINTS
823
824 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
825 {
826 if ( m_constraints )
827 {
828 UnsetConstraints(m_constraints);
829 delete m_constraints;
830 }
831 m_constraints = constraints;
832 if ( m_constraints )
833 {
834 // Make sure other windows know they're part of a 'meaningful relationship'
835 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
836 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
837 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
838 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
839 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
840 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
841 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
842 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
843 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
844 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
845 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
846 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
847 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
848 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
849 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
850 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
851 }
852 }
853
854 // This removes any dangling pointers to this window in other windows'
855 // constraintsInvolvedIn lists.
856 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
857 {
858 if ( c )
859 {
860 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
861 c->left.GetOtherWindow()->RemoveConstraintReference(this);
862 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
863 c->top.GetOtherWindow()->RemoveConstraintReference(this);
864 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
865 c->right.GetOtherWindow()->RemoveConstraintReference(this);
866 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
867 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
868 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
869 c->width.GetOtherWindow()->RemoveConstraintReference(this);
870 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
871 c->height.GetOtherWindow()->RemoveConstraintReference(this);
872 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
873 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
874 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
875 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
876 }
877 }
878
879 // Back-pointer to other windows we're involved with, so if we delete this
880 // window, we must delete any constraints we're involved with.
881 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
882 {
883 if ( !m_constraintsInvolvedIn )
884 m_constraintsInvolvedIn = new wxWindowList;
885 if ( !m_constraintsInvolvedIn->Find(otherWin) )
886 m_constraintsInvolvedIn->Append(otherWin);
887 }
888
889 // REMOVE back-pointer to other windows we're involved with.
890 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
891 {
892 if ( m_constraintsInvolvedIn )
893 m_constraintsInvolvedIn->DeleteObject(otherWin);
894 }
895
896 // Reset any constraints that mention this window
897 void wxWindowBase::DeleteRelatedConstraints()
898 {
899 if ( m_constraintsInvolvedIn )
900 {
901 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
902 while (node)
903 {
904 wxWindow *win = node->GetData();
905 wxLayoutConstraints *constr = win->GetConstraints();
906
907 // Reset any constraints involving this window
908 if ( constr )
909 {
910 constr->left.ResetIfWin(this);
911 constr->top.ResetIfWin(this);
912 constr->right.ResetIfWin(this);
913 constr->bottom.ResetIfWin(this);
914 constr->width.ResetIfWin(this);
915 constr->height.ResetIfWin(this);
916 constr->centreX.ResetIfWin(this);
917 constr->centreY.ResetIfWin(this);
918 }
919
920 wxWindowList::Node *next = node->GetNext();
921 delete node;
922 node = next;
923 }
924
925 delete m_constraintsInvolvedIn;
926 m_constraintsInvolvedIn = (wxWindowList *) NULL;
927 }
928 }
929
930 void wxWindowBase::SetSizer(wxSizer *sizer)
931 {
932 if (m_windowSizer) delete m_windowSizer;
933
934 m_windowSizer = sizer;
935 }
936
937 bool wxWindowBase::Layout()
938 {
939 int w, h;
940 GetClientSize(&w, &h);
941
942 // If there is a sizer, use it instead of the constraints
943 if ( GetSizer() )
944 {
945 GetSizer()->SetDimension( 0, 0, w, h );
946 return TRUE;
947 }
948
949 if ( GetConstraints() )
950 {
951 GetConstraints()->width.SetValue(w);
952 GetConstraints()->height.SetValue(h);
953 }
954
955 // Evaluate child constraints
956 ResetConstraints(); // Mark all constraints as unevaluated
957 DoPhase(1); // Just one phase need if no sizers involved
958 DoPhase(2);
959 SetConstraintSizes(); // Recursively set the real window sizes
960
961 return TRUE;
962 }
963
964
965 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
966 // do a similar thing, but also impose their own 'constraints' and order the
967 // evaluation differently.
968 bool wxWindowBase::LayoutPhase1(int *noChanges)
969 {
970 wxLayoutConstraints *constr = GetConstraints();
971 if ( constr )
972 {
973 return constr->SatisfyConstraints(this, noChanges);
974 }
975 else
976 return TRUE;
977 }
978
979 bool wxWindowBase::LayoutPhase2(int *noChanges)
980 {
981 *noChanges = 0;
982
983 // Layout children
984 DoPhase(1);
985 DoPhase(2);
986 return TRUE;
987 }
988
989 // Do a phase of evaluating child constraints
990 bool wxWindowBase::DoPhase(int phase)
991 {
992 int noIterations = 0;
993 int maxIterations = 500;
994 int noChanges = 1;
995 int noFailures = 0;
996 wxWindowList succeeded;
997 while ((noChanges > 0) && (noIterations < maxIterations))
998 {
999 noChanges = 0;
1000 noFailures = 0;
1001 wxWindowList::Node *node = GetChildren().GetFirst();
1002 while (node)
1003 {
1004 wxWindow *child = node->GetData();
1005 if ( !child->IsTopLevel() )
1006 {
1007 wxLayoutConstraints *constr = child->GetConstraints();
1008 if ( constr )
1009 {
1010 if ( !succeeded.Find(child) )
1011 {
1012 int tempNoChanges = 0;
1013 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
1014 noChanges += tempNoChanges;
1015 if ( success )
1016 {
1017 succeeded.Append(child);
1018 }
1019 }
1020 }
1021 }
1022 node = node->GetNext();
1023 }
1024
1025 noIterations++;
1026 }
1027
1028 return TRUE;
1029 }
1030
1031 void wxWindowBase::ResetConstraints()
1032 {
1033 wxLayoutConstraints *constr = GetConstraints();
1034 if ( constr )
1035 {
1036 constr->left.SetDone(FALSE);
1037 constr->top.SetDone(FALSE);
1038 constr->right.SetDone(FALSE);
1039 constr->bottom.SetDone(FALSE);
1040 constr->width.SetDone(FALSE);
1041 constr->height.SetDone(FALSE);
1042 constr->centreX.SetDone(FALSE);
1043 constr->centreY.SetDone(FALSE);
1044 }
1045 wxWindowList::Node *node = GetChildren().GetFirst();
1046 while (node)
1047 {
1048 wxWindow *win = node->GetData();
1049 if ( !win->IsTopLevel() )
1050 win->ResetConstraints();
1051 node = node->GetNext();
1052 }
1053 }
1054
1055 // Need to distinguish between setting the 'fake' size for windows and sizers,
1056 // and setting the real values.
1057 void wxWindowBase::SetConstraintSizes(bool recurse)
1058 {
1059 wxLayoutConstraints *constr = GetConstraints();
1060 if ( constr && constr->left.GetDone() && constr->right.GetDone( ) &&
1061 constr->width.GetDone() && constr->height.GetDone())
1062 {
1063 int x = constr->left.GetValue();
1064 int y = constr->top.GetValue();
1065 int w = constr->width.GetValue();
1066 int h = constr->height.GetValue();
1067
1068 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1069 (constr->height.GetRelationship() != wxAsIs) )
1070 {
1071 SetSize(x, y, w, h);
1072 }
1073 else
1074 {
1075 // If we don't want to resize this window, just move it...
1076 Move(x, y);
1077 }
1078 }
1079 else if ( constr )
1080 {
1081 wxChar *windowClass = GetClassInfo()->GetClassName();
1082
1083 wxString winName;
1084 if ( GetName() == wxT("") )
1085 winName = wxT("unnamed");
1086 else
1087 winName = GetName();
1088 wxLogDebug( wxT("Constraint(s) not satisfied for window of type %s, name %s:\n"),
1089 (const wxChar *)windowClass,
1090 (const wxChar *)winName);
1091 if ( !constr->left.GetDone()) wxLogDebug( wxT(" unsatisfied 'left' constraint.\n") );
1092 if ( !constr->right.GetDone()) wxLogDebug( wxT(" unsatisfied 'right' constraint.\n") );
1093 if ( !constr->width.GetDone()) wxLogDebug( wxT(" unsatisfied 'width' constraint.\n") );
1094 if ( !constr->height.GetDone()) wxLogDebug( wxT(" unsatisfied 'height' constraint.\n") );
1095 wxLogDebug( wxT("Please check constraints: try adding AsIs() constraints.\n") );
1096 }
1097
1098 if ( recurse )
1099 {
1100 wxWindowList::Node *node = GetChildren().GetFirst();
1101 while (node)
1102 {
1103 wxWindow *win = node->GetData();
1104 if ( !win->IsTopLevel() )
1105 win->SetConstraintSizes();
1106 node = node->GetNext();
1107 }
1108 }
1109 }
1110
1111 // Only set the size/position of the constraint (if any)
1112 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1113 {
1114 wxLayoutConstraints *constr = GetConstraints();
1115 if ( constr )
1116 {
1117 if ( x != -1 )
1118 {
1119 constr->left.SetValue(x);
1120 constr->left.SetDone(TRUE);
1121 }
1122 if ( y != -1 )
1123 {
1124 constr->top.SetValue(y);
1125 constr->top.SetDone(TRUE);
1126 }
1127 if ( w != -1 )
1128 {
1129 constr->width.SetValue(w);
1130 constr->width.SetDone(TRUE);
1131 }
1132 if ( h != -1 )
1133 {
1134 constr->height.SetValue(h);
1135 constr->height.SetDone(TRUE);
1136 }
1137 }
1138 }
1139
1140 void wxWindowBase::MoveConstraint(int x, int y)
1141 {
1142 wxLayoutConstraints *constr = GetConstraints();
1143 if ( constr )
1144 {
1145 if ( x != -1 )
1146 {
1147 constr->left.SetValue(x);
1148 constr->left.SetDone(TRUE);
1149 }
1150 if ( y != -1 )
1151 {
1152 constr->top.SetValue(y);
1153 constr->top.SetDone(TRUE);
1154 }
1155 }
1156 }
1157
1158 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1159 {
1160 wxLayoutConstraints *constr = GetConstraints();
1161 if ( constr )
1162 {
1163 *w = constr->width.GetValue();
1164 *h = constr->height.GetValue();
1165 }
1166 else
1167 GetSize(w, h);
1168 }
1169
1170 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1171 {
1172 wxLayoutConstraints *constr = GetConstraints();
1173 if ( constr )
1174 {
1175 *w = constr->width.GetValue();
1176 *h = constr->height.GetValue();
1177 }
1178 else
1179 GetClientSize(w, h);
1180 }
1181
1182 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1183 {
1184 wxLayoutConstraints *constr = GetConstraints();
1185 if ( constr )
1186 {
1187 *x = constr->left.GetValue();
1188 *y = constr->top.GetValue();
1189 }
1190 else
1191 GetPosition(x, y);
1192 }
1193
1194 #endif // wxUSE_CONSTRAINTS
1195
1196 // ----------------------------------------------------------------------------
1197 // do Update UI processing for child controls
1198 // ----------------------------------------------------------------------------
1199
1200 // TODO: should this be implemented for the child window rather
1201 // than the parent? Then you can override it e.g. for wxCheckBox
1202 // to do the Right Thing rather than having to assume a fixed number
1203 // of control classes.
1204 void wxWindowBase::UpdateWindowUI()
1205 {
1206 wxUpdateUIEvent event(GetId());
1207 event.m_eventObject = this;
1208
1209 if ( GetEventHandler()->ProcessEvent(event) )
1210 {
1211 if ( event.GetSetEnabled() )
1212 Enable(event.GetEnabled());
1213
1214 if ( event.GetSetText() )
1215 {
1216 wxControl *control = wxDynamicCast(this, wxControl);
1217 if ( control )
1218 {
1219 wxTextCtrl *text = wxDynamicCast(control, wxTextCtrl);
1220 if ( text )
1221 text->SetValue(event.GetText());
1222 else
1223 control->SetLabel(event.GetText());
1224 }
1225 }
1226
1227 #if wxUSE_CHECKBOX
1228 wxCheckBox *checkbox = wxDynamicCast(this, wxCheckBox);
1229 if ( checkbox )
1230 {
1231 if ( event.GetSetChecked() )
1232 checkbox->SetValue(event.GetChecked());
1233 }
1234 #endif // wxUSE_CHECKBOX
1235
1236 #if wxUSE_RADIOBUTTON
1237 wxRadioButton *radiobtn = wxDynamicCast(this, wxRadioButton);
1238 if ( radiobtn )
1239 {
1240 if ( event.GetSetChecked() )
1241 radiobtn->SetValue(event.GetChecked());
1242 }
1243 #endif // wxUSE_RADIOBUTTON
1244 }
1245 }
1246
1247 // ----------------------------------------------------------------------------
1248 // dialog units translations
1249 // ----------------------------------------------------------------------------
1250
1251 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
1252 {
1253 int charWidth = GetCharWidth();
1254 int charHeight = GetCharHeight();
1255 wxPoint pt2(-1, -1);
1256 if (pt.x != -1)
1257 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1258 if (pt.y != -1)
1259 pt2.y = (int) ((pt.y * 8) / charHeight) ;
1260
1261 return pt2;
1262 }
1263
1264 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
1265 {
1266 int charWidth = GetCharWidth();
1267 int charHeight = GetCharHeight();
1268 wxPoint pt2(-1, -1);
1269 if (pt.x != -1)
1270 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1271 if (pt.y != -1)
1272 pt2.y = (int) ((pt.y * charHeight) / 8) ;
1273
1274 return pt2;
1275 }
1276
1277 // ----------------------------------------------------------------------------
1278 // client data
1279 // ----------------------------------------------------------------------------
1280
1281 void wxWindowBase::DoSetClientObject( wxClientData *data )
1282 {
1283 wxASSERT_MSG( m_clientDataType != ClientData_Void,
1284 wxT("can't have both object and void client data") );
1285
1286 if ( m_clientObject )
1287 delete m_clientObject;
1288
1289 m_clientObject = data;
1290 m_clientDataType = ClientData_Object;
1291 }
1292
1293 wxClientData *wxWindowBase::DoGetClientObject() const
1294 {
1295 wxASSERT_MSG( m_clientDataType == ClientData_Object,
1296 wxT("this window doesn't have object client data") );
1297
1298 return m_clientObject;
1299 }
1300
1301 void wxWindowBase::DoSetClientData( void *data )
1302 {
1303 wxASSERT_MSG( m_clientDataType != ClientData_Object,
1304 wxT("can't have both object and void client data") );
1305
1306 m_clientData = data;
1307 m_clientDataType = ClientData_Void;
1308 }
1309
1310 void *wxWindowBase::DoGetClientData() const
1311 {
1312 wxASSERT_MSG( m_clientDataType == ClientData_Void,
1313 wxT("this window doesn't have void client data") );
1314
1315 return m_clientData;
1316 }
1317
1318 // ----------------------------------------------------------------------------
1319 // event handlers
1320 // ----------------------------------------------------------------------------
1321
1322 // propagate the colour change event to the subwindows
1323 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1324 {
1325 wxWindowList::Node *node = GetChildren().GetFirst();
1326 while ( node )
1327 {
1328 // Only propagate to non-top-level windows
1329 wxWindow *win = node->GetData();
1330 if ( !win->IsTopLevel() )
1331 {
1332 wxSysColourChangedEvent event2;
1333 event.m_eventObject = win;
1334 win->GetEventHandler()->ProcessEvent(event2);
1335 }
1336
1337 node = node->GetNext();
1338 }
1339 }
1340
1341 // the default action is to populate dialog with data when it's created
1342 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1343 {
1344 TransferDataToWindow();
1345 }
1346
1347 // ----------------------------------------------------------------------------
1348 // list classes implementation
1349 // ----------------------------------------------------------------------------
1350
1351 void wxWindowListNode::DeleteData()
1352 {
1353 delete (wxWindow *)GetData();
1354 }
1355