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