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