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