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