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