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