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