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