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