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