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