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