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