]> git.saurik.com Git - wxWidgets.git/blame - src/common/wincmn.cpp
RemoveAt() added to array classes
[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
07cf98cb
VZ
357 // move the centre of this window to this position
358 Move(xNew, yNew);
7631a292
RD
359}
360
f03fc89f
VZ
361// fits the window around the children
362void wxWindowBase::Fit()
363{
364 int maxX = 0,
365 maxY = 0;
366
bfac8499
VZ
367 for ( wxWindowList::Node *node = GetChildren().GetFirst();
368 node;
369 node = node->GetNext() )
f03fc89f
VZ
370 {
371 wxWindow *win = node->GetData();
34636400 372 if ( win->IsTopLevel() )
42e69d6b 373 {
34636400 374 // dialogs and frames lie in different top level windows - don't
42e69d6b
VZ
375 // deal with them here
376 continue;
377 }
378
f03fc89f
VZ
379 int wx, wy, ww, wh;
380 win->GetPosition(&wx, &wy);
381 win->GetSize(&ww, &wh);
382 if ( wx + ww > maxX )
383 maxX = wx + ww;
384 if ( wy + wh > maxY )
385 maxY = wy + wh;
f03fc89f
VZ
386 }
387
388 // leave a margin
389 SetClientSize(maxX + 7, maxY + 14);
390}
391
392// set the min/max size of the window
393
394void wxWindowBase::SetSizeHints(int minW, int minH,
395 int maxW, int maxH,
396 int WXUNUSED(incW), int WXUNUSED(incH))
397{
398 m_minWidth = minW;
399 m_maxWidth = maxW;
400 m_minHeight = minH;
401 m_maxHeight = maxH;
402}
403
404// ----------------------------------------------------------------------------
405// show/hide/enable/disable the window
406// ----------------------------------------------------------------------------
407
408bool wxWindowBase::Show(bool show)
409{
410 if ( show != m_isShown )
411 {
412 m_isShown = show;
413
414 return TRUE;
415 }
416 else
417 {
418 return FALSE;
419 }
420}
421
422bool wxWindowBase::Enable(bool enable)
423{
424 if ( enable != m_isEnabled )
425 {
426 m_isEnabled = enable;
427
428 return TRUE;
429 }
430 else
431 {
432 return FALSE;
433 }
434}
34636400
VZ
435// ----------------------------------------------------------------------------
436// RTTI
437// ----------------------------------------------------------------------------
438
439bool wxWindowBase::IsTopLevel() const
440{
8487f887 441 return FALSE;
34636400 442}
f03fc89f
VZ
443
444// ----------------------------------------------------------------------------
445// reparenting the window
446// ----------------------------------------------------------------------------
447
448void wxWindowBase::AddChild(wxWindowBase *child)
449{
223d09f6 450 wxCHECK_RET( child, wxT("can't add a NULL child") );
f03fc89f
VZ
451
452 GetChildren().Append(child);
453 child->SetParent(this);
454}
455
456void wxWindowBase::RemoveChild(wxWindowBase *child)
457{
223d09f6 458 wxCHECK_RET( child, wxT("can't remove a NULL child") );
f03fc89f
VZ
459
460 GetChildren().DeleteObject(child);
461 child->SetParent((wxWindow *)NULL);
462}
439b3bf1 463
f03fc89f
VZ
464bool wxWindowBase::Reparent(wxWindowBase *newParent)
465{
466 wxWindow *oldParent = GetParent();
467 if ( newParent == oldParent )
468 {
469 // nothing done
470 return FALSE;
471 }
472
473 // unlink this window from the existing parent.
474 if ( oldParent )
475 {
476 oldParent->RemoveChild(this);
477 }
478 else
479 {
480 wxTopLevelWindows.DeleteObject(this);
481 }
482
483 // add it to the new one
484 if ( newParent )
485 {
486 newParent->AddChild(this);
487 }
488 else
489 {
490 wxTopLevelWindows.Append(this);
491 }
492
493 return TRUE;
494}
495
496// ----------------------------------------------------------------------------
497// event handler stuff
498// ----------------------------------------------------------------------------
499
500void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
501{
502 handler->SetNextHandler(GetEventHandler());
503 SetEventHandler(handler);
504}
505
506wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
507{
508 wxEvtHandler *handlerA = GetEventHandler();
509 if ( handlerA )
510 {
511 wxEvtHandler *handlerB = handlerA->GetNextHandler();
512 handlerA->SetNextHandler((wxEvtHandler *)NULL);
513 SetEventHandler(handlerB);
514 if ( deleteHandler )
515 {
516 delete handlerA;
517 handlerA = (wxEvtHandler *)NULL;
518 }
519 }
520
521 return handlerA;
522}
523
524// ----------------------------------------------------------------------------
525// cursors, fonts &c
526// ----------------------------------------------------------------------------
527
528bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
529{
530 if ( !colour.Ok() || (colour == m_backgroundColour) )
531 return FALSE;
532
533 m_backgroundColour = colour;
534
535 return TRUE;
536}
537
538bool wxWindowBase::SetForegroundColour( const wxColour &colour )
539{
540 if ( !colour.Ok() || (colour == m_foregroundColour) )
541 return FALSE;
542
543 m_foregroundColour = colour;
544
545 return TRUE;
546}
547
548bool wxWindowBase::SetCursor(const wxCursor& cursor)
549{
550 // don't try to set invalid cursor, always fall back to the default
551 const wxCursor& cursorOk = cursor.Ok() ? cursor : *wxSTANDARD_CURSOR;
552
913df6f2 553 if ( (wxCursor&)cursorOk == m_cursor )
f03fc89f
VZ
554 {
555 // no change
556 return FALSE;
557 }
558
559 m_cursor = cursorOk;
560
561 return TRUE;
562}
563
564bool wxWindowBase::SetFont(const wxFont& font)
565{
566 // don't try to set invalid font, always fall back to the default
567 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
568
913df6f2 569 if ( (wxFont&)fontOk == m_font )
f03fc89f
VZ
570 {
571 // no change
572 return FALSE;
573 }
574
575 m_font = fontOk;
576
577 return TRUE;
578}
579
789295bf
VZ
580#if wxUSE_CARET
581void wxWindowBase::SetCaret(wxCaret *caret)
582{
583 if ( m_caret )
584 {
585 delete m_caret;
586 }
587
588 m_caret = caret;
589
590 if ( m_caret )
591 {
592 wxASSERT_MSG( m_caret->GetWindow() == this,
223d09f6 593 wxT("caret should be created associated to this window") );
789295bf
VZ
594 }
595}
596#endif // wxUSE_CARET
597
88ac883a 598#if wxUSE_VALIDATORS
f03fc89f
VZ
599// ----------------------------------------------------------------------------
600// validators
601// ----------------------------------------------------------------------------
602
5d4b632b
DW
603# if defined(__VISAGECPP__)
604void wxWindowBase::SetValidator(const wxValidator* validator)
605{
606 if ( m_windowValidator )
607 delete m_windowValidator;
608
609 m_windowValidator = (wxValidator *)validator->Clone();
610
611 if ( m_windowValidator )
612 m_windowValidator->SetWindow(this) ;
613}
614# else
f03fc89f
VZ
615void wxWindowBase::SetValidator(const wxValidator& validator)
616{
617 if ( m_windowValidator )
618 delete m_windowValidator;
619
620 m_windowValidator = (wxValidator *)validator.Clone();
621
622 if ( m_windowValidator )
623 m_windowValidator->SetWindow(this) ;
624}
5d4b632b 625# endif // __VISAGECPP__
88ac883a 626#endif // wxUSE_VALIDATORS
f03fc89f
VZ
627
628// ----------------------------------------------------------------------------
629// update region testing
630// ----------------------------------------------------------------------------
631
632bool wxWindowBase::IsExposed(int x, int y) const
633{
634 return m_updateRegion.Contains(x, y) != wxOutRegion;
635}
636
637bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
638{
639 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
640}
641
642// ----------------------------------------------------------------------------
643// find window by id or name
644// ----------------------------------------------------------------------------
645
646wxWindow *wxWindowBase::FindWindow( long id )
647{
648 if ( id == m_windowId )
649 return (wxWindow *)this;
650
651 wxWindowBase *res = (wxWindow *)NULL;
652 wxWindowList::Node *node;
653 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
654 {
655 wxWindowBase *child = node->GetData();
656 res = child->FindWindow( id );
657 }
658
659 return (wxWindow *)res;
660}
661
662wxWindow *wxWindowBase::FindWindow( const wxString& name )
663{
664 if ( name == m_windowName )
665 return (wxWindow *)this;
666
667 wxWindowBase *res = (wxWindow *)NULL;
668 wxWindowList::Node *node;
669 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
670 {
671 wxWindow *child = node->GetData();
672 res = child->FindWindow(name);
673 }
674
675 return (wxWindow *)res;
676}
677
678// ----------------------------------------------------------------------------
679// dialog oriented functions
680// ----------------------------------------------------------------------------
681
34636400 682void wxWindowBase::MakeModal(bool modal)
f03fc89f 683{
34636400
VZ
684 // Disable all other windows
685 if ( IsTopLevel() )
686 {
687 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
688 while (node)
689 {
690 wxWindow *win = node->GetData();
691 if (win != this)
692 win->Enable(!modal);
693
694 node = node->GetNext();
695 }
696 }
f03fc89f
VZ
697}
698
699bool wxWindowBase::Validate()
700{
88ac883a 701#if wxUSE_VALIDATORS
f03fc89f
VZ
702 wxWindowList::Node *node;
703 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
704 {
705 wxWindowBase *child = node->GetData();
706 wxValidator *validator = child->GetValidator();
dcd6b914 707 if ( validator && !validator->Validate((wxWindow *)this) )
f03fc89f
VZ
708 {
709 return FALSE;
710 }
711 }
88ac883a 712#endif // wxUSE_VALIDATORS
f03fc89f
VZ
713
714 return TRUE;
715}
716
717bool wxWindowBase::TransferDataToWindow()
718{
88ac883a 719#if wxUSE_VALIDATORS
f03fc89f
VZ
720 wxWindowList::Node *node;
721 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
722 {
723 wxWindowBase *child = node->GetData();
724 wxValidator *validator = child->GetValidator();
725 if ( validator && !validator->TransferToWindow() )
726 {
727 wxLog *log = wxLog::GetActiveTarget();
728 if ( log )
729 {
730 wxLogWarning(_("Could not transfer data to window"));
731 log->Flush();
732 }
733
734 return FALSE;
735 }
736 }
88ac883a 737#endif // wxUSE_VALIDATORS
f03fc89f
VZ
738
739 return TRUE;
740}
741
742bool wxWindowBase::TransferDataFromWindow()
743{
88ac883a 744#if wxUSE_VALIDATORS
f03fc89f
VZ
745 wxWindowList::Node *node;
746 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
747 {
748 wxWindow *child = node->GetData();
749 if ( child->GetValidator() &&
750 !child->GetValidator()->TransferFromWindow() )
751 {
752 return FALSE;
753 }
754 }
88ac883a 755#endif // wxUSE_VALIDATORS
f03fc89f
VZ
756
757 return TRUE;
758}
759
760void wxWindowBase::InitDialog()
761{
762 wxInitDialogEvent event(GetId());
763 event.SetEventObject( this );
764 GetEventHandler()->ProcessEvent(event);
765}
766
767// ----------------------------------------------------------------------------
768// tooltips
769// ----------------------------------------------------------------------------
770
771#if wxUSE_TOOLTIPS
772
773void wxWindowBase::SetToolTip( const wxString &tip )
774{
775 // don't create the new tooltip if we already have one
776 if ( m_tooltip )
777 {
778 m_tooltip->SetTip( tip );
779 }
780 else
781 {
782 SetToolTip( new wxToolTip( tip ) );
783 }
784
785 // setting empty tooltip text does not remove the tooltip any more - use
786 // SetToolTip((wxToolTip *)NULL) for this
787}
788
789void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
790{
791 if ( m_tooltip )
792 delete m_tooltip;
793
794 m_tooltip = tooltip;
795}
796
797#endif // wxUSE_TOOLTIPS
798
799// ----------------------------------------------------------------------------
800// constraints and sizers
801// ----------------------------------------------------------------------------
802
803#if wxUSE_CONSTRAINTS
804
805void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
806{
807 if ( m_constraints )
808 {
809 UnsetConstraints(m_constraints);
810 delete m_constraints;
811 }
812 m_constraints = constraints;
813 if ( m_constraints )
814 {
815 // Make sure other windows know they're part of a 'meaningful relationship'
816 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
817 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
818 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
819 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
820 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
821 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
822 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
823 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
824 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
825 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
826 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
827 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
828 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
829 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
830 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
831 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
832 }
833}
834
835// This removes any dangling pointers to this window in other windows'
836// constraintsInvolvedIn lists.
837void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
838{
839 if ( c )
840 {
841 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
842 c->left.GetOtherWindow()->RemoveConstraintReference(this);
843 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
844 c->top.GetOtherWindow()->RemoveConstraintReference(this);
845 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
846 c->right.GetOtherWindow()->RemoveConstraintReference(this);
847 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
848 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
849 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
850 c->width.GetOtherWindow()->RemoveConstraintReference(this);
851 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
852 c->height.GetOtherWindow()->RemoveConstraintReference(this);
853 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
854 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
855 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
856 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
857 }
858}
859
860// Back-pointer to other windows we're involved with, so if we delete this
861// window, we must delete any constraints we're involved with.
862void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
863{
864 if ( !m_constraintsInvolvedIn )
865 m_constraintsInvolvedIn = new wxWindowList;
866 if ( !m_constraintsInvolvedIn->Find(otherWin) )
867 m_constraintsInvolvedIn->Append(otherWin);
868}
869
870// REMOVE back-pointer to other windows we're involved with.
871void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
872{
873 if ( m_constraintsInvolvedIn )
874 m_constraintsInvolvedIn->DeleteObject(otherWin);
875}
876
877// Reset any constraints that mention this window
878void wxWindowBase::DeleteRelatedConstraints()
879{
880 if ( m_constraintsInvolvedIn )
881 {
882 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
883 while (node)
884 {
885 wxWindow *win = node->GetData();
886 wxLayoutConstraints *constr = win->GetConstraints();
887
888 // Reset any constraints involving this window
889 if ( constr )
890 {
891 constr->left.ResetIfWin(this);
892 constr->top.ResetIfWin(this);
893 constr->right.ResetIfWin(this);
894 constr->bottom.ResetIfWin(this);
895 constr->width.ResetIfWin(this);
896 constr->height.ResetIfWin(this);
897 constr->centreX.ResetIfWin(this);
898 constr->centreY.ResetIfWin(this);
899 }
900
901 wxWindowList::Node *next = node->GetNext();
902 delete node;
903 node = next;
904 }
905
906 delete m_constraintsInvolvedIn;
907 m_constraintsInvolvedIn = (wxWindowList *) NULL;
908 }
909}
910
911void wxWindowBase::SetSizer(wxSizer *sizer)
912{
3417c2cd
RR
913 if (m_windowSizer) delete m_windowSizer;
914
f03fc89f 915 m_windowSizer = sizer;
f03fc89f
VZ
916}
917
918bool wxWindowBase::Layout()
919{
3417c2cd
RR
920 int w, h;
921 GetClientSize(&w, &h);
5d4b632b 922
3417c2cd 923 // If there is a sizer, use it instead of the constraints
f03fc89f
VZ
924 if ( GetSizer() )
925 {
3417c2cd 926 GetSizer()->SetDimension( 0, 0, w, h );
f03fc89f
VZ
927 return TRUE;
928 }
5d4b632b 929
3417c2cd 930 if ( GetConstraints() )
f03fc89f 931 {
3417c2cd
RR
932 GetConstraints()->width.SetValue(w);
933 GetConstraints()->height.SetValue(h);
f03fc89f 934 }
3417c2cd
RR
935
936 // Evaluate child constraints
937 ResetConstraints(); // Mark all constraints as unevaluated
938 DoPhase(1); // Just one phase need if no sizers involved
939 DoPhase(2);
940 SetConstraintSizes(); // Recursively set the real window sizes
5d4b632b 941
f03fc89f
VZ
942 return TRUE;
943}
944
945
946// Do a phase of evaluating constraints: the default behaviour. wxSizers may
947// do a similar thing, but also impose their own 'constraints' and order the
948// evaluation differently.
949bool wxWindowBase::LayoutPhase1(int *noChanges)
950{
951 wxLayoutConstraints *constr = GetConstraints();
952 if ( constr )
953 {
954 return constr->SatisfyConstraints(this, noChanges);
955 }
956 else
957 return TRUE;
958}
959
960bool wxWindowBase::LayoutPhase2(int *noChanges)
961{
962 *noChanges = 0;
963
964 // Layout children
965 DoPhase(1);
966 DoPhase(2);
967 return TRUE;
968}
969
970// Do a phase of evaluating child constraints
971bool wxWindowBase::DoPhase(int phase)
972{
973 int noIterations = 0;
974 int maxIterations = 500;
975 int noChanges = 1;
976 int noFailures = 0;
977 wxWindowList succeeded;
978 while ((noChanges > 0) && (noIterations < maxIterations))
979 {
980 noChanges = 0;
981 noFailures = 0;
982 wxWindowList::Node *node = GetChildren().GetFirst();
983 while (node)
984 {
985 wxWindow *child = node->GetData();
34636400 986 if ( !child->IsTopLevel() )
f03fc89f
VZ
987 {
988 wxLayoutConstraints *constr = child->GetConstraints();
989 if ( constr )
990 {
991 if ( !succeeded.Find(child) )
992 {
993 int tempNoChanges = 0;
994 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
995 noChanges += tempNoChanges;
996 if ( success )
997 {
998 succeeded.Append(child);
999 }
1000 }
1001 }
1002 }
1003 node = node->GetNext();
1004 }
1005
1006 noIterations++;
1007 }
1008
1009 return TRUE;
1010}
1011
1012void wxWindowBase::ResetConstraints()
1013{
1014 wxLayoutConstraints *constr = GetConstraints();
1015 if ( constr )
1016 {
1017 constr->left.SetDone(FALSE);
1018 constr->top.SetDone(FALSE);
1019 constr->right.SetDone(FALSE);
1020 constr->bottom.SetDone(FALSE);
1021 constr->width.SetDone(FALSE);
1022 constr->height.SetDone(FALSE);
1023 constr->centreX.SetDone(FALSE);
1024 constr->centreY.SetDone(FALSE);
1025 }
1026 wxWindowList::Node *node = GetChildren().GetFirst();
1027 while (node)
1028 {
1029 wxWindow *win = node->GetData();
34636400 1030 if ( !win->IsTopLevel() )
f03fc89f
VZ
1031 win->ResetConstraints();
1032 node = node->GetNext();
1033 }
1034}
1035
1036// Need to distinguish between setting the 'fake' size for windows and sizers,
1037// and setting the real values.
1038void wxWindowBase::SetConstraintSizes(bool recurse)
1039{
1040 wxLayoutConstraints *constr = GetConstraints();
1041 if ( constr && constr->left.GetDone() && constr->right.GetDone( ) &&
1042 constr->width.GetDone() && constr->height.GetDone())
1043 {
1044 int x = constr->left.GetValue();
1045 int y = constr->top.GetValue();
1046 int w = constr->width.GetValue();
1047 int h = constr->height.GetValue();
1048
f03fc89f 1049 if ( (constr->width.GetRelationship() != wxAsIs ) ||
3417c2cd 1050 (constr->height.GetRelationship() != wxAsIs) )
f03fc89f 1051 {
3417c2cd 1052 SetSize(x, y, w, h);
f03fc89f
VZ
1053 }
1054 else
1055 {
3417c2cd
RR
1056 // If we don't want to resize this window, just move it...
1057 Move(x, y);
f03fc89f
VZ
1058 }
1059 }
1060 else if ( constr )
1061 {
1062 wxChar *windowClass = GetClassInfo()->GetClassName();
1063
1064 wxString winName;
223d09f6
KB
1065 if ( GetName() == wxT("") )
1066 winName = wxT("unnamed");
f03fc89f
VZ
1067 else
1068 winName = GetName();
223d09f6 1069 wxLogDebug( wxT("Constraint(s) not satisfied for window of type %s, name %s:\n"),
f03fc89f
VZ
1070 (const wxChar *)windowClass,
1071 (const wxChar *)winName);
223d09f6
KB
1072 if ( !constr->left.GetDone()) wxLogDebug( wxT(" unsatisfied 'left' constraint.\n") );
1073 if ( !constr->right.GetDone()) wxLogDebug( wxT(" unsatisfied 'right' constraint.\n") );
1074 if ( !constr->width.GetDone()) wxLogDebug( wxT(" unsatisfied 'width' constraint.\n") );
1075 if ( !constr->height.GetDone()) wxLogDebug( wxT(" unsatisfied 'height' constraint.\n") );
1076 wxLogDebug( wxT("Please check constraints: try adding AsIs() constraints.\n") );
f03fc89f
VZ
1077 }
1078
1079 if ( recurse )
1080 {
1081 wxWindowList::Node *node = GetChildren().GetFirst();
1082 while (node)
1083 {
1084 wxWindow *win = node->GetData();
34636400 1085 if ( !win->IsTopLevel() )
f03fc89f
VZ
1086 win->SetConstraintSizes();
1087 node = node->GetNext();
1088 }
1089 }
1090}
1091
f03fc89f
VZ
1092// Only set the size/position of the constraint (if any)
1093void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1094{
1095 wxLayoutConstraints *constr = GetConstraints();
1096 if ( constr )
1097 {
1098 if ( x != -1 )
1099 {
1100 constr->left.SetValue(x);
1101 constr->left.SetDone(TRUE);
1102 }
1103 if ( y != -1 )
1104 {
1105 constr->top.SetValue(y);
1106 constr->top.SetDone(TRUE);
1107 }
1108 if ( w != -1 )
1109 {
1110 constr->width.SetValue(w);
1111 constr->width.SetDone(TRUE);
1112 }
1113 if ( h != -1 )
1114 {
1115 constr->height.SetValue(h);
1116 constr->height.SetDone(TRUE);
1117 }
1118 }
1119}
1120
1121void wxWindowBase::MoveConstraint(int x, int y)
1122{
1123 wxLayoutConstraints *constr = GetConstraints();
1124 if ( constr )
1125 {
1126 if ( x != -1 )
1127 {
1128 constr->left.SetValue(x);
1129 constr->left.SetDone(TRUE);
1130 }
1131 if ( y != -1 )
1132 {
1133 constr->top.SetValue(y);
1134 constr->top.SetDone(TRUE);
1135 }
1136 }
1137}
1138
1139void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1140{
1141 wxLayoutConstraints *constr = GetConstraints();
1142 if ( constr )
1143 {
1144 *w = constr->width.GetValue();
1145 *h = constr->height.GetValue();
1146 }
1147 else
1148 GetSize(w, h);
1149}
1150
1151void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1152{
1153 wxLayoutConstraints *constr = GetConstraints();
1154 if ( constr )
1155 {
1156 *w = constr->width.GetValue();
1157 *h = constr->height.GetValue();
1158 }
1159 else
1160 GetClientSize(w, h);
1161}
1162
1163void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1164{
1165 wxLayoutConstraints *constr = GetConstraints();
1166 if ( constr )
1167 {
1168 *x = constr->left.GetValue();
1169 *y = constr->top.GetValue();
1170 }
1171 else
1172 GetPosition(x, y);
1173}
1174
1175#endif // wxUSE_CONSTRAINTS
1176
1177// ----------------------------------------------------------------------------
1178// do Update UI processing for child controls
1179// ----------------------------------------------------------------------------
7ec1983b
VZ
1180
1181// TODO: should this be implemented for the child window rather
1182// than the parent? Then you can override it e.g. for wxCheckBox
1183// to do the Right Thing rather than having to assume a fixed number
1184// of control classes.
f03fc89f 1185void wxWindowBase::UpdateWindowUI()
7ec1983b 1186{
26bf1ce0
VZ
1187 wxUpdateUIEvent event(GetId());
1188 event.m_eventObject = this;
1189
1190 if ( GetEventHandler()->ProcessEvent(event) )
7ec1983b 1191 {
26bf1ce0
VZ
1192 if ( event.GetSetEnabled() )
1193 Enable(event.GetEnabled());
7ec1983b 1194
26bf1ce0 1195 if ( event.GetSetText() )
f03fc89f 1196 {
26bf1ce0
VZ
1197 wxControl *control = wxDynamicCast(this, wxControl);
1198 if ( control )
34636400 1199 {
26bf1ce0
VZ
1200 wxTextCtrl *text = wxDynamicCast(control, wxTextCtrl);
1201 if ( text )
1202 text->SetValue(event.GetText());
1203 else
34636400
VZ
1204 control->SetLabel(event.GetText());
1205 }
26bf1ce0 1206 }
f03fc89f 1207
88ac883a 1208#if wxUSE_CHECKBOX
26bf1ce0
VZ
1209 wxCheckBox *checkbox = wxDynamicCast(this, wxCheckBox);
1210 if ( checkbox )
1211 {
1212 if ( event.GetSetChecked() )
1213 checkbox->SetValue(event.GetChecked());
1214 }
88ac883a
VZ
1215#endif // wxUSE_CHECKBOX
1216
1217#if wxUSE_RADIOBUTTON
26bf1ce0
VZ
1218 wxRadioButton *radiobtn = wxDynamicCast(this, wxRadioButton);
1219 if ( radiobtn )
1220 {
1221 if ( event.GetSetChecked() )
1222 radiobtn->SetValue(event.GetChecked());
f03fc89f 1223 }
26bf1ce0 1224#endif // wxUSE_RADIOBUTTON
7ec1983b 1225 }
7ec1983b 1226}
fd71308f 1227
f03fc89f
VZ
1228// ----------------------------------------------------------------------------
1229// dialog units translations
1230// ----------------------------------------------------------------------------
1231
1232wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
fd71308f
JS
1233{
1234 int charWidth = GetCharWidth();
1235 int charHeight = GetCharHeight();
5d9c2818
RD
1236 wxPoint pt2(-1, -1);
1237 if (pt.x != -1)
1238 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1239 if (pt.y != -1)
1240 pt2.y = (int) ((pt.y * 8) / charHeight) ;
fd71308f
JS
1241
1242 return pt2;
1243}
1244
f03fc89f 1245wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
fd71308f
JS
1246{
1247 int charWidth = GetCharWidth();
1248 int charHeight = GetCharHeight();
5d9c2818
RD
1249 wxPoint pt2(-1, -1);
1250 if (pt.x != -1)
1251 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1252 if (pt.y != -1)
1253 pt2.y = (int) ((pt.y * charHeight) / 8) ;
fd71308f
JS
1254
1255 return pt2;
1256}
1257
8d99be5f
VZ
1258// ----------------------------------------------------------------------------
1259// client data
1260// ----------------------------------------------------------------------------
1261
1262void wxWindowBase::DoSetClientObject( wxClientData *data )
1263{
1264 wxASSERT_MSG( m_clientDataType != ClientData_Void,
223d09f6 1265 wxT("can't have both object and void client data") );
8d99be5f
VZ
1266
1267 if ( m_clientObject )
1268 delete m_clientObject;
1269
1270 m_clientObject = data;
1271 m_clientDataType = ClientData_Object;
1272}
1273
1274wxClientData *wxWindowBase::DoGetClientObject() const
1275{
1276 wxASSERT_MSG( m_clientDataType == ClientData_Object,
223d09f6 1277 wxT("this window doesn't have object client data") );
8d99be5f
VZ
1278
1279 return m_clientObject;
1280}
1281
1282void wxWindowBase::DoSetClientData( void *data )
1283{
1284 wxASSERT_MSG( m_clientDataType != ClientData_Object,
223d09f6 1285 wxT("can't have both object and void client data") );
8d99be5f
VZ
1286
1287 m_clientData = data;
1288 m_clientDataType = ClientData_Void;
1289}
1290
1291void *wxWindowBase::DoGetClientData() const
1292{
1293 wxASSERT_MSG( m_clientDataType == ClientData_Void,
223d09f6 1294 wxT("this window doesn't have void client data") );
8d99be5f
VZ
1295
1296 return m_clientData;
1297}
1298
f03fc89f
VZ
1299// ----------------------------------------------------------------------------
1300// event handlers
1301// ----------------------------------------------------------------------------
1302
1303// propagate the colour change event to the subwindows
1304void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1305{
1306 wxWindowList::Node *node = GetChildren().GetFirst();
1307 while ( node )
1308 {
1309 // Only propagate to non-top-level windows
1310 wxWindow *win = node->GetData();
1311 if ( !win->IsTopLevel() )
1312 {
1313 wxSysColourChangedEvent event2;
1314 event.m_eventObject = win;
1315 win->GetEventHandler()->ProcessEvent(event2);
1316 }
1317
1318 node = node->GetNext();
1319 }
1320}
1321
1322// the default action is to populate dialog with data when it's created
1323void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1324{
1325 TransferDataToWindow();
1326}
1327
1328// ----------------------------------------------------------------------------
1329// list classes implementation
1330// ----------------------------------------------------------------------------
1331
1332void wxWindowListNode::DeleteData()
1333{
1334 delete (wxWindow *)GetData();
1335}
1336