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