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