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