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