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