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