]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
Implement and document wxDataViewTreeCtrl::IsContainer(), use it in the sample to...
[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/window.h"
33 #include "wx/control.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobut.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
38 #include "wx/settings.h"
39 #include "wx/dialog.h"
40 #include "wx/msgdlg.h"
41 #include "wx/msgout.h"
42 #include "wx/statusbr.h"
43 #include "wx/toolbar.h"
44 #include "wx/dcclient.h"
45 #include "wx/scrolbar.h"
46 #include "wx/layout.h"
47 #include "wx/sizer.h"
48 #include "wx/menu.h"
49 #endif //WX_PRECOMP
50
51 #if wxUSE_DRAG_AND_DROP
52 #include "wx/dnd.h"
53 #endif // wxUSE_DRAG_AND_DROP
54
55 #if wxUSE_ACCESSIBILITY
56 #include "wx/access.h"
57 #endif
58
59 #if wxUSE_HELP
60 #include "wx/cshelp.h"
61 #endif // wxUSE_HELP
62
63 #if wxUSE_TOOLTIPS
64 #include "wx/tooltip.h"
65 #endif // wxUSE_TOOLTIPS
66
67 #if wxUSE_CARET
68 #include "wx/caret.h"
69 #endif // wxUSE_CARET
70
71 #if wxUSE_SYSTEM_OPTIONS
72 #include "wx/sysopt.h"
73 #endif
74
75 #include "wx/platinfo.h"
76
77 // Windows List
78 WXDLLIMPEXP_DATA_CORE(wxWindowList) wxTopLevelWindows;
79
80 // globals
81 #if wxUSE_MENUS
82 wxMenu *wxCurrentPopupMenu = NULL;
83 #endif // wxUSE_MENUS
84
85 // ----------------------------------------------------------------------------
86 // static data
87 // ----------------------------------------------------------------------------
88
89
90 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
91
92 // ----------------------------------------------------------------------------
93 // event table
94 // ----------------------------------------------------------------------------
95
96 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
97 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
98 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
99 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
100
101 #if wxUSE_HELP
102 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
103 #endif // wxUSE_HELP
104
105 EVT_SIZE(wxWindowBase::InternalOnSize)
106 END_EVENT_TABLE()
107
108 // ============================================================================
109 // implementation of the common functionality of the wxWindow class
110 // ============================================================================
111
112 // ----------------------------------------------------------------------------
113 // initialization
114 // ----------------------------------------------------------------------------
115
116 // the default initialization
117 wxWindowBase::wxWindowBase()
118 {
119 // no window yet, no parent nor children
120 m_parent = NULL;
121 m_windowId = wxID_ANY;
122
123 // no constraints on the minimal window size
124 m_minWidth =
125 m_maxWidth = wxDefaultCoord;
126 m_minHeight =
127 m_maxHeight = wxDefaultCoord;
128
129 // invalidiated cache value
130 m_bestSizeCache = wxDefaultSize;
131
132 // window are created enabled and visible by default
133 m_isShown =
134 m_isEnabled = true;
135
136 // the default event handler is just this window
137 m_eventHandler = this;
138
139 #if wxUSE_VALIDATORS
140 // no validator
141 m_windowValidator = NULL;
142 #endif // wxUSE_VALIDATORS
143
144 // the colours/fonts are default for now, so leave m_font,
145 // m_backgroundColour and m_foregroundColour uninitialized and set those
146 m_hasBgCol =
147 m_hasFgCol =
148 m_hasFont = false;
149 m_inheritBgCol =
150 m_inheritFgCol =
151 m_inheritFont = false;
152
153 // no style bits
154 m_exStyle =
155 m_windowStyle = 0;
156
157 m_backgroundStyle = wxBG_STYLE_ERASE;
158
159 #if wxUSE_CONSTRAINTS
160 // no constraints whatsoever
161 m_constraints = NULL;
162 m_constraintsInvolvedIn = NULL;
163 #endif // wxUSE_CONSTRAINTS
164
165 m_windowSizer = NULL;
166 m_containingSizer = NULL;
167 m_autoLayout = false;
168
169 #if wxUSE_DRAG_AND_DROP
170 m_dropTarget = NULL;
171 #endif // wxUSE_DRAG_AND_DROP
172
173 #if wxUSE_TOOLTIPS
174 m_tooltip = NULL;
175 #endif // wxUSE_TOOLTIPS
176
177 #if wxUSE_CARET
178 m_caret = NULL;
179 #endif // wxUSE_CARET
180
181 #if wxUSE_PALETTE
182 m_hasCustomPalette = false;
183 #endif // wxUSE_PALETTE
184
185 #if wxUSE_ACCESSIBILITY
186 m_accessible = NULL;
187 #endif
188
189 m_virtualSize = wxDefaultSize;
190
191 m_scrollHelper = NULL;
192
193 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
194 #if wxUSE_SYSTEM_OPTIONS
195 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT) )
196 {
197 m_windowVariant = (wxWindowVariant) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT ) ;
198 }
199 #endif
200
201 // Whether we're using the current theme for this window (wxGTK only for now)
202 m_themeEnabled = false;
203
204 // This is set to true by SendDestroyEvent() which should be called by the
205 // most derived class to ensure that the destruction event is sent as soon
206 // as possible to allow its handlers to still see the undestroyed window
207 m_isBeingDeleted = false;
208
209 m_freezeCount = 0;
210 }
211
212 // common part of window creation process
213 bool wxWindowBase::CreateBase(wxWindowBase *parent,
214 wxWindowID id,
215 const wxPoint& WXUNUSED(pos),
216 const wxSize& WXUNUSED(size),
217 long style,
218 const wxString& name)
219 {
220 // ids are limited to 16 bits under MSW so if you care about portability,
221 // it's not a good idea to use ids out of this range (and negative ids are
222 // reserved for wxWidgets own usage)
223 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767) ||
224 (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST),
225 wxT("invalid id value") );
226
227 // generate a new id if the user doesn't care about it
228 if ( id == wxID_ANY )
229 {
230 m_windowId = NewControlId();
231 }
232 else // valid id specified
233 {
234 m_windowId = id;
235 }
236
237 // don't use SetWindowStyleFlag() here, this function should only be called
238 // to change the flag after creation as it tries to reflect the changes in
239 // flags by updating the window dynamically and we don't need this here
240 m_windowStyle = style;
241
242 SetName(name);
243 SetParent(parent);
244
245 return true;
246 }
247
248 bool wxWindowBase::CreateBase(wxWindowBase *parent,
249 wxWindowID id,
250 const wxPoint& pos,
251 const wxSize& size,
252 long style,
253 const wxValidator& wxVALIDATOR_PARAM(validator),
254 const wxString& name)
255 {
256 if ( !CreateBase(parent, id, pos, size, style, name) )
257 return false;
258
259 #if wxUSE_VALIDATORS
260 SetValidator(validator);
261 #endif // wxUSE_VALIDATORS
262
263 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
264 // have it too - like this it's possible to set it only in the top level
265 // dialog/frame and all children will inherit it by defult
266 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
267 {
268 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
269 }
270
271 return true;
272 }
273
274 bool wxWindowBase::ToggleWindowStyle(int flag)
275 {
276 wxASSERT_MSG( flag, wxT("flags with 0 value can't be toggled") );
277
278 bool rc;
279 long style = GetWindowStyleFlag();
280 if ( style & flag )
281 {
282 style &= ~flag;
283 rc = false;
284 }
285 else // currently off
286 {
287 style |= flag;
288 rc = true;
289 }
290
291 SetWindowStyleFlag(style);
292
293 return rc;
294 }
295
296 // ----------------------------------------------------------------------------
297 // destruction
298 // ----------------------------------------------------------------------------
299
300 // common clean up
301 wxWindowBase::~wxWindowBase()
302 {
303 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
304
305 // FIXME if these 2 cases result from programming errors in the user code
306 // we should probably assert here instead of silently fixing them
307
308 // Just in case the window has been Closed, but we're then deleting
309 // immediately: don't leave dangling pointers.
310 wxPendingDelete.DeleteObject(this);
311
312 // Just in case we've loaded a top-level window via LoadNativeDialog but
313 // we weren't a dialog class
314 wxTopLevelWindows.DeleteObject((wxWindow*)this);
315
316 #if wxUSE_MENUS
317 // The associated popup menu can still be alive, disassociate from it in
318 // this case
319 if ( wxCurrentPopupMenu && wxCurrentPopupMenu->GetInvokingWindow() == this )
320 wxCurrentPopupMenu->SetInvokingWindow(NULL);
321 #endif // wxUSE_MENUS
322
323 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
324
325 // notify the parent about this window destruction
326 if ( m_parent )
327 m_parent->RemoveChild(this);
328
329 #if wxUSE_CARET
330 delete m_caret;
331 #endif // wxUSE_CARET
332
333 #if wxUSE_VALIDATORS
334 delete m_windowValidator;
335 #endif // wxUSE_VALIDATORS
336
337 #if wxUSE_CONSTRAINTS
338 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
339 // at deleted windows as they delete themselves.
340 DeleteRelatedConstraints();
341
342 if ( m_constraints )
343 {
344 // This removes any dangling pointers to this window in other windows'
345 // constraintsInvolvedIn lists.
346 UnsetConstraints(m_constraints);
347 delete m_constraints;
348 m_constraints = NULL;
349 }
350 #endif // wxUSE_CONSTRAINTS
351
352 if ( m_containingSizer )
353 m_containingSizer->Detach( (wxWindow*)this );
354
355 delete m_windowSizer;
356
357 #if wxUSE_DRAG_AND_DROP
358 delete m_dropTarget;
359 #endif // wxUSE_DRAG_AND_DROP
360
361 #if wxUSE_TOOLTIPS
362 delete m_tooltip;
363 #endif // wxUSE_TOOLTIPS
364
365 #if wxUSE_ACCESSIBILITY
366 delete m_accessible;
367 #endif
368
369 #if wxUSE_HELP
370 // NB: this has to be called unconditionally, because we don't know
371 // whether this window has associated help text or not
372 wxHelpProvider *helpProvider = wxHelpProvider::Get();
373 if ( helpProvider )
374 helpProvider->RemoveHelp(this);
375 #endif
376 }
377
378 bool wxWindowBase::IsBeingDeleted() const
379 {
380 return m_isBeingDeleted ||
381 (!IsTopLevel() && m_parent && m_parent->IsBeingDeleted());
382 }
383
384 void wxWindowBase::SendDestroyEvent()
385 {
386 if ( m_isBeingDeleted )
387 {
388 // we could have been already called from a more derived class dtor,
389 // e.g. ~wxTLW calls us and so does ~wxWindow and the latter call
390 // should be simply ignored
391 return;
392 }
393
394 m_isBeingDeleted = true;
395
396 wxWindowDestroyEvent event;
397 event.SetEventObject(this);
398 event.SetId(GetId());
399 GetEventHandler()->ProcessEvent(event);
400 }
401
402 bool wxWindowBase::Destroy()
403 {
404 SendDestroyEvent();
405
406 delete this;
407
408 return true;
409 }
410
411 bool wxWindowBase::Close(bool force)
412 {
413 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
414 event.SetEventObject(this);
415 event.SetCanVeto(!force);
416
417 // return false if window wasn't closed because the application vetoed the
418 // close event
419 return HandleWindowEvent(event) && !event.GetVeto();
420 }
421
422 bool wxWindowBase::DestroyChildren()
423 {
424 wxWindowList::compatibility_iterator node;
425 for ( ;; )
426 {
427 // we iterate until the list becomes empty
428 node = GetChildren().GetFirst();
429 if ( !node )
430 break;
431
432 wxWindow *child = node->GetData();
433
434 // note that we really want to delete it immediately so don't call the
435 // possible overridden Destroy() version which might not delete the
436 // child immediately resulting in problems with our (top level) child
437 // outliving its parent
438 child->wxWindowBase::Destroy();
439
440 wxASSERT_MSG( !GetChildren().Find(child),
441 wxT("child didn't remove itself using RemoveChild()") );
442 }
443
444 return true;
445 }
446
447 // ----------------------------------------------------------------------------
448 // size/position related methods
449 // ----------------------------------------------------------------------------
450
451 // centre the window with respect to its parent in either (or both) directions
452 void wxWindowBase::DoCentre(int dir)
453 {
454 wxCHECK_RET( !(dir & wxCENTRE_ON_SCREEN) && GetParent(),
455 wxT("this method only implements centering child windows") );
456
457 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir));
458 }
459
460 // fits the window around the children
461 void wxWindowBase::Fit()
462 {
463 if ( !GetChildren().empty() )
464 {
465 SetSize(GetBestSize());
466 }
467 //else: do nothing if we have no children
468 }
469
470 // fits virtual size (ie. scrolled area etc.) around children
471 void wxWindowBase::FitInside()
472 {
473 if ( GetChildren().GetCount() > 0 )
474 {
475 SetVirtualSize( GetBestVirtualSize() );
476 }
477 }
478
479 // On Mac, scrollbars are explicitly children.
480 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
481 static bool wxHasRealChildren(const wxWindowBase* win)
482 {
483 int realChildCount = 0;
484
485 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
486 node;
487 node = node->GetNext() )
488 {
489 wxWindow *win = node->GetData();
490 if ( !win->IsTopLevel() && win->IsShown()
491 #if wxUSE_SCROLLBAR
492 && !win->IsKindOf(CLASSINFO(wxScrollBar))
493 #endif
494 )
495 realChildCount ++;
496 }
497 return (realChildCount > 0);
498 }
499 #endif
500
501 void wxWindowBase::InvalidateBestSize()
502 {
503 m_bestSizeCache = wxDefaultSize;
504
505 // parent's best size calculation may depend on its children's
506 // as long as child window we are in is not top level window itself
507 // (because the TLW size is never resized automatically)
508 // so let's invalidate it as well to be safe:
509 if (m_parent && !IsTopLevel())
510 m_parent->InvalidateBestSize();
511 }
512
513 // return the size best suited for the current window
514 wxSize wxWindowBase::DoGetBestSize() const
515 {
516 wxSize best;
517
518 if ( m_windowSizer )
519 {
520 best = m_windowSizer->GetMinSize();
521 }
522 #if wxUSE_CONSTRAINTS
523 else if ( m_constraints )
524 {
525 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
526
527 // our minimal acceptable size is such that all our windows fit inside
528 int maxX = 0,
529 maxY = 0;
530
531 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
532 node;
533 node = node->GetNext() )
534 {
535 wxLayoutConstraints *c = node->GetData()->GetConstraints();
536 if ( !c )
537 {
538 // it's not normal that we have an unconstrained child, but
539 // what can we do about it?
540 continue;
541 }
542
543 int x = c->right.GetValue(),
544 y = c->bottom.GetValue();
545
546 if ( x > maxX )
547 maxX = x;
548
549 if ( y > maxY )
550 maxY = y;
551
552 // TODO: we must calculate the overlaps somehow, otherwise we
553 // will never return a size bigger than the current one :-(
554 }
555
556 best = wxSize(maxX, maxY);
557 }
558 #endif // wxUSE_CONSTRAINTS
559 else if ( !GetChildren().empty()
560 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
561 && wxHasRealChildren(this)
562 #endif
563 )
564 {
565 // our minimal acceptable size is such that all our visible child
566 // windows fit inside
567 int maxX = 0,
568 maxY = 0;
569
570 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
571 node;
572 node = node->GetNext() )
573 {
574 wxWindow *win = node->GetData();
575 if ( win->IsTopLevel()
576 || !win->IsShown()
577 #if wxUSE_STATUSBAR
578 || wxDynamicCast(win, wxStatusBar)
579 #endif // wxUSE_STATUSBAR
580 )
581 {
582 // dialogs and frames lie in different top level windows -
583 // don't deal with them here; as for the status bars, they
584 // don't lie in the client area at all
585 continue;
586 }
587
588 int wx, wy, ww, wh;
589 win->GetPosition(&wx, &wy);
590
591 // if the window hadn't been positioned yet, assume that it is in
592 // the origin
593 if ( wx == wxDefaultCoord )
594 wx = 0;
595 if ( wy == wxDefaultCoord )
596 wy = 0;
597
598 win->GetSize(&ww, &wh);
599 if ( wx + ww > maxX )
600 maxX = wx + ww;
601 if ( wy + wh > maxY )
602 maxY = wy + wh;
603 }
604
605 best = wxSize(maxX, maxY);
606 }
607 else // ! has children
608 {
609 wxSize size = GetMinSize();
610 if ( !size.IsFullySpecified() )
611 {
612 // if the window doesn't define its best size we assume that it can
613 // be arbitrarily small -- usually this is not the case, of course,
614 // but we have no way to know what the limit is, it should really
615 // override DoGetBestClientSize() itself to tell us
616 size.SetDefaults(wxSize(1, 1));
617 }
618
619 // return as-is, unadjusted by the client size difference.
620 return size;
621 }
622
623 // Add any difference between size and client size
624 wxSize diff = GetSize() - GetClientSize();
625 best.x += wxMax(0, diff.x);
626 best.y += wxMax(0, diff.y);
627
628 return best;
629 }
630
631 // helper of GetWindowBorderSize(): as many ports don't implement support for
632 // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
633 // fallbacks in this case
634 static int wxGetMetricOrDefault(wxSystemMetric what, const wxWindowBase* win)
635 {
636 int rc = wxSystemSettings::GetMetric(
637 what, static_cast<wxWindow*>(const_cast<wxWindowBase*>(win)));
638 if ( rc == -1 )
639 {
640 switch ( what )
641 {
642 case wxSYS_BORDER_X:
643 case wxSYS_BORDER_Y:
644 // 2D border is by default 1 pixel wide
645 rc = 1;
646 break;
647
648 case wxSYS_EDGE_X:
649 case wxSYS_EDGE_Y:
650 // 3D borders are by default 2 pixels
651 rc = 2;
652 break;
653
654 default:
655 wxFAIL_MSG( wxT("unexpected wxGetMetricOrDefault() argument") );
656 rc = 0;
657 }
658 }
659
660 return rc;
661 }
662
663 wxSize wxWindowBase::GetWindowBorderSize() const
664 {
665 wxSize size;
666
667 switch ( GetBorder() )
668 {
669 case wxBORDER_NONE:
670 // nothing to do, size is already (0, 0)
671 break;
672
673 case wxBORDER_SIMPLE:
674 case wxBORDER_STATIC:
675 size.x = wxGetMetricOrDefault(wxSYS_BORDER_X, this);
676 size.y = wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
677 break;
678
679 case wxBORDER_SUNKEN:
680 case wxBORDER_RAISED:
681 size.x = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X, this),
682 wxGetMetricOrDefault(wxSYS_BORDER_X, this));
683 size.y = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y, this),
684 wxGetMetricOrDefault(wxSYS_BORDER_Y, this));
685 break;
686
687 case wxBORDER_DOUBLE:
688 size.x = wxGetMetricOrDefault(wxSYS_EDGE_X, this) +
689 wxGetMetricOrDefault(wxSYS_BORDER_X, this);
690 size.y = wxGetMetricOrDefault(wxSYS_EDGE_Y, this) +
691 wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
692 break;
693
694 default:
695 wxFAIL_MSG(wxT("Unknown border style."));
696 break;
697 }
698
699 // we have borders on both sides
700 return size*2;
701 }
702
703 wxSize wxWindowBase::GetEffectiveMinSize() const
704 {
705 // merge the best size with the min size, giving priority to the min size
706 wxSize min = GetMinSize();
707
708 if (min.x == wxDefaultCoord || min.y == wxDefaultCoord)
709 {
710 wxSize best = GetBestSize();
711 if (min.x == wxDefaultCoord) min.x = best.x;
712 if (min.y == wxDefaultCoord) min.y = best.y;
713 }
714
715 return min;
716 }
717
718 wxSize wxWindowBase::GetBestSize() const
719 {
720 if ( !m_windowSizer && m_bestSizeCache.IsFullySpecified() )
721 return m_bestSizeCache;
722
723 // call DoGetBestClientSize() first, if a derived class overrides it wants
724 // it to be used
725 wxSize size = DoGetBestClientSize();
726 if ( size != wxDefaultSize )
727 {
728 size += DoGetBorderSize();
729
730 CacheBestSize(size);
731 return size;
732 }
733
734 return DoGetBestSize();
735 }
736
737 void wxWindowBase::SetMinSize(const wxSize& minSize)
738 {
739 m_minWidth = minSize.x;
740 m_minHeight = minSize.y;
741 }
742
743 void wxWindowBase::SetMaxSize(const wxSize& maxSize)
744 {
745 m_maxWidth = maxSize.x;
746 m_maxHeight = maxSize.y;
747 }
748
749 void wxWindowBase::SetInitialSize(const wxSize& size)
750 {
751 // Set the min size to the size passed in. This will usually either be
752 // wxDefaultSize or the size passed to this window's ctor/Create function.
753 SetMinSize(size);
754
755 // Merge the size with the best size if needed
756 wxSize best = GetEffectiveMinSize();
757
758 // If the current size doesn't match then change it
759 if (GetSize() != best)
760 SetSize(best);
761 }
762
763
764 // by default the origin is not shifted
765 wxPoint wxWindowBase::GetClientAreaOrigin() const
766 {
767 return wxPoint(0,0);
768 }
769
770 wxSize wxWindowBase::ClientToWindowSize(const wxSize& size) const
771 {
772 const wxSize diff(GetSize() - GetClientSize());
773
774 return wxSize(size.x == -1 ? -1 : size.x + diff.x,
775 size.y == -1 ? -1 : size.y + diff.y);
776 }
777
778 wxSize wxWindowBase::WindowToClientSize(const wxSize& size) const
779 {
780 const wxSize diff(GetSize() - GetClientSize());
781
782 return wxSize(size.x == -1 ? -1 : size.x - diff.x,
783 size.y == -1 ? -1 : size.y - diff.y);
784 }
785
786 void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
787 {
788 if ( m_windowVariant != variant )
789 {
790 m_windowVariant = variant;
791
792 DoSetWindowVariant(variant);
793 }
794 }
795
796 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
797 {
798 // adjust the font height to correspond to our new variant (notice that
799 // we're only called if something really changed)
800 wxFont font = GetFont();
801 int size = font.GetPointSize();
802 switch ( variant )
803 {
804 case wxWINDOW_VARIANT_NORMAL:
805 break;
806
807 case wxWINDOW_VARIANT_SMALL:
808 size *= 3;
809 size /= 4;
810 break;
811
812 case wxWINDOW_VARIANT_MINI:
813 size *= 2;
814 size /= 3;
815 break;
816
817 case wxWINDOW_VARIANT_LARGE:
818 size *= 5;
819 size /= 4;
820 break;
821
822 default:
823 wxFAIL_MSG(wxT("unexpected window variant"));
824 break;
825 }
826
827 font.SetPointSize(size);
828 SetFont(font);
829 }
830
831 void wxWindowBase::DoSetSizeHints( int minW, int minH,
832 int maxW, int maxH,
833 int WXUNUSED(incW), int WXUNUSED(incH) )
834 {
835 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
836 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
837 wxT("min width/height must be less than max width/height!") );
838
839 m_minWidth = minW;
840 m_maxWidth = maxW;
841 m_minHeight = minH;
842 m_maxHeight = maxH;
843 }
844
845
846 #if WXWIN_COMPATIBILITY_2_8
847 void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW), int WXUNUSED(minH),
848 int WXUNUSED(maxW), int WXUNUSED(maxH))
849 {
850 }
851
852 void wxWindowBase::SetVirtualSizeHints(const wxSize& WXUNUSED(minsize),
853 const wxSize& WXUNUSED(maxsize))
854 {
855 }
856 #endif // WXWIN_COMPATIBILITY_2_8
857
858 void wxWindowBase::DoSetVirtualSize( int x, int y )
859 {
860 m_virtualSize = wxSize(x, y);
861 }
862
863 wxSize wxWindowBase::DoGetVirtualSize() const
864 {
865 // we should use the entire client area so if it is greater than our
866 // virtual size, expand it to fit (otherwise if the window is big enough we
867 // wouldn't be using parts of it)
868 wxSize size = GetClientSize();
869 if ( m_virtualSize.x > size.x )
870 size.x = m_virtualSize.x;
871
872 if ( m_virtualSize.y >= size.y )
873 size.y = m_virtualSize.y;
874
875 return size;
876 }
877
878 void wxWindowBase::DoGetScreenPosition(int *x, int *y) const
879 {
880 // screen position is the same as (0, 0) in client coords for non TLWs (and
881 // TLWs override this method)
882 if ( x )
883 *x = 0;
884 if ( y )
885 *y = 0;
886
887 ClientToScreen(x, y);
888 }
889
890 void wxWindowBase::SendSizeEvent(int flags)
891 {
892 wxSizeEvent event(GetSize(), GetId());
893 event.SetEventObject(this);
894 if ( flags & wxSEND_EVENT_POST )
895 wxPostEvent(this, event);
896 else
897 HandleWindowEvent(event);
898 }
899
900 void wxWindowBase::SendSizeEventToParent(int flags)
901 {
902 wxWindow * const parent = GetParent();
903 if ( parent && !parent->IsBeingDeleted() )
904 parent->SendSizeEvent(flags);
905 }
906
907 bool wxWindowBase::HasScrollbar(int orient) const
908 {
909 // if scrolling in the given direction is disabled, we can't have the
910 // corresponding scrollbar no matter what
911 if ( !CanScroll(orient) )
912 return false;
913
914 const wxSize sizeVirt = GetVirtualSize();
915 const wxSize sizeClient = GetClientSize();
916
917 return orient == wxHORIZONTAL ? sizeVirt.x > sizeClient.x
918 : sizeVirt.y > sizeClient.y;
919 }
920
921 // ----------------------------------------------------------------------------
922 // show/hide/enable/disable the window
923 // ----------------------------------------------------------------------------
924
925 bool wxWindowBase::Show(bool show)
926 {
927 if ( show != m_isShown )
928 {
929 m_isShown = show;
930
931 return true;
932 }
933 else
934 {
935 return false;
936 }
937 }
938
939 bool wxWindowBase::IsEnabled() const
940 {
941 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
942 }
943
944 void wxWindowBase::NotifyWindowOnEnableChange(bool enabled)
945 {
946 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
947 DoEnable(enabled);
948 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
949
950 OnEnabled(enabled);
951
952 // If we are top-level then the logic doesn't apply - otherwise
953 // showing a modal dialog would result in total greying out (and ungreying
954 // out later) of everything which would be really ugly
955 if ( IsTopLevel() )
956 return;
957
958 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
959 node;
960 node = node->GetNext() )
961 {
962 wxWindowBase * const child = node->GetData();
963 if ( !child->IsTopLevel() && child->IsThisEnabled() )
964 child->NotifyWindowOnEnableChange(enabled);
965 }
966 }
967
968 bool wxWindowBase::Enable(bool enable)
969 {
970 if ( enable == IsThisEnabled() )
971 return false;
972
973 m_isEnabled = enable;
974
975 #ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
976 DoEnable(enable);
977 #else // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
978 wxWindowBase * const parent = GetParent();
979 if( !IsTopLevel() && parent && !parent->IsEnabled() )
980 {
981 return true;
982 }
983 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
984
985 NotifyWindowOnEnableChange(enable);
986
987 return true;
988 }
989
990 bool wxWindowBase::IsShownOnScreen() const
991 {
992 // A window is shown on screen if it itself is shown and so are all its
993 // parents. But if a window is toplevel one, then its always visible on
994 // screen if IsShown() returns true, even if it has a hidden parent.
995 return IsShown() &&
996 (IsTopLevel() || GetParent() == NULL || GetParent()->IsShownOnScreen());
997 }
998
999 // ----------------------------------------------------------------------------
1000 // RTTI
1001 // ----------------------------------------------------------------------------
1002
1003 bool wxWindowBase::IsTopLevel() const
1004 {
1005 return false;
1006 }
1007
1008 // ----------------------------------------------------------------------------
1009 // Freeze/Thaw
1010 // ----------------------------------------------------------------------------
1011
1012 void wxWindowBase::Freeze()
1013 {
1014 if ( !m_freezeCount++ )
1015 {
1016 // physically freeze this window:
1017 DoFreeze();
1018
1019 // and recursively freeze all children:
1020 for ( wxWindowList::iterator i = GetChildren().begin();
1021 i != GetChildren().end(); ++i )
1022 {
1023 wxWindow *child = *i;
1024 if ( child->IsTopLevel() )
1025 continue;
1026
1027 child->Freeze();
1028 }
1029 }
1030 }
1031
1032 void wxWindowBase::Thaw()
1033 {
1034 wxASSERT_MSG( m_freezeCount, "Thaw() without matching Freeze()" );
1035
1036 if ( !--m_freezeCount )
1037 {
1038 // recursively thaw all children:
1039 for ( wxWindowList::iterator i = GetChildren().begin();
1040 i != GetChildren().end(); ++i )
1041 {
1042 wxWindow *child = *i;
1043 if ( child->IsTopLevel() )
1044 continue;
1045
1046 child->Thaw();
1047 }
1048
1049 // physically thaw this window:
1050 DoThaw();
1051 }
1052 }
1053
1054 // ----------------------------------------------------------------------------
1055 // reparenting the window
1056 // ----------------------------------------------------------------------------
1057
1058 void wxWindowBase::AddChild(wxWindowBase *child)
1059 {
1060 wxCHECK_RET( child, wxT("can't add a NULL child") );
1061
1062 // this should never happen and it will lead to a crash later if it does
1063 // because RemoveChild() will remove only one node from the children list
1064 // and the other(s) one(s) will be left with dangling pointers in them
1065 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), wxT("AddChild() called twice") );
1066
1067 GetChildren().Append((wxWindow*)child);
1068 child->SetParent(this);
1069
1070 // adding a child while frozen will assert when thawed, so freeze it as if
1071 // it had been already present when we were frozen
1072 if ( IsFrozen() && !child->IsTopLevel() )
1073 child->Freeze();
1074 }
1075
1076 void wxWindowBase::RemoveChild(wxWindowBase *child)
1077 {
1078 wxCHECK_RET( child, wxT("can't remove a NULL child") );
1079
1080 // removing a child while frozen may result in permanently frozen window
1081 // if used e.g. from Reparent(), so thaw it
1082 //
1083 // NB: IsTopLevel() doesn't return true any more when a TLW child is being
1084 // removed from its ~wxWindowBase, so check for IsBeingDeleted() too
1085 if ( IsFrozen() && !child->IsBeingDeleted() && !child->IsTopLevel() )
1086 child->Thaw();
1087
1088 GetChildren().DeleteObject((wxWindow *)child);
1089 child->SetParent(NULL);
1090 }
1091
1092 bool wxWindowBase::Reparent(wxWindowBase *newParent)
1093 {
1094 wxWindow *oldParent = GetParent();
1095 if ( newParent == oldParent )
1096 {
1097 // nothing done
1098 return false;
1099 }
1100
1101 const bool oldEnabledState = IsEnabled();
1102
1103 // unlink this window from the existing parent.
1104 if ( oldParent )
1105 {
1106 oldParent->RemoveChild(this);
1107 }
1108 else
1109 {
1110 wxTopLevelWindows.DeleteObject((wxWindow *)this);
1111 }
1112
1113 // add it to the new one
1114 if ( newParent )
1115 {
1116 newParent->AddChild(this);
1117 }
1118 else
1119 {
1120 wxTopLevelWindows.Append((wxWindow *)this);
1121 }
1122
1123 // We need to notify window (and its subwindows) if by changing the parent
1124 // we also change our enabled/disabled status.
1125 const bool newEnabledState = IsEnabled();
1126 if ( newEnabledState != oldEnabledState )
1127 {
1128 NotifyWindowOnEnableChange(newEnabledState);
1129 }
1130
1131 return true;
1132 }
1133
1134 // ----------------------------------------------------------------------------
1135 // event handler stuff
1136 // ----------------------------------------------------------------------------
1137
1138 void wxWindowBase::SetEventHandler(wxEvtHandler *handler)
1139 {
1140 wxCHECK_RET(handler != NULL, "SetEventHandler(NULL) called");
1141
1142 m_eventHandler = handler;
1143 }
1144
1145 void wxWindowBase::SetNextHandler(wxEvtHandler *WXUNUSED(handler))
1146 {
1147 // disable wxEvtHandler chain mechanism for wxWindows:
1148 // wxWindow uses its own stack mechanism which doesn't mix well with wxEvtHandler's one
1149
1150 wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1151 }
1152 void wxWindowBase::SetPreviousHandler(wxEvtHandler *WXUNUSED(handler))
1153 {
1154 // we can't simply wxFAIL here as in SetNextHandler: in fact the last
1155 // handler of our stack when is destroyed will be Unlink()ed and thus
1156 // will call this function to update the pointer of this window...
1157
1158 //wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1159 }
1160
1161 void wxWindowBase::PushEventHandler(wxEvtHandler *handlerToPush)
1162 {
1163 wxCHECK_RET( handlerToPush != NULL, "PushEventHandler(NULL) called" );
1164
1165 // the new handler is going to be part of the wxWindow stack of event handlers:
1166 // it can't be part also of an event handler double-linked chain:
1167 wxASSERT_MSG(handlerToPush->IsUnlinked(),
1168 "The handler being pushed in the wxWindow stack shouldn't be part of "
1169 "a wxEvtHandler chain; call Unlink() on it first");
1170
1171 wxEvtHandler *handlerOld = GetEventHandler();
1172 wxCHECK_RET( handlerOld, "an old event handler is NULL?" );
1173
1174 // now use wxEvtHandler double-linked list to implement a stack:
1175 handlerToPush->SetNextHandler(handlerOld);
1176
1177 if (handlerOld != this)
1178 handlerOld->SetPreviousHandler(handlerToPush);
1179
1180 SetEventHandler(handlerToPush);
1181
1182 #if wxDEBUG_LEVEL
1183 // final checks of the operations done above:
1184 wxASSERT_MSG( handlerToPush->GetPreviousHandler() == NULL,
1185 "the first handler of the wxWindow stack should "
1186 "have no previous handlers set" );
1187 wxASSERT_MSG( handlerToPush->GetNextHandler() != NULL,
1188 "the first handler of the wxWindow stack should "
1189 "have non-NULL next handler" );
1190
1191 wxEvtHandler* pLast = handlerToPush;
1192 while ( pLast && pLast != this )
1193 pLast = pLast->GetNextHandler();
1194 wxASSERT_MSG( pLast->GetNextHandler() == NULL,
1195 "the last handler of the wxWindow stack should "
1196 "have this window as next handler" );
1197 #endif // wxDEBUG_LEVEL
1198 }
1199
1200 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
1201 {
1202 // we need to pop the wxWindow stack, i.e. we need to remove the first handler
1203
1204 wxEvtHandler *firstHandler = GetEventHandler();
1205 wxCHECK_MSG( firstHandler != NULL, NULL, "wxWindow cannot have a NULL event handler" );
1206 wxCHECK_MSG( firstHandler != this, NULL, "cannot pop the wxWindow itself" );
1207 wxCHECK_MSG( firstHandler->GetPreviousHandler() == NULL, NULL,
1208 "the first handler of the wxWindow stack should have no previous handlers set" );
1209
1210 wxEvtHandler *secondHandler = firstHandler->GetNextHandler();
1211 wxCHECK_MSG( secondHandler != NULL, NULL,
1212 "the first handler of the wxWindow stack should have non-NULL next handler" );
1213
1214 firstHandler->SetNextHandler(NULL);
1215 secondHandler->SetPreviousHandler(NULL);
1216
1217 // now firstHandler is completely unlinked; set secondHandler as the new window event handler
1218 SetEventHandler(secondHandler);
1219
1220 if ( deleteHandler )
1221 {
1222 delete firstHandler;
1223 firstHandler = NULL;
1224 }
1225
1226 return firstHandler;
1227 }
1228
1229 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handlerToRemove)
1230 {
1231 wxCHECK_MSG( handlerToRemove != NULL, false, "RemoveEventHandler(NULL) called" );
1232 wxCHECK_MSG( handlerToRemove != this, false, "Cannot remove the window itself" );
1233
1234 if (handlerToRemove == GetEventHandler())
1235 {
1236 // removing the first event handler is equivalent to "popping" the stack
1237 PopEventHandler(false);
1238 return true;
1239 }
1240
1241 // NOTE: the wxWindow event handler list is always terminated with "this" handler
1242 wxEvtHandler *handlerCur = GetEventHandler()->GetNextHandler();
1243 while ( handlerCur != this )
1244 {
1245 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
1246
1247 if ( handlerCur == handlerToRemove )
1248 {
1249 handlerCur->Unlink();
1250
1251 wxASSERT_MSG( handlerCur != GetEventHandler(),
1252 "the case Remove == Pop should was already handled" );
1253 return true;
1254 }
1255
1256 handlerCur = handlerNext;
1257 }
1258
1259 wxFAIL_MSG( wxT("where has the event handler gone?") );
1260
1261 return false;
1262 }
1263
1264 bool wxWindowBase::HandleWindowEvent(wxEvent& event) const
1265 {
1266 // SafelyProcessEvent() will handle exceptions nicely
1267 return GetEventHandler()->SafelyProcessEvent(event);
1268 }
1269
1270 // ----------------------------------------------------------------------------
1271 // colours, fonts &c
1272 // ----------------------------------------------------------------------------
1273
1274 void wxWindowBase::InheritAttributes()
1275 {
1276 const wxWindowBase * const parent = GetParent();
1277 if ( !parent )
1278 return;
1279
1280 // we only inherit attributes which had been explicitly set for the parent
1281 // which ensures that this only happens if the user really wants it and
1282 // not by default which wouldn't make any sense in modern GUIs where the
1283 // controls don't all use the same fonts (nor colours)
1284 if ( parent->m_inheritFont && !m_hasFont )
1285 SetFont(parent->GetFont());
1286
1287 // in addition, there is a possibility to explicitly forbid inheriting
1288 // colours at each class level by overriding ShouldInheritColours()
1289 if ( ShouldInheritColours() )
1290 {
1291 if ( parent->m_inheritFgCol && !m_hasFgCol )
1292 SetForegroundColour(parent->GetForegroundColour());
1293
1294 // inheriting (solid) background colour is wrong as it totally breaks
1295 // any kind of themed backgrounds
1296 //
1297 // instead, the controls should use the same background as their parent
1298 // (ideally by not drawing it at all)
1299 #if 0
1300 if ( parent->m_inheritBgCol && !m_hasBgCol )
1301 SetBackgroundColour(parent->GetBackgroundColour());
1302 #endif // 0
1303 }
1304 }
1305
1306 /* static */ wxVisualAttributes
1307 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1308 {
1309 // it is important to return valid values for all attributes from here,
1310 // GetXXX() below rely on this
1311 wxVisualAttributes attrs;
1312 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1313 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1314
1315 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1316 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1317 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1318 // colour on other platforms.
1319
1320 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1321 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1322 #else
1323 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1324 #endif
1325 return attrs;
1326 }
1327
1328 wxColour wxWindowBase::GetBackgroundColour() const
1329 {
1330 if ( !m_backgroundColour.IsOk() )
1331 {
1332 wxASSERT_MSG( !m_hasBgCol, wxT("we have invalid explicit bg colour?") );
1333
1334 // get our default background colour
1335 wxColour colBg = GetDefaultAttributes().colBg;
1336
1337 // we must return some valid colour to avoid redoing this every time
1338 // and also to avoid surprizing the applications written for older
1339 // wxWidgets versions where GetBackgroundColour() always returned
1340 // something -- so give them something even if it doesn't make sense
1341 // for this window (e.g. it has a themed background)
1342 if ( !colBg.Ok() )
1343 colBg = GetClassDefaultAttributes().colBg;
1344
1345 return colBg;
1346 }
1347 else
1348 return m_backgroundColour;
1349 }
1350
1351 wxColour wxWindowBase::GetForegroundColour() const
1352 {
1353 // logic is the same as above
1354 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
1355 {
1356 wxColour colFg = GetDefaultAttributes().colFg;
1357
1358 if ( !colFg.IsOk() )
1359 colFg = GetClassDefaultAttributes().colFg;
1360
1361 return colFg;
1362 }
1363 else
1364 return m_foregroundColour;
1365 }
1366
1367 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1368 {
1369 if ( colour == m_backgroundColour )
1370 return false;
1371
1372 m_hasBgCol = colour.IsOk();
1373
1374 m_inheritBgCol = m_hasBgCol;
1375 m_backgroundColour = colour;
1376 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
1377 return true;
1378 }
1379
1380 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1381 {
1382 if (colour == m_foregroundColour )
1383 return false;
1384
1385 m_hasFgCol = colour.IsOk();
1386 m_inheritFgCol = m_hasFgCol;
1387 m_foregroundColour = colour;
1388 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
1389 return true;
1390 }
1391
1392 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1393 {
1394 // setting an invalid cursor is ok, it means that we don't have any special
1395 // cursor
1396 if ( m_cursor.IsSameAs(cursor) )
1397 {
1398 // no change
1399 return false;
1400 }
1401
1402 m_cursor = cursor;
1403
1404 return true;
1405 }
1406
1407 wxFont wxWindowBase::GetFont() const
1408 {
1409 // logic is the same as in GetBackgroundColour()
1410 if ( !m_font.IsOk() )
1411 {
1412 wxASSERT_MSG( !m_hasFont, wxT("we have invalid explicit font?") );
1413
1414 wxFont font = GetDefaultAttributes().font;
1415 if ( !font.IsOk() )
1416 font = GetClassDefaultAttributes().font;
1417
1418 return font;
1419 }
1420 else
1421 return m_font;
1422 }
1423
1424 bool wxWindowBase::SetFont(const wxFont& font)
1425 {
1426 if ( font == m_font )
1427 {
1428 // no change
1429 return false;
1430 }
1431
1432 m_font = font;
1433 m_hasFont = font.IsOk();
1434 m_inheritFont = m_hasFont;
1435
1436 InvalidateBestSize();
1437
1438 return true;
1439 }
1440
1441 #if wxUSE_PALETTE
1442
1443 void wxWindowBase::SetPalette(const wxPalette& pal)
1444 {
1445 m_hasCustomPalette = true;
1446 m_palette = pal;
1447
1448 // VZ: can anyone explain me what do we do here?
1449 wxWindowDC d((wxWindow *) this);
1450 d.SetPalette(pal);
1451 }
1452
1453 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1454 {
1455 wxWindow *win = (wxWindow *)this;
1456 while ( win && !win->HasCustomPalette() )
1457 {
1458 win = win->GetParent();
1459 }
1460
1461 return win;
1462 }
1463
1464 #endif // wxUSE_PALETTE
1465
1466 #if wxUSE_CARET
1467 void wxWindowBase::SetCaret(wxCaret *caret)
1468 {
1469 if ( m_caret )
1470 {
1471 delete m_caret;
1472 }
1473
1474 m_caret = caret;
1475
1476 if ( m_caret )
1477 {
1478 wxASSERT_MSG( m_caret->GetWindow() == this,
1479 wxT("caret should be created associated to this window") );
1480 }
1481 }
1482 #endif // wxUSE_CARET
1483
1484 #if wxUSE_VALIDATORS
1485 // ----------------------------------------------------------------------------
1486 // validators
1487 // ----------------------------------------------------------------------------
1488
1489 void wxWindowBase::SetValidator(const wxValidator& validator)
1490 {
1491 if ( m_windowValidator )
1492 delete m_windowValidator;
1493
1494 m_windowValidator = (wxValidator *)validator.Clone();
1495
1496 if ( m_windowValidator )
1497 m_windowValidator->SetWindow(this);
1498 }
1499 #endif // wxUSE_VALIDATORS
1500
1501 // ----------------------------------------------------------------------------
1502 // update region stuff
1503 // ----------------------------------------------------------------------------
1504
1505 wxRect wxWindowBase::GetUpdateClientRect() const
1506 {
1507 wxRegion rgnUpdate = GetUpdateRegion();
1508 rgnUpdate.Intersect(GetClientRect());
1509 wxRect rectUpdate = rgnUpdate.GetBox();
1510 wxPoint ptOrigin = GetClientAreaOrigin();
1511 rectUpdate.x -= ptOrigin.x;
1512 rectUpdate.y -= ptOrigin.y;
1513
1514 return rectUpdate;
1515 }
1516
1517 bool wxWindowBase::DoIsExposed(int x, int y) const
1518 {
1519 return m_updateRegion.Contains(x, y) != wxOutRegion;
1520 }
1521
1522 bool wxWindowBase::DoIsExposed(int x, int y, int w, int h) const
1523 {
1524 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1525 }
1526
1527 void wxWindowBase::ClearBackground()
1528 {
1529 // wxGTK uses its own version, no need to add never used code
1530 #ifndef __WXGTK__
1531 wxClientDC dc((wxWindow *)this);
1532 wxBrush brush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID);
1533 dc.SetBackground(brush);
1534 dc.Clear();
1535 #endif // __WXGTK__
1536 }
1537
1538 // ----------------------------------------------------------------------------
1539 // find child window by id or name
1540 // ----------------------------------------------------------------------------
1541
1542 wxWindow *wxWindowBase::FindWindow(long id) const
1543 {
1544 if ( id == m_windowId )
1545 return (wxWindow *)this;
1546
1547 wxWindowBase *res = NULL;
1548 wxWindowList::compatibility_iterator node;
1549 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1550 {
1551 wxWindowBase *child = node->GetData();
1552 res = child->FindWindow( id );
1553 }
1554
1555 return (wxWindow *)res;
1556 }
1557
1558 wxWindow *wxWindowBase::FindWindow(const wxString& name) const
1559 {
1560 if ( name == m_windowName )
1561 return (wxWindow *)this;
1562
1563 wxWindowBase *res = NULL;
1564 wxWindowList::compatibility_iterator node;
1565 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1566 {
1567 wxWindow *child = node->GetData();
1568 res = child->FindWindow(name);
1569 }
1570
1571 return (wxWindow *)res;
1572 }
1573
1574
1575 // find any window by id or name or label: If parent is non-NULL, look through
1576 // children for a label or title matching the specified string. If NULL, look
1577 // through all top-level windows.
1578 //
1579 // to avoid duplicating code we reuse the same helper function but with
1580 // different comparators
1581
1582 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1583 const wxString& label, long id);
1584
1585 static
1586 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1587 long WXUNUSED(id))
1588 {
1589 return win->GetLabel() == label;
1590 }
1591
1592 static
1593 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1594 long WXUNUSED(id))
1595 {
1596 return win->GetName() == label;
1597 }
1598
1599 static
1600 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1601 long id)
1602 {
1603 return win->GetId() == id;
1604 }
1605
1606 // recursive helper for the FindWindowByXXX() functions
1607 static
1608 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1609 const wxString& label,
1610 long id,
1611 wxFindWindowCmp cmp)
1612 {
1613 if ( parent )
1614 {
1615 // see if this is the one we're looking for
1616 if ( (*cmp)(parent, label, id) )
1617 return (wxWindow *)parent;
1618
1619 // It wasn't, so check all its children
1620 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1621 node;
1622 node = node->GetNext() )
1623 {
1624 // recursively check each child
1625 wxWindow *win = (wxWindow *)node->GetData();
1626 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1627 if (retwin)
1628 return retwin;
1629 }
1630 }
1631
1632 // Not found
1633 return NULL;
1634 }
1635
1636 // helper for FindWindowByXXX()
1637 static
1638 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1639 const wxString& label,
1640 long id,
1641 wxFindWindowCmp cmp)
1642 {
1643 if ( parent )
1644 {
1645 // just check parent and all its children
1646 return wxFindWindowRecursively(parent, label, id, cmp);
1647 }
1648
1649 // start at very top of wx's windows
1650 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1651 node;
1652 node = node->GetNext() )
1653 {
1654 // recursively check each window & its children
1655 wxWindow *win = node->GetData();
1656 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1657 if (retwin)
1658 return retwin;
1659 }
1660
1661 return NULL;
1662 }
1663
1664 /* static */
1665 wxWindow *
1666 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1667 {
1668 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1669 }
1670
1671 /* static */
1672 wxWindow *
1673 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1674 {
1675 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1676
1677 if ( !win )
1678 {
1679 // fall back to the label
1680 win = FindWindowByLabel(title, parent);
1681 }
1682
1683 return win;
1684 }
1685
1686 /* static */
1687 wxWindow *
1688 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1689 {
1690 return wxFindWindowHelper(parent, wxEmptyString, id, wxFindWindowCmpIds);
1691 }
1692
1693 // ----------------------------------------------------------------------------
1694 // dialog oriented functions
1695 // ----------------------------------------------------------------------------
1696
1697 void wxWindowBase::MakeModal(bool modal)
1698 {
1699 // Disable all other windows
1700 if ( IsTopLevel() )
1701 {
1702 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1703 while (node)
1704 {
1705 wxWindow *win = node->GetData();
1706 if (win != this)
1707 win->Enable(!modal);
1708
1709 node = node->GetNext();
1710 }
1711 }
1712 }
1713
1714 bool wxWindowBase::Validate()
1715 {
1716 #if wxUSE_VALIDATORS
1717 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1718
1719 wxWindowList::compatibility_iterator node;
1720 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1721 {
1722 wxWindowBase *child = node->GetData();
1723 wxValidator *validator = child->GetValidator();
1724 if ( validator && !validator->Validate((wxWindow *)this) )
1725 {
1726 return false;
1727 }
1728
1729 if ( recurse && !child->Validate() )
1730 {
1731 return false;
1732 }
1733 }
1734 #endif // wxUSE_VALIDATORS
1735
1736 return true;
1737 }
1738
1739 bool wxWindowBase::TransferDataToWindow()
1740 {
1741 #if wxUSE_VALIDATORS
1742 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1743
1744 wxWindowList::compatibility_iterator node;
1745 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1746 {
1747 wxWindowBase *child = node->GetData();
1748 wxValidator *validator = child->GetValidator();
1749 if ( validator && !validator->TransferToWindow() )
1750 {
1751 wxLogWarning(_("Could not transfer data to window"));
1752 #if wxUSE_LOG
1753 wxLog::FlushActive();
1754 #endif // wxUSE_LOG
1755
1756 return false;
1757 }
1758
1759 if ( recurse )
1760 {
1761 if ( !child->TransferDataToWindow() )
1762 {
1763 // warning already given
1764 return false;
1765 }
1766 }
1767 }
1768 #endif // wxUSE_VALIDATORS
1769
1770 return true;
1771 }
1772
1773 bool wxWindowBase::TransferDataFromWindow()
1774 {
1775 #if wxUSE_VALIDATORS
1776 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1777
1778 wxWindowList::compatibility_iterator node;
1779 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1780 {
1781 wxWindow *child = node->GetData();
1782 wxValidator *validator = child->GetValidator();
1783 if ( validator && !validator->TransferFromWindow() )
1784 {
1785 // nop warning here because the application is supposed to give
1786 // one itself - we don't know here what might have gone wrongly
1787
1788 return false;
1789 }
1790
1791 if ( recurse )
1792 {
1793 if ( !child->TransferDataFromWindow() )
1794 {
1795 // warning already given
1796 return false;
1797 }
1798 }
1799 }
1800 #endif // wxUSE_VALIDATORS
1801
1802 return true;
1803 }
1804
1805 void wxWindowBase::InitDialog()
1806 {
1807 wxInitDialogEvent event(GetId());
1808 event.SetEventObject( this );
1809 GetEventHandler()->ProcessEvent(event);
1810 }
1811
1812 // ----------------------------------------------------------------------------
1813 // context-sensitive help support
1814 // ----------------------------------------------------------------------------
1815
1816 #if wxUSE_HELP
1817
1818 // associate this help text with this window
1819 void wxWindowBase::SetHelpText(const wxString& text)
1820 {
1821 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1822 if ( helpProvider )
1823 {
1824 helpProvider->AddHelp(this, text);
1825 }
1826 }
1827
1828 #if WXWIN_COMPATIBILITY_2_8
1829 // associate this help text with all windows with the same id as this
1830 // one
1831 void wxWindowBase::SetHelpTextForId(const wxString& text)
1832 {
1833 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1834 if ( helpProvider )
1835 {
1836 helpProvider->AddHelp(GetId(), text);
1837 }
1838 }
1839 #endif // WXWIN_COMPATIBILITY_2_8
1840
1841 // get the help string associated with this window (may be empty)
1842 // default implementation forwards calls to the help provider
1843 wxString
1844 wxWindowBase::GetHelpTextAtPoint(const wxPoint & WXUNUSED(pt),
1845 wxHelpEvent::Origin WXUNUSED(origin)) const
1846 {
1847 wxString text;
1848 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1849 if ( helpProvider )
1850 {
1851 text = helpProvider->GetHelp(this);
1852 }
1853
1854 return text;
1855 }
1856
1857 // show help for this window
1858 void wxWindowBase::OnHelp(wxHelpEvent& event)
1859 {
1860 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1861 if ( helpProvider )
1862 {
1863 wxPoint pos = event.GetPosition();
1864 const wxHelpEvent::Origin origin = event.GetOrigin();
1865 if ( origin == wxHelpEvent::Origin_Keyboard )
1866 {
1867 // if the help event was generated from keyboard it shouldn't
1868 // appear at the mouse position (which is still the only position
1869 // associated with help event) if the mouse is far away, although
1870 // we still do use the mouse position if it's over the window
1871 // because we suppose the user looks approximately at the mouse
1872 // already and so it would be more convenient than showing tooltip
1873 // at some arbitrary position which can be quite far from it
1874 const wxRect rectClient = GetClientRect();
1875 if ( !rectClient.Contains(ScreenToClient(pos)) )
1876 {
1877 // position help slightly under and to the right of this window
1878 pos = ClientToScreen(wxPoint(
1879 2*GetCharWidth(),
1880 rectClient.height + GetCharHeight()
1881 ));
1882 }
1883 }
1884
1885 if ( helpProvider->ShowHelpAtPoint(this, pos, origin) )
1886 {
1887 // skip the event.Skip() below
1888 return;
1889 }
1890 }
1891
1892 event.Skip();
1893 }
1894
1895 #endif // wxUSE_HELP
1896
1897 // ----------------------------------------------------------------------------
1898 // tooltips
1899 // ----------------------------------------------------------------------------
1900
1901 #if wxUSE_TOOLTIPS
1902
1903 wxString wxWindowBase::GetToolTipText() const
1904 {
1905 return m_tooltip ? m_tooltip->GetTip() : wxString();
1906 }
1907
1908 void wxWindowBase::SetToolTip( const wxString &tip )
1909 {
1910 // don't create the new tooltip if we already have one
1911 if ( m_tooltip )
1912 {
1913 m_tooltip->SetTip( tip );
1914 }
1915 else
1916 {
1917 SetToolTip( new wxToolTip( tip ) );
1918 }
1919
1920 // setting empty tooltip text does not remove the tooltip any more - use
1921 // SetToolTip(NULL) for this
1922 }
1923
1924 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1925 {
1926 if ( m_tooltip != tooltip )
1927 {
1928 if ( m_tooltip )
1929 delete m_tooltip;
1930
1931 m_tooltip = tooltip;
1932 }
1933 }
1934
1935 #endif // wxUSE_TOOLTIPS
1936
1937 // ----------------------------------------------------------------------------
1938 // constraints and sizers
1939 // ----------------------------------------------------------------------------
1940
1941 #if wxUSE_CONSTRAINTS
1942
1943 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1944 {
1945 if ( m_constraints )
1946 {
1947 UnsetConstraints(m_constraints);
1948 delete m_constraints;
1949 }
1950 m_constraints = constraints;
1951 if ( m_constraints )
1952 {
1953 // Make sure other windows know they're part of a 'meaningful relationship'
1954 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1955 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1956 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1957 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1958 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1959 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1960 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1961 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1962 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1963 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1964 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1965 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1966 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1967 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1968 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1969 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1970 }
1971 }
1972
1973 // This removes any dangling pointers to this window in other windows'
1974 // constraintsInvolvedIn lists.
1975 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1976 {
1977 if ( c )
1978 {
1979 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1980 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1981 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1982 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1983 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1984 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1985 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1986 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1987 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1988 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1989 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1990 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1991 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1992 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1993 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1994 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1995 }
1996 }
1997
1998 // Back-pointer to other windows we're involved with, so if we delete this
1999 // window, we must delete any constraints we're involved with.
2000 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
2001 {
2002 if ( !m_constraintsInvolvedIn )
2003 m_constraintsInvolvedIn = new wxWindowList;
2004 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
2005 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
2006 }
2007
2008 // REMOVE back-pointer to other windows we're involved with.
2009 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
2010 {
2011 if ( m_constraintsInvolvedIn )
2012 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
2013 }
2014
2015 // Reset any constraints that mention this window
2016 void wxWindowBase::DeleteRelatedConstraints()
2017 {
2018 if ( m_constraintsInvolvedIn )
2019 {
2020 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
2021 while (node)
2022 {
2023 wxWindow *win = node->GetData();
2024 wxLayoutConstraints *constr = win->GetConstraints();
2025
2026 // Reset any constraints involving this window
2027 if ( constr )
2028 {
2029 constr->left.ResetIfWin(this);
2030 constr->top.ResetIfWin(this);
2031 constr->right.ResetIfWin(this);
2032 constr->bottom.ResetIfWin(this);
2033 constr->width.ResetIfWin(this);
2034 constr->height.ResetIfWin(this);
2035 constr->centreX.ResetIfWin(this);
2036 constr->centreY.ResetIfWin(this);
2037 }
2038
2039 wxWindowList::compatibility_iterator next = node->GetNext();
2040 m_constraintsInvolvedIn->Erase(node);
2041 node = next;
2042 }
2043
2044 delete m_constraintsInvolvedIn;
2045 m_constraintsInvolvedIn = NULL;
2046 }
2047 }
2048
2049 #endif // wxUSE_CONSTRAINTS
2050
2051 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
2052 {
2053 if ( sizer == m_windowSizer)
2054 return;
2055
2056 if ( m_windowSizer )
2057 {
2058 m_windowSizer->SetContainingWindow(NULL);
2059
2060 if ( deleteOld )
2061 delete m_windowSizer;
2062 }
2063
2064 m_windowSizer = sizer;
2065 if ( m_windowSizer )
2066 {
2067 m_windowSizer->SetContainingWindow((wxWindow *)this);
2068 }
2069
2070 SetAutoLayout(m_windowSizer != NULL);
2071 }
2072
2073 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
2074 {
2075 SetSizer( sizer, deleteOld );
2076 sizer->SetSizeHints( (wxWindow*) this );
2077 }
2078
2079
2080 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
2081 {
2082 // adding a window to a sizer twice is going to result in fatal and
2083 // hard to debug problems later because when deleting the second
2084 // associated wxSizerItem we're going to dereference a dangling
2085 // pointer; so try to detect this as early as possible
2086 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
2087 wxT("Adding a window to the same sizer twice?") );
2088
2089 m_containingSizer = sizer;
2090 }
2091
2092 #if wxUSE_CONSTRAINTS
2093
2094 void wxWindowBase::SatisfyConstraints()
2095 {
2096 wxLayoutConstraints *constr = GetConstraints();
2097 bool wasOk = constr && constr->AreSatisfied();
2098
2099 ResetConstraints(); // Mark all constraints as unevaluated
2100
2101 int noChanges = 1;
2102
2103 // if we're a top level panel (i.e. our parent is frame/dialog), our
2104 // own constraints will never be satisfied any more unless we do it
2105 // here
2106 if ( wasOk )
2107 {
2108 while ( noChanges > 0 )
2109 {
2110 LayoutPhase1(&noChanges);
2111 }
2112 }
2113
2114 LayoutPhase2(&noChanges);
2115 }
2116
2117 #endif // wxUSE_CONSTRAINTS
2118
2119 bool wxWindowBase::Layout()
2120 {
2121 // If there is a sizer, use it instead of the constraints
2122 if ( GetSizer() )
2123 {
2124 int w = 0, h = 0;
2125 GetVirtualSize(&w, &h);
2126 GetSizer()->SetDimension( 0, 0, w, h );
2127 }
2128 #if wxUSE_CONSTRAINTS
2129 else
2130 {
2131 SatisfyConstraints(); // Find the right constraints values
2132 SetConstraintSizes(); // Recursively set the real window sizes
2133 }
2134 #endif
2135
2136 return true;
2137 }
2138
2139 void wxWindowBase::InternalOnSize(wxSizeEvent& event)
2140 {
2141 if ( GetAutoLayout() )
2142 Layout();
2143
2144 event.Skip();
2145 }
2146
2147 #if wxUSE_CONSTRAINTS
2148
2149 // first phase of the constraints evaluation: set our own constraints
2150 bool wxWindowBase::LayoutPhase1(int *noChanges)
2151 {
2152 wxLayoutConstraints *constr = GetConstraints();
2153
2154 return !constr || constr->SatisfyConstraints(this, noChanges);
2155 }
2156
2157 // second phase: set the constraints for our children
2158 bool wxWindowBase::LayoutPhase2(int *noChanges)
2159 {
2160 *noChanges = 0;
2161
2162 // Layout children
2163 DoPhase(1);
2164
2165 // Layout grand children
2166 DoPhase(2);
2167
2168 return true;
2169 }
2170
2171 // Do a phase of evaluating child constraints
2172 bool wxWindowBase::DoPhase(int phase)
2173 {
2174 // the list containing the children for which the constraints are already
2175 // set correctly
2176 wxWindowList succeeded;
2177
2178 // the max number of iterations we loop before concluding that we can't set
2179 // the constraints
2180 static const int maxIterations = 500;
2181
2182 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
2183 {
2184 int noChanges = 0;
2185
2186 // loop over all children setting their constraints
2187 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2188 node;
2189 node = node->GetNext() )
2190 {
2191 wxWindow *child = node->GetData();
2192 if ( child->IsTopLevel() )
2193 {
2194 // top level children are not inside our client area
2195 continue;
2196 }
2197
2198 if ( !child->GetConstraints() || succeeded.Find(child) )
2199 {
2200 // this one is either already ok or nothing we can do about it
2201 continue;
2202 }
2203
2204 int tempNoChanges = 0;
2205 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
2206 : child->LayoutPhase2(&tempNoChanges);
2207 noChanges += tempNoChanges;
2208
2209 if ( success )
2210 {
2211 succeeded.Append(child);
2212 }
2213 }
2214
2215 if ( !noChanges )
2216 {
2217 // constraints are set
2218 break;
2219 }
2220 }
2221
2222 return true;
2223 }
2224
2225 void wxWindowBase::ResetConstraints()
2226 {
2227 wxLayoutConstraints *constr = GetConstraints();
2228 if ( constr )
2229 {
2230 constr->left.SetDone(false);
2231 constr->top.SetDone(false);
2232 constr->right.SetDone(false);
2233 constr->bottom.SetDone(false);
2234 constr->width.SetDone(false);
2235 constr->height.SetDone(false);
2236 constr->centreX.SetDone(false);
2237 constr->centreY.SetDone(false);
2238 }
2239
2240 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2241 while (node)
2242 {
2243 wxWindow *win = node->GetData();
2244 if ( !win->IsTopLevel() )
2245 win->ResetConstraints();
2246 node = node->GetNext();
2247 }
2248 }
2249
2250 // Need to distinguish between setting the 'fake' size for windows and sizers,
2251 // and setting the real values.
2252 void wxWindowBase::SetConstraintSizes(bool recurse)
2253 {
2254 wxLayoutConstraints *constr = GetConstraints();
2255 if ( constr && constr->AreSatisfied() )
2256 {
2257 int x = constr->left.GetValue();
2258 int y = constr->top.GetValue();
2259 int w = constr->width.GetValue();
2260 int h = constr->height.GetValue();
2261
2262 if ( (constr->width.GetRelationship() != wxAsIs ) ||
2263 (constr->height.GetRelationship() != wxAsIs) )
2264 {
2265 SetSize(x, y, w, h);
2266 }
2267 else
2268 {
2269 // If we don't want to resize this window, just move it...
2270 Move(x, y);
2271 }
2272 }
2273 else if ( constr )
2274 {
2275 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2276 GetClassInfo()->GetClassName(),
2277 GetName().c_str());
2278 }
2279
2280 if ( recurse )
2281 {
2282 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2283 while (node)
2284 {
2285 wxWindow *win = node->GetData();
2286 if ( !win->IsTopLevel() && win->GetConstraints() )
2287 win->SetConstraintSizes();
2288 node = node->GetNext();
2289 }
2290 }
2291 }
2292
2293 // Only set the size/position of the constraint (if any)
2294 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
2295 {
2296 wxLayoutConstraints *constr = GetConstraints();
2297 if ( constr )
2298 {
2299 if ( x != wxDefaultCoord )
2300 {
2301 constr->left.SetValue(x);
2302 constr->left.SetDone(true);
2303 }
2304 if ( y != wxDefaultCoord )
2305 {
2306 constr->top.SetValue(y);
2307 constr->top.SetDone(true);
2308 }
2309 if ( w != wxDefaultCoord )
2310 {
2311 constr->width.SetValue(w);
2312 constr->width.SetDone(true);
2313 }
2314 if ( h != wxDefaultCoord )
2315 {
2316 constr->height.SetValue(h);
2317 constr->height.SetDone(true);
2318 }
2319 }
2320 }
2321
2322 void wxWindowBase::MoveConstraint(int x, int y)
2323 {
2324 wxLayoutConstraints *constr = GetConstraints();
2325 if ( constr )
2326 {
2327 if ( x != wxDefaultCoord )
2328 {
2329 constr->left.SetValue(x);
2330 constr->left.SetDone(true);
2331 }
2332 if ( y != wxDefaultCoord )
2333 {
2334 constr->top.SetValue(y);
2335 constr->top.SetDone(true);
2336 }
2337 }
2338 }
2339
2340 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
2341 {
2342 wxLayoutConstraints *constr = GetConstraints();
2343 if ( constr )
2344 {
2345 *w = constr->width.GetValue();
2346 *h = constr->height.GetValue();
2347 }
2348 else
2349 GetSize(w, h);
2350 }
2351
2352 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
2353 {
2354 wxLayoutConstraints *constr = GetConstraints();
2355 if ( constr )
2356 {
2357 *w = constr->width.GetValue();
2358 *h = constr->height.GetValue();
2359 }
2360 else
2361 GetClientSize(w, h);
2362 }
2363
2364 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
2365 {
2366 wxLayoutConstraints *constr = GetConstraints();
2367 if ( constr )
2368 {
2369 *x = constr->left.GetValue();
2370 *y = constr->top.GetValue();
2371 }
2372 else
2373 GetPosition(x, y);
2374 }
2375
2376 #endif // wxUSE_CONSTRAINTS
2377
2378 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
2379 {
2380 // don't do it for the dialogs/frames - they float independently of their
2381 // parent
2382 if ( !IsTopLevel() )
2383 {
2384 wxWindow *parent = GetParent();
2385 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
2386 {
2387 wxPoint pt(parent->GetClientAreaOrigin());
2388 x += pt.x;
2389 y += pt.y;
2390 }
2391 }
2392 }
2393
2394 // ----------------------------------------------------------------------------
2395 // Update UI processing
2396 // ----------------------------------------------------------------------------
2397
2398 void wxWindowBase::UpdateWindowUI(long flags)
2399 {
2400 wxUpdateUIEvent event(GetId());
2401 event.SetEventObject(this);
2402
2403 if ( GetEventHandler()->ProcessEvent(event) )
2404 {
2405 DoUpdateWindowUI(event);
2406 }
2407
2408 if (flags & wxUPDATE_UI_RECURSE)
2409 {
2410 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2411 while (node)
2412 {
2413 wxWindow* child = (wxWindow*) node->GetData();
2414 child->UpdateWindowUI(flags);
2415 node = node->GetNext();
2416 }
2417 }
2418 }
2419
2420 // do the window-specific processing after processing the update event
2421 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2422 {
2423 if ( event.GetSetEnabled() )
2424 Enable(event.GetEnabled());
2425
2426 if ( event.GetSetShown() )
2427 Show(event.GetShown());
2428 }
2429
2430 // ----------------------------------------------------------------------------
2431 // dialog units translations
2432 // ----------------------------------------------------------------------------
2433
2434 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2435 {
2436 int charWidth = GetCharWidth();
2437 int charHeight = GetCharHeight();
2438 wxPoint pt2 = wxDefaultPosition;
2439 if (pt.x != wxDefaultCoord)
2440 pt2.x = (int) ((pt.x * 4) / charWidth);
2441 if (pt.y != wxDefaultCoord)
2442 pt2.y = (int) ((pt.y * 8) / charHeight);
2443
2444 return pt2;
2445 }
2446
2447 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2448 {
2449 int charWidth = GetCharWidth();
2450 int charHeight = GetCharHeight();
2451 wxPoint pt2 = wxDefaultPosition;
2452 if (pt.x != wxDefaultCoord)
2453 pt2.x = (int) ((pt.x * charWidth) / 4);
2454 if (pt.y != wxDefaultCoord)
2455 pt2.y = (int) ((pt.y * charHeight) / 8);
2456
2457 return pt2;
2458 }
2459
2460 // ----------------------------------------------------------------------------
2461 // event handlers
2462 // ----------------------------------------------------------------------------
2463
2464 // propagate the colour change event to the subwindows
2465 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2466 {
2467 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2468 while ( node )
2469 {
2470 // Only propagate to non-top-level windows
2471 wxWindow *win = node->GetData();
2472 if ( !win->IsTopLevel() )
2473 {
2474 wxSysColourChangedEvent event2;
2475 event.SetEventObject(win);
2476 win->GetEventHandler()->ProcessEvent(event2);
2477 }
2478
2479 node = node->GetNext();
2480 }
2481
2482 Refresh();
2483 }
2484
2485 // the default action is to populate dialog with data when it's created,
2486 // and nudge the UI into displaying itself correctly in case
2487 // we've turned the wxUpdateUIEvents frequency down low.
2488 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2489 {
2490 TransferDataToWindow();
2491
2492 // Update the UI at this point
2493 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2494 }
2495
2496 // ----------------------------------------------------------------------------
2497 // menu-related functions
2498 // ----------------------------------------------------------------------------
2499
2500 #if wxUSE_MENUS
2501
2502 bool wxWindowBase::PopupMenu(wxMenu *menu, int x, int y)
2503 {
2504 wxCHECK_MSG( menu, false, "can't popup NULL menu" );
2505
2506 wxCurrentPopupMenu = menu;
2507 const bool rc = DoPopupMenu(menu, x, y);
2508 wxCurrentPopupMenu = NULL;
2509
2510 return rc;
2511 }
2512
2513 // this is used to pass the id of the selected item from the menu event handler
2514 // to the main function itself
2515 //
2516 // it's ok to use a global here as there can be at most one popup menu shown at
2517 // any time
2518 static int gs_popupMenuSelection = wxID_NONE;
2519
2520 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event)
2521 {
2522 // store the id in a global variable where we'll retrieve it from later
2523 gs_popupMenuSelection = event.GetId();
2524 }
2525
2526 void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent& WXUNUSED(event))
2527 {
2528 // nothing to do but do not skip it
2529 }
2530
2531 int
2532 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y)
2533 {
2534 gs_popupMenuSelection = wxID_NONE;
2535
2536 Connect(wxEVT_COMMAND_MENU_SELECTED,
2537 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2538 NULL,
2539 this);
2540
2541 // it is common to construct the menu passed to this function dynamically
2542 // using some fixed range of ids which could clash with the ids used
2543 // elsewhere in the program, which could result in some menu items being
2544 // unintentionally disabled or otherwise modified by update UI handlers
2545 // elsewhere in the program code and this is difficult to avoid in the
2546 // program itself, so instead we just temporarily suspend UI updating while
2547 // this menu is shown
2548 Connect(wxEVT_UPDATE_UI,
2549 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2550 NULL,
2551 this);
2552
2553 PopupMenu(&menu, x, y);
2554
2555 Disconnect(wxEVT_UPDATE_UI,
2556 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2557 NULL,
2558 this);
2559 Disconnect(wxEVT_COMMAND_MENU_SELECTED,
2560 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2561 NULL,
2562 this);
2563
2564 return gs_popupMenuSelection;
2565 }
2566
2567 #endif // wxUSE_MENUS
2568
2569 // methods for drawing the sizers in a visible way: this is currently only
2570 // enabled for "full debug" builds with wxDEBUG_LEVEL==2 as it doesn't work
2571 // that well and also because we don't want to leave it enabled in default
2572 // builds used for production
2573 #if wxDEBUG_LEVEL > 1
2574
2575 static void DrawSizers(wxWindowBase *win);
2576
2577 static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill, const wxPen* pen)
2578 {
2579 wxClientDC dc((wxWindow *)win);
2580 dc.SetPen(*pen);
2581 dc.SetBrush(fill ? wxBrush(pen->GetColour(), wxBRUSHSTYLE_CROSSDIAG_HATCH) :
2582 *wxTRANSPARENT_BRUSH);
2583 dc.DrawRectangle(rect.Deflate(1, 1));
2584 }
2585
2586 static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
2587 {
2588 const wxSizerItemList& items = sizer->GetChildren();
2589 for ( wxSizerItemList::const_iterator i = items.begin(),
2590 end = items.end();
2591 i != end;
2592 ++i )
2593 {
2594 wxSizerItem *item = *i;
2595 if ( item->IsSizer() )
2596 {
2597 DrawBorder(win, item->GetRect().Deflate(2), false, wxRED_PEN);
2598 DrawSizer(win, item->GetSizer());
2599 }
2600 else if ( item->IsSpacer() )
2601 {
2602 DrawBorder(win, item->GetRect().Deflate(2), true, wxBLUE_PEN);
2603 }
2604 else if ( item->IsWindow() )
2605 {
2606 DrawSizers(item->GetWindow());
2607 }
2608 else
2609 wxFAIL_MSG("inconsistent wxSizerItem status!");
2610 }
2611 }
2612
2613 static void DrawSizers(wxWindowBase *win)
2614 {
2615 DrawBorder(win, win->GetClientSize(), false, wxGREEN_PEN);
2616
2617 wxSizer *sizer = win->GetSizer();
2618 if ( sizer )
2619 {
2620 DrawSizer(win, sizer);
2621 }
2622 else // no sizer, still recurse into the children
2623 {
2624 const wxWindowList& children = win->GetChildren();
2625 for ( wxWindowList::const_iterator i = children.begin(),
2626 end = children.end();
2627 i != end;
2628 ++i )
2629 {
2630 DrawSizers(*i);
2631 }
2632
2633 // show all kind of sizes of this window; see the "window sizing" topic
2634 // overview for more info about the various differences:
2635 wxSize fullSz = win->GetSize();
2636 wxSize clientSz = win->GetClientSize();
2637 wxSize bestSz = win->GetBestSize();
2638 wxSize minSz = win->GetMinSize();
2639 wxSize maxSz = win->GetMaxSize();
2640 wxSize virtualSz = win->GetVirtualSize();
2641
2642 wxMessageOutputDebug dbgout;
2643 dbgout.Printf(
2644 "%-10s => fullsz=%4d;%-4d clientsz=%4d;%-4d bestsz=%4d;%-4d minsz=%4d;%-4d maxsz=%4d;%-4d virtualsz=%4d;%-4d\n",
2645 win->GetName(),
2646 fullSz.x, fullSz.y,
2647 clientSz.x, clientSz.y,
2648 bestSz.x, bestSz.y,
2649 minSz.x, minSz.y,
2650 maxSz.x, maxSz.y,
2651 virtualSz.x, virtualSz.y);
2652 }
2653 }
2654
2655 #endif // wxDEBUG_LEVEL
2656
2657 // process special middle clicks
2658 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2659 {
2660 if ( event.ControlDown() && event.AltDown() )
2661 {
2662 #if wxDEBUG_LEVEL > 1
2663 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2664 if ( event.ShiftDown() )
2665 {
2666 DrawSizers(this);
2667 }
2668 else
2669 #endif // __WXDEBUG__
2670 {
2671 // just Ctrl-Alt-middle click shows information about wx version
2672 ::wxInfoMessageBox((wxWindow*)this);
2673 }
2674 }
2675 else
2676 {
2677 event.Skip();
2678 }
2679 }
2680
2681 // ----------------------------------------------------------------------------
2682 // accessibility
2683 // ----------------------------------------------------------------------------
2684
2685 #if wxUSE_ACCESSIBILITY
2686 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2687 {
2688 if (m_accessible && (accessible != m_accessible))
2689 delete m_accessible;
2690 m_accessible = accessible;
2691 if (m_accessible)
2692 m_accessible->SetWindow((wxWindow*) this);
2693 }
2694
2695 // Returns the accessible object, creating if necessary.
2696 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2697 {
2698 if (!m_accessible)
2699 m_accessible = CreateAccessible();
2700 return m_accessible;
2701 }
2702
2703 // Override to create a specific accessible object.
2704 wxAccessible* wxWindowBase::CreateAccessible()
2705 {
2706 return new wxWindowAccessible((wxWindow*) this);
2707 }
2708
2709 #endif
2710
2711 // ----------------------------------------------------------------------------
2712 // list classes implementation
2713 // ----------------------------------------------------------------------------
2714
2715 #if wxUSE_STL
2716
2717 #include "wx/listimpl.cpp"
2718 WX_DEFINE_LIST(wxWindowList)
2719
2720 #else // !wxUSE_STL
2721
2722 void wxWindowListNode::DeleteData()
2723 {
2724 delete (wxWindow *)GetData();
2725 }
2726
2727 #endif // wxUSE_STL/!wxUSE_STL
2728
2729 // ----------------------------------------------------------------------------
2730 // borders
2731 // ----------------------------------------------------------------------------
2732
2733 wxBorder wxWindowBase::GetBorder(long flags) const
2734 {
2735 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2736 if ( border == wxBORDER_DEFAULT )
2737 {
2738 border = GetDefaultBorder();
2739 }
2740 else if ( border == wxBORDER_THEME )
2741 {
2742 border = GetDefaultBorderForControl();
2743 }
2744
2745 return border;
2746 }
2747
2748 wxBorder wxWindowBase::GetDefaultBorder() const
2749 {
2750 return wxBORDER_NONE;
2751 }
2752
2753 // ----------------------------------------------------------------------------
2754 // hit testing
2755 // ----------------------------------------------------------------------------
2756
2757 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2758 {
2759 // here we just check if the point is inside the window or not
2760
2761 // check the top and left border first
2762 bool outside = x < 0 || y < 0;
2763 if ( !outside )
2764 {
2765 // check the right and bottom borders too
2766 wxSize size = GetSize();
2767 outside = x >= size.x || y >= size.y;
2768 }
2769
2770 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2771 }
2772
2773 // ----------------------------------------------------------------------------
2774 // mouse capture
2775 // ----------------------------------------------------------------------------
2776
2777 struct WXDLLEXPORT wxWindowNext
2778 {
2779 wxWindow *win;
2780 wxWindowNext *next;
2781 } *wxWindowBase::ms_winCaptureNext = NULL;
2782 wxWindow *wxWindowBase::ms_winCaptureCurrent = NULL;
2783 bool wxWindowBase::ms_winCaptureChanging = false;
2784
2785 void wxWindowBase::CaptureMouse()
2786 {
2787 wxLogTrace(wxT("mousecapture"), wxT("CaptureMouse(%p)"), static_cast<void*>(this));
2788
2789 wxASSERT_MSG( !ms_winCaptureChanging, wxT("recursive CaptureMouse call?") );
2790
2791 ms_winCaptureChanging = true;
2792
2793 wxWindow *winOld = GetCapture();
2794 if ( winOld )
2795 {
2796 ((wxWindowBase*) winOld)->DoReleaseMouse();
2797
2798 // save it on stack
2799 wxWindowNext *item = new wxWindowNext;
2800 item->win = winOld;
2801 item->next = ms_winCaptureNext;
2802 ms_winCaptureNext = item;
2803 }
2804 //else: no mouse capture to save
2805
2806 DoCaptureMouse();
2807 ms_winCaptureCurrent = (wxWindow*)this;
2808
2809 ms_winCaptureChanging = false;
2810 }
2811
2812 void wxWindowBase::ReleaseMouse()
2813 {
2814 wxLogTrace(wxT("mousecapture"), wxT("ReleaseMouse(%p)"), static_cast<void*>(this));
2815
2816 wxASSERT_MSG( !ms_winCaptureChanging, wxT("recursive ReleaseMouse call?") );
2817
2818 wxASSERT_MSG( GetCapture() == this,
2819 "attempt to release mouse, but this window hasn't captured it" );
2820 wxASSERT_MSG( ms_winCaptureCurrent == this,
2821 "attempt to release mouse, but this window hasn't captured it" );
2822
2823 ms_winCaptureChanging = true;
2824
2825 DoReleaseMouse();
2826 ms_winCaptureCurrent = NULL;
2827
2828 if ( ms_winCaptureNext )
2829 {
2830 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2831 ms_winCaptureCurrent = ms_winCaptureNext->win;
2832
2833 wxWindowNext *item = ms_winCaptureNext;
2834 ms_winCaptureNext = item->next;
2835 delete item;
2836 }
2837 //else: stack is empty, no previous capture
2838
2839 ms_winCaptureChanging = false;
2840
2841 wxLogTrace(wxT("mousecapture"),
2842 (const wxChar *) wxT("After ReleaseMouse() mouse is captured by %p"),
2843 static_cast<void*>(GetCapture()));
2844 }
2845
2846 static void DoNotifyWindowAboutCaptureLost(wxWindow *win)
2847 {
2848 wxMouseCaptureLostEvent event(win->GetId());
2849 event.SetEventObject(win);
2850 if ( !win->GetEventHandler()->ProcessEvent(event) )
2851 {
2852 // windows must handle this event, otherwise the app wouldn't behave
2853 // correctly if it loses capture unexpectedly; see the discussion here:
2854 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2855 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
2856 wxFAIL_MSG( wxT("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2857 }
2858 }
2859
2860 /* static */
2861 void wxWindowBase::NotifyCaptureLost()
2862 {
2863 // don't do anything if capture lost was expected, i.e. resulted from
2864 // a wx call to ReleaseMouse or CaptureMouse:
2865 if ( ms_winCaptureChanging )
2866 return;
2867
2868 // if the capture was lost unexpectedly, notify every window that has
2869 // capture (on stack or current) about it and clear the stack:
2870
2871 if ( ms_winCaptureCurrent )
2872 {
2873 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent);
2874 ms_winCaptureCurrent = NULL;
2875 }
2876
2877 while ( ms_winCaptureNext )
2878 {
2879 wxWindowNext *item = ms_winCaptureNext;
2880 ms_winCaptureNext = item->next;
2881
2882 DoNotifyWindowAboutCaptureLost(item->win);
2883
2884 delete item;
2885 }
2886 }
2887
2888 #if wxUSE_HOTKEY
2889
2890 bool
2891 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2892 int WXUNUSED(modifiers),
2893 int WXUNUSED(keycode))
2894 {
2895 // not implemented
2896 return false;
2897 }
2898
2899 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2900 {
2901 // not implemented
2902 return false;
2903 }
2904
2905 #endif // wxUSE_HOTKEY
2906
2907 // ----------------------------------------------------------------------------
2908 // event processing
2909 // ----------------------------------------------------------------------------
2910
2911 bool wxWindowBase::TryBefore(wxEvent& event)
2912 {
2913 #if wxUSE_VALIDATORS
2914 // Can only use the validator of the window which
2915 // is receiving the event
2916 if ( event.GetEventObject() == this )
2917 {
2918 wxValidator * const validator = GetValidator();
2919 if ( validator && validator->ProcessEventHere(event) )
2920 {
2921 return true;
2922 }
2923 }
2924 #endif // wxUSE_VALIDATORS
2925
2926 return wxEvtHandler::TryBefore(event);
2927 }
2928
2929 bool wxWindowBase::TryAfter(wxEvent& event)
2930 {
2931 // carry on up the parent-child hierarchy if the propagation count hasn't
2932 // reached zero yet
2933 if ( event.ShouldPropagate() )
2934 {
2935 // honour the requests to stop propagation at this window: this is
2936 // used by the dialogs, for example, to prevent processing the events
2937 // from the dialog controls in the parent frame which rarely, if ever,
2938 // makes sense
2939 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2940 {
2941 wxWindow *parent = GetParent();
2942 if ( parent && !parent->IsBeingDeleted() )
2943 {
2944 wxPropagateOnce propagateOnce(event);
2945
2946 return parent->GetEventHandler()->ProcessEvent(event);
2947 }
2948 }
2949 }
2950
2951 return wxEvtHandler::TryAfter(event);
2952 }
2953
2954 // ----------------------------------------------------------------------------
2955 // window relationships
2956 // ----------------------------------------------------------------------------
2957
2958 wxWindow *wxWindowBase::DoGetSibling(WindowOrder order) const
2959 {
2960 wxCHECK_MSG( GetParent(), NULL,
2961 wxT("GetPrev/NextSibling() don't work for TLWs!") );
2962
2963 wxWindowList& siblings = GetParent()->GetChildren();
2964 wxWindowList::compatibility_iterator i = siblings.Find((wxWindow *)this);
2965 wxCHECK_MSG( i, NULL, wxT("window not a child of its parent?") );
2966
2967 if ( order == OrderBefore )
2968 i = i->GetPrevious();
2969 else // OrderAfter
2970 i = i->GetNext();
2971
2972 return i ? i->GetData() : NULL;
2973 }
2974
2975 // ----------------------------------------------------------------------------
2976 // keyboard navigation
2977 // ----------------------------------------------------------------------------
2978
2979 // Navigates in the specified direction inside this window
2980 bool wxWindowBase::DoNavigateIn(int flags)
2981 {
2982 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2983 // native code doesn't process our wxNavigationKeyEvents anyhow
2984 wxUnusedVar(flags);
2985 return false;
2986 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
2987 wxNavigationKeyEvent eventNav;
2988 wxWindow *focused = FindFocus();
2989 eventNav.SetCurrentFocus(focused);
2990 eventNav.SetEventObject(focused);
2991 eventNav.SetFlags(flags);
2992 return GetEventHandler()->ProcessEvent(eventNav);
2993 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
2994 }
2995
2996 bool wxWindowBase::HandleAsNavigationKey(const wxKeyEvent& event)
2997 {
2998 if ( event.GetKeyCode() != WXK_TAB )
2999 return false;
3000
3001 int flags = wxNavigationKeyEvent::FromTab;
3002
3003 if ( event.ShiftDown() )
3004 flags |= wxNavigationKeyEvent::IsBackward;
3005 else
3006 flags |= wxNavigationKeyEvent::IsForward;
3007
3008 if ( event.ControlDown() )
3009 flags |= wxNavigationKeyEvent::WinChange;
3010
3011 Navigate(flags);
3012 return true;
3013 }
3014
3015 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, WindowOrder move)
3016 {
3017 // check that we're not a top level window
3018 wxCHECK_RET( GetParent(),
3019 wxT("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
3020
3021 // detect the special case when we have nothing to do anyhow and when the
3022 // code below wouldn't work
3023 if ( win == this )
3024 return;
3025
3026 // find the target window in the siblings list
3027 wxWindowList& siblings = GetParent()->GetChildren();
3028 wxWindowList::compatibility_iterator i = siblings.Find(win);
3029 wxCHECK_RET( i, wxT("MoveBefore/AfterInTabOrder(): win is not a sibling") );
3030
3031 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
3032 // can't just move the node around
3033 wxWindow *self = (wxWindow *)this;
3034 siblings.DeleteObject(self);
3035 if ( move == OrderAfter )
3036 {
3037 i = i->GetNext();
3038 }
3039
3040 if ( i )
3041 {
3042 siblings.Insert(i, self);
3043 }
3044 else // OrderAfter and win was the last sibling
3045 {
3046 siblings.Append(self);
3047 }
3048 }
3049
3050 // ----------------------------------------------------------------------------
3051 // focus handling
3052 // ----------------------------------------------------------------------------
3053
3054 /*static*/ wxWindow* wxWindowBase::FindFocus()
3055 {
3056 wxWindowBase *win = DoFindFocus();
3057 return win ? win->GetMainWindowOfCompositeControl() : NULL;
3058 }
3059
3060 bool wxWindowBase::HasFocus() const
3061 {
3062 wxWindowBase *win = DoFindFocus();
3063 return win == this ||
3064 win == wxConstCast(this, wxWindowBase)->GetMainWindowOfCompositeControl();
3065 }
3066
3067 // ----------------------------------------------------------------------------
3068 // drag and drop
3069 // ----------------------------------------------------------------------------
3070
3071 #if wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3072
3073 namespace
3074 {
3075
3076 class DragAcceptFilesTarget : public wxFileDropTarget
3077 {
3078 public:
3079 DragAcceptFilesTarget(wxWindowBase *win) : m_win(win) {}
3080
3081 virtual bool OnDropFiles(wxCoord x, wxCoord y,
3082 const wxArrayString& filenames)
3083 {
3084 wxDropFilesEvent event(wxEVT_DROP_FILES,
3085 filenames.size(),
3086 wxCArrayString(filenames).Release());
3087 event.SetEventObject(m_win);
3088 event.m_pos.x = x;
3089 event.m_pos.y = y;
3090
3091 return m_win->HandleWindowEvent(event);
3092 }
3093
3094 private:
3095 wxWindowBase * const m_win;
3096
3097 wxDECLARE_NO_COPY_CLASS(DragAcceptFilesTarget);
3098 };
3099
3100
3101 } // anonymous namespace
3102
3103 // Generic version of DragAcceptFiles(). It works by installing a simple
3104 // wxFileDropTarget-to-EVT_DROP_FILES adaptor and therefore cannot be used
3105 // together with explicit SetDropTarget() calls.
3106 void wxWindowBase::DragAcceptFiles(bool accept)
3107 {
3108 if ( accept )
3109 {
3110 wxASSERT_MSG( !GetDropTarget(),
3111 "cannot use DragAcceptFiles() and SetDropTarget() together" );
3112 SetDropTarget(new DragAcceptFilesTarget(this));
3113 }
3114 else
3115 {
3116 SetDropTarget(NULL);
3117 }
3118 }
3119
3120 #endif // wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3121
3122 // ----------------------------------------------------------------------------
3123 // global functions
3124 // ----------------------------------------------------------------------------
3125
3126 wxWindow* wxGetTopLevelParent(wxWindow *win)
3127 {
3128 while ( win && !win->IsTopLevel() )
3129 win = win->GetParent();
3130
3131 return win;
3132 }
3133
3134 #if wxUSE_ACCESSIBILITY
3135 // ----------------------------------------------------------------------------
3136 // accessible object for windows
3137 // ----------------------------------------------------------------------------
3138
3139 // Can return either a child object, or an integer
3140 // representing the child element, starting from 1.
3141 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
3142 {
3143 wxASSERT( GetWindow() != NULL );
3144 if (!GetWindow())
3145 return wxACC_FAIL;
3146
3147 return wxACC_NOT_IMPLEMENTED;
3148 }
3149
3150 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
3151 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
3152 {
3153 wxASSERT( GetWindow() != NULL );
3154 if (!GetWindow())
3155 return wxACC_FAIL;
3156
3157 wxWindow* win = NULL;
3158 if (elementId == 0)
3159 {
3160 win = GetWindow();
3161 }
3162 else
3163 {
3164 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
3165 {
3166 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
3167 }
3168 else
3169 return wxACC_FAIL;
3170 }
3171 if (win)
3172 {
3173 rect = win->GetRect();
3174 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
3175 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
3176 return wxACC_OK;
3177 }
3178
3179 return wxACC_NOT_IMPLEMENTED;
3180 }
3181
3182 // Navigates from fromId to toId/toObject.
3183 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
3184 int* WXUNUSED(toId), wxAccessible** toObject)
3185 {
3186 wxASSERT( GetWindow() != NULL );
3187 if (!GetWindow())
3188 return wxACC_FAIL;
3189
3190 switch (navDir)
3191 {
3192 case wxNAVDIR_FIRSTCHILD:
3193 {
3194 if (GetWindow()->GetChildren().GetCount() == 0)
3195 return wxACC_FALSE;
3196 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
3197 *toObject = childWindow->GetOrCreateAccessible();
3198
3199 return wxACC_OK;
3200 }
3201 case wxNAVDIR_LASTCHILD:
3202 {
3203 if (GetWindow()->GetChildren().GetCount() == 0)
3204 return wxACC_FALSE;
3205 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
3206 *toObject = childWindow->GetOrCreateAccessible();
3207
3208 return wxACC_OK;
3209 }
3210 case wxNAVDIR_RIGHT:
3211 case wxNAVDIR_DOWN:
3212 case wxNAVDIR_NEXT:
3213 {
3214 wxWindowList::compatibility_iterator node =
3215 wxWindowList::compatibility_iterator();
3216 if (fromId == 0)
3217 {
3218 // Can't navigate to sibling of this window
3219 // if we're a top-level window.
3220 if (!GetWindow()->GetParent())
3221 return wxACC_NOT_IMPLEMENTED;
3222
3223 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3224 }
3225 else
3226 {
3227 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
3228 node = GetWindow()->GetChildren().Item(fromId-1);
3229 }
3230
3231 if (node && node->GetNext())
3232 {
3233 wxWindow* nextWindow = node->GetNext()->GetData();
3234 *toObject = nextWindow->GetOrCreateAccessible();
3235 return wxACC_OK;
3236 }
3237 else
3238 return wxACC_FALSE;
3239 }
3240 case wxNAVDIR_LEFT:
3241 case wxNAVDIR_UP:
3242 case wxNAVDIR_PREVIOUS:
3243 {
3244 wxWindowList::compatibility_iterator node =
3245 wxWindowList::compatibility_iterator();
3246 if (fromId == 0)
3247 {
3248 // Can't navigate to sibling of this window
3249 // if we're a top-level window.
3250 if (!GetWindow()->GetParent())
3251 return wxACC_NOT_IMPLEMENTED;
3252
3253 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3254 }
3255 else
3256 {
3257 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
3258 node = GetWindow()->GetChildren().Item(fromId-1);
3259 }
3260
3261 if (node && node->GetPrevious())
3262 {
3263 wxWindow* previousWindow = node->GetPrevious()->GetData();
3264 *toObject = previousWindow->GetOrCreateAccessible();
3265 return wxACC_OK;
3266 }
3267 else
3268 return wxACC_FALSE;
3269 }
3270 }
3271
3272 return wxACC_NOT_IMPLEMENTED;
3273 }
3274
3275 // Gets the name of the specified object.
3276 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
3277 {
3278 wxASSERT( GetWindow() != NULL );
3279 if (!GetWindow())
3280 return wxACC_FAIL;
3281
3282 wxString title;
3283
3284 // If a child, leave wxWidgets to call the function on the actual
3285 // child object.
3286 if (childId > 0)
3287 return wxACC_NOT_IMPLEMENTED;
3288
3289 // This will eventually be replaced by specialised
3290 // accessible classes, one for each kind of wxWidgets
3291 // control or window.
3292 #if wxUSE_BUTTON
3293 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
3294 title = ((wxButton*) GetWindow())->GetLabel();
3295 else
3296 #endif
3297 title = GetWindow()->GetName();
3298
3299 if (!title.empty())
3300 {
3301 *name = title;
3302 return wxACC_OK;
3303 }
3304 else
3305 return wxACC_NOT_IMPLEMENTED;
3306 }
3307
3308 // Gets the number of children.
3309 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
3310 {
3311 wxASSERT( GetWindow() != NULL );
3312 if (!GetWindow())
3313 return wxACC_FAIL;
3314
3315 *childId = (int) GetWindow()->GetChildren().GetCount();
3316 return wxACC_OK;
3317 }
3318
3319 // Gets the specified child (starting from 1).
3320 // If *child is NULL and return value is wxACC_OK,
3321 // this means that the child is a simple element and
3322 // not an accessible object.
3323 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
3324 {
3325 wxASSERT( GetWindow() != NULL );
3326 if (!GetWindow())
3327 return wxACC_FAIL;
3328
3329 if (childId == 0)
3330 {
3331 *child = this;
3332 return wxACC_OK;
3333 }
3334
3335 if (childId > (int) GetWindow()->GetChildren().GetCount())
3336 return wxACC_FAIL;
3337
3338 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
3339 *child = childWindow->GetOrCreateAccessible();
3340 if (*child)
3341 return wxACC_OK;
3342 else
3343 return wxACC_FAIL;
3344 }
3345
3346 // Gets the parent, or NULL.
3347 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
3348 {
3349 wxASSERT( GetWindow() != NULL );
3350 if (!GetWindow())
3351 return wxACC_FAIL;
3352
3353 wxWindow* parentWindow = GetWindow()->GetParent();
3354 if (!parentWindow)
3355 {
3356 *parent = NULL;
3357 return wxACC_OK;
3358 }
3359 else
3360 {
3361 *parent = parentWindow->GetOrCreateAccessible();
3362 if (*parent)
3363 return wxACC_OK;
3364 else
3365 return wxACC_FAIL;
3366 }
3367 }
3368
3369 // Performs the default action. childId is 0 (the action for this object)
3370 // or > 0 (the action for a child).
3371 // Return wxACC_NOT_SUPPORTED if there is no default action for this
3372 // window (e.g. an edit control).
3373 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
3374 {
3375 wxASSERT( GetWindow() != NULL );
3376 if (!GetWindow())
3377 return wxACC_FAIL;
3378
3379 return wxACC_NOT_IMPLEMENTED;
3380 }
3381
3382 // Gets the default action for this object (0) or > 0 (the action for a child).
3383 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
3384 // string if there is no action.
3385 // The retrieved string describes the action that is performed on an object,
3386 // not what the object does as a result. For example, a toolbar button that prints
3387 // a document has a default action of "Press" rather than "Prints the current document."
3388 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
3389 {
3390 wxASSERT( GetWindow() != NULL );
3391 if (!GetWindow())
3392 return wxACC_FAIL;
3393
3394 return wxACC_NOT_IMPLEMENTED;
3395 }
3396
3397 // Returns the description for this object or a child.
3398 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
3399 {
3400 wxASSERT( GetWindow() != NULL );
3401 if (!GetWindow())
3402 return wxACC_FAIL;
3403
3404 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3405 if (!ht.empty())
3406 {
3407 *description = ht;
3408 return wxACC_OK;
3409 }
3410 return wxACC_NOT_IMPLEMENTED;
3411 }
3412
3413 // Returns help text for this object or a child, similar to tooltip text.
3414 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
3415 {
3416 wxASSERT( GetWindow() != NULL );
3417 if (!GetWindow())
3418 return wxACC_FAIL;
3419
3420 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3421 if (!ht.empty())
3422 {
3423 *helpText = ht;
3424 return wxACC_OK;
3425 }
3426 return wxACC_NOT_IMPLEMENTED;
3427 }
3428
3429 // Returns the keyboard shortcut for this object or child.
3430 // Return e.g. ALT+K
3431 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
3432 {
3433 wxASSERT( GetWindow() != NULL );
3434 if (!GetWindow())
3435 return wxACC_FAIL;
3436
3437 return wxACC_NOT_IMPLEMENTED;
3438 }
3439
3440 // Returns a role constant.
3441 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
3442 {
3443 wxASSERT( GetWindow() != NULL );
3444 if (!GetWindow())
3445 return wxACC_FAIL;
3446
3447 // If a child, leave wxWidgets to call the function on the actual
3448 // child object.
3449 if (childId > 0)
3450 return wxACC_NOT_IMPLEMENTED;
3451
3452 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3453 return wxACC_NOT_IMPLEMENTED;
3454 #if wxUSE_STATUSBAR
3455 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3456 return wxACC_NOT_IMPLEMENTED;
3457 #endif
3458 #if wxUSE_TOOLBAR
3459 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3460 return wxACC_NOT_IMPLEMENTED;
3461 #endif
3462
3463 //*role = wxROLE_SYSTEM_CLIENT;
3464 *role = wxROLE_SYSTEM_CLIENT;
3465 return wxACC_OK;
3466
3467 #if 0
3468 return wxACC_NOT_IMPLEMENTED;
3469 #endif
3470 }
3471
3472 // Returns a state constant.
3473 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
3474 {
3475 wxASSERT( GetWindow() != NULL );
3476 if (!GetWindow())
3477 return wxACC_FAIL;
3478
3479 // If a child, leave wxWidgets to call the function on the actual
3480 // child object.
3481 if (childId > 0)
3482 return wxACC_NOT_IMPLEMENTED;
3483
3484 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3485 return wxACC_NOT_IMPLEMENTED;
3486
3487 #if wxUSE_STATUSBAR
3488 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3489 return wxACC_NOT_IMPLEMENTED;
3490 #endif
3491 #if wxUSE_TOOLBAR
3492 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3493 return wxACC_NOT_IMPLEMENTED;
3494 #endif
3495
3496 *state = 0;
3497 return wxACC_OK;
3498
3499 #if 0
3500 return wxACC_NOT_IMPLEMENTED;
3501 #endif
3502 }
3503
3504 // Returns a localized string representing the value for the object
3505 // or child.
3506 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
3507 {
3508 wxASSERT( GetWindow() != NULL );
3509 if (!GetWindow())
3510 return wxACC_FAIL;
3511
3512 return wxACC_NOT_IMPLEMENTED;
3513 }
3514
3515 // Selects the object or child.
3516 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
3517 {
3518 wxASSERT( GetWindow() != NULL );
3519 if (!GetWindow())
3520 return wxACC_FAIL;
3521
3522 return wxACC_NOT_IMPLEMENTED;
3523 }
3524
3525 // Gets the window with the keyboard focus.
3526 // If childId is 0 and child is NULL, no object in
3527 // this subhierarchy has the focus.
3528 // If this object has the focus, child should be 'this'.
3529 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
3530 {
3531 wxASSERT( GetWindow() != NULL );
3532 if (!GetWindow())
3533 return wxACC_FAIL;
3534
3535 return wxACC_NOT_IMPLEMENTED;
3536 }
3537
3538 #if wxUSE_VARIANT
3539 // Gets a variant representing the selected children
3540 // of this object.
3541 // Acceptable values:
3542 // - a null variant (IsNull() returns true)
3543 // - a list variant (GetType() == wxT("list")
3544 // - an integer representing the selected child element,
3545 // or 0 if this object is selected (GetType() == wxT("long")
3546 // - a "void*" pointer to a wxAccessible child object
3547 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
3548 {
3549 wxASSERT( GetWindow() != NULL );
3550 if (!GetWindow())
3551 return wxACC_FAIL;
3552
3553 return wxACC_NOT_IMPLEMENTED;
3554 }
3555 #endif // wxUSE_VARIANT
3556
3557 #endif // wxUSE_ACCESSIBILITY
3558
3559 // ----------------------------------------------------------------------------
3560 // RTL support
3561 // ----------------------------------------------------------------------------
3562
3563 wxCoord
3564 wxWindowBase::AdjustForLayoutDirection(wxCoord x,
3565 wxCoord width,
3566 wxCoord widthTotal) const
3567 {
3568 if ( GetLayoutDirection() == wxLayout_RightToLeft )
3569 {
3570 x = widthTotal - x - width;
3571 }
3572
3573 return x;
3574 }
3575
3576