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