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