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