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