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