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