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