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