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