]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
Small fixes for Tex2RTF but alas, we still get memory problems.
[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 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 // don't try to set invalid cursor, always fall back to the default
590 const wxCursor& cursorOk = cursor.Ok() ? cursor : *wxSTANDARD_CURSOR;
591
592 if ( (wxCursor&)cursorOk == m_cursor )
593 {
594 // no change
595 return FALSE;
596 }
597
598 m_cursor = cursorOk;
599
600 return TRUE;
601 }
602
603 bool wxWindowBase::SetFont(const wxFont& font)
604 {
605 // don't try to set invalid font, always fall back to the default
606 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
607
608 if ( (wxFont&)fontOk == m_font )
609 {
610 // no change
611 return FALSE;
612 }
613
614 m_font = fontOk;
615
616 return TRUE;
617 }
618
619 #if wxUSE_CARET
620 void wxWindowBase::SetCaret(wxCaret *caret)
621 {
622 if ( m_caret )
623 {
624 delete m_caret;
625 }
626
627 m_caret = caret;
628
629 if ( m_caret )
630 {
631 wxASSERT_MSG( m_caret->GetWindow() == this,
632 wxT("caret should be created associated to this window") );
633 }
634 }
635 #endif // wxUSE_CARET
636
637 #if wxUSE_VALIDATORS
638 // ----------------------------------------------------------------------------
639 // validators
640 // ----------------------------------------------------------------------------
641
642 void wxWindowBase::SetValidator(const wxValidator& validator)
643 {
644 if ( m_windowValidator )
645 delete m_windowValidator;
646
647 m_windowValidator = (wxValidator *)validator.Clone();
648
649 if ( m_windowValidator )
650 m_windowValidator->SetWindow(this) ;
651 }
652 #endif // wxUSE_VALIDATORS
653
654 // ----------------------------------------------------------------------------
655 // update region testing
656 // ----------------------------------------------------------------------------
657
658 bool wxWindowBase::IsExposed(int x, int y) const
659 {
660 return m_updateRegion.Contains(x, y) != wxOutRegion;
661 }
662
663 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
664 {
665 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
666 }
667
668 // ----------------------------------------------------------------------------
669 // find window by id or name
670 // ----------------------------------------------------------------------------
671
672 wxWindow *wxWindowBase::FindWindow( long id )
673 {
674 if ( id == m_windowId )
675 return (wxWindow *)this;
676
677 wxWindowBase *res = (wxWindow *)NULL;
678 wxWindowList::Node *node;
679 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
680 {
681 wxWindowBase *child = node->GetData();
682 res = child->FindWindow( id );
683 }
684
685 return (wxWindow *)res;
686 }
687
688 wxWindow *wxWindowBase::FindWindow( const wxString& name )
689 {
690 if ( name == m_windowName )
691 return (wxWindow *)this;
692
693 wxWindowBase *res = (wxWindow *)NULL;
694 wxWindowList::Node *node;
695 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
696 {
697 wxWindow *child = node->GetData();
698 res = child->FindWindow(name);
699 }
700
701 return (wxWindow *)res;
702 }
703
704 // ----------------------------------------------------------------------------
705 // dialog oriented functions
706 // ----------------------------------------------------------------------------
707
708 void wxWindowBase::MakeModal(bool modal)
709 {
710 // Disable all other windows
711 if ( IsTopLevel() )
712 {
713 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
714 while (node)
715 {
716 wxWindow *win = node->GetData();
717 if (win != this)
718 win->Enable(!modal);
719
720 node = node->GetNext();
721 }
722 }
723 }
724
725 bool wxWindowBase::Validate()
726 {
727 #if wxUSE_VALIDATORS
728 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
729
730 wxWindowList::Node *node;
731 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
732 {
733 wxWindowBase *child = node->GetData();
734 wxValidator *validator = child->GetValidator();
735 if ( validator && !validator->Validate((wxWindow *)this) )
736 {
737 return FALSE;
738 }
739
740 if ( recurse && !child->Validate() )
741 {
742 return FALSE;
743 }
744 }
745 #endif // wxUSE_VALIDATORS
746
747 return TRUE;
748 }
749
750 bool wxWindowBase::TransferDataToWindow()
751 {
752 #if wxUSE_VALIDATORS
753 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
754
755 wxWindowList::Node *node;
756 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
757 {
758 wxWindowBase *child = node->GetData();
759 wxValidator *validator = child->GetValidator();
760 if ( validator && !validator->TransferToWindow() )
761 {
762 wxLogWarning(_("Could not transfer data to window"));
763 wxLog::FlushActive();
764
765 return FALSE;
766 }
767
768 if ( recurse )
769 {
770 if ( !child->TransferDataToWindow() )
771 {
772 // warning already given
773 return FALSE;
774 }
775 }
776 }
777 #endif // wxUSE_VALIDATORS
778
779 return TRUE;
780 }
781
782 bool wxWindowBase::TransferDataFromWindow()
783 {
784 #if wxUSE_VALIDATORS
785 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
786
787 wxWindowList::Node *node;
788 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
789 {
790 wxWindow *child = node->GetData();
791 wxValidator *validator = child->GetValidator();
792 if ( validator && !validator->TransferFromWindow() )
793 {
794 // nop warning here because the application is supposed to give
795 // one itself - we don't know here what might have gone wrongly
796
797 return FALSE;
798 }
799
800 if ( recurse )
801 {
802 if ( !child->TransferDataFromWindow() )
803 {
804 // warning already given
805 return FALSE;
806 }
807 }
808 }
809 #endif // wxUSE_VALIDATORS
810
811 return TRUE;
812 }
813
814 void wxWindowBase::InitDialog()
815 {
816 wxInitDialogEvent event(GetId());
817 event.SetEventObject( this );
818 GetEventHandler()->ProcessEvent(event);
819 }
820
821 // ----------------------------------------------------------------------------
822 // tooltips
823 // ----------------------------------------------------------------------------
824
825 #if wxUSE_TOOLTIPS
826
827 void wxWindowBase::SetToolTip( const wxString &tip )
828 {
829 // don't create the new tooltip if we already have one
830 if ( m_tooltip )
831 {
832 m_tooltip->SetTip( tip );
833 }
834 else
835 {
836 SetToolTip( new wxToolTip( tip ) );
837 }
838
839 // setting empty tooltip text does not remove the tooltip any more - use
840 // SetToolTip((wxToolTip *)NULL) for this
841 }
842
843 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
844 {
845 if ( m_tooltip )
846 delete m_tooltip;
847
848 m_tooltip = tooltip;
849 }
850
851 #endif // wxUSE_TOOLTIPS
852
853 // ----------------------------------------------------------------------------
854 // constraints and sizers
855 // ----------------------------------------------------------------------------
856
857 #if wxUSE_CONSTRAINTS
858
859 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
860 {
861 if ( m_constraints )
862 {
863 UnsetConstraints(m_constraints);
864 delete m_constraints;
865 }
866 m_constraints = constraints;
867 if ( m_constraints )
868 {
869 // Make sure other windows know they're part of a 'meaningful relationship'
870 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
871 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
872 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
873 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
874 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
875 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
876 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
877 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
878 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
879 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
880 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
881 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
882 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
883 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
884 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
885 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
886 }
887 }
888
889 // This removes any dangling pointers to this window in other windows'
890 // constraintsInvolvedIn lists.
891 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
892 {
893 if ( c )
894 {
895 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
896 c->left.GetOtherWindow()->RemoveConstraintReference(this);
897 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
898 c->top.GetOtherWindow()->RemoveConstraintReference(this);
899 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
900 c->right.GetOtherWindow()->RemoveConstraintReference(this);
901 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
902 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
903 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
904 c->width.GetOtherWindow()->RemoveConstraintReference(this);
905 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
906 c->height.GetOtherWindow()->RemoveConstraintReference(this);
907 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
908 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
909 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
910 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
911 }
912 }
913
914 // Back-pointer to other windows we're involved with, so if we delete this
915 // window, we must delete any constraints we're involved with.
916 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
917 {
918 if ( !m_constraintsInvolvedIn )
919 m_constraintsInvolvedIn = new wxWindowList;
920 if ( !m_constraintsInvolvedIn->Find(otherWin) )
921 m_constraintsInvolvedIn->Append(otherWin);
922 }
923
924 // REMOVE back-pointer to other windows we're involved with.
925 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
926 {
927 if ( m_constraintsInvolvedIn )
928 m_constraintsInvolvedIn->DeleteObject(otherWin);
929 }
930
931 // Reset any constraints that mention this window
932 void wxWindowBase::DeleteRelatedConstraints()
933 {
934 if ( m_constraintsInvolvedIn )
935 {
936 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
937 while (node)
938 {
939 wxWindow *win = node->GetData();
940 wxLayoutConstraints *constr = win->GetConstraints();
941
942 // Reset any constraints involving this window
943 if ( constr )
944 {
945 constr->left.ResetIfWin(this);
946 constr->top.ResetIfWin(this);
947 constr->right.ResetIfWin(this);
948 constr->bottom.ResetIfWin(this);
949 constr->width.ResetIfWin(this);
950 constr->height.ResetIfWin(this);
951 constr->centreX.ResetIfWin(this);
952 constr->centreY.ResetIfWin(this);
953 }
954
955 wxWindowList::Node *next = node->GetNext();
956 delete node;
957 node = next;
958 }
959
960 delete m_constraintsInvolvedIn;
961 m_constraintsInvolvedIn = (wxWindowList *) NULL;
962 }
963 }
964
965 void wxWindowBase::SetSizer(wxSizer *sizer)
966 {
967 if (m_windowSizer) delete m_windowSizer;
968
969 m_windowSizer = sizer;
970 }
971
972 bool wxWindowBase::Layout()
973 {
974 // If there is a sizer, use it instead of the constraints
975 if ( GetSizer() )
976 {
977 int w, h;
978 GetClientSize(&w, &h);
979
980 GetSizer()->SetDimension( 0, 0, w, h );
981 }
982 else
983 {
984 wxLayoutConstraints *constr = GetConstraints();
985 bool wasOk = constr && constr->AreSatisfied();
986
987 ResetConstraints(); // Mark all constraints as unevaluated
988
989 // if we're a top level panel (i.e. our parent is frame/dialog), our
990 // own constraints will never be satisfied any more unless we do it
991 // here
992 if ( wasOk )
993 {
994 int noChanges = 1;
995 while ( noChanges > 0 )
996 {
997 constr->SatisfyConstraints(this, &noChanges);
998 }
999 }
1000
1001 DoPhase(1); // Layout children
1002 DoPhase(2); // Layout grand children
1003 SetConstraintSizes(); // Recursively set the real window sizes
1004 }
1005
1006 return TRUE;
1007 }
1008
1009
1010 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1011 // do a similar thing, but also impose their own 'constraints' and order the
1012 // evaluation differently.
1013 bool wxWindowBase::LayoutPhase1(int *noChanges)
1014 {
1015 wxLayoutConstraints *constr = GetConstraints();
1016 if ( constr )
1017 {
1018 return constr->SatisfyConstraints(this, noChanges);
1019 }
1020 else
1021 return TRUE;
1022 }
1023
1024 bool wxWindowBase::LayoutPhase2(int *noChanges)
1025 {
1026 *noChanges = 0;
1027
1028 // Layout children
1029 DoPhase(1);
1030 DoPhase(2);
1031 return TRUE;
1032 }
1033
1034 // Do a phase of evaluating child constraints
1035 bool wxWindowBase::DoPhase(int phase)
1036 {
1037 int noIterations = 0;
1038 int maxIterations = 500;
1039 int noChanges = 1;
1040 int noFailures = 0;
1041 wxWindowList succeeded;
1042 while ((noChanges > 0) && (noIterations < maxIterations))
1043 {
1044 noChanges = 0;
1045 noFailures = 0;
1046 wxWindowList::Node *node = GetChildren().GetFirst();
1047 while (node)
1048 {
1049 wxWindow *child = node->GetData();
1050 if ( !child->IsTopLevel() )
1051 {
1052 wxLayoutConstraints *constr = child->GetConstraints();
1053 if ( constr )
1054 {
1055 if ( !succeeded.Find(child) )
1056 {
1057 int tempNoChanges = 0;
1058 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
1059 noChanges += tempNoChanges;
1060 if ( success )
1061 {
1062 succeeded.Append(child);
1063 }
1064 }
1065 }
1066 }
1067 node = node->GetNext();
1068 }
1069
1070 noIterations++;
1071 }
1072
1073 return TRUE;
1074 }
1075
1076 void wxWindowBase::ResetConstraints()
1077 {
1078 wxLayoutConstraints *constr = GetConstraints();
1079 if ( constr )
1080 {
1081 constr->left.SetDone(FALSE);
1082 constr->top.SetDone(FALSE);
1083 constr->right.SetDone(FALSE);
1084 constr->bottom.SetDone(FALSE);
1085 constr->width.SetDone(FALSE);
1086 constr->height.SetDone(FALSE);
1087 constr->centreX.SetDone(FALSE);
1088 constr->centreY.SetDone(FALSE);
1089 }
1090
1091 wxWindowList::Node *node = GetChildren().GetFirst();
1092 while (node)
1093 {
1094 wxWindow *win = node->GetData();
1095 if ( !win->IsTopLevel() )
1096 win->ResetConstraints();
1097 node = node->GetNext();
1098 }
1099 }
1100
1101 // Need to distinguish between setting the 'fake' size for windows and sizers,
1102 // and setting the real values.
1103 void wxWindowBase::SetConstraintSizes(bool recurse)
1104 {
1105 wxLayoutConstraints *constr = GetConstraints();
1106 if ( constr && constr->AreSatisfied() )
1107 {
1108 int x = constr->left.GetValue();
1109 int y = constr->top.GetValue();
1110 int w = constr->width.GetValue();
1111 int h = constr->height.GetValue();
1112
1113 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1114 (constr->height.GetRelationship() != wxAsIs) )
1115 {
1116 SetSize(x, y, w, h);
1117 }
1118 else
1119 {
1120 // If we don't want to resize this window, just move it...
1121 Move(x, y);
1122 }
1123 }
1124 else if ( constr )
1125 {
1126 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1127 GetClassInfo()->GetClassName(),
1128 GetName().c_str());
1129 }
1130
1131 if ( recurse )
1132 {
1133 wxWindowList::Node *node = GetChildren().GetFirst();
1134 while (node)
1135 {
1136 wxWindow *win = node->GetData();
1137 if ( !win->IsTopLevel() )
1138 win->SetConstraintSizes();
1139 node = node->GetNext();
1140 }
1141 }
1142 }
1143
1144 // Only set the size/position of the constraint (if any)
1145 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1146 {
1147 wxLayoutConstraints *constr = GetConstraints();
1148 if ( constr )
1149 {
1150 if ( x != -1 )
1151 {
1152 constr->left.SetValue(x);
1153 constr->left.SetDone(TRUE);
1154 }
1155 if ( y != -1 )
1156 {
1157 constr->top.SetValue(y);
1158 constr->top.SetDone(TRUE);
1159 }
1160 if ( w != -1 )
1161 {
1162 constr->width.SetValue(w);
1163 constr->width.SetDone(TRUE);
1164 }
1165 if ( h != -1 )
1166 {
1167 constr->height.SetValue(h);
1168 constr->height.SetDone(TRUE);
1169 }
1170 }
1171 }
1172
1173 void wxWindowBase::MoveConstraint(int x, int y)
1174 {
1175 wxLayoutConstraints *constr = GetConstraints();
1176 if ( constr )
1177 {
1178 if ( x != -1 )
1179 {
1180 constr->left.SetValue(x);
1181 constr->left.SetDone(TRUE);
1182 }
1183 if ( y != -1 )
1184 {
1185 constr->top.SetValue(y);
1186 constr->top.SetDone(TRUE);
1187 }
1188 }
1189 }
1190
1191 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1192 {
1193 wxLayoutConstraints *constr = GetConstraints();
1194 if ( constr )
1195 {
1196 *w = constr->width.GetValue();
1197 *h = constr->height.GetValue();
1198 }
1199 else
1200 GetSize(w, h);
1201 }
1202
1203 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1204 {
1205 wxLayoutConstraints *constr = GetConstraints();
1206 if ( constr )
1207 {
1208 *w = constr->width.GetValue();
1209 *h = constr->height.GetValue();
1210 }
1211 else
1212 GetClientSize(w, h);
1213 }
1214
1215 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1216 {
1217 wxLayoutConstraints *constr = GetConstraints();
1218 if ( constr )
1219 {
1220 *x = constr->left.GetValue();
1221 *y = constr->top.GetValue();
1222 }
1223 else
1224 GetPosition(x, y);
1225 }
1226
1227 #endif // wxUSE_CONSTRAINTS
1228
1229 // ----------------------------------------------------------------------------
1230 // do Update UI processing for child controls
1231 // ----------------------------------------------------------------------------
1232
1233 // TODO: should this be implemented for the child window rather
1234 // than the parent? Then you can override it e.g. for wxCheckBox
1235 // to do the Right Thing rather than having to assume a fixed number
1236 // of control classes.
1237 void wxWindowBase::UpdateWindowUI()
1238 {
1239 wxUpdateUIEvent event(GetId());
1240 event.m_eventObject = this;
1241
1242 if ( GetEventHandler()->ProcessEvent(event) )
1243 {
1244 if ( event.GetSetEnabled() )
1245 Enable(event.GetEnabled());
1246
1247 if ( event.GetSetText() )
1248 {
1249 wxControl *control = wxDynamicCast(this, wxControl);
1250 if ( control )
1251 {
1252 wxTextCtrl *text = wxDynamicCast(control, wxTextCtrl);
1253 if ( text )
1254 text->SetValue(event.GetText());
1255 else
1256 control->SetLabel(event.GetText());
1257 }
1258 }
1259
1260 #if wxUSE_CHECKBOX
1261 wxCheckBox *checkbox = wxDynamicCast(this, wxCheckBox);
1262 if ( checkbox )
1263 {
1264 if ( event.GetSetChecked() )
1265 checkbox->SetValue(event.GetChecked());
1266 }
1267 #endif // wxUSE_CHECKBOX
1268
1269 #if wxUSE_RADIOBTN
1270 wxRadioButton *radiobtn = wxDynamicCast(this, wxRadioButton);
1271 if ( radiobtn )
1272 {
1273 if ( event.GetSetChecked() )
1274 radiobtn->SetValue(event.GetChecked());
1275 }
1276 #endif // wxUSE_RADIOBTN
1277 }
1278 }
1279
1280 // ----------------------------------------------------------------------------
1281 // dialog units translations
1282 // ----------------------------------------------------------------------------
1283
1284 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
1285 {
1286 int charWidth = GetCharWidth();
1287 int charHeight = GetCharHeight();
1288 wxPoint pt2(-1, -1);
1289 if (pt.x != -1)
1290 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1291 if (pt.y != -1)
1292 pt2.y = (int) ((pt.y * 8) / charHeight) ;
1293
1294 return pt2;
1295 }
1296
1297 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
1298 {
1299 int charWidth = GetCharWidth();
1300 int charHeight = GetCharHeight();
1301 wxPoint pt2(-1, -1);
1302 if (pt.x != -1)
1303 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1304 if (pt.y != -1)
1305 pt2.y = (int) ((pt.y * charHeight) / 8) ;
1306
1307 return pt2;
1308 }
1309
1310 // ----------------------------------------------------------------------------
1311 // client data
1312 // ----------------------------------------------------------------------------
1313
1314 void wxWindowBase::DoSetClientObject( wxClientData *data )
1315 {
1316 wxASSERT_MSG( m_clientDataType != ClientData_Void,
1317 wxT("can't have both object and void client data") );
1318
1319 if ( m_clientObject )
1320 delete m_clientObject;
1321
1322 m_clientObject = data;
1323 m_clientDataType = ClientData_Object;
1324 }
1325
1326 wxClientData *wxWindowBase::DoGetClientObject() const
1327 {
1328 // it's not an error to call GetClientObject() on a window which doesn't
1329 // have client data at all - NULL will be returned
1330 wxASSERT_MSG( m_clientDataType != ClientData_Void,
1331 wxT("this window doesn't have object client data") );
1332
1333 return m_clientObject;
1334 }
1335
1336 void wxWindowBase::DoSetClientData( void *data )
1337 {
1338 wxASSERT_MSG( m_clientDataType != ClientData_Object,
1339 wxT("can't have both object and void client data") );
1340
1341 m_clientData = data;
1342 m_clientDataType = ClientData_Void;
1343 }
1344
1345 void *wxWindowBase::DoGetClientData() const
1346 {
1347 // it's not an error to call GetClientData() on a window which doesn't have
1348 // client data at all - NULL will be returned
1349 wxASSERT_MSG( m_clientDataType != ClientData_Object,
1350 wxT("this window doesn't have void client data") );
1351
1352 return m_clientData;
1353 }
1354
1355 // ----------------------------------------------------------------------------
1356 // event handlers
1357 // ----------------------------------------------------------------------------
1358
1359 // propagate the colour change event to the subwindows
1360 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1361 {
1362 wxWindowList::Node *node = GetChildren().GetFirst();
1363 while ( node )
1364 {
1365 // Only propagate to non-top-level windows
1366 wxWindow *win = node->GetData();
1367 if ( !win->IsTopLevel() )
1368 {
1369 wxSysColourChangedEvent event2;
1370 event.m_eventObject = win;
1371 win->GetEventHandler()->ProcessEvent(event2);
1372 }
1373
1374 node = node->GetNext();
1375 }
1376 }
1377
1378 // the default action is to populate dialog with data when it's created
1379 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1380 {
1381 TransferDataToWindow();
1382 }
1383
1384 // process Ctrl-Alt-mclick
1385 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
1386 {
1387 if ( event.ControlDown() && event.AltDown() )
1388 {
1389 // don't translate these strings
1390 wxString port;
1391 switch ( wxGetOsVersion() )
1392 {
1393 case wxMOTIF_X: port = _T("Motif"); break;
1394 case wxMACINTOSH: port = _T("Mac"); break;
1395 case wxBEOS: port = _T("BeOS"); break;
1396 case wxGTK:
1397 case wxGTK_WIN32:
1398 case wxGTK_OS2:
1399 case wxGTK_BEOS: port = _T("GTK"); break;
1400 case wxWINDOWS:
1401 case wxPENWINDOWS:
1402 case wxWINDOWS_NT:
1403 case wxWIN32S:
1404 case wxWIN95:
1405 case wxWIN386: port = _T("MS Windows"); break;
1406 case wxMGL_UNIX:
1407 case wxMGL_X:
1408 case wxMGL_WIN32:
1409 case wxMGL_OS2: port = _T("MGL"); break;
1410 case wxWINDOWS_OS2:
1411 case wxOS2_PM: port = _T("OS/2"); break;
1412 default: port = _T("unknown"); break;
1413 }
1414
1415 wxMessageBox(wxString::Format(
1416 _T(
1417 " wxWindows Library (%s port)\n"
1418 "Version %u.%u.%u, compiled at %s %s\n"
1419 " Copyright (c) 1995-2000 wxWindows team"
1420 ),
1421 port.c_str(),
1422 wxMAJOR_VERSION,
1423 wxMINOR_VERSION,
1424 wxRELEASE_NUMBER,
1425 __DATE__,
1426 __TIME__
1427 ),
1428 _T("wxWindows information"),
1429 wxICON_INFORMATION | wxOK,
1430 (wxWindow *)this);
1431 }
1432 else
1433 {
1434 event.Skip();
1435 }
1436 }
1437
1438 // ----------------------------------------------------------------------------
1439 // list classes implementation
1440 // ----------------------------------------------------------------------------
1441
1442 void wxWindowListNode::DeleteData()
1443 {
1444 delete (wxWindow *)GetData();
1445 }
1446