]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
added CentreOnScreen(), updated the docs to clear this mess a bit
[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/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #endif //WX_PRECOMP
45
46 #if wxUSE_CONSTRAINTS
47 #include "wx/layout.h"
48 #include "wx/sizer.h"
49 #endif // wxUSE_CONSTRAINTS
50
51 #if wxUSE_DRAG_AND_DROP
52 #include "wx/dnd.h"
53 #endif // wxUSE_DRAG_AND_DROP
54
55 #if wxUSE_TOOLTIPS
56 #include "wx/tooltip.h"
57 #endif // wxUSE_TOOLTIPS
58
59 #if wxUSE_CARET
60 #include "wx/caret.h"
61 #endif // wxUSE_CARET
62
63 // ----------------------------------------------------------------------------
64 // static data
65 // ----------------------------------------------------------------------------
66
67 int wxWindowBase::ms_lastControlId = -200;
68
69 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
70
71 // ----------------------------------------------------------------------------
72 // event table
73 // ----------------------------------------------------------------------------
74
75 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
76 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
77 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
78 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
79 END_EVENT_TABLE()
80
81 // ============================================================================
82 // implementation of the common functionality of the wxWindow class
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // initialization
87 // ----------------------------------------------------------------------------
88
89 // the default initialization
90 void wxWindowBase::InitBase()
91 {
92 // no window yet, no parent nor children
93 m_parent = (wxWindow *)NULL;
94 m_windowId = -1;
95 m_children.DeleteContents( FALSE ); // don't auto delete node data
96
97 // no constraints on the minimal window size
98 m_minWidth =
99 m_minHeight =
100 m_maxWidth =
101 m_maxHeight = -1;
102
103 // window is created enabled but it's not visible yet
104 m_isShown = FALSE;
105 m_isEnabled = TRUE;
106
107 // no client data (yet)
108 m_clientData = NULL;
109 m_clientDataType = ClientData_None;
110
111 // the default event handler is just this window
112 m_eventHandler = this;
113
114 #if wxUSE_VALIDATORS
115 // no validator
116 m_windowValidator = (wxValidator *) NULL;
117 #endif // wxUSE_VALIDATORS
118
119 // use the system default colours
120 wxSystemSettings settings;
121
122 m_backgroundColour = settings.GetSystemColour(wxSYS_COLOUR_BTNFACE);
123 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
124 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
125
126 #if !defined(__WXMAC__) && !defined(__WXGTK__)
127 m_font = *wxSWISS_FONT; // and this?
128 #else
129 m_font = settings.GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
130 #endif
131
132 // no style bits
133 m_exStyle =
134 m_windowStyle = 0;
135
136 // an optimization for the event processing: checking this flag is much
137 // faster than using IsKindOf(CLASSINFO(wxWindow))
138 m_isWindow = TRUE;
139
140 #if wxUSE_CONSTRAINTS
141 // no constraints whatsoever
142 m_constraints = (wxLayoutConstraints *) NULL;
143 m_constraintsInvolvedIn = (wxWindowList *) NULL;
144 m_windowSizer = (wxSizer *) NULL;
145 m_autoLayout = FALSE;
146 #endif // wxUSE_CONSTRAINTS
147
148 #if wxUSE_DRAG_AND_DROP
149 m_dropTarget = (wxDropTarget *)NULL;
150 #endif // wxUSE_DRAG_AND_DROP
151
152 #if wxUSE_TOOLTIPS
153 m_tooltip = (wxToolTip *)NULL;
154 #endif // wxUSE_TOOLTIPS
155
156 #if wxUSE_CARET
157 m_caret = (wxCaret *)NULL;
158 #endif // wxUSE_CARET
159 }
160
161 // common part of window creation process
162 bool wxWindowBase::CreateBase(wxWindowBase *parent,
163 wxWindowID id,
164 const wxPoint& WXUNUSED(pos),
165 const wxSize& WXUNUSED(size),
166 long style,
167 const wxValidator& validator,
168 const wxString& name)
169 {
170 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
171 // member variables - check that it has been called (will catch the case
172 // when a new ctor is added which doesn't call InitWindow)
173 wxASSERT_MSG( m_isWindow, wxT("Init() must have been called before!") );
174
175 // generate a new id if the user doesn't care about it
176 m_windowId = id == -1 ? NewControlId() : id;
177
178 SetName(name);
179
180 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
181 // have it too - like this it's possible to set it only in the top level
182 // dialog/frame and all children will inherit it by defult
183 if ( parent && (parent->GetWindowStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
184 {
185 style |= wxWS_EX_VALIDATE_RECURSIVELY;
186 }
187
188 SetWindowStyleFlag(style);
189 SetParent(parent);
190
191 #if wxUSE_VALIDATORS
192 SetValidator(validator);
193 #endif // wxUSE_VALIDATORS
194
195 return TRUE;
196 }
197
198 // ----------------------------------------------------------------------------
199 // destruction
200 // ----------------------------------------------------------------------------
201
202 // common clean up
203 wxWindowBase::~wxWindowBase()
204 {
205 // FIXME if these 2 cases result from programming errors in the user code
206 // we should probably assert here instead of silently fixing them
207
208 // Just in case the window has been Closed, but we're then deleting
209 // immediately: don't leave dangling pointers.
210 wxPendingDelete.DeleteObject(this);
211
212 // Just in case we've loaded a top-level window via LoadNativeDialog but
213 // we weren't a dialog class
214 wxTopLevelWindows.DeleteObject(this);
215
216 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
217
218 // make sure that there are no dangling pointers left pointing to us
219 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
220 if ( panel )
221 {
222 if ( panel->GetLastFocus() == this )
223 {
224 panel->SetLastFocus((wxWindow *)NULL);
225 }
226 }
227
228 #if wxUSE_CARET
229 if ( m_caret )
230 delete m_caret;
231 #endif // wxUSE_CARET
232
233 #if wxUSE_VALIDATORS
234 if ( m_windowValidator )
235 delete m_windowValidator;
236 #endif // wxUSE_VALIDATORS
237
238 // we only delete object data, not untyped
239 if ( m_clientDataType == ClientData_Object )
240 delete m_clientObject;
241
242 #if wxUSE_CONSTRAINTS
243 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
244 // at deleted windows as they delete themselves.
245 DeleteRelatedConstraints();
246
247 if ( m_constraints )
248 {
249 // This removes any dangling pointers to this window in other windows'
250 // constraintsInvolvedIn lists.
251 UnsetConstraints(m_constraints);
252 delete m_constraints;
253 m_constraints = NULL;
254 }
255
256 if ( m_windowSizer )
257 delete m_windowSizer;
258
259 #endif // wxUSE_CONSTRAINTS
260
261 #if wxUSE_DRAG_AND_DROP
262 if ( m_dropTarget )
263 delete m_dropTarget;
264 #endif // wxUSE_DRAG_AND_DROP
265
266 #if wxUSE_TOOLTIPS
267 if ( m_tooltip )
268 delete m_tooltip;
269 #endif // wxUSE_TOOLTIPS
270 }
271
272 bool wxWindowBase::Destroy()
273 {
274 delete this;
275
276 return TRUE;
277 }
278
279 bool wxWindowBase::Close(bool force)
280 {
281 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
282 event.SetEventObject(this);
283 #if WXWIN_COMPATIBILITY
284 event.SetForce(force);
285 #endif // WXWIN_COMPATIBILITY
286 event.SetCanVeto(!force);
287
288 // return FALSE if window wasn't closed because the application vetoed the
289 // close event
290 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
291 }
292
293 bool wxWindowBase::DestroyChildren()
294 {
295 wxWindowList::Node *node;
296 for ( ;; )
297 {
298 // we iterate until the list becomes empty
299 node = GetChildren().GetFirst();
300 if ( !node )
301 break;
302
303 wxWindow *child = node->GetData();
304
305 wxASSERT_MSG( child, wxT("children list contains empty nodes") );
306
307 delete child;
308
309 wxASSERT_MSG( !GetChildren().Find(child),
310 wxT("child didn't remove itself using RemoveChild()") );
311 }
312
313 return TRUE;
314 }
315
316 // ----------------------------------------------------------------------------
317 // size/position related methods
318 // ----------------------------------------------------------------------------
319
320 // centre the window with respect to its parent in either (or both) directions
321 void wxWindowBase::Centre(int direction)
322 {
323 int widthParent, heightParent;
324
325 wxWindow *parent = GetParent();
326 if ( !parent )
327 {
328 // no other choice
329 direction |= wxCENTRE_ON_SCREEN;
330 }
331
332 if ( direction & wxCENTRE_ON_SCREEN )
333 {
334 // centre with respect to the whole screen
335 wxDisplaySize(&widthParent, &heightParent);
336 }
337 else
338 {
339 // centre inside the parents rectangle
340 parent->GetClientSize(&widthParent, &heightParent);
341 }
342
343 int width, height;
344 GetSize(&width, &height);
345
346 int xNew = -1,
347 yNew = -1;
348
349 if ( direction & wxHORIZONTAL )
350 xNew = (widthParent - width)/2;
351
352 if ( direction & wxVERTICAL )
353 yNew = (heightParent - height)/2;
354
355 // controls are always centered on their parent because it doesn't make
356 // sense to centre them on the screen
357 if ( !(direction & wxCENTRE_ON_SCREEN) || !IsTopLevel() )
358 {
359 // the only chance to get this is to have a not top level window
360 // without parent which shouldn't happen
361 wxCHECK_RET( parent, wxT("this window must have a parent") );
362
363 // adjust to the parents client area origin
364 wxPoint posParent = parent->ClientToScreen(wxPoint(0, 0));
365
366 xNew += posParent.x;
367 yNew += posParent.y;
368 }
369
370 // move the centre of this window to this position
371 Move(xNew, yNew);
372 }
373
374 // fits the window around the children
375 void wxWindowBase::Fit()
376 {
377 if ( GetChildren().GetCount() > 0 )
378 {
379 SetClientSize(DoGetBestSize());
380 }
381 //else: do nothing if we have no children
382 }
383
384 // return the size best suited for the current window
385 wxSize wxWindowBase::DoGetBestSize() const
386 {
387 if ( GetChildren().GetCount() > 0 )
388 {
389 // our minimal acceptable size is such that all our windows fit inside
390 int maxX = 0,
391 maxY = 0;
392
393 for ( wxWindowList::Node *node = GetChildren().GetFirst();
394 node;
395 node = node->GetNext() )
396 {
397 wxWindow *win = node->GetData();
398 if ( win->IsTopLevel() )
399 {
400 // dialogs and frames lie in different top level windows -
401 // don't deal with them here
402 continue;
403 }
404
405 int wx, wy, ww, wh;
406 win->GetPosition(&wx, &wy);
407 win->GetSize(&ww, &wh);
408 if ( wx + ww > maxX )
409 maxX = wx + ww;
410 if ( wy + wh > maxY )
411 maxY = wy + wh;
412 }
413
414 // leave a margin
415 return wxSize(maxX + 7, maxY + 14);
416 }
417 else
418 {
419 // for a generic window there is no natural best size - just use the
420 // current one
421 return GetSize();
422 }
423 }
424
425 // set the min/max size of the window
426 void wxWindowBase::SetSizeHints(int minW, int minH,
427 int maxW, int maxH,
428 int WXUNUSED(incW), int WXUNUSED(incH))
429 {
430 m_minWidth = minW;
431 m_maxWidth = maxW;
432 m_minHeight = minH;
433 m_maxHeight = maxH;
434 }
435
436 // ----------------------------------------------------------------------------
437 // show/hide/enable/disable the window
438 // ----------------------------------------------------------------------------
439
440 bool wxWindowBase::Show(bool show)
441 {
442 if ( show != m_isShown )
443 {
444 m_isShown = show;
445
446 return TRUE;
447 }
448 else
449 {
450 return FALSE;
451 }
452 }
453
454 bool wxWindowBase::Enable(bool enable)
455 {
456 if ( enable != m_isEnabled )
457 {
458 m_isEnabled = enable;
459
460 return TRUE;
461 }
462 else
463 {
464 return FALSE;
465 }
466 }
467 // ----------------------------------------------------------------------------
468 // RTTI
469 // ----------------------------------------------------------------------------
470
471 bool wxWindowBase::IsTopLevel() const
472 {
473 return FALSE;
474 }
475
476 // ----------------------------------------------------------------------------
477 // reparenting the window
478 // ----------------------------------------------------------------------------
479
480 void wxWindowBase::AddChild(wxWindowBase *child)
481 {
482 wxCHECK_RET( child, wxT("can't add a NULL child") );
483
484 GetChildren().Append(child);
485 child->SetParent(this);
486 }
487
488 void wxWindowBase::RemoveChild(wxWindowBase *child)
489 {
490 wxCHECK_RET( child, wxT("can't remove a NULL child") );
491
492 GetChildren().DeleteObject(child);
493 child->SetParent((wxWindow *)NULL);
494 }
495
496 bool wxWindowBase::Reparent(wxWindowBase *newParent)
497 {
498 wxWindow *oldParent = GetParent();
499 if ( newParent == oldParent )
500 {
501 // nothing done
502 return FALSE;
503 }
504
505 // unlink this window from the existing parent.
506 if ( oldParent )
507 {
508 oldParent->RemoveChild(this);
509 }
510 else
511 {
512 wxTopLevelWindows.DeleteObject(this);
513 }
514
515 // add it to the new one
516 if ( newParent )
517 {
518 newParent->AddChild(this);
519 }
520 else
521 {
522 wxTopLevelWindows.Append(this);
523 }
524
525 return TRUE;
526 }
527
528 // ----------------------------------------------------------------------------
529 // event handler stuff
530 // ----------------------------------------------------------------------------
531
532 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
533 {
534 handler->SetNextHandler(GetEventHandler());
535 SetEventHandler(handler);
536 }
537
538 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
539 {
540 wxEvtHandler *handlerA = GetEventHandler();
541 if ( handlerA )
542 {
543 wxEvtHandler *handlerB = handlerA->GetNextHandler();
544 handlerA->SetNextHandler((wxEvtHandler *)NULL);
545 SetEventHandler(handlerB);
546 if ( deleteHandler )
547 {
548 delete handlerA;
549 handlerA = (wxEvtHandler *)NULL;
550 }
551 }
552
553 return handlerA;
554 }
555
556 // ----------------------------------------------------------------------------
557 // cursors, fonts &c
558 // ----------------------------------------------------------------------------
559
560 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
561 {
562 if ( !colour.Ok() || (colour == m_backgroundColour) )
563 return FALSE;
564
565 m_backgroundColour = colour;
566
567 return TRUE;
568 }
569
570 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
571 {
572 if ( !colour.Ok() || (colour == m_foregroundColour) )
573 return FALSE;
574
575 m_foregroundColour = colour;
576
577 return TRUE;
578 }
579
580 bool wxWindowBase::SetCursor(const wxCursor& cursor)
581 {
582 // don't try to set invalid cursor, always fall back to the default
583 const wxCursor& cursorOk = cursor.Ok() ? cursor : *wxSTANDARD_CURSOR;
584
585 if ( (wxCursor&)cursorOk == m_cursor )
586 {
587 // no change
588 return FALSE;
589 }
590
591 m_cursor = cursorOk;
592
593 return TRUE;
594 }
595
596 bool wxWindowBase::SetFont(const wxFont& font)
597 {
598 // don't try to set invalid font, always fall back to the default
599 const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
600
601 if ( (wxFont&)fontOk == m_font )
602 {
603 // no change
604 return FALSE;
605 }
606
607 m_font = fontOk;
608
609 return TRUE;
610 }
611
612 #if wxUSE_CARET
613 void wxWindowBase::SetCaret(wxCaret *caret)
614 {
615 if ( m_caret )
616 {
617 delete m_caret;
618 }
619
620 m_caret = caret;
621
622 if ( m_caret )
623 {
624 wxASSERT_MSG( m_caret->GetWindow() == this,
625 wxT("caret should be created associated to this window") );
626 }
627 }
628 #endif // wxUSE_CARET
629
630 #if wxUSE_VALIDATORS
631 // ----------------------------------------------------------------------------
632 // validators
633 // ----------------------------------------------------------------------------
634
635 void wxWindowBase::SetValidator(const wxValidator& validator)
636 {
637 if ( m_windowValidator )
638 delete m_windowValidator;
639
640 m_windowValidator = (wxValidator *)validator.Clone();
641
642 if ( m_windowValidator )
643 m_windowValidator->SetWindow(this) ;
644 }
645 #endif // wxUSE_VALIDATORS
646
647 // ----------------------------------------------------------------------------
648 // update region testing
649 // ----------------------------------------------------------------------------
650
651 bool wxWindowBase::IsExposed(int x, int y) const
652 {
653 return m_updateRegion.Contains(x, y) != wxOutRegion;
654 }
655
656 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
657 {
658 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
659 }
660
661 // ----------------------------------------------------------------------------
662 // find window by id or name
663 // ----------------------------------------------------------------------------
664
665 wxWindow *wxWindowBase::FindWindow( long id )
666 {
667 if ( id == m_windowId )
668 return (wxWindow *)this;
669
670 wxWindowBase *res = (wxWindow *)NULL;
671 wxWindowList::Node *node;
672 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
673 {
674 wxWindowBase *child = node->GetData();
675 res = child->FindWindow( id );
676 }
677
678 return (wxWindow *)res;
679 }
680
681 wxWindow *wxWindowBase::FindWindow( const wxString& name )
682 {
683 if ( name == m_windowName )
684 return (wxWindow *)this;
685
686 wxWindowBase *res = (wxWindow *)NULL;
687 wxWindowList::Node *node;
688 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
689 {
690 wxWindow *child = node->GetData();
691 res = child->FindWindow(name);
692 }
693
694 return (wxWindow *)res;
695 }
696
697 // ----------------------------------------------------------------------------
698 // dialog oriented functions
699 // ----------------------------------------------------------------------------
700
701 void wxWindowBase::MakeModal(bool modal)
702 {
703 // Disable all other windows
704 if ( IsTopLevel() )
705 {
706 wxWindowList::Node *node = wxTopLevelWindows.GetFirst();
707 while (node)
708 {
709 wxWindow *win = node->GetData();
710 if (win != this)
711 win->Enable(!modal);
712
713 node = node->GetNext();
714 }
715 }
716 }
717
718 bool wxWindowBase::Validate()
719 {
720 #if wxUSE_VALIDATORS
721 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
722
723 wxWindowList::Node *node;
724 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
725 {
726 wxWindowBase *child = node->GetData();
727 wxValidator *validator = child->GetValidator();
728 if ( validator && !validator->Validate((wxWindow *)this) )
729 {
730 return FALSE;
731 }
732
733 if ( recurse && !child->Validate() )
734 {
735 return FALSE;
736 }
737 }
738 #endif // wxUSE_VALIDATORS
739
740 return TRUE;
741 }
742
743 bool wxWindowBase::TransferDataToWindow()
744 {
745 #if wxUSE_VALIDATORS
746 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
747
748 wxWindowList::Node *node;
749 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
750 {
751 wxWindowBase *child = node->GetData();
752 wxValidator *validator = child->GetValidator();
753 if ( validator && !validator->TransferToWindow() )
754 {
755 wxLogWarning(_("Could not transfer data to window"));
756 wxLog::FlushActive();
757
758 return FALSE;
759 }
760
761 if ( recurse )
762 {
763 if ( !child->TransferDataToWindow() )
764 {
765 // warning already given
766 return FALSE;
767 }
768 }
769 }
770 #endif // wxUSE_VALIDATORS
771
772 return TRUE;
773 }
774
775 bool wxWindowBase::TransferDataFromWindow()
776 {
777 #if wxUSE_VALIDATORS
778 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
779
780 wxWindowList::Node *node;
781 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
782 {
783 wxWindow *child = node->GetData();
784 wxValidator *validator = child->GetValidator();
785 if ( validator && !validator->TransferFromWindow() )
786 {
787 // nop warning here because the application is supposed to give
788 // one itself - we don't know here what might have gone wrongly
789
790 return FALSE;
791 }
792
793 if ( recurse )
794 {
795 if ( !child->TransferDataFromWindow() )
796 {
797 // warning already given
798 return FALSE;
799 }
800 }
801 }
802 #endif // wxUSE_VALIDATORS
803
804 return TRUE;
805 }
806
807 void wxWindowBase::InitDialog()
808 {
809 wxInitDialogEvent event(GetId());
810 event.SetEventObject( this );
811 GetEventHandler()->ProcessEvent(event);
812 }
813
814 // ----------------------------------------------------------------------------
815 // tooltips
816 // ----------------------------------------------------------------------------
817
818 #if wxUSE_TOOLTIPS
819
820 void wxWindowBase::SetToolTip( const wxString &tip )
821 {
822 // don't create the new tooltip if we already have one
823 if ( m_tooltip )
824 {
825 m_tooltip->SetTip( tip );
826 }
827 else
828 {
829 SetToolTip( new wxToolTip( tip ) );
830 }
831
832 // setting empty tooltip text does not remove the tooltip any more - use
833 // SetToolTip((wxToolTip *)NULL) for this
834 }
835
836 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
837 {
838 if ( m_tooltip )
839 delete m_tooltip;
840
841 m_tooltip = tooltip;
842 }
843
844 #endif // wxUSE_TOOLTIPS
845
846 // ----------------------------------------------------------------------------
847 // constraints and sizers
848 // ----------------------------------------------------------------------------
849
850 #if wxUSE_CONSTRAINTS
851
852 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
853 {
854 if ( m_constraints )
855 {
856 UnsetConstraints(m_constraints);
857 delete m_constraints;
858 }
859 m_constraints = constraints;
860 if ( m_constraints )
861 {
862 // Make sure other windows know they're part of a 'meaningful relationship'
863 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
864 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
865 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
866 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
867 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
868 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
869 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
870 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
871 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
872 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
873 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
874 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
875 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
876 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
877 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
878 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
879 }
880 }
881
882 // This removes any dangling pointers to this window in other windows'
883 // constraintsInvolvedIn lists.
884 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
885 {
886 if ( c )
887 {
888 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
889 c->left.GetOtherWindow()->RemoveConstraintReference(this);
890 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
891 c->top.GetOtherWindow()->RemoveConstraintReference(this);
892 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
893 c->right.GetOtherWindow()->RemoveConstraintReference(this);
894 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
895 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
896 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
897 c->width.GetOtherWindow()->RemoveConstraintReference(this);
898 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
899 c->height.GetOtherWindow()->RemoveConstraintReference(this);
900 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
901 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
902 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
903 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
904 }
905 }
906
907 // Back-pointer to other windows we're involved with, so if we delete this
908 // window, we must delete any constraints we're involved with.
909 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
910 {
911 if ( !m_constraintsInvolvedIn )
912 m_constraintsInvolvedIn = new wxWindowList;
913 if ( !m_constraintsInvolvedIn->Find(otherWin) )
914 m_constraintsInvolvedIn->Append(otherWin);
915 }
916
917 // REMOVE back-pointer to other windows we're involved with.
918 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
919 {
920 if ( m_constraintsInvolvedIn )
921 m_constraintsInvolvedIn->DeleteObject(otherWin);
922 }
923
924 // Reset any constraints that mention this window
925 void wxWindowBase::DeleteRelatedConstraints()
926 {
927 if ( m_constraintsInvolvedIn )
928 {
929 wxWindowList::Node *node = m_constraintsInvolvedIn->GetFirst();
930 while (node)
931 {
932 wxWindow *win = node->GetData();
933 wxLayoutConstraints *constr = win->GetConstraints();
934
935 // Reset any constraints involving this window
936 if ( constr )
937 {
938 constr->left.ResetIfWin(this);
939 constr->top.ResetIfWin(this);
940 constr->right.ResetIfWin(this);
941 constr->bottom.ResetIfWin(this);
942 constr->width.ResetIfWin(this);
943 constr->height.ResetIfWin(this);
944 constr->centreX.ResetIfWin(this);
945 constr->centreY.ResetIfWin(this);
946 }
947
948 wxWindowList::Node *next = node->GetNext();
949 delete node;
950 node = next;
951 }
952
953 delete m_constraintsInvolvedIn;
954 m_constraintsInvolvedIn = (wxWindowList *) NULL;
955 }
956 }
957
958 void wxWindowBase::SetSizer(wxSizer *sizer)
959 {
960 if (m_windowSizer) delete m_windowSizer;
961
962 m_windowSizer = sizer;
963 }
964
965 bool wxWindowBase::Layout()
966 {
967 // If there is a sizer, use it instead of the constraints
968 if ( GetSizer() )
969 {
970 int w, h;
971 GetClientSize(&w, &h);
972
973 GetSizer()->SetDimension( 0, 0, w, h );
974 }
975 else
976 {
977 wxLayoutConstraints *constr = GetConstraints();
978 bool wasOk = constr && constr->AreSatisfied();
979
980 ResetConstraints(); // Mark all constraints as unevaluated
981
982 // if we're a top level panel (i.e. our parent is frame/dialog), our
983 // own constraints will never be satisfied any more unless we do it
984 // here
985 if ( wasOk )
986 {
987 int noChanges = 1;
988 while ( noChanges > 0 )
989 {
990 constr->SatisfyConstraints(this, &noChanges);
991 }
992 }
993
994 DoPhase(1); // Layout children
995 DoPhase(2); // Layout grand children
996 SetConstraintSizes(); // Recursively set the real window sizes
997 }
998
999 return TRUE;
1000 }
1001
1002
1003 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1004 // do a similar thing, but also impose their own 'constraints' and order the
1005 // evaluation differently.
1006 bool wxWindowBase::LayoutPhase1(int *noChanges)
1007 {
1008 wxLayoutConstraints *constr = GetConstraints();
1009 if ( constr )
1010 {
1011 return constr->SatisfyConstraints(this, noChanges);
1012 }
1013 else
1014 return TRUE;
1015 }
1016
1017 bool wxWindowBase::LayoutPhase2(int *noChanges)
1018 {
1019 *noChanges = 0;
1020
1021 // Layout children
1022 DoPhase(1);
1023 DoPhase(2);
1024 return TRUE;
1025 }
1026
1027 // Do a phase of evaluating child constraints
1028 bool wxWindowBase::DoPhase(int phase)
1029 {
1030 int noIterations = 0;
1031 int maxIterations = 500;
1032 int noChanges = 1;
1033 int noFailures = 0;
1034 wxWindowList succeeded;
1035 while ((noChanges > 0) && (noIterations < maxIterations))
1036 {
1037 noChanges = 0;
1038 noFailures = 0;
1039 wxWindowList::Node *node = GetChildren().GetFirst();
1040 while (node)
1041 {
1042 wxWindow *child = node->GetData();
1043 if ( !child->IsTopLevel() )
1044 {
1045 wxLayoutConstraints *constr = child->GetConstraints();
1046 if ( constr )
1047 {
1048 if ( !succeeded.Find(child) )
1049 {
1050 int tempNoChanges = 0;
1051 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
1052 noChanges += tempNoChanges;
1053 if ( success )
1054 {
1055 succeeded.Append(child);
1056 }
1057 }
1058 }
1059 }
1060 node = node->GetNext();
1061 }
1062
1063 noIterations++;
1064 }
1065
1066 return TRUE;
1067 }
1068
1069 void wxWindowBase::ResetConstraints()
1070 {
1071 wxLayoutConstraints *constr = GetConstraints();
1072 if ( constr )
1073 {
1074 constr->left.SetDone(FALSE);
1075 constr->top.SetDone(FALSE);
1076 constr->right.SetDone(FALSE);
1077 constr->bottom.SetDone(FALSE);
1078 constr->width.SetDone(FALSE);
1079 constr->height.SetDone(FALSE);
1080 constr->centreX.SetDone(FALSE);
1081 constr->centreY.SetDone(FALSE);
1082 }
1083
1084 wxWindowList::Node *node = GetChildren().GetFirst();
1085 while (node)
1086 {
1087 wxWindow *win = node->GetData();
1088 if ( !win->IsTopLevel() )
1089 win->ResetConstraints();
1090 node = node->GetNext();
1091 }
1092 }
1093
1094 // Need to distinguish between setting the 'fake' size for windows and sizers,
1095 // and setting the real values.
1096 void wxWindowBase::SetConstraintSizes(bool recurse)
1097 {
1098 wxLayoutConstraints *constr = GetConstraints();
1099 if ( constr && constr->AreSatisfied() )
1100 {
1101 int x = constr->left.GetValue();
1102 int y = constr->top.GetValue();
1103 int w = constr->width.GetValue();
1104 int h = constr->height.GetValue();
1105
1106 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1107 (constr->height.GetRelationship() != wxAsIs) )
1108 {
1109 SetSize(x, y, w, h);
1110 }
1111 else
1112 {
1113 // If we don't want to resize this window, just move it...
1114 Move(x, y);
1115 }
1116 }
1117 else if ( constr )
1118 {
1119 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1120 GetClassInfo()->GetClassName(),
1121 GetName().c_str());
1122 }
1123
1124 if ( recurse )
1125 {
1126 wxWindowList::Node *node = GetChildren().GetFirst();
1127 while (node)
1128 {
1129 wxWindow *win = node->GetData();
1130 if ( !win->IsTopLevel() )
1131 win->SetConstraintSizes();
1132 node = node->GetNext();
1133 }
1134 }
1135 }
1136
1137 // Only set the size/position of the constraint (if any)
1138 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1139 {
1140 wxLayoutConstraints *constr = GetConstraints();
1141 if ( constr )
1142 {
1143 if ( x != -1 )
1144 {
1145 constr->left.SetValue(x);
1146 constr->left.SetDone(TRUE);
1147 }
1148 if ( y != -1 )
1149 {
1150 constr->top.SetValue(y);
1151 constr->top.SetDone(TRUE);
1152 }
1153 if ( w != -1 )
1154 {
1155 constr->width.SetValue(w);
1156 constr->width.SetDone(TRUE);
1157 }
1158 if ( h != -1 )
1159 {
1160 constr->height.SetValue(h);
1161 constr->height.SetDone(TRUE);
1162 }
1163 }
1164 }
1165
1166 void wxWindowBase::MoveConstraint(int x, int y)
1167 {
1168 wxLayoutConstraints *constr = GetConstraints();
1169 if ( constr )
1170 {
1171 if ( x != -1 )
1172 {
1173 constr->left.SetValue(x);
1174 constr->left.SetDone(TRUE);
1175 }
1176 if ( y != -1 )
1177 {
1178 constr->top.SetValue(y);
1179 constr->top.SetDone(TRUE);
1180 }
1181 }
1182 }
1183
1184 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1185 {
1186 wxLayoutConstraints *constr = GetConstraints();
1187 if ( constr )
1188 {
1189 *w = constr->width.GetValue();
1190 *h = constr->height.GetValue();
1191 }
1192 else
1193 GetSize(w, h);
1194 }
1195
1196 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1197 {
1198 wxLayoutConstraints *constr = GetConstraints();
1199 if ( constr )
1200 {
1201 *w = constr->width.GetValue();
1202 *h = constr->height.GetValue();
1203 }
1204 else
1205 GetClientSize(w, h);
1206 }
1207
1208 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1209 {
1210 wxLayoutConstraints *constr = GetConstraints();
1211 if ( constr )
1212 {
1213 *x = constr->left.GetValue();
1214 *y = constr->top.GetValue();
1215 }
1216 else
1217 GetPosition(x, y);
1218 }
1219
1220 #endif // wxUSE_CONSTRAINTS
1221
1222 // ----------------------------------------------------------------------------
1223 // do Update UI processing for child controls
1224 // ----------------------------------------------------------------------------
1225
1226 // TODO: should this be implemented for the child window rather
1227 // than the parent? Then you can override it e.g. for wxCheckBox
1228 // to do the Right Thing rather than having to assume a fixed number
1229 // of control classes.
1230 void wxWindowBase::UpdateWindowUI()
1231 {
1232 wxUpdateUIEvent event(GetId());
1233 event.m_eventObject = this;
1234
1235 if ( GetEventHandler()->ProcessEvent(event) )
1236 {
1237 if ( event.GetSetEnabled() )
1238 Enable(event.GetEnabled());
1239
1240 if ( event.GetSetText() )
1241 {
1242 wxControl *control = wxDynamicCast(this, wxControl);
1243 if ( control )
1244 {
1245 wxTextCtrl *text = wxDynamicCast(control, wxTextCtrl);
1246 if ( text )
1247 text->SetValue(event.GetText());
1248 else
1249 control->SetLabel(event.GetText());
1250 }
1251 }
1252
1253 #if wxUSE_CHECKBOX
1254 wxCheckBox *checkbox = wxDynamicCast(this, wxCheckBox);
1255 if ( checkbox )
1256 {
1257 if ( event.GetSetChecked() )
1258 checkbox->SetValue(event.GetChecked());
1259 }
1260 #endif // wxUSE_CHECKBOX
1261
1262 #if wxUSE_RADIOBTN
1263 wxRadioButton *radiobtn = wxDynamicCast(this, wxRadioButton);
1264 if ( radiobtn )
1265 {
1266 if ( event.GetSetChecked() )
1267 radiobtn->SetValue(event.GetChecked());
1268 }
1269 #endif // wxUSE_RADIOBTN
1270 }
1271 }
1272
1273 // ----------------------------------------------------------------------------
1274 // dialog units translations
1275 // ----------------------------------------------------------------------------
1276
1277 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
1278 {
1279 int charWidth = GetCharWidth();
1280 int charHeight = GetCharHeight();
1281 wxPoint pt2(-1, -1);
1282 if (pt.x != -1)
1283 pt2.x = (int) ((pt.x * 4) / charWidth) ;
1284 if (pt.y != -1)
1285 pt2.y = (int) ((pt.y * 8) / charHeight) ;
1286
1287 return pt2;
1288 }
1289
1290 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
1291 {
1292 int charWidth = GetCharWidth();
1293 int charHeight = GetCharHeight();
1294 wxPoint pt2(-1, -1);
1295 if (pt.x != -1)
1296 pt2.x = (int) ((pt.x * charWidth) / 4) ;
1297 if (pt.y != -1)
1298 pt2.y = (int) ((pt.y * charHeight) / 8) ;
1299
1300 return pt2;
1301 }
1302
1303 // ----------------------------------------------------------------------------
1304 // client data
1305 // ----------------------------------------------------------------------------
1306
1307 void wxWindowBase::DoSetClientObject( wxClientData *data )
1308 {
1309 wxASSERT_MSG( m_clientDataType != ClientData_Void,
1310 wxT("can't have both object and void client data") );
1311
1312 if ( m_clientObject )
1313 delete m_clientObject;
1314
1315 m_clientObject = data;
1316 m_clientDataType = ClientData_Object;
1317 }
1318
1319 wxClientData *wxWindowBase::DoGetClientObject() const
1320 {
1321 // it's not an error to call GetClientObject() on a window which doesn't
1322 // have client data at all - NULL will be returned
1323 wxASSERT_MSG( m_clientDataType != ClientData_Void,
1324 wxT("this window doesn't have object client data") );
1325
1326 return m_clientObject;
1327 }
1328
1329 void wxWindowBase::DoSetClientData( void *data )
1330 {
1331 wxASSERT_MSG( m_clientDataType != ClientData_Object,
1332 wxT("can't have both object and void client data") );
1333
1334 m_clientData = data;
1335 m_clientDataType = ClientData_Void;
1336 }
1337
1338 void *wxWindowBase::DoGetClientData() const
1339 {
1340 // it's not an error to call GetClientData() on a window which doesn't have
1341 // client data at all - NULL will be returned
1342 wxASSERT_MSG( m_clientDataType != ClientData_Object,
1343 wxT("this window doesn't have void client data") );
1344
1345 return m_clientData;
1346 }
1347
1348 // ----------------------------------------------------------------------------
1349 // event handlers
1350 // ----------------------------------------------------------------------------
1351
1352 // propagate the colour change event to the subwindows
1353 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
1354 {
1355 wxWindowList::Node *node = GetChildren().GetFirst();
1356 while ( node )
1357 {
1358 // Only propagate to non-top-level windows
1359 wxWindow *win = node->GetData();
1360 if ( !win->IsTopLevel() )
1361 {
1362 wxSysColourChangedEvent event2;
1363 event.m_eventObject = win;
1364 win->GetEventHandler()->ProcessEvent(event2);
1365 }
1366
1367 node = node->GetNext();
1368 }
1369 }
1370
1371 // the default action is to populate dialog with data when it's created
1372 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
1373 {
1374 TransferDataToWindow();
1375 }
1376
1377 // process Ctrl-Alt-mclick
1378 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
1379 {
1380 if ( event.ControlDown() && event.AltDown() )
1381 {
1382 // don't translate these strings
1383 wxString port;
1384 switch ( wxGetOsVersion() )
1385 {
1386 case wxMOTIF_X: port = _T("Motif"); break;
1387 case wxMACINTOSH: port = _T("Mac"); break;
1388 case wxBEOS: port = _T("BeOS"); break;
1389 case wxGTK:
1390 case wxGTK_WIN32:
1391 case wxGTK_OS2:
1392 case wxGTK_BEOS: port = _T("GTK"); break;
1393 case wxWINDOWS:
1394 case wxPENWINDOWS:
1395 case wxWINDOWS_NT:
1396 case wxWIN32S:
1397 case wxWIN95:
1398 case wxWIN386: port = _T("MS Windows"); break;
1399 case wxMGL_UNIX:
1400 case wxMGL_X:
1401 case wxMGL_WIN32:
1402 case wxMGL_OS2: port = _T("MGL"); break;
1403 case wxWINDOWS_OS2:
1404 case wxOS2_PM: port = _T("OS/2"); break;
1405 default: port = _T("unknown"); break;
1406 }
1407
1408 wxMessageBox(wxString::Format(
1409 _T(
1410 " wxWindows Library (%s port)\n"
1411 "Version %u.%u.%u, compiled at %s %s\n"
1412 " Copyright (c) 1995-2000 wxWindows team"
1413 ),
1414 port.c_str(),
1415 wxMAJOR_VERSION,
1416 wxMINOR_VERSION,
1417 wxRELEASE_NUMBER,
1418 __DATE__,
1419 __TIME__
1420 ),
1421 _T("wxWindows information"),
1422 wxICON_INFORMATION | wxOK,
1423 (wxWindow *)this);
1424 }
1425 else
1426 {
1427 event.Skip();
1428 }
1429 }
1430
1431 // ----------------------------------------------------------------------------
1432 // list classes implementation
1433 // ----------------------------------------------------------------------------
1434
1435 void wxWindowListNode::DeleteData()
1436 {
1437 delete (wxWindow *)GetData();
1438 }
1439