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