]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
26c63a13f5e14b8b6467d134ac3ffbde1ebb551b
[wxWidgets.git] / src / common / wincmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "windowbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/frame.h"
36 #include "wx/defs.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/statbox.h"
42 #include "wx/textctrl.h"
43 #include "wx/settings.h"
44 #include "wx/dialog.h"
45 #include "wx/msgdlg.h"
46 #include "wx/statusbr.h"
47 #include "wx/dcclient.h"
48 #endif //WX_PRECOMP
49
50 #if defined(__WXMAC__) && wxUSE_SCROLLBAR
51 #include "wx/scrolbar.h"
52 #endif
53
54 #if wxUSE_CONSTRAINTS
55 #include "wx/layout.h"
56 #endif // wxUSE_CONSTRAINTS
57
58 #include "wx/sizer.h"
59
60 #if wxUSE_DRAG_AND_DROP
61 #include "wx/dnd.h"
62 #endif // wxUSE_DRAG_AND_DROP
63
64 #if wxUSE_ACCESSIBILITY
65 #include "wx/access.h"
66 #endif
67
68 #if wxUSE_HELP
69 #include "wx/cshelp.h"
70 #endif // wxUSE_HELP
71
72 #if wxUSE_TOOLTIPS
73 #include "wx/tooltip.h"
74 #endif // wxUSE_TOOLTIPS
75
76 #if wxUSE_CARET
77 #include "wx/caret.h"
78 #endif // wxUSE_CARET
79
80 // ----------------------------------------------------------------------------
81 // static data
82 // ----------------------------------------------------------------------------
83
84 #if defined(__WXPM__)
85 int wxWindowBase::ms_lastControlId = 2000;
86 #else
87 int wxWindowBase::ms_lastControlId = -200;
88 #endif
89
90 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
91
92 // ----------------------------------------------------------------------------
93 // event table
94 // ----------------------------------------------------------------------------
95
96 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
97 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
98 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
99 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
100
101 #if wxUSE_HELP
102 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
103 #endif // wxUSE_HELP
104
105 END_EVENT_TABLE()
106
107 // ============================================================================
108 // implementation of the common functionality of the wxWindow class
109 // ============================================================================
110
111 // ----------------------------------------------------------------------------
112 // initialization
113 // ----------------------------------------------------------------------------
114
115 // the default initialization
116 wxWindowBase::wxWindowBase()
117 {
118 // no window yet, no parent nor children
119 m_parent = (wxWindow *)NULL;
120 m_windowId = wxID_ANY;
121
122 // no constraints on the minimal window size
123 m_minWidth =
124 m_maxWidth = wxDefaultSize.x;
125 m_minHeight =
126 m_maxHeight = wxDefaultSize.y;
127
128 // window are created enabled and visible by default
129 m_isShown =
130 m_isEnabled = true;
131
132 // the default event handler is just this window
133 m_eventHandler = this;
134
135 #if wxUSE_VALIDATORS
136 // no validator
137 m_windowValidator = (wxValidator *) NULL;
138 #endif // wxUSE_VALIDATORS
139
140 // the colours/fonts are default for now, so leave m_font,
141 // m_backgroundColour and m_foregroundColour uninitialized and set those
142 m_hasBgCol =
143 m_hasFgCol =
144 m_hasFont = false;
145
146 // no style bits
147 m_exStyle =
148 m_windowStyle = 0;
149
150 #if wxUSE_CONSTRAINTS
151 // no constraints whatsoever
152 m_constraints = (wxLayoutConstraints *) NULL;
153 m_constraintsInvolvedIn = (wxWindowList *) NULL;
154 #endif // wxUSE_CONSTRAINTS
155
156 m_windowSizer = (wxSizer *) NULL;
157 m_containingSizer = (wxSizer *) NULL;
158 m_autoLayout = false;
159
160 #if wxUSE_DRAG_AND_DROP
161 m_dropTarget = (wxDropTarget *)NULL;
162 #endif // wxUSE_DRAG_AND_DROP
163
164 #if wxUSE_TOOLTIPS
165 m_tooltip = (wxToolTip *)NULL;
166 #endif // wxUSE_TOOLTIPS
167
168 #if wxUSE_CARET
169 m_caret = (wxCaret *)NULL;
170 #endif // wxUSE_CARET
171
172 #if wxUSE_PALETTE
173 m_hasCustomPalette = false;
174 #endif // wxUSE_PALETTE
175
176 #if wxUSE_ACCESSIBILITY
177 m_accessible = NULL;
178 #endif
179
180 m_virtualSize = wxDefaultSize;
181
182 m_minVirtualWidth =
183 m_maxVirtualWidth = wxDefaultSize.x;
184 m_minVirtualHeight =
185 m_maxVirtualHeight = wxDefaultSize.y;
186
187 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
188
189 // Whether we're using the current theme for this window (wxGTK only for now)
190 m_themeEnabled = false;
191
192 // VZ: this one shouldn't exist...
193 m_isBeingDeleted = false;
194 }
195
196 // common part of window creation process
197 bool wxWindowBase::CreateBase(wxWindowBase *parent,
198 wxWindowID id,
199 const wxPoint& WXUNUSED(pos),
200 const wxSize& WXUNUSED(size),
201 long style,
202 const wxValidator& wxVALIDATOR_PARAM(validator),
203 const wxString& name)
204 {
205 #if wxUSE_STATBOX
206 // wxGTK doesn't allow to create controls with static box as the parent so
207 // this will result in a crash when the program is ported to wxGTK so warn
208 // the user about it
209
210 // if you get this assert, the correct solution is to create the controls
211 // as siblings of the static box
212 wxASSERT_MSG( !parent || !wxDynamicCast(parent, wxStaticBox),
213 _T("wxStaticBox can't be used as a window parent!") );
214 #endif // wxUSE_STATBOX
215
216 // ids are limited to 16 bits under MSW so if you care about portability,
217 // it's not a good idea to use ids out of this range (and negative ids are
218 // reserved for wxWidgets own usage)
219 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767),
220 _T("invalid id value") );
221
222 // generate a new id if the user doesn't care about it
223 m_windowId = id == wxID_ANY ? NewControlId() : id;
224
225 SetName(name);
226 SetWindowStyleFlag(style);
227 SetParent(parent);
228
229 #if wxUSE_VALIDATORS
230 SetValidator(validator);
231 #endif // wxUSE_VALIDATORS
232
233 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
234 // have it too - like this it's possible to set it only in the top level
235 // dialog/frame and all children will inherit it by defult
236 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
237 {
238 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
239 }
240
241 return true;
242 }
243
244 // ----------------------------------------------------------------------------
245 // destruction
246 // ----------------------------------------------------------------------------
247
248 // common clean up
249 wxWindowBase::~wxWindowBase()
250 {
251 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
252
253 // FIXME if these 2 cases result from programming errors in the user code
254 // we should probably assert here instead of silently fixing them
255
256 // Just in case the window has been Closed, but we're then deleting
257 // immediately: don't leave dangling pointers.
258 wxPendingDelete.DeleteObject(this);
259
260 // Just in case we've loaded a top-level window via LoadNativeDialog but
261 // we weren't a dialog class
262 wxTopLevelWindows.DeleteObject((wxWindow*)this);
263
264 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
265
266 // reset the dangling pointer our parent window may keep to us
267 if ( m_parent )
268 {
269 if ( m_parent->GetDefaultItem() == this )
270 {
271 m_parent->SetDefaultItem(NULL);
272 }
273
274 m_parent->RemoveChild(this);
275 }
276
277 #if wxUSE_CARET
278 delete m_caret;
279 #endif // wxUSE_CARET
280
281 #if wxUSE_VALIDATORS
282 delete m_windowValidator;
283 #endif // wxUSE_VALIDATORS
284
285 #if wxUSE_CONSTRAINTS
286 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
287 // at deleted windows as they delete themselves.
288 DeleteRelatedConstraints();
289
290 if ( m_constraints )
291 {
292 // This removes any dangling pointers to this window in other windows'
293 // constraintsInvolvedIn lists.
294 UnsetConstraints(m_constraints);
295 delete m_constraints;
296 m_constraints = NULL;
297 }
298 #endif // wxUSE_CONSTRAINTS
299
300 if ( m_containingSizer )
301 m_containingSizer->Detach( (wxWindow*)this );
302
303 delete m_windowSizer;
304
305 #if wxUSE_DRAG_AND_DROP
306 delete m_dropTarget;
307 #endif // wxUSE_DRAG_AND_DROP
308
309 #if wxUSE_TOOLTIPS
310 delete m_tooltip;
311 #endif // wxUSE_TOOLTIPS
312
313 #if wxUSE_ACCESSIBILITY
314 delete m_accessible;
315 #endif
316 }
317
318 bool wxWindowBase::Destroy()
319 {
320 delete this;
321
322 return true;
323 }
324
325 bool wxWindowBase::Close(bool force)
326 {
327 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
328 event.SetEventObject(this);
329 event.SetCanVeto(!force);
330
331 // return false if window wasn't closed because the application vetoed the
332 // close event
333 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
334 }
335
336 bool wxWindowBase::DestroyChildren()
337 {
338 wxWindowList::compatibility_iterator node;
339 for ( ;; )
340 {
341 // we iterate until the list becomes empty
342 node = GetChildren().GetFirst();
343 if ( !node )
344 break;
345
346 wxWindow *child = node->GetData();
347
348 // note that we really want to call delete and not ->Destroy() here
349 // because we want to delete the child immediately, before we are
350 // deleted, and delayed deletion would result in problems as our (top
351 // level) child could outlive its parent
352 delete child;
353
354 wxASSERT_MSG( !GetChildren().Find(child),
355 wxT("child didn't remove itself using RemoveChild()") );
356 }
357
358 return true;
359 }
360
361 // ----------------------------------------------------------------------------
362 // size/position related methods
363 // ----------------------------------------------------------------------------
364
365 // centre the window with respect to its parent in either (or both) directions
366 void wxWindowBase::Centre(int direction)
367 {
368 // the position/size of the parent window or of the entire screen
369 wxPoint posParent;
370 int widthParent, heightParent;
371
372 wxWindow *parent = NULL;
373
374 if ( !(direction & wxCENTRE_ON_SCREEN) )
375 {
376 // find the parent to centre this window on: it should be the
377 // immediate parent for the controls but the top level parent for the
378 // top level windows (like dialogs)
379 parent = GetParent();
380 if ( IsTopLevel() )
381 {
382 while ( parent && !parent->IsTopLevel() )
383 {
384 parent = parent->GetParent();
385 }
386 }
387
388 // there is no wxTopLevelWindow under wxMotif yet
389 #ifndef __WXMOTIF__
390 // we shouldn't center the dialog on the iconized window: under
391 // Windows, for example, this places it completely off the screen
392 if ( parent )
393 {
394 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
395 if ( winTop && winTop->IsIconized() )
396 {
397 parent = NULL;
398 }
399 }
400 #endif // __WXMOTIF__
401
402 // did we find the parent?
403 if ( !parent )
404 {
405 // no other choice
406 direction |= wxCENTRE_ON_SCREEN;
407 }
408 }
409
410 if ( direction & wxCENTRE_ON_SCREEN )
411 {
412 // centre with respect to the whole screen
413 wxDisplaySize(&widthParent, &heightParent);
414 }
415 else
416 {
417 if ( IsTopLevel() )
418 {
419 // centre on the parent
420 parent->GetSize(&widthParent, &heightParent);
421
422 // adjust to the parents position
423 posParent = parent->GetPosition();
424 }
425 else
426 {
427 // centre inside the parents client rectangle
428 parent->GetClientSize(&widthParent, &heightParent);
429 }
430 }
431
432 int width, height;
433 GetSize(&width, &height);
434
435 int xNew = wxDefaultPosition.x,
436 yNew = wxDefaultPosition.y;
437
438 if ( direction & wxHORIZONTAL )
439 xNew = (widthParent - width)/2;
440
441 if ( direction & wxVERTICAL )
442 yNew = (heightParent - height)/2;
443
444 xNew += posParent.x;
445 yNew += posParent.y;
446
447 // Base size of the visible dimensions of the display
448 // to take into account the taskbar
449 wxRect rect = wxGetClientDisplayRect();
450 wxSize size (rect.width,rect.height);
451
452 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
453 // but it may mean that the window is placed on other than the main
454 // display. Therefore we only make sure centered window is on the main display
455 // if the parent is at least partially present here.
456 if (posParent.x + widthParent >= 0) // if parent is (partially) on the main display
457 {
458 if (xNew < 0)
459 xNew = 0;
460 else if (xNew+width > size.x)
461 xNew = size.x-width-1;
462 }
463 if (posParent.y + heightParent >= 0) // if parent is (partially) on the main display
464 {
465 if (yNew+height > size.y)
466 yNew = size.y-height-1;
467
468 // Make certain that the title bar is initially visible
469 // always, even if this would push the bottom of the
470 // dialog of the visible area of the display
471 if (yNew < 0)
472 yNew = 0;
473 }
474
475 // move the window to this position (keeping the old size but using
476 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
477 SetSize(xNew, yNew, width, height, wxSIZE_ALLOW_MINUS_ONE);
478 }
479
480 // fits the window around the children
481 void wxWindowBase::Fit()
482 {
483 if ( GetChildren().GetCount() > 0 )
484 {
485 SetClientSize(DoGetBestSize());
486 }
487 //else: do nothing if we have no children
488 }
489
490 // fits virtual size (ie. scrolled area etc.) around children
491 void wxWindowBase::FitInside()
492 {
493 if ( GetChildren().GetCount() > 0 )
494 {
495 SetVirtualSize( GetBestVirtualSize() );
496 }
497 }
498
499 // On Mac, scrollbars are explicitly children.
500 #ifdef __WXMAC__
501 static bool wxHasRealChildren(const wxWindowBase* win)
502 {
503 int realChildCount = 0;
504
505 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
506 node;
507 node = node->GetNext() )
508 {
509 wxWindow *win = node->GetData();
510 if ( !win->IsTopLevel() && win->IsShown() && !win->IsKindOf(CLASSINFO(wxScrollBar)))
511 realChildCount ++;
512 }
513 return (realChildCount > 0);
514 }
515 #endif
516
517 // return the size best suited for the current window
518 wxSize wxWindowBase::DoGetBestSize() const
519 {
520 if ( m_windowSizer )
521 {
522 return m_windowSizer->GetMinSize();
523 }
524 #if wxUSE_CONSTRAINTS
525 else if ( m_constraints )
526 {
527 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
528
529 // our minimal acceptable size is such that all our windows fit inside
530 int maxX = 0,
531 maxY = 0;
532
533 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
534 node;
535 node = node->GetNext() )
536 {
537 wxLayoutConstraints *c = node->GetData()->GetConstraints();
538 if ( !c )
539 {
540 // it's not normal that we have an unconstrained child, but
541 // what can we do about it?
542 continue;
543 }
544
545 int x = c->right.GetValue(),
546 y = c->bottom.GetValue();
547
548 if ( x > maxX )
549 maxX = x;
550
551 if ( y > maxY )
552 maxY = y;
553
554 // TODO: we must calculate the overlaps somehow, otherwise we
555 // will never return a size bigger than the current one :-(
556 }
557
558 return wxSize(maxX, maxY);
559 }
560 #endif // wxUSE_CONSTRAINTS
561 else if ( !GetChildren().empty()
562 #ifdef __WXMAC__
563 && wxHasRealChildren(this)
564 #endif
565 )
566 {
567 // our minimal acceptable size is such that all our visible child windows fit inside
568 int maxX = 0,
569 maxY = 0;
570
571 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
572 node;
573 node = node->GetNext() )
574 {
575 wxWindow *win = node->GetData();
576 if ( win->IsTopLevel() || ( ! win->IsShown() )
577 #if wxUSE_STATUSBAR
578 || wxDynamicCast(win, wxStatusBar)
579 #endif // wxUSE_STATUSBAR
580 )
581 {
582 // dialogs and frames lie in different top level windows -
583 // don't deal with them here; as for the status bars, they
584 // don't lie in the client area at all
585 continue;
586 }
587
588 int wx, wy, ww, wh;
589 win->GetPosition(&wx, &wy);
590
591 // if the window hadn't been positioned yet, assume that it is in
592 // the origin
593 if ( wx == wxDefaultPosition.x )
594 wx = 0;
595 if ( wy == wxDefaultPosition.y )
596 wy = 0;
597
598 win->GetSize(&ww, &wh);
599 if ( wx + ww > maxX )
600 maxX = wx + ww;
601 if ( wy + wh > maxY )
602 maxY = wy + wh;
603 }
604
605 // for compatibility with the old versions and because it really looks
606 // slightly more pretty like this, add a pad
607 maxX += 7;
608 maxY += 14;
609
610 return wxSize(maxX, maxY);
611 }
612 else // ! has children
613 {
614 // for a generic window there is no natural best size - just use either the
615 // minimum size if there is one, or the current size
616 if ( GetMinSize().IsFullySpecified() )
617 return GetMinSize();
618 else
619 return GetSize();
620 }
621 }
622
623 void wxWindowBase::SetBestSize(const wxSize& size)
624 {
625 // If the given size is incomplete then merge with the best size.
626 wxSize sizeBest;
627 if ( size.x == wxDefaultSize.x || size.y == wxDefaultSize.y )
628 {
629 sizeBest = DoGetBestSize();
630 if ( size.x != wxDefaultSize.x )
631 sizeBest.x = size.x;
632 if ( size.y != wxDefaultSize.y )
633 sizeBest.y = size.y;
634 }
635 else // have complete explicit size
636 {
637 sizeBest = size;
638 }
639
640 // Change the size if needed
641 if (GetSize() != sizeBest)
642 SetSize(sizeBest);
643
644 // don't shrink the control below its best size
645 m_minWidth = sizeBest.x;
646 m_minHeight = sizeBest.y;
647 }
648
649 // by default the origin is not shifted
650 wxPoint wxWindowBase::GetClientAreaOrigin() const
651 {
652 return wxPoint(0, 0);
653 }
654
655 // set the min/max size of the window
656 void wxWindowBase::SetSizeHints(int minW, int minH,
657 int maxW, int maxH,
658 int WXUNUSED(incW), int WXUNUSED(incH))
659 {
660 // setting min width greater than max width leads to infinite loops under
661 // X11 and generally doesn't make any sense, so don't allow it
662 wxCHECK_RET( (minW == wxDefaultSize.x || maxW == wxDefaultSize.x || minW <= maxW) &&
663 (minH == wxDefaultSize.y || maxH == wxDefaultSize.y || minH <= maxH),
664 _T("min width/height must be less than max width/height!") );
665
666 m_minWidth = minW;
667 m_maxWidth = maxW;
668 m_minHeight = minH;
669 m_maxHeight = maxH;
670 }
671
672 void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
673 {
674 if ( m_windowVariant != variant )
675 {
676 m_windowVariant = variant;
677
678 DoSetWindowVariant(variant);
679 }
680 }
681
682 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
683 {
684 // adjust the font height to correspond to our new variant (notice that
685 // we're only called if something really changed)
686 wxFont font = GetFont();
687 int size = font.GetPointSize();
688 switch ( variant )
689 {
690 case wxWINDOW_VARIANT_NORMAL:
691 break;
692
693 case wxWINDOW_VARIANT_SMALL:
694 size *= 3;
695 size /= 4;
696 break;
697
698 case wxWINDOW_VARIANT_MINI:
699 size *= 2;
700 size /= 3;
701 break;
702
703 case wxWINDOW_VARIANT_LARGE:
704 size *= 5;
705 size /= 4;
706 break;
707
708 default:
709 wxFAIL_MSG(_T("unexpected window variant"));
710 break;
711 }
712
713 font.SetPointSize(size);
714 SetFont(font);
715 }
716
717 void wxWindowBase::SetVirtualSizeHints( int minW, int minH,
718 int maxW, int maxH )
719 {
720 m_minVirtualWidth = minW;
721 m_maxVirtualWidth = maxW;
722 m_minVirtualHeight = minH;
723 m_maxVirtualHeight = maxH;
724 }
725
726 void wxWindowBase::DoSetVirtualSize( int x, int y )
727 {
728 if ( m_minVirtualWidth != wxDefaultSize.x && m_minVirtualWidth > x )
729 x = m_minVirtualWidth;
730 if ( m_maxVirtualWidth != wxDefaultSize.x && m_maxVirtualWidth < x )
731 x = m_maxVirtualWidth;
732 if ( m_minVirtualHeight != wxDefaultSize.y && m_minVirtualHeight > y )
733 y = m_minVirtualHeight;
734 if ( m_maxVirtualHeight != wxDefaultSize.y && m_maxVirtualHeight < y )
735 y = m_maxVirtualHeight;
736
737 m_virtualSize = wxSize(x, y);
738 }
739
740 wxSize wxWindowBase::DoGetVirtualSize() const
741 {
742 wxSize s( GetClientSize() );
743
744 return wxSize( wxMax( m_virtualSize.GetWidth(), s.GetWidth() ),
745 wxMax( m_virtualSize.GetHeight(), s.GetHeight() ) );
746 }
747
748 // ----------------------------------------------------------------------------
749 // show/hide/enable/disable the window
750 // ----------------------------------------------------------------------------
751
752 bool wxWindowBase::Show(bool show)
753 {
754 if ( show != m_isShown )
755 {
756 m_isShown = show;
757
758 return true;
759 }
760 else
761 {
762 return false;
763 }
764 }
765
766 bool wxWindowBase::Enable(bool enable)
767 {
768 if ( enable != m_isEnabled )
769 {
770 m_isEnabled = enable;
771
772 return true;
773 }
774 else
775 {
776 return false;
777 }
778 }
779 // ----------------------------------------------------------------------------
780 // RTTI
781 // ----------------------------------------------------------------------------
782
783 bool wxWindowBase::IsTopLevel() const
784 {
785 return false;
786 }
787
788 // ----------------------------------------------------------------------------
789 // reparenting the window
790 // ----------------------------------------------------------------------------
791
792 void wxWindowBase::AddChild(wxWindowBase *child)
793 {
794 wxCHECK_RET( child, wxT("can't add a NULL child") );
795
796 // this should never happen and it will lead to a crash later if it does
797 // because RemoveChild() will remove only one node from the children list
798 // and the other(s) one(s) will be left with dangling pointers in them
799 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") );
800
801 GetChildren().Append((wxWindow*)child);
802 child->SetParent(this);
803 }
804
805 void wxWindowBase::RemoveChild(wxWindowBase *child)
806 {
807 wxCHECK_RET( child, wxT("can't remove a NULL child") );
808
809 GetChildren().DeleteObject((wxWindow *)child);
810 child->SetParent(NULL);
811 }
812
813 bool wxWindowBase::Reparent(wxWindowBase *newParent)
814 {
815 wxWindow *oldParent = GetParent();
816 if ( newParent == oldParent )
817 {
818 // nothing done
819 return false;
820 }
821
822 // unlink this window from the existing parent.
823 if ( oldParent )
824 {
825 oldParent->RemoveChild(this);
826 }
827 else
828 {
829 wxTopLevelWindows.DeleteObject((wxWindow *)this);
830 }
831
832 // add it to the new one
833 if ( newParent )
834 {
835 newParent->AddChild(this);
836 }
837 else
838 {
839 wxTopLevelWindows.Append((wxWindow *)this);
840 }
841
842 return true;
843 }
844
845 // ----------------------------------------------------------------------------
846 // event handler stuff
847 // ----------------------------------------------------------------------------
848
849 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
850 {
851 wxEvtHandler *handlerOld = GetEventHandler();
852
853 handler->SetNextHandler(handlerOld);
854
855 if ( handlerOld )
856 GetEventHandler()->SetPreviousHandler(handler);
857
858 SetEventHandler(handler);
859 }
860
861 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
862 {
863 wxEvtHandler *handlerA = GetEventHandler();
864 if ( handlerA )
865 {
866 wxEvtHandler *handlerB = handlerA->GetNextHandler();
867 handlerA->SetNextHandler((wxEvtHandler *)NULL);
868
869 if ( handlerB )
870 handlerB->SetPreviousHandler((wxEvtHandler *)NULL);
871 SetEventHandler(handlerB);
872
873 if ( deleteHandler )
874 {
875 delete handlerA;
876 handlerA = (wxEvtHandler *)NULL;
877 }
878 }
879
880 return handlerA;
881 }
882
883 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handler)
884 {
885 wxCHECK_MSG( handler, false, _T("RemoveEventHandler(NULL) called") );
886
887 wxEvtHandler *handlerPrev = NULL,
888 *handlerCur = GetEventHandler();
889 while ( handlerCur )
890 {
891 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
892
893 if ( handlerCur == handler )
894 {
895 if ( handlerPrev )
896 {
897 handlerPrev->SetNextHandler(handlerNext);
898 }
899 else
900 {
901 SetEventHandler(handlerNext);
902 }
903
904 if ( handlerNext )
905 {
906 handlerNext->SetPreviousHandler ( handlerPrev );
907 }
908
909 handler->SetNextHandler(NULL);
910 handler->SetPreviousHandler(NULL);
911
912 return true;
913 }
914
915 handlerPrev = handlerCur;
916 handlerCur = handlerNext;
917 }
918
919 wxFAIL_MSG( _T("where has the event handler gone?") );
920
921 return false;
922 }
923
924 // ----------------------------------------------------------------------------
925 // colours, fonts &c
926 // ----------------------------------------------------------------------------
927
928 void wxWindowBase::InheritAttributes()
929 {
930 const wxWindowBase * const parent = GetParent();
931 if ( !parent )
932 return;
933
934 // we only inherit attributes which had been explicitly set for the parent
935 // which ensures that this only happens if the user really wants it and
936 // not by default which wouldn't make any sense in modern GUIs where the
937 // controls don't all use the same fonts (nor colours)
938 if ( parent->m_hasFont && !m_hasFont )
939 SetFont(parent->GetFont());
940
941 // in addition, there is a possibility to explicitly forbid inheriting
942 // colours at each class level by overriding ShouldInheritColours()
943 if ( ShouldInheritColours() )
944 {
945 if ( parent->m_hasFgCol && !m_hasFgCol )
946 SetForegroundColour(parent->GetForegroundColour());
947
948 if ( parent->m_hasBgCol && !m_hasBgCol )
949 SetBackgroundColour(parent->GetBackgroundColour());
950 }
951 }
952
953 /* static */ wxVisualAttributes
954 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
955 {
956 // it is important to return valid values for all attributes from here,
957 // GetXXX() below rely on this
958 wxVisualAttributes attrs;
959 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
960 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
961 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
962
963 return attrs;
964 }
965
966 wxColour wxWindowBase::GetBackgroundColour() const
967 {
968 if ( !m_backgroundColour.Ok() )
969 {
970 wxASSERT_MSG( !m_hasBgCol, _T("we have invalid explicit bg colour?") );
971
972 // get our default background colour
973 wxColour colBg = GetDefaultAttributes().colBg;
974
975 // we must return some valid colour to avoid redoing this every time
976 // and also to avoid surprizing the applications written for older
977 // wxWidgets versions where GetBackgroundColour() always returned
978 // something -- so give them something even if it doesn't make sense
979 // for this window (e.g. it has a themed background)
980 if ( !colBg.Ok() )
981 colBg = GetClassDefaultAttributes().colBg;
982
983 // cache it for the next call
984 wxConstCast(this, wxWindowBase)->m_backgroundColour = colBg;
985 }
986
987 return m_backgroundColour;
988 }
989
990 wxColour wxWindowBase::GetForegroundColour() const
991 {
992 // logic is the same as above
993 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
994 {
995 wxASSERT_MSG( !m_hasFgCol, _T("we have invalid explicit fg colour?") );
996
997 wxColour colFg = GetDefaultAttributes().colFg;
998
999 if ( !colFg.Ok() )
1000 colFg = GetClassDefaultAttributes().colFg;
1001
1002 wxConstCast(this, wxWindowBase)->m_foregroundColour = colFg;
1003 }
1004
1005 return m_foregroundColour;
1006 }
1007
1008 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1009 {
1010 if ( colour == m_backgroundColour )
1011 return false;
1012
1013 m_hasBgCol = colour.Ok();
1014 m_backgroundColour = colour;
1015 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
1016 return true;
1017 }
1018
1019 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1020 {
1021 if (colour == m_foregroundColour )
1022 return false;
1023
1024 m_hasFgCol = colour.Ok();
1025 m_foregroundColour = colour;
1026 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
1027 return true;
1028 }
1029
1030 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1031 {
1032 // setting an invalid cursor is ok, it means that we don't have any special
1033 // cursor
1034 if ( m_cursor == cursor )
1035 {
1036 // no change
1037 return false;
1038 }
1039
1040 m_cursor = cursor;
1041
1042 return true;
1043 }
1044
1045 wxFont& wxWindowBase::DoGetFont() const
1046 {
1047 // logic is the same as in GetBackgroundColour()
1048 if ( !m_font.Ok() )
1049 {
1050 wxASSERT_MSG( !m_hasFont, _T("we have invalid explicit font?") );
1051
1052 wxFont font = GetDefaultAttributes().font;
1053 if ( !font.Ok() )
1054 font = GetClassDefaultAttributes().font;
1055
1056 wxConstCast(this, wxWindowBase)->m_font = font;
1057 }
1058
1059 // cast is here for non-const GetFont() convenience
1060 return wxConstCast(this, wxWindowBase)->m_font;
1061 }
1062
1063 bool wxWindowBase::SetFont(const wxFont& font)
1064 {
1065 if ( !font.Ok() )
1066 return false;
1067
1068 if ( font == m_font )
1069 {
1070 // no change
1071 return false;
1072 }
1073
1074 m_font = font;
1075
1076 m_hasFont = true;
1077
1078 return true;
1079 }
1080
1081 #if wxUSE_PALETTE
1082
1083 void wxWindowBase::SetPalette(const wxPalette& pal)
1084 {
1085 m_hasCustomPalette = true;
1086 m_palette = pal;
1087
1088 // VZ: can anyone explain me what do we do here?
1089 wxWindowDC d((wxWindow *) this);
1090 d.SetPalette(pal);
1091 }
1092
1093 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1094 {
1095 wxWindow *win = (wxWindow *)this;
1096 while ( win && !win->HasCustomPalette() )
1097 {
1098 win = win->GetParent();
1099 }
1100
1101 return win;
1102 }
1103
1104 #endif // wxUSE_PALETTE
1105
1106 #if wxUSE_CARET
1107 void wxWindowBase::SetCaret(wxCaret *caret)
1108 {
1109 if ( m_caret )
1110 {
1111 delete m_caret;
1112 }
1113
1114 m_caret = caret;
1115
1116 if ( m_caret )
1117 {
1118 wxASSERT_MSG( m_caret->GetWindow() == this,
1119 wxT("caret should be created associated to this window") );
1120 }
1121 }
1122 #endif // wxUSE_CARET
1123
1124 #if wxUSE_VALIDATORS
1125 // ----------------------------------------------------------------------------
1126 // validators
1127 // ----------------------------------------------------------------------------
1128
1129 void wxWindowBase::SetValidator(const wxValidator& validator)
1130 {
1131 if ( m_windowValidator )
1132 delete m_windowValidator;
1133
1134 m_windowValidator = (wxValidator *)validator.Clone();
1135
1136 if ( m_windowValidator )
1137 m_windowValidator->SetWindow(this);
1138 }
1139 #endif // wxUSE_VALIDATORS
1140
1141 // ----------------------------------------------------------------------------
1142 // update region stuff
1143 // ----------------------------------------------------------------------------
1144
1145 wxRect wxWindowBase::GetUpdateClientRect() const
1146 {
1147 wxRegion rgnUpdate = GetUpdateRegion();
1148 rgnUpdate.Intersect(GetClientRect());
1149 wxRect rectUpdate = rgnUpdate.GetBox();
1150 wxPoint ptOrigin = GetClientAreaOrigin();
1151 rectUpdate.x -= ptOrigin.x;
1152 rectUpdate.y -= ptOrigin.y;
1153
1154 return rectUpdate;
1155 }
1156
1157 bool wxWindowBase::IsExposed(int x, int y) const
1158 {
1159 return m_updateRegion.Contains(x, y) != wxOutRegion;
1160 }
1161
1162 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
1163 {
1164 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1165 }
1166
1167 void wxWindowBase::ClearBackground()
1168 {
1169 // wxGTK uses its own version, no need to add never used code
1170 #ifndef __WXGTK__
1171 wxClientDC dc((wxWindow *)this);
1172 wxBrush brush(GetBackgroundColour(), wxSOLID);
1173 dc.SetBackground(brush);
1174 dc.Clear();
1175 #endif // __WXGTK__
1176 }
1177
1178 // ----------------------------------------------------------------------------
1179 // find child window by id or name
1180 // ----------------------------------------------------------------------------
1181
1182 wxWindow *wxWindowBase::FindWindow( long id )
1183 {
1184 if ( id == m_windowId )
1185 return (wxWindow *)this;
1186
1187 wxWindowBase *res = (wxWindow *)NULL;
1188 wxWindowList::compatibility_iterator node;
1189 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1190 {
1191 wxWindowBase *child = node->GetData();
1192 res = child->FindWindow( id );
1193 }
1194
1195 return (wxWindow *)res;
1196 }
1197
1198 wxWindow *wxWindowBase::FindWindow( const wxString& name )
1199 {
1200 if ( name == m_windowName )
1201 return (wxWindow *)this;
1202
1203 wxWindowBase *res = (wxWindow *)NULL;
1204 wxWindowList::compatibility_iterator node;
1205 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1206 {
1207 wxWindow *child = node->GetData();
1208 res = child->FindWindow(name);
1209 }
1210
1211 return (wxWindow *)res;
1212 }
1213
1214
1215 // find any window by id or name or label: If parent is non-NULL, look through
1216 // children for a label or title matching the specified string. If NULL, look
1217 // through all top-level windows.
1218 //
1219 // to avoid duplicating code we reuse the same helper function but with
1220 // different comparators
1221
1222 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1223 const wxString& label, long id);
1224
1225 static
1226 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1227 long WXUNUSED(id))
1228 {
1229 return win->GetLabel() == label;
1230 }
1231
1232 static
1233 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1234 long WXUNUSED(id))
1235 {
1236 return win->GetName() == label;
1237 }
1238
1239 static
1240 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1241 long id)
1242 {
1243 return win->GetId() == id;
1244 }
1245
1246 // recursive helper for the FindWindowByXXX() functions
1247 static
1248 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1249 const wxString& label,
1250 long id,
1251 wxFindWindowCmp cmp)
1252 {
1253 if ( parent )
1254 {
1255 // see if this is the one we're looking for
1256 if ( (*cmp)(parent, label, id) )
1257 return (wxWindow *)parent;
1258
1259 // It wasn't, so check all its children
1260 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1261 node;
1262 node = node->GetNext() )
1263 {
1264 // recursively check each child
1265 wxWindow *win = (wxWindow *)node->GetData();
1266 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1267 if (retwin)
1268 return retwin;
1269 }
1270 }
1271
1272 // Not found
1273 return NULL;
1274 }
1275
1276 // helper for FindWindowByXXX()
1277 static
1278 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1279 const wxString& label,
1280 long id,
1281 wxFindWindowCmp cmp)
1282 {
1283 if ( parent )
1284 {
1285 // just check parent and all its children
1286 return wxFindWindowRecursively(parent, label, id, cmp);
1287 }
1288
1289 // start at very top of wx's windows
1290 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1291 node;
1292 node = node->GetNext() )
1293 {
1294 // recursively check each window & its children
1295 wxWindow *win = node->GetData();
1296 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1297 if (retwin)
1298 return retwin;
1299 }
1300
1301 return NULL;
1302 }
1303
1304 /* static */
1305 wxWindow *
1306 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1307 {
1308 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1309 }
1310
1311 /* static */
1312 wxWindow *
1313 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1314 {
1315 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1316
1317 if ( !win )
1318 {
1319 // fall back to the label
1320 win = FindWindowByLabel(title, parent);
1321 }
1322
1323 return win;
1324 }
1325
1326 /* static */
1327 wxWindow *
1328 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1329 {
1330 return wxFindWindowHelper(parent, _T(""), id, wxFindWindowCmpIds);
1331 }
1332
1333 // ----------------------------------------------------------------------------
1334 // dialog oriented functions
1335 // ----------------------------------------------------------------------------
1336
1337 void wxWindowBase::MakeModal(bool modal)
1338 {
1339 // Disable all other windows
1340 if ( IsTopLevel() )
1341 {
1342 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1343 while (node)
1344 {
1345 wxWindow *win = node->GetData();
1346 if (win != this)
1347 win->Enable(!modal);
1348
1349 node = node->GetNext();
1350 }
1351 }
1352 }
1353
1354 bool wxWindowBase::Validate()
1355 {
1356 #if wxUSE_VALIDATORS
1357 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1358
1359 wxWindowList::compatibility_iterator node;
1360 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1361 {
1362 wxWindowBase *child = node->GetData();
1363 wxValidator *validator = child->GetValidator();
1364 if ( validator && !validator->Validate((wxWindow *)this) )
1365 {
1366 return false;
1367 }
1368
1369 if ( recurse && !child->Validate() )
1370 {
1371 return false;
1372 }
1373 }
1374 #endif // wxUSE_VALIDATORS
1375
1376 return true;
1377 }
1378
1379 bool wxWindowBase::TransferDataToWindow()
1380 {
1381 #if wxUSE_VALIDATORS
1382 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1383
1384 wxWindowList::compatibility_iterator node;
1385 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1386 {
1387 wxWindowBase *child = node->GetData();
1388 wxValidator *validator = child->GetValidator();
1389 if ( validator && !validator->TransferToWindow() )
1390 {
1391 wxLogWarning(_("Could not transfer data to window"));
1392 #if wxUSE_LOG
1393 wxLog::FlushActive();
1394 #endif // wxUSE_LOG
1395
1396 return false;
1397 }
1398
1399 if ( recurse )
1400 {
1401 if ( !child->TransferDataToWindow() )
1402 {
1403 // warning already given
1404 return false;
1405 }
1406 }
1407 }
1408 #endif // wxUSE_VALIDATORS
1409
1410 return true;
1411 }
1412
1413 bool wxWindowBase::TransferDataFromWindow()
1414 {
1415 #if wxUSE_VALIDATORS
1416 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1417
1418 wxWindowList::compatibility_iterator node;
1419 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1420 {
1421 wxWindow *child = node->GetData();
1422 wxValidator *validator = child->GetValidator();
1423 if ( validator && !validator->TransferFromWindow() )
1424 {
1425 // nop warning here because the application is supposed to give
1426 // one itself - we don't know here what might have gone wrongly
1427
1428 return false;
1429 }
1430
1431 if ( recurse )
1432 {
1433 if ( !child->TransferDataFromWindow() )
1434 {
1435 // warning already given
1436 return false;
1437 }
1438 }
1439 }
1440 #endif // wxUSE_VALIDATORS
1441
1442 return true;
1443 }
1444
1445 void wxWindowBase::InitDialog()
1446 {
1447 wxInitDialogEvent event(GetId());
1448 event.SetEventObject( this );
1449 GetEventHandler()->ProcessEvent(event);
1450 }
1451
1452 // ----------------------------------------------------------------------------
1453 // context-sensitive help support
1454 // ----------------------------------------------------------------------------
1455
1456 #if wxUSE_HELP
1457
1458 // associate this help text with this window
1459 void wxWindowBase::SetHelpText(const wxString& text)
1460 {
1461 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1462 if ( helpProvider )
1463 {
1464 helpProvider->AddHelp(this, text);
1465 }
1466 }
1467
1468 // associate this help text with all windows with the same id as this
1469 // one
1470 void wxWindowBase::SetHelpTextForId(const wxString& text)
1471 {
1472 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1473 if ( helpProvider )
1474 {
1475 helpProvider->AddHelp(GetId(), text);
1476 }
1477 }
1478
1479 // get the help string associated with this window (may be empty)
1480 wxString wxWindowBase::GetHelpText() const
1481 {
1482 wxString text;
1483 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1484 if ( helpProvider )
1485 {
1486 text = helpProvider->GetHelp(this);
1487 }
1488
1489 return text;
1490 }
1491
1492 // show help for this window
1493 void wxWindowBase::OnHelp(wxHelpEvent& event)
1494 {
1495 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1496 if ( helpProvider )
1497 {
1498 if ( helpProvider->ShowHelp(this) )
1499 {
1500 // skip the event.Skip() below
1501 return;
1502 }
1503 }
1504
1505 event.Skip();
1506 }
1507
1508 #endif // wxUSE_HELP
1509
1510 // ----------------------------------------------------------------------------
1511 // tooltipsroot.Replace("\\", "/");
1512 // ----------------------------------------------------------------------------
1513
1514 #if wxUSE_TOOLTIPS
1515
1516 void wxWindowBase::SetToolTip( const wxString &tip )
1517 {
1518 // don't create the new tooltip if we already have one
1519 if ( m_tooltip )
1520 {
1521 m_tooltip->SetTip( tip );
1522 }
1523 else
1524 {
1525 SetToolTip( new wxToolTip( tip ) );
1526 }
1527
1528 // setting empty tooltip text does not remove the tooltip any more - use
1529 // SetToolTip((wxToolTip *)NULL) for this
1530 }
1531
1532 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1533 {
1534 if ( m_tooltip )
1535 delete m_tooltip;
1536
1537 m_tooltip = tooltip;
1538 }
1539
1540 #endif // wxUSE_TOOLTIPS
1541
1542 // ----------------------------------------------------------------------------
1543 // constraints and sizers
1544 // ----------------------------------------------------------------------------
1545
1546 #if wxUSE_CONSTRAINTS
1547
1548 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1549 {
1550 if ( m_constraints )
1551 {
1552 UnsetConstraints(m_constraints);
1553 delete m_constraints;
1554 }
1555 m_constraints = constraints;
1556 if ( m_constraints )
1557 {
1558 // Make sure other windows know they're part of a 'meaningful relationship'
1559 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1560 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1561 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1562 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1563 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1564 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1565 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1566 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1567 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1568 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1569 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1570 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1571 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1572 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1573 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1574 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1575 }
1576 }
1577
1578 // This removes any dangling pointers to this window in other windows'
1579 // constraintsInvolvedIn lists.
1580 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1581 {
1582 if ( c )
1583 {
1584 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1585 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1586 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1587 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1588 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1589 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1590 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1591 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1592 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1593 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1594 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1595 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1596 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1597 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1598 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1599 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1600 }
1601 }
1602
1603 // Back-pointer to other windows we're involved with, so if we delete this
1604 // window, we must delete any constraints we're involved with.
1605 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1606 {
1607 if ( !m_constraintsInvolvedIn )
1608 m_constraintsInvolvedIn = new wxWindowList;
1609 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
1610 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
1611 }
1612
1613 // REMOVE back-pointer to other windows we're involved with.
1614 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1615 {
1616 if ( m_constraintsInvolvedIn )
1617 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
1618 }
1619
1620 // Reset any constraints that mention this window
1621 void wxWindowBase::DeleteRelatedConstraints()
1622 {
1623 if ( m_constraintsInvolvedIn )
1624 {
1625 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
1626 while (node)
1627 {
1628 wxWindow *win = node->GetData();
1629 wxLayoutConstraints *constr = win->GetConstraints();
1630
1631 // Reset any constraints involving this window
1632 if ( constr )
1633 {
1634 constr->left.ResetIfWin(this);
1635 constr->top.ResetIfWin(this);
1636 constr->right.ResetIfWin(this);
1637 constr->bottom.ResetIfWin(this);
1638 constr->width.ResetIfWin(this);
1639 constr->height.ResetIfWin(this);
1640 constr->centreX.ResetIfWin(this);
1641 constr->centreY.ResetIfWin(this);
1642 }
1643
1644 wxWindowList::compatibility_iterator next = node->GetNext();
1645 m_constraintsInvolvedIn->Erase(node);
1646 node = next;
1647 }
1648
1649 delete m_constraintsInvolvedIn;
1650 m_constraintsInvolvedIn = (wxWindowList *) NULL;
1651 }
1652 }
1653
1654 #endif // wxUSE_CONSTRAINTS
1655
1656 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
1657 {
1658 if ( sizer == m_windowSizer)
1659 return;
1660
1661 if ( deleteOld )
1662 delete m_windowSizer;
1663
1664 m_windowSizer = sizer;
1665
1666 SetAutoLayout( sizer != NULL );
1667 }
1668
1669 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
1670 {
1671 SetSizer( sizer, deleteOld );
1672 sizer->SetSizeHints( (wxWindow*) this );
1673 }
1674
1675
1676 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
1677 {
1678 // adding a window to a sizer twice is going to result in fatal and
1679 // hard to debug problems later because when deleting the second
1680 // associated wxSizerItem we're going to dereference a dangling
1681 // pointer; so try to detect this as early as possible
1682 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
1683 _T("Adding a window to the same sizer twice?") );
1684
1685 m_containingSizer = sizer;
1686 }
1687
1688 #if wxUSE_CONSTRAINTS
1689
1690 void wxWindowBase::SatisfyConstraints()
1691 {
1692 wxLayoutConstraints *constr = GetConstraints();
1693 bool wasOk = constr && constr->AreSatisfied();
1694
1695 ResetConstraints(); // Mark all constraints as unevaluated
1696
1697 int noChanges = 1;
1698
1699 // if we're a top level panel (i.e. our parent is frame/dialog), our
1700 // own constraints will never be satisfied any more unless we do it
1701 // here
1702 if ( wasOk )
1703 {
1704 while ( noChanges > 0 )
1705 {
1706 LayoutPhase1(&noChanges);
1707 }
1708 }
1709
1710 LayoutPhase2(&noChanges);
1711 }
1712
1713 #endif // wxUSE_CONSTRAINTS
1714
1715 bool wxWindowBase::Layout()
1716 {
1717 // If there is a sizer, use it instead of the constraints
1718 if ( GetSizer() )
1719 {
1720 int w, h;
1721 GetVirtualSize(&w, &h);
1722 GetSizer()->SetDimension( 0, 0, w, h );
1723 }
1724 #if wxUSE_CONSTRAINTS
1725 else
1726 {
1727 SatisfyConstraints(); // Find the right constraints values
1728 SetConstraintSizes(); // Recursively set the real window sizes
1729 }
1730 #endif
1731
1732 return true;
1733 }
1734
1735 #if wxUSE_CONSTRAINTS
1736
1737 // first phase of the constraints evaluation: set our own constraints
1738 bool wxWindowBase::LayoutPhase1(int *noChanges)
1739 {
1740 wxLayoutConstraints *constr = GetConstraints();
1741
1742 return !constr || constr->SatisfyConstraints(this, noChanges);
1743 }
1744
1745 // second phase: set the constraints for our children
1746 bool wxWindowBase::LayoutPhase2(int *noChanges)
1747 {
1748 *noChanges = 0;
1749
1750 // Layout children
1751 DoPhase(1);
1752
1753 // Layout grand children
1754 DoPhase(2);
1755
1756 return true;
1757 }
1758
1759 // Do a phase of evaluating child constraints
1760 bool wxWindowBase::DoPhase(int phase)
1761 {
1762 // the list containing the children for which the constraints are already
1763 // set correctly
1764 wxWindowList succeeded;
1765
1766 // the max number of iterations we loop before concluding that we can't set
1767 // the constraints
1768 static const int maxIterations = 500;
1769
1770 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
1771 {
1772 int noChanges = 0;
1773
1774 // loop over all children setting their constraints
1775 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1776 node;
1777 node = node->GetNext() )
1778 {
1779 wxWindow *child = node->GetData();
1780 if ( child->IsTopLevel() )
1781 {
1782 // top level children are not inside our client area
1783 continue;
1784 }
1785
1786 if ( !child->GetConstraints() || succeeded.Find(child) )
1787 {
1788 // this one is either already ok or nothing we can do about it
1789 continue;
1790 }
1791
1792 int tempNoChanges = 0;
1793 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
1794 : child->LayoutPhase2(&tempNoChanges);
1795 noChanges += tempNoChanges;
1796
1797 if ( success )
1798 {
1799 succeeded.Append(child);
1800 }
1801 }
1802
1803 if ( !noChanges )
1804 {
1805 // constraints are set
1806 break;
1807 }
1808 }
1809
1810 return true;
1811 }
1812
1813 void wxWindowBase::ResetConstraints()
1814 {
1815 wxLayoutConstraints *constr = GetConstraints();
1816 if ( constr )
1817 {
1818 constr->left.SetDone(false);
1819 constr->top.SetDone(false);
1820 constr->right.SetDone(false);
1821 constr->bottom.SetDone(false);
1822 constr->width.SetDone(false);
1823 constr->height.SetDone(false);
1824 constr->centreX.SetDone(false);
1825 constr->centreY.SetDone(false);
1826 }
1827
1828 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1829 while (node)
1830 {
1831 wxWindow *win = node->GetData();
1832 if ( !win->IsTopLevel() )
1833 win->ResetConstraints();
1834 node = node->GetNext();
1835 }
1836 }
1837
1838 // Need to distinguish between setting the 'fake' size for windows and sizers,
1839 // and setting the real values.
1840 void wxWindowBase::SetConstraintSizes(bool recurse)
1841 {
1842 wxLayoutConstraints *constr = GetConstraints();
1843 if ( constr && constr->AreSatisfied() )
1844 {
1845 int x = constr->left.GetValue();
1846 int y = constr->top.GetValue();
1847 int w = constr->width.GetValue();
1848 int h = constr->height.GetValue();
1849
1850 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1851 (constr->height.GetRelationship() != wxAsIs) )
1852 {
1853 SetSize(x, y, w, h);
1854 }
1855 else
1856 {
1857 // If we don't want to resize this window, just move it...
1858 Move(x, y);
1859 }
1860 }
1861 else if ( constr )
1862 {
1863 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1864 GetClassInfo()->GetClassName(),
1865 GetName().c_str());
1866 }
1867
1868 if ( recurse )
1869 {
1870 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1871 while (node)
1872 {
1873 wxWindow *win = node->GetData();
1874 if ( !win->IsTopLevel() && win->GetConstraints() )
1875 win->SetConstraintSizes();
1876 node = node->GetNext();
1877 }
1878 }
1879 }
1880
1881 // Only set the size/position of the constraint (if any)
1882 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1883 {
1884 wxLayoutConstraints *constr = GetConstraints();
1885 if ( constr )
1886 {
1887 if ( x != wxDefaultPosition.x )
1888 {
1889 constr->left.SetValue(x);
1890 constr->left.SetDone(true);
1891 }
1892 if ( y != wxDefaultPosition.y )
1893 {
1894 constr->top.SetValue(y);
1895 constr->top.SetDone(true);
1896 }
1897 if ( w != wxDefaultSize.x )
1898 {
1899 constr->width.SetValue(w);
1900 constr->width.SetDone(true);
1901 }
1902 if ( h != wxDefaultSize.y )
1903 {
1904 constr->height.SetValue(h);
1905 constr->height.SetDone(true);
1906 }
1907 }
1908 }
1909
1910 void wxWindowBase::MoveConstraint(int x, int y)
1911 {
1912 wxLayoutConstraints *constr = GetConstraints();
1913 if ( constr )
1914 {
1915 if ( x != wxDefaultPosition.x )
1916 {
1917 constr->left.SetValue(x);
1918 constr->left.SetDone(true);
1919 }
1920 if ( y != wxDefaultPosition.y )
1921 {
1922 constr->top.SetValue(y);
1923 constr->top.SetDone(true);
1924 }
1925 }
1926 }
1927
1928 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1929 {
1930 wxLayoutConstraints *constr = GetConstraints();
1931 if ( constr )
1932 {
1933 *w = constr->width.GetValue();
1934 *h = constr->height.GetValue();
1935 }
1936 else
1937 GetSize(w, h);
1938 }
1939
1940 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1941 {
1942 wxLayoutConstraints *constr = GetConstraints();
1943 if ( constr )
1944 {
1945 *w = constr->width.GetValue();
1946 *h = constr->height.GetValue();
1947 }
1948 else
1949 GetClientSize(w, h);
1950 }
1951
1952 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1953 {
1954 wxLayoutConstraints *constr = GetConstraints();
1955 if ( constr )
1956 {
1957 *x = constr->left.GetValue();
1958 *y = constr->top.GetValue();
1959 }
1960 else
1961 GetPosition(x, y);
1962 }
1963
1964 #endif // wxUSE_CONSTRAINTS
1965
1966 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
1967 {
1968 // don't do it for the dialogs/frames - they float independently of their
1969 // parent
1970 if ( !IsTopLevel() )
1971 {
1972 wxWindow *parent = GetParent();
1973 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
1974 {
1975 wxPoint pt(parent->GetClientAreaOrigin());
1976 x += pt.x;
1977 y += pt.y;
1978 }
1979 }
1980 }
1981
1982 // ----------------------------------------------------------------------------
1983 // do Update UI processing for child controls
1984 // ----------------------------------------------------------------------------
1985
1986 void wxWindowBase::UpdateWindowUI(long flags)
1987 {
1988 wxUpdateUIEvent event(GetId());
1989 event.m_eventObject = this;
1990
1991 if ( GetEventHandler()->ProcessEvent(event) )
1992 {
1993 DoUpdateWindowUI(event);
1994 }
1995
1996 if (flags & wxUPDATE_UI_RECURSE)
1997 {
1998 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1999 while (node)
2000 {
2001 wxWindow* child = (wxWindow*) node->GetData();
2002 child->UpdateWindowUI(flags);
2003 node = node->GetNext();
2004 }
2005 }
2006 }
2007
2008 // do the window-specific processing after processing the update event
2009 // TODO: take specific knowledge out of this function and
2010 // put in each control's base class. Unfortunately we don't
2011 // yet have base implementation files for wxCheckBox and wxRadioButton.
2012 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2013 {
2014 if ( event.GetSetEnabled() )
2015 Enable(event.GetEnabled());
2016
2017 #if wxUSE_CONTROLS
2018 if ( event.GetSetText() )
2019 {
2020 wxControl *control = wxDynamicCastThis(wxControl);
2021 if ( control )
2022 {
2023 if ( event.GetText() != control->GetLabel() )
2024 control->SetLabel(event.GetText());
2025 }
2026 #if wxUSE_CHECKBOX
2027 wxCheckBox *checkbox = wxDynamicCastThis(wxCheckBox);
2028 if ( checkbox )
2029 {
2030 if ( event.GetSetChecked() )
2031 checkbox->SetValue(event.GetChecked());
2032 }
2033 #endif // wxUSE_CHECKBOX
2034
2035 #if wxUSE_RADIOBTN
2036 wxRadioButton *radiobtn = wxDynamicCastThis(wxRadioButton);
2037 if ( radiobtn )
2038 {
2039 if ( event.GetSetChecked() )
2040 radiobtn->SetValue(event.GetChecked());
2041 }
2042 #endif // wxUSE_RADIOBTN
2043 }
2044 #endif
2045 }
2046
2047 #if 0
2048 // call internal idle recursively
2049 // may be obsolete (wait until OnIdle scheme stabilises)
2050 void wxWindowBase::ProcessInternalIdle()
2051 {
2052 OnInternalIdle();
2053
2054 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2055 while (node)
2056 {
2057 wxWindow *child = node->GetData();
2058 child->ProcessInternalIdle();
2059 node = node->GetNext();
2060 }
2061 }
2062 #endif
2063
2064 // ----------------------------------------------------------------------------
2065 // dialog units translations
2066 // ----------------------------------------------------------------------------
2067
2068 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2069 {
2070 int charWidth = GetCharWidth();
2071 int charHeight = GetCharHeight();
2072 wxPoint pt2(-1, -1);
2073 if (pt.x != -1)
2074 pt2.x = (int) ((pt.x * 4) / charWidth);
2075 if (pt.y != -1)
2076 pt2.y = (int) ((pt.y * 8) / charHeight);
2077
2078 return pt2;
2079 }
2080
2081 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2082 {
2083 int charWidth = GetCharWidth();
2084 int charHeight = GetCharHeight();
2085 wxPoint pt2(-1, -1);
2086 if (pt.x != -1)
2087 pt2.x = (int) ((pt.x * charWidth) / 4);
2088 if (pt.y != -1)
2089 pt2.y = (int) ((pt.y * charHeight) / 8);
2090
2091 return pt2;
2092 }
2093
2094 // ----------------------------------------------------------------------------
2095 // event handlers
2096 // ----------------------------------------------------------------------------
2097
2098 // propagate the colour change event to the subwindows
2099 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2100 {
2101 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2102 while ( node )
2103 {
2104 // Only propagate to non-top-level windows
2105 wxWindow *win = node->GetData();
2106 if ( !win->IsTopLevel() )
2107 {
2108 wxSysColourChangedEvent event2;
2109 event.m_eventObject = win;
2110 win->GetEventHandler()->ProcessEvent(event2);
2111 }
2112
2113 node = node->GetNext();
2114 }
2115 }
2116
2117 // the default action is to populate dialog with data when it's created,
2118 // and nudge the UI into displaying itself correctly in case
2119 // we've turned the wxUpdateUIEvents frequency down low.
2120 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2121 {
2122 TransferDataToWindow();
2123
2124 // Update the UI at this point
2125 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2126 }
2127
2128 // process Ctrl-Alt-mclick
2129 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2130 {
2131 #if wxUSE_MSGDLG
2132 if ( event.ControlDown() && event.AltDown() )
2133 {
2134 // don't translate these strings
2135 wxString port;
2136
2137 #ifdef __WXUNIVERSAL__
2138 port = _T("Univ/");
2139 #endif // __WXUNIVERSAL__
2140
2141 switch ( wxGetOsVersion() )
2142 {
2143 case wxMOTIF_X: port += _T("Motif"); break;
2144 case wxMAC:
2145 case wxMAC_DARWIN: port += _T("Mac"); break;
2146 case wxBEOS: port += _T("BeOS"); break;
2147 case wxGTK:
2148 case wxGTK_WIN32:
2149 case wxGTK_OS2:
2150 case wxGTK_BEOS: port += _T("GTK"); break;
2151 case wxWINDOWS:
2152 case wxPENWINDOWS:
2153 case wxWINDOWS_NT:
2154 case wxWIN32S:
2155 case wxWIN95:
2156 case wxWIN386: port += _T("MS Windows"); break;
2157 case wxMGL_UNIX:
2158 case wxMGL_X:
2159 case wxMGL_WIN32:
2160 case wxMGL_OS2: port += _T("MGL"); break;
2161 case wxWINDOWS_OS2:
2162 case wxOS2_PM: port += _T("OS/2"); break;
2163 default: port += _T("unknown"); break;
2164 }
2165
2166 wxMessageBox(wxString::Format(
2167 _T(
2168 " wxWidgets Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWidgets team"
2169 ),
2170 port.c_str(),
2171 wxMAJOR_VERSION,
2172 wxMINOR_VERSION,
2173 wxRELEASE_NUMBER,
2174 #if wxUSE_UNICODE
2175 L" (Unicode)",
2176 #else
2177 "",
2178 #endif
2179 __TDATE__,
2180 __TTIME__
2181 ),
2182 _T("wxWidgets information"),
2183 wxICON_INFORMATION | wxOK,
2184 (wxWindow *)this);
2185 }
2186 else
2187 #endif // wxUSE_MSGDLG
2188 {
2189 event.Skip();
2190 }
2191 }
2192
2193 // ----------------------------------------------------------------------------
2194 // accessibility
2195 // ----------------------------------------------------------------------------
2196
2197 #if wxUSE_ACCESSIBILITY
2198 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2199 {
2200 if (m_accessible && (accessible != m_accessible))
2201 delete m_accessible;
2202 m_accessible = accessible;
2203 if (m_accessible)
2204 m_accessible->SetWindow((wxWindow*) this);
2205 }
2206
2207 // Returns the accessible object, creating if necessary.
2208 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2209 {
2210 if (!m_accessible)
2211 m_accessible = CreateAccessible();
2212 return m_accessible;
2213 }
2214
2215 // Override to create a specific accessible object.
2216 wxAccessible* wxWindowBase::CreateAccessible()
2217 {
2218 return new wxWindowAccessible((wxWindow*) this);
2219 }
2220
2221 #endif
2222
2223 #if !wxUSE_STL
2224 // ----------------------------------------------------------------------------
2225 // list classes implementation
2226 // ----------------------------------------------------------------------------
2227
2228 void wxWindowListNode::DeleteData()
2229 {
2230 delete (wxWindow *)GetData();
2231 }
2232 #endif
2233
2234 // ----------------------------------------------------------------------------
2235 // borders
2236 // ----------------------------------------------------------------------------
2237
2238 wxBorder wxWindowBase::GetBorder(long flags) const
2239 {
2240 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2241 if ( border == wxBORDER_DEFAULT )
2242 {
2243 border = GetDefaultBorder();
2244 }
2245
2246 return border;
2247 }
2248
2249 wxBorder wxWindowBase::GetDefaultBorder() const
2250 {
2251 return wxBORDER_NONE;
2252 }
2253
2254 // ----------------------------------------------------------------------------
2255 // hit testing
2256 // ----------------------------------------------------------------------------
2257
2258 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2259 {
2260 // here we just check if the point is inside the window or not
2261
2262 // check the top and left border first
2263 bool outside = x < 0 || y < 0;
2264 if ( !outside )
2265 {
2266 // check the right and bottom borders too
2267 wxSize size = GetSize();
2268 outside = x >= size.x || y >= size.y;
2269 }
2270
2271 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2272 }
2273
2274 // ----------------------------------------------------------------------------
2275 // mouse capture
2276 // ----------------------------------------------------------------------------
2277
2278 struct WXDLLEXPORT wxWindowNext
2279 {
2280 wxWindow *win;
2281 wxWindowNext *next;
2282 } *wxWindowBase::ms_winCaptureNext = NULL;
2283
2284 void wxWindowBase::CaptureMouse()
2285 {
2286 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), this);
2287
2288 wxWindow *winOld = GetCapture();
2289 if ( winOld )
2290 {
2291 ((wxWindowBase*) winOld)->DoReleaseMouse();
2292
2293 // save it on stack
2294 wxWindowNext *item = new wxWindowNext;
2295 item->win = winOld;
2296 item->next = ms_winCaptureNext;
2297 ms_winCaptureNext = item;
2298 }
2299 //else: no mouse capture to save
2300
2301 DoCaptureMouse();
2302 }
2303
2304 void wxWindowBase::ReleaseMouse()
2305 {
2306 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), this);
2307
2308 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2309
2310 DoReleaseMouse();
2311
2312 if ( ms_winCaptureNext )
2313 {
2314 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2315
2316 wxWindowNext *item = ms_winCaptureNext;
2317 ms_winCaptureNext = item->next;
2318 delete item;
2319 }
2320 //else: stack is empty, no previous capture
2321
2322 wxLogTrace(_T("mousecapture"),
2323 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
2324 GetCapture());
2325 }
2326
2327 #if wxUSE_HOTKEY
2328
2329 bool
2330 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2331 int WXUNUSED(modifiers),
2332 int WXUNUSED(keycode))
2333 {
2334 // not implemented
2335 return false;
2336 }
2337
2338 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2339 {
2340 // not implemented
2341 return false;
2342 }
2343
2344 #endif // wxUSE_HOTKEY
2345
2346 void wxWindowBase::SendDestroyEvent()
2347 {
2348 wxWindowDestroyEvent event;
2349 event.SetEventObject(this);
2350 event.SetId(GetId());
2351 GetEventHandler()->ProcessEvent(event);
2352 }
2353
2354 // ----------------------------------------------------------------------------
2355 // event processing
2356 // ----------------------------------------------------------------------------
2357
2358 bool wxWindowBase::TryValidator(wxEvent& wxVALIDATOR_PARAM(event))
2359 {
2360 #if wxUSE_VALIDATORS
2361 // Can only use the validator of the window which
2362 // is receiving the event
2363 if ( event.GetEventObject() == this )
2364 {
2365 wxValidator *validator = GetValidator();
2366 if ( validator && validator->ProcessEvent(event) )
2367 {
2368 return true;
2369 }
2370 }
2371 #endif // wxUSE_VALIDATORS
2372
2373 return false;
2374 }
2375
2376 bool wxWindowBase::TryParent(wxEvent& event)
2377 {
2378 // carry on up the parent-child hierarchy if the propgation count hasn't
2379 // reached zero yet
2380 if ( event.ShouldPropagate() )
2381 {
2382 // honour the requests to stop propagation at this window: this is
2383 // used by the dialogs, for example, to prevent processing the events
2384 // from the dialog controls in the parent frame which rarely, if ever,
2385 // makes sense
2386 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2387 {
2388 wxWindow *parent = GetParent();
2389 if ( parent && !parent->IsBeingDeleted() )
2390 {
2391 wxPropagateOnce propagateOnce(event);
2392
2393 return parent->GetEventHandler()->ProcessEvent(event);
2394 }
2395 }
2396 }
2397
2398 return wxEvtHandler::TryParent(event);
2399 }
2400
2401 // ----------------------------------------------------------------------------
2402 // navigation
2403 // ----------------------------------------------------------------------------
2404
2405 // Navigates in the specified direction.
2406 bool wxWindowBase::Navigate(int flags)
2407 {
2408 wxNavigationKeyEvent eventNav;
2409 eventNav.SetFlags(flags);
2410 eventNav.SetEventObject(this);
2411 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
2412 {
2413 return true;
2414 }
2415 return false;
2416 }
2417
2418 // ----------------------------------------------------------------------------
2419 // global functions
2420 // ----------------------------------------------------------------------------
2421
2422 wxWindow* wxGetTopLevelParent(wxWindow *win)
2423 {
2424 while ( win && !win->IsTopLevel() )
2425 win = win->GetParent();
2426
2427 return win;
2428 }
2429
2430 #if wxUSE_ACCESSIBILITY
2431 // ----------------------------------------------------------------------------
2432 // accessible object for windows
2433 // ----------------------------------------------------------------------------
2434
2435 // Can return either a child object, or an integer
2436 // representing the child element, starting from 1.
2437 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
2438 {
2439 wxASSERT( GetWindow() != NULL );
2440 if (!GetWindow())
2441 return wxACC_FAIL;
2442
2443 return wxACC_NOT_IMPLEMENTED;
2444 }
2445
2446 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2447 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
2448 {
2449 wxASSERT( GetWindow() != NULL );
2450 if (!GetWindow())
2451 return wxACC_FAIL;
2452
2453 wxWindow* win = NULL;
2454 if (elementId == 0)
2455 {
2456 win = GetWindow();
2457 }
2458 else
2459 {
2460 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
2461 {
2462 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
2463 }
2464 else
2465 return wxACC_FAIL;
2466 }
2467 if (win)
2468 {
2469 rect = win->GetRect();
2470 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
2471 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
2472 return wxACC_OK;
2473 }
2474
2475 return wxACC_NOT_IMPLEMENTED;
2476 }
2477
2478 // Navigates from fromId to toId/toObject.
2479 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
2480 int* WXUNUSED(toId), wxAccessible** toObject)
2481 {
2482 wxASSERT( GetWindow() != NULL );
2483 if (!GetWindow())
2484 return wxACC_FAIL;
2485
2486 switch (navDir)
2487 {
2488 case wxNAVDIR_FIRSTCHILD:
2489 {
2490 if (GetWindow()->GetChildren().GetCount() == 0)
2491 return wxACC_FALSE;
2492 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
2493 *toObject = childWindow->GetOrCreateAccessible();
2494
2495 return wxACC_OK;
2496 }
2497 case wxNAVDIR_LASTCHILD:
2498 {
2499 if (GetWindow()->GetChildren().GetCount() == 0)
2500 return wxACC_FALSE;
2501 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
2502 *toObject = childWindow->GetOrCreateAccessible();
2503
2504 return wxACC_OK;
2505 }
2506 case wxNAVDIR_RIGHT:
2507 case wxNAVDIR_DOWN:
2508 case wxNAVDIR_NEXT:
2509 {
2510 wxWindowList::compatibility_iterator node =
2511 wxWindowList::compatibility_iterator();
2512 if (fromId == 0)
2513 {
2514 // Can't navigate to sibling of this window
2515 // if we're a top-level window.
2516 if (!GetWindow()->GetParent())
2517 return wxACC_NOT_IMPLEMENTED;
2518
2519 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2520 }
2521 else
2522 {
2523 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2524 node = GetWindow()->GetChildren().Item(fromId-1);
2525 }
2526
2527 if (node && node->GetNext())
2528 {
2529 wxWindow* nextWindow = node->GetNext()->GetData();
2530 *toObject = nextWindow->GetOrCreateAccessible();
2531 return wxACC_OK;
2532 }
2533 else
2534 return wxACC_FALSE;
2535 }
2536 case wxNAVDIR_LEFT:
2537 case wxNAVDIR_UP:
2538 case wxNAVDIR_PREVIOUS:
2539 {
2540 wxWindowList::compatibility_iterator node =
2541 wxWindowList::compatibility_iterator();
2542 if (fromId == 0)
2543 {
2544 // Can't navigate to sibling of this window
2545 // if we're a top-level window.
2546 if (!GetWindow()->GetParent())
2547 return wxACC_NOT_IMPLEMENTED;
2548
2549 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2550 }
2551 else
2552 {
2553 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2554 node = GetWindow()->GetChildren().Item(fromId-1);
2555 }
2556
2557 if (node && node->GetPrevious())
2558 {
2559 wxWindow* previousWindow = node->GetPrevious()->GetData();
2560 *toObject = previousWindow->GetOrCreateAccessible();
2561 return wxACC_OK;
2562 }
2563 else
2564 return wxACC_FALSE;
2565 }
2566 }
2567
2568 return wxACC_NOT_IMPLEMENTED;
2569 }
2570
2571 // Gets the name of the specified object.
2572 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
2573 {
2574 wxASSERT( GetWindow() != NULL );
2575 if (!GetWindow())
2576 return wxACC_FAIL;
2577
2578 wxString title;
2579
2580 // If a child, leave wxWidgets to call the function on the actual
2581 // child object.
2582 if (childId > 0)
2583 return wxACC_NOT_IMPLEMENTED;
2584
2585 // This will eventually be replaced by specialised
2586 // accessible classes, one for each kind of wxWidgets
2587 // control or window.
2588 #if wxUSE_BUTTON
2589 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
2590 title = ((wxButton*) GetWindow())->GetLabel();
2591 else
2592 #endif
2593 title = GetWindow()->GetName();
2594
2595 if (!title.IsEmpty())
2596 {
2597 *name = title;
2598 return wxACC_OK;
2599 }
2600 else
2601 return wxACC_NOT_IMPLEMENTED;
2602 }
2603
2604 // Gets the number of children.
2605 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
2606 {
2607 wxASSERT( GetWindow() != NULL );
2608 if (!GetWindow())
2609 return wxACC_FAIL;
2610
2611 *childId = (int) GetWindow()->GetChildren().GetCount();
2612 return wxACC_OK;
2613 }
2614
2615 // Gets the specified child (starting from 1).
2616 // If *child is NULL and return value is wxACC_OK,
2617 // this means that the child is a simple element and
2618 // not an accessible object.
2619 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
2620 {
2621 wxASSERT( GetWindow() != NULL );
2622 if (!GetWindow())
2623 return wxACC_FAIL;
2624
2625 if (childId == 0)
2626 {
2627 *child = this;
2628 return wxACC_OK;
2629 }
2630
2631 if (childId > (int) GetWindow()->GetChildren().GetCount())
2632 return wxACC_FAIL;
2633
2634 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
2635 *child = childWindow->GetOrCreateAccessible();
2636 if (*child)
2637 return wxACC_OK;
2638 else
2639 return wxACC_FAIL;
2640 }
2641
2642 // Gets the parent, or NULL.
2643 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
2644 {
2645 wxASSERT( GetWindow() != NULL );
2646 if (!GetWindow())
2647 return wxACC_FAIL;
2648
2649 wxWindow* parentWindow = GetWindow()->GetParent();
2650 if (!parentWindow)
2651 {
2652 *parent = NULL;
2653 return wxACC_OK;
2654 }
2655 else
2656 {
2657 *parent = parentWindow->GetOrCreateAccessible();
2658 if (*parent)
2659 return wxACC_OK;
2660 else
2661 return wxACC_FAIL;
2662 }
2663 }
2664
2665 // Performs the default action. childId is 0 (the action for this object)
2666 // or > 0 (the action for a child).
2667 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2668 // window (e.g. an edit control).
2669 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
2670 {
2671 wxASSERT( GetWindow() != NULL );
2672 if (!GetWindow())
2673 return wxACC_FAIL;
2674
2675 return wxACC_NOT_IMPLEMENTED;
2676 }
2677
2678 // Gets the default action for this object (0) or > 0 (the action for a child).
2679 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2680 // string if there is no action.
2681 // The retrieved string describes the action that is performed on an object,
2682 // not what the object does as a result. For example, a toolbar button that prints
2683 // a document has a default action of "Press" rather than "Prints the current document."
2684 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
2685 {
2686 wxASSERT( GetWindow() != NULL );
2687 if (!GetWindow())
2688 return wxACC_FAIL;
2689
2690 return wxACC_NOT_IMPLEMENTED;
2691 }
2692
2693 // Returns the description for this object or a child.
2694 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
2695 {
2696 wxASSERT( GetWindow() != NULL );
2697 if (!GetWindow())
2698 return wxACC_FAIL;
2699
2700 wxString ht(GetWindow()->GetHelpText());
2701 if (!ht.IsEmpty())
2702 {
2703 *description = ht;
2704 return wxACC_OK;
2705 }
2706 return wxACC_NOT_IMPLEMENTED;
2707 }
2708
2709 // Returns help text for this object or a child, similar to tooltip text.
2710 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
2711 {
2712 wxASSERT( GetWindow() != NULL );
2713 if (!GetWindow())
2714 return wxACC_FAIL;
2715
2716 wxString ht(GetWindow()->GetHelpText());
2717 if (!ht.IsEmpty())
2718 {
2719 *helpText = ht;
2720 return wxACC_OK;
2721 }
2722 return wxACC_NOT_IMPLEMENTED;
2723 }
2724
2725 // Returns the keyboard shortcut for this object or child.
2726 // Return e.g. ALT+K
2727 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
2728 {
2729 wxASSERT( GetWindow() != NULL );
2730 if (!GetWindow())
2731 return wxACC_FAIL;
2732
2733 return wxACC_NOT_IMPLEMENTED;
2734 }
2735
2736 // Returns a role constant.
2737 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
2738 {
2739 wxASSERT( GetWindow() != NULL );
2740 if (!GetWindow())
2741 return wxACC_FAIL;
2742
2743 // If a child, leave wxWidgets to call the function on the actual
2744 // child object.
2745 if (childId > 0)
2746 return wxACC_NOT_IMPLEMENTED;
2747
2748 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2749 return wxACC_NOT_IMPLEMENTED;
2750 #if wxUSE_STATUSBAR
2751 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2752 return wxACC_NOT_IMPLEMENTED;
2753 #endif
2754 #if wxUSE_TOOLBAR
2755 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2756 return wxACC_NOT_IMPLEMENTED;
2757 #endif
2758
2759 //*role = wxROLE_SYSTEM_CLIENT;
2760 *role = wxROLE_SYSTEM_CLIENT;
2761 return wxACC_OK;
2762
2763 #if 0
2764 return wxACC_NOT_IMPLEMENTED;
2765 #endif
2766 }
2767
2768 // Returns a state constant.
2769 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
2770 {
2771 wxASSERT( GetWindow() != NULL );
2772 if (!GetWindow())
2773 return wxACC_FAIL;
2774
2775 // If a child, leave wxWidgets to call the function on the actual
2776 // child object.
2777 if (childId > 0)
2778 return wxACC_NOT_IMPLEMENTED;
2779
2780 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2781 return wxACC_NOT_IMPLEMENTED;
2782
2783 #if wxUSE_STATUSBAR
2784 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2785 return wxACC_NOT_IMPLEMENTED;
2786 #endif
2787 #if wxUSE_TOOLBAR
2788 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2789 return wxACC_NOT_IMPLEMENTED;
2790 #endif
2791
2792 *state = 0;
2793 return wxACC_OK;
2794
2795 #if 0
2796 return wxACC_NOT_IMPLEMENTED;
2797 #endif
2798 }
2799
2800 // Returns a localized string representing the value for the object
2801 // or child.
2802 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
2803 {
2804 wxASSERT( GetWindow() != NULL );
2805 if (!GetWindow())
2806 return wxACC_FAIL;
2807
2808 return wxACC_NOT_IMPLEMENTED;
2809 }
2810
2811 // Selects the object or child.
2812 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
2813 {
2814 wxASSERT( GetWindow() != NULL );
2815 if (!GetWindow())
2816 return wxACC_FAIL;
2817
2818 return wxACC_NOT_IMPLEMENTED;
2819 }
2820
2821 // Gets the window with the keyboard focus.
2822 // If childId is 0 and child is NULL, no object in
2823 // this subhierarchy has the focus.
2824 // If this object has the focus, child should be 'this'.
2825 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
2826 {
2827 wxASSERT( GetWindow() != NULL );
2828 if (!GetWindow())
2829 return wxACC_FAIL;
2830
2831 return wxACC_NOT_IMPLEMENTED;
2832 }
2833
2834 // Gets a variant representing the selected children
2835 // of this object.
2836 // Acceptable values:
2837 // - a null variant (IsNull() returns TRUE)
2838 // - a list variant (GetType() == wxT("list")
2839 // - an integer representing the selected child element,
2840 // or 0 if this object is selected (GetType() == wxT("long")
2841 // - a "void*" pointer to a wxAccessible child object
2842 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
2843 {
2844 wxASSERT( GetWindow() != NULL );
2845 if (!GetWindow())
2846 return wxACC_FAIL;
2847
2848 return wxACC_NOT_IMPLEMENTED;
2849 }
2850
2851 #endif // wxUSE_ACCESSIBILITY