]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / src / common / wincmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wincmn.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Modified by:
6 // Created: 13/07/98
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #include "wx/log.h"
29 #include "wx/intl.h"
30 #include "wx/frame.h"
31 #include "wx/window.h"
32 #include "wx/control.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobut.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37 #include "wx/settings.h"
38 #include "wx/dialog.h"
39 #include "wx/msgdlg.h"
40 #include "wx/msgout.h"
41 #include "wx/statusbr.h"
42 #include "wx/toolbar.h"
43 #include "wx/dcclient.h"
44 #include "wx/scrolbar.h"
45 #include "wx/layout.h"
46 #include "wx/sizer.h"
47 #include "wx/menu.h"
48 #endif //WX_PRECOMP
49
50 #if wxUSE_DRAG_AND_DROP
51 #include "wx/dnd.h"
52 #endif // wxUSE_DRAG_AND_DROP
53
54 #if wxUSE_ACCESSIBILITY
55 #include "wx/access.h"
56 #endif
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 #if wxUSE_SYSTEM_OPTIONS
71 #include "wx/sysopt.h"
72 #endif
73
74 #include "wx/platinfo.h"
75 #include "wx/private/window.h"
76
77 #ifdef __WINDOWS__
78 #include "wx/msw/wrapwin.h"
79 #endif
80
81 // Windows List
82 WXDLLIMPEXP_DATA_CORE(wxWindowList) wxTopLevelWindows;
83
84 // globals
85 #if wxUSE_MENUS
86 wxMenu *wxCurrentPopupMenu = NULL;
87 #endif // wxUSE_MENUS
88
89 extern WXDLLEXPORT_DATA(const char) wxPanelNameStr[] = "panel";
90
91 // ----------------------------------------------------------------------------
92 // static data
93 // ----------------------------------------------------------------------------
94
95
96 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
97
98 // ----------------------------------------------------------------------------
99 // event table
100 // ----------------------------------------------------------------------------
101
102 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
103 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
104 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
105 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
106
107 #if wxUSE_HELP
108 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
109 #endif // wxUSE_HELP
110
111 EVT_SIZE(wxWindowBase::InternalOnSize)
112 END_EVENT_TABLE()
113
114 // ============================================================================
115 // implementation of the common functionality of the wxWindow class
116 // ============================================================================
117
118 // ----------------------------------------------------------------------------
119 // XTI
120 // ----------------------------------------------------------------------------
121
122 #if wxUSE_EXTENDED_RTTI
123
124 // windows that are created from a parent window during its Create method,
125 // eg. spin controls in a calendar controls must never been streamed out
126 // separately otherwise chaos occurs. Right now easiest is to test for negative ids,
127 // as windows with negative ids never can be recreated anyway
128
129
130 bool wxWindowStreamingCallback( const wxObject *object, wxObjectWriter *,
131 wxObjectWriterCallback *, const wxStringToAnyHashMap & )
132 {
133 const wxWindow * win = wx_dynamic_cast(const wxWindow*, object);
134 if ( win && win->GetId() < 0 )
135 return false;
136 return true;
137 }
138
139 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxWindow, wxWindowBase, "wx/window.h", \
140 wxWindowStreamingCallback)
141
142 // make wxWindowList known before the property is used
143
144 wxCOLLECTION_TYPE_INFO( wxWindow*, wxWindowList );
145
146 template<> void wxCollectionToVariantArray( wxWindowList const &theList,
147 wxAnyList &value)
148 {
149 wxListCollectionToAnyList<wxWindowList::compatibility_iterator>( theList, value );
150 }
151
152 wxDEFINE_FLAGS( wxWindowStyle )
153
154 wxBEGIN_FLAGS( wxWindowStyle )
155 // new style border flags, we put them first to
156 // use them for streaming out
157
158 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
159 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
160 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
161 wxFLAGS_MEMBER(wxBORDER_RAISED)
162 wxFLAGS_MEMBER(wxBORDER_STATIC)
163 wxFLAGS_MEMBER(wxBORDER_NONE)
164
165 // old style border flags
166 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
167 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
168 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
169 wxFLAGS_MEMBER(wxRAISED_BORDER)
170 wxFLAGS_MEMBER(wxSTATIC_BORDER)
171 wxFLAGS_MEMBER(wxBORDER)
172
173 // standard window styles
174 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
175 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
176 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
177 wxFLAGS_MEMBER(wxWANTS_CHARS)
178 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
179 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
180 wxFLAGS_MEMBER(wxVSCROLL)
181 wxFLAGS_MEMBER(wxHSCROLL)
182
183 wxEND_FLAGS( wxWindowStyle )
184
185 wxBEGIN_PROPERTIES_TABLE(wxWindow)
186 wxEVENT_PROPERTY( Close, wxEVT_CLOSE_WINDOW, wxCloseEvent)
187 wxEVENT_PROPERTY( Create, wxEVT_CREATE, wxWindowCreateEvent )
188 wxEVENT_PROPERTY( Destroy, wxEVT_DESTROY, wxWindowDestroyEvent )
189 // Always constructor Properties first
190
191 wxREADONLY_PROPERTY( Parent,wxWindow*, GetParent, wxEMPTY_PARAMETER_VALUE, \
192 0 /*flags*/, wxT("Helpstring"), wxT("group"))
193 wxPROPERTY( Id,wxWindowID, SetId, GetId, -1 /*wxID_ANY*/, 0 /*flags*/, \
194 wxT("Helpstring"), wxT("group") )
195 wxPROPERTY( Position,wxPoint, SetPosition, GetPosition, wxDefaultPosition, \
196 0 /*flags*/, wxT("Helpstring"), wxT("group")) // pos
197 wxPROPERTY( Size,wxSize, SetSize, GetSize, wxDefaultSize, 0 /*flags*/, \
198 wxT("Helpstring"), wxT("group")) // size
199 wxPROPERTY( WindowStyle, long, SetWindowStyleFlag, GetWindowStyleFlag, \
200 wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, wxT("Helpstring"), wxT("group")) // style
201 wxPROPERTY( Name,wxString, SetName, GetName, wxEmptyString, 0 /*flags*/, \
202 wxT("Helpstring"), wxT("group") )
203
204 // Then all relations of the object graph
205
206 wxREADONLY_PROPERTY_COLLECTION( Children, wxWindowList, wxWindowBase*, \
207 GetWindowChildren, wxPROP_OBJECT_GRAPH /*flags*/, \
208 wxT("Helpstring"), wxT("group"))
209
210 // and finally all other properties
211
212 wxPROPERTY( ExtraStyle, long, SetExtraStyle, GetExtraStyle, wxEMPTY_PARAMETER_VALUE, \
213 0 /*flags*/, wxT("Helpstring"), wxT("group")) // extstyle
214 wxPROPERTY( BackgroundColour, wxColour, SetBackgroundColour, GetBackgroundColour, \
215 wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, wxT("Helpstring"), wxT("group")) // bg
216 wxPROPERTY( ForegroundColour, wxColour, SetForegroundColour, GetForegroundColour, \
217 wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, wxT("Helpstring"), wxT("group")) // fg
218 wxPROPERTY( Enabled, bool, Enable, IsEnabled, wxAny((bool)true), 0 /*flags*/, \
219 wxT("Helpstring"), wxT("group"))
220 wxPROPERTY( Shown, bool, Show, IsShown, wxAny((bool)true), 0 /*flags*/, \
221 wxT("Helpstring"), wxT("group"))
222
223 #if 0
224 // possible property candidates (not in xrc) or not valid in all subclasses
225 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxEmptyString )
226 wxPROPERTY( Font, wxFont, SetFont, GetWindowFont , )
227 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxEmptyString )
228 // MaxHeight, Width, MinHeight, Width
229 // TODO switch label to control and title to toplevels
230
231 wxPROPERTY( ThemeEnabled, bool, SetThemeEnabled, GetThemeEnabled, )
232 //wxPROPERTY( Cursor, wxCursor, SetCursor, GetCursor, )
233 // wxPROPERTY( ToolTip, wxString, SetToolTip, GetToolTipText, )
234 wxPROPERTY( AutoLayout, bool, SetAutoLayout, GetAutoLayout, )
235 #endif
236 wxEND_PROPERTIES_TABLE()
237
238 wxEMPTY_HANDLERS_TABLE(wxWindow)
239
240 wxCONSTRUCTOR_DUMMY(wxWindow)
241
242 #else
243
244 #ifndef __WXUNIVERSAL__
245 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
246 #endif
247
248 #endif
249
250 // ----------------------------------------------------------------------------
251 // initialization
252 // ----------------------------------------------------------------------------
253
254 // the default initialization
255 wxWindowBase::wxWindowBase()
256 {
257 // no window yet, no parent nor children
258 m_parent = NULL;
259 m_windowId = wxID_ANY;
260
261 // no constraints on the minimal window size
262 m_minWidth =
263 m_maxWidth = wxDefaultCoord;
264 m_minHeight =
265 m_maxHeight = wxDefaultCoord;
266
267 // invalidiated cache value
268 m_bestSizeCache = wxDefaultSize;
269
270 // window are created enabled and visible by default
271 m_isShown =
272 m_isEnabled = true;
273
274 // the default event handler is just this window
275 m_eventHandler = this;
276
277 #if wxUSE_VALIDATORS
278 // no validator
279 m_windowValidator = NULL;
280 #endif // wxUSE_VALIDATORS
281
282 // the colours/fonts are default for now, so leave m_font,
283 // m_backgroundColour and m_foregroundColour uninitialized and set those
284 m_hasBgCol =
285 m_hasFgCol =
286 m_hasFont = false;
287 m_inheritBgCol =
288 m_inheritFgCol =
289 m_inheritFont = false;
290
291 // no style bits
292 m_exStyle =
293 m_windowStyle = 0;
294
295 m_backgroundStyle = wxBG_STYLE_ERASE;
296
297 #if wxUSE_CONSTRAINTS
298 // no constraints whatsoever
299 m_constraints = NULL;
300 m_constraintsInvolvedIn = NULL;
301 #endif // wxUSE_CONSTRAINTS
302
303 m_windowSizer = NULL;
304 m_containingSizer = NULL;
305 m_autoLayout = false;
306
307 #if wxUSE_DRAG_AND_DROP
308 m_dropTarget = NULL;
309 #endif // wxUSE_DRAG_AND_DROP
310
311 #if wxUSE_TOOLTIPS
312 m_tooltip = NULL;
313 #endif // wxUSE_TOOLTIPS
314
315 #if wxUSE_CARET
316 m_caret = NULL;
317 #endif // wxUSE_CARET
318
319 #if wxUSE_PALETTE
320 m_hasCustomPalette = false;
321 #endif // wxUSE_PALETTE
322
323 #if wxUSE_ACCESSIBILITY
324 m_accessible = NULL;
325 #endif
326
327 m_virtualSize = wxDefaultSize;
328
329 m_scrollHelper = NULL;
330
331 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
332 #if wxUSE_SYSTEM_OPTIONS
333 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT) )
334 {
335 m_windowVariant = (wxWindowVariant) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT ) ;
336 }
337 #endif
338
339 // Whether we're using the current theme for this window (wxGTK only for now)
340 m_themeEnabled = false;
341
342 // This is set to true by SendDestroyEvent() which should be called by the
343 // most derived class to ensure that the destruction event is sent as soon
344 // as possible to allow its handlers to still see the undestroyed window
345 m_isBeingDeleted = false;
346
347 m_freezeCount = 0;
348 }
349
350 // common part of window creation process
351 bool wxWindowBase::CreateBase(wxWindowBase *parent,
352 wxWindowID id,
353 const wxPoint& WXUNUSED(pos),
354 const wxSize& size,
355 long style,
356 const wxString& name)
357 {
358 // ids are limited to 16 bits under MSW so if you care about portability,
359 // it's not a good idea to use ids out of this range (and negative ids are
360 // reserved for wxWidgets own usage)
361 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767) ||
362 (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST),
363 wxT("invalid id value") );
364
365 // generate a new id if the user doesn't care about it
366 if ( id == wxID_ANY )
367 {
368 m_windowId = NewControlId();
369 }
370 else // valid id specified
371 {
372 m_windowId = id;
373 }
374
375 // don't use SetWindowStyleFlag() here, this function should only be called
376 // to change the flag after creation as it tries to reflect the changes in
377 // flags by updating the window dynamically and we don't need this here
378 m_windowStyle = style;
379
380 // assume the user doesn't want this window to shrink beneath its initial
381 // size, this worked like this in wxWidgets 2.8 and before and generally
382 // often makes sense for child windows (for top level ones it definitely
383 // does not as the user should be able to resize the window)
384 //
385 // note that we can't use IsTopLevel() from ctor
386 if ( size != wxDefaultSize && !wxTopLevelWindows.Find((wxWindow *)this) )
387 SetMinSize(size);
388
389 SetName(name);
390 SetParent(parent);
391
392 return true;
393 }
394
395 bool wxWindowBase::CreateBase(wxWindowBase *parent,
396 wxWindowID id,
397 const wxPoint& pos,
398 const wxSize& size,
399 long style,
400 const wxValidator& wxVALIDATOR_PARAM(validator),
401 const wxString& name)
402 {
403 if ( !CreateBase(parent, id, pos, size, style, name) )
404 return false;
405
406 #if wxUSE_VALIDATORS
407 SetValidator(validator);
408 #endif // wxUSE_VALIDATORS
409
410 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
411 // have it too - like this it's possible to set it only in the top level
412 // dialog/frame and all children will inherit it by defult
413 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
414 {
415 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
416 }
417
418 return true;
419 }
420
421 bool wxWindowBase::ToggleWindowStyle(int flag)
422 {
423 wxASSERT_MSG( flag, wxT("flags with 0 value can't be toggled") );
424
425 bool rc;
426 long style = GetWindowStyleFlag();
427 if ( style & flag )
428 {
429 style &= ~flag;
430 rc = false;
431 }
432 else // currently off
433 {
434 style |= flag;
435 rc = true;
436 }
437
438 SetWindowStyleFlag(style);
439
440 return rc;
441 }
442
443 // ----------------------------------------------------------------------------
444 // destruction
445 // ----------------------------------------------------------------------------
446
447 // common clean up
448 wxWindowBase::~wxWindowBase()
449 {
450 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
451
452 // FIXME if these 2 cases result from programming errors in the user code
453 // we should probably assert here instead of silently fixing them
454
455 // Just in case the window has been Closed, but we're then deleting
456 // immediately: don't leave dangling pointers.
457 wxPendingDelete.DeleteObject(this);
458
459 // Just in case we've loaded a top-level window via LoadNativeDialog but
460 // we weren't a dialog class
461 wxTopLevelWindows.DeleteObject((wxWindow*)this);
462
463 // Any additional event handlers should be popped before the window is
464 // deleted as otherwise the last handler will be left with a dangling
465 // pointer to this window result in a difficult to diagnose crash later on.
466 wxASSERT_MSG( GetEventHandler() == this,
467 wxT("any pushed event handlers must have been removed") );
468
469 #if wxUSE_MENUS
470 // The associated popup menu can still be alive, disassociate from it in
471 // this case
472 if ( wxCurrentPopupMenu && wxCurrentPopupMenu->GetInvokingWindow() == this )
473 wxCurrentPopupMenu->SetInvokingWindow(NULL);
474 #endif // wxUSE_MENUS
475
476 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
477
478 // notify the parent about this window destruction
479 if ( m_parent )
480 m_parent->RemoveChild(this);
481
482 #if wxUSE_CARET
483 delete m_caret;
484 #endif // wxUSE_CARET
485
486 #if wxUSE_VALIDATORS
487 delete m_windowValidator;
488 #endif // wxUSE_VALIDATORS
489
490 #if wxUSE_CONSTRAINTS
491 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
492 // at deleted windows as they delete themselves.
493 DeleteRelatedConstraints();
494
495 if ( m_constraints )
496 {
497 // This removes any dangling pointers to this window in other windows'
498 // constraintsInvolvedIn lists.
499 UnsetConstraints(m_constraints);
500 wxDELETE(m_constraints);
501 }
502 #endif // wxUSE_CONSTRAINTS
503
504 if ( m_containingSizer )
505 m_containingSizer->Detach( (wxWindow*)this );
506
507 delete m_windowSizer;
508
509 #if wxUSE_DRAG_AND_DROP
510 delete m_dropTarget;
511 #endif // wxUSE_DRAG_AND_DROP
512
513 #if wxUSE_TOOLTIPS
514 delete m_tooltip;
515 #endif // wxUSE_TOOLTIPS
516
517 #if wxUSE_ACCESSIBILITY
518 delete m_accessible;
519 #endif
520
521 #if wxUSE_HELP
522 // NB: this has to be called unconditionally, because we don't know
523 // whether this window has associated help text or not
524 wxHelpProvider *helpProvider = wxHelpProvider::Get();
525 if ( helpProvider )
526 helpProvider->RemoveHelp(this);
527 #endif
528 }
529
530 bool wxWindowBase::IsBeingDeleted() const
531 {
532 return m_isBeingDeleted ||
533 (!IsTopLevel() && m_parent && m_parent->IsBeingDeleted());
534 }
535
536 void wxWindowBase::SendDestroyEvent()
537 {
538 if ( m_isBeingDeleted )
539 {
540 // we could have been already called from a more derived class dtor,
541 // e.g. ~wxTLW calls us and so does ~wxWindow and the latter call
542 // should be simply ignored
543 return;
544 }
545
546 m_isBeingDeleted = true;
547
548 wxWindowDestroyEvent event;
549 event.SetEventObject(this);
550 event.SetId(GetId());
551 GetEventHandler()->ProcessEvent(event);
552 }
553
554 bool wxWindowBase::Destroy()
555 {
556 // If our handle is invalid, it means that this window has never been
557 // created, either because creating it failed or, more typically, because
558 // this wxWindow object was default-constructed and its Create() method had
559 // never been called. As we didn't send wxWindowCreateEvent in this case
560 // (which is sent after successful creation), don't send the matching
561 // wxWindowDestroyEvent neither.
562 if ( GetHandle() )
563 SendDestroyEvent();
564
565 delete this;
566
567 return true;
568 }
569
570 bool wxWindowBase::Close(bool force)
571 {
572 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
573 event.SetEventObject(this);
574 event.SetCanVeto(!force);
575
576 // return false if window wasn't closed because the application vetoed the
577 // close event
578 return HandleWindowEvent(event) && !event.GetVeto();
579 }
580
581 bool wxWindowBase::DestroyChildren()
582 {
583 wxWindowList::compatibility_iterator node;
584 for ( ;; )
585 {
586 // we iterate until the list becomes empty
587 node = GetChildren().GetFirst();
588 if ( !node )
589 break;
590
591 wxWindow *child = node->GetData();
592
593 // note that we really want to delete it immediately so don't call the
594 // possible overridden Destroy() version which might not delete the
595 // child immediately resulting in problems with our (top level) child
596 // outliving its parent
597 child->wxWindowBase::Destroy();
598
599 wxASSERT_MSG( !GetChildren().Find(child),
600 wxT("child didn't remove itself using RemoveChild()") );
601 }
602
603 return true;
604 }
605
606 // ----------------------------------------------------------------------------
607 // size/position related methods
608 // ----------------------------------------------------------------------------
609
610 // centre the window with respect to its parent in either (or both) directions
611 void wxWindowBase::DoCentre(int dir)
612 {
613 wxCHECK_RET( !(dir & wxCENTRE_ON_SCREEN) && GetParent(),
614 wxT("this method only implements centering child windows") );
615
616 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir));
617 }
618
619 // fits the window around the children
620 void wxWindowBase::Fit()
621 {
622 SetSize(GetBestSize());
623 }
624
625 // fits virtual size (ie. scrolled area etc.) around children
626 void wxWindowBase::FitInside()
627 {
628 SetVirtualSize( GetBestVirtualSize() );
629 }
630
631 // On Mac, scrollbars are explicitly children.
632 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
633 static bool wxHasRealChildren(const wxWindowBase* win)
634 {
635 int realChildCount = 0;
636
637 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
638 node;
639 node = node->GetNext() )
640 {
641 wxWindow *win = node->GetData();
642 if ( !win->IsTopLevel() && win->IsShown()
643 #if wxUSE_SCROLLBAR
644 && !wxDynamicCast(win, wxScrollBar)
645 #endif
646 )
647 realChildCount ++;
648 }
649 return (realChildCount > 0);
650 }
651 #endif
652
653 void wxWindowBase::InvalidateBestSize()
654 {
655 m_bestSizeCache = wxDefaultSize;
656
657 // parent's best size calculation may depend on its children's
658 // as long as child window we are in is not top level window itself
659 // (because the TLW size is never resized automatically)
660 // so let's invalidate it as well to be safe:
661 if (m_parent && !IsTopLevel())
662 m_parent->InvalidateBestSize();
663 }
664
665 // return the size best suited for the current window
666 wxSize wxWindowBase::DoGetBestSize() const
667 {
668 wxSize best;
669
670 if ( m_windowSizer )
671 {
672 best = m_windowSizer->GetMinSize();
673 }
674 #if wxUSE_CONSTRAINTS
675 else if ( m_constraints )
676 {
677 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
678
679 // our minimal acceptable size is such that all our windows fit inside
680 int maxX = 0,
681 maxY = 0;
682
683 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
684 node;
685 node = node->GetNext() )
686 {
687 wxLayoutConstraints *c = node->GetData()->GetConstraints();
688 if ( !c )
689 {
690 // it's not normal that we have an unconstrained child, but
691 // what can we do about it?
692 continue;
693 }
694
695 int x = c->right.GetValue(),
696 y = c->bottom.GetValue();
697
698 if ( x > maxX )
699 maxX = x;
700
701 if ( y > maxY )
702 maxY = y;
703
704 // TODO: we must calculate the overlaps somehow, otherwise we
705 // will never return a size bigger than the current one :-(
706 }
707
708 best = wxSize(maxX, maxY);
709 }
710 #endif // wxUSE_CONSTRAINTS
711 else if ( !GetChildren().empty()
712 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
713 && wxHasRealChildren(this)
714 #endif
715 )
716 {
717 // our minimal acceptable size is such that all our visible child
718 // windows fit inside
719 int maxX = 0,
720 maxY = 0;
721
722 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
723 node;
724 node = node->GetNext() )
725 {
726 wxWindow *win = node->GetData();
727 if ( win->IsTopLevel()
728 || !win->IsShown()
729 #if wxUSE_STATUSBAR
730 || wxDynamicCast(win, wxStatusBar)
731 #endif // wxUSE_STATUSBAR
732 )
733 {
734 // dialogs and frames lie in different top level windows -
735 // don't deal with them here; as for the status bars, they
736 // don't lie in the client area at all
737 continue;
738 }
739
740 int wx, wy, ww, wh;
741 win->GetPosition(&wx, &wy);
742
743 // if the window hadn't been positioned yet, assume that it is in
744 // the origin
745 if ( wx == wxDefaultCoord )
746 wx = 0;
747 if ( wy == wxDefaultCoord )
748 wy = 0;
749
750 win->GetSize(&ww, &wh);
751 if ( wx + ww > maxX )
752 maxX = wx + ww;
753 if ( wy + wh > maxY )
754 maxY = wy + wh;
755 }
756
757 best = wxSize(maxX, maxY);
758 }
759 else // ! has children
760 {
761 wxSize size = GetMinSize();
762 if ( !size.IsFullySpecified() )
763 {
764 // if the window doesn't define its best size we assume that it can
765 // be arbitrarily small -- usually this is not the case, of course,
766 // but we have no way to know what the limit is, it should really
767 // override DoGetBestClientSize() itself to tell us
768 size.SetDefaults(wxSize(1, 1));
769 }
770
771 // return as-is, unadjusted by the client size difference.
772 return size;
773 }
774
775 // Add any difference between size and client size
776 wxSize diff = GetSize() - GetClientSize();
777 best.x += wxMax(0, diff.x);
778 best.y += wxMax(0, diff.y);
779
780 return best;
781 }
782
783 // helper of GetWindowBorderSize(): as many ports don't implement support for
784 // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
785 // fallbacks in this case
786 static int wxGetMetricOrDefault(wxSystemMetric what, const wxWindowBase* win)
787 {
788 int rc = wxSystemSettings::GetMetric(
789 what, static_cast<wxWindow*>(const_cast<wxWindowBase*>(win)));
790 if ( rc == -1 )
791 {
792 switch ( what )
793 {
794 case wxSYS_BORDER_X:
795 case wxSYS_BORDER_Y:
796 // 2D border is by default 1 pixel wide
797 rc = 1;
798 break;
799
800 case wxSYS_EDGE_X:
801 case wxSYS_EDGE_Y:
802 // 3D borders are by default 2 pixels
803 rc = 2;
804 break;
805
806 default:
807 wxFAIL_MSG( wxT("unexpected wxGetMetricOrDefault() argument") );
808 rc = 0;
809 }
810 }
811
812 return rc;
813 }
814
815 wxSize wxWindowBase::GetWindowBorderSize() const
816 {
817 wxSize size;
818
819 switch ( GetBorder() )
820 {
821 case wxBORDER_NONE:
822 // nothing to do, size is already (0, 0)
823 break;
824
825 case wxBORDER_SIMPLE:
826 case wxBORDER_STATIC:
827 size.x = wxGetMetricOrDefault(wxSYS_BORDER_X, this);
828 size.y = wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
829 break;
830
831 case wxBORDER_SUNKEN:
832 case wxBORDER_RAISED:
833 size.x = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X, this),
834 wxGetMetricOrDefault(wxSYS_BORDER_X, this));
835 size.y = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y, this),
836 wxGetMetricOrDefault(wxSYS_BORDER_Y, this));
837 break;
838
839 case wxBORDER_DOUBLE:
840 size.x = wxGetMetricOrDefault(wxSYS_EDGE_X, this) +
841 wxGetMetricOrDefault(wxSYS_BORDER_X, this);
842 size.y = wxGetMetricOrDefault(wxSYS_EDGE_Y, this) +
843 wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
844 break;
845
846 default:
847 wxFAIL_MSG(wxT("Unknown border style."));
848 break;
849 }
850
851 // we have borders on both sides
852 return size*2;
853 }
854
855 bool
856 wxWindowBase::InformFirstDirection(int direction,
857 int size,
858 int availableOtherDir)
859 {
860 return GetSizer() && GetSizer()->InformFirstDirection(direction,
861 size,
862 availableOtherDir);
863 }
864
865 wxSize wxWindowBase::GetEffectiveMinSize() const
866 {
867 // merge the best size with the min size, giving priority to the min size
868 wxSize min = GetMinSize();
869
870 if (min.x == wxDefaultCoord || min.y == wxDefaultCoord)
871 {
872 wxSize best = GetBestSize();
873 if (min.x == wxDefaultCoord) min.x = best.x;
874 if (min.y == wxDefaultCoord) min.y = best.y;
875 }
876
877 return min;
878 }
879
880 wxSize wxWindowBase::DoGetBorderSize() const
881 {
882 // there is one case in which we can implement it for all ports easily
883 if ( GetBorder() == wxBORDER_NONE )
884 return wxSize(0, 0);
885
886 // otherwise use the difference between the real size and the client size
887 // as a fallback: notice that this is incorrect in general as client size
888 // also doesn't take the scrollbars into account
889 return GetSize() - GetClientSize();
890 }
891
892 wxSize wxWindowBase::GetBestSize() const
893 {
894 if ( !m_windowSizer && m_bestSizeCache.IsFullySpecified() )
895 return m_bestSizeCache;
896
897 // call DoGetBestClientSize() first, if a derived class overrides it wants
898 // it to be used
899 wxSize size = DoGetBestClientSize();
900 if ( size != wxDefaultSize )
901 size += DoGetBorderSize();
902 else
903 size = DoGetBestSize();
904
905 // Ensure that the best size is at least as large as min size.
906 size.IncTo(GetMinSize());
907
908 // And not larger than max size.
909 size.DecToIfSpecified(GetMaxSize());
910
911 // Finally cache result and return.
912 CacheBestSize(size);
913 return size;
914 }
915
916 int wxWindowBase::GetBestHeight(int width) const
917 {
918 const int height = DoGetBestClientHeight(width);
919
920 return height == wxDefaultCoord
921 ? GetBestSize().y
922 : height + DoGetBorderSize().y;
923 }
924
925 int wxWindowBase::GetBestWidth(int height) const
926 {
927 const int width = DoGetBestClientWidth(height);
928
929 return width == wxDefaultCoord
930 ? GetBestSize().x
931 : width + DoGetBorderSize().x;
932 }
933
934 void wxWindowBase::SetMinSize(const wxSize& minSize)
935 {
936 m_minWidth = minSize.x;
937 m_minHeight = minSize.y;
938
939 InvalidateBestSize();
940 }
941
942 void wxWindowBase::SetMaxSize(const wxSize& maxSize)
943 {
944 m_maxWidth = maxSize.x;
945 m_maxHeight = maxSize.y;
946
947 InvalidateBestSize();
948 }
949
950 void wxWindowBase::SetInitialSize(const wxSize& size)
951 {
952 // Set the min size to the size passed in. This will usually either be
953 // wxDefaultSize or the size passed to this window's ctor/Create function.
954 SetMinSize(size);
955
956 // Merge the size with the best size if needed
957 wxSize best = GetEffectiveMinSize();
958
959 // If the current size doesn't match then change it
960 if (GetSize() != best)
961 SetSize(best);
962 }
963
964
965 // by default the origin is not shifted
966 wxPoint wxWindowBase::GetClientAreaOrigin() const
967 {
968 return wxPoint(0,0);
969 }
970
971 wxSize wxWindowBase::ClientToWindowSize(const wxSize& size) const
972 {
973 const wxSize diff(GetSize() - GetClientSize());
974
975 return wxSize(size.x == -1 ? -1 : size.x + diff.x,
976 size.y == -1 ? -1 : size.y + diff.y);
977 }
978
979 wxSize wxWindowBase::WindowToClientSize(const wxSize& size) const
980 {
981 const wxSize diff(GetSize() - GetClientSize());
982
983 return wxSize(size.x == -1 ? -1 : size.x - diff.x,
984 size.y == -1 ? -1 : size.y - diff.y);
985 }
986
987 void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
988 {
989 if ( m_windowVariant != variant )
990 {
991 m_windowVariant = variant;
992
993 DoSetWindowVariant(variant);
994 }
995 }
996
997 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
998 {
999 // adjust the font height to correspond to our new variant (notice that
1000 // we're only called if something really changed)
1001 wxFont font = GetFont();
1002 int size = font.GetPointSize();
1003 switch ( variant )
1004 {
1005 case wxWINDOW_VARIANT_NORMAL:
1006 break;
1007
1008 case wxWINDOW_VARIANT_SMALL:
1009 size = wxRound(size * 3.0 / 4.0);
1010 break;
1011
1012 case wxWINDOW_VARIANT_MINI:
1013 size = wxRound(size * 2.0 / 3.0);
1014 break;
1015
1016 case wxWINDOW_VARIANT_LARGE:
1017 size = wxRound(size * 5.0 / 4.0);
1018 break;
1019
1020 default:
1021 wxFAIL_MSG(wxT("unexpected window variant"));
1022 break;
1023 }
1024
1025 font.SetPointSize(size);
1026 SetFont(font);
1027 }
1028
1029 void wxWindowBase::DoSetSizeHints( int minW, int minH,
1030 int maxW, int maxH,
1031 int WXUNUSED(incW), int WXUNUSED(incH) )
1032 {
1033 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
1034 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
1035 wxT("min width/height must be less than max width/height!") );
1036
1037 m_minWidth = minW;
1038 m_maxWidth = maxW;
1039 m_minHeight = minH;
1040 m_maxHeight = maxH;
1041 }
1042
1043
1044 #if WXWIN_COMPATIBILITY_2_8
1045 void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW), int WXUNUSED(minH),
1046 int WXUNUSED(maxW), int WXUNUSED(maxH))
1047 {
1048 }
1049
1050 void wxWindowBase::SetVirtualSizeHints(const wxSize& WXUNUSED(minsize),
1051 const wxSize& WXUNUSED(maxsize))
1052 {
1053 }
1054 #endif // WXWIN_COMPATIBILITY_2_8
1055
1056 void wxWindowBase::DoSetVirtualSize( int x, int y )
1057 {
1058 m_virtualSize = wxSize(x, y);
1059 }
1060
1061 wxSize wxWindowBase::DoGetVirtualSize() const
1062 {
1063 // we should use the entire client area so if it is greater than our
1064 // virtual size, expand it to fit (otherwise if the window is big enough we
1065 // wouldn't be using parts of it)
1066 wxSize size = GetClientSize();
1067 if ( m_virtualSize.x > size.x )
1068 size.x = m_virtualSize.x;
1069
1070 if ( m_virtualSize.y >= size.y )
1071 size.y = m_virtualSize.y;
1072
1073 return size;
1074 }
1075
1076 void wxWindowBase::DoGetScreenPosition(int *x, int *y) const
1077 {
1078 // screen position is the same as (0, 0) in client coords for non TLWs (and
1079 // TLWs override this method)
1080 if ( x )
1081 *x = 0;
1082 if ( y )
1083 *y = 0;
1084
1085 ClientToScreen(x, y);
1086 }
1087
1088 void wxWindowBase::SendSizeEvent(int flags)
1089 {
1090 wxSizeEvent event(GetSize(), GetId());
1091 event.SetEventObject(this);
1092 if ( flags & wxSEND_EVENT_POST )
1093 wxPostEvent(GetEventHandler(), event);
1094 else
1095 HandleWindowEvent(event);
1096 }
1097
1098 void wxWindowBase::SendSizeEventToParent(int flags)
1099 {
1100 wxWindow * const parent = GetParent();
1101 if ( parent && !parent->IsBeingDeleted() )
1102 parent->SendSizeEvent(flags);
1103 }
1104
1105 bool wxWindowBase::HasScrollbar(int orient) const
1106 {
1107 // if scrolling in the given direction is disabled, we can't have the
1108 // corresponding scrollbar no matter what
1109 if ( !CanScroll(orient) )
1110 return false;
1111
1112 const wxSize sizeVirt = GetVirtualSize();
1113 const wxSize sizeClient = GetClientSize();
1114
1115 return orient == wxHORIZONTAL ? sizeVirt.x > sizeClient.x
1116 : sizeVirt.y > sizeClient.y;
1117 }
1118
1119 // ----------------------------------------------------------------------------
1120 // show/hide/enable/disable the window
1121 // ----------------------------------------------------------------------------
1122
1123 bool wxWindowBase::Show(bool show)
1124 {
1125 if ( show != m_isShown )
1126 {
1127 m_isShown = show;
1128
1129 return true;
1130 }
1131 else
1132 {
1133 return false;
1134 }
1135 }
1136
1137 bool wxWindowBase::IsEnabled() const
1138 {
1139 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
1140 }
1141
1142 void wxWindowBase::NotifyWindowOnEnableChange(bool enabled)
1143 {
1144 // Under some platforms there is no need to update the window state
1145 // explicitly, it will become disabled when its parent is. On other ones we
1146 // do need to disable all windows recursively though.
1147 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
1148 DoEnable(enabled);
1149 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
1150
1151 // Disabling a top level window is typically done when showing a modal
1152 // dialog and we don't need to disable its children in this case, they will
1153 // be logically disabled anyhow (i.e. their IsEnabled() will return false)
1154 // and the TLW won't accept any input for them. Moreover, explicitly
1155 // disabling them would look ugly as the entire TLW would be greyed out
1156 // whenever a modal dialog is shown and no native applications under any
1157 // platform behave like this.
1158 if ( IsTopLevel() && !enabled )
1159 return;
1160
1161 // When disabling (or enabling back) a non-TLW window we need to
1162 // recursively propagate the change of the state to its children, otherwise
1163 // they would still show as enabled even though they wouldn't actually
1164 // accept any input (at least under MSW where children don't accept input
1165 // if any of the windows in their parent chain is enabled).
1166 #ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
1167 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1168 node;
1169 node = node->GetNext() )
1170 {
1171 wxWindowBase * const child = node->GetData();
1172 if ( !child->IsTopLevel() && child->IsThisEnabled() )
1173 child->NotifyWindowOnEnableChange(enabled);
1174 }
1175 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
1176 }
1177
1178 bool wxWindowBase::Enable(bool enable)
1179 {
1180 if ( enable == IsThisEnabled() )
1181 return false;
1182
1183 m_isEnabled = enable;
1184
1185 // If we call DoEnable() from NotifyWindowOnEnableChange(), we don't need
1186 // to do it from here.
1187 #ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
1188 DoEnable(enable);
1189 #endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
1190
1191 NotifyWindowOnEnableChange(enable);
1192
1193 return true;
1194 }
1195
1196 bool wxWindowBase::IsShownOnScreen() const
1197 {
1198 // A window is shown on screen if it itself is shown and so are all its
1199 // parents. But if a window is toplevel one, then its always visible on
1200 // screen if IsShown() returns true, even if it has a hidden parent.
1201 return IsShown() &&
1202 (IsTopLevel() || GetParent() == NULL || GetParent()->IsShownOnScreen());
1203 }
1204
1205 // ----------------------------------------------------------------------------
1206 // RTTI
1207 // ----------------------------------------------------------------------------
1208
1209 bool wxWindowBase::IsTopLevel() const
1210 {
1211 return false;
1212 }
1213
1214 // ----------------------------------------------------------------------------
1215 // Freeze/Thaw
1216 // ----------------------------------------------------------------------------
1217
1218 void wxWindowBase::Freeze()
1219 {
1220 if ( !m_freezeCount++ )
1221 {
1222 // physically freeze this window:
1223 DoFreeze();
1224
1225 // and recursively freeze all children:
1226 for ( wxWindowList::iterator i = GetChildren().begin();
1227 i != GetChildren().end(); ++i )
1228 {
1229 wxWindow *child = *i;
1230 if ( child->IsTopLevel() )
1231 continue;
1232
1233 child->Freeze();
1234 }
1235 }
1236 }
1237
1238 void wxWindowBase::Thaw()
1239 {
1240 wxASSERT_MSG( m_freezeCount, "Thaw() without matching Freeze()" );
1241
1242 if ( !--m_freezeCount )
1243 {
1244 // recursively thaw all children:
1245 for ( wxWindowList::iterator i = GetChildren().begin();
1246 i != GetChildren().end(); ++i )
1247 {
1248 wxWindow *child = *i;
1249 if ( child->IsTopLevel() )
1250 continue;
1251
1252 child->Thaw();
1253 }
1254
1255 // physically thaw this window:
1256 DoThaw();
1257 }
1258 }
1259
1260 // ----------------------------------------------------------------------------
1261 // Dealing with parents and children.
1262 // ----------------------------------------------------------------------------
1263
1264 bool wxWindowBase::IsDescendant(wxWindowBase* win) const
1265 {
1266 // Iterate until we find this window in the parent chain or exhaust it.
1267 while ( win )
1268 {
1269 if ( win == this )
1270 return true;
1271
1272 // Stop iterating on reaching the top level window boundary.
1273 if ( win->IsTopLevel() )
1274 break;
1275
1276 win = win->GetParent();
1277 }
1278
1279 return false;
1280 }
1281
1282 void wxWindowBase::AddChild(wxWindowBase *child)
1283 {
1284 wxCHECK_RET( child, wxT("can't add a NULL child") );
1285
1286 // this should never happen and it will lead to a crash later if it does
1287 // because RemoveChild() will remove only one node from the children list
1288 // and the other(s) one(s) will be left with dangling pointers in them
1289 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), wxT("AddChild() called twice") );
1290
1291 GetChildren().Append((wxWindow*)child);
1292 child->SetParent(this);
1293
1294 // adding a child while frozen will assert when thawed, so freeze it as if
1295 // it had been already present when we were frozen
1296 if ( IsFrozen() && !child->IsTopLevel() )
1297 child->Freeze();
1298 }
1299
1300 void wxWindowBase::RemoveChild(wxWindowBase *child)
1301 {
1302 wxCHECK_RET( child, wxT("can't remove a NULL child") );
1303
1304 // removing a child while frozen may result in permanently frozen window
1305 // if used e.g. from Reparent(), so thaw it
1306 //
1307 // NB: IsTopLevel() doesn't return true any more when a TLW child is being
1308 // removed from its ~wxWindowBase, so check for IsBeingDeleted() too
1309 if ( IsFrozen() && !child->IsBeingDeleted() && !child->IsTopLevel() )
1310 child->Thaw();
1311
1312 GetChildren().DeleteObject((wxWindow *)child);
1313 child->SetParent(NULL);
1314 }
1315
1316 void wxWindowBase::SetParent(wxWindowBase *parent)
1317 {
1318 // This assert catches typos which may result in using "this" instead of
1319 // "parent" when creating the window. This doesn't happen often but when it
1320 // does the results are unpleasant because the program typically just
1321 // crashes when due to a stack overflow or something similar and this
1322 // assert doesn't cost much (OTOH doing a more general check that the
1323 // parent is not one of our children would be more expensive and probably
1324 // not worth it).
1325 wxASSERT_MSG( parent != this, wxS("Can't use window as its own parent") );
1326
1327 m_parent = (wxWindow *)parent;
1328 }
1329
1330 bool wxWindowBase::Reparent(wxWindowBase *newParent)
1331 {
1332 wxWindow *oldParent = GetParent();
1333 if ( newParent == oldParent )
1334 {
1335 // nothing done
1336 return false;
1337 }
1338
1339 const bool oldEnabledState = IsEnabled();
1340
1341 // unlink this window from the existing parent.
1342 if ( oldParent )
1343 {
1344 oldParent->RemoveChild(this);
1345 }
1346 else
1347 {
1348 wxTopLevelWindows.DeleteObject((wxWindow *)this);
1349 }
1350
1351 // add it to the new one
1352 if ( newParent )
1353 {
1354 newParent->AddChild(this);
1355 }
1356 else
1357 {
1358 wxTopLevelWindows.Append((wxWindow *)this);
1359 }
1360
1361 // We need to notify window (and its subwindows) if by changing the parent
1362 // we also change our enabled/disabled status.
1363 const bool newEnabledState = IsEnabled();
1364 if ( newEnabledState != oldEnabledState )
1365 {
1366 NotifyWindowOnEnableChange(newEnabledState);
1367 }
1368
1369 return true;
1370 }
1371
1372 // ----------------------------------------------------------------------------
1373 // event handler stuff
1374 // ----------------------------------------------------------------------------
1375
1376 void wxWindowBase::SetEventHandler(wxEvtHandler *handler)
1377 {
1378 wxCHECK_RET(handler != NULL, "SetEventHandler(NULL) called");
1379
1380 m_eventHandler = handler;
1381 }
1382
1383 void wxWindowBase::SetNextHandler(wxEvtHandler *WXUNUSED(handler))
1384 {
1385 // disable wxEvtHandler chain mechanism for wxWindows:
1386 // wxWindow uses its own stack mechanism which doesn't mix well with wxEvtHandler's one
1387
1388 wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1389 }
1390 void wxWindowBase::SetPreviousHandler(wxEvtHandler *WXUNUSED(handler))
1391 {
1392 // we can't simply wxFAIL here as in SetNextHandler: in fact the last
1393 // handler of our stack when is destroyed will be Unlink()ed and thus
1394 // will call this function to update the pointer of this window...
1395
1396 //wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1397 }
1398
1399 void wxWindowBase::PushEventHandler(wxEvtHandler *handlerToPush)
1400 {
1401 wxCHECK_RET( handlerToPush != NULL, "PushEventHandler(NULL) called" );
1402
1403 // the new handler is going to be part of the wxWindow stack of event handlers:
1404 // it can't be part also of an event handler double-linked chain:
1405 wxASSERT_MSG(handlerToPush->IsUnlinked(),
1406 "The handler being pushed in the wxWindow stack shouldn't be part of "
1407 "a wxEvtHandler chain; call Unlink() on it first");
1408
1409 wxEvtHandler *handlerOld = GetEventHandler();
1410 wxCHECK_RET( handlerOld, "an old event handler is NULL?" );
1411
1412 // now use wxEvtHandler double-linked list to implement a stack:
1413 handlerToPush->SetNextHandler(handlerOld);
1414
1415 if (handlerOld != this)
1416 handlerOld->SetPreviousHandler(handlerToPush);
1417
1418 SetEventHandler(handlerToPush);
1419
1420 #if wxDEBUG_LEVEL
1421 // final checks of the operations done above:
1422 wxASSERT_MSG( handlerToPush->GetPreviousHandler() == NULL,
1423 "the first handler of the wxWindow stack should "
1424 "have no previous handlers set" );
1425 wxASSERT_MSG( handlerToPush->GetNextHandler() != NULL,
1426 "the first handler of the wxWindow stack should "
1427 "have non-NULL next handler" );
1428
1429 wxEvtHandler* pLast = handlerToPush;
1430 while ( pLast && pLast != this )
1431 pLast = pLast->GetNextHandler();
1432 wxASSERT_MSG( pLast->GetNextHandler() == NULL,
1433 "the last handler of the wxWindow stack should "
1434 "have this window as next handler" );
1435 #endif // wxDEBUG_LEVEL
1436 }
1437
1438 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
1439 {
1440 // we need to pop the wxWindow stack, i.e. we need to remove the first handler
1441
1442 wxEvtHandler *firstHandler = GetEventHandler();
1443 wxCHECK_MSG( firstHandler != NULL, NULL, "wxWindow cannot have a NULL event handler" );
1444 wxCHECK_MSG( firstHandler != this, NULL, "cannot pop the wxWindow itself" );
1445 wxCHECK_MSG( firstHandler->GetPreviousHandler() == NULL, NULL,
1446 "the first handler of the wxWindow stack should have no previous handlers set" );
1447
1448 wxEvtHandler *secondHandler = firstHandler->GetNextHandler();
1449 wxCHECK_MSG( secondHandler != NULL, NULL,
1450 "the first handler of the wxWindow stack should have non-NULL next handler" );
1451
1452 firstHandler->SetNextHandler(NULL);
1453
1454 // It is harmless but useless to unset the previous handler of the window
1455 // itself as it's always NULL anyhow, so don't do this.
1456 if ( secondHandler != this )
1457 secondHandler->SetPreviousHandler(NULL);
1458
1459 // now firstHandler is completely unlinked; set secondHandler as the new window event handler
1460 SetEventHandler(secondHandler);
1461
1462 if ( deleteHandler )
1463 {
1464 wxDELETE(firstHandler);
1465 }
1466
1467 return firstHandler;
1468 }
1469
1470 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handlerToRemove)
1471 {
1472 wxCHECK_MSG( handlerToRemove != NULL, false, "RemoveEventHandler(NULL) called" );
1473 wxCHECK_MSG( handlerToRemove != this, false, "Cannot remove the window itself" );
1474
1475 if (handlerToRemove == GetEventHandler())
1476 {
1477 // removing the first event handler is equivalent to "popping" the stack
1478 PopEventHandler(false);
1479 return true;
1480 }
1481
1482 // NOTE: the wxWindow event handler list is always terminated with "this" handler
1483 wxEvtHandler *handlerCur = GetEventHandler()->GetNextHandler();
1484 while ( handlerCur != this && handlerCur )
1485 {
1486 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
1487
1488 if ( handlerCur == handlerToRemove )
1489 {
1490 handlerCur->Unlink();
1491
1492 wxASSERT_MSG( handlerCur != GetEventHandler(),
1493 "the case Remove == Pop should was already handled" );
1494 return true;
1495 }
1496
1497 handlerCur = handlerNext;
1498 }
1499
1500 wxFAIL_MSG( wxT("where has the event handler gone?") );
1501
1502 return false;
1503 }
1504
1505 bool wxWindowBase::HandleWindowEvent(wxEvent& event) const
1506 {
1507 // SafelyProcessEvent() will handle exceptions nicely
1508 return GetEventHandler()->SafelyProcessEvent(event);
1509 }
1510
1511 // ----------------------------------------------------------------------------
1512 // colours, fonts &c
1513 // ----------------------------------------------------------------------------
1514
1515 void wxWindowBase::InheritAttributes()
1516 {
1517 const wxWindowBase * const parent = GetParent();
1518 if ( !parent )
1519 return;
1520
1521 // we only inherit attributes which had been explicitly set for the parent
1522 // which ensures that this only happens if the user really wants it and
1523 // not by default which wouldn't make any sense in modern GUIs where the
1524 // controls don't all use the same fonts (nor colours)
1525 if ( parent->m_inheritFont && !m_hasFont )
1526 SetFont(parent->GetFont());
1527
1528 // in addition, there is a possibility to explicitly forbid inheriting
1529 // colours at each class level by overriding ShouldInheritColours()
1530 if ( ShouldInheritColours() )
1531 {
1532 if ( parent->m_inheritFgCol && !m_hasFgCol )
1533 SetForegroundColour(parent->GetForegroundColour());
1534
1535 // inheriting (solid) background colour is wrong as it totally breaks
1536 // any kind of themed backgrounds
1537 //
1538 // instead, the controls should use the same background as their parent
1539 // (ideally by not drawing it at all)
1540 #if 0
1541 if ( parent->m_inheritBgCol && !m_hasBgCol )
1542 SetBackgroundColour(parent->GetBackgroundColour());
1543 #endif // 0
1544 }
1545 }
1546
1547 /* static */ wxVisualAttributes
1548 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1549 {
1550 // it is important to return valid values for all attributes from here,
1551 // GetXXX() below rely on this
1552 wxVisualAttributes attrs;
1553 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1554 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1555
1556 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1557 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1558 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1559 // colour on other platforms.
1560
1561 #if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1562 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1563 #else
1564 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1565 #endif
1566 return attrs;
1567 }
1568
1569 wxColour wxWindowBase::GetBackgroundColour() const
1570 {
1571 if ( !m_backgroundColour.IsOk() )
1572 {
1573 wxASSERT_MSG( !m_hasBgCol, wxT("we have invalid explicit bg colour?") );
1574
1575 // get our default background colour
1576 wxColour colBg = GetDefaultAttributes().colBg;
1577
1578 // we must return some valid colour to avoid redoing this every time
1579 // and also to avoid surprising the applications written for older
1580 // wxWidgets versions where GetBackgroundColour() always returned
1581 // something -- so give them something even if it doesn't make sense
1582 // for this window (e.g. it has a themed background)
1583 if ( !colBg.IsOk() )
1584 colBg = GetClassDefaultAttributes().colBg;
1585
1586 return colBg;
1587 }
1588 else
1589 return m_backgroundColour;
1590 }
1591
1592 wxColour wxWindowBase::GetForegroundColour() const
1593 {
1594 // logic is the same as above
1595 if ( !m_hasFgCol && !m_foregroundColour.IsOk() )
1596 {
1597 wxColour colFg = GetDefaultAttributes().colFg;
1598
1599 if ( !colFg.IsOk() )
1600 colFg = GetClassDefaultAttributes().colFg;
1601
1602 return colFg;
1603 }
1604 else
1605 return m_foregroundColour;
1606 }
1607
1608 bool wxWindowBase::SetBackgroundStyle(wxBackgroundStyle style)
1609 {
1610 // The checks below shouldn't be triggered if we're not really changing the
1611 // style.
1612 if ( style == m_backgroundStyle )
1613 return true;
1614
1615 // Transparent background style can be only set before creation because of
1616 // wxGTK limitation.
1617 wxCHECK_MSG( (style != wxBG_STYLE_TRANSPARENT) || !GetHandle(),
1618 false,
1619 "wxBG_STYLE_TRANSPARENT style can only be set before "
1620 "Create()-ing the window." );
1621
1622 // And once it is set, wxBG_STYLE_TRANSPARENT can't be unset.
1623 wxCHECK_MSG( (m_backgroundStyle != wxBG_STYLE_TRANSPARENT) ||
1624 (style == wxBG_STYLE_TRANSPARENT),
1625 false,
1626 "wxBG_STYLE_TRANSPARENT can't be unset once it was set." );
1627
1628 m_backgroundStyle = style;
1629
1630 return true;
1631 }
1632
1633 bool wxWindowBase::IsTransparentBackgroundSupported(wxString *reason) const
1634 {
1635 if ( reason )
1636 *reason = _("This platform does not support background transparency.");
1637
1638 return false;
1639 }
1640
1641 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1642 {
1643 if ( colour == m_backgroundColour )
1644 return false;
1645
1646 m_hasBgCol = colour.IsOk();
1647
1648 m_inheritBgCol = m_hasBgCol;
1649 m_backgroundColour = colour;
1650 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.IsOk() );
1651 return true;
1652 }
1653
1654 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1655 {
1656 if (colour == m_foregroundColour )
1657 return false;
1658
1659 m_hasFgCol = colour.IsOk();
1660 m_inheritFgCol = m_hasFgCol;
1661 m_foregroundColour = colour;
1662 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.IsOk() );
1663 return true;
1664 }
1665
1666 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1667 {
1668 // setting an invalid cursor is ok, it means that we don't have any special
1669 // cursor
1670 if ( m_cursor.IsSameAs(cursor) )
1671 {
1672 // no change
1673 return false;
1674 }
1675
1676 m_cursor = cursor;
1677
1678 return true;
1679 }
1680
1681 wxFont wxWindowBase::GetFont() const
1682 {
1683 // logic is the same as in GetBackgroundColour()
1684 if ( !m_font.IsOk() )
1685 {
1686 wxASSERT_MSG( !m_hasFont, wxT("we have invalid explicit font?") );
1687
1688 wxFont font = GetDefaultAttributes().font;
1689 if ( !font.IsOk() )
1690 font = GetClassDefaultAttributes().font;
1691
1692 return font;
1693 }
1694 else
1695 return m_font;
1696 }
1697
1698 bool wxWindowBase::SetFont(const wxFont& font)
1699 {
1700 if ( font == m_font )
1701 {
1702 // no change
1703 return false;
1704 }
1705
1706 m_font = font;
1707 m_hasFont = font.IsOk();
1708 m_inheritFont = m_hasFont;
1709
1710 InvalidateBestSize();
1711
1712 return true;
1713 }
1714
1715 #if wxUSE_PALETTE
1716
1717 void wxWindowBase::SetPalette(const wxPalette& pal)
1718 {
1719 m_hasCustomPalette = true;
1720 m_palette = pal;
1721
1722 // VZ: can anyone explain me what do we do here?
1723 wxWindowDC d((wxWindow *) this);
1724 d.SetPalette(pal);
1725 }
1726
1727 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1728 {
1729 wxWindow *win = (wxWindow *)this;
1730 while ( win && !win->HasCustomPalette() )
1731 {
1732 win = win->GetParent();
1733 }
1734
1735 return win;
1736 }
1737
1738 #endif // wxUSE_PALETTE
1739
1740 #if wxUSE_CARET
1741 void wxWindowBase::SetCaret(wxCaret *caret)
1742 {
1743 if ( m_caret )
1744 {
1745 delete m_caret;
1746 }
1747
1748 m_caret = caret;
1749
1750 if ( m_caret )
1751 {
1752 wxASSERT_MSG( m_caret->GetWindow() == this,
1753 wxT("caret should be created associated to this window") );
1754 }
1755 }
1756 #endif // wxUSE_CARET
1757
1758 #if wxUSE_VALIDATORS
1759 // ----------------------------------------------------------------------------
1760 // validators
1761 // ----------------------------------------------------------------------------
1762
1763 void wxWindowBase::SetValidator(const wxValidator& validator)
1764 {
1765 if ( m_windowValidator )
1766 delete m_windowValidator;
1767
1768 m_windowValidator = (wxValidator *)validator.Clone();
1769
1770 if ( m_windowValidator )
1771 m_windowValidator->SetWindow(this);
1772 }
1773 #endif // wxUSE_VALIDATORS
1774
1775 // ----------------------------------------------------------------------------
1776 // update region stuff
1777 // ----------------------------------------------------------------------------
1778
1779 wxRect wxWindowBase::GetUpdateClientRect() const
1780 {
1781 wxRegion rgnUpdate = GetUpdateRegion();
1782 rgnUpdate.Intersect(GetClientRect());
1783 wxRect rectUpdate = rgnUpdate.GetBox();
1784 wxPoint ptOrigin = GetClientAreaOrigin();
1785 rectUpdate.x -= ptOrigin.x;
1786 rectUpdate.y -= ptOrigin.y;
1787
1788 return rectUpdate;
1789 }
1790
1791 bool wxWindowBase::DoIsExposed(int x, int y) const
1792 {
1793 return m_updateRegion.Contains(x, y) != wxOutRegion;
1794 }
1795
1796 bool wxWindowBase::DoIsExposed(int x, int y, int w, int h) const
1797 {
1798 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1799 }
1800
1801 void wxWindowBase::ClearBackground()
1802 {
1803 // wxGTK uses its own version, no need to add never used code
1804 #ifndef __WXGTK__
1805 wxClientDC dc((wxWindow *)this);
1806 wxBrush brush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID);
1807 dc.SetBackground(brush);
1808 dc.Clear();
1809 #endif // __WXGTK__
1810 }
1811
1812 // ----------------------------------------------------------------------------
1813 // find child window by id or name
1814 // ----------------------------------------------------------------------------
1815
1816 wxWindow *wxWindowBase::FindWindow(long id) const
1817 {
1818 if ( id == m_windowId )
1819 return (wxWindow *)this;
1820
1821 wxWindowBase *res = NULL;
1822 wxWindowList::compatibility_iterator node;
1823 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1824 {
1825 wxWindowBase *child = node->GetData();
1826 res = child->FindWindow( id );
1827 }
1828
1829 return (wxWindow *)res;
1830 }
1831
1832 wxWindow *wxWindowBase::FindWindow(const wxString& name) const
1833 {
1834 if ( name == m_windowName )
1835 return (wxWindow *)this;
1836
1837 wxWindowBase *res = NULL;
1838 wxWindowList::compatibility_iterator node;
1839 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1840 {
1841 wxWindow *child = node->GetData();
1842 res = child->FindWindow(name);
1843 }
1844
1845 return (wxWindow *)res;
1846 }
1847
1848
1849 // find any window by id or name or label: If parent is non-NULL, look through
1850 // children for a label or title matching the specified string. If NULL, look
1851 // through all top-level windows.
1852 //
1853 // to avoid duplicating code we reuse the same helper function but with
1854 // different comparators
1855
1856 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1857 const wxString& label, long id);
1858
1859 static
1860 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1861 long WXUNUSED(id))
1862 {
1863 return win->GetLabel() == label;
1864 }
1865
1866 static
1867 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1868 long WXUNUSED(id))
1869 {
1870 return win->GetName() == label;
1871 }
1872
1873 static
1874 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1875 long id)
1876 {
1877 return win->GetId() == id;
1878 }
1879
1880 // recursive helper for the FindWindowByXXX() functions
1881 static
1882 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1883 const wxString& label,
1884 long id,
1885 wxFindWindowCmp cmp)
1886 {
1887 if ( parent )
1888 {
1889 // see if this is the one we're looking for
1890 if ( (*cmp)(parent, label, id) )
1891 return (wxWindow *)parent;
1892
1893 // It wasn't, so check all its children
1894 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1895 node;
1896 node = node->GetNext() )
1897 {
1898 // recursively check each child
1899 wxWindow *win = (wxWindow *)node->GetData();
1900 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1901 if (retwin)
1902 return retwin;
1903 }
1904 }
1905
1906 // Not found
1907 return NULL;
1908 }
1909
1910 // helper for FindWindowByXXX()
1911 static
1912 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1913 const wxString& label,
1914 long id,
1915 wxFindWindowCmp cmp)
1916 {
1917 if ( parent )
1918 {
1919 // just check parent and all its children
1920 return wxFindWindowRecursively(parent, label, id, cmp);
1921 }
1922
1923 // start at very top of wx's windows
1924 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1925 node;
1926 node = node->GetNext() )
1927 {
1928 // recursively check each window & its children
1929 wxWindow *win = node->GetData();
1930 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1931 if (retwin)
1932 return retwin;
1933 }
1934
1935 return NULL;
1936 }
1937
1938 /* static */
1939 wxWindow *
1940 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1941 {
1942 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1943 }
1944
1945 /* static */
1946 wxWindow *
1947 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1948 {
1949 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1950
1951 if ( !win )
1952 {
1953 // fall back to the label
1954 win = FindWindowByLabel(title, parent);
1955 }
1956
1957 return win;
1958 }
1959
1960 /* static */
1961 wxWindow *
1962 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1963 {
1964 return wxFindWindowHelper(parent, wxEmptyString, id, wxFindWindowCmpIds);
1965 }
1966
1967 // ----------------------------------------------------------------------------
1968 // dialog oriented functions
1969 // ----------------------------------------------------------------------------
1970
1971 #if WXWIN_COMPATIBILITY_2_8
1972 void wxWindowBase::MakeModal(bool modal)
1973 {
1974 // Disable all other windows
1975 if ( IsTopLevel() )
1976 {
1977 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1978 while (node)
1979 {
1980 wxWindow *win = node->GetData();
1981 if (win != this)
1982 win->Enable(!modal);
1983
1984 node = node->GetNext();
1985 }
1986 }
1987 }
1988 #endif // WXWIN_COMPATIBILITY_2_8
1989
1990 #if wxUSE_VALIDATORS
1991
1992 namespace
1993 {
1994
1995 // This class encapsulates possibly recursive iteration on window children done
1996 // by Validate() and TransferData{To,From}Window() and allows to avoid code
1997 // duplication in all three functions.
1998 class ValidationTraverserBase
1999 {
2000 public:
2001 wxEXPLICIT ValidationTraverserBase(wxWindowBase* win)
2002 : m_win(static_cast<wxWindow*>(win))
2003 {
2004 }
2005
2006 // Traverse all the direct children calling OnDo() on them and also all
2007 // grandchildren if wxWS_EX_VALIDATE_RECURSIVELY is used, calling
2008 // OnRecurse() for them.
2009 bool DoForAllChildren()
2010 {
2011 const bool recurse = m_win->HasExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
2012
2013 wxWindowList& children = m_win->GetChildren();
2014 for ( wxWindowList::iterator i = children.begin();
2015 i != children.end();
2016 ++i )
2017 {
2018 wxWindow* const child = static_cast<wxWindow*>(*i);
2019 wxValidator* const validator = child->GetValidator();
2020 if ( validator && !OnDo(validator) )
2021 {
2022 return false;
2023 }
2024
2025 // Notice that validation should never recurse into top level
2026 // children, e.g. some other dialog which might happen to be
2027 // currently shown.
2028 if ( recurse && !child->IsTopLevel() && !OnRecurse(child) )
2029 {
2030 return false;
2031 }
2032 }
2033
2034 return true;
2035 }
2036
2037 // Give it a virtual dtor just to suppress gcc warnings about a class with
2038 // virtual methods but non-virtual dtor -- even if this is completely safe
2039 // here as we never use the objects of this class polymorphically.
2040 virtual ~ValidationTraverserBase() { }
2041
2042 protected:
2043 // Called for each child, validator is guaranteed to be non-NULL.
2044 virtual bool OnDo(wxValidator* validator) = 0;
2045
2046 // Called for each child if we need to recurse into its children.
2047 virtual bool OnRecurse(wxWindow* child) = 0;
2048
2049
2050 // The window whose children we're traversing.
2051 wxWindow* const m_win;
2052
2053 wxDECLARE_NO_COPY_CLASS(ValidationTraverserBase);
2054 };
2055
2056 } // anonymous namespace
2057
2058 #endif // wxUSE_VALIDATORS
2059
2060 bool wxWindowBase::Validate()
2061 {
2062 #if wxUSE_VALIDATORS
2063 class ValidateTraverser : public ValidationTraverserBase
2064 {
2065 public:
2066 wxEXPLICIT ValidateTraverser(wxWindowBase* win)
2067 : ValidationTraverserBase(win)
2068 {
2069 }
2070
2071 virtual bool OnDo(wxValidator* validator)
2072 {
2073 return validator->Validate(m_win);
2074 }
2075
2076 virtual bool OnRecurse(wxWindow* child)
2077 {
2078 return child->Validate();
2079 }
2080 };
2081
2082 return ValidateTraverser(this).DoForAllChildren();
2083 #else // !wxUSE_VALIDATORS
2084 return true;
2085 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
2086 }
2087
2088 bool wxWindowBase::TransferDataToWindow()
2089 {
2090 #if wxUSE_VALIDATORS
2091 class DataToWindowTraverser : public ValidationTraverserBase
2092 {
2093 public:
2094 wxEXPLICIT DataToWindowTraverser(wxWindowBase* win)
2095 : ValidationTraverserBase(win)
2096 {
2097 }
2098
2099 virtual bool OnDo(wxValidator* validator)
2100 {
2101 if ( !validator->TransferToWindow() )
2102 {
2103 wxLogWarning(_("Could not transfer data to window"));
2104 #if wxUSE_LOG
2105 wxLog::FlushActive();
2106 #endif // wxUSE_LOG
2107
2108 return false;
2109 }
2110
2111 return true;
2112 }
2113
2114 virtual bool OnRecurse(wxWindow* child)
2115 {
2116 return child->TransferDataToWindow();
2117 }
2118 };
2119
2120 return DataToWindowTraverser(this).DoForAllChildren();
2121 #else // !wxUSE_VALIDATORS
2122 return true;
2123 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
2124 }
2125
2126 bool wxWindowBase::TransferDataFromWindow()
2127 {
2128 #if wxUSE_VALIDATORS
2129 class DataFromWindowTraverser : public ValidationTraverserBase
2130 {
2131 public:
2132 DataFromWindowTraverser(wxWindowBase* win)
2133 : ValidationTraverserBase(win)
2134 {
2135 }
2136
2137 virtual bool OnDo(wxValidator* validator)
2138 {
2139 return validator->TransferFromWindow();
2140 }
2141
2142 virtual bool OnRecurse(wxWindow* child)
2143 {
2144 return child->TransferDataFromWindow();
2145 }
2146 };
2147
2148 return DataFromWindowTraverser(this).DoForAllChildren();
2149 #else // !wxUSE_VALIDATORS
2150 return true;
2151 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
2152 }
2153
2154 void wxWindowBase::InitDialog()
2155 {
2156 wxInitDialogEvent event(GetId());
2157 event.SetEventObject( this );
2158 GetEventHandler()->ProcessEvent(event);
2159 }
2160
2161 // ----------------------------------------------------------------------------
2162 // context-sensitive help support
2163 // ----------------------------------------------------------------------------
2164
2165 #if wxUSE_HELP
2166
2167 // associate this help text with this window
2168 void wxWindowBase::SetHelpText(const wxString& text)
2169 {
2170 wxHelpProvider *helpProvider = wxHelpProvider::Get();
2171 if ( helpProvider )
2172 {
2173 helpProvider->AddHelp(this, text);
2174 }
2175 }
2176
2177 #if WXWIN_COMPATIBILITY_2_8
2178 // associate this help text with all windows with the same id as this
2179 // one
2180 void wxWindowBase::SetHelpTextForId(const wxString& text)
2181 {
2182 wxHelpProvider *helpProvider = wxHelpProvider::Get();
2183 if ( helpProvider )
2184 {
2185 helpProvider->AddHelp(GetId(), text);
2186 }
2187 }
2188 #endif // WXWIN_COMPATIBILITY_2_8
2189
2190 // get the help string associated with this window (may be empty)
2191 // default implementation forwards calls to the help provider
2192 wxString
2193 wxWindowBase::GetHelpTextAtPoint(const wxPoint & WXUNUSED(pt),
2194 wxHelpEvent::Origin WXUNUSED(origin)) const
2195 {
2196 wxString text;
2197 wxHelpProvider *helpProvider = wxHelpProvider::Get();
2198 if ( helpProvider )
2199 {
2200 text = helpProvider->GetHelp(this);
2201 }
2202
2203 return text;
2204 }
2205
2206 // show help for this window
2207 void wxWindowBase::OnHelp(wxHelpEvent& event)
2208 {
2209 wxHelpProvider *helpProvider = wxHelpProvider::Get();
2210 if ( helpProvider )
2211 {
2212 wxPoint pos = event.GetPosition();
2213 const wxHelpEvent::Origin origin = event.GetOrigin();
2214 if ( origin == wxHelpEvent::Origin_Keyboard )
2215 {
2216 // if the help event was generated from keyboard it shouldn't
2217 // appear at the mouse position (which is still the only position
2218 // associated with help event) if the mouse is far away, although
2219 // we still do use the mouse position if it's over the window
2220 // because we suppose the user looks approximately at the mouse
2221 // already and so it would be more convenient than showing tooltip
2222 // at some arbitrary position which can be quite far from it
2223 const wxRect rectClient = GetClientRect();
2224 if ( !rectClient.Contains(ScreenToClient(pos)) )
2225 {
2226 // position help slightly under and to the right of this window
2227 pos = ClientToScreen(wxPoint(
2228 2*GetCharWidth(),
2229 rectClient.height + GetCharHeight()
2230 ));
2231 }
2232 }
2233
2234 if ( helpProvider->ShowHelpAtPoint(this, pos, origin) )
2235 {
2236 // skip the event.Skip() below
2237 return;
2238 }
2239 }
2240
2241 event.Skip();
2242 }
2243
2244 #endif // wxUSE_HELP
2245
2246 // ----------------------------------------------------------------------------
2247 // tooltips
2248 // ----------------------------------------------------------------------------
2249
2250 #if wxUSE_TOOLTIPS
2251
2252 wxString wxWindowBase::GetToolTipText() const
2253 {
2254 return m_tooltip ? m_tooltip->GetTip() : wxString();
2255 }
2256
2257 void wxWindowBase::SetToolTip( const wxString &tip )
2258 {
2259 // don't create the new tooltip if we already have one
2260 if ( m_tooltip )
2261 {
2262 m_tooltip->SetTip( tip );
2263 }
2264 else
2265 {
2266 SetToolTip( new wxToolTip( tip ) );
2267 }
2268
2269 // setting empty tooltip text does not remove the tooltip any more - use
2270 // SetToolTip(NULL) for this
2271 }
2272
2273 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
2274 {
2275 if ( m_tooltip != tooltip )
2276 {
2277 if ( m_tooltip )
2278 delete m_tooltip;
2279
2280 m_tooltip = tooltip;
2281 }
2282 }
2283
2284 bool wxWindowBase::CopyToolTip(wxToolTip *tip)
2285 {
2286 SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL);
2287
2288 return tip != NULL;
2289 }
2290
2291 #endif // wxUSE_TOOLTIPS
2292
2293 // ----------------------------------------------------------------------------
2294 // constraints and sizers
2295 // ----------------------------------------------------------------------------
2296
2297 #if wxUSE_CONSTRAINTS
2298
2299 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
2300 {
2301 if ( m_constraints )
2302 {
2303 UnsetConstraints(m_constraints);
2304 delete m_constraints;
2305 }
2306 m_constraints = constraints;
2307 if ( m_constraints )
2308 {
2309 // Make sure other windows know they're part of a 'meaningful relationship'
2310 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
2311 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
2312 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
2313 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
2314 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
2315 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
2316 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
2317 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
2318 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
2319 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
2320 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
2321 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
2322 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
2323 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
2324 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
2325 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
2326 }
2327 }
2328
2329 // This removes any dangling pointers to this window in other windows'
2330 // constraintsInvolvedIn lists.
2331 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
2332 {
2333 if ( c )
2334 {
2335 if ( c->left.GetOtherWindow() && (c->left.GetOtherWindow() != this) )
2336 c->left.GetOtherWindow()->RemoveConstraintReference(this);
2337 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
2338 c->top.GetOtherWindow()->RemoveConstraintReference(this);
2339 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
2340 c->right.GetOtherWindow()->RemoveConstraintReference(this);
2341 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
2342 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
2343 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
2344 c->width.GetOtherWindow()->RemoveConstraintReference(this);
2345 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
2346 c->height.GetOtherWindow()->RemoveConstraintReference(this);
2347 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
2348 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
2349 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
2350 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
2351 }
2352 }
2353
2354 // Back-pointer to other windows we're involved with, so if we delete this
2355 // window, we must delete any constraints we're involved with.
2356 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
2357 {
2358 if ( !m_constraintsInvolvedIn )
2359 m_constraintsInvolvedIn = new wxWindowList;
2360 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
2361 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
2362 }
2363
2364 // REMOVE back-pointer to other windows we're involved with.
2365 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
2366 {
2367 if ( m_constraintsInvolvedIn )
2368 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
2369 }
2370
2371 // Reset any constraints that mention this window
2372 void wxWindowBase::DeleteRelatedConstraints()
2373 {
2374 if ( m_constraintsInvolvedIn )
2375 {
2376 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
2377 while (node)
2378 {
2379 wxWindow *win = node->GetData();
2380 wxLayoutConstraints *constr = win->GetConstraints();
2381
2382 // Reset any constraints involving this window
2383 if ( constr )
2384 {
2385 constr->left.ResetIfWin(this);
2386 constr->top.ResetIfWin(this);
2387 constr->right.ResetIfWin(this);
2388 constr->bottom.ResetIfWin(this);
2389 constr->width.ResetIfWin(this);
2390 constr->height.ResetIfWin(this);
2391 constr->centreX.ResetIfWin(this);
2392 constr->centreY.ResetIfWin(this);
2393 }
2394
2395 wxWindowList::compatibility_iterator next = node->GetNext();
2396 m_constraintsInvolvedIn->Erase(node);
2397 node = next;
2398 }
2399
2400 wxDELETE(m_constraintsInvolvedIn);
2401 }
2402 }
2403
2404 #endif // wxUSE_CONSTRAINTS
2405
2406 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
2407 {
2408 if ( sizer == m_windowSizer)
2409 return;
2410
2411 if ( m_windowSizer )
2412 {
2413 m_windowSizer->SetContainingWindow(NULL);
2414
2415 if ( deleteOld )
2416 delete m_windowSizer;
2417 }
2418
2419 m_windowSizer = sizer;
2420 if ( m_windowSizer )
2421 {
2422 m_windowSizer->SetContainingWindow((wxWindow *)this);
2423 }
2424
2425 SetAutoLayout(m_windowSizer != NULL);
2426 }
2427
2428 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
2429 {
2430 SetSizer( sizer, deleteOld );
2431 sizer->SetSizeHints( (wxWindow*) this );
2432 }
2433
2434
2435 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
2436 {
2437 // adding a window to a sizer twice is going to result in fatal and
2438 // hard to debug problems later because when deleting the second
2439 // associated wxSizerItem we're going to dereference a dangling
2440 // pointer; so try to detect this as early as possible
2441 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
2442 wxT("Adding a window to the same sizer twice?") );
2443
2444 m_containingSizer = sizer;
2445 }
2446
2447 #if wxUSE_CONSTRAINTS
2448
2449 void wxWindowBase::SatisfyConstraints()
2450 {
2451 wxLayoutConstraints *constr = GetConstraints();
2452 bool wasOk = constr && constr->AreSatisfied();
2453
2454 ResetConstraints(); // Mark all constraints as unevaluated
2455
2456 int noChanges = 1;
2457
2458 // if we're a top level panel (i.e. our parent is frame/dialog), our
2459 // own constraints will never be satisfied any more unless we do it
2460 // here
2461 if ( wasOk )
2462 {
2463 while ( noChanges > 0 )
2464 {
2465 LayoutPhase1(&noChanges);
2466 }
2467 }
2468
2469 LayoutPhase2(&noChanges);
2470 }
2471
2472 #endif // wxUSE_CONSTRAINTS
2473
2474 bool wxWindowBase::Layout()
2475 {
2476 // If there is a sizer, use it instead of the constraints
2477 if ( GetSizer() )
2478 {
2479 int w = 0, h = 0;
2480 GetVirtualSize(&w, &h);
2481 GetSizer()->SetDimension( 0, 0, w, h );
2482 }
2483 #if wxUSE_CONSTRAINTS
2484 else
2485 {
2486 SatisfyConstraints(); // Find the right constraints values
2487 SetConstraintSizes(); // Recursively set the real window sizes
2488 }
2489 #endif
2490
2491 return true;
2492 }
2493
2494 void wxWindowBase::InternalOnSize(wxSizeEvent& event)
2495 {
2496 if ( GetAutoLayout() )
2497 Layout();
2498
2499 event.Skip();
2500 }
2501
2502 #if wxUSE_CONSTRAINTS
2503
2504 // first phase of the constraints evaluation: set our own constraints
2505 bool wxWindowBase::LayoutPhase1(int *noChanges)
2506 {
2507 wxLayoutConstraints *constr = GetConstraints();
2508
2509 return !constr || constr->SatisfyConstraints(this, noChanges);
2510 }
2511
2512 // second phase: set the constraints for our children
2513 bool wxWindowBase::LayoutPhase2(int *noChanges)
2514 {
2515 *noChanges = 0;
2516
2517 // Layout children
2518 DoPhase(1);
2519
2520 // Layout grand children
2521 DoPhase(2);
2522
2523 return true;
2524 }
2525
2526 // Do a phase of evaluating child constraints
2527 bool wxWindowBase::DoPhase(int phase)
2528 {
2529 // the list containing the children for which the constraints are already
2530 // set correctly
2531 wxWindowList succeeded;
2532
2533 // the max number of iterations we loop before concluding that we can't set
2534 // the constraints
2535 static const int maxIterations = 500;
2536
2537 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
2538 {
2539 int noChanges = 0;
2540
2541 // loop over all children setting their constraints
2542 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2543 node;
2544 node = node->GetNext() )
2545 {
2546 wxWindow *child = node->GetData();
2547 if ( child->IsTopLevel() )
2548 {
2549 // top level children are not inside our client area
2550 continue;
2551 }
2552
2553 if ( !child->GetConstraints() || succeeded.Find(child) )
2554 {
2555 // this one is either already ok or nothing we can do about it
2556 continue;
2557 }
2558
2559 int tempNoChanges = 0;
2560 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
2561 : child->LayoutPhase2(&tempNoChanges);
2562 noChanges += tempNoChanges;
2563
2564 if ( success )
2565 {
2566 succeeded.Append(child);
2567 }
2568 }
2569
2570 if ( !noChanges )
2571 {
2572 // constraints are set
2573 break;
2574 }
2575 }
2576
2577 return true;
2578 }
2579
2580 void wxWindowBase::ResetConstraints()
2581 {
2582 wxLayoutConstraints *constr = GetConstraints();
2583 if ( constr )
2584 {
2585 constr->left.SetDone(false);
2586 constr->top.SetDone(false);
2587 constr->right.SetDone(false);
2588 constr->bottom.SetDone(false);
2589 constr->width.SetDone(false);
2590 constr->height.SetDone(false);
2591 constr->centreX.SetDone(false);
2592 constr->centreY.SetDone(false);
2593 }
2594
2595 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2596 while (node)
2597 {
2598 wxWindow *win = node->GetData();
2599 if ( !win->IsTopLevel() )
2600 win->ResetConstraints();
2601 node = node->GetNext();
2602 }
2603 }
2604
2605 // Need to distinguish between setting the 'fake' size for windows and sizers,
2606 // and setting the real values.
2607 void wxWindowBase::SetConstraintSizes(bool recurse)
2608 {
2609 wxLayoutConstraints *constr = GetConstraints();
2610 if ( constr && constr->AreSatisfied() )
2611 {
2612 ChildrenRepositioningGuard repositionGuard(this);
2613
2614 int x = constr->left.GetValue();
2615 int y = constr->top.GetValue();
2616 int w = constr->width.GetValue();
2617 int h = constr->height.GetValue();
2618
2619 if ( (constr->width.GetRelationship() != wxAsIs ) ||
2620 (constr->height.GetRelationship() != wxAsIs) )
2621 {
2622 // We really shouldn't set negative sizes for the windows so make
2623 // them at least of 1*1 size
2624 SetSize(x, y, w > 0 ? w : 1, h > 0 ? h : 1);
2625 }
2626 else
2627 {
2628 // If we don't want to resize this window, just move it...
2629 Move(x, y);
2630 }
2631 }
2632 else if ( constr )
2633 {
2634 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
2635 GetClassInfo()->GetClassName(),
2636 GetName().c_str());
2637 }
2638
2639 if ( recurse )
2640 {
2641 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2642 while (node)
2643 {
2644 wxWindow *win = node->GetData();
2645 if ( !win->IsTopLevel() && win->GetConstraints() )
2646 win->SetConstraintSizes();
2647 node = node->GetNext();
2648 }
2649 }
2650 }
2651
2652 // Only set the size/position of the constraint (if any)
2653 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
2654 {
2655 wxLayoutConstraints *constr = GetConstraints();
2656 if ( constr )
2657 {
2658 if ( x != wxDefaultCoord )
2659 {
2660 constr->left.SetValue(x);
2661 constr->left.SetDone(true);
2662 }
2663 if ( y != wxDefaultCoord )
2664 {
2665 constr->top.SetValue(y);
2666 constr->top.SetDone(true);
2667 }
2668 if ( w != wxDefaultCoord )
2669 {
2670 constr->width.SetValue(w);
2671 constr->width.SetDone(true);
2672 }
2673 if ( h != wxDefaultCoord )
2674 {
2675 constr->height.SetValue(h);
2676 constr->height.SetDone(true);
2677 }
2678 }
2679 }
2680
2681 void wxWindowBase::MoveConstraint(int x, int y)
2682 {
2683 wxLayoutConstraints *constr = GetConstraints();
2684 if ( constr )
2685 {
2686 if ( x != wxDefaultCoord )
2687 {
2688 constr->left.SetValue(x);
2689 constr->left.SetDone(true);
2690 }
2691 if ( y != wxDefaultCoord )
2692 {
2693 constr->top.SetValue(y);
2694 constr->top.SetDone(true);
2695 }
2696 }
2697 }
2698
2699 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
2700 {
2701 wxLayoutConstraints *constr = GetConstraints();
2702 if ( constr )
2703 {
2704 *w = constr->width.GetValue();
2705 *h = constr->height.GetValue();
2706 }
2707 else
2708 GetSize(w, h);
2709 }
2710
2711 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
2712 {
2713 wxLayoutConstraints *constr = GetConstraints();
2714 if ( constr )
2715 {
2716 *w = constr->width.GetValue();
2717 *h = constr->height.GetValue();
2718 }
2719 else
2720 GetClientSize(w, h);
2721 }
2722
2723 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
2724 {
2725 wxLayoutConstraints *constr = GetConstraints();
2726 if ( constr )
2727 {
2728 *x = constr->left.GetValue();
2729 *y = constr->top.GetValue();
2730 }
2731 else
2732 GetPosition(x, y);
2733 }
2734
2735 #endif // wxUSE_CONSTRAINTS
2736
2737 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
2738 {
2739 wxWindow *parent = GetParent();
2740 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
2741 {
2742 wxPoint pt(parent->GetClientAreaOrigin());
2743 x += pt.x;
2744 y += pt.y;
2745 }
2746 }
2747
2748 // ----------------------------------------------------------------------------
2749 // Update UI processing
2750 // ----------------------------------------------------------------------------
2751
2752 void wxWindowBase::UpdateWindowUI(long flags)
2753 {
2754 wxUpdateUIEvent event(GetId());
2755 event.SetEventObject(this);
2756
2757 if ( GetEventHandler()->ProcessEvent(event) )
2758 {
2759 DoUpdateWindowUI(event);
2760 }
2761
2762 if (flags & wxUPDATE_UI_RECURSE)
2763 {
2764 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2765 while (node)
2766 {
2767 wxWindow* child = (wxWindow*) node->GetData();
2768 child->UpdateWindowUI(flags);
2769 node = node->GetNext();
2770 }
2771 }
2772 }
2773
2774 // do the window-specific processing after processing the update event
2775 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2776 {
2777 if ( event.GetSetEnabled() )
2778 Enable(event.GetEnabled());
2779
2780 if ( event.GetSetShown() )
2781 Show(event.GetShown());
2782 }
2783
2784 // ----------------------------------------------------------------------------
2785 // Idle processing
2786 // ----------------------------------------------------------------------------
2787
2788 // Send idle event to window and all subwindows
2789 bool wxWindowBase::SendIdleEvents(wxIdleEvent& event)
2790 {
2791 bool needMore = false;
2792
2793 OnInternalIdle();
2794
2795 // should we send idle event to this window?
2796 if (wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
2797 HasExtraStyle(wxWS_EX_PROCESS_IDLE))
2798 {
2799 event.SetEventObject(this);
2800 HandleWindowEvent(event);
2801
2802 if (event.MoreRequested())
2803 needMore = true;
2804 }
2805 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2806 for (; node; node = node->GetNext())
2807 {
2808 wxWindow* child = node->GetData();
2809 if (child->SendIdleEvents(event))
2810 needMore = true;
2811 }
2812
2813 return needMore;
2814 }
2815
2816 void wxWindowBase::OnInternalIdle()
2817 {
2818 if ( wxUpdateUIEvent::CanUpdate(this) )
2819 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2820 }
2821
2822 // ----------------------------------------------------------------------------
2823 // dialog units translations
2824 // ----------------------------------------------------------------------------
2825
2826 // Windows' computes dialog units using average character width over upper-
2827 // and lower-case ASCII alphabet and not using the average character width
2828 // metadata stored in the font; see
2829 // http://support.microsoft.com/default.aspx/kb/145994 for detailed discussion.
2830 // It's important that we perform the conversion in identical way, because
2831 // dialog units natively exist only on Windows and Windows HIG is expressed
2832 // using them.
2833 wxSize wxWindowBase::GetDlgUnitBase() const
2834 {
2835 const wxWindowBase * const parent = wxGetTopLevelParent((wxWindow*)this);
2836
2837 wxCHECK_MSG( parent, wxDefaultSize, wxS("Must have TLW parent") );
2838
2839 if ( !parent->m_font.IsOk() )
2840 {
2841 // Default GUI font is used. This is the most common case, so
2842 // cache the results.
2843 static wxSize s_defFontSize;
2844 if ( s_defFontSize.x == 0 )
2845 s_defFontSize = wxPrivate::GetAverageASCIILetterSize(*parent);
2846 return s_defFontSize;
2847 }
2848 else
2849 {
2850 // Custom font, we always need to compute the result
2851 return wxPrivate::GetAverageASCIILetterSize(*parent);
2852 }
2853 }
2854
2855 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt) const
2856 {
2857 const wxSize base = GetDlgUnitBase();
2858
2859 // NB: wxMulDivInt32() is used, because it correctly rounds the result
2860
2861 wxPoint pt2 = wxDefaultPosition;
2862 if (pt.x != wxDefaultCoord)
2863 pt2.x = wxMulDivInt32(pt.x, 4, base.x);
2864 if (pt.y != wxDefaultCoord)
2865 pt2.y = wxMulDivInt32(pt.y, 8, base.y);
2866
2867 return pt2;
2868 }
2869
2870 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const
2871 {
2872 const wxSize base = GetDlgUnitBase();
2873
2874 wxPoint pt2 = wxDefaultPosition;
2875 if (pt.x != wxDefaultCoord)
2876 pt2.x = wxMulDivInt32(pt.x, base.x, 4);
2877 if (pt.y != wxDefaultCoord)
2878 pt2.y = wxMulDivInt32(pt.y, base.y, 8);
2879
2880 return pt2;
2881 }
2882
2883 // ----------------------------------------------------------------------------
2884 // event handlers
2885 // ----------------------------------------------------------------------------
2886
2887 // propagate the colour change event to the subwindows
2888 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
2889 {
2890 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2891 while ( node )
2892 {
2893 // Only propagate to non-top-level windows
2894 wxWindow *win = node->GetData();
2895 if ( !win->IsTopLevel() )
2896 {
2897 wxSysColourChangedEvent event2;
2898 event2.SetEventObject(win);
2899 win->GetEventHandler()->ProcessEvent(event2);
2900 }
2901
2902 node = node->GetNext();
2903 }
2904
2905 Refresh();
2906 }
2907
2908 // the default action is to populate dialog with data when it's created,
2909 // and nudge the UI into displaying itself correctly in case
2910 // we've turned the wxUpdateUIEvents frequency down low.
2911 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2912 {
2913 TransferDataToWindow();
2914
2915 // Update the UI at this point
2916 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2917 }
2918
2919 // ----------------------------------------------------------------------------
2920 // menu-related functions
2921 // ----------------------------------------------------------------------------
2922
2923 #if wxUSE_MENUS
2924
2925 bool wxWindowBase::PopupMenu(wxMenu *menu, int x, int y)
2926 {
2927 wxCHECK_MSG( menu, false, "can't popup NULL menu" );
2928
2929 wxMenuInvokingWindowSetter
2930 setInvokingWin(*menu, static_cast<wxWindow *>(this));
2931
2932 wxCurrentPopupMenu = menu;
2933 const bool rc = DoPopupMenu(menu, x, y);
2934 wxCurrentPopupMenu = NULL;
2935
2936 return rc;
2937 }
2938
2939 // this is used to pass the id of the selected item from the menu event handler
2940 // to the main function itself
2941 //
2942 // it's ok to use a global here as there can be at most one popup menu shown at
2943 // any time
2944 static int gs_popupMenuSelection = wxID_NONE;
2945
2946 void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event)
2947 {
2948 // store the id in a global variable where we'll retrieve it from later
2949 gs_popupMenuSelection = event.GetId();
2950 }
2951
2952 void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent& WXUNUSED(event))
2953 {
2954 // nothing to do but do not skip it
2955 }
2956
2957 int
2958 wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y)
2959 {
2960 gs_popupMenuSelection = wxID_NONE;
2961
2962 Connect(wxEVT_MENU,
2963 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2964 NULL,
2965 this);
2966
2967 // it is common to construct the menu passed to this function dynamically
2968 // using some fixed range of ids which could clash with the ids used
2969 // elsewhere in the program, which could result in some menu items being
2970 // unintentionally disabled or otherwise modified by update UI handlers
2971 // elsewhere in the program code and this is difficult to avoid in the
2972 // program itself, so instead we just temporarily suspend UI updating while
2973 // this menu is shown
2974 Connect(wxEVT_UPDATE_UI,
2975 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2976 NULL,
2977 this);
2978
2979 PopupMenu(&menu, x, y);
2980
2981 Disconnect(wxEVT_UPDATE_UI,
2982 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2983 NULL,
2984 this);
2985 Disconnect(wxEVT_MENU,
2986 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2987 NULL,
2988 this);
2989
2990 return gs_popupMenuSelection;
2991 }
2992
2993 #endif // wxUSE_MENUS
2994
2995 // methods for drawing the sizers in a visible way: this is currently only
2996 // enabled for "full debug" builds with wxDEBUG_LEVEL==2 as it doesn't work
2997 // that well and also because we don't want to leave it enabled in default
2998 // builds used for production
2999 #if wxDEBUG_LEVEL > 1
3000
3001 static void DrawSizers(wxWindowBase *win);
3002
3003 static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill, const wxPen* pen)
3004 {
3005 wxClientDC dc((wxWindow *)win);
3006 dc.SetPen(*pen);
3007 dc.SetBrush(fill ? wxBrush(pen->GetColour(), wxBRUSHSTYLE_CROSSDIAG_HATCH) :
3008 *wxTRANSPARENT_BRUSH);
3009 dc.DrawRectangle(rect.Deflate(1, 1));
3010 }
3011
3012 static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
3013 {
3014 const wxSizerItemList& items = sizer->GetChildren();
3015 for ( wxSizerItemList::const_iterator i = items.begin(),
3016 end = items.end();
3017 i != end;
3018 ++i )
3019 {
3020 wxSizerItem *item = *i;
3021 if ( item->IsSizer() )
3022 {
3023 DrawBorder(win, item->GetRect().Deflate(2), false, wxRED_PEN);
3024 DrawSizer(win, item->GetSizer());
3025 }
3026 else if ( item->IsSpacer() )
3027 {
3028 DrawBorder(win, item->GetRect().Deflate(2), true, wxBLUE_PEN);
3029 }
3030 else if ( item->IsWindow() )
3031 {
3032 DrawSizers(item->GetWindow());
3033 }
3034 else
3035 wxFAIL_MSG("inconsistent wxSizerItem status!");
3036 }
3037 }
3038
3039 static void DrawSizers(wxWindowBase *win)
3040 {
3041 DrawBorder(win, win->GetClientSize(), false, wxGREEN_PEN);
3042
3043 wxSizer *sizer = win->GetSizer();
3044 if ( sizer )
3045 {
3046 DrawSizer(win, sizer);
3047 }
3048 else // no sizer, still recurse into the children
3049 {
3050 const wxWindowList& children = win->GetChildren();
3051 for ( wxWindowList::const_iterator i = children.begin(),
3052 end = children.end();
3053 i != end;
3054 ++i )
3055 {
3056 DrawSizers(*i);
3057 }
3058
3059 // show all kind of sizes of this window; see the "window sizing" topic
3060 // overview for more info about the various differences:
3061 wxSize fullSz = win->GetSize();
3062 wxSize clientSz = win->GetClientSize();
3063 wxSize bestSz = win->GetBestSize();
3064 wxSize minSz = win->GetMinSize();
3065 wxSize maxSz = win->GetMaxSize();
3066 wxSize virtualSz = win->GetVirtualSize();
3067
3068 wxMessageOutputDebug dbgout;
3069 dbgout.Printf(
3070 "%-10s => fullsz=%4d;%-4d clientsz=%4d;%-4d bestsz=%4d;%-4d minsz=%4d;%-4d maxsz=%4d;%-4d virtualsz=%4d;%-4d\n",
3071 win->GetName(),
3072 fullSz.x, fullSz.y,
3073 clientSz.x, clientSz.y,
3074 bestSz.x, bestSz.y,
3075 minSz.x, minSz.y,
3076 maxSz.x, maxSz.y,
3077 virtualSz.x, virtualSz.y);
3078 }
3079 }
3080
3081 #endif // wxDEBUG_LEVEL
3082
3083 // process special middle clicks
3084 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
3085 {
3086 if ( event.ControlDown() && event.AltDown() )
3087 {
3088 #if wxDEBUG_LEVEL > 1
3089 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
3090 if ( event.ShiftDown() )
3091 {
3092 DrawSizers(this);
3093 }
3094 else
3095 #endif // __WXDEBUG__
3096 {
3097 #if wxUSE_MSGDLG
3098 // just Ctrl-Alt-middle click shows information about wx version
3099 ::wxInfoMessageBox((wxWindow*)this);
3100 #endif // wxUSE_MSGDLG
3101 }
3102 }
3103 else
3104 {
3105 event.Skip();
3106 }
3107 }
3108
3109 // ----------------------------------------------------------------------------
3110 // accessibility
3111 // ----------------------------------------------------------------------------
3112
3113 #if wxUSE_ACCESSIBILITY
3114 void wxWindowBase::SetAccessible(wxAccessible* accessible)
3115 {
3116 if (m_accessible && (accessible != m_accessible))
3117 delete m_accessible;
3118 m_accessible = accessible;
3119 if (m_accessible)
3120 m_accessible->SetWindow((wxWindow*) this);
3121 }
3122
3123 // Returns the accessible object, creating if necessary.
3124 wxAccessible* wxWindowBase::GetOrCreateAccessible()
3125 {
3126 if (!m_accessible)
3127 m_accessible = CreateAccessible();
3128 return m_accessible;
3129 }
3130
3131 // Override to create a specific accessible object.
3132 wxAccessible* wxWindowBase::CreateAccessible()
3133 {
3134 return new wxWindowAccessible((wxWindow*) this);
3135 }
3136
3137 #endif
3138
3139 // ----------------------------------------------------------------------------
3140 // list classes implementation
3141 // ----------------------------------------------------------------------------
3142
3143 #if wxUSE_STD_CONTAINERS
3144
3145 #include "wx/listimpl.cpp"
3146 WX_DEFINE_LIST(wxWindowList)
3147
3148 #else // !wxUSE_STD_CONTAINERS
3149
3150 void wxWindowListNode::DeleteData()
3151 {
3152 delete (wxWindow *)GetData();
3153 }
3154
3155 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
3156
3157 // ----------------------------------------------------------------------------
3158 // borders
3159 // ----------------------------------------------------------------------------
3160
3161 wxBorder wxWindowBase::GetBorder(long flags) const
3162 {
3163 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
3164 if ( border == wxBORDER_DEFAULT )
3165 {
3166 border = GetDefaultBorder();
3167 }
3168 else if ( border == wxBORDER_THEME )
3169 {
3170 border = GetDefaultBorderForControl();
3171 }
3172
3173 return border;
3174 }
3175
3176 wxBorder wxWindowBase::GetDefaultBorder() const
3177 {
3178 return wxBORDER_NONE;
3179 }
3180
3181 // ----------------------------------------------------------------------------
3182 // hit testing
3183 // ----------------------------------------------------------------------------
3184
3185 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
3186 {
3187 // here we just check if the point is inside the window or not
3188
3189 // check the top and left border first
3190 bool outside = x < 0 || y < 0;
3191 if ( !outside )
3192 {
3193 // check the right and bottom borders too
3194 wxSize size = GetSize();
3195 outside = x >= size.x || y >= size.y;
3196 }
3197
3198 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
3199 }
3200
3201 // ----------------------------------------------------------------------------
3202 // mouse capture
3203 // ----------------------------------------------------------------------------
3204
3205 struct WXDLLEXPORT wxWindowNext
3206 {
3207 wxWindow *win;
3208 wxWindowNext *next;
3209 } *wxWindowBase::ms_winCaptureNext = NULL;
3210 wxWindow *wxWindowBase::ms_winCaptureCurrent = NULL;
3211 bool wxWindowBase::ms_winCaptureChanging = false;
3212
3213 void wxWindowBase::CaptureMouse()
3214 {
3215 wxLogTrace(wxT("mousecapture"), wxT("CaptureMouse(%p)"), static_cast<void*>(this));
3216
3217 wxASSERT_MSG( !ms_winCaptureChanging, wxT("recursive CaptureMouse call?") );
3218
3219 ms_winCaptureChanging = true;
3220
3221 wxWindow *winOld = GetCapture();
3222 if ( winOld )
3223 {
3224 ((wxWindowBase*) winOld)->DoReleaseMouse();
3225
3226 // save it on stack
3227 wxWindowNext *item = new wxWindowNext;
3228 item->win = winOld;
3229 item->next = ms_winCaptureNext;
3230 ms_winCaptureNext = item;
3231 }
3232 //else: no mouse capture to save
3233
3234 DoCaptureMouse();
3235 ms_winCaptureCurrent = (wxWindow*)this;
3236
3237 ms_winCaptureChanging = false;
3238 }
3239
3240 void wxWindowBase::ReleaseMouse()
3241 {
3242 wxLogTrace(wxT("mousecapture"), wxT("ReleaseMouse(%p)"), static_cast<void*>(this));
3243
3244 wxASSERT_MSG( !ms_winCaptureChanging, wxT("recursive ReleaseMouse call?") );
3245
3246 wxASSERT_MSG( GetCapture() == this,
3247 "attempt to release mouse, but this window hasn't captured it" );
3248 wxASSERT_MSG( ms_winCaptureCurrent == this,
3249 "attempt to release mouse, but this window hasn't captured it" );
3250
3251 ms_winCaptureChanging = true;
3252
3253 DoReleaseMouse();
3254 ms_winCaptureCurrent = NULL;
3255
3256 if ( ms_winCaptureNext )
3257 {
3258 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
3259 ms_winCaptureCurrent = ms_winCaptureNext->win;
3260
3261 wxWindowNext *item = ms_winCaptureNext;
3262 ms_winCaptureNext = item->next;
3263 delete item;
3264 }
3265 //else: stack is empty, no previous capture
3266
3267 ms_winCaptureChanging = false;
3268
3269 wxLogTrace(wxT("mousecapture"),
3270 (const wxChar *) wxT("After ReleaseMouse() mouse is captured by %p"),
3271 static_cast<void*>(GetCapture()));
3272 }
3273
3274 static void DoNotifyWindowAboutCaptureLost(wxWindow *win)
3275 {
3276 wxMouseCaptureLostEvent event(win->GetId());
3277 event.SetEventObject(win);
3278 if ( !win->GetEventHandler()->ProcessEvent(event) )
3279 {
3280 // windows must handle this event, otherwise the app wouldn't behave
3281 // correctly if it loses capture unexpectedly; see the discussion here:
3282 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
3283 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
3284 wxFAIL_MSG( wxT("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
3285 }
3286 }
3287
3288 /* static */
3289 void wxWindowBase::NotifyCaptureLost()
3290 {
3291 // don't do anything if capture lost was expected, i.e. resulted from
3292 // a wx call to ReleaseMouse or CaptureMouse:
3293 if ( ms_winCaptureChanging )
3294 return;
3295
3296 // if the capture was lost unexpectedly, notify every window that has
3297 // capture (on stack or current) about it and clear the stack:
3298
3299 if ( ms_winCaptureCurrent )
3300 {
3301 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent);
3302 ms_winCaptureCurrent = NULL;
3303 }
3304
3305 while ( ms_winCaptureNext )
3306 {
3307 wxWindowNext *item = ms_winCaptureNext;
3308 ms_winCaptureNext = item->next;
3309
3310 DoNotifyWindowAboutCaptureLost(item->win);
3311
3312 delete item;
3313 }
3314 }
3315
3316 #if wxUSE_HOTKEY
3317
3318 bool
3319 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
3320 int WXUNUSED(modifiers),
3321 int WXUNUSED(keycode))
3322 {
3323 // not implemented
3324 return false;
3325 }
3326
3327 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
3328 {
3329 // not implemented
3330 return false;
3331 }
3332
3333 #endif // wxUSE_HOTKEY
3334
3335 // ----------------------------------------------------------------------------
3336 // event processing
3337 // ----------------------------------------------------------------------------
3338
3339 bool wxWindowBase::TryBefore(wxEvent& event)
3340 {
3341 #if wxUSE_VALIDATORS
3342 // Can only use the validator of the window which
3343 // is receiving the event
3344 if ( event.GetEventObject() == this )
3345 {
3346 wxValidator * const validator = GetValidator();
3347 if ( validator && validator->ProcessEventLocally(event) )
3348 {
3349 return true;
3350 }
3351 }
3352 #endif // wxUSE_VALIDATORS
3353
3354 return wxEvtHandler::TryBefore(event);
3355 }
3356
3357 bool wxWindowBase::TryAfter(wxEvent& event)
3358 {
3359 // carry on up the parent-child hierarchy if the propagation count hasn't
3360 // reached zero yet
3361 if ( event.ShouldPropagate() )
3362 {
3363 // honour the requests to stop propagation at this window: this is
3364 // used by the dialogs, for example, to prevent processing the events
3365 // from the dialog controls in the parent frame which rarely, if ever,
3366 // makes sense
3367 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
3368 {
3369 wxWindow *parent = GetParent();
3370 if ( parent && !parent->IsBeingDeleted() )
3371 {
3372 wxPropagateOnce propagateOnce(event, this);
3373
3374 return parent->GetEventHandler()->ProcessEvent(event);
3375 }
3376 }
3377 }
3378
3379 return wxEvtHandler::TryAfter(event);
3380 }
3381
3382 // ----------------------------------------------------------------------------
3383 // window relationships
3384 // ----------------------------------------------------------------------------
3385
3386 wxWindow *wxWindowBase::DoGetSibling(WindowOrder order) const
3387 {
3388 wxCHECK_MSG( GetParent(), NULL,
3389 wxT("GetPrev/NextSibling() don't work for TLWs!") );
3390
3391 wxWindowList& siblings = GetParent()->GetChildren();
3392 wxWindowList::compatibility_iterator i = siblings.Find((wxWindow *)this);
3393 wxCHECK_MSG( i, NULL, wxT("window not a child of its parent?") );
3394
3395 if ( order == OrderBefore )
3396 i = i->GetPrevious();
3397 else // OrderAfter
3398 i = i->GetNext();
3399
3400 return i ? i->GetData() : NULL;
3401 }
3402
3403 // ----------------------------------------------------------------------------
3404 // keyboard navigation
3405 // ----------------------------------------------------------------------------
3406
3407 // Navigates in the specified direction inside this window
3408 bool wxWindowBase::DoNavigateIn(int flags)
3409 {
3410 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
3411 // native code doesn't process our wxNavigationKeyEvents anyhow
3412 wxUnusedVar(flags);
3413 return false;
3414 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
3415 wxNavigationKeyEvent eventNav;
3416 wxWindow *focused = FindFocus();
3417 eventNav.SetCurrentFocus(focused);
3418 eventNav.SetEventObject(focused);
3419 eventNav.SetFlags(flags);
3420 return GetEventHandler()->ProcessEvent(eventNav);
3421 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
3422 }
3423
3424 bool wxWindowBase::HandleAsNavigationKey(const wxKeyEvent& event)
3425 {
3426 if ( event.GetKeyCode() != WXK_TAB )
3427 return false;
3428
3429 int flags = wxNavigationKeyEvent::FromTab;
3430
3431 if ( event.ShiftDown() )
3432 flags |= wxNavigationKeyEvent::IsBackward;
3433 else
3434 flags |= wxNavigationKeyEvent::IsForward;
3435
3436 if ( event.ControlDown() )
3437 flags |= wxNavigationKeyEvent::WinChange;
3438
3439 Navigate(flags);
3440 return true;
3441 }
3442
3443 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, WindowOrder move)
3444 {
3445 // check that we're not a top level window
3446 wxCHECK_RET( GetParent(),
3447 wxT("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
3448
3449 // detect the special case when we have nothing to do anyhow and when the
3450 // code below wouldn't work
3451 if ( win == this )
3452 return;
3453
3454 // find the target window in the siblings list
3455 wxWindowList& siblings = GetParent()->GetChildren();
3456 wxWindowList::compatibility_iterator i = siblings.Find(win);
3457 wxCHECK_RET( i, wxT("MoveBefore/AfterInTabOrder(): win is not a sibling") );
3458
3459 // unfortunately, when wxUSE_STD_CONTAINERS == 1 DetachNode() is not
3460 // implemented so we can't just move the node around
3461 wxWindow *self = (wxWindow *)this;
3462 siblings.DeleteObject(self);
3463 if ( move == OrderAfter )
3464 {
3465 i = i->GetNext();
3466 }
3467
3468 if ( i )
3469 {
3470 siblings.Insert(i, self);
3471 }
3472 else // OrderAfter and win was the last sibling
3473 {
3474 siblings.Append(self);
3475 }
3476 }
3477
3478 // ----------------------------------------------------------------------------
3479 // focus handling
3480 // ----------------------------------------------------------------------------
3481
3482 /*static*/ wxWindow* wxWindowBase::FindFocus()
3483 {
3484 wxWindowBase *win = DoFindFocus();
3485 return win ? win->GetMainWindowOfCompositeControl() : NULL;
3486 }
3487
3488 bool wxWindowBase::HasFocus() const
3489 {
3490 wxWindowBase* const win = DoFindFocus();
3491 return win &&
3492 (this == win || this == win->GetMainWindowOfCompositeControl());
3493 }
3494
3495 // ----------------------------------------------------------------------------
3496 // drag and drop
3497 // ----------------------------------------------------------------------------
3498
3499 #if wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3500
3501 namespace
3502 {
3503
3504 class DragAcceptFilesTarget : public wxFileDropTarget
3505 {
3506 public:
3507 DragAcceptFilesTarget(wxWindowBase *win) : m_win(win) {}
3508
3509 virtual bool OnDropFiles(wxCoord x, wxCoord y,
3510 const wxArrayString& filenames)
3511 {
3512 wxDropFilesEvent event(wxEVT_DROP_FILES,
3513 filenames.size(),
3514 wxCArrayString(filenames).Release());
3515 event.SetEventObject(m_win);
3516 event.m_pos.x = x;
3517 event.m_pos.y = y;
3518
3519 return m_win->HandleWindowEvent(event);
3520 }
3521
3522 private:
3523 wxWindowBase * const m_win;
3524
3525 wxDECLARE_NO_COPY_CLASS(DragAcceptFilesTarget);
3526 };
3527
3528
3529 } // anonymous namespace
3530
3531 // Generic version of DragAcceptFiles(). It works by installing a simple
3532 // wxFileDropTarget-to-EVT_DROP_FILES adaptor and therefore cannot be used
3533 // together with explicit SetDropTarget() calls.
3534 void wxWindowBase::DragAcceptFiles(bool accept)
3535 {
3536 if ( accept )
3537 {
3538 wxASSERT_MSG( !GetDropTarget(),
3539 "cannot use DragAcceptFiles() and SetDropTarget() together" );
3540 SetDropTarget(new DragAcceptFilesTarget(this));
3541 }
3542 else
3543 {
3544 SetDropTarget(NULL);
3545 }
3546 }
3547
3548 #endif // wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3549
3550 // ----------------------------------------------------------------------------
3551 // global functions
3552 // ----------------------------------------------------------------------------
3553
3554 wxWindow* wxGetTopLevelParent(wxWindow *win)
3555 {
3556 while ( win && !win->IsTopLevel() )
3557 win = win->GetParent();
3558
3559 return win;
3560 }
3561
3562 #if wxUSE_ACCESSIBILITY
3563 // ----------------------------------------------------------------------------
3564 // accessible object for windows
3565 // ----------------------------------------------------------------------------
3566
3567 // Can return either a child object, or an integer
3568 // representing the child element, starting from 1.
3569 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
3570 {
3571 wxASSERT( GetWindow() != NULL );
3572 if (!GetWindow())
3573 return wxACC_FAIL;
3574
3575 return wxACC_NOT_IMPLEMENTED;
3576 }
3577
3578 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
3579 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
3580 {
3581 wxASSERT( GetWindow() != NULL );
3582 if (!GetWindow())
3583 return wxACC_FAIL;
3584
3585 wxWindow* win = NULL;
3586 if (elementId == 0)
3587 {
3588 win = GetWindow();
3589 }
3590 else
3591 {
3592 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
3593 {
3594 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
3595 }
3596 else
3597 return wxACC_FAIL;
3598 }
3599 if (win)
3600 {
3601 rect = win->GetRect();
3602 if (win->GetParent() && !wxDynamicCast(win, wxTopLevelWindow))
3603 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
3604 return wxACC_OK;
3605 }
3606
3607 return wxACC_NOT_IMPLEMENTED;
3608 }
3609
3610 // Navigates from fromId to toId/toObject.
3611 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
3612 int* WXUNUSED(toId), wxAccessible** toObject)
3613 {
3614 wxASSERT( GetWindow() != NULL );
3615 if (!GetWindow())
3616 return wxACC_FAIL;
3617
3618 switch (navDir)
3619 {
3620 case wxNAVDIR_FIRSTCHILD:
3621 {
3622 if (GetWindow()->GetChildren().GetCount() == 0)
3623 return wxACC_FALSE;
3624 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
3625 *toObject = childWindow->GetOrCreateAccessible();
3626
3627 return wxACC_OK;
3628 }
3629 case wxNAVDIR_LASTCHILD:
3630 {
3631 if (GetWindow()->GetChildren().GetCount() == 0)
3632 return wxACC_FALSE;
3633 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
3634 *toObject = childWindow->GetOrCreateAccessible();
3635
3636 return wxACC_OK;
3637 }
3638 case wxNAVDIR_RIGHT:
3639 case wxNAVDIR_DOWN:
3640 case wxNAVDIR_NEXT:
3641 {
3642 wxWindowList::compatibility_iterator node =
3643 wxWindowList::compatibility_iterator();
3644 if (fromId == 0)
3645 {
3646 // Can't navigate to sibling of this window
3647 // if we're a top-level window.
3648 if (!GetWindow()->GetParent())
3649 return wxACC_NOT_IMPLEMENTED;
3650
3651 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3652 }
3653 else
3654 {
3655 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
3656 node = GetWindow()->GetChildren().Item(fromId-1);
3657 }
3658
3659 if (node && node->GetNext())
3660 {
3661 wxWindow* nextWindow = node->GetNext()->GetData();
3662 *toObject = nextWindow->GetOrCreateAccessible();
3663 return wxACC_OK;
3664 }
3665 else
3666 return wxACC_FALSE;
3667 }
3668 case wxNAVDIR_LEFT:
3669 case wxNAVDIR_UP:
3670 case wxNAVDIR_PREVIOUS:
3671 {
3672 wxWindowList::compatibility_iterator node =
3673 wxWindowList::compatibility_iterator();
3674 if (fromId == 0)
3675 {
3676 // Can't navigate to sibling of this window
3677 // if we're a top-level window.
3678 if (!GetWindow()->GetParent())
3679 return wxACC_NOT_IMPLEMENTED;
3680
3681 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3682 }
3683 else
3684 {
3685 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
3686 node = GetWindow()->GetChildren().Item(fromId-1);
3687 }
3688
3689 if (node && node->GetPrevious())
3690 {
3691 wxWindow* previousWindow = node->GetPrevious()->GetData();
3692 *toObject = previousWindow->GetOrCreateAccessible();
3693 return wxACC_OK;
3694 }
3695 else
3696 return wxACC_FALSE;
3697 }
3698 }
3699
3700 return wxACC_NOT_IMPLEMENTED;
3701 }
3702
3703 // Gets the name of the specified object.
3704 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
3705 {
3706 wxASSERT( GetWindow() != NULL );
3707 if (!GetWindow())
3708 return wxACC_FAIL;
3709
3710 wxString title;
3711
3712 // If a child, leave wxWidgets to call the function on the actual
3713 // child object.
3714 if (childId > 0)
3715 return wxACC_NOT_IMPLEMENTED;
3716
3717 // This will eventually be replaced by specialised
3718 // accessible classes, one for each kind of wxWidgets
3719 // control or window.
3720 #if wxUSE_BUTTON
3721 if (wxDynamicCast(GetWindow(), wxButton))
3722 title = ((wxButton*) GetWindow())->GetLabel();
3723 else
3724 #endif
3725 title = GetWindow()->GetName();
3726
3727 if (!title.empty())
3728 {
3729 *name = title;
3730 return wxACC_OK;
3731 }
3732 else
3733 return wxACC_NOT_IMPLEMENTED;
3734 }
3735
3736 // Gets the number of children.
3737 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
3738 {
3739 wxASSERT( GetWindow() != NULL );
3740 if (!GetWindow())
3741 return wxACC_FAIL;
3742
3743 *childId = (int) GetWindow()->GetChildren().GetCount();
3744 return wxACC_OK;
3745 }
3746
3747 // Gets the specified child (starting from 1).
3748 // If *child is NULL and return value is wxACC_OK,
3749 // this means that the child is a simple element and
3750 // not an accessible object.
3751 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
3752 {
3753 wxASSERT( GetWindow() != NULL );
3754 if (!GetWindow())
3755 return wxACC_FAIL;
3756
3757 if (childId == 0)
3758 {
3759 *child = this;
3760 return wxACC_OK;
3761 }
3762
3763 if (childId > (int) GetWindow()->GetChildren().GetCount())
3764 return wxACC_FAIL;
3765
3766 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
3767 *child = childWindow->GetOrCreateAccessible();
3768 if (*child)
3769 return wxACC_OK;
3770 else
3771 return wxACC_FAIL;
3772 }
3773
3774 // Gets the parent, or NULL.
3775 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
3776 {
3777 wxASSERT( GetWindow() != NULL );
3778 if (!GetWindow())
3779 return wxACC_FAIL;
3780
3781 wxWindow* parentWindow = GetWindow()->GetParent();
3782 if (!parentWindow)
3783 {
3784 *parent = NULL;
3785 return wxACC_OK;
3786 }
3787 else
3788 {
3789 *parent = parentWindow->GetOrCreateAccessible();
3790 if (*parent)
3791 return wxACC_OK;
3792 else
3793 return wxACC_FAIL;
3794 }
3795 }
3796
3797 // Performs the default action. childId is 0 (the action for this object)
3798 // or > 0 (the action for a child).
3799 // Return wxACC_NOT_SUPPORTED if there is no default action for this
3800 // window (e.g. an edit control).
3801 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
3802 {
3803 wxASSERT( GetWindow() != NULL );
3804 if (!GetWindow())
3805 return wxACC_FAIL;
3806
3807 return wxACC_NOT_IMPLEMENTED;
3808 }
3809
3810 // Gets the default action for this object (0) or > 0 (the action for a child).
3811 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
3812 // string if there is no action.
3813 // The retrieved string describes the action that is performed on an object,
3814 // not what the object does as a result. For example, a toolbar button that prints
3815 // a document has a default action of "Press" rather than "Prints the current document."
3816 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
3817 {
3818 wxASSERT( GetWindow() != NULL );
3819 if (!GetWindow())
3820 return wxACC_FAIL;
3821
3822 return wxACC_NOT_IMPLEMENTED;
3823 }
3824
3825 // Returns the description for this object or a child.
3826 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
3827 {
3828 wxASSERT( GetWindow() != NULL );
3829 if (!GetWindow())
3830 return wxACC_FAIL;
3831
3832 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3833 if (!ht.empty())
3834 {
3835 *description = ht;
3836 return wxACC_OK;
3837 }
3838 return wxACC_NOT_IMPLEMENTED;
3839 }
3840
3841 // Returns help text for this object or a child, similar to tooltip text.
3842 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
3843 {
3844 wxASSERT( GetWindow() != NULL );
3845 if (!GetWindow())
3846 return wxACC_FAIL;
3847
3848 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
3849 if (!ht.empty())
3850 {
3851 *helpText = ht;
3852 return wxACC_OK;
3853 }
3854 return wxACC_NOT_IMPLEMENTED;
3855 }
3856
3857 // Returns the keyboard shortcut for this object or child.
3858 // Return e.g. ALT+K
3859 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
3860 {
3861 wxASSERT( GetWindow() != NULL );
3862 if (!GetWindow())
3863 return wxACC_FAIL;
3864
3865 return wxACC_NOT_IMPLEMENTED;
3866 }
3867
3868 // Returns a role constant.
3869 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
3870 {
3871 wxASSERT( GetWindow() != NULL );
3872 if (!GetWindow())
3873 return wxACC_FAIL;
3874
3875 // If a child, leave wxWidgets to call the function on the actual
3876 // child object.
3877 if (childId > 0)
3878 return wxACC_NOT_IMPLEMENTED;
3879
3880 if (wxDynamicCast(GetWindow(), wxControl))
3881 return wxACC_NOT_IMPLEMENTED;
3882 #if wxUSE_STATUSBAR
3883 if (wxDynamicCast(GetWindow(), wxStatusBar))
3884 return wxACC_NOT_IMPLEMENTED;
3885 #endif
3886 #if wxUSE_TOOLBAR
3887 if (wxDynamicCast(GetWindow(), wxToolBar))
3888 return wxACC_NOT_IMPLEMENTED;
3889 #endif
3890
3891 //*role = wxROLE_SYSTEM_CLIENT;
3892 *role = wxROLE_SYSTEM_CLIENT;
3893 return wxACC_OK;
3894
3895 #if 0
3896 return wxACC_NOT_IMPLEMENTED;
3897 #endif
3898 }
3899
3900 // Returns a state constant.
3901 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
3902 {
3903 wxASSERT( GetWindow() != NULL );
3904 if (!GetWindow())
3905 return wxACC_FAIL;
3906
3907 // If a child, leave wxWidgets to call the function on the actual
3908 // child object.
3909 if (childId > 0)
3910 return wxACC_NOT_IMPLEMENTED;
3911
3912 if (wxDynamicCast(GetWindow(), wxControl))
3913 return wxACC_NOT_IMPLEMENTED;
3914
3915 #if wxUSE_STATUSBAR
3916 if (wxDynamicCast(GetWindow(), wxStatusBar))
3917 return wxACC_NOT_IMPLEMENTED;
3918 #endif
3919 #if wxUSE_TOOLBAR
3920 if (wxDynamicCast(GetWindow(), wxToolBar))
3921 return wxACC_NOT_IMPLEMENTED;
3922 #endif
3923
3924 *state = 0;
3925 return wxACC_OK;
3926
3927 #if 0
3928 return wxACC_NOT_IMPLEMENTED;
3929 #endif
3930 }
3931
3932 // Returns a localized string representing the value for the object
3933 // or child.
3934 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
3935 {
3936 wxASSERT( GetWindow() != NULL );
3937 if (!GetWindow())
3938 return wxACC_FAIL;
3939
3940 return wxACC_NOT_IMPLEMENTED;
3941 }
3942
3943 // Selects the object or child.
3944 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
3945 {
3946 wxASSERT( GetWindow() != NULL );
3947 if (!GetWindow())
3948 return wxACC_FAIL;
3949
3950 return wxACC_NOT_IMPLEMENTED;
3951 }
3952
3953 // Gets the window with the keyboard focus.
3954 // If childId is 0 and child is NULL, no object in
3955 // this subhierarchy has the focus.
3956 // If this object has the focus, child should be 'this'.
3957 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
3958 {
3959 wxASSERT( GetWindow() != NULL );
3960 if (!GetWindow())
3961 return wxACC_FAIL;
3962
3963 return wxACC_NOT_IMPLEMENTED;
3964 }
3965
3966 #if wxUSE_VARIANT
3967 // Gets a variant representing the selected children
3968 // of this object.
3969 // Acceptable values:
3970 // - a null variant (IsNull() returns true)
3971 // - a list variant (GetType() == wxT("list")
3972 // - an integer representing the selected child element,
3973 // or 0 if this object is selected (GetType() == wxT("long")
3974 // - a "void*" pointer to a wxAccessible child object
3975 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
3976 {
3977 wxASSERT( GetWindow() != NULL );
3978 if (!GetWindow())
3979 return wxACC_FAIL;
3980
3981 return wxACC_NOT_IMPLEMENTED;
3982 }
3983 #endif // wxUSE_VARIANT
3984
3985 #endif // wxUSE_ACCESSIBILITY
3986
3987 // ----------------------------------------------------------------------------
3988 // RTL support
3989 // ----------------------------------------------------------------------------
3990
3991 wxCoord
3992 wxWindowBase::AdjustForLayoutDirection(wxCoord x,
3993 wxCoord width,
3994 wxCoord widthTotal) const
3995 {
3996 if ( GetLayoutDirection() == wxLayout_RightToLeft )
3997 {
3998 x = widthTotal - x - width;
3999 }
4000
4001 return x;
4002 }
4003
4004