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