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