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