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