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