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