toplevel window is shown on screen even if its parent is hidden
[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 // A window is shown on screen if it itself is shown and so are all its
885 // parents. But if a window is toplevel one, then its always visible on
886 // screen if IsShown() returns true, even if it has a hidden parent.
887 return IsShown() &&
888 (IsTopLevel() || GetParent() == NULL || GetParent()->IsShownOnScreen());
889 }
890
891 // ----------------------------------------------------------------------------
892 // RTTI
893 // ----------------------------------------------------------------------------
894
895 bool wxWindowBase::IsTopLevel() const
896 {
897 return false;
898 }
899
900 // ----------------------------------------------------------------------------
901 // reparenting the window
902 // ----------------------------------------------------------------------------
903
904 void wxWindowBase::AddChild(wxWindowBase *child)
905 {
906 wxCHECK_RET( child, wxT("can't add a NULL child") );
907
908 // this should never happen and it will lead to a crash later if it does
909 // because RemoveChild() will remove only one node from the children list
910 // and the other(s) one(s) will be left with dangling pointers in them
911 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") );
912
913 GetChildren().Append((wxWindow*)child);
914 child->SetParent(this);
915 }
916
917 void wxWindowBase::RemoveChild(wxWindowBase *child)
918 {
919 wxCHECK_RET( child, wxT("can't remove a NULL child") );
920
921 GetChildren().DeleteObject((wxWindow *)child);
922 child->SetParent(NULL);
923 }
924
925 bool wxWindowBase::Reparent(wxWindowBase *newParent)
926 {
927 wxWindow *oldParent = GetParent();
928 if ( newParent == oldParent )
929 {
930 // nothing done
931 return false;
932 }
933
934 const bool oldEnabledState = IsEnabled();
935
936 // unlink this window from the existing parent.
937 if ( oldParent )
938 {
939 oldParent->RemoveChild(this);
940 }
941 else
942 {
943 wxTopLevelWindows.DeleteObject((wxWindow *)this);
944 }
945
946 // add it to the new one
947 if ( newParent )
948 {
949 newParent->AddChild(this);
950 }
951 else
952 {
953 wxTopLevelWindows.Append((wxWindow *)this);
954 }
955
956 // We need to notify window (and its subwindows) if by changing the parent
957 // we also change our enabled/disabled status.
958 const bool newEnabledState = IsEnabled();
959 if ( newEnabledState != oldEnabledState )
960 {
961 NotifyWindowOnEnableChange(newEnabledState);
962 }
963
964 return true;
965 }
966
967 // ----------------------------------------------------------------------------
968 // event handler stuff
969 // ----------------------------------------------------------------------------
970
971 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
972 {
973 wxEvtHandler *handlerOld = GetEventHandler();
974
975 handler->SetNextHandler(handlerOld);
976
977 if ( handlerOld )
978 GetEventHandler()->SetPreviousHandler(handler);
979
980 SetEventHandler(handler);
981 }
982
983 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
984 {
985 wxEvtHandler *handlerA = GetEventHandler();
986 if ( handlerA )
987 {
988 wxEvtHandler *handlerB = handlerA->GetNextHandler();
989 handlerA->SetNextHandler((wxEvtHandler *)NULL);
990
991 if ( handlerB )
992 handlerB->SetPreviousHandler((wxEvtHandler *)NULL);
993 SetEventHandler(handlerB);
994
995 if ( deleteHandler )
996 {
997 delete handlerA;
998 handlerA = (wxEvtHandler *)NULL;
999 }
1000 }
1001
1002 return handlerA;
1003 }
1004
1005 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handler)
1006 {
1007 wxCHECK_MSG( handler, false, _T("RemoveEventHandler(NULL) called") );
1008
1009 wxEvtHandler *handlerPrev = NULL,
1010 *handlerCur = GetEventHandler();
1011 while ( handlerCur )
1012 {
1013 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
1014
1015 if ( handlerCur == handler )
1016 {
1017 if ( handlerPrev )
1018 {
1019 handlerPrev->SetNextHandler(handlerNext);
1020 }
1021 else
1022 {
1023 SetEventHandler(handlerNext);
1024 }
1025
1026 if ( handlerNext )
1027 {
1028 handlerNext->SetPreviousHandler ( handlerPrev );
1029 }
1030
1031 handler->SetNextHandler(NULL);
1032 handler->SetPreviousHandler(NULL);
1033
1034 return true;
1035 }
1036
1037 handlerPrev = handlerCur;
1038 handlerCur = handlerNext;
1039 }
1040
1041 wxFAIL_MSG( _T("where has the event handler gone?") );
1042
1043 return false;
1044 }
1045
1046 bool wxWindowBase::HandleWindowEvent(wxEvent& event) const
1047 {
1048 return GetEventHandler()->SafelyProcessEvent(event);
1049 }
1050
1051 // ----------------------------------------------------------------------------
1052 // colours, fonts &c
1053 // ----------------------------------------------------------------------------
1054
1055 void wxWindowBase::InheritAttributes()
1056 {
1057 const wxWindowBase * const parent = GetParent();
1058 if ( !parent )
1059 return;
1060
1061 // we only inherit attributes which had been explicitly set for the parent
1062 // which ensures that this only happens if the user really wants it and
1063 // not by default which wouldn't make any sense in modern GUIs where the
1064 // controls don't all use the same fonts (nor colours)
1065 if ( parent->m_inheritFont && !m_hasFont )
1066 SetFont(parent->GetFont());
1067
1068 // in addition, there is a possibility to explicitly forbid inheriting
1069 // colours at each class level by overriding ShouldInheritColours()
1070 if ( ShouldInheritColours() )
1071 {
1072 if ( parent->m_inheritFgCol && !m_hasFgCol )
1073 SetForegroundColour(parent->GetForegroundColour());
1074
1075 // inheriting (solid) background colour is wrong as it totally breaks
1076 // any kind of themed backgrounds
1077 //
1078 // instead, the controls should use the same background as their parent
1079 // (ideally by not drawing it at all)
1080 #if 0
1081 if ( parent->m_inheritBgCol && !m_hasBgCol )
1082 SetBackgroundColour(parent->GetBackgroundColour());
1083 #endif // 0
1084 }
1085 }
1086
1087 /* static */ wxVisualAttributes
1088 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1089 {
1090 // it is important to return valid values for all attributes from here,
1091 // GetXXX() below rely on this
1092 wxVisualAttributes attrs;
1093 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1094 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1095
1096 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1097 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1098 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1099 // colour on other platforms.
1100
1101 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1102 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1103 #else
1104 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1105 #endif
1106 return attrs;
1107 }
1108
1109 wxColour wxWindowBase::GetBackgroundColour() const
1110 {
1111 if ( !m_backgroundColour.IsOk() )
1112 {
1113 wxASSERT_MSG( !m_hasBgCol, _T("we have invalid explicit bg colour?") );
1114
1115 // get our default background colour
1116 wxColour colBg = GetDefaultAttributes().colBg;
1117
1118 // we must return some valid colour to avoid redoing this every time
1119 // and also to avoid surprizing the applications written for older
1120 // wxWidgets versions where GetBackgroundColour() always returned
1121 // something -- so give them something even if it doesn't make sense
1122 // for this window (e.g. it has a themed background)
1123 if ( !colBg.Ok() )
1124 colBg = GetClassDefaultAttributes().colBg;
1125
1126 return colBg;
1127 }
1128 else
1129 return m_backgroundColour;
1130 }
1131
1132 wxColour wxWindowBase::GetForegroundColour() const
1133 {
1134 // logic is the same as above
1135 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
1136 {
1137 wxColour colFg = GetDefaultAttributes().colFg;
1138
1139 if ( !colFg.IsOk() )
1140 colFg = GetClassDefaultAttributes().colFg;
1141
1142 return colFg;
1143 }
1144 else
1145 return m_foregroundColour;
1146 }
1147
1148 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1149 {
1150 if ( colour == m_backgroundColour )
1151 return false;
1152
1153 m_hasBgCol = colour.IsOk();
1154 if ( m_backgroundStyle != wxBG_STYLE_CUSTOM )
1155 m_backgroundStyle = m_hasBgCol ? wxBG_STYLE_COLOUR : wxBG_STYLE_SYSTEM;
1156
1157 m_inheritBgCol = m_hasBgCol;
1158 m_backgroundColour = colour;
1159 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
1160 return true;
1161 }
1162
1163 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1164 {
1165 if (colour == m_foregroundColour )
1166 return false;
1167
1168 m_hasFgCol = colour.IsOk();
1169 m_inheritFgCol = m_hasFgCol;
1170 m_foregroundColour = colour;
1171 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
1172 return true;
1173 }
1174
1175 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1176 {
1177 // setting an invalid cursor is ok, it means that we don't have any special
1178 // cursor
1179 if ( m_cursor.IsSameAs(cursor) )
1180 {
1181 // no change
1182 return false;
1183 }
1184
1185 m_cursor = cursor;
1186
1187 return true;
1188 }
1189
1190 wxFont wxWindowBase::GetFont() const
1191 {
1192 // logic is the same as in GetBackgroundColour()
1193 if ( !m_font.IsOk() )
1194 {
1195 wxASSERT_MSG( !m_hasFont, _T("we have invalid explicit font?") );
1196
1197 wxFont font = GetDefaultAttributes().font;
1198 if ( !font.IsOk() )
1199 font = GetClassDefaultAttributes().font;
1200
1201 return font;
1202 }
1203 else
1204 return m_font;
1205 }
1206
1207 bool wxWindowBase::SetFont(const wxFont& font)
1208 {
1209 if ( font == m_font )
1210 {
1211 // no change
1212 return false;
1213 }
1214
1215 m_font = font;
1216 m_hasFont = font.IsOk();
1217 m_inheritFont = m_hasFont;
1218
1219 InvalidateBestSize();
1220
1221 return true;
1222 }
1223
1224 #if wxUSE_PALETTE
1225
1226 void wxWindowBase::SetPalette(const wxPalette& pal)
1227 {
1228 m_hasCustomPalette = true;
1229 m_palette = pal;
1230
1231 // VZ: can anyone explain me what do we do here?
1232 wxWindowDC d((wxWindow *) this);
1233 d.SetPalette(pal);
1234 }
1235
1236 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1237 {
1238 wxWindow *win = (wxWindow *)this;
1239 while ( win && !win->HasCustomPalette() )
1240 {
1241 win = win->GetParent();
1242 }
1243
1244 return win;
1245 }
1246
1247 #endif // wxUSE_PALETTE
1248
1249 #if wxUSE_CARET
1250 void wxWindowBase::SetCaret(wxCaret *caret)
1251 {
1252 if ( m_caret )
1253 {
1254 delete m_caret;
1255 }
1256
1257 m_caret = caret;
1258
1259 if ( m_caret )
1260 {
1261 wxASSERT_MSG( m_caret->GetWindow() == this,
1262 wxT("caret should be created associated to this window") );
1263 }
1264 }
1265 #endif // wxUSE_CARET
1266
1267 #if wxUSE_VALIDATORS
1268 // ----------------------------------------------------------------------------
1269 // validators
1270 // ----------------------------------------------------------------------------
1271
1272 void wxWindowBase::SetValidator(const wxValidator& validator)
1273 {
1274 if ( m_windowValidator )
1275 delete m_windowValidator;
1276
1277 m_windowValidator = (wxValidator *)validator.Clone();
1278
1279 if ( m_windowValidator )
1280 m_windowValidator->SetWindow(this);
1281 }
1282 #endif // wxUSE_VALIDATORS
1283
1284 // ----------------------------------------------------------------------------
1285 // update region stuff
1286 // ----------------------------------------------------------------------------
1287
1288 wxRect wxWindowBase::GetUpdateClientRect() const
1289 {
1290 wxRegion rgnUpdate = GetUpdateRegion();
1291 rgnUpdate.Intersect(GetClientRect());
1292 wxRect rectUpdate = rgnUpdate.GetBox();
1293 wxPoint ptOrigin = GetClientAreaOrigin();
1294 rectUpdate.x -= ptOrigin.x;
1295 rectUpdate.y -= ptOrigin.y;
1296
1297 return rectUpdate;
1298 }
1299
1300 bool wxWindowBase::DoIsExposed(int x, int y) const
1301 {
1302 return m_updateRegion.Contains(x, y) != wxOutRegion;
1303 }
1304
1305 bool wxWindowBase::DoIsExposed(int x, int y, int w, int h) const
1306 {
1307 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1308 }
1309
1310 void wxWindowBase::ClearBackground()
1311 {
1312 // wxGTK uses its own version, no need to add never used code
1313 #ifndef __WXGTK__
1314 wxClientDC dc((wxWindow *)this);
1315 wxBrush brush(GetBackgroundColour(), wxSOLID);
1316 dc.SetBackground(brush);
1317 dc.Clear();
1318 #endif // __WXGTK__
1319 }
1320
1321 // ----------------------------------------------------------------------------
1322 // find child window by id or name
1323 // ----------------------------------------------------------------------------
1324
1325 wxWindow *wxWindowBase::FindWindow(long id) const
1326 {
1327 if ( id == m_windowId )
1328 return (wxWindow *)this;
1329
1330 wxWindowBase *res = (wxWindow *)NULL;
1331 wxWindowList::compatibility_iterator node;
1332 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1333 {
1334 wxWindowBase *child = node->GetData();
1335 res = child->FindWindow( id );
1336 }
1337
1338 return (wxWindow *)res;
1339 }
1340
1341 wxWindow *wxWindowBase::FindWindow(const wxString& name) const
1342 {
1343 if ( name == m_windowName )
1344 return (wxWindow *)this;
1345
1346 wxWindowBase *res = (wxWindow *)NULL;
1347 wxWindowList::compatibility_iterator node;
1348 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1349 {
1350 wxWindow *child = node->GetData();
1351 res = child->FindWindow(name);
1352 }
1353
1354 return (wxWindow *)res;
1355 }
1356
1357
1358 // find any window by id or name or label: If parent is non-NULL, look through
1359 // children for a label or title matching the specified string. If NULL, look
1360 // through all top-level windows.
1361 //
1362 // to avoid duplicating code we reuse the same helper function but with
1363 // different comparators
1364
1365 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1366 const wxString& label, long id);
1367
1368 static
1369 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1370 long WXUNUSED(id))
1371 {
1372 return win->GetLabel() == label;
1373 }
1374
1375 static
1376 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1377 long WXUNUSED(id))
1378 {
1379 return win->GetName() == label;
1380 }
1381
1382 static
1383 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1384 long id)
1385 {
1386 return win->GetId() == id;
1387 }
1388
1389 // recursive helper for the FindWindowByXXX() functions
1390 static
1391 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1392 const wxString& label,
1393 long id,
1394 wxFindWindowCmp cmp)
1395 {
1396 if ( parent )
1397 {
1398 // see if this is the one we're looking for
1399 if ( (*cmp)(parent, label, id) )
1400 return (wxWindow *)parent;
1401
1402 // It wasn't, so check all its children
1403 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1404 node;
1405 node = node->GetNext() )
1406 {
1407 // recursively check each child
1408 wxWindow *win = (wxWindow *)node->GetData();
1409 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1410 if (retwin)
1411 return retwin;
1412 }
1413 }
1414
1415 // Not found
1416 return NULL;
1417 }
1418
1419 // helper for FindWindowByXXX()
1420 static
1421 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1422 const wxString& label,
1423 long id,
1424 wxFindWindowCmp cmp)
1425 {
1426 if ( parent )
1427 {
1428 // just check parent and all its children
1429 return wxFindWindowRecursively(parent, label, id, cmp);
1430 }
1431
1432 // start at very top of wx's windows
1433 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1434 node;
1435 node = node->GetNext() )
1436 {
1437 // recursively check each window & its children
1438 wxWindow *win = node->GetData();
1439 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1440 if (retwin)
1441 return retwin;
1442 }
1443
1444 return NULL;
1445 }
1446
1447 /* static */
1448 wxWindow *
1449 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1450 {
1451 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1452 }
1453
1454 /* static */
1455 wxWindow *
1456 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1457 {
1458 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1459
1460 if ( !win )
1461 {
1462 // fall back to the label
1463 win = FindWindowByLabel(title, parent);
1464 }
1465
1466 return win;
1467 }
1468
1469 /* static */
1470 wxWindow *
1471 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1472 {
1473 return wxFindWindowHelper(parent, wxEmptyString, id, wxFindWindowCmpIds);
1474 }
1475
1476 // ----------------------------------------------------------------------------
1477 // dialog oriented functions
1478 // ----------------------------------------------------------------------------
1479
1480 void wxWindowBase::MakeModal(bool modal)
1481 {
1482 // Disable all other windows
1483 if ( IsTopLevel() )
1484 {
1485 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1486 while (node)
1487 {
1488 wxWindow *win = node->GetData();
1489 if (win != this)
1490 win->Enable(!modal);
1491
1492 node = node->GetNext();
1493 }
1494 }
1495 }
1496
1497 bool wxWindowBase::Validate()
1498 {
1499 #if wxUSE_VALIDATORS
1500 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1501
1502 wxWindowList::compatibility_iterator node;
1503 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1504 {
1505 wxWindowBase *child = node->GetData();
1506 wxValidator *validator = child->GetValidator();
1507 if ( validator && !validator->Validate((wxWindow *)this) )
1508 {
1509 return false;
1510 }
1511
1512 if ( recurse && !child->Validate() )
1513 {
1514 return false;
1515 }
1516 }
1517 #endif // wxUSE_VALIDATORS
1518
1519 return true;
1520 }
1521
1522 bool wxWindowBase::TransferDataToWindow()
1523 {
1524 #if wxUSE_VALIDATORS
1525 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1526
1527 wxWindowList::compatibility_iterator node;
1528 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1529 {
1530 wxWindowBase *child = node->GetData();
1531 wxValidator *validator = child->GetValidator();
1532 if ( validator && !validator->TransferToWindow() )
1533 {
1534 wxLogWarning(_("Could not transfer data to window"));
1535 #if wxUSE_LOG
1536 wxLog::FlushActive();
1537 #endif // wxUSE_LOG
1538
1539 return false;
1540 }
1541
1542 if ( recurse )
1543 {
1544 if ( !child->TransferDataToWindow() )
1545 {
1546 // warning already given
1547 return false;
1548 }
1549 }
1550 }
1551 #endif // wxUSE_VALIDATORS
1552
1553 return true;
1554 }
1555
1556 bool wxWindowBase::TransferDataFromWindow()
1557 {
1558 #if wxUSE_VALIDATORS
1559 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1560
1561 wxWindowList::compatibility_iterator node;
1562 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1563 {
1564 wxWindow *child = node->GetData();
1565 wxValidator *validator = child->GetValidator();
1566 if ( validator && !validator->TransferFromWindow() )
1567 {
1568 // nop warning here because the application is supposed to give
1569 // one itself - we don't know here what might have gone wrongly
1570
1571 return false;
1572 }
1573
1574 if ( recurse )
1575 {
1576 if ( !child->TransferDataFromWindow() )
1577 {
1578 // warning already given
1579 return false;
1580 }
1581 }
1582 }
1583 #endif // wxUSE_VALIDATORS
1584
1585 return true;
1586 }
1587
1588 void wxWindowBase::InitDialog()
1589 {
1590 wxInitDialogEvent event(GetId());
1591 event.SetEventObject( this );
1592 GetEventHandler()->ProcessEvent(event);
1593 }
1594
1595 // ----------------------------------------------------------------------------
1596 // context-sensitive help support
1597 // ----------------------------------------------------------------------------
1598
1599 #if wxUSE_HELP
1600
1601 // associate this help text with this window
1602 void wxWindowBase::SetHelpText(const wxString& text)
1603 {
1604 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1605 if ( helpProvider )
1606 {
1607 helpProvider->AddHelp(this, text);
1608 }
1609 }
1610
1611 // associate this help text with all windows with the same id as this
1612 // one
1613 void wxWindowBase::SetHelpTextForId(const wxString& text)
1614 {
1615 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1616 if ( helpProvider )
1617 {
1618 helpProvider->AddHelp(GetId(), text);
1619 }
1620 }
1621
1622 // get the help string associated with this window (may be empty)
1623 // default implementation forwards calls to the help provider
1624 wxString
1625 wxWindowBase::GetHelpTextAtPoint(const wxPoint & WXUNUSED(pt),
1626 wxHelpEvent::Origin WXUNUSED(origin)) const
1627 {
1628 wxString text;
1629 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1630 if ( helpProvider )
1631 {
1632 text = helpProvider->GetHelp(this);
1633 }
1634
1635 return text;
1636 }
1637
1638 // show help for this window
1639 void wxWindowBase::OnHelp(wxHelpEvent& event)
1640 {
1641 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1642 if ( helpProvider )
1643 {
1644 if ( helpProvider->ShowHelpAtPoint(this, event.GetPosition(), event.GetOrigin()) )
1645 {
1646 // skip the event.Skip() below
1647 return;
1648 }
1649 }
1650
1651 event.Skip();
1652 }
1653
1654 #endif // wxUSE_HELP
1655
1656 // ----------------------------------------------------------------------------
1657 // tooltips
1658 // ----------------------------------------------------------------------------
1659
1660 #if wxUSE_TOOLTIPS
1661
1662 void wxWindowBase::SetToolTip( const wxString &tip )
1663 {
1664 // don't create the new tooltip if we already have one
1665 if ( m_tooltip )
1666 {
1667 m_tooltip->SetTip( tip );
1668 }
1669 else
1670 {
1671 SetToolTip( new wxToolTip( tip ) );
1672 }
1673
1674 // setting empty tooltip text does not remove the tooltip any more - use
1675 // SetToolTip((wxToolTip *)NULL) for this
1676 }
1677
1678 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1679 {
1680 if ( m_tooltip != tooltip )
1681 {
1682 if ( m_tooltip )
1683 delete m_tooltip;
1684
1685 m_tooltip = tooltip;
1686 }
1687 }
1688
1689 #endif // wxUSE_TOOLTIPS
1690
1691 // ----------------------------------------------------------------------------
1692 // constraints and sizers
1693 // ----------------------------------------------------------------------------
1694
1695 #if wxUSE_CONSTRAINTS
1696
1697 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1698 {
1699 if ( m_constraints )
1700 {
1701 UnsetConstraints(m_constraints);
1702 delete m_constraints;
1703 }
1704 m_constraints = constraints;
1705 if ( m_constraints )
1706 {
1707 // Make sure other windows know they're part of a 'meaningful relationship'
1708 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1709 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1710 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1711 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1712 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1713 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1714 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1715 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1716 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1717 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1718 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1719 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1720 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1721 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1722 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1723 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1724 }
1725 }
1726
1727 // This removes any dangling pointers to this window in other windows'
1728 // constraintsInvolvedIn lists.
1729 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1730 {
1731 if ( c )
1732 {
1733 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1734 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1735 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1736 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1737 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1738 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1739 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1740 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1741 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1742 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1743 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1744 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1745 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1746 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1747 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1748 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1749 }
1750 }
1751
1752 // Back-pointer to other windows we're involved with, so if we delete this
1753 // window, we must delete any constraints we're involved with.
1754 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1755 {
1756 if ( !m_constraintsInvolvedIn )
1757 m_constraintsInvolvedIn = new wxWindowList;
1758 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
1759 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
1760 }
1761
1762 // REMOVE back-pointer to other windows we're involved with.
1763 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1764 {
1765 if ( m_constraintsInvolvedIn )
1766 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
1767 }
1768
1769 // Reset any constraints that mention this window
1770 void wxWindowBase::DeleteRelatedConstraints()
1771 {
1772 if ( m_constraintsInvolvedIn )
1773 {
1774 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
1775 while (node)
1776 {
1777 wxWindow *win = node->GetData();
1778 wxLayoutConstraints *constr = win->GetConstraints();
1779
1780 // Reset any constraints involving this window
1781 if ( constr )
1782 {
1783 constr->left.ResetIfWin(this);
1784 constr->top.ResetIfWin(this);
1785 constr->right.ResetIfWin(this);
1786 constr->bottom.ResetIfWin(this);
1787 constr->width.ResetIfWin(this);
1788 constr->height.ResetIfWin(this);
1789 constr->centreX.ResetIfWin(this);
1790 constr->centreY.ResetIfWin(this);
1791 }
1792
1793 wxWindowList::compatibility_iterator next = node->GetNext();
1794 m_constraintsInvolvedIn->Erase(node);
1795 node = next;
1796 }
1797
1798 delete m_constraintsInvolvedIn;
1799 m_constraintsInvolvedIn = (wxWindowList *) NULL;
1800 }
1801 }
1802
1803 #endif // wxUSE_CONSTRAINTS
1804
1805 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
1806 {
1807 if ( sizer == m_windowSizer)
1808 return;
1809
1810 if ( m_windowSizer )
1811 {
1812 m_windowSizer->SetContainingWindow(NULL);
1813
1814 if ( deleteOld )
1815 delete m_windowSizer;
1816 }
1817
1818 m_windowSizer = sizer;
1819 if ( m_windowSizer )
1820 {
1821 m_windowSizer->SetContainingWindow((wxWindow *)this);
1822 }
1823
1824 SetAutoLayout(m_windowSizer != NULL);
1825 }
1826
1827 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
1828 {
1829 SetSizer( sizer, deleteOld );
1830 sizer->SetSizeHints( (wxWindow*) this );
1831 }
1832
1833
1834 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
1835 {
1836 // adding a window to a sizer twice is going to result in fatal and
1837 // hard to debug problems later because when deleting the second
1838 // associated wxSizerItem we're going to dereference a dangling
1839 // pointer; so try to detect this as early as possible
1840 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
1841 _T("Adding a window to the same sizer twice?") );
1842
1843 m_containingSizer = sizer;
1844 }
1845
1846 #if wxUSE_CONSTRAINTS
1847
1848 void wxWindowBase::SatisfyConstraints()
1849 {
1850 wxLayoutConstraints *constr = GetConstraints();
1851 bool wasOk = constr && constr->AreSatisfied();
1852
1853 ResetConstraints(); // Mark all constraints as unevaluated
1854
1855 int noChanges = 1;
1856
1857 // if we're a top level panel (i.e. our parent is frame/dialog), our
1858 // own constraints will never be satisfied any more unless we do it
1859 // here
1860 if ( wasOk )
1861 {
1862 while ( noChanges > 0 )
1863 {
1864 LayoutPhase1(&noChanges);
1865 }
1866 }
1867
1868 LayoutPhase2(&noChanges);
1869 }
1870
1871 #endif // wxUSE_CONSTRAINTS
1872
1873 bool wxWindowBase::Layout()
1874 {
1875 // If there is a sizer, use it instead of the constraints
1876 if ( GetSizer() )
1877 {
1878 int w = 0, h = 0;
1879 GetVirtualSize(&w, &h);
1880 GetSizer()->SetDimension( 0, 0, w, h );
1881 }
1882 #if wxUSE_CONSTRAINTS
1883 else
1884 {
1885 SatisfyConstraints(); // Find the right constraints values
1886 SetConstraintSizes(); // Recursively set the real window sizes
1887 }
1888 #endif
1889
1890 return true;
1891 }
1892
1893 #if wxUSE_CONSTRAINTS
1894
1895 // first phase of the constraints evaluation: set our own constraints
1896 bool wxWindowBase::LayoutPhase1(int *noChanges)
1897 {
1898 wxLayoutConstraints *constr = GetConstraints();
1899
1900 return !constr || constr->SatisfyConstraints(this, noChanges);
1901 }
1902
1903 // second phase: set the constraints for our children
1904 bool wxWindowBase::LayoutPhase2(int *noChanges)
1905 {
1906 *noChanges = 0;
1907
1908 // Layout children
1909 DoPhase(1);
1910
1911 // Layout grand children
1912 DoPhase(2);
1913
1914 return true;
1915 }
1916
1917 // Do a phase of evaluating child constraints
1918 bool wxWindowBase::DoPhase(int phase)
1919 {
1920 // the list containing the children for which the constraints are already
1921 // set correctly
1922 wxWindowList succeeded;
1923
1924 // the max number of iterations we loop before concluding that we can't set
1925 // the constraints
1926 static const int maxIterations = 500;
1927
1928 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
1929 {
1930 int noChanges = 0;
1931
1932 // loop over all children setting their constraints
1933 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1934 node;
1935 node = node->GetNext() )
1936 {
1937 wxWindow *child = node->GetData();
1938 if ( child->IsTopLevel() )
1939 {
1940 // top level children are not inside our client area
1941 continue;
1942 }
1943
1944 if ( !child->GetConstraints() || succeeded.Find(child) )
1945 {
1946 // this one is either already ok or nothing we can do about it
1947 continue;
1948 }
1949
1950 int tempNoChanges = 0;
1951 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
1952 : child->LayoutPhase2(&tempNoChanges);
1953 noChanges += tempNoChanges;
1954
1955 if ( success )
1956 {
1957 succeeded.Append(child);
1958 }
1959 }
1960
1961 if ( !noChanges )
1962 {
1963 // constraints are set
1964 break;
1965 }
1966 }
1967
1968 return true;
1969 }
1970
1971 void wxWindowBase::ResetConstraints()
1972 {
1973 wxLayoutConstraints *constr = GetConstraints();
1974 if ( constr )
1975 {
1976 constr->left.SetDone(false);
1977 constr->top.SetDone(false);
1978 constr->right.SetDone(false);
1979 constr->bottom.SetDone(false);
1980 constr->width.SetDone(false);
1981 constr->height.SetDone(false);
1982 constr->centreX.SetDone(false);
1983 constr->centreY.SetDone(false);
1984 }
1985
1986 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1987 while (node)
1988 {
1989 wxWindow *win = node->GetData();
1990 if ( !win->IsTopLevel() )
1991 win->ResetConstraints();
1992 node = node->GetNext();
1993 }
1994 }
1995
1996 // Need to distinguish between setting the 'fake' size for windows and sizers,
1997 // and setting the real values.
1998 void wxWindowBase::SetConstraintSizes(bool recurse)
1999 {
2000 wxLayoutConstraints *constr = GetConstraints();
2001 if ( constr && constr->AreSatisfied() )
2002 {
2003 int x = constr->left.GetValue();
2004 int y = constr->top.GetValue();
2005 int w = constr->width.GetValue();
2006 int h = constr->height.GetValue();
2007
2008 if ( (constr->width.GetRelationship() != wxAsIs ) ||
2009 (constr->height.GetRelationship() != wxAsIs) )
2010 {
2011 SetSize(x, y, w, h);
2012 }
2013 else
2014 {
2015 // If we don't want to resize this window, just move it...
2016 Move(x, y);
2017 }
2018 }
2019 else if ( constr )
2020 {
2021 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2022 GetClassInfo()->GetClassName(),
2023 GetName().c_str());
2024 }
2025
2026 if ( recurse )
2027 {
2028 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2029 while (node)
2030 {
2031 wxWindow *win = node->GetData();
2032 if ( !win->IsTopLevel() && win->GetConstraints() )
2033 win->SetConstraintSizes();
2034 node = node->GetNext();
2035 }
2036 }
2037 }
2038
2039 // Only set the size/position of the constraint (if any)
2040 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
2041 {
2042 wxLayoutConstraints *constr = GetConstraints();
2043 if ( constr )
2044 {
2045 if ( x != wxDefaultCoord )
2046 {
2047 constr->left.SetValue(x);
2048 constr->left.SetDone(true);
2049 }
2050 if ( y != wxDefaultCoord )
2051 {
2052 constr->top.SetValue(y);
2053 constr->top.SetDone(true);
2054 }
2055 if ( w != wxDefaultCoord )
2056 {
2057 constr->width.SetValue(w);
2058 constr->width.SetDone(true);
2059 }
2060 if ( h != wxDefaultCoord )
2061 {
2062 constr->height.SetValue(h);
2063 constr->height.SetDone(true);
2064 }
2065 }
2066 }
2067
2068 void wxWindowBase::MoveConstraint(int x, int y)
2069 {
2070 wxLayoutConstraints *constr = GetConstraints();
2071 if ( constr )
2072 {
2073 if ( x != wxDefaultCoord )
2074 {
2075 constr->left.SetValue(x);
2076 constr->left.SetDone(true);
2077 }
2078 if ( y != wxDefaultCoord )
2079 {
2080 constr->top.SetValue(y);
2081 constr->top.SetDone(true);
2082 }
2083 }
2084 }
2085
2086 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
2087 {
2088 wxLayoutConstraints *constr = GetConstraints();
2089 if ( constr )
2090 {
2091 *w = constr->width.GetValue();
2092 *h = constr->height.GetValue();
2093 }
2094 else
2095 GetSize(w, h);
2096 }
2097
2098 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
2099 {
2100 wxLayoutConstraints *constr = GetConstraints();
2101 if ( constr )
2102 {
2103 *w = constr->width.GetValue();
2104 *h = constr->height.GetValue();
2105 }
2106 else
2107 GetClientSize(w, h);
2108 }
2109
2110 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
2111 {
2112 wxLayoutConstraints *constr = GetConstraints();
2113 if ( constr )
2114 {
2115 *x = constr->left.GetValue();
2116 *y = constr->top.GetValue();
2117 }
2118 else
2119 GetPosition(x, y);
2120 }
2121
2122 #endif // wxUSE_CONSTRAINTS
2123
2124 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
2125 {
2126 // don't do it for the dialogs/frames - they float independently of their
2127 // parent
2128 if ( !IsTopLevel() )
2129 {
2130 wxWindow *parent = GetParent();
2131 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
2132 {
2133 wxPoint pt(parent->GetClientAreaOrigin());
2134 x += pt.x;
2135 y += pt.y;
2136 }
2137 }
2138 }
2139
2140 // ----------------------------------------------------------------------------
2141 // Update UI processing
2142 // ----------------------------------------------------------------------------
2143
2144 void wxWindowBase::UpdateWindowUI(long flags)
2145 {
2146 wxUpdateUIEvent event(GetId());
2147 event.SetEventObject(this);
2148
2149 if ( GetEventHandler()->ProcessEvent(event) )
2150 {
2151 DoUpdateWindowUI(event);
2152 }
2153
2154 if (flags & wxUPDATE_UI_RECURSE)
2155 {
2156 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2157 while (node)
2158 {
2159 wxWindow* child = (wxWindow*) node->GetData();
2160 child->UpdateWindowUI(flags);
2161 node = node->GetNext();
2162 }
2163 }
2164 }
2165
2166 // do the window-specific processing after processing the update event
2167 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2168 {
2169 if ( event.GetSetEnabled() )
2170 Enable(event.GetEnabled());
2171
2172 if ( event.GetSetShown() )
2173 Show(event.GetShown());
2174 }
2175
2176 // ----------------------------------------------------------------------------
2177 // dialog units translations
2178 // ----------------------------------------------------------------------------
2179
2180 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2181 {
2182 int charWidth = GetCharWidth();
2183 int charHeight = GetCharHeight();
2184 wxPoint pt2 = wxDefaultPosition;
2185 if (pt.x != wxDefaultCoord)
2186 pt2.x = (int) ((pt.x * 4) / charWidth);
2187 if (pt.y != wxDefaultCoord)
2188 pt2.y = (int) ((pt.y * 8) / charHeight);
2189
2190 return pt2;
2191 }
2192
2193 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2194 {
2195 int charWidth = GetCharWidth();
2196 int charHeight = GetCharHeight();
2197 wxPoint pt2 = wxDefaultPosition;
2198 if (pt.x != wxDefaultCoord)
2199 pt2.x = (int) ((pt.x * charWidth) / 4);
2200 if (pt.y != wxDefaultCoord)
2201 pt2.y = (int) ((pt.y * charHeight) / 8);
2202
2203 return pt2;
2204 }
2205
2206 // ----------------------------------------------------------------------------
2207 // event handlers
2208 // ----------------------------------------------------------------------------
2209
2210 // propagate the colour change event to the subwindows
2211 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2212 {
2213 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2214 while ( node )
2215 {
2216 // Only propagate to non-top-level windows
2217 wxWindow *win = node->GetData();
2218 if ( !win->IsTopLevel() )
2219 {
2220 wxSysColourChangedEvent event2;
2221 event.SetEventObject(win);
2222 win->GetEventHandler()->ProcessEvent(event2);
2223 }
2224
2225 node = node->GetNext();
2226 }
2227
2228 Refresh();
2229 }
2230
2231 // the default action is to populate dialog with data when it's created,
2232 // and nudge the UI into displaying itself correctly in case
2233 // we've turned the wxUpdateUIEvents frequency down low.
2234 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2235 {
2236 TransferDataToWindow();
2237
2238 // Update the UI at this point
2239 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2240 }
2241
2242 // ----------------------------------------------------------------------------
2243 // menu-related functions
2244 // ----------------------------------------------------------------------------
2245
2246 #if wxUSE_MENUS
2247
2248 // this is used to pass the id of the selected item from the menu event handler
2249 // to the main function itself
2250 //
2251 // it's ok to use a global here as there can be at most one popup menu shown at
2252 // any time
2253 static int gs_popupMenuSelection = wxID_NONE;
2254
2255 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event)
2256 {
2257 // store the id in a global variable where we'll retrieve it from later
2258 gs_popupMenuSelection = event.GetId();
2259 }
2260
2261 int
2262 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y)
2263 {
2264 gs_popupMenuSelection = wxID_NONE;
2265
2266 Connect(wxEVT_COMMAND_MENU_SELECTED,
2267 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2268 NULL,
2269 this);
2270
2271 PopupMenu(&menu, x, y);
2272
2273 Disconnect(wxEVT_COMMAND_MENU_SELECTED,
2274 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2275 NULL,
2276 this);
2277
2278 return gs_popupMenuSelection;
2279 }
2280
2281 #endif // wxUSE_MENUS
2282
2283 // methods for drawing the sizers in a visible way
2284 #ifdef __WXDEBUG__
2285
2286 static void DrawSizers(wxWindowBase *win);
2287
2288 static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill = false)
2289 {
2290 wxClientDC dc((wxWindow *)win);
2291 dc.SetPen(*wxRED_PEN);
2292 dc.SetBrush(fill ? wxBrush(*wxRED, wxCROSSDIAG_HATCH): *wxTRANSPARENT_BRUSH);
2293 dc.DrawRectangle(rect.Deflate(1, 1));
2294 }
2295
2296 static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
2297 {
2298 const wxSizerItemList& items = sizer->GetChildren();
2299 for ( wxSizerItemList::const_iterator i = items.begin(),
2300 end = items.end();
2301 i != end;
2302 ++i )
2303 {
2304 wxSizerItem *item = *i;
2305 if ( item->IsSizer() )
2306 {
2307 DrawBorder(win, item->GetRect().Deflate(2));
2308 DrawSizer(win, item->GetSizer());
2309 }
2310 else if ( item->IsSpacer() )
2311 {
2312 DrawBorder(win, item->GetRect().Deflate(2), true);
2313 }
2314 else if ( item->IsWindow() )
2315 {
2316 DrawSizers(item->GetWindow());
2317 }
2318 }
2319 }
2320
2321 static void DrawSizers(wxWindowBase *win)
2322 {
2323 wxSizer *sizer = win->GetSizer();
2324 if ( sizer )
2325 {
2326 DrawBorder(win, win->GetClientSize());
2327 DrawSizer(win, sizer);
2328 }
2329 else // no sizer, still recurse into the children
2330 {
2331 const wxWindowList& children = win->GetChildren();
2332 for ( wxWindowList::const_iterator i = children.begin(),
2333 end = children.end();
2334 i != end;
2335 ++i )
2336 {
2337 DrawSizers(*i);
2338 }
2339 }
2340 }
2341
2342 #endif // __WXDEBUG__
2343
2344 // process special middle clicks
2345 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2346 {
2347 if ( event.ControlDown() && event.AltDown() )
2348 {
2349 #ifdef __WXDEBUG__
2350 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2351 if ( event.ShiftDown() )
2352 {
2353 DrawSizers(this);
2354 return;
2355 }
2356 #endif // __WXDEBUG__
2357 ::wxInfoMessageBox((wxWindow*)this);
2358 }
2359 else
2360 {
2361 event.Skip();
2362 }
2363 }
2364
2365 // ----------------------------------------------------------------------------
2366 // accessibility
2367 // ----------------------------------------------------------------------------
2368
2369 #if wxUSE_ACCESSIBILITY
2370 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2371 {
2372 if (m_accessible && (accessible != m_accessible))
2373 delete m_accessible;
2374 m_accessible = accessible;
2375 if (m_accessible)
2376 m_accessible->SetWindow((wxWindow*) this);
2377 }
2378
2379 // Returns the accessible object, creating if necessary.
2380 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2381 {
2382 if (!m_accessible)
2383 m_accessible = CreateAccessible();
2384 return m_accessible;
2385 }
2386
2387 // Override to create a specific accessible object.
2388 wxAccessible* wxWindowBase::CreateAccessible()
2389 {
2390 return new wxWindowAccessible((wxWindow*) this);
2391 }
2392
2393 #endif
2394
2395 // ----------------------------------------------------------------------------
2396 // list classes implementation
2397 // ----------------------------------------------------------------------------
2398
2399 #if wxUSE_STL
2400
2401 #include "wx/listimpl.cpp"
2402 WX_DEFINE_LIST(wxWindowList)
2403
2404 #else // !wxUSE_STL
2405
2406 void wxWindowListNode::DeleteData()
2407 {
2408 delete (wxWindow *)GetData();
2409 }
2410
2411 #endif // wxUSE_STL/!wxUSE_STL
2412
2413 // ----------------------------------------------------------------------------
2414 // borders
2415 // ----------------------------------------------------------------------------
2416
2417 wxBorder wxWindowBase::GetBorder(long flags) const
2418 {
2419 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2420 if ( border == wxBORDER_DEFAULT )
2421 {
2422 border = GetDefaultBorder();
2423 }
2424 else if ( border == wxBORDER_THEME )
2425 {
2426 border = GetDefaultBorderForControl();
2427 }
2428
2429 return border;
2430 }
2431
2432 wxBorder wxWindowBase::GetDefaultBorder() const
2433 {
2434 return wxBORDER_NONE;
2435 }
2436
2437 // ----------------------------------------------------------------------------
2438 // hit testing
2439 // ----------------------------------------------------------------------------
2440
2441 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2442 {
2443 // here we just check if the point is inside the window or not
2444
2445 // check the top and left border first
2446 bool outside = x < 0 || y < 0;
2447 if ( !outside )
2448 {
2449 // check the right and bottom borders too
2450 wxSize size = GetSize();
2451 outside = x >= size.x || y >= size.y;
2452 }
2453
2454 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2455 }
2456
2457 // ----------------------------------------------------------------------------
2458 // mouse capture
2459 // ----------------------------------------------------------------------------
2460
2461 struct WXDLLEXPORT wxWindowNext
2462 {
2463 wxWindow *win;
2464 wxWindowNext *next;
2465 } *wxWindowBase::ms_winCaptureNext = NULL;
2466 wxWindow *wxWindowBase::ms_winCaptureCurrent = NULL;
2467 bool wxWindowBase::ms_winCaptureChanging = false;
2468
2469 void wxWindowBase::CaptureMouse()
2470 {
2471 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), wx_static_cast(void*, this));
2472
2473 wxASSERT_MSG( !ms_winCaptureChanging, _T("recursive CaptureMouse call?") );
2474
2475 ms_winCaptureChanging = true;
2476
2477 wxWindow *winOld = GetCapture();
2478 if ( winOld )
2479 {
2480 ((wxWindowBase*) winOld)->DoReleaseMouse();
2481
2482 // save it on stack
2483 wxWindowNext *item = new wxWindowNext;
2484 item->win = winOld;
2485 item->next = ms_winCaptureNext;
2486 ms_winCaptureNext = item;
2487 }
2488 //else: no mouse capture to save
2489
2490 DoCaptureMouse();
2491 ms_winCaptureCurrent = (wxWindow*)this;
2492
2493 ms_winCaptureChanging = false;
2494 }
2495
2496 void wxWindowBase::ReleaseMouse()
2497 {
2498 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), wx_static_cast(void*, this));
2499
2500 wxASSERT_MSG( !ms_winCaptureChanging, _T("recursive ReleaseMouse call?") );
2501
2502 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2503
2504 ms_winCaptureChanging = true;
2505
2506 DoReleaseMouse();
2507 ms_winCaptureCurrent = NULL;
2508
2509 if ( ms_winCaptureNext )
2510 {
2511 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2512 ms_winCaptureCurrent = ms_winCaptureNext->win;
2513
2514 wxWindowNext *item = ms_winCaptureNext;
2515 ms_winCaptureNext = item->next;
2516 delete item;
2517 }
2518 //else: stack is empty, no previous capture
2519
2520 ms_winCaptureChanging = false;
2521
2522 wxLogTrace(_T("mousecapture"),
2523 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
2524 wx_static_cast(void*, GetCapture()));
2525 }
2526
2527 static void DoNotifyWindowAboutCaptureLost(wxWindow *win)
2528 {
2529 wxMouseCaptureLostEvent event(win->GetId());
2530 event.SetEventObject(win);
2531 if ( !win->GetEventHandler()->ProcessEvent(event) )
2532 {
2533 // windows must handle this event, otherwise the app wouldn't behave
2534 // correctly if it loses capture unexpectedly; see the discussion here:
2535 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2536 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
2537 wxFAIL_MSG( _T("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2538 }
2539 }
2540
2541 /* static */
2542 void wxWindowBase::NotifyCaptureLost()
2543 {
2544 // don't do anything if capture lost was expected, i.e. resulted from
2545 // a wx call to ReleaseMouse or CaptureMouse:
2546 if ( ms_winCaptureChanging )
2547 return;
2548
2549 // if the capture was lost unexpectedly, notify every window that has
2550 // capture (on stack or current) about it and clear the stack:
2551
2552 if ( ms_winCaptureCurrent )
2553 {
2554 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent);
2555 ms_winCaptureCurrent = NULL;
2556 }
2557
2558 while ( ms_winCaptureNext )
2559 {
2560 wxWindowNext *item = ms_winCaptureNext;
2561 ms_winCaptureNext = item->next;
2562
2563 DoNotifyWindowAboutCaptureLost(item->win);
2564
2565 delete item;
2566 }
2567 }
2568
2569 #if wxUSE_HOTKEY
2570
2571 bool
2572 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2573 int WXUNUSED(modifiers),
2574 int WXUNUSED(keycode))
2575 {
2576 // not implemented
2577 return false;
2578 }
2579
2580 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2581 {
2582 // not implemented
2583 return false;
2584 }
2585
2586 #endif // wxUSE_HOTKEY
2587
2588 // ----------------------------------------------------------------------------
2589 // event processing
2590 // ----------------------------------------------------------------------------
2591
2592 bool wxWindowBase::TryValidator(wxEvent& wxVALIDATOR_PARAM(event))
2593 {
2594 #if wxUSE_VALIDATORS
2595 // Can only use the validator of the window which
2596 // is receiving the event
2597 if ( event.GetEventObject() == this )
2598 {
2599 wxValidator *validator = GetValidator();
2600 if ( validator && validator->ProcessEvent(event) )
2601 {
2602 return true;
2603 }
2604 }
2605 #endif // wxUSE_VALIDATORS
2606
2607 return false;
2608 }
2609
2610 bool wxWindowBase::TryParent(wxEvent& event)
2611 {
2612 // carry on up the parent-child hierarchy if the propagation count hasn't
2613 // reached zero yet
2614 if ( event.ShouldPropagate() )
2615 {
2616 // honour the requests to stop propagation at this window: this is
2617 // used by the dialogs, for example, to prevent processing the events
2618 // from the dialog controls in the parent frame which rarely, if ever,
2619 // makes sense
2620 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2621 {
2622 wxWindow *parent = GetParent();
2623 if ( parent && !parent->IsBeingDeleted() )
2624 {
2625 wxPropagateOnce propagateOnce(event);
2626
2627 return parent->GetEventHandler()->ProcessEvent(event);
2628 }
2629 }
2630 }
2631
2632 return wxEvtHandler::TryParent(event);
2633 }
2634
2635 // ----------------------------------------------------------------------------
2636 // window relationships
2637 // ----------------------------------------------------------------------------
2638
2639 wxWindow *wxWindowBase::DoGetSibling(WindowOrder order) const
2640 {
2641 wxCHECK_MSG( GetParent(), NULL,
2642 _T("GetPrev/NextSibling() don't work for TLWs!") );
2643
2644 wxWindowList& siblings = GetParent()->GetChildren();
2645 wxWindowList::compatibility_iterator i = siblings.Find((wxWindow *)this);
2646 wxCHECK_MSG( i, NULL, _T("window not a child of its parent?") );
2647
2648 if ( order == OrderBefore )
2649 i = i->GetPrevious();
2650 else // OrderAfter
2651 i = i->GetNext();
2652
2653 return i ? i->GetData() : NULL;
2654 }
2655
2656 // ----------------------------------------------------------------------------
2657 // keyboard navigation
2658 // ----------------------------------------------------------------------------
2659
2660 // Navigates in the specified direction inside this window
2661 bool wxWindowBase::DoNavigateIn(int flags)
2662 {
2663 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2664 // native code doesn't process our wxNavigationKeyEvents anyhow
2665 wxUnusedVar(flags);
2666 return false;
2667 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
2668 wxNavigationKeyEvent eventNav;
2669 eventNav.SetFlags(flags);
2670 eventNav.SetEventObject(FindFocus());
2671 return GetEventHandler()->ProcessEvent(eventNav);
2672 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
2673 }
2674
2675 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, WindowOrder move)
2676 {
2677 // check that we're not a top level window
2678 wxCHECK_RET( GetParent(),
2679 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2680
2681 // detect the special case when we have nothing to do anyhow and when the
2682 // code below wouldn't work
2683 if ( win == this )
2684 return;
2685
2686 // find the target window in the siblings list
2687 wxWindowList& siblings = GetParent()->GetChildren();
2688 wxWindowList::compatibility_iterator i = siblings.Find(win);
2689 wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2690
2691 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2692 // can't just move the node around
2693 wxWindow *self = (wxWindow *)this;
2694 siblings.DeleteObject(self);
2695 if ( move == OrderAfter )
2696 {
2697 i = i->GetNext();
2698 }
2699
2700 if ( i )
2701 {
2702 siblings.Insert(i, self);
2703 }
2704 else // OrderAfter and win was the last sibling
2705 {
2706 siblings.Append(self);
2707 }
2708 }
2709
2710 // ----------------------------------------------------------------------------
2711 // focus handling
2712 // ----------------------------------------------------------------------------
2713
2714 /*static*/ wxWindow* wxWindowBase::FindFocus()
2715 {
2716 wxWindowBase *win = DoFindFocus();
2717 return win ? win->GetMainWindowOfCompositeControl() : NULL;
2718 }
2719
2720 // ----------------------------------------------------------------------------
2721 // global functions
2722 // ----------------------------------------------------------------------------
2723
2724 wxWindow* wxGetTopLevelParent(wxWindow *win)
2725 {
2726 while ( win && !win->IsTopLevel() )
2727 win = win->GetParent();
2728
2729 return win;
2730 }
2731
2732 #if wxUSE_ACCESSIBILITY
2733 // ----------------------------------------------------------------------------
2734 // accessible object for windows
2735 // ----------------------------------------------------------------------------
2736
2737 // Can return either a child object, or an integer
2738 // representing the child element, starting from 1.
2739 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
2740 {
2741 wxASSERT( GetWindow() != NULL );
2742 if (!GetWindow())
2743 return wxACC_FAIL;
2744
2745 return wxACC_NOT_IMPLEMENTED;
2746 }
2747
2748 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2749 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
2750 {
2751 wxASSERT( GetWindow() != NULL );
2752 if (!GetWindow())
2753 return wxACC_FAIL;
2754
2755 wxWindow* win = NULL;
2756 if (elementId == 0)
2757 {
2758 win = GetWindow();
2759 }
2760 else
2761 {
2762 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
2763 {
2764 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
2765 }
2766 else
2767 return wxACC_FAIL;
2768 }
2769 if (win)
2770 {
2771 rect = win->GetRect();
2772 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
2773 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
2774 return wxACC_OK;
2775 }
2776
2777 return wxACC_NOT_IMPLEMENTED;
2778 }
2779
2780 // Navigates from fromId to toId/toObject.
2781 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
2782 int* WXUNUSED(toId), wxAccessible** toObject)
2783 {
2784 wxASSERT( GetWindow() != NULL );
2785 if (!GetWindow())
2786 return wxACC_FAIL;
2787
2788 switch (navDir)
2789 {
2790 case wxNAVDIR_FIRSTCHILD:
2791 {
2792 if (GetWindow()->GetChildren().GetCount() == 0)
2793 return wxACC_FALSE;
2794 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
2795 *toObject = childWindow->GetOrCreateAccessible();
2796
2797 return wxACC_OK;
2798 }
2799 case wxNAVDIR_LASTCHILD:
2800 {
2801 if (GetWindow()->GetChildren().GetCount() == 0)
2802 return wxACC_FALSE;
2803 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
2804 *toObject = childWindow->GetOrCreateAccessible();
2805
2806 return wxACC_OK;
2807 }
2808 case wxNAVDIR_RIGHT:
2809 case wxNAVDIR_DOWN:
2810 case wxNAVDIR_NEXT:
2811 {
2812 wxWindowList::compatibility_iterator node =
2813 wxWindowList::compatibility_iterator();
2814 if (fromId == 0)
2815 {
2816 // Can't navigate to sibling of this window
2817 // if we're a top-level window.
2818 if (!GetWindow()->GetParent())
2819 return wxACC_NOT_IMPLEMENTED;
2820
2821 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2822 }
2823 else
2824 {
2825 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2826 node = GetWindow()->GetChildren().Item(fromId-1);
2827 }
2828
2829 if (node && node->GetNext())
2830 {
2831 wxWindow* nextWindow = node->GetNext()->GetData();
2832 *toObject = nextWindow->GetOrCreateAccessible();
2833 return wxACC_OK;
2834 }
2835 else
2836 return wxACC_FALSE;
2837 }
2838 case wxNAVDIR_LEFT:
2839 case wxNAVDIR_UP:
2840 case wxNAVDIR_PREVIOUS:
2841 {
2842 wxWindowList::compatibility_iterator node =
2843 wxWindowList::compatibility_iterator();
2844 if (fromId == 0)
2845 {
2846 // Can't navigate to sibling of this window
2847 // if we're a top-level window.
2848 if (!GetWindow()->GetParent())
2849 return wxACC_NOT_IMPLEMENTED;
2850
2851 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2852 }
2853 else
2854 {
2855 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2856 node = GetWindow()->GetChildren().Item(fromId-1);
2857 }
2858
2859 if (node && node->GetPrevious())
2860 {
2861 wxWindow* previousWindow = node->GetPrevious()->GetData();
2862 *toObject = previousWindow->GetOrCreateAccessible();
2863 return wxACC_OK;
2864 }
2865 else
2866 return wxACC_FALSE;
2867 }
2868 }
2869
2870 return wxACC_NOT_IMPLEMENTED;
2871 }
2872
2873 // Gets the name of the specified object.
2874 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
2875 {
2876 wxASSERT( GetWindow() != NULL );
2877 if (!GetWindow())
2878 return wxACC_FAIL;
2879
2880 wxString title;
2881
2882 // If a child, leave wxWidgets to call the function on the actual
2883 // child object.
2884 if (childId > 0)
2885 return wxACC_NOT_IMPLEMENTED;
2886
2887 // This will eventually be replaced by specialised
2888 // accessible classes, one for each kind of wxWidgets
2889 // control or window.
2890 #if wxUSE_BUTTON
2891 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
2892 title = ((wxButton*) GetWindow())->GetLabel();
2893 else
2894 #endif
2895 title = GetWindow()->GetName();
2896
2897 if (!title.empty())
2898 {
2899 *name = title;
2900 return wxACC_OK;
2901 }
2902 else
2903 return wxACC_NOT_IMPLEMENTED;
2904 }
2905
2906 // Gets the number of children.
2907 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
2908 {
2909 wxASSERT( GetWindow() != NULL );
2910 if (!GetWindow())
2911 return wxACC_FAIL;
2912
2913 *childId = (int) GetWindow()->GetChildren().GetCount();
2914 return wxACC_OK;
2915 }
2916
2917 // Gets the specified child (starting from 1).
2918 // If *child is NULL and return value is wxACC_OK,
2919 // this means that the child is a simple element and
2920 // not an accessible object.
2921 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
2922 {
2923 wxASSERT( GetWindow() != NULL );
2924 if (!GetWindow())
2925 return wxACC_FAIL;
2926
2927 if (childId == 0)
2928 {
2929 *child = this;
2930 return wxACC_OK;
2931 }
2932
2933 if (childId > (int) GetWindow()->GetChildren().GetCount())
2934 return wxACC_FAIL;
2935
2936 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
2937 *child = childWindow->GetOrCreateAccessible();
2938 if (*child)
2939 return wxACC_OK;
2940 else
2941 return wxACC_FAIL;
2942 }
2943
2944 // Gets the parent, or NULL.
2945 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
2946 {
2947 wxASSERT( GetWindow() != NULL );
2948 if (!GetWindow())
2949 return wxACC_FAIL;
2950
2951 wxWindow* parentWindow = GetWindow()->GetParent();
2952 if (!parentWindow)
2953 {
2954 *parent = NULL;
2955 return wxACC_OK;
2956 }
2957 else
2958 {
2959 *parent = parentWindow->GetOrCreateAccessible();
2960 if (*parent)
2961 return wxACC_OK;
2962 else
2963 return wxACC_FAIL;
2964 }
2965 }
2966
2967 // Performs the default action. childId is 0 (the action for this object)
2968 // or > 0 (the action for a child).
2969 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2970 // window (e.g. an edit control).
2971 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
2972 {
2973 wxASSERT( GetWindow() != NULL );
2974 if (!GetWindow())
2975 return wxACC_FAIL;
2976
2977 return wxACC_NOT_IMPLEMENTED;
2978 }
2979
2980 // Gets the default action for this object (0) or > 0 (the action for a child).
2981 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2982 // string if there is no action.
2983 // The retrieved string describes the action that is performed on an object,
2984 // not what the object does as a result. For example, a toolbar button that prints
2985 // a document has a default action of "Press" rather than "Prints the current document."
2986 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
2987 {
2988 wxASSERT( GetWindow() != NULL );
2989 if (!GetWindow())
2990 return wxACC_FAIL;
2991
2992 return wxACC_NOT_IMPLEMENTED;
2993 }
2994
2995 // Returns the description for this object or a child.
2996 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
2997 {
2998 wxASSERT( GetWindow() != NULL );
2999 if (!GetWindow())
3000 return wxACC_FAIL;
3001
3002 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3003 if (!ht.empty())
3004 {
3005 *description = ht;
3006 return wxACC_OK;
3007 }
3008 return wxACC_NOT_IMPLEMENTED;
3009 }
3010
3011 // Returns help text for this object or a child, similar to tooltip text.
3012 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
3013 {
3014 wxASSERT( GetWindow() != NULL );
3015 if (!GetWindow())
3016 return wxACC_FAIL;
3017
3018 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3019 if (!ht.empty())
3020 {
3021 *helpText = ht;
3022 return wxACC_OK;
3023 }
3024 return wxACC_NOT_IMPLEMENTED;
3025 }
3026
3027 // Returns the keyboard shortcut for this object or child.
3028 // Return e.g. ALT+K
3029 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
3030 {
3031 wxASSERT( GetWindow() != NULL );
3032 if (!GetWindow())
3033 return wxACC_FAIL;
3034
3035 return wxACC_NOT_IMPLEMENTED;
3036 }
3037
3038 // Returns a role constant.
3039 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
3040 {
3041 wxASSERT( GetWindow() != NULL );
3042 if (!GetWindow())
3043 return wxACC_FAIL;
3044
3045 // If a child, leave wxWidgets to call the function on the actual
3046 // child object.
3047 if (childId > 0)
3048 return wxACC_NOT_IMPLEMENTED;
3049
3050 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3051 return wxACC_NOT_IMPLEMENTED;
3052 #if wxUSE_STATUSBAR
3053 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3054 return wxACC_NOT_IMPLEMENTED;
3055 #endif
3056 #if wxUSE_TOOLBAR
3057 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3058 return wxACC_NOT_IMPLEMENTED;
3059 #endif
3060
3061 //*role = wxROLE_SYSTEM_CLIENT;
3062 *role = wxROLE_SYSTEM_CLIENT;
3063 return wxACC_OK;
3064
3065 #if 0
3066 return wxACC_NOT_IMPLEMENTED;
3067 #endif
3068 }
3069
3070 // Returns a state constant.
3071 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
3072 {
3073 wxASSERT( GetWindow() != NULL );
3074 if (!GetWindow())
3075 return wxACC_FAIL;
3076
3077 // If a child, leave wxWidgets to call the function on the actual
3078 // child object.
3079 if (childId > 0)
3080 return wxACC_NOT_IMPLEMENTED;
3081
3082 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3083 return wxACC_NOT_IMPLEMENTED;
3084
3085 #if wxUSE_STATUSBAR
3086 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3087 return wxACC_NOT_IMPLEMENTED;
3088 #endif
3089 #if wxUSE_TOOLBAR
3090 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3091 return wxACC_NOT_IMPLEMENTED;
3092 #endif
3093
3094 *state = 0;
3095 return wxACC_OK;
3096
3097 #if 0
3098 return wxACC_NOT_IMPLEMENTED;
3099 #endif
3100 }
3101
3102 // Returns a localized string representing the value for the object
3103 // or child.
3104 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
3105 {
3106 wxASSERT( GetWindow() != NULL );
3107 if (!GetWindow())
3108 return wxACC_FAIL;
3109
3110 return wxACC_NOT_IMPLEMENTED;
3111 }
3112
3113 // Selects the object or child.
3114 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
3115 {
3116 wxASSERT( GetWindow() != NULL );
3117 if (!GetWindow())
3118 return wxACC_FAIL;
3119
3120 return wxACC_NOT_IMPLEMENTED;
3121 }
3122
3123 // Gets the window with the keyboard focus.
3124 // If childId is 0 and child is NULL, no object in
3125 // this subhierarchy has the focus.
3126 // If this object has the focus, child should be 'this'.
3127 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
3128 {
3129 wxASSERT( GetWindow() != NULL );
3130 if (!GetWindow())
3131 return wxACC_FAIL;
3132
3133 return wxACC_NOT_IMPLEMENTED;
3134 }
3135
3136 #if wxUSE_VARIANT
3137 // Gets a variant representing the selected children
3138 // of this object.
3139 // Acceptable values:
3140 // - a null variant (IsNull() returns true)
3141 // - a list variant (GetType() == wxT("list")
3142 // - an integer representing the selected child element,
3143 // or 0 if this object is selected (GetType() == wxT("long")
3144 // - a "void*" pointer to a wxAccessible child object
3145 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
3146 {
3147 wxASSERT( GetWindow() != NULL );
3148 if (!GetWindow())
3149 return wxACC_FAIL;
3150
3151 return wxACC_NOT_IMPLEMENTED;
3152 }
3153 #endif // wxUSE_VARIANT
3154
3155 #endif // wxUSE_ACCESSIBILITY
3156
3157 // ----------------------------------------------------------------------------
3158 // RTL support
3159 // ----------------------------------------------------------------------------
3160
3161 wxCoord
3162 wxWindowBase::AdjustForLayoutDirection(wxCoord x,
3163 wxCoord width,
3164 wxCoord widthTotal) const
3165 {
3166 if ( GetLayoutDirection() == wxLayout_RightToLeft )
3167 {
3168 x = widthTotal - x - width;
3169 }
3170
3171 return x;
3172 }
3173
3174 // ----------------------------------------------------------------------------
3175 // Window (and menu items) identifiers management
3176 // ----------------------------------------------------------------------------
3177
3178 namespace
3179 {
3180
3181 // this array contains, in packed form, the "in use" flags for the entire
3182 // auto-generated ids range: N-th element of the array contains the flags for
3183 // ids in [wxID_AUTO_LOWEST + 8*N, wxID_AUTO_LOWEST + 8*N + 7] range
3184 //
3185 // initially no ids are in use and we allocate them consecutively, but after we
3186 // exhaust the entire range, we wrap around and reuse the ids freed in the
3187 // meanwhile
3188 wxByte gs_autoIdsInUse[(wxID_AUTO_HIGHEST - wxID_AUTO_LOWEST + 1)/8 + 1] = { 0 };
3189
3190 // this is an optimization used until we wrap around wxID_AUTO_HIGHEST: if this
3191 // value is < wxID_AUTO_HIGHEST we know that we haven't wrapped yet and so can
3192 // allocate the ids simply by incrementing it
3193 static wxWindowID gs_nextControlId = wxID_AUTO_LOWEST;
3194
3195 void MarkAutoIdUsed(wxWindowID id)
3196 {
3197 id -= wxID_AUTO_LOWEST;
3198
3199 const int theByte = id / 8;
3200 const int theBit = id % 8;
3201
3202 gs_autoIdsInUse[theByte] |= 1 << theBit;
3203 }
3204
3205 void FreeAutoId(wxWindowID id)
3206 {
3207 id -= wxID_AUTO_LOWEST;
3208
3209 const int theByte = id / 8;
3210 const int theBit = id % 8;
3211
3212 gs_autoIdsInUse[theByte] &= ~(1 << theBit);
3213 }
3214
3215 bool IsAutoIdInUse(wxWindowID id)
3216 {
3217 id -= wxID_AUTO_LOWEST;
3218
3219 const int theByte = id / 8;
3220 const int theBit = id % 8;
3221
3222 return (gs_autoIdsInUse[theByte] & (1 << theBit)) != 0;
3223 }
3224
3225 } // anonymous namespace
3226
3227
3228 /* static */
3229 bool wxWindowBase::IsAutoGeneratedId(wxWindowID id)
3230 {
3231 if ( id < wxID_AUTO_LOWEST || id > wxID_AUTO_HIGHEST )
3232 return false;
3233
3234 // we shouldn't have any stray ids in this range
3235 wxASSERT_MSG( IsAutoIdInUse(id), "unused automatically generated id?" );
3236
3237 return true;
3238 }
3239
3240 wxWindowID wxWindowBase::NewControlId(int count)
3241 {
3242 wxASSERT_MSG( count > 0, "can't allocate less than 1 id" );
3243
3244 if ( gs_nextControlId + count - 1 <= wxID_AUTO_HIGHEST )
3245 {
3246 // we haven't wrapped yet, so we can just grab the next count ids
3247 wxWindowID id = gs_nextControlId;
3248
3249 while ( count-- )
3250 MarkAutoIdUsed(gs_nextControlId++);
3251
3252 return id;
3253 }
3254 else // we've already wrapped or are now going to
3255 {
3256 // brute-force search for the id values
3257
3258 // number of consecutive free ids found so far
3259 int found = 0;
3260
3261 for ( wxWindowID id = wxID_AUTO_LOWEST; id <= wxID_AUTO_HIGHEST; id++ )
3262 {
3263 if ( !IsAutoIdInUse(id) )
3264 {
3265 // found another consecutive available id
3266 found++;
3267 if ( found == count )
3268 {
3269 // mark all count consecutive free ids we found as being in
3270 // use now and rewind back to the start of available range
3271 // in the process
3272 while ( count-- )
3273 MarkAutoIdUsed(id--);
3274
3275 return id;
3276 }
3277 }
3278 else // this id is in use
3279 {
3280 // reset the number of consecutive free values found
3281 found = 0;
3282 }
3283 }
3284 }
3285
3286 // if we get here, there are not enough consecutive free ids
3287 return wxID_NONE;
3288 }
3289
3290 void wxWindowBase::ReleaseControlId(wxWindowID id)
3291 {
3292 wxCHECK_RET( IsAutoGeneratedId(id), "can't release non auto-generated id" );
3293
3294 FreeAutoId(id);
3295 }