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