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