]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
UsePrimarySelection
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
46 #endif //WX_PRECOMP
47
48 #if wxUSE_CONSTRAINTS
49 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
51
52 #include "wx/sizer.h"
53
54 #if wxUSE_DRAG_AND_DROP
55 #include "wx/dnd.h"
56 #endif // wxUSE_DRAG_AND_DROP
57
58 #if wxUSE_HELP
59 #include "wx/cshelp.h"
60 #endif // wxUSE_HELP
61
62 #if wxUSE_TOOLTIPS
63 #include "wx/tooltip.h"
64 #endif // wxUSE_TOOLTIPS
65
66 #if wxUSE_CARET
67 #include "wx/caret.h"
68 #endif // wxUSE_CARET
69
70 // ----------------------------------------------------------------------------
71 // static data
72 // ----------------------------------------------------------------------------
73
74 int wxWindowBase::ms_lastControlId = -200;
75
76 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
77
78 // ----------------------------------------------------------------------------
79 // event table
80 // ----------------------------------------------------------------------------
81
82 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
83 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
84 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
85 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
86
87 #if wxUSE_HELP
88 EVT_HELP(-1, wxWindowBase::OnHelp)
89 #endif // wxUSE_HELP
90
91 END_EVENT_TABLE()
92
93 // ============================================================================
94 // implementation of the common functionality of the wxWindow class
95 // ============================================================================
96
97 // ----------------------------------------------------------------------------
98 // initialization
99 // ----------------------------------------------------------------------------
100
101 // the default initialization
102 void wxWindowBase::InitBase()
103 {
104 // no window yet, no parent nor children
105 m_parent = (wxWindow *)NULL;
106 m_windowId = -1;
107 m_children.DeleteContents( FALSE ); // don't auto delete node data
108
109 // no constraints on the minimal window size
110 m_minWidth =
111 m_minHeight =
112 m_maxWidth =
113 m_maxHeight = -1;
114
115 // window is created enabled but it's not visible yet
116 m_isShown = FALSE;
117 m_isEnabled = TRUE;
118
119 // the default event handler is just this window
120 m_eventHandler = this;
121
122 #if wxUSE_VALIDATORS
123 // no validator
124 m_windowValidator = (wxValidator *) NULL;
125 #endif // wxUSE_VALIDATORS
126
127 // use the system default colours
128 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
129 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
130
131 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
132 // so the font is *not* really set - but calls to SetFont() later won't do
133 // anything because m_font appears to be already set!
134 #ifndef __WXMSW__
135 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
136 #endif // __WXMSW__
137
138 // the colours/fonts are default for now
139 m_hasBgCol =
140 m_hasFgCol =
141 m_hasFont = FALSE;
142
143 // no style bits
144 m_exStyle =
145 m_windowStyle = 0;
146
147 // an optimization for the event processing: checking this flag is much
148 // faster than using IsKindOf(CLASSINFO(wxWindow))
149 m_isWindow = TRUE;
150
151 #if wxUSE_CONSTRAINTS
152 // no constraints whatsoever
153 m_constraints = (wxLayoutConstraints *) NULL;
154 m_constraintsInvolvedIn = (wxWindowList *) NULL;
155 #endif // wxUSE_CONSTRAINTS
156
157 m_windowSizer = (wxSizer *) NULL;
158 m_containingSizer = (wxSizer *) NULL;
159 m_autoLayout = FALSE;
160
161 #if wxUSE_DRAG_AND_DROP
162 m_dropTarget = (wxDropTarget *)NULL;
163 #endif // wxUSE_DRAG_AND_DROP
164
165 #if wxUSE_TOOLTIPS
166 m_tooltip = (wxToolTip *)NULL;
167 #endif // wxUSE_TOOLTIPS
168
169 #if wxUSE_CARET
170 m_caret = (wxCaret *)NULL;
171 #endif // wxUSE_CARET
172
173 #if wxUSE_PALETTE
174 m_hasCustomPalette = FALSE;
175 #endif // wxUSE_PALETTE
176
177 m_virtualSize = wxDefaultSize;
178 m_minVirtualWidth = -1;
179 m_minVirtualHeight = -1;
180 m_maxVirtualWidth = -1;
181 m_maxVirtualHeight = -1;
182
183 // Whether we're using the current theme for this window (wxGTK only for now)
184 m_themeEnabled = FALSE;
185 }
186
187 // common part of window creation process
188 bool wxWindowBase::CreateBase(wxWindowBase *parent,
189 wxWindowID id,
190 const wxPoint& WXUNUSED(pos),
191 const wxSize& WXUNUSED(size),
192 long style,
193 const wxValidator& validator,
194 const wxString& name)
195 {
196 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
197 // member variables - check that it has been called (will catch the case
198 // when a new ctor is added which doesn't call InitWindow)
199 wxASSERT_MSG( m_isWindow, wxT("Init() must have been called before!") );
200
201 // generate a new id if the user doesn't care about it
202 m_windowId = id == -1 ? NewControlId() : id;
203
204 SetName(name);
205 SetWindowStyleFlag(style);
206 SetParent(parent);
207
208 #if wxUSE_VALIDATORS
209 SetValidator(validator);
210 #endif // wxUSE_VALIDATORS
211
212 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
213 // have it too - like this it's possible to set it only in the top level
214 // dialog/frame and all children will inherit it by defult
215 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
216 {
217 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
218 }
219
220 return TRUE;
221 }
222
223 // ----------------------------------------------------------------------------
224 // destruction
225 // ----------------------------------------------------------------------------
226
227 // common clean up
228 wxWindowBase::~wxWindowBase()
229 {
230 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
231
232 // FIXME if these 2 cases result from programming errors in the user code
233 // we should probably assert here instead of silently fixing them
234
235 // Just in case the window has been Closed, but we're then deleting
236 // immediately: don't leave dangling pointers.
237 wxPendingDelete.DeleteObject(this);
238
239 // Just in case we've loaded a top-level window via LoadNativeDialog but
240 // we weren't a dialog class
241 wxTopLevelWindows.DeleteObject(this);
242
243 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
244
245 #if wxUSE_CARET
246 if ( m_caret )
247 delete m_caret;
248 #endif // wxUSE_CARET
249
250 #if wxUSE_VALIDATORS
251 if ( m_windowValidator )
252 delete m_windowValidator;
253 #endif // wxUSE_VALIDATORS
254
255 #if wxUSE_CONSTRAINTS
256 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
257 // at deleted windows as they delete themselves.
258 DeleteRelatedConstraints();
259
260 if ( m_constraints )
261 {
262 // This removes any dangling pointers to this window in other windows'
263 // constraintsInvolvedIn lists.
264 UnsetConstraints(m_constraints);
265 delete m_constraints;
266 m_constraints = NULL;
267 }
268
269 #endif // wxUSE_CONSTRAINTS
270
271 if ( m_containingSizer )
272 m_containingSizer->Remove((wxWindow*)this);
273
274 if ( m_windowSizer )
275 delete m_windowSizer;
276
277 #if wxUSE_DRAG_AND_DROP
278 if ( m_dropTarget )
279 delete m_dropTarget;
280 #endif // wxUSE_DRAG_AND_DROP
281
282 #if wxUSE_TOOLTIPS
283 if ( m_tooltip )
284 delete m_tooltip;
285 #endif // wxUSE_TOOLTIPS
286
287 // reset the dangling pointer our parent window may keep to us
288 if ( m_parent && m_parent->GetDefaultItem() == this )
289 {
290 m_parent->SetDefaultItem(NULL);
291 }
292 }
293
294 bool wxWindowBase::Destroy()
295 {
296 delete this;
297
298 return TRUE;
299 }
300
301 bool wxWindowBase::Close(bool force)
302 {
303 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
304 event.SetEventObject(this);
305 #if WXWIN_COMPATIBILITY
306 event.SetForce(force);
307 #endif // WXWIN_COMPATIBILITY
308 event.SetCanVeto(!force);
309
310 // return FALSE if window wasn't closed because the application vetoed the
311 // close event
312 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
313 }
314
315 bool wxWindowBase::DestroyChildren()
316 {
317 wxWindowList::Node *node;
318 for ( ;; )
319 {
320 // we iterate until the list becomes empty
321 node = GetChildren().GetFirst();
322 if ( !node )
323 break;
324
325 wxWindow *child = node->GetData();
326
327 wxASSERT_MSG( child, wxT("children list contains empty nodes") );
328
329 child->Show(FALSE);
330 delete child;
331
332 wxASSERT_MSG( !GetChildren().Find(child),
333 wxT("child didn't remove itself using RemoveChild()") );
334 }
335
336 return TRUE;
337 }
338
339 // ----------------------------------------------------------------------------
340 // size/position related methods
341 // ----------------------------------------------------------------------------
342
343 // centre the window with respect to its parent in either (or both) directions
344 void wxWindowBase::Centre(int direction)
345 {
346 // the position/size of the parent window or of the entire screen
347 wxPoint posParent;
348 int widthParent, heightParent;
349
350 wxWindow *parent = NULL;
351
352 if ( !(direction & wxCENTRE_ON_SCREEN) )
353 {
354 // find the parent to centre this window on: it should be the
355 // immediate parent for the controls but the top level parent for the
356 // top level windows (like dialogs)
357 parent = GetParent();
358 if ( IsTopLevel() )
359 {
360 while ( parent && !parent->IsTopLevel() )
361 {
362 parent = parent->GetParent();
363 }
364 }
365
366 // did we find the parent?
367 if ( !parent )
368 {
369 // no other choice
370 direction |= wxCENTRE_ON_SCREEN;
371 }
372 }
373
374 if ( direction & wxCENTRE_ON_SCREEN )
375 {
376 // centre with respect to the whole screen
377 wxDisplaySize(&widthParent, &heightParent);
378 }
379 else
380 {
381 if ( IsTopLevel() )
382 {
383 // centre on the parent
384 parent->GetSize(&widthParent, &heightParent);
385
386 // adjust to the parents position
387 posParent = parent->GetPosition();
388 }
389 else
390 {
391 // centre inside the parents client rectangle
392 parent->GetClientSize(&widthParent, &heightParent);
393 }
394 }
395
396 int width, height;
397 GetSize(&width, &height);
398
399 int xNew = -1,
400 yNew = -1;
401
402 if ( direction & wxHORIZONTAL )
403 xNew = (widthParent - width)/2;
404
405 if ( direction & wxVERTICAL )
406 yNew = (heightParent - height)/2;
407
408 xNew += posParent.x;
409 yNew += posParent.y;
410
411 // Base size of the visible dimensions of the display
412 // to take into account the taskbar
413 wxRect rect = wxGetClientDisplayRect();
414 wxSize size (rect.width,rect.height);
415
416 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
417 // but it may mean that the window is placed on other than the main
418 // display. Therefore we only make sure centered window is on the main display
419 // if the parent is at least partially present here.
420 if (posParent.x + widthParent >= 0) // if parent is (partially) on the main display
421 {
422 if (xNew < 0)
423 xNew = 0;
424 else if (xNew+width > size.x)
425 xNew = size.x-width-1;
426 }
427 if (posParent.y + heightParent >= 0) // if parent is (partially) on the main display
428 {
429 if (yNew+height > size.y)
430 yNew = size.y-height-1;
431
432 // Make certain that the title bar is initially visible
433 // always, even if this would push the bottom of the
434 // dialog of the visible area of the display
435 if (yNew < 0)
436 yNew = 0;
437 }
438
439 // move the window to this position (keeping the old size but using
440 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
441 SetSize(xNew, yNew, width, height, wxSIZE_ALLOW_MINUS_ONE);
442 }
443
444 // fits the window around the children
445 void wxWindowBase::Fit()
446 {
447 if ( GetChildren().GetCount() > 0 )
448 {
449 SetClientSize(DoGetBestSize());
450 }
451 //else: do nothing if we have no children
452 }
453
454 // return the size best suited for the current window
455 wxSize wxWindowBase::DoGetBestSize() const
456 {
457 if ( m_windowSizer )
458 {
459 return m_windowSizer->GetMinSize();
460 }
461 #if wxUSE_CONSTRAINTS
462 else if ( m_constraints )
463 {
464 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
465
466 // our minimal acceptable size is such that all our windows fit inside
467 int maxX = 0,
468 maxY = 0;
469
470 for ( wxWindowList::Node *node = GetChildren().GetFirst();
471 node;
472 node = node->GetNext() )
473 {
474 wxLayoutConstraints *c = node->GetData()->GetConstraints();
475 if ( !c )
476 {
477 // it's not normal that we have an unconstrained child, but
478 // what can we do about it?
479 continue;
480 }
481
482 int x = c->right.GetValue(),
483 y = c->bottom.GetValue();
484
485 if ( x > maxX )
486 maxX = x;
487
488 if ( y > maxY )
489 maxY = y;
490
491 // TODO: we must calculate the overlaps somehow, otherwise we
492 // will never return a size bigger than the current one :-(
493 }
494
495 return wxSize(maxX, maxY);
496 }
497 #endif // wxUSE_CONSTRAINTS
498 else if ( GetChildren().GetCount() > 0 )
499 {
500 // our minimal acceptable size is such that all our windows fit inside
501 int maxX = 0,
502 maxY = 0;
503
504 for ( wxWindowList::Node *node = GetChildren().GetFirst();
505 node;
506 node = node->GetNext() )
507 {
508 wxWindow *win = node->GetData();
509 if ( win->IsTopLevel()
510 #if wxUSE_STATUSBAR
511 || wxDynamicCast(win, wxStatusBar)
512 #endif // wxUSE_STATUSBAR
513 )
514 {
515 // dialogs and frames lie in different top level windows -
516 // don't deal with them here; as for the status bars, they
517 // don't lie in the client area at all
518 continue;
519 }
520
521 int wx, wy, ww, wh;
522 win->GetPosition(&wx, &wy);
523
524 // if the window hadn't been positioned yet, assume that it is in
525 // the origin
526 if ( wx == -1 )
527 wx = 0;
528 if ( wy == -1 )
529 wy = 0;
530
531 win->GetSize(&ww, &wh);
532 if ( wx + ww > maxX )
533 maxX = wx + ww;
534 if ( wy + wh > maxY )
535 maxY = wy + wh;
536 }
537
538 // for compatibility with the old versions and because it really looks
539 // slightly more pretty like this, add a pad
540 maxX += 7;
541 maxY += 14;
542
543 return wxSize(maxX, maxY);
544 }
545 else
546 {
547 // for a generic window there is no natural best size - just use the
548 // current one
549 return GetSize();
550 }
551 }
552
553 // by default the origin is not shifted
554 wxPoint wxWindowBase::GetClientAreaOrigin() const
555 {
556 return wxPoint(0, 0);
557 }
558
559 // set the min/max size of the window
560 void wxWindowBase::SetSizeHints(int minW, int minH,
561 int maxW, int maxH,
562 int WXUNUSED(incW), int WXUNUSED(incH))
563 {
564 m_minWidth = minW;
565 m_maxWidth = maxW;
566 m_minHeight = minH;
567 m_maxHeight = maxH;
568 }
569
570 void wxWindowBase::SetVirtualSizeHints( int minW, int minH,
571 int maxW, int maxH )
572 {
573 m_minVirtualWidth = minW;
574 m_maxVirtualWidth = maxW;
575 m_minVirtualHeight = minH;
576 m_maxVirtualHeight = maxH;
577
578 SetVirtualSize( GetClientSize() );
579 }
580
581 void wxWindowBase::DoSetVirtualSize( int x, int y )
582 {
583 if( m_minVirtualWidth != -1 && m_minVirtualWidth > x ) x = m_minVirtualWidth;
584 if( m_maxVirtualWidth != -1 && m_maxVirtualWidth < x ) x = m_maxVirtualWidth;
585 if( m_minVirtualHeight != -1 && m_minVirtualHeight > y ) y = m_minVirtualHeight;
586 if( m_maxVirtualHeight != -1 && m_maxVirtualHeight < y ) y = m_maxVirtualHeight;
587
588 m_virtualSize.SetWidth( x );
589 m_virtualSize.SetHeight( y );
590 }
591
592 wxSize wxWindowBase::DoGetVirtualSize() const
593 {
594 wxSize s( GetClientSize() );
595
596 if( m_virtualSize.GetWidth() != -1 )
597 s.SetWidth( m_virtualSize.GetWidth() );
598 if( m_virtualSize.GetHeight() != -1 )
599 s.SetHeight( m_virtualSize.GetHeight() );
600
601 return s;
602 }
603
604 // ----------------------------------------------------------------------------
605 // show/hide/enable/disable the window
606 // ----------------------------------------------------------------------------
607
608 bool wxWindowBase::Show(bool show)
609 {
610 if ( show != m_isShown )
611 {
612 m_isShown = show;
613
614 return TRUE;
615 }
616 else
617 {
618 return FALSE;
619 }
620 }
621
622 bool wxWindowBase::Enable(bool enable)
623 {
624 if ( enable != m_isEnabled )
625 {
626 m_isEnabled = enable;
627
628 return TRUE;
629 }
630 else
631 {
632 return FALSE;
633 }
634 }
635 // ----------------------------------------------------------------------------
636 // RTTI
637 // ----------------------------------------------------------------------------
638
639 bool wxWindowBase::IsTopLevel() const
640 {
641 return FALSE;
642 }
643
644 // ----------------------------------------------------------------------------
645 // reparenting the window
646 // ----------------------------------------------------------------------------
647
648 void wxWindowBase::AddChild(wxWindowBase *child)
649 {
650 wxCHECK_RET( child, wxT("can't add a NULL child") );
651
652 // this should never happen and it will lead to a crash later if it does
653 // because RemoveChild() will remove only one node from the children list
654 // and the other(s) one(s) will be left with dangling pointers in them
655 wxASSERT_MSG( !GetChildren().Find(child), _T("AddChild() called twice") );
656
657 GetChildren().Append(child);
658 child->SetParent(this);
659 }
660
661 void wxWindowBase::RemoveChild(wxWindowBase *child)
662 {
663 wxCHECK_RET( child, wxT("can't remove a NULL child") );
664
665 GetChildren().DeleteObject(child);
666 child->SetParent((wxWindow *)NULL);
667 }
668
669 bool wxWindowBase::Reparent(wxWindowBase *newParent)
670 {
671 wxWindow *oldParent = GetParent();
672 if ( newParent == oldParent )
673 {
674 // nothing done
675 return FALSE;
676 }
677
678 // unlink this window from the existing parent.
679 if ( oldParent )
680 {
681 oldParent->RemoveChild(this);
682 }
683 else
684 {
685 wxTopLevelWindows.DeleteObject(this);
686 }
687
688 // add it to the new one
689 if ( newParent )
690 {
691 newParent->AddChild(this);
692 }
693 else
694 {
695 wxTopLevelWindows.Append(this);
696 }
697
698 return TRUE;
699 }
700
701 // ----------------------------------------------------------------------------
702 // event handler stuff
703 // ----------------------------------------------------------------------------
704
705 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
706 {
707 handler->SetNextHandler(GetEventHandler());
708 SetEventHandler(handler);
709 }
710
711 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
712 {
713 wxEvtHandler *handlerA = GetEventHandler();
714 if ( handlerA )
715 {
716 wxEvtHandler *handlerB = handlerA->GetNextHandler();
717 handlerA->SetNextHandler((wxEvtHandler *)NULL);
718 SetEventHandler(handlerB);
719 if ( deleteHandler )
720 {
721 delete handlerA;
722 handlerA = (wxEvtHandler *)NULL;
723 }
724 }
725
726 return handlerA;
727 }
728
729 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handler)
730 {
731 wxCHECK_MSG( handler, FALSE, _T("RemoveEventHandler(NULL) called") );
732
733 wxEvtHandler *handlerPrev = NULL,
734 *handlerCur = GetEventHandler();
735 while ( handlerCur )
736 {
737 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
738
739 if ( handlerCur == handler )
740 {
741 if ( handlerPrev )
742 {
743 handlerPrev->SetNextHandler(handlerNext);
744 }
745 else
746 {
747 SetEventHandler(handlerNext);
748 }
749
750 handler->SetNextHandler(NULL);
751
752 return TRUE;
753 }
754
755 handlerPrev = handlerCur;
756 handlerCur = handlerNext;
757 }
758
759 wxFAIL_MSG( _T("where has the event handler gone?") );
760
761 return FALSE;
762 }
763
764 // ----------------------------------------------------------------------------
765 // cursors, fonts &c
766 // ----------------------------------------------------------------------------
767
768 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
769 {
770 if ( !colour.Ok() || (colour == m_backgroundColour) )
771 return FALSE;
772
773 m_backgroundColour = colour;
774
775 m_hasBgCol = TRUE;
776
777 return TRUE;
778 }
779
780 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
781 {
782 if ( !colour.Ok() || (colour == m_foregroundColour) )
783 return FALSE;
784
785 m_foregroundColour = colour;
786
787 m_hasFgCol = TRUE;
788
789 return TRUE;
790 }
791
792 bool wxWindowBase::SetCursor(const wxCursor& cursor)
793 {
794 // setting an invalid cursor is ok, it means that we don't have any special
795 // cursor
796 if ( m_cursor == cursor )
797 {
798 // no change
799 return FALSE;
800 }
801
802 m_cursor = cursor;
803
804 return TRUE;
805 }
806
807 bool wxWindowBase::SetFont(const wxFont& font)
808 {
809 // don't try to set invalid font, always fall back to the default
810 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
811
812 if ( fontOk == m_font )
813 {
814 // no change
815 return FALSE;
816 }
817
818 m_font = fontOk;
819
820 m_hasFont = TRUE;
821
822 return TRUE;
823 }
824
825 #if wxUSE_PALETTE
826
827 void wxWindowBase::SetPalette(const wxPalette& pal)
828 {
829 m_hasCustomPalette = TRUE;
830 m_palette = pal;
831
832 // VZ: can anyone explain me what do we do here?
833 wxWindowDC d((wxWindow *) this);
834 d.SetPalette(pal);
835 }
836
837 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
838 {
839 wxWindow *win = (wxWindow *)this;
840 while ( win && !win->HasCustomPalette() )
841 {
842 win = win->GetParent();
843 }
844
845 return win;
846 }
847
848 #endif // wxUSE_PALETTE
849
850 #if wxUSE_CARET
851 void wxWindowBase::SetCaret(wxCaret *caret)
852 {
853 if ( m_caret )
854 {
855 delete m_caret;
856 }
857
858 m_caret = caret;
859
860 if ( m_caret )
861 {
862 wxASSERT_MSG( m_caret->GetWindow() == this,
863 wxT("caret should be created associated to this window") );
864 }
865 }
866 #endif // wxUSE_CARET
867
868 #if wxUSE_VALIDATORS
869 // ----------------------------------------------------------------------------
870 // validators
871 // ----------------------------------------------------------------------------
872
873 void wxWindowBase::SetValidator(const wxValidator& validator)
874 {
875 if ( m_windowValidator )
876 delete m_windowValidator;
877
878 m_windowValidator = (wxValidator *)validator.Clone();
879
880 if ( m_windowValidator )
881 m_windowValidator->SetWindow(this) ;
882 }
883 #endif // wxUSE_VALIDATORS
884
885 // ----------------------------------------------------------------------------
886 // update region stuff
887 // ----------------------------------------------------------------------------
888
889 wxRect wxWindowBase::GetUpdateClientRect() const
890 {
891 wxRegion rgnUpdate = GetUpdateRegion();
892 rgnUpdate.Intersect(GetClientRect());
893 wxRect rectUpdate = rgnUpdate.GetBox();
894 wxPoint ptOrigin = GetClientAreaOrigin();
895 rectUpdate.x -= ptOrigin.x;
896 rectUpdate.y -= ptOrigin.y;
897
898 return rectUpdate;
899 }
900
901 bool wxWindowBase::IsExposed(int x, int y) const
902 {
903 return m_updateRegion.Contains(x, y) != wxOutRegion;
904 }
905
906 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
907 {
908 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
909 }
910
911 // ----------------------------------------------------------------------------
912 // find child window by id or name
913 // ----------------------------------------------------------------------------
914
915 wxWindow *wxWindowBase::FindWindow( long id )
916 {
917 if ( id == m_windowId )
918 return (wxWindow *)this;
919
920 wxWindowBase *res = (wxWindow *)NULL;
921 wxWindowList::Node *node;
922 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
923 {
924 wxWindowBase *child = node->GetData();
925 res = child->FindWindow( id );
926 }
927
928 return (wxWindow *)res;
929 }
930
931 wxWindow *wxWindowBase::FindWindow( const wxString& name )
932 {
933 if ( name == m_windowName )
934 return (wxWindow *)this;
935
936 wxWindowBase *res = (wxWindow *)NULL;
937 wxWindowList::Node *node;
938 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
939 {
940 wxWindow *child = node->GetData();
941 res = child->FindWindow(name);
942 }
943
944 return (wxWindow *)res;
945 }
946
947
948 // find any window by id or name or label: If parent is non-NULL, look through
949 // children for a label or title matching the specified string. If NULL, look
950 // through all top-level windows.
951 //
952 // to avoid duplicating code we reuse the same helper function but with
953 // different comparators
954
955 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
956 const wxString& label, long id);
957
958 static
959 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
960 long WXUNUSED(id))
961 {
962 return win->GetLabel() == label;
963 }
964
965 static
966 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
967 long WXUNUSED(id))
968 {
969 return win->GetName() == label;
970 }
971
972 static
973 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
974 long id)
975 {
976 return win->GetId() == id;
977 }
978
979 // recursive helper for the FindWindowByXXX() functions
980 static
981 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
982 const wxString& label,
983 long id,
984 wxFindWindowCmp cmp)
985 {
986 if ( parent )
987 {
988 // see if this is the one we're looking for
989 if ( (*cmp)(parent, label, id) )
990 return (wxWindow *)parent;
991
992 // It wasn't, so check all its children
993 for ( wxWindowList::Node * node = parent->GetChildren().GetFirst();
994 node;
995 node = node->GetNext() )
996 {
997 // recursively check each child
998 wxWindow *win = (wxWindow *)node->GetData();
999 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1000 if (retwin)
1001 return retwin;
1002 }
1003 }
1004
1005 // Not found
1006 return NULL;
1007 }
1008
1009 // helper for FindWindowByXXX()
1010 static
1011 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1012 const wxString& label,
1013 long id,
1014 wxFindWindowCmp cmp)
1015 {
1016 if ( parent )
1017 {
1018 // just check parent and all its children
1019 return wxFindWindowRecursively(parent, label, id, cmp);
1020 }
1021
1022 // start at very top of wx's windows
1023 for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
1024 node;
1025 node = node->GetNext() )
1026 {
1027 // recursively check each window & its children
1028 wxWindow *win = node->GetData();
1029 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1030 if (retwin)
1031 return retwin;
1032 }
1033
1034 return NULL;
1035 }
1036
1037 /* static */
1038 wxWindow *
1039 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1040 {
1041 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1042 }
1043
1044 /* static */
1045 wxWindow *
1046 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1047 {
1048 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1049
1050 if ( !win )
1051 {
1052 // fall back to the label
1053 win = FindWindowByLabel(title, parent);
1054 }
1055
1056 return win;
1057 }
1058
1059 /* static */
1060 wxWindow *
1061 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1062 {
1063 return wxFindWindowHelper(parent, _T(""), id, wxFindWindowCmpIds);
1064 }
1065
1066 // ----------------------------------------------------------------------------
1067 // dialog oriented functions
1068 // ----------------------------------------------------------------------------
1069
1070 void wxWindowBase::MakeModal(bool modal)
1071 {
1072 // Disable all other windows
1073 if ( IsTopLevel() )
1074 {
1075 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
1076 while (node)
1077 {
1078 wxWindow *win = node->GetData();
1079 if (win != this)
1080 win->Enable(!modal);
1081
1082 node = node->GetNext();
1083 }
1084 }
1085 }
1086
1087 bool wxWindowBase::Validate()
1088 {
1089 #if wxUSE_VALIDATORS
1090 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1091
1092 wxWindowList::Node *node;
1093 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1094 {
1095 wxWindowBase *child = node->GetData();
1096 wxValidator *validator = child->GetValidator();
1097 if ( validator && !validator->Validate((wxWindow *)this) )
1098 {
1099 return FALSE;
1100 }
1101
1102 if ( recurse && !child->Validate() )
1103 {
1104 return FALSE;
1105 }
1106 }
1107 #endif // wxUSE_VALIDATORS
1108
1109 return TRUE;
1110 }
1111
1112 bool wxWindowBase::TransferDataToWindow()
1113 {
1114 #if wxUSE_VALIDATORS
1115 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1116
1117 wxWindowList::Node *node;
1118 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1119 {
1120 wxWindowBase *child = node->GetData();
1121 wxValidator *validator = child->GetValidator();
1122 if ( validator && !validator->TransferToWindow() )
1123 {
1124 wxLogWarning(_("Could not transfer data to window"));
1125 wxLog::FlushActive();
1126
1127 return FALSE;
1128 }
1129
1130 if ( recurse )
1131 {
1132 if ( !child->TransferDataToWindow() )
1133 {
1134 // warning already given
1135 return FALSE;
1136 }
1137 }
1138 }
1139 #endif // wxUSE_VALIDATORS
1140
1141 return TRUE;
1142 }
1143
1144 bool wxWindowBase::TransferDataFromWindow()
1145 {
1146 #if wxUSE_VALIDATORS
1147 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1148
1149 wxWindowList::Node *node;
1150 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1151 {
1152 wxWindow *child = node->GetData();
1153 wxValidator *validator = child->GetValidator();
1154 if ( validator && !validator->TransferFromWindow() )
1155 {
1156 // nop warning here because the application is supposed to give
1157 // one itself - we don't know here what might have gone wrongly
1158
1159 return FALSE;
1160 }
1161
1162 if ( recurse )
1163 {
1164 if ( !child->TransferDataFromWindow() )
1165 {
1166 // warning already given
1167 return FALSE;
1168 }
1169 }
1170 }
1171 #endif // wxUSE_VALIDATORS
1172
1173 return TRUE;
1174 }
1175
1176 void wxWindowBase::InitDialog()
1177 {
1178 wxInitDialogEvent event(GetId());
1179 event.SetEventObject( this );
1180 GetEventHandler()->ProcessEvent(event);
1181 }
1182
1183 // ----------------------------------------------------------------------------
1184 // context-sensitive help support
1185 // ----------------------------------------------------------------------------
1186
1187 #if wxUSE_HELP
1188
1189 // associate this help text with this window
1190 void wxWindowBase::SetHelpText(const wxString& text)
1191 {
1192 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1193 if ( helpProvider )
1194 {
1195 helpProvider->AddHelp(this, text);
1196 }
1197 }
1198
1199 // associate this help text with all windows with the same id as this
1200 // one
1201 void wxWindowBase::SetHelpTextForId(const wxString& text)
1202 {
1203 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1204 if ( helpProvider )
1205 {
1206 helpProvider->AddHelp(GetId(), text);
1207 }
1208 }
1209
1210 // get the help string associated with this window (may be empty)
1211 wxString wxWindowBase::GetHelpText() const
1212 {
1213 wxString text;
1214 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1215 if ( helpProvider )
1216 {
1217 text = helpProvider->GetHelp(this);
1218 }
1219
1220 return text;
1221 }
1222
1223 // show help for this window
1224 void wxWindowBase::OnHelp(wxHelpEvent& event)
1225 {
1226 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1227 if ( helpProvider )
1228 {
1229 if ( helpProvider->ShowHelp(this) )
1230 {
1231 // skip the event.Skip() below
1232 return;
1233 }
1234 }
1235
1236 event.Skip();
1237 }
1238
1239 #endif // wxUSE_HELP
1240
1241 // ----------------------------------------------------------------------------
1242 // tooltipsroot.Replace("\\", "/");
1243 // ----------------------------------------------------------------------------
1244
1245 #if wxUSE_TOOLTIPS
1246
1247 void wxWindowBase::SetToolTip( const wxString &tip )
1248 {
1249 // don't create the new tooltip if we already have one
1250 if ( m_tooltip )
1251 {
1252 m_tooltip->SetTip( tip );
1253 }
1254 else
1255 {
1256 SetToolTip( new wxToolTip( tip ) );
1257 }
1258
1259 // setting empty tooltip text does not remove the tooltip any more - use
1260 // SetToolTip((wxToolTip *)NULL) for this
1261 }
1262
1263 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1264 {
1265 if ( m_tooltip )
1266 delete m_tooltip;
1267
1268 m_tooltip = tooltip;
1269 }
1270
1271 #endif // wxUSE_TOOLTIPS
1272
1273 // ----------------------------------------------------------------------------
1274 // constraints and sizers
1275 // ----------------------------------------------------------------------------
1276
1277 #if wxUSE_CONSTRAINTS
1278
1279 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1280 {
1281 if ( m_constraints )
1282 {
1283 UnsetConstraints(m_constraints);
1284 delete m_constraints;
1285 }
1286 m_constraints = constraints;
1287 if ( m_constraints )
1288 {
1289 // Make sure other windows know they're part of a 'meaningful relationship'
1290 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1291 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1292 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1293 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1294 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1295 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1296 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1297 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1298 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1299 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1300 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1301 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1302 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1303 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1304 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1305 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1306 }
1307 }
1308
1309 // This removes any dangling pointers to this window in other windows'
1310 // constraintsInvolvedIn lists.
1311 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1312 {
1313 if ( c )
1314 {
1315 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1316 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1317 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1318 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1319 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1320 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1321 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1322 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1323 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1324 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1325 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1326 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1327 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1328 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1329 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1330 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1331 }
1332 }
1333
1334 // Back-pointer to other windows we're involved with, so if we delete this
1335 // window, we must delete any constraints we're involved with.
1336 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1337 {
1338 if ( !m_constraintsInvolvedIn )
1339 m_constraintsInvolvedIn = new wxWindowList;
1340 if ( !m_constraintsInvolvedIn->Find(otherWin) )
1341 m_constraintsInvolvedIn->Append(otherWin);
1342 }
1343
1344 // REMOVE back-pointer to other windows we're involved with.
1345 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1346 {
1347 if ( m_constraintsInvolvedIn )
1348 m_constraintsInvolvedIn->DeleteObject(otherWin);
1349 }
1350
1351 // Reset any constraints that mention this window
1352 void wxWindowBase::DeleteRelatedConstraints()
1353 {
1354 if ( m_constraintsInvolvedIn )
1355 {
1356 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
1357 while (node)
1358 {
1359 wxWindow *win = node->GetData();
1360 wxLayoutConstraints *constr = win->GetConstraints();
1361
1362 // Reset any constraints involving this window
1363 if ( constr )
1364 {
1365 constr->left.ResetIfWin(this);
1366 constr->top.ResetIfWin(this);
1367 constr->right.ResetIfWin(this);
1368 constr->bottom.ResetIfWin(this);
1369 constr->width.ResetIfWin(this);
1370 constr->height.ResetIfWin(this);
1371 constr->centreX.ResetIfWin(this);
1372 constr->centreY.ResetIfWin(this);
1373 }
1374
1375 wxWindowList::Node *next = node->GetNext();
1376 delete node;
1377 node = next;
1378 }
1379
1380 delete m_constraintsInvolvedIn;
1381 m_constraintsInvolvedIn = (wxWindowList *) NULL;
1382 }
1383 }
1384
1385 #endif // wxUSE_CONSTRAINTS
1386
1387 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
1388 {
1389 if ( deleteOld )
1390 delete m_windowSizer;
1391
1392 m_windowSizer = sizer;
1393
1394 SetAutoLayout( sizer != NULL );
1395 }
1396
1397 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
1398 {
1399 SetSizer( sizer, deleteOld );
1400 sizer->SetSizeHints( (wxWindow*) this );
1401 }
1402
1403 #if wxUSE_CONSTRAINTS
1404
1405 void wxWindowBase::SatisfyConstraints()
1406 {
1407 wxLayoutConstraints *constr = GetConstraints();
1408 bool wasOk = constr && constr->AreSatisfied();
1409
1410 ResetConstraints(); // Mark all constraints as unevaluated
1411
1412 int noChanges = 1;
1413
1414 // if we're a top level panel (i.e. our parent is frame/dialog), our
1415 // own constraints will never be satisfied any more unless we do it
1416 // here
1417 if ( wasOk )
1418 {
1419 while ( noChanges > 0 )
1420 {
1421 LayoutPhase1(&noChanges);
1422 }
1423 }
1424
1425 LayoutPhase2(&noChanges);
1426 }
1427
1428 #endif // wxUSE_CONSTRAINTS
1429
1430 bool wxWindowBase::Layout()
1431 {
1432 // If there is a sizer, use it instead of the constraints
1433 if ( GetSizer() )
1434 {
1435 int w, h;
1436 GetVirtualSize(&w, &h);
1437 GetSizer()->SetDimension( 0, 0, w, h );
1438 }
1439 #if wxUSE_CONSTRAINTS
1440 else
1441 {
1442 SatisfyConstraints(); // Find the right constraints values
1443 SetConstraintSizes(); // Recursively set the real window sizes
1444 }
1445 #endif
1446
1447 return TRUE;
1448 }
1449
1450 #if wxUSE_CONSTRAINTS
1451
1452 // first phase of the constraints evaluation: set our own constraints
1453 bool wxWindowBase::LayoutPhase1(int *noChanges)
1454 {
1455 wxLayoutConstraints *constr = GetConstraints();
1456
1457 return !constr || constr->SatisfyConstraints(this, noChanges);
1458 }
1459
1460 // second phase: set the constraints for our children
1461 bool wxWindowBase::LayoutPhase2(int *noChanges)
1462 {
1463 *noChanges = 0;
1464
1465 // Layout children
1466 DoPhase(1);
1467
1468 // Layout grand children
1469 DoPhase(2);
1470
1471 return TRUE;
1472 }
1473
1474 // Do a phase of evaluating child constraints
1475 bool wxWindowBase::DoPhase(int phase)
1476 {
1477 // the list containing the children for which the constraints are already
1478 // set correctly
1479 wxWindowList succeeded;
1480
1481 // the max number of iterations we loop before concluding that we can't set
1482 // the constraints
1483 static const int maxIterations = 500;
1484
1485 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
1486 {
1487 int noChanges = 0;
1488
1489 // loop over all children setting their constraints
1490 for ( wxWindowList::Node *node = GetChildren().GetFirst();
1491 node;
1492 node = node->GetNext() )
1493 {
1494 wxWindow *child = node->GetData();
1495 if ( child->IsTopLevel() )
1496 {
1497 // top level children are not inside our client area
1498 continue;
1499 }
1500
1501 if ( !child->GetConstraints() || succeeded.Find(child) )
1502 {
1503 // this one is either already ok or nothing we can do about it
1504 continue;
1505 }
1506
1507 int tempNoChanges = 0;
1508 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
1509 : child->LayoutPhase2(&tempNoChanges);
1510 noChanges += tempNoChanges;
1511
1512 if ( success )
1513 {
1514 succeeded.Append(child);
1515 }
1516 }
1517
1518 if ( !noChanges )
1519 {
1520 // constraints are set
1521 break;
1522 }
1523 }
1524
1525 return TRUE;
1526 }
1527
1528 void wxWindowBase::ResetConstraints()
1529 {
1530 wxLayoutConstraints *constr = GetConstraints();
1531 if ( constr )
1532 {
1533 constr->left.SetDone(FALSE);
1534 constr->top.SetDone(FALSE);
1535 constr->right.SetDone(FALSE);
1536 constr->bottom.SetDone(FALSE);
1537 constr->width.SetDone(FALSE);
1538 constr->height.SetDone(FALSE);
1539 constr->centreX.SetDone(FALSE);
1540 constr->centreY.SetDone(FALSE);
1541 }
1542
1543 wxWindowList::Node *node = GetChildren().GetFirst();
1544 while (node)
1545 {
1546 wxWindow *win = node->GetData();
1547 if ( !win->IsTopLevel() )
1548 win->ResetConstraints();
1549 node = node->GetNext();
1550 }
1551 }
1552
1553 // Need to distinguish between setting the 'fake' size for windows and sizers,
1554 // and setting the real values.
1555 void wxWindowBase::SetConstraintSizes(bool recurse)
1556 {
1557 wxLayoutConstraints *constr = GetConstraints();
1558 if ( constr && constr->AreSatisfied() )
1559 {
1560 int x = constr->left.GetValue();
1561 int y = constr->top.GetValue();
1562 int w = constr->width.GetValue();
1563 int h = constr->height.GetValue();
1564
1565 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1566 (constr->height.GetRelationship() != wxAsIs) )
1567 {
1568 SetSize(x, y, w, h);
1569 }
1570 else
1571 {
1572 // If we don't want to resize this window, just move it...
1573 Move(x, y);
1574 }
1575 }
1576 else if ( constr )
1577 {
1578 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1579 GetClassInfo()->GetClassName(),
1580 GetName().c_str());
1581 }
1582
1583 if ( recurse )
1584 {
1585 wxWindowList::Node *node = GetChildren().GetFirst();
1586 while (node)
1587 {
1588 wxWindow *win = node->GetData();
1589 if ( !win->IsTopLevel() && win->GetConstraints() )
1590 win->SetConstraintSizes();
1591 node = node->GetNext();
1592 }
1593 }
1594 }
1595
1596 // Only set the size/position of the constraint (if any)
1597 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1598 {
1599 wxLayoutConstraints *constr = GetConstraints();
1600 if ( constr )
1601 {
1602 if ( x != -1 )
1603 {
1604 constr->left.SetValue(x);
1605 constr->left.SetDone(TRUE);
1606 }
1607 if ( y != -1 )
1608 {
1609 constr->top.SetValue(y);
1610 constr->top.SetDone(TRUE);
1611 }
1612 if ( w != -1 )
1613 {
1614 constr->width.SetValue(w);
1615 constr->width.SetDone(TRUE);
1616 }
1617 if ( h != -1 )
1618 {
1619 constr->height.SetValue(h);
1620 constr->height.SetDone(TRUE);
1621 }
1622 }
1623 }
1624
1625 void wxWindowBase::MoveConstraint(int x, int y)
1626 {
1627 wxLayoutConstraints *constr = GetConstraints();
1628 if ( constr )
1629 {
1630 if ( x != -1 )
1631 {
1632 constr->left.SetValue(x);
1633 constr->left.SetDone(TRUE);
1634 }
1635 if ( y != -1 )
1636 {
1637 constr->top.SetValue(y);
1638 constr->top.SetDone(TRUE);
1639 }
1640 }
1641 }
1642
1643 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1644 {
1645 wxLayoutConstraints *constr = GetConstraints();
1646 if ( constr )
1647 {
1648 *w = constr->width.GetValue();
1649 *h = constr->height.GetValue();
1650 }
1651 else
1652 GetSize(w, h);
1653 }
1654
1655 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1656 {
1657 wxLayoutConstraints *constr = GetConstraints();
1658 if ( constr )
1659 {
1660 *w = constr->width.GetValue();
1661 *h = constr->height.GetValue();
1662 }
1663 else
1664 GetClientSize(w, h);
1665 }
1666
1667 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1668 {
1669 wxLayoutConstraints *constr = GetConstraints();
1670 if ( constr )
1671 {
1672 *x = constr->left.GetValue();
1673 *y = constr->top.GetValue();
1674 }
1675 else
1676 GetPosition(x, y);
1677 }
1678
1679 #endif // wxUSE_CONSTRAINTS
1680
1681 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
1682 {
1683 // don't do it for the dialogs/frames - they float independently of their
1684 // parent
1685 if ( !IsTopLevel() )
1686 {
1687 wxWindow *parent = GetParent();
1688 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
1689 {
1690 wxPoint pt(parent->GetClientAreaOrigin());
1691 x += pt.x;
1692 y += pt.y;
1693 }
1694 }
1695 }
1696
1697 // ----------------------------------------------------------------------------
1698 // do Update UI processing for child controls
1699 // ----------------------------------------------------------------------------
1700
1701 // TODO: should this be implemented for the child window rather
1702 // than the parent? Then you can override it e.g. for wxCheckBox
1703 // to do the Right Thing rather than having to assume a fixed number
1704 // of control classes.
1705 void wxWindowBase::UpdateWindowUI()
1706 {
1707 #if wxUSE_CONTROLS
1708 wxUpdateUIEvent event(GetId());
1709 event.m_eventObject = this;
1710
1711 if ( GetEventHandler()->ProcessEvent(event) )
1712 {
1713 if ( event.GetSetEnabled() )
1714 Enable(event.GetEnabled());
1715
1716 if ( event.GetSetText() )
1717 {
1718 wxControl *control = wxDynamicCastThis(wxControl);
1719 if ( control )
1720 {
1721 #if wxUSE_TEXTCTRL
1722 wxTextCtrl *text = wxDynamicCast(control, wxTextCtrl);
1723 if ( text )
1724 text->SetValue(event.GetText());
1725 else
1726 #endif // wxUSE_TEXTCTRL
1727 control->SetLabel(event.GetText());
1728 }
1729 }
1730
1731 #if wxUSE_CHECKBOX
1732 wxCheckBox *checkbox = wxDynamicCastThis(wxCheckBox);
1733 if ( checkbox )
1734 {
1735 if ( event.GetSetChecked() )
1736 checkbox->SetValue(event.GetChecked());
1737 }
1738 #endif // wxUSE_CHECKBOX
1739
1740 #if wxUSE_RADIOBTN
1741 wxRadioButton *radiobtn = wxDynamicCastThis(wxRadioButton);
1742 if ( radiobtn )
1743 {
1744 if ( event.GetSetChecked() )
1745 radiobtn->SetValue(event.GetChecked());
1746 }
1747 #endif // wxUSE_RADIOBTN
1748 }
1749 #endif // wxUSE_CONTROLS
1750 }
1751
1752 // ----------------------------------------------------------------------------
1753 // dialog units translations
1754 // ----------------------------------------------------------------------------
1755
1756 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
1757 {
1758 int charWidth = GetCharWidth();
1759 int charHeight = GetCharHeight();
1760 wxPoint pt2(-1, -1);
1761 if (pt.x != -1)
1762 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1763 if (pt.y != -1)
1764 pt2.y = (int) ((pt.y * 8) / charHeight) ;
1765
1766 return pt2;
1767 }
1768
1769 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
1770 {
1771 int charWidth = GetCharWidth();
1772 int charHeight = GetCharHeight();
1773 wxPoint pt2(-1, -1);
1774 if (pt.x != -1)
1775 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1776 if (pt.y != -1)
1777 pt2.y = (int) ((pt.y * charHeight) / 8) ;
1778
1779 return pt2;
1780 }
1781
1782 // ----------------------------------------------------------------------------
1783 // event handlers
1784 // ----------------------------------------------------------------------------
1785
1786 // propagate the colour change event to the subwindows
1787 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1788 {
1789 wxWindowList::Node *node = GetChildren().GetFirst();
1790 while ( node )
1791 {
1792 // Only propagate to non-top-level windows
1793 wxWindow *win = node->GetData();
1794 if ( !win->IsTopLevel() )
1795 {
1796 wxSysColourChangedEvent event2;
1797 event.m_eventObject = win;
1798 win->GetEventHandler()->ProcessEvent(event2);
1799 }
1800
1801 node = node->GetNext();
1802 }
1803 }
1804
1805 // the default action is to populate dialog with data when it's created
1806 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1807 {
1808 TransferDataToWindow();
1809 }
1810
1811 // process Ctrl-Alt-mclick
1812 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
1813 {
1814 #if wxUSE_MSGDLG
1815 if ( event.ControlDown() && event.AltDown() )
1816 {
1817 // don't translate these strings
1818 wxString port;
1819
1820 #ifdef __WXUNIVERSAL__
1821 port = _T("Univ/");
1822 #endif // __WXUNIVERSAL__
1823
1824 switch ( wxGetOsVersion() )
1825 {
1826 case wxMOTIF_X: port = _T("Motif"); break;
1827 case wxMAC:
1828 case wxMAC_DARWIN: port = _T("Mac"); break;
1829 case wxBEOS: port = _T("BeOS"); break;
1830 case wxGTK:
1831 case wxGTK_WIN32:
1832 case wxGTK_OS2:
1833 case wxGTK_BEOS: port = _T("GTK"); break;
1834 case wxWINDOWS:
1835 case wxPENWINDOWS:
1836 case wxWINDOWS_NT:
1837 case wxWIN32S:
1838 case wxWIN95:
1839 case wxWIN386: port = _T("MS Windows"); break;
1840 case wxMGL_UNIX:
1841 case wxMGL_X:
1842 case wxMGL_WIN32:
1843 case wxMGL_OS2: port = _T("MGL"); break;
1844 case wxWINDOWS_OS2:
1845 case wxOS2_PM: port = _T("OS/2"); break;
1846 default: port = _T("unknown"); break;
1847 }
1848
1849 wxMessageBox(wxString::Format(
1850 _T(
1851 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1852 ),
1853 port.c_str(),
1854 wxMAJOR_VERSION,
1855 wxMINOR_VERSION,
1856 wxRELEASE_NUMBER,
1857 #if wxUSE_UNICODE
1858 L" (Unicode)",
1859 #else
1860 "",
1861 #endif
1862 __TDATE__,
1863 __TTIME__
1864 ),
1865 _T("wxWindows information"),
1866 wxICON_INFORMATION | wxOK,
1867 (wxWindow *)this);
1868 }
1869 else
1870 #endif // wxUSE_MSGDLG
1871 {
1872 event.Skip();
1873 }
1874 }
1875
1876 // ----------------------------------------------------------------------------
1877 // list classes implementation
1878 // ----------------------------------------------------------------------------
1879
1880 void wxWindowListNode::DeleteData()
1881 {
1882 delete (wxWindow *)GetData();
1883 }
1884
1885 // ----------------------------------------------------------------------------
1886 // borders
1887 // ----------------------------------------------------------------------------
1888
1889 wxBorder wxWindowBase::GetBorder() const
1890 {
1891 wxBorder border = (wxBorder)(m_windowStyle & wxBORDER_MASK);
1892 if ( border == wxBORDER_DEFAULT )
1893 {
1894 border = GetDefaultBorder();
1895 }
1896
1897 return border;
1898 }
1899
1900 wxBorder wxWindowBase::GetDefaultBorder() const
1901 {
1902 return wxBORDER_NONE;
1903 }
1904
1905 // ----------------------------------------------------------------------------
1906 // hit testing
1907 // ----------------------------------------------------------------------------
1908
1909 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
1910 {
1911 // here we just check if the point is inside the window or not
1912
1913 // check the top and left border first
1914 bool outside = x < 0 || y < 0;
1915 if ( !outside )
1916 {
1917 // check the right and bottom borders too
1918 wxSize size = GetSize();
1919 outside = x >= size.x || y >= size.y;
1920 }
1921
1922 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
1923 }
1924
1925 // ----------------------------------------------------------------------------
1926 // mouse capture
1927 // ----------------------------------------------------------------------------
1928
1929 struct WXDLLEXPORT wxWindowNext
1930 {
1931 wxWindow *win;
1932 wxWindowNext *next;
1933 } *wxWindowBase::ms_winCaptureNext = NULL;
1934
1935 void wxWindowBase::CaptureMouse()
1936 {
1937 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1938
1939 wxWindow *winOld = GetCapture();
1940 if ( winOld )
1941 {
1942 ((wxWindowBase*) winOld)->DoReleaseMouse();
1943
1944 // save it on stack
1945 wxWindowNext *item = new wxWindowNext;
1946 item->win = winOld;
1947 item->next = ms_winCaptureNext;
1948 ms_winCaptureNext = item;
1949 }
1950 //else: no mouse capture to save
1951
1952 DoCaptureMouse();
1953 }
1954
1955 void wxWindowBase::ReleaseMouse()
1956 {
1957 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1958
1959 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1960
1961 DoReleaseMouse();
1962
1963 if ( ms_winCaptureNext )
1964 {
1965 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
1966
1967 wxWindowNext *item = ms_winCaptureNext;
1968 ms_winCaptureNext = item->next;
1969 delete item;
1970 }
1971 //else: stack is empty, no previous capture
1972
1973 wxLogTrace(_T("mousecapture"),
1974 _T("After ReleaseMouse() mouse is captured by 0x%08x"),
1975 GetCapture());
1976 }
1977
1978 // ----------------------------------------------------------------------------
1979 // global functions
1980 // ----------------------------------------------------------------------------
1981
1982 wxWindow* wxGetTopLevelParent(wxWindow *win)
1983 {
1984 while ( win && !win->IsTopLevel() )
1985 win = win->GetParent();
1986
1987 return win;
1988 }
1989