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