]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
Stop invalidating size on wxTLW
[wxWidgets.git] / src / common / wincmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/window.cpp
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$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #include "wx/frame.h"
32 #include "wx/defs.h"
33 #include "wx/window.h"
34 #include "wx/control.h"
35 #include "wx/checkbox.h"
36 #include "wx/radiobut.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
39 #include "wx/settings.h"
40 #include "wx/dialog.h"
41 #include "wx/msgdlg.h"
42 #include "wx/statusbr.h"
43 #include "wx/toolbar.h"
44 #include "wx/dcclient.h"
45 #endif //WX_PRECOMP
46
47 #if defined(__WXMAC__) && wxUSE_SCROLLBAR
48 #include "wx/scrolbar.h"
49 #endif
50
51 #if wxUSE_CONSTRAINTS
52 #include "wx/layout.h"
53 #endif // wxUSE_CONSTRAINTS
54
55 #include "wx/sizer.h"
56
57 #if wxUSE_DRAG_AND_DROP
58 #include "wx/dnd.h"
59 #endif // wxUSE_DRAG_AND_DROP
60
61 #if wxUSE_ACCESSIBILITY
62 #include "wx/access.h"
63 #endif
64
65 #if wxUSE_HELP
66 #include "wx/cshelp.h"
67 #endif // wxUSE_HELP
68
69 #if wxUSE_TOOLTIPS
70 #include "wx/tooltip.h"
71 #endif // wxUSE_TOOLTIPS
72
73 #if wxUSE_CARET
74 #include "wx/caret.h"
75 #endif // wxUSE_CARET
76
77 #if wxUSE_SYSTEM_OPTIONS
78 #include "wx/sysopt.h"
79 #endif
80
81 // For reporting compile- and runtime version of GTK+ in the ctrl+alt+mclick dialog.
82 // The gtk includes don't pull any other headers in, at least not on my system - MR
83 #ifdef __WXGTK__
84 #ifdef __WXGTK20__
85 #include <gtk/gtkversion.h>
86 #else
87 #include <gtk/gtkfeatures.h>
88 #endif
89 extern const unsigned int gtk_major_version;
90 extern const unsigned int gtk_minor_version;
91 extern const unsigned int gtk_micro_version;
92 #endif
93
94 // ----------------------------------------------------------------------------
95 // static data
96 // ----------------------------------------------------------------------------
97
98 #if defined(__WXPALMOS__)
99 int wxWindowBase::ms_lastControlId = 32767;
100 #elif defined(__WXPM__)
101 int wxWindowBase::ms_lastControlId = 2000;
102 #else
103 int wxWindowBase::ms_lastControlId = -200;
104 #endif
105
106 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
107
108 // ----------------------------------------------------------------------------
109 // event table
110 // ----------------------------------------------------------------------------
111
112 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
113 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
114 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
115 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
116
117 #if wxUSE_HELP
118 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
119 #endif // wxUSE_HELP
120
121 END_EVENT_TABLE()
122
123 // ============================================================================
124 // implementation of the common functionality of the wxWindow class
125 // ============================================================================
126
127 // ----------------------------------------------------------------------------
128 // initialization
129 // ----------------------------------------------------------------------------
130
131 // the default initialization
132 wxWindowBase::wxWindowBase()
133 {
134 // no window yet, no parent nor children
135 m_parent = (wxWindow *)NULL;
136 m_windowId = wxID_ANY;
137
138 // no constraints on the minimal window size
139 m_minWidth =
140 m_maxWidth = wxDefaultCoord;
141 m_minHeight =
142 m_maxHeight = wxDefaultCoord;
143
144 // invalidiated cache value
145 m_bestSizeCache = wxDefaultSize;
146
147 // window are created enabled and visible by default
148 m_isShown =
149 m_isEnabled = true;
150
151 // the default event handler is just this window
152 m_eventHandler = this;
153
154 #if wxUSE_VALIDATORS
155 // no validator
156 m_windowValidator = (wxValidator *) NULL;
157 #endif // wxUSE_VALIDATORS
158
159 // the colours/fonts are default for now, so leave m_font,
160 // m_backgroundColour and m_foregroundColour uninitialized and set those
161 m_hasBgCol =
162 m_hasFgCol =
163 m_hasFont = false;
164 m_inheritBgCol =
165 m_inheritFgCol =
166 m_inheritFont = false;
167
168 // no style bits
169 m_exStyle =
170 m_windowStyle = 0;
171
172 m_backgroundStyle = wxBG_STYLE_SYSTEM;
173
174 #if wxUSE_CONSTRAINTS
175 // no constraints whatsoever
176 m_constraints = (wxLayoutConstraints *) NULL;
177 m_constraintsInvolvedIn = (wxWindowList *) NULL;
178 #endif // wxUSE_CONSTRAINTS
179
180 m_windowSizer = (wxSizer *) NULL;
181 m_containingSizer = (wxSizer *) NULL;
182 m_autoLayout = false;
183
184 #if wxUSE_DRAG_AND_DROP
185 m_dropTarget = (wxDropTarget *)NULL;
186 #endif // wxUSE_DRAG_AND_DROP
187
188 #if wxUSE_TOOLTIPS
189 m_tooltip = (wxToolTip *)NULL;
190 #endif // wxUSE_TOOLTIPS
191
192 #if wxUSE_CARET
193 m_caret = (wxCaret *)NULL;
194 #endif // wxUSE_CARET
195
196 #if wxUSE_PALETTE
197 m_hasCustomPalette = false;
198 #endif // wxUSE_PALETTE
199
200 #if wxUSE_ACCESSIBILITY
201 m_accessible = NULL;
202 #endif
203
204 m_virtualSize = wxDefaultSize;
205
206 m_minVirtualWidth =
207 m_maxVirtualWidth = wxDefaultCoord;
208 m_minVirtualHeight =
209 m_maxVirtualHeight = wxDefaultCoord;
210
211 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
212 #if wxUSE_SYSTEM_OPTIONS
213 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT) )
214 {
215 m_windowVariant = (wxWindowVariant) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT ) ;
216 }
217 #endif
218
219 // Whether we're using the current theme for this window (wxGTK only for now)
220 m_themeEnabled = false;
221
222 // VZ: this one shouldn't exist...
223 m_isBeingDeleted = false;
224
225 // Reserved for future use
226 m_windowReserved = NULL;
227 }
228
229 // common part of window creation process
230 bool wxWindowBase::CreateBase(wxWindowBase *parent,
231 wxWindowID id,
232 const wxPoint& WXUNUSED(pos),
233 const wxSize& WXUNUSED(size),
234 long style,
235 const wxValidator& wxVALIDATOR_PARAM(validator),
236 const wxString& name)
237 {
238 #if wxUSE_STATBOX
239 // wxGTK doesn't allow to create controls with static box as the parent so
240 // this will result in a crash when the program is ported to wxGTK so warn
241 // the user about it
242
243 // if you get this assert, the correct solution is to create the controls
244 // as siblings of the static box
245 wxASSERT_MSG( !parent || !wxDynamicCast(parent, wxStaticBox),
246 _T("wxStaticBox can't be used as a window parent!") );
247 #endif // wxUSE_STATBOX
248
249 // ids are limited to 16 bits under MSW so if you care about portability,
250 // it's not a good idea to use ids out of this range (and negative ids are
251 // reserved for wxWidgets own usage)
252 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767),
253 _T("invalid id value") );
254
255 // generate a new id if the user doesn't care about it
256 m_windowId = id == wxID_ANY ? NewControlId() : id;
257
258 SetName(name);
259 SetWindowStyleFlag(style);
260 SetParent(parent);
261
262 #if wxUSE_VALIDATORS
263 SetValidator(validator);
264 #endif // wxUSE_VALIDATORS
265
266 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
267 // have it too - like this it's possible to set it only in the top level
268 // dialog/frame and all children will inherit it by defult
269 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
270 {
271 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
272 }
273
274 return true;
275 }
276
277 // ----------------------------------------------------------------------------
278 // destruction
279 // ----------------------------------------------------------------------------
280
281 // common clean up
282 wxWindowBase::~wxWindowBase()
283 {
284 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
285
286 // FIXME if these 2 cases result from programming errors in the user code
287 // we should probably assert here instead of silently fixing them
288
289 // Just in case the window has been Closed, but we're then deleting
290 // immediately: don't leave dangling pointers.
291 wxPendingDelete.DeleteObject(this);
292
293 // Just in case we've loaded a top-level window via LoadNativeDialog but
294 // we weren't a dialog class
295 wxTopLevelWindows.DeleteObject((wxWindow*)this);
296
297 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
298
299 // reset the dangling pointer our parent window may keep to us
300 if ( m_parent )
301 {
302 if ( m_parent->GetDefaultItem() == this )
303 {
304 m_parent->SetDefaultItem(NULL);
305 }
306
307 m_parent->RemoveChild(this);
308 }
309
310 #if wxUSE_CARET
311 delete m_caret;
312 #endif // wxUSE_CARET
313
314 #if wxUSE_VALIDATORS
315 delete m_windowValidator;
316 #endif // wxUSE_VALIDATORS
317
318 #if wxUSE_CONSTRAINTS
319 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
320 // at deleted windows as they delete themselves.
321 DeleteRelatedConstraints();
322
323 if ( m_constraints )
324 {
325 // This removes any dangling pointers to this window in other windows'
326 // constraintsInvolvedIn lists.
327 UnsetConstraints(m_constraints);
328 delete m_constraints;
329 m_constraints = NULL;
330 }
331 #endif // wxUSE_CONSTRAINTS
332
333 if ( m_containingSizer )
334 m_containingSizer->Detach( (wxWindow*)this );
335
336 delete m_windowSizer;
337
338 #if wxUSE_DRAG_AND_DROP
339 delete m_dropTarget;
340 #endif // wxUSE_DRAG_AND_DROP
341
342 #if wxUSE_TOOLTIPS
343 delete m_tooltip;
344 #endif // wxUSE_TOOLTIPS
345
346 #if wxUSE_ACCESSIBILITY
347 delete m_accessible;
348 #endif
349 }
350
351 bool wxWindowBase::Destroy()
352 {
353 delete this;
354
355 return true;
356 }
357
358 bool wxWindowBase::Close(bool force)
359 {
360 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
361 event.SetEventObject(this);
362 event.SetCanVeto(!force);
363
364 // return false if window wasn't closed because the application vetoed the
365 // close event
366 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
367 }
368
369 bool wxWindowBase::DestroyChildren()
370 {
371 wxWindowList::compatibility_iterator node;
372 for ( ;; )
373 {
374 // we iterate until the list becomes empty
375 node = GetChildren().GetFirst();
376 if ( !node )
377 break;
378
379 wxWindow *child = node->GetData();
380
381 // note that we really want to call delete and not ->Destroy() here
382 // because we want to delete the child immediately, before we are
383 // deleted, and delayed deletion would result in problems as our (top
384 // level) child could outlive its parent
385 delete child;
386
387 wxASSERT_MSG( !GetChildren().Find(child),
388 wxT("child didn't remove itself using RemoveChild()") );
389 }
390
391 return true;
392 }
393
394 // ----------------------------------------------------------------------------
395 // size/position related methods
396 // ----------------------------------------------------------------------------
397
398 // centre the window with respect to its parent in either (or both) directions
399 void wxWindowBase::DoCentre(int dir)
400 {
401 wxCHECK_RET( !(dir & wxCENTRE_ON_SCREEN) && GetParent(),
402 _T("this method only implements centering child windows") );
403
404 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir));
405 }
406
407 // fits the window around the children
408 void wxWindowBase::Fit()
409 {
410 if ( !GetChildren().empty() )
411 {
412 SetClientSize(GetBestSize());
413 }
414 //else: do nothing if we have no children
415 }
416
417 // fits virtual size (ie. scrolled area etc.) around children
418 void wxWindowBase::FitInside()
419 {
420 if ( GetChildren().GetCount() > 0 )
421 {
422 SetVirtualSize( GetBestVirtualSize() );
423 }
424 }
425
426 // On Mac, scrollbars are explicitly children.
427 #ifdef __WXMAC__
428 static bool wxHasRealChildren(const wxWindowBase* win)
429 {
430 int realChildCount = 0;
431
432 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
433 node;
434 node = node->GetNext() )
435 {
436 wxWindow *win = node->GetData();
437 if ( !win->IsTopLevel() && win->IsShown() && !win->IsKindOf(CLASSINFO(wxScrollBar)))
438 realChildCount ++;
439 }
440 return (realChildCount > 0);
441 }
442 #endif
443
444 void wxWindowBase::InvalidateBestSize()
445 {
446 m_bestSizeCache = wxDefaultSize;
447
448 // parent's best size calculation may depend on its children's
449 // best sizes, so let's invalidate it as well to be safe
450 // (but don't influence other wxTLWs)
451 if (m_parent && !IsTopLevel())
452 m_parent->InvalidateBestSize();
453 }
454
455 // return the size best suited for the current window
456 wxSize wxWindowBase::DoGetBestSize() const
457 {
458 wxSize best;
459
460 if ( m_windowSizer )
461 {
462 best = GetWindowSizeForVirtualSize(m_windowSizer->GetMinSize());
463 }
464 #if wxUSE_CONSTRAINTS
465 else if ( m_constraints )
466 {
467 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
468
469 // our minimal acceptable size is such that all our windows fit inside
470 int maxX = 0,
471 maxY = 0;
472
473 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
474 node;
475 node = node->GetNext() )
476 {
477 wxLayoutConstraints *c = node->GetData()->GetConstraints();
478 if ( !c )
479 {
480 // it's not normal that we have an unconstrained child, but
481 // what can we do about it?
482 continue;
483 }
484
485 int x = c->right.GetValue(),
486 y = c->bottom.GetValue();
487
488 if ( x > maxX )
489 maxX = x;
490
491 if ( y > maxY )
492 maxY = y;
493
494 // TODO: we must calculate the overlaps somehow, otherwise we
495 // will never return a size bigger than the current one :-(
496 }
497
498 best = wxSize(maxX, maxY);
499 }
500 #endif // wxUSE_CONSTRAINTS
501 else if ( !GetChildren().empty()
502 #ifdef __WXMAC__
503 && wxHasRealChildren(this)
504 #endif
505 )
506 {
507 // our minimal acceptable size is such that all our visible child windows fit inside
508 int maxX = 0,
509 maxY = 0;
510
511 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
512 node;
513 node = node->GetNext() )
514 {
515 wxWindow *win = node->GetData();
516 if ( win->IsTopLevel() || ( ! win->IsShown() )
517 #if wxUSE_STATUSBAR
518 || wxDynamicCast(win, wxStatusBar)
519 #endif // wxUSE_STATUSBAR
520 )
521 {
522 // dialogs and frames lie in different top level windows -
523 // don't deal with them here; as for the status bars, they
524 // don't lie in the client area at all
525 continue;
526 }
527
528 int wx, wy, ww, wh;
529 win->GetPosition(&wx, &wy);
530
531 // if the window hadn't been positioned yet, assume that it is in
532 // the origin
533 if ( wx == wxDefaultCoord )
534 wx = 0;
535 if ( wy == wxDefaultCoord )
536 wy = 0;
537
538 win->GetSize(&ww, &wh);
539 if ( wx + ww > maxX )
540 maxX = wx + ww;
541 if ( wy + wh > maxY )
542 maxY = wy + wh;
543 }
544
545 // for compatibility with the old versions and because it really looks
546 // slightly more pretty like this, add a pad
547 maxX += 7;
548 maxY += 14;
549
550 best = wxSize(maxX, maxY);
551 }
552 else // ! has children
553 {
554 // for a generic window there is no natural best size so, if the
555 // minimal size is not set, use the current size but take care to
556 // remember it as minimal size for the next time because our best size
557 // should be constant: otherwise we could get into a situation when the
558 // window is initially at some size, then expanded to a larger size and
559 // then, when the containing window is shrunk back (because our initial
560 // best size had been used for computing the parent min size), we can't
561 // be shrunk back any more because our best size is now bigger
562 wxSize size = GetMinSize();
563 if ( !size.IsFullySpecified() )
564 {
565 size.SetDefaults(GetSize());
566 wxConstCast(this, wxWindowBase)->SetMinSize(size);
567 }
568
569 // return as-is, unadjusted by the client size difference.
570 return size;
571 }
572
573 // Add any difference between size and client size
574 wxSize diff = GetSize() - GetClientSize();
575 best.x += wxMax(0, diff.x);
576 best.y += wxMax(0, diff.y);
577
578 return best;
579 }
580
581
582 wxSize wxWindowBase::GetBestFittingSize() const
583 {
584 // merge the best size with the min size, giving priority to the min size
585 wxSize min = GetMinSize();
586 if (min.x == wxDefaultCoord || min.y == wxDefaultCoord)
587 {
588 wxSize best = GetBestSize();
589 if (min.x == wxDefaultCoord) min.x = best.x;
590 if (min.y == wxDefaultCoord) min.y = best.y;
591 }
592 return min;
593 }
594
595
596 void wxWindowBase::SetBestFittingSize(const wxSize& size)
597 {
598 // Set the min size to the size passed in. This will usually either be
599 // wxDefaultSize or the size passed to this window's ctor/Create function.
600 SetMinSize(size);
601
602 // Merge the size with the best size if needed
603 wxSize best = GetBestFittingSize();
604
605 // If the current size doesn't match then change it
606 if (GetSize() != best)
607 SetSize(best);
608 }
609
610
611 // by default the origin is not shifted
612 wxPoint wxWindowBase::GetClientAreaOrigin() const
613 {
614 return wxPoint(0,0);
615 }
616
617 // set the min/max size of the window
618 void wxWindowBase::DoSetSizeHints(int minW, int minH,
619 int maxW, int maxH,
620 int WXUNUSED(incW), int WXUNUSED(incH))
621 {
622 // setting min width greater than max width leads to infinite loops under
623 // X11 and generally doesn't make any sense, so don't allow it
624 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
625 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
626 _T("min width/height must be less than max width/height!") );
627
628 m_minWidth = minW;
629 m_maxWidth = maxW;
630 m_minHeight = minH;
631 m_maxHeight = maxH;
632 }
633
634 void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
635 {
636 if ( m_windowVariant != variant )
637 {
638 m_windowVariant = variant;
639
640 DoSetWindowVariant(variant);
641 }
642 }
643
644 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
645 {
646 // adjust the font height to correspond to our new variant (notice that
647 // we're only called if something really changed)
648 wxFont font = GetFont();
649 int size = font.GetPointSize();
650 switch ( variant )
651 {
652 case wxWINDOW_VARIANT_NORMAL:
653 break;
654
655 case wxWINDOW_VARIANT_SMALL:
656 size *= 3;
657 size /= 4;
658 break;
659
660 case wxWINDOW_VARIANT_MINI:
661 size *= 2;
662 size /= 3;
663 break;
664
665 case wxWINDOW_VARIANT_LARGE:
666 size *= 5;
667 size /= 4;
668 break;
669
670 default:
671 wxFAIL_MSG(_T("unexpected window variant"));
672 break;
673 }
674
675 font.SetPointSize(size);
676 SetFont(font);
677 }
678
679 void wxWindowBase::SetVirtualSizeHints( int minW, int minH,
680 int maxW, int maxH )
681 {
682 m_minVirtualWidth = minW;
683 m_maxVirtualWidth = maxW;
684 m_minVirtualHeight = minH;
685 m_maxVirtualHeight = maxH;
686 }
687
688 void wxWindowBase::DoSetVirtualSize( int x, int y )
689 {
690 if ( m_minVirtualWidth != wxDefaultCoord && m_minVirtualWidth > x )
691 x = m_minVirtualWidth;
692 if ( m_maxVirtualWidth != wxDefaultCoord && m_maxVirtualWidth < x )
693 x = m_maxVirtualWidth;
694 if ( m_minVirtualHeight != wxDefaultCoord && m_minVirtualHeight > y )
695 y = m_minVirtualHeight;
696 if ( m_maxVirtualHeight != wxDefaultCoord && m_maxVirtualHeight < y )
697 y = m_maxVirtualHeight;
698
699 m_virtualSize = wxSize(x, y);
700 }
701
702 wxSize wxWindowBase::DoGetVirtualSize() const
703 {
704 // we should use the entire client area so if it is greater than our
705 // virtual size, expand it to fit (otherwise if the window is big enough we
706 // wouldn't be using parts of it)
707 wxSize size = GetClientSize();
708 if ( m_virtualSize.x > size.x )
709 size.x = m_virtualSize.x;
710
711 if ( m_virtualSize.y >= size.y )
712 size.y = m_virtualSize.y;
713
714 return size;
715 }
716
717 // ----------------------------------------------------------------------------
718 // show/hide/enable/disable the window
719 // ----------------------------------------------------------------------------
720
721 bool wxWindowBase::Show(bool show)
722 {
723 if ( show != m_isShown )
724 {
725 m_isShown = show;
726
727 return true;
728 }
729 else
730 {
731 return false;
732 }
733 }
734
735 bool wxWindowBase::Enable(bool enable)
736 {
737 if ( enable != m_isEnabled )
738 {
739 m_isEnabled = enable;
740
741 return true;
742 }
743 else
744 {
745 return false;
746 }
747 }
748 // ----------------------------------------------------------------------------
749 // RTTI
750 // ----------------------------------------------------------------------------
751
752 bool wxWindowBase::IsTopLevel() const
753 {
754 return false;
755 }
756
757 // ----------------------------------------------------------------------------
758 // reparenting the window
759 // ----------------------------------------------------------------------------
760
761 void wxWindowBase::AddChild(wxWindowBase *child)
762 {
763 wxCHECK_RET( child, wxT("can't add a NULL child") );
764
765 // this should never happen and it will lead to a crash later if it does
766 // because RemoveChild() will remove only one node from the children list
767 // and the other(s) one(s) will be left with dangling pointers in them
768 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") );
769
770 GetChildren().Append((wxWindow*)child);
771 child->SetParent(this);
772 }
773
774 void wxWindowBase::RemoveChild(wxWindowBase *child)
775 {
776 wxCHECK_RET( child, wxT("can't remove a NULL child") );
777
778 GetChildren().DeleteObject((wxWindow *)child);
779 child->SetParent(NULL);
780 }
781
782 bool wxWindowBase::Reparent(wxWindowBase *newParent)
783 {
784 wxWindow *oldParent = GetParent();
785 if ( newParent == oldParent )
786 {
787 // nothing done
788 return false;
789 }
790
791 // unlink this window from the existing parent.
792 if ( oldParent )
793 {
794 oldParent->RemoveChild(this);
795 }
796 else
797 {
798 wxTopLevelWindows.DeleteObject((wxWindow *)this);
799 }
800
801 // add it to the new one
802 if ( newParent )
803 {
804 newParent->AddChild(this);
805 }
806 else
807 {
808 wxTopLevelWindows.Append((wxWindow *)this);
809 }
810
811 return true;
812 }
813
814 // ----------------------------------------------------------------------------
815 // event handler stuff
816 // ----------------------------------------------------------------------------
817
818 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
819 {
820 wxEvtHandler *handlerOld = GetEventHandler();
821
822 handler->SetNextHandler(handlerOld);
823
824 if ( handlerOld )
825 GetEventHandler()->SetPreviousHandler(handler);
826
827 SetEventHandler(handler);
828 }
829
830 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
831 {
832 wxEvtHandler *handlerA = GetEventHandler();
833 if ( handlerA )
834 {
835 wxEvtHandler *handlerB = handlerA->GetNextHandler();
836 handlerA->SetNextHandler((wxEvtHandler *)NULL);
837
838 if ( handlerB )
839 handlerB->SetPreviousHandler((wxEvtHandler *)NULL);
840 SetEventHandler(handlerB);
841
842 if ( deleteHandler )
843 {
844 delete handlerA;
845 handlerA = (wxEvtHandler *)NULL;
846 }
847 }
848
849 return handlerA;
850 }
851
852 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handler)
853 {
854 wxCHECK_MSG( handler, false, _T("RemoveEventHandler(NULL) called") );
855
856 wxEvtHandler *handlerPrev = NULL,
857 *handlerCur = GetEventHandler();
858 while ( handlerCur )
859 {
860 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
861
862 if ( handlerCur == handler )
863 {
864 if ( handlerPrev )
865 {
866 handlerPrev->SetNextHandler(handlerNext);
867 }
868 else
869 {
870 SetEventHandler(handlerNext);
871 }
872
873 if ( handlerNext )
874 {
875 handlerNext->SetPreviousHandler ( handlerPrev );
876 }
877
878 handler->SetNextHandler(NULL);
879 handler->SetPreviousHandler(NULL);
880
881 return true;
882 }
883
884 handlerPrev = handlerCur;
885 handlerCur = handlerNext;
886 }
887
888 wxFAIL_MSG( _T("where has the event handler gone?") );
889
890 return false;
891 }
892
893 // ----------------------------------------------------------------------------
894 // colours, fonts &c
895 // ----------------------------------------------------------------------------
896
897 void wxWindowBase::InheritAttributes()
898 {
899 const wxWindowBase * const parent = GetParent();
900 if ( !parent )
901 return;
902
903 // we only inherit attributes which had been explicitly set for the parent
904 // which ensures that this only happens if the user really wants it and
905 // not by default which wouldn't make any sense in modern GUIs where the
906 // controls don't all use the same fonts (nor colours)
907 if ( parent->m_inheritFont && !m_hasFont )
908 SetFont(parent->GetFont());
909
910 // in addition, there is a possibility to explicitly forbid inheriting
911 // colours at each class level by overriding ShouldInheritColours()
912 if ( ShouldInheritColours() )
913 {
914 if ( parent->m_inheritFgCol && !m_hasFgCol )
915 SetForegroundColour(parent->GetForegroundColour());
916
917 // inheriting (solid) background colour is wrong as it totally breaks
918 // any kind of themed backgrounds
919 //
920 // instead, the controls should use the same background as their parent
921 // (ideally by not drawing it at all)
922 #if 0
923 if ( parent->m_inheritBgCol && !m_hasBgCol )
924 SetBackgroundColour(parent->GetBackgroundColour());
925 #endif // 0
926 }
927 }
928
929 /* static */ wxVisualAttributes
930 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
931 {
932 // it is important to return valid values for all attributes from here,
933 // GetXXX() below rely on this
934 wxVisualAttributes attrs;
935 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
936 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
937
938 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
939 // the usual background colour than wxSYS_COLOUR_BTNFACE.
940 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
941 // colour on other platforms.
942
943 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
944 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
945 #else
946 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
947 #endif
948 return attrs;
949 }
950
951 wxColour wxWindowBase::GetBackgroundColour() const
952 {
953 if ( !m_backgroundColour.Ok() )
954 {
955 wxASSERT_MSG( !m_hasBgCol, _T("we have invalid explicit bg colour?") );
956
957 // get our default background colour
958 wxColour colBg = GetDefaultAttributes().colBg;
959
960 // we must return some valid colour to avoid redoing this every time
961 // and also to avoid surprizing the applications written for older
962 // wxWidgets versions where GetBackgroundColour() always returned
963 // something -- so give them something even if it doesn't make sense
964 // for this window (e.g. it has a themed background)
965 if ( !colBg.Ok() )
966 colBg = GetClassDefaultAttributes().colBg;
967
968 return colBg;
969 }
970 else
971 return m_backgroundColour;
972 }
973
974 wxColour wxWindowBase::GetForegroundColour() const
975 {
976 // logic is the same as above
977 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
978 {
979 wxASSERT_MSG( !m_hasFgCol, _T("we have invalid explicit fg colour?") );
980
981 wxColour colFg = GetDefaultAttributes().colFg;
982
983 if ( !colFg.Ok() )
984 colFg = GetClassDefaultAttributes().colFg;
985
986 return colFg;
987 }
988 else
989 return m_foregroundColour;
990 }
991
992 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
993 {
994 if ( colour == m_backgroundColour )
995 return false;
996
997 m_hasBgCol = colour.Ok();
998 if ( m_backgroundStyle != wxBG_STYLE_CUSTOM )
999 m_backgroundStyle = m_hasBgCol ? wxBG_STYLE_COLOUR : wxBG_STYLE_SYSTEM;
1000
1001 m_inheritBgCol = m_hasBgCol;
1002 m_backgroundColour = colour;
1003 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
1004 return true;
1005 }
1006
1007 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1008 {
1009 if (colour == m_foregroundColour )
1010 return false;
1011
1012 m_hasFgCol = colour.Ok();
1013 m_inheritFgCol = m_hasFgCol;
1014 m_foregroundColour = colour;
1015 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
1016 return true;
1017 }
1018
1019 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1020 {
1021 // setting an invalid cursor is ok, it means that we don't have any special
1022 // cursor
1023 if ( m_cursor == cursor )
1024 {
1025 // no change
1026 return false;
1027 }
1028
1029 m_cursor = cursor;
1030
1031 return true;
1032 }
1033
1034 wxFont wxWindowBase::GetFont() const
1035 {
1036 // logic is the same as in GetBackgroundColour()
1037 if ( !m_font.Ok() )
1038 {
1039 wxASSERT_MSG( !m_hasFont, _T("we have invalid explicit font?") );
1040
1041 wxFont font = GetDefaultAttributes().font;
1042 if ( !font.Ok() )
1043 font = GetClassDefaultAttributes().font;
1044
1045 return font;
1046 }
1047 else
1048 return m_font;
1049 }
1050
1051 bool wxWindowBase::SetFont(const wxFont& font)
1052 {
1053 if ( font == m_font )
1054 {
1055 // no change
1056 return false;
1057 }
1058
1059 m_font = font;
1060 m_hasFont = font.Ok();
1061 m_inheritFont = m_hasFont;
1062
1063 InvalidateBestSize();
1064
1065 return true;
1066 }
1067
1068 #if wxUSE_PALETTE
1069
1070 void wxWindowBase::SetPalette(const wxPalette& pal)
1071 {
1072 m_hasCustomPalette = true;
1073 m_palette = pal;
1074
1075 // VZ: can anyone explain me what do we do here?
1076 wxWindowDC d((wxWindow *) this);
1077 d.SetPalette(pal);
1078 }
1079
1080 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1081 {
1082 wxWindow *win = (wxWindow *)this;
1083 while ( win && !win->HasCustomPalette() )
1084 {
1085 win = win->GetParent();
1086 }
1087
1088 return win;
1089 }
1090
1091 #endif // wxUSE_PALETTE
1092
1093 #if wxUSE_CARET
1094 void wxWindowBase::SetCaret(wxCaret *caret)
1095 {
1096 if ( m_caret )
1097 {
1098 delete m_caret;
1099 }
1100
1101 m_caret = caret;
1102
1103 if ( m_caret )
1104 {
1105 wxASSERT_MSG( m_caret->GetWindow() == this,
1106 wxT("caret should be created associated to this window") );
1107 }
1108 }
1109 #endif // wxUSE_CARET
1110
1111 #if wxUSE_VALIDATORS
1112 // ----------------------------------------------------------------------------
1113 // validators
1114 // ----------------------------------------------------------------------------
1115
1116 void wxWindowBase::SetValidator(const wxValidator& validator)
1117 {
1118 if ( m_windowValidator )
1119 delete m_windowValidator;
1120
1121 m_windowValidator = (wxValidator *)validator.Clone();
1122
1123 if ( m_windowValidator )
1124 m_windowValidator->SetWindow(this);
1125 }
1126 #endif // wxUSE_VALIDATORS
1127
1128 // ----------------------------------------------------------------------------
1129 // update region stuff
1130 // ----------------------------------------------------------------------------
1131
1132 wxRect wxWindowBase::GetUpdateClientRect() const
1133 {
1134 wxRegion rgnUpdate = GetUpdateRegion();
1135 rgnUpdate.Intersect(GetClientRect());
1136 wxRect rectUpdate = rgnUpdate.GetBox();
1137 wxPoint ptOrigin = GetClientAreaOrigin();
1138 rectUpdate.x -= ptOrigin.x;
1139 rectUpdate.y -= ptOrigin.y;
1140
1141 return rectUpdate;
1142 }
1143
1144 bool wxWindowBase::IsExposed(int x, int y) const
1145 {
1146 return m_updateRegion.Contains(x, y) != wxOutRegion;
1147 }
1148
1149 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
1150 {
1151 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1152 }
1153
1154 void wxWindowBase::ClearBackground()
1155 {
1156 // wxGTK uses its own version, no need to add never used code
1157 #ifndef __WXGTK__
1158 wxClientDC dc((wxWindow *)this);
1159 wxBrush brush(GetBackgroundColour(), wxSOLID);
1160 dc.SetBackground(brush);
1161 dc.Clear();
1162 #endif // __WXGTK__
1163 }
1164
1165 // ----------------------------------------------------------------------------
1166 // find child window by id or name
1167 // ----------------------------------------------------------------------------
1168
1169 wxWindow *wxWindowBase::FindWindow(long id) const
1170 {
1171 if ( id == m_windowId )
1172 return (wxWindow *)this;
1173
1174 wxWindowBase *res = (wxWindow *)NULL;
1175 wxWindowList::compatibility_iterator node;
1176 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1177 {
1178 wxWindowBase *child = node->GetData();
1179 res = child->FindWindow( id );
1180 }
1181
1182 return (wxWindow *)res;
1183 }
1184
1185 wxWindow *wxWindowBase::FindWindow(const wxString& name) const
1186 {
1187 if ( name == m_windowName )
1188 return (wxWindow *)this;
1189
1190 wxWindowBase *res = (wxWindow *)NULL;
1191 wxWindowList::compatibility_iterator node;
1192 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1193 {
1194 wxWindow *child = node->GetData();
1195 res = child->FindWindow(name);
1196 }
1197
1198 return (wxWindow *)res;
1199 }
1200
1201
1202 // find any window by id or name or label: If parent is non-NULL, look through
1203 // children for a label or title matching the specified string. If NULL, look
1204 // through all top-level windows.
1205 //
1206 // to avoid duplicating code we reuse the same helper function but with
1207 // different comparators
1208
1209 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1210 const wxString& label, long id);
1211
1212 static
1213 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1214 long WXUNUSED(id))
1215 {
1216 return win->GetLabel() == label;
1217 }
1218
1219 static
1220 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1221 long WXUNUSED(id))
1222 {
1223 return win->GetName() == label;
1224 }
1225
1226 static
1227 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1228 long id)
1229 {
1230 return win->GetId() == id;
1231 }
1232
1233 // recursive helper for the FindWindowByXXX() functions
1234 static
1235 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1236 const wxString& label,
1237 long id,
1238 wxFindWindowCmp cmp)
1239 {
1240 if ( parent )
1241 {
1242 // see if this is the one we're looking for
1243 if ( (*cmp)(parent, label, id) )
1244 return (wxWindow *)parent;
1245
1246 // It wasn't, so check all its children
1247 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1248 node;
1249 node = node->GetNext() )
1250 {
1251 // recursively check each child
1252 wxWindow *win = (wxWindow *)node->GetData();
1253 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1254 if (retwin)
1255 return retwin;
1256 }
1257 }
1258
1259 // Not found
1260 return NULL;
1261 }
1262
1263 // helper for FindWindowByXXX()
1264 static
1265 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1266 const wxString& label,
1267 long id,
1268 wxFindWindowCmp cmp)
1269 {
1270 if ( parent )
1271 {
1272 // just check parent and all its children
1273 return wxFindWindowRecursively(parent, label, id, cmp);
1274 }
1275
1276 // start at very top of wx's windows
1277 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1278 node;
1279 node = node->GetNext() )
1280 {
1281 // recursively check each window & its children
1282 wxWindow *win = node->GetData();
1283 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1284 if (retwin)
1285 return retwin;
1286 }
1287
1288 return NULL;
1289 }
1290
1291 /* static */
1292 wxWindow *
1293 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1294 {
1295 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1296 }
1297
1298 /* static */
1299 wxWindow *
1300 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1301 {
1302 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1303
1304 if ( !win )
1305 {
1306 // fall back to the label
1307 win = FindWindowByLabel(title, parent);
1308 }
1309
1310 return win;
1311 }
1312
1313 /* static */
1314 wxWindow *
1315 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1316 {
1317 return wxFindWindowHelper(parent, wxEmptyString, id, wxFindWindowCmpIds);
1318 }
1319
1320 // ----------------------------------------------------------------------------
1321 // dialog oriented functions
1322 // ----------------------------------------------------------------------------
1323
1324 void wxWindowBase::MakeModal(bool modal)
1325 {
1326 // Disable all other windows
1327 if ( IsTopLevel() )
1328 {
1329 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1330 while (node)
1331 {
1332 wxWindow *win = node->GetData();
1333 if (win != this)
1334 win->Enable(!modal);
1335
1336 node = node->GetNext();
1337 }
1338 }
1339 }
1340
1341 bool wxWindowBase::Validate()
1342 {
1343 #if wxUSE_VALIDATORS
1344 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1345
1346 wxWindowList::compatibility_iterator node;
1347 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1348 {
1349 wxWindowBase *child = node->GetData();
1350 wxValidator *validator = child->GetValidator();
1351 if ( validator && !validator->Validate((wxWindow *)this) )
1352 {
1353 return false;
1354 }
1355
1356 if ( recurse && !child->Validate() )
1357 {
1358 return false;
1359 }
1360 }
1361 #endif // wxUSE_VALIDATORS
1362
1363 return true;
1364 }
1365
1366 bool wxWindowBase::TransferDataToWindow()
1367 {
1368 #if wxUSE_VALIDATORS
1369 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1370
1371 wxWindowList::compatibility_iterator node;
1372 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1373 {
1374 wxWindowBase *child = node->GetData();
1375 wxValidator *validator = child->GetValidator();
1376 if ( validator && !validator->TransferToWindow() )
1377 {
1378 wxLogWarning(_("Could not transfer data to window"));
1379 #if wxUSE_LOG
1380 wxLog::FlushActive();
1381 #endif // wxUSE_LOG
1382
1383 return false;
1384 }
1385
1386 if ( recurse )
1387 {
1388 if ( !child->TransferDataToWindow() )
1389 {
1390 // warning already given
1391 return false;
1392 }
1393 }
1394 }
1395 #endif // wxUSE_VALIDATORS
1396
1397 return true;
1398 }
1399
1400 bool wxWindowBase::TransferDataFromWindow()
1401 {
1402 #if wxUSE_VALIDATORS
1403 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1404
1405 wxWindowList::compatibility_iterator node;
1406 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1407 {
1408 wxWindow *child = node->GetData();
1409 wxValidator *validator = child->GetValidator();
1410 if ( validator && !validator->TransferFromWindow() )
1411 {
1412 // nop warning here because the application is supposed to give
1413 // one itself - we don't know here what might have gone wrongly
1414
1415 return false;
1416 }
1417
1418 if ( recurse )
1419 {
1420 if ( !child->TransferDataFromWindow() )
1421 {
1422 // warning already given
1423 return false;
1424 }
1425 }
1426 }
1427 #endif // wxUSE_VALIDATORS
1428
1429 return true;
1430 }
1431
1432 void wxWindowBase::InitDialog()
1433 {
1434 wxInitDialogEvent event(GetId());
1435 event.SetEventObject( this );
1436 GetEventHandler()->ProcessEvent(event);
1437 }
1438
1439 // ----------------------------------------------------------------------------
1440 // context-sensitive help support
1441 // ----------------------------------------------------------------------------
1442
1443 #if wxUSE_HELP
1444
1445 // associate this help text with this window
1446 void wxWindowBase::SetHelpText(const wxString& text)
1447 {
1448 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1449 if ( helpProvider )
1450 {
1451 helpProvider->AddHelp(this, text);
1452 }
1453 }
1454
1455 // associate this help text with all windows with the same id as this
1456 // one
1457 void wxWindowBase::SetHelpTextForId(const wxString& text)
1458 {
1459 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1460 if ( helpProvider )
1461 {
1462 helpProvider->AddHelp(GetId(), text);
1463 }
1464 }
1465
1466 // get the help string associated with this window (may be empty)
1467 wxString wxWindowBase::GetHelpText() const
1468 {
1469 wxString text;
1470 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1471 if ( helpProvider )
1472 {
1473 text = helpProvider->GetHelp(this);
1474 }
1475
1476 return text;
1477 }
1478
1479 // show help for this window
1480 void wxWindowBase::OnHelp(wxHelpEvent& event)
1481 {
1482 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1483 if ( helpProvider )
1484 {
1485 if ( helpProvider->ShowHelp(this) )
1486 {
1487 // skip the event.Skip() below
1488 return;
1489 }
1490 }
1491
1492 event.Skip();
1493 }
1494
1495 #endif // wxUSE_HELP
1496
1497 // ----------------------------------------------------------------------------
1498 // tooltipsroot.Replace("\\", "/");
1499 // ----------------------------------------------------------------------------
1500
1501 #if wxUSE_TOOLTIPS
1502
1503 void wxWindowBase::SetToolTip( const wxString &tip )
1504 {
1505 // don't create the new tooltip if we already have one
1506 if ( m_tooltip )
1507 {
1508 m_tooltip->SetTip( tip );
1509 }
1510 else
1511 {
1512 SetToolTip( new wxToolTip( tip ) );
1513 }
1514
1515 // setting empty tooltip text does not remove the tooltip any more - use
1516 // SetToolTip((wxToolTip *)NULL) for this
1517 }
1518
1519 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1520 {
1521 if ( m_tooltip )
1522 delete m_tooltip;
1523
1524 m_tooltip = tooltip;
1525 }
1526
1527 #endif // wxUSE_TOOLTIPS
1528
1529 // ----------------------------------------------------------------------------
1530 // constraints and sizers
1531 // ----------------------------------------------------------------------------
1532
1533 #if wxUSE_CONSTRAINTS
1534
1535 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1536 {
1537 if ( m_constraints )
1538 {
1539 UnsetConstraints(m_constraints);
1540 delete m_constraints;
1541 }
1542 m_constraints = constraints;
1543 if ( m_constraints )
1544 {
1545 // Make sure other windows know they're part of a 'meaningful relationship'
1546 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1547 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1548 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1549 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1550 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1551 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1552 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1553 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1554 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1555 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1556 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1557 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1558 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1559 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1560 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1561 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1562 }
1563 }
1564
1565 // This removes any dangling pointers to this window in other windows'
1566 // constraintsInvolvedIn lists.
1567 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1568 {
1569 if ( c )
1570 {
1571 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1572 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1573 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1574 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1575 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1576 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1577 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1578 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1579 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1580 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1581 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1582 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1583 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1584 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1585 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1586 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1587 }
1588 }
1589
1590 // Back-pointer to other windows we're involved with, so if we delete this
1591 // window, we must delete any constraints we're involved with.
1592 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1593 {
1594 if ( !m_constraintsInvolvedIn )
1595 m_constraintsInvolvedIn = new wxWindowList;
1596 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
1597 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
1598 }
1599
1600 // REMOVE back-pointer to other windows we're involved with.
1601 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1602 {
1603 if ( m_constraintsInvolvedIn )
1604 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
1605 }
1606
1607 // Reset any constraints that mention this window
1608 void wxWindowBase::DeleteRelatedConstraints()
1609 {
1610 if ( m_constraintsInvolvedIn )
1611 {
1612 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
1613 while (node)
1614 {
1615 wxWindow *win = node->GetData();
1616 wxLayoutConstraints *constr = win->GetConstraints();
1617
1618 // Reset any constraints involving this window
1619 if ( constr )
1620 {
1621 constr->left.ResetIfWin(this);
1622 constr->top.ResetIfWin(this);
1623 constr->right.ResetIfWin(this);
1624 constr->bottom.ResetIfWin(this);
1625 constr->width.ResetIfWin(this);
1626 constr->height.ResetIfWin(this);
1627 constr->centreX.ResetIfWin(this);
1628 constr->centreY.ResetIfWin(this);
1629 }
1630
1631 wxWindowList::compatibility_iterator next = node->GetNext();
1632 m_constraintsInvolvedIn->Erase(node);
1633 node = next;
1634 }
1635
1636 delete m_constraintsInvolvedIn;
1637 m_constraintsInvolvedIn = (wxWindowList *) NULL;
1638 }
1639 }
1640
1641 #endif // wxUSE_CONSTRAINTS
1642
1643 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
1644 {
1645 if ( sizer == m_windowSizer)
1646 return;
1647
1648 if ( deleteOld )
1649 delete m_windowSizer;
1650
1651 m_windowSizer = sizer;
1652
1653 SetAutoLayout( sizer != NULL );
1654 }
1655
1656 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
1657 {
1658 SetSizer( sizer, deleteOld );
1659 sizer->SetSizeHints( (wxWindow*) this );
1660 }
1661
1662
1663 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
1664 {
1665 // adding a window to a sizer twice is going to result in fatal and
1666 // hard to debug problems later because when deleting the second
1667 // associated wxSizerItem we're going to dereference a dangling
1668 // pointer; so try to detect this as early as possible
1669 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
1670 _T("Adding a window to the same sizer twice?") );
1671
1672 m_containingSizer = sizer;
1673 }
1674
1675 #if wxUSE_CONSTRAINTS
1676
1677 void wxWindowBase::SatisfyConstraints()
1678 {
1679 wxLayoutConstraints *constr = GetConstraints();
1680 bool wasOk = constr && constr->AreSatisfied();
1681
1682 ResetConstraints(); // Mark all constraints as unevaluated
1683
1684 int noChanges = 1;
1685
1686 // if we're a top level panel (i.e. our parent is frame/dialog), our
1687 // own constraints will never be satisfied any more unless we do it
1688 // here
1689 if ( wasOk )
1690 {
1691 while ( noChanges > 0 )
1692 {
1693 LayoutPhase1(&noChanges);
1694 }
1695 }
1696
1697 LayoutPhase2(&noChanges);
1698 }
1699
1700 #endif // wxUSE_CONSTRAINTS
1701
1702 bool wxWindowBase::Layout()
1703 {
1704 // If there is a sizer, use it instead of the constraints
1705 if ( GetSizer() )
1706 {
1707 int w = 0, h = 0;
1708 GetVirtualSize(&w, &h);
1709 GetSizer()->SetDimension( 0, 0, w, h );
1710 }
1711 #if wxUSE_CONSTRAINTS
1712 else
1713 {
1714 SatisfyConstraints(); // Find the right constraints values
1715 SetConstraintSizes(); // Recursively set the real window sizes
1716 }
1717 #endif
1718
1719 return true;
1720 }
1721
1722 #if wxUSE_CONSTRAINTS
1723
1724 // first phase of the constraints evaluation: set our own constraints
1725 bool wxWindowBase::LayoutPhase1(int *noChanges)
1726 {
1727 wxLayoutConstraints *constr = GetConstraints();
1728
1729 return !constr || constr->SatisfyConstraints(this, noChanges);
1730 }
1731
1732 // second phase: set the constraints for our children
1733 bool wxWindowBase::LayoutPhase2(int *noChanges)
1734 {
1735 *noChanges = 0;
1736
1737 // Layout children
1738 DoPhase(1);
1739
1740 // Layout grand children
1741 DoPhase(2);
1742
1743 return true;
1744 }
1745
1746 // Do a phase of evaluating child constraints
1747 bool wxWindowBase::DoPhase(int phase)
1748 {
1749 // the list containing the children for which the constraints are already
1750 // set correctly
1751 wxWindowList succeeded;
1752
1753 // the max number of iterations we loop before concluding that we can't set
1754 // the constraints
1755 static const int maxIterations = 500;
1756
1757 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
1758 {
1759 int noChanges = 0;
1760
1761 // loop over all children setting their constraints
1762 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1763 node;
1764 node = node->GetNext() )
1765 {
1766 wxWindow *child = node->GetData();
1767 if ( child->IsTopLevel() )
1768 {
1769 // top level children are not inside our client area
1770 continue;
1771 }
1772
1773 if ( !child->GetConstraints() || succeeded.Find(child) )
1774 {
1775 // this one is either already ok or nothing we can do about it
1776 continue;
1777 }
1778
1779 int tempNoChanges = 0;
1780 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
1781 : child->LayoutPhase2(&tempNoChanges);
1782 noChanges += tempNoChanges;
1783
1784 if ( success )
1785 {
1786 succeeded.Append(child);
1787 }
1788 }
1789
1790 if ( !noChanges )
1791 {
1792 // constraints are set
1793 break;
1794 }
1795 }
1796
1797 return true;
1798 }
1799
1800 void wxWindowBase::ResetConstraints()
1801 {
1802 wxLayoutConstraints *constr = GetConstraints();
1803 if ( constr )
1804 {
1805 constr->left.SetDone(false);
1806 constr->top.SetDone(false);
1807 constr->right.SetDone(false);
1808 constr->bottom.SetDone(false);
1809 constr->width.SetDone(false);
1810 constr->height.SetDone(false);
1811 constr->centreX.SetDone(false);
1812 constr->centreY.SetDone(false);
1813 }
1814
1815 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1816 while (node)
1817 {
1818 wxWindow *win = node->GetData();
1819 if ( !win->IsTopLevel() )
1820 win->ResetConstraints();
1821 node = node->GetNext();
1822 }
1823 }
1824
1825 // Need to distinguish between setting the 'fake' size for windows and sizers,
1826 // and setting the real values.
1827 void wxWindowBase::SetConstraintSizes(bool recurse)
1828 {
1829 wxLayoutConstraints *constr = GetConstraints();
1830 if ( constr && constr->AreSatisfied() )
1831 {
1832 int x = constr->left.GetValue();
1833 int y = constr->top.GetValue();
1834 int w = constr->width.GetValue();
1835 int h = constr->height.GetValue();
1836
1837 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1838 (constr->height.GetRelationship() != wxAsIs) )
1839 {
1840 SetSize(x, y, w, h);
1841 }
1842 else
1843 {
1844 // If we don't want to resize this window, just move it...
1845 Move(x, y);
1846 }
1847 }
1848 else if ( constr )
1849 {
1850 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1851 GetClassInfo()->GetClassName(),
1852 GetName().c_str());
1853 }
1854
1855 if ( recurse )
1856 {
1857 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1858 while (node)
1859 {
1860 wxWindow *win = node->GetData();
1861 if ( !win->IsTopLevel() && win->GetConstraints() )
1862 win->SetConstraintSizes();
1863 node = node->GetNext();
1864 }
1865 }
1866 }
1867
1868 // Only set the size/position of the constraint (if any)
1869 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1870 {
1871 wxLayoutConstraints *constr = GetConstraints();
1872 if ( constr )
1873 {
1874 if ( x != wxDefaultCoord )
1875 {
1876 constr->left.SetValue(x);
1877 constr->left.SetDone(true);
1878 }
1879 if ( y != wxDefaultCoord )
1880 {
1881 constr->top.SetValue(y);
1882 constr->top.SetDone(true);
1883 }
1884 if ( w != wxDefaultCoord )
1885 {
1886 constr->width.SetValue(w);
1887 constr->width.SetDone(true);
1888 }
1889 if ( h != wxDefaultCoord )
1890 {
1891 constr->height.SetValue(h);
1892 constr->height.SetDone(true);
1893 }
1894 }
1895 }
1896
1897 void wxWindowBase::MoveConstraint(int x, int y)
1898 {
1899 wxLayoutConstraints *constr = GetConstraints();
1900 if ( constr )
1901 {
1902 if ( x != wxDefaultCoord )
1903 {
1904 constr->left.SetValue(x);
1905 constr->left.SetDone(true);
1906 }
1907 if ( y != wxDefaultCoord )
1908 {
1909 constr->top.SetValue(y);
1910 constr->top.SetDone(true);
1911 }
1912 }
1913 }
1914
1915 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1916 {
1917 wxLayoutConstraints *constr = GetConstraints();
1918 if ( constr )
1919 {
1920 *w = constr->width.GetValue();
1921 *h = constr->height.GetValue();
1922 }
1923 else
1924 GetSize(w, h);
1925 }
1926
1927 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1928 {
1929 wxLayoutConstraints *constr = GetConstraints();
1930 if ( constr )
1931 {
1932 *w = constr->width.GetValue();
1933 *h = constr->height.GetValue();
1934 }
1935 else
1936 GetClientSize(w, h);
1937 }
1938
1939 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1940 {
1941 wxLayoutConstraints *constr = GetConstraints();
1942 if ( constr )
1943 {
1944 *x = constr->left.GetValue();
1945 *y = constr->top.GetValue();
1946 }
1947 else
1948 GetPosition(x, y);
1949 }
1950
1951 #endif // wxUSE_CONSTRAINTS
1952
1953 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
1954 {
1955 // don't do it for the dialogs/frames - they float independently of their
1956 // parent
1957 if ( !IsTopLevel() )
1958 {
1959 wxWindow *parent = GetParent();
1960 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
1961 {
1962 wxPoint pt(parent->GetClientAreaOrigin());
1963 x += pt.x;
1964 y += pt.y;
1965 }
1966 }
1967 }
1968
1969 // ----------------------------------------------------------------------------
1970 // do Update UI processing for child controls
1971 // ----------------------------------------------------------------------------
1972
1973 void wxWindowBase::UpdateWindowUI(long flags)
1974 {
1975 wxUpdateUIEvent event(GetId());
1976 event.SetEventObject(this);
1977
1978 if ( GetEventHandler()->ProcessEvent(event) )
1979 {
1980 DoUpdateWindowUI(event);
1981 }
1982
1983 if (flags & wxUPDATE_UI_RECURSE)
1984 {
1985 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1986 while (node)
1987 {
1988 wxWindow* child = (wxWindow*) node->GetData();
1989 child->UpdateWindowUI(flags);
1990 node = node->GetNext();
1991 }
1992 }
1993 }
1994
1995 // do the window-specific processing after processing the update event
1996 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
1997 {
1998 if ( event.GetSetEnabled() )
1999 Enable(event.GetEnabled());
2000
2001 if ( event.GetSetShown() )
2002 Show(event.GetShown());
2003 }
2004
2005 #if 0
2006 // call internal idle recursively
2007 // may be obsolete (wait until OnIdle scheme stabilises)
2008 void wxWindowBase::ProcessInternalIdle()
2009 {
2010 OnInternalIdle();
2011
2012 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2013 while (node)
2014 {
2015 wxWindow *child = node->GetData();
2016 child->ProcessInternalIdle();
2017 node = node->GetNext();
2018 }
2019 }
2020 #endif
2021
2022 // ----------------------------------------------------------------------------
2023 // dialog units translations
2024 // ----------------------------------------------------------------------------
2025
2026 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2027 {
2028 int charWidth = GetCharWidth();
2029 int charHeight = GetCharHeight();
2030 wxPoint pt2 = wxDefaultPosition;
2031 if (pt.x != wxDefaultCoord)
2032 pt2.x = (int) ((pt.x * 4) / charWidth);
2033 if (pt.y != wxDefaultCoord)
2034 pt2.y = (int) ((pt.y * 8) / charHeight);
2035
2036 return pt2;
2037 }
2038
2039 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2040 {
2041 int charWidth = GetCharWidth();
2042 int charHeight = GetCharHeight();
2043 wxPoint pt2 = wxDefaultPosition;
2044 if (pt.x != wxDefaultCoord)
2045 pt2.x = (int) ((pt.x * charWidth) / 4);
2046 if (pt.y != wxDefaultCoord)
2047 pt2.y = (int) ((pt.y * charHeight) / 8);
2048
2049 return pt2;
2050 }
2051
2052 // ----------------------------------------------------------------------------
2053 // event handlers
2054 // ----------------------------------------------------------------------------
2055
2056 // propagate the colour change event to the subwindows
2057 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2058 {
2059 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2060 while ( node )
2061 {
2062 // Only propagate to non-top-level windows
2063 wxWindow *win = node->GetData();
2064 if ( !win->IsTopLevel() )
2065 {
2066 wxSysColourChangedEvent event2;
2067 event.SetEventObject(win);
2068 win->GetEventHandler()->ProcessEvent(event2);
2069 }
2070
2071 node = node->GetNext();
2072 }
2073
2074 Refresh();
2075 }
2076
2077 // the default action is to populate dialog with data when it's created,
2078 // and nudge the UI into displaying itself correctly in case
2079 // we've turned the wxUpdateUIEvents frequency down low.
2080 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2081 {
2082 TransferDataToWindow();
2083
2084 // Update the UI at this point
2085 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2086 }
2087
2088 // methods for drawing the sizers in a visible way
2089 #ifdef __WXDEBUG__
2090
2091 static void DrawSizers(wxWindowBase *win);
2092
2093 static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill = false)
2094 {
2095 wxClientDC dc((wxWindow *)win);
2096 dc.SetPen(*wxRED_PEN);
2097 dc.SetBrush(fill ? wxBrush(*wxRED, wxCROSSDIAG_HATCH): *wxTRANSPARENT_BRUSH);
2098 dc.DrawRectangle(rect.Deflate(1, 1));
2099 }
2100
2101 static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
2102 {
2103 const wxSizerItemList& items = sizer->GetChildren();
2104 for ( wxSizerItemList::const_iterator i = items.begin(),
2105 end = items.end();
2106 i != end;
2107 ++i )
2108 {
2109 wxSizerItem *item = *i;
2110 if ( item->IsSizer() )
2111 {
2112 DrawBorder(win, item->GetRect().Deflate(2));
2113 DrawSizer(win, item->GetSizer());
2114 }
2115 else if ( item->IsSpacer() )
2116 {
2117 DrawBorder(win, item->GetRect().Deflate(2), true);
2118 }
2119 else if ( item->IsWindow() )
2120 {
2121 DrawSizers(item->GetWindow());
2122 }
2123 }
2124 }
2125
2126 static void DrawSizers(wxWindowBase *win)
2127 {
2128 wxSizer *sizer = win->GetSizer();
2129 if ( sizer )
2130 {
2131 DrawBorder(win, win->GetClientSize());
2132 DrawSizer(win, sizer);
2133 }
2134 else // no sizer, still recurse into the children
2135 {
2136 const wxWindowList& children = win->GetChildren();
2137 for ( wxWindowList::const_iterator i = children.begin(),
2138 end = children.end();
2139 i != end;
2140 ++i )
2141 {
2142 DrawSizers(*i);
2143 }
2144 }
2145 }
2146
2147 #endif // __WXDEBUG__
2148
2149 // process special middle clicks
2150 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2151 {
2152 if ( event.ControlDown() && event.AltDown() )
2153 {
2154 #ifdef __WXDEBUG__
2155 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2156 if ( event.ShiftDown() )
2157 {
2158 DrawSizers(this);
2159 return;
2160 }
2161 #endif // __WXDEBUG__
2162
2163 #if wxUSE_MSGDLG
2164 // don't translate these strings
2165 wxString port;
2166
2167 #ifdef __WXUNIVERSAL__
2168 port = _T("Univ/");
2169 #endif // __WXUNIVERSAL__
2170
2171 switch ( wxGetOsVersion() )
2172 {
2173 case wxMOTIF_X: port += _T("Motif"); break;
2174 case wxMAC:
2175 case wxMAC_DARWIN: port += _T("Mac"); break;
2176 case wxBEOS: port += _T("BeOS"); break;
2177 case wxGTK:
2178 case wxGTK_WIN32:
2179 case wxGTK_OS2:
2180 case wxGTK_BEOS: port += _T("GTK"); break;
2181 case wxWINDOWS:
2182 case wxPENWINDOWS:
2183 case wxWINDOWS_NT:
2184 case wxWIN32S:
2185 case wxWIN95:
2186 case wxWIN386: port += _T("MS Windows"); break;
2187 case wxMGL_UNIX:
2188 case wxMGL_X:
2189 case wxMGL_WIN32:
2190 case wxMGL_OS2: port += _T("MGL"); break;
2191 case wxWINDOWS_OS2:
2192 case wxOS2_PM: port += _T("OS/2"); break;
2193 case wxPALMOS: port += _T("Palm OS"); break;
2194 case wxWINDOWS_CE: port += _T("Windows CE (generic)"); break;
2195 case wxWINDOWS_POCKETPC: port += _T("Windows CE PocketPC"); break;
2196 case wxWINDOWS_SMARTPHONE: port += _T("Windows CE Smartphone"); break;
2197 default: port += _T("unknown"); break;
2198 }
2199
2200 wxMessageBox(wxString::Format(
2201 _T(
2202 " wxWidgets Library (%s port)\nVersion %d.%d.%d%s%s, compiled at %s %s%s\n Copyright (c) 1995-2006 wxWidgets team"
2203 ),
2204 port.c_str(),
2205 wxMAJOR_VERSION,
2206 wxMINOR_VERSION,
2207 wxRELEASE_NUMBER,
2208 #if wxUSE_UNICODE
2209 L" (Unicode)",
2210 #else
2211 "",
2212 #endif
2213 #ifdef __WXDEBUG__
2214 _T(" Debug build"),
2215 #else
2216 wxEmptyString,
2217 #endif
2218 __TDATE__,
2219 __TTIME__,
2220 #ifdef __WXGTK__
2221 wxString::Format(_T("\nagainst GTK+ %d.%d.%d. Runtime GTK+ version: %d.%d.%d"), GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION, gtk_major_version, gtk_minor_version, gtk_micro_version).c_str()
2222 #else
2223 ""
2224 #endif
2225 ),
2226 _T("wxWidgets information"),
2227 wxICON_INFORMATION | wxOK,
2228 (wxWindow *)this);
2229 }
2230 else
2231 #endif // wxUSE_MSGDLG
2232 {
2233 event.Skip();
2234 }
2235 }
2236
2237 // ----------------------------------------------------------------------------
2238 // accessibility
2239 // ----------------------------------------------------------------------------
2240
2241 #if wxUSE_ACCESSIBILITY
2242 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2243 {
2244 if (m_accessible && (accessible != m_accessible))
2245 delete m_accessible;
2246 m_accessible = accessible;
2247 if (m_accessible)
2248 m_accessible->SetWindow((wxWindow*) this);
2249 }
2250
2251 // Returns the accessible object, creating if necessary.
2252 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2253 {
2254 if (!m_accessible)
2255 m_accessible = CreateAccessible();
2256 return m_accessible;
2257 }
2258
2259 // Override to create a specific accessible object.
2260 wxAccessible* wxWindowBase::CreateAccessible()
2261 {
2262 return new wxWindowAccessible((wxWindow*) this);
2263 }
2264
2265 #endif
2266
2267 // ----------------------------------------------------------------------------
2268 // list classes implementation
2269 // ----------------------------------------------------------------------------
2270
2271 #if wxUSE_STL
2272
2273 #include "wx/listimpl.cpp"
2274 WX_DEFINE_LIST(wxWindowList)
2275
2276 #else
2277
2278 void wxWindowListNode::DeleteData()
2279 {
2280 delete (wxWindow *)GetData();
2281 }
2282
2283 #endif
2284
2285 // ----------------------------------------------------------------------------
2286 // borders
2287 // ----------------------------------------------------------------------------
2288
2289 wxBorder wxWindowBase::GetBorder(long flags) const
2290 {
2291 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2292 if ( border == wxBORDER_DEFAULT )
2293 {
2294 border = GetDefaultBorder();
2295 }
2296
2297 return border;
2298 }
2299
2300 wxBorder wxWindowBase::GetDefaultBorder() const
2301 {
2302 return wxBORDER_NONE;
2303 }
2304
2305 // ----------------------------------------------------------------------------
2306 // hit testing
2307 // ----------------------------------------------------------------------------
2308
2309 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2310 {
2311 // here we just check if the point is inside the window or not
2312
2313 // check the top and left border first
2314 bool outside = x < 0 || y < 0;
2315 if ( !outside )
2316 {
2317 // check the right and bottom borders too
2318 wxSize size = GetSize();
2319 outside = x >= size.x || y >= size.y;
2320 }
2321
2322 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2323 }
2324
2325 // ----------------------------------------------------------------------------
2326 // mouse capture
2327 // ----------------------------------------------------------------------------
2328
2329 struct WXDLLEXPORT wxWindowNext
2330 {
2331 wxWindow *win;
2332 wxWindowNext *next;
2333 } *wxWindowBase::ms_winCaptureNext = NULL;
2334
2335 void wxWindowBase::CaptureMouse()
2336 {
2337 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), this);
2338
2339 wxWindow *winOld = GetCapture();
2340 if ( winOld )
2341 {
2342 ((wxWindowBase*) winOld)->DoReleaseMouse();
2343
2344 // save it on stack
2345 wxWindowNext *item = new wxWindowNext;
2346 item->win = winOld;
2347 item->next = ms_winCaptureNext;
2348 ms_winCaptureNext = item;
2349 }
2350 //else: no mouse capture to save
2351
2352 DoCaptureMouse();
2353 }
2354
2355 void wxWindowBase::ReleaseMouse()
2356 {
2357 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), this);
2358
2359 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2360
2361 DoReleaseMouse();
2362
2363 if ( ms_winCaptureNext )
2364 {
2365 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2366
2367 wxWindowNext *item = ms_winCaptureNext;
2368 ms_winCaptureNext = item->next;
2369 delete item;
2370 }
2371 //else: stack is empty, no previous capture
2372
2373 wxLogTrace(_T("mousecapture"),
2374 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
2375 GetCapture());
2376 }
2377
2378 #if wxUSE_HOTKEY
2379
2380 bool
2381 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2382 int WXUNUSED(modifiers),
2383 int WXUNUSED(keycode))
2384 {
2385 // not implemented
2386 return false;
2387 }
2388
2389 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2390 {
2391 // not implemented
2392 return false;
2393 }
2394
2395 #endif // wxUSE_HOTKEY
2396
2397 void wxWindowBase::SendDestroyEvent()
2398 {
2399 wxWindowDestroyEvent event;
2400 event.SetEventObject(this);
2401 event.SetId(GetId());
2402 GetEventHandler()->ProcessEvent(event);
2403 }
2404
2405 // ----------------------------------------------------------------------------
2406 // event processing
2407 // ----------------------------------------------------------------------------
2408
2409 bool wxWindowBase::TryValidator(wxEvent& wxVALIDATOR_PARAM(event))
2410 {
2411 #if wxUSE_VALIDATORS
2412 // Can only use the validator of the window which
2413 // is receiving the event
2414 if ( event.GetEventObject() == this )
2415 {
2416 wxValidator *validator = GetValidator();
2417 if ( validator && validator->ProcessEvent(event) )
2418 {
2419 return true;
2420 }
2421 }
2422 #endif // wxUSE_VALIDATORS
2423
2424 return false;
2425 }
2426
2427 bool wxWindowBase::TryParent(wxEvent& event)
2428 {
2429 // carry on up the parent-child hierarchy if the propagation count hasn't
2430 // reached zero yet
2431 if ( event.ShouldPropagate() )
2432 {
2433 // honour the requests to stop propagation at this window: this is
2434 // used by the dialogs, for example, to prevent processing the events
2435 // from the dialog controls in the parent frame which rarely, if ever,
2436 // makes sense
2437 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2438 {
2439 wxWindow *parent = GetParent();
2440 if ( parent && !parent->IsBeingDeleted() )
2441 {
2442 wxPropagateOnce propagateOnce(event);
2443
2444 return parent->GetEventHandler()->ProcessEvent(event);
2445 }
2446 }
2447 }
2448
2449 return wxEvtHandler::TryParent(event);
2450 }
2451
2452 // ----------------------------------------------------------------------------
2453 // keyboard navigation
2454 // ----------------------------------------------------------------------------
2455
2456 // Navigates in the specified direction.
2457 bool wxWindowBase::Navigate(int flags)
2458 {
2459 wxNavigationKeyEvent eventNav;
2460 eventNav.SetFlags(flags);
2461 eventNav.SetEventObject(this);
2462 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
2463 {
2464 return true;
2465 }
2466 return false;
2467 }
2468
2469 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, MoveKind move)
2470 {
2471 // check that we're not a top level window
2472 wxCHECK_RET( GetParent(),
2473 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2474
2475 // detect the special case when we have nothing to do anyhow and when the
2476 // code below wouldn't work
2477 if ( win == this )
2478 return;
2479
2480 // find the target window in the siblings list
2481 wxWindowList& siblings = GetParent()->GetChildren();
2482 wxWindowList::compatibility_iterator i = siblings.Find(win);
2483 wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2484
2485 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2486 // can't just move the node around
2487 wxWindow *self = (wxWindow *)this;
2488 siblings.DeleteObject(self);
2489 if ( move == MoveAfter )
2490 {
2491 i = i->GetNext();
2492 }
2493
2494 if ( i )
2495 {
2496 siblings.Insert(i, self);
2497 }
2498 else // MoveAfter and win was the last sibling
2499 {
2500 siblings.Append(self);
2501 }
2502 }
2503
2504 // ----------------------------------------------------------------------------
2505 // focus handling
2506 // ----------------------------------------------------------------------------
2507
2508 /*static*/ wxWindow* wxWindowBase::FindFocus()
2509 {
2510 wxWindowBase *win = DoFindFocus();
2511 return win ? win->GetMainWindowOfCompositeControl() : NULL;
2512 }
2513
2514 // ----------------------------------------------------------------------------
2515 // global functions
2516 // ----------------------------------------------------------------------------
2517
2518 wxWindow* wxGetTopLevelParent(wxWindow *win)
2519 {
2520 while ( win && !win->IsTopLevel() )
2521 win = win->GetParent();
2522
2523 return win;
2524 }
2525
2526 #if wxUSE_ACCESSIBILITY
2527 // ----------------------------------------------------------------------------
2528 // accessible object for windows
2529 // ----------------------------------------------------------------------------
2530
2531 // Can return either a child object, or an integer
2532 // representing the child element, starting from 1.
2533 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
2534 {
2535 wxASSERT( GetWindow() != NULL );
2536 if (!GetWindow())
2537 return wxACC_FAIL;
2538
2539 return wxACC_NOT_IMPLEMENTED;
2540 }
2541
2542 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2543 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
2544 {
2545 wxASSERT( GetWindow() != NULL );
2546 if (!GetWindow())
2547 return wxACC_FAIL;
2548
2549 wxWindow* win = NULL;
2550 if (elementId == 0)
2551 {
2552 win = GetWindow();
2553 }
2554 else
2555 {
2556 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
2557 {
2558 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
2559 }
2560 else
2561 return wxACC_FAIL;
2562 }
2563 if (win)
2564 {
2565 rect = win->GetRect();
2566 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
2567 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
2568 return wxACC_OK;
2569 }
2570
2571 return wxACC_NOT_IMPLEMENTED;
2572 }
2573
2574 // Navigates from fromId to toId/toObject.
2575 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
2576 int* WXUNUSED(toId), wxAccessible** toObject)
2577 {
2578 wxASSERT( GetWindow() != NULL );
2579 if (!GetWindow())
2580 return wxACC_FAIL;
2581
2582 switch (navDir)
2583 {
2584 case wxNAVDIR_FIRSTCHILD:
2585 {
2586 if (GetWindow()->GetChildren().GetCount() == 0)
2587 return wxACC_FALSE;
2588 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
2589 *toObject = childWindow->GetOrCreateAccessible();
2590
2591 return wxACC_OK;
2592 }
2593 case wxNAVDIR_LASTCHILD:
2594 {
2595 if (GetWindow()->GetChildren().GetCount() == 0)
2596 return wxACC_FALSE;
2597 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
2598 *toObject = childWindow->GetOrCreateAccessible();
2599
2600 return wxACC_OK;
2601 }
2602 case wxNAVDIR_RIGHT:
2603 case wxNAVDIR_DOWN:
2604 case wxNAVDIR_NEXT:
2605 {
2606 wxWindowList::compatibility_iterator node =
2607 wxWindowList::compatibility_iterator();
2608 if (fromId == 0)
2609 {
2610 // Can't navigate to sibling of this window
2611 // if we're a top-level window.
2612 if (!GetWindow()->GetParent())
2613 return wxACC_NOT_IMPLEMENTED;
2614
2615 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2616 }
2617 else
2618 {
2619 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2620 node = GetWindow()->GetChildren().Item(fromId-1);
2621 }
2622
2623 if (node && node->GetNext())
2624 {
2625 wxWindow* nextWindow = node->GetNext()->GetData();
2626 *toObject = nextWindow->GetOrCreateAccessible();
2627 return wxACC_OK;
2628 }
2629 else
2630 return wxACC_FALSE;
2631 }
2632 case wxNAVDIR_LEFT:
2633 case wxNAVDIR_UP:
2634 case wxNAVDIR_PREVIOUS:
2635 {
2636 wxWindowList::compatibility_iterator node =
2637 wxWindowList::compatibility_iterator();
2638 if (fromId == 0)
2639 {
2640 // Can't navigate to sibling of this window
2641 // if we're a top-level window.
2642 if (!GetWindow()->GetParent())
2643 return wxACC_NOT_IMPLEMENTED;
2644
2645 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2646 }
2647 else
2648 {
2649 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2650 node = GetWindow()->GetChildren().Item(fromId-1);
2651 }
2652
2653 if (node && node->GetPrevious())
2654 {
2655 wxWindow* previousWindow = node->GetPrevious()->GetData();
2656 *toObject = previousWindow->GetOrCreateAccessible();
2657 return wxACC_OK;
2658 }
2659 else
2660 return wxACC_FALSE;
2661 }
2662 }
2663
2664 return wxACC_NOT_IMPLEMENTED;
2665 }
2666
2667 // Gets the name of the specified object.
2668 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
2669 {
2670 wxASSERT( GetWindow() != NULL );
2671 if (!GetWindow())
2672 return wxACC_FAIL;
2673
2674 wxString title;
2675
2676 // If a child, leave wxWidgets to call the function on the actual
2677 // child object.
2678 if (childId > 0)
2679 return wxACC_NOT_IMPLEMENTED;
2680
2681 // This will eventually be replaced by specialised
2682 // accessible classes, one for each kind of wxWidgets
2683 // control or window.
2684 #if wxUSE_BUTTON
2685 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
2686 title = ((wxButton*) GetWindow())->GetLabel();
2687 else
2688 #endif
2689 title = GetWindow()->GetName();
2690
2691 if (!title.empty())
2692 {
2693 *name = title;
2694 return wxACC_OK;
2695 }
2696 else
2697 return wxACC_NOT_IMPLEMENTED;
2698 }
2699
2700 // Gets the number of children.
2701 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
2702 {
2703 wxASSERT( GetWindow() != NULL );
2704 if (!GetWindow())
2705 return wxACC_FAIL;
2706
2707 *childId = (int) GetWindow()->GetChildren().GetCount();
2708 return wxACC_OK;
2709 }
2710
2711 // Gets the specified child (starting from 1).
2712 // If *child is NULL and return value is wxACC_OK,
2713 // this means that the child is a simple element and
2714 // not an accessible object.
2715 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
2716 {
2717 wxASSERT( GetWindow() != NULL );
2718 if (!GetWindow())
2719 return wxACC_FAIL;
2720
2721 if (childId == 0)
2722 {
2723 *child = this;
2724 return wxACC_OK;
2725 }
2726
2727 if (childId > (int) GetWindow()->GetChildren().GetCount())
2728 return wxACC_FAIL;
2729
2730 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
2731 *child = childWindow->GetOrCreateAccessible();
2732 if (*child)
2733 return wxACC_OK;
2734 else
2735 return wxACC_FAIL;
2736 }
2737
2738 // Gets the parent, or NULL.
2739 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
2740 {
2741 wxASSERT( GetWindow() != NULL );
2742 if (!GetWindow())
2743 return wxACC_FAIL;
2744
2745 wxWindow* parentWindow = GetWindow()->GetParent();
2746 if (!parentWindow)
2747 {
2748 *parent = NULL;
2749 return wxACC_OK;
2750 }
2751 else
2752 {
2753 *parent = parentWindow->GetOrCreateAccessible();
2754 if (*parent)
2755 return wxACC_OK;
2756 else
2757 return wxACC_FAIL;
2758 }
2759 }
2760
2761 // Performs the default action. childId is 0 (the action for this object)
2762 // or > 0 (the action for a child).
2763 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2764 // window (e.g. an edit control).
2765 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
2766 {
2767 wxASSERT( GetWindow() != NULL );
2768 if (!GetWindow())
2769 return wxACC_FAIL;
2770
2771 return wxACC_NOT_IMPLEMENTED;
2772 }
2773
2774 // Gets the default action for this object (0) or > 0 (the action for a child).
2775 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2776 // string if there is no action.
2777 // The retrieved string describes the action that is performed on an object,
2778 // not what the object does as a result. For example, a toolbar button that prints
2779 // a document has a default action of "Press" rather than "Prints the current document."
2780 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
2781 {
2782 wxASSERT( GetWindow() != NULL );
2783 if (!GetWindow())
2784 return wxACC_FAIL;
2785
2786 return wxACC_NOT_IMPLEMENTED;
2787 }
2788
2789 // Returns the description for this object or a child.
2790 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
2791 {
2792 wxASSERT( GetWindow() != NULL );
2793 if (!GetWindow())
2794 return wxACC_FAIL;
2795
2796 wxString ht(GetWindow()->GetHelpText());
2797 if (!ht.empty())
2798 {
2799 *description = ht;
2800 return wxACC_OK;
2801 }
2802 return wxACC_NOT_IMPLEMENTED;
2803 }
2804
2805 // Returns help text for this object or a child, similar to tooltip text.
2806 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
2807 {
2808 wxASSERT( GetWindow() != NULL );
2809 if (!GetWindow())
2810 return wxACC_FAIL;
2811
2812 wxString ht(GetWindow()->GetHelpText());
2813 if (!ht.empty())
2814 {
2815 *helpText = ht;
2816 return wxACC_OK;
2817 }
2818 return wxACC_NOT_IMPLEMENTED;
2819 }
2820
2821 // Returns the keyboard shortcut for this object or child.
2822 // Return e.g. ALT+K
2823 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
2824 {
2825 wxASSERT( GetWindow() != NULL );
2826 if (!GetWindow())
2827 return wxACC_FAIL;
2828
2829 return wxACC_NOT_IMPLEMENTED;
2830 }
2831
2832 // Returns a role constant.
2833 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
2834 {
2835 wxASSERT( GetWindow() != NULL );
2836 if (!GetWindow())
2837 return wxACC_FAIL;
2838
2839 // If a child, leave wxWidgets to call the function on the actual
2840 // child object.
2841 if (childId > 0)
2842 return wxACC_NOT_IMPLEMENTED;
2843
2844 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2845 return wxACC_NOT_IMPLEMENTED;
2846 #if wxUSE_STATUSBAR
2847 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2848 return wxACC_NOT_IMPLEMENTED;
2849 #endif
2850 #if wxUSE_TOOLBAR
2851 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2852 return wxACC_NOT_IMPLEMENTED;
2853 #endif
2854
2855 //*role = wxROLE_SYSTEM_CLIENT;
2856 *role = wxROLE_SYSTEM_CLIENT;
2857 return wxACC_OK;
2858
2859 #if 0
2860 return wxACC_NOT_IMPLEMENTED;
2861 #endif
2862 }
2863
2864 // Returns a state constant.
2865 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
2866 {
2867 wxASSERT( GetWindow() != NULL );
2868 if (!GetWindow())
2869 return wxACC_FAIL;
2870
2871 // If a child, leave wxWidgets to call the function on the actual
2872 // child object.
2873 if (childId > 0)
2874 return wxACC_NOT_IMPLEMENTED;
2875
2876 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2877 return wxACC_NOT_IMPLEMENTED;
2878
2879 #if wxUSE_STATUSBAR
2880 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2881 return wxACC_NOT_IMPLEMENTED;
2882 #endif
2883 #if wxUSE_TOOLBAR
2884 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2885 return wxACC_NOT_IMPLEMENTED;
2886 #endif
2887
2888 *state = 0;
2889 return wxACC_OK;
2890
2891 #if 0
2892 return wxACC_NOT_IMPLEMENTED;
2893 #endif
2894 }
2895
2896 // Returns a localized string representing the value for the object
2897 // or child.
2898 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
2899 {
2900 wxASSERT( GetWindow() != NULL );
2901 if (!GetWindow())
2902 return wxACC_FAIL;
2903
2904 return wxACC_NOT_IMPLEMENTED;
2905 }
2906
2907 // Selects the object or child.
2908 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
2909 {
2910 wxASSERT( GetWindow() != NULL );
2911 if (!GetWindow())
2912 return wxACC_FAIL;
2913
2914 return wxACC_NOT_IMPLEMENTED;
2915 }
2916
2917 // Gets the window with the keyboard focus.
2918 // If childId is 0 and child is NULL, no object in
2919 // this subhierarchy has the focus.
2920 // If this object has the focus, child should be 'this'.
2921 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
2922 {
2923 wxASSERT( GetWindow() != NULL );
2924 if (!GetWindow())
2925 return wxACC_FAIL;
2926
2927 return wxACC_NOT_IMPLEMENTED;
2928 }
2929
2930 // Gets a variant representing the selected children
2931 // of this object.
2932 // Acceptable values:
2933 // - a null variant (IsNull() returns true)
2934 // - a list variant (GetType() == wxT("list")
2935 // - an integer representing the selected child element,
2936 // or 0 if this object is selected (GetType() == wxT("long")
2937 // - a "void*" pointer to a wxAccessible child object
2938 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
2939 {
2940 wxASSERT( GetWindow() != NULL );
2941 if (!GetWindow())
2942 return wxACC_FAIL;
2943
2944 return wxACC_NOT_IMPLEMENTED;
2945 }
2946
2947 #endif // wxUSE_ACCESSIBILITY