]> git.saurik.com Git - wxWidgets.git/blame - src/univ/winuniv.cpp
relinquish the mouse capture when a dialog is about to be made modal to ensure that...
[wxWidgets.git] / src / univ / winuniv.cpp
CommitLineData
1e6feb95 1///////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/univ/window.cpp
1e6feb95
VZ
3// Purpose: implementation of extra wxWindow methods for wxUniv port
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.08.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
1e6feb95
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
e4db172a
WS
27#include "wx/window.h"
28
1e6feb95
VZ
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
1e6feb95
VZ
31 #include "wx/dcclient.h"
32 #include "wx/dcmemory.h"
33 #include "wx/event.h"
34 #include "wx/scrolbar.h"
35 #include "wx/menu.h"
8cb172b4 36 #include "wx/frame.h"
e4db172a 37 #include "wx/log.h"
1e6feb95
VZ
38#endif // WX_PRECOMP
39
40#include "wx/univ/colschem.h"
41#include "wx/univ/renderer.h"
42#include "wx/univ/theme.h"
43
44#if wxUSE_CARET
45 #include "wx/caret.h"
46#endif // wxUSE_CARET
47
48// turn Refresh() debugging on/off
49#define WXDEBUG_REFRESH
50
51#ifndef __WXDEBUG__
52 #undef WXDEBUG_REFRESH
53#endif
54
cff7ef89
VS
55#if defined(WXDEBUG_REFRESH) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
56#include "wx/msw/private.h"
57#endif
58
1e6feb95
VZ
59// ============================================================================
60// implementation
61// ============================================================================
62
c04c7a3d
VS
63// ----------------------------------------------------------------------------
64// scrollbars class
65// ----------------------------------------------------------------------------
66
67// This is scrollbar class used to implement wxWindow's "built-in" scrollbars;
68// unlike the standard wxScrollBar class, this one is positioned outside of its
69// parent's client area
70class wxWindowScrollBar : public wxScrollBar
71{
72public:
73 wxWindowScrollBar(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos = wxDefaultPosition,
76 const wxSize& size = wxDefaultSize,
77 long style = wxSB_HORIZONTAL)
78 : wxScrollBar(parent, id, pos, size, style)
79 {
80 }
81
82 virtual bool CanBeOutsideClientArea() const { return true; }
83};
84
85
1e6feb95
VZ
86// ----------------------------------------------------------------------------
87// event tables
88// ----------------------------------------------------------------------------
89
90// we can't use wxWindowNative here as it won't be expanded inside the macro
91#if defined(__WXMSW__)
92 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMSW)
93#elif defined(__WXGTK__)
94 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowGTK)
95#elif defined(__WXMGL__)
96 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMGL)
b3c86150
VS
97#elif defined(__WXDFB__)
98 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowDFB)
8354aa92
RR
99#elif defined(__WXX11__)
100 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowX11)
19193a2c
KB
101#elif defined(__WXPM__)
102 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowOS2)
1e6feb95
VZ
103#endif
104
105BEGIN_EVENT_TABLE(wxWindow, wxWindowNative)
106 EVT_SIZE(wxWindow::OnSize)
107
108#if wxUSE_ACCEL || wxUSE_MENUS
109 EVT_KEY_DOWN(wxWindow::OnKeyDown)
110#endif // wxUSE_ACCEL
111
112#if wxUSE_MENUS
113 EVT_CHAR(wxWindow::OnChar)
114 EVT_KEY_UP(wxWindow::OnKeyUp)
115#endif // wxUSE_MENUS
116
117 EVT_PAINT(wxWindow::OnPaint)
118 EVT_NC_PAINT(wxWindow::OnNcPaint)
119 EVT_ERASE_BACKGROUND(wxWindow::OnErase)
120END_EVENT_TABLE()
121
122// ----------------------------------------------------------------------------
123// creation
124// ----------------------------------------------------------------------------
125
126void wxWindow::Init()
127{
9a6384ca 128#if wxUSE_SCROLLBAR
1e6feb95
VZ
129 m_scrollbarVert =
130 m_scrollbarHorz = (wxScrollBar *)NULL;
9a6384ca 131#endif // wxUSE_SCROLLBAR
1e6feb95 132
a290fa5a 133 m_isCurrent = false;
1e6feb95
VZ
134
135 m_renderer = wxTheme::Get()->GetRenderer();
2b5f62a0 136
a290fa5a
WS
137 m_oldSize.x = wxDefaultCoord;
138 m_oldSize.y = wxDefaultCoord;
1e6feb95
VZ
139}
140
141bool wxWindow::Create(wxWindow *parent,
142 wxWindowID id,
143 const wxPoint& pos,
144 const wxSize& size,
145 long style,
146 const wxString& name)
147{
588cf92f 148 long actualStyle = style;
8da1f9a9 149
54e90b2a
VS
150 // we add wxCLIP_CHILDREN to get the same ("natural") behaviour under MSW
151 // as under the other platforms
152 actualStyle |= wxCLIP_CHILDREN;
153
588cf92f
JS
154 actualStyle &= ~wxVSCROLL;
155 actualStyle &= ~wxHSCROLL;
54e90b2a 156
d743254a 157#ifdef __WXMSW__
54e90b2a
VS
158 // without this, borders (non-client areas in general) are not repainted
159 // correctly when resizing; apparently, native NC areas are fully repainted
160 // even without this style by MSW, but wxUniv implements client area
161 // itself, so it doesn't work correctly for us
162 //
163 // FIXME: this is very expensive, we need to fix the (commented-out) code
164 // in OnSize() instead
165 actualStyle |= wxFULL_REPAINT_ON_RESIZE;
8da1f9a9
VZ
166#endif
167
54e90b2a 168 if ( !wxWindowNative::Create(parent, id, pos, size, actualStyle, name) )
a290fa5a 169 return false;
1e6feb95 170
588cf92f
JS
171 // Set full style again, including those we didn't want present
172 // when calling the base window Create().
173 wxWindowBase::SetWindowStyleFlag(style);
174
1cb853a8
JS
175 // if we allow or should always have a vertical scrollbar, make it
176 if ( style & wxVSCROLL || style & wxALWAYS_SHOW_SB )
1e6feb95 177 {
ab6b6b15 178#if wxUSE_TWO_WINDOWS
a290fa5a 179 SetInsertIntoMain( true );
ab6b6b15 180#endif
9a6384ca 181#if wxUSE_SCROLLBAR
c04c7a3d
VS
182 m_scrollbarVert = new wxWindowScrollBar(this, wxID_ANY,
183 wxDefaultPosition, wxDefaultSize,
184 wxSB_VERTICAL);
9a6384ca 185#endif // wxUSE_SCROLLBAR
ab6b6b15 186#if wxUSE_TWO_WINDOWS
a290fa5a 187 SetInsertIntoMain( false );
ab6b6b15 188#endif
588cf92f 189 }
1e6feb95 190
1cb853a8 191 // if we should allow a horizontal scrollbar, make it
588cf92f
JS
192 if ( style & wxHSCROLL )
193 {
194#if wxUSE_TWO_WINDOWS
a290fa5a 195 SetInsertIntoMain( true );
588cf92f 196#endif
9a6384ca 197#if wxUSE_SCROLLBAR
c04c7a3d
VS
198 m_scrollbarHorz = new wxWindowScrollBar(this, wxID_ANY,
199 wxDefaultPosition, wxDefaultSize,
200 wxSB_HORIZONTAL);
9a6384ca 201#endif // wxUSE_SCROLLBAR
588cf92f 202#if wxUSE_TWO_WINDOWS
a290fa5a 203 SetInsertIntoMain( false );
588cf92f
JS
204#endif
205 }
8da1f9a9 206
9a6384ca 207#if wxUSE_SCROLLBAR
588cf92f
JS
208 if (m_scrollbarHorz || m_scrollbarVert)
209 {
210 // position it/them
1e6feb95
VZ
211 PositionScrollbars();
212 }
9a6384ca 213#endif // wxUSE_SCROLLBAR
1e6feb95 214
a290fa5a 215 return true;
1e6feb95
VZ
216}
217
8da1f9a9
VZ
218wxWindow::~wxWindow()
219{
220 m_isBeingDeleted = true;
221
478db4fc
VS
222#if wxUSE_SCROLLBAR
223 // clear pointers to scrollbar before deleting the children: they are
224 // children and so will be deleted by DestroyChildren() call below and if
225 // any code using the scrollbars would be called in the process or from
226 // ~wxWindowBase, the app would crash:
227 m_scrollbarVert = m_scrollbarHorz = NULL;
228#endif
229
8da1f9a9
VZ
230 // we have to destroy our children before we're destroyed because our
231 // children suppose that we're of type wxWindow, not just wxWindowNative,
232 // and so bad things may happen if they're deleted from the base class dtor
233 // as by then we're not a wxWindow any longer and wxUniv-specific virtual
234 // functions can't be called
235 DestroyChildren();
236}
237
1e6feb95
VZ
238// ----------------------------------------------------------------------------
239// background pixmap
240// ----------------------------------------------------------------------------
241
242void wxWindow::SetBackground(const wxBitmap& bitmap,
243 int alignment,
244 wxStretch stretch)
245{
246 m_bitmapBg = bitmap;
247 m_alignBgBitmap = alignment;
248 m_stretchBgBitmap = stretch;
249}
250
251const wxBitmap& wxWindow::GetBackgroundBitmap(int *alignment,
252 wxStretch *stretch) const
253{
254 if ( m_bitmapBg.Ok() )
255 {
256 if ( alignment )
257 *alignment = m_alignBgBitmap;
258 if ( stretch )
259 *stretch = m_stretchBgBitmap;
260 }
261
262 return m_bitmapBg;
263}
264
265// ----------------------------------------------------------------------------
266// painting
267// ----------------------------------------------------------------------------
268
1e6feb95 269// the event handlers executed when the window must be repainted
3795cdba 270void wxWindow::OnNcPaint(wxNcPaintEvent& WXUNUSED(event))
1e6feb95
VZ
271{
272 if ( m_renderer )
273 {
274 // get the window rect
9a6384ca 275 wxRect rect(GetSize());
1e6feb95 276
9a6384ca 277#if wxUSE_SCROLLBAR
1e6feb95
VZ
278 // if the scrollbars are outside the border, we must adjust the rect to
279 // exclude them
280 if ( !m_renderer->AreScrollbarsInsideBorder() )
281 {
282 wxScrollBar *scrollbar = GetScrollbar(wxVERTICAL);
283 if ( scrollbar )
284 rect.width -= scrollbar->GetSize().x;
285
286 scrollbar = GetScrollbar(wxHORIZONTAL);
287 if ( scrollbar )
288 rect.height -= scrollbar->GetSize().y;
289 }
9a6384ca 290#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
291
292 // get the DC and draw the border on it
293 wxWindowDC dc(this);
294 DoDrawBorder(dc, rect);
295 }
296}
297
298void wxWindow::OnPaint(wxPaintEvent& event)
299{
300 if ( !m_renderer )
301 {
302 // it is a native control which paints itself
303 event.Skip();
304 }
305 else
306 {
307 // get the DC to use and create renderer on it
308 wxPaintDC dc(this);
309 wxControlRenderer renderer(this, dc, m_renderer);
310
311 // draw the control
312 DoDraw(&renderer);
313 }
314}
315
5ebcf581
RR
316// the event handler executed when the window background must be painted
317void wxWindow::OnErase(wxEraseEvent& event)
318{
319 if ( !m_renderer )
320 {
321 event.Skip();
322
323 return;
324 }
2b5f62a0 325
5ebcf581
RR
326 DoDrawBackground(*event.GetDC());
327
9a6384ca 328#if wxUSE_SCROLLBAR
5ebcf581
RR
329 // if we have both scrollbars, we also have a square in the corner between
330 // them which we must paint
331 if ( m_scrollbarVert && m_scrollbarHorz )
332 {
333 wxSize size = GetSize();
334 wxRect rectClient = GetClientRect(),
335 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
336
337 wxRect rectCorner;
338 rectCorner.x = rectClient.GetRight() + 1;
339 rectCorner.y = rectClient.GetBottom() + 1;
340 rectCorner.SetRight(size.x - rectBorder.width);
341 rectCorner.SetBottom(size.y - rectBorder.height);
342
343 if ( GetUpdateRegion().Contains(rectCorner) )
344 {
345 m_renderer->DrawScrollCorner(*event.GetDC(), rectCorner);
346 }
347 }
9a6384ca 348#endif // wxUSE_SCROLLBAR
5ebcf581
RR
349}
350
1e6feb95
VZ
351bool wxWindow::DoDrawBackground(wxDC& dc)
352{
1e6feb95 353 wxRect rect;
2b5f62a0 354
5ebcf581
RR
355 wxSize size = GetSize(); // Why not GetClientSize() ?
356 rect.x = 0;
357 rect.y = 0;
358 rect.width = size.x;
359 rect.height = size.y;
2b5f62a0
VZ
360
361 wxWindow * const parent = GetParent();
3b6c95eb 362 if ( HasTransparentBackground() && !UseBgCol() && parent )
1e6feb95 363 {
92d912a2
VS
364 // DirectFB paints the parent first, then its child windows, so by
365 // the time this code is called, parent's background was already
366 // drawn and there's no point in (imperfectly!) duplicating the work
367 // here:
368#ifndef __WXDFB__
5ebcf581 369 wxASSERT( !IsTopLevel() );
2b5f62a0 370
5ebcf581 371 wxPoint pos = GetPosition();
2b5f62a0 372
5ebcf581 373 AdjustForParentClientOrigin( pos.x, pos.y, 0 );
2b5f62a0 374
5ebcf581 375 // Adjust DC logical origin
635eb8bc
JS
376 wxCoord org_x, org_y, x, y;
377 dc.GetLogicalOrigin( &org_x, &org_y );
378 x = org_x + pos.x;
379 y = org_y + pos.y;
5ebcf581 380 dc.SetLogicalOrigin( x, y );
2b5f62a0 381
5ebcf581
RR
382 // Adjust draw rect
383 rect.x = pos.x;
384 rect.y = pos.y;
2b5f62a0 385
5ebcf581 386 // Let parent draw the background
2b5f62a0 387 parent->EraseBackground( dc, rect );
635eb8bc
JS
388
389 // Restore DC logical origin
390 dc.SetLogicalOrigin( org_x, org_y );
92d912a2 391#endif // !__WXDFB__
1e6feb95 392 }
5ebcf581
RR
393 else
394 {
90e572f1 395 // Draw background ourselves
2b5f62a0 396 EraseBackground( dc, rect );
5ebcf581 397 }
2b5f62a0 398
a290fa5a 399 return true;
5ebcf581 400}
1e6feb95 401
5ebcf581
RR
402void wxWindow::EraseBackground(wxDC& dc, const wxRect& rect)
403{
1e6feb95
VZ
404 if ( GetBackgroundBitmap().Ok() )
405 {
5ebcf581 406 // Get the bitmap and the flags
1e6feb95
VZ
407 int alignment;
408 wxStretch stretch;
409 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
410 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
411 }
2b5f62a0 412 else
1e6feb95 413 {
5ebcf581 414 // Just fill it with bg colour if no bitmap
2b5f62a0 415
1e6feb95
VZ
416 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this),
417 rect, GetStateFlags());
418 }
1e6feb95
VZ
419}
420
421void wxWindow::DoDrawBorder(wxDC& dc, const wxRect& rect)
422{
423 // draw outline unless the update region is enitrely inside it in which
424 // case we don't need to do it
425#if 0 // doesn't seem to work, why?
426 if ( wxRegion(rect).Contains(GetUpdateRegion().GetBox()) != wxInRegion )
427#endif
428 {
429 m_renderer->DrawBorder(dc, GetBorder(), rect, GetStateFlags());
430 }
431}
432
61fef19b 433void wxWindow::DoDraw(wxControlRenderer * WXUNUSED(renderer))
1e6feb95
VZ
434{
435}
436
6ef4b814 437void wxWindow::Refresh(bool eraseBackground, const wxRect *rect)
1e6feb95 438{
6ef4b814
VS
439 wxRect rectClient; // the same rectangle in client coordinates
440 wxPoint origin = GetClientAreaOrigin();
1e6feb95
VZ
441
442 wxSize size = GetClientSize();
443
6ef4b814 444 if ( rect )
1e6feb95 445 {
6ef4b814
VS
446 // the rectangle passed as argument is in client coordinates
447 rectClient = *rect;
1e6feb95
VZ
448
449 // don't refresh anything beyond the client area (scrollbars for
450 // example)
6ef4b814
VS
451 if ( rectClient.GetRight() > size.x )
452 rectClient.SetRight(size.x);
453 if ( rectClient.GetBottom() > size.y )
454 rectClient.SetBottom(size.y);
1e6feb95 455
1e6feb95
VZ
456 }
457 else // refresh the entire client area
458 {
6ef4b814
VS
459 // x,y is already set to 0 by default
460 rectClient.SetSize(size);
1e6feb95
VZ
461 }
462
6ef4b814
VS
463 // convert refresh rectangle to window coordinates:
464 wxRect rectWin(rectClient);
465 rectWin.Offset(origin);
466
1e6feb95
VZ
467 // debugging helper
468#ifdef WXDEBUG_REFRESH
a290fa5a 469 static bool s_refreshDebug = false;
1e6feb95
VZ
470 if ( s_refreshDebug )
471 {
472 wxWindowDC dc(this);
473 dc.SetBrush(*wxCYAN_BRUSH);
474 dc.SetPen(*wxTRANSPARENT_PEN);
475 dc.DrawRectangle(rectWin);
476
477 // under Unix we use "--sync" X option for this
8cb172b4 478 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1e6feb95
VZ
479 ::GdiFlush();
480 ::Sleep(200);
481 #endif // __WXMSW__
482 }
483#endif // WXDEBUG_REFRESH
484
485 wxWindowNative::Refresh(eraseBackground, &rectWin);
82e5f91b
JS
486
487 // Refresh all sub controls if any.
6ef4b814
VS
488 wxWindowList& children = GetChildren();
489 for ( wxWindowList::iterator i = children.begin(); i != children.end(); ++i )
82e5f91b 490 {
6ef4b814
VS
491 wxWindow *child = *i;
492 // only refresh subcontrols if they are visible:
493 if ( child->IsTopLevel() || !child->IsShown() || child->IsFrozen() )
494 continue;
495
496 // ...and when the subcontrols are in the update region:
497 wxRect childrect(child->GetRect());
498 childrect.Intersect(rectClient);
499 if ( childrect.IsEmpty() )
500 continue;
501
502 // refresh the subcontrol now:
503 childrect.Offset(-child->GetPosition());
504 // NB: We must call wxWindowNative version because we need to refresh
505 // the entire control, not just its client area, and this is why we
506 // don't account for child client area origin here neither. Also
507 // note that we don't pass eraseBackground to the child, but use
508 // true instead: this is because we can't be sure that
509 // eraseBackground=false is safe for children as well and not only
510 // for the parent.
511 child->wxWindowNative::Refresh(eraseBackground, &childrect);
82e5f91b 512 }
1e6feb95
VZ
513}
514
515// ----------------------------------------------------------------------------
516// state flags
517// ----------------------------------------------------------------------------
518
519bool wxWindow::Enable(bool enable)
520{
521 if ( !wxWindowNative::Enable(enable) )
a290fa5a 522 return false;
1e6feb95
VZ
523
524 // disabled window can't keep focus
c4bcd8fc 525 if ( FindFocus() == this && GetParent() != NULL )
1e6feb95
VZ
526 {
527 GetParent()->SetFocus();
528 }
529
530 if ( m_renderer )
531 {
532 // a window with renderer is drawn by ourselves and it has to be
533 // refreshed to reflect its new status
534 Refresh();
535 }
536
a290fa5a 537 return true;
1e6feb95
VZ
538}
539
540bool wxWindow::IsFocused() const
541{
5625d0d4 542 return FindFocus() == this;
1e6feb95
VZ
543}
544
545bool wxWindow::IsPressed() const
546{
a290fa5a 547 return false;
1e6feb95
VZ
548}
549
550bool wxWindow::IsDefault() const
551{
a290fa5a 552 return false;
1e6feb95
VZ
553}
554
555bool wxWindow::IsCurrent() const
556{
557 return m_isCurrent;
558}
559
560bool wxWindow::SetCurrent(bool doit)
561{
562 if ( doit == m_isCurrent )
a290fa5a 563 return false;
1e6feb95
VZ
564
565 m_isCurrent = doit;
566
567 if ( CanBeHighlighted() )
568 Refresh();
569
a290fa5a 570 return true;
1e6feb95
VZ
571}
572
573int wxWindow::GetStateFlags() const
574{
575 int flags = 0;
576 if ( !IsEnabled() )
577 flags |= wxCONTROL_DISABLED;
578
579 // the following states are only possible if our application is active - if
580 // it is not, even our default/focused controls shouldn't appear as such
581 if ( wxTheApp->IsActive() )
582 {
583 if ( IsCurrent() )
584 flags |= wxCONTROL_CURRENT;
585 if ( IsFocused() )
586 flags |= wxCONTROL_FOCUSED;
587 if ( IsPressed() )
588 flags |= wxCONTROL_PRESSED;
589 if ( IsDefault() )
590 flags |= wxCONTROL_ISDEFAULT;
591 }
592
593 return flags;
594}
595
596// ----------------------------------------------------------------------------
597// size
598// ----------------------------------------------------------------------------
599
600void wxWindow::OnSize(wxSizeEvent& event)
601{
aae73669 602 event.Skip();
2b5f62a0 603
9a6384ca 604#if wxUSE_SCROLLBAR
1e6feb95
VZ
605 if ( m_scrollbarVert || m_scrollbarHorz )
606 {
607 PositionScrollbars();
608 }
9a6384ca 609#endif // wxUSE_SCROLLBAR
2b5f62a0 610
ab6b6b15 611#if 0 // ndef __WXMSW__
ec47a147 612 // Refresh the area (strip) previously occupied by the border
2b5f62a0 613
e441e1f4 614 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) && IsShown() )
ec47a147 615 {
aae73669
RR
616 // This code assumes that wxSizeEvent.GetSize() returns
617 // the area of the entire window, not just the client
618 // area.
ec47a147 619 wxSize newSize = event.GetSize();
2b5f62a0 620
a290fa5a 621 if (m_oldSize.x == wxDefaultCoord && m_oldSize.y == wxDefaultCoord)
aae73669
RR
622 {
623 m_oldSize = newSize;
624 return;
625 }
2b5f62a0 626
ec47a147
RR
627 if (HasFlag( wxSIMPLE_BORDER ))
628 {
629 if (newSize.y > m_oldSize.y)
630 {
631 wxRect rect;
632 rect.x = 0;
633 rect.width = m_oldSize.x;
aae73669 634 rect.y = m_oldSize.y-2;
ec47a147 635 rect.height = 1;
a290fa5a 636 Refresh( true, &rect );
ec47a147 637 }
aae73669
RR
638 else if (newSize.y < m_oldSize.y)
639 {
640 wxRect rect;
641 rect.y = newSize.y;
642 rect.x = 0;
643 rect.height = 1;
644 rect.width = newSize.x;
a290fa5a 645 wxWindowNative::Refresh( true, &rect );
aae73669 646 }
2b5f62a0 647
ec47a147
RR
648 if (newSize.x > m_oldSize.x)
649 {
650 wxRect rect;
651 rect.y = 0;
652 rect.height = m_oldSize.y;
aae73669 653 rect.x = m_oldSize.x-2;
ec47a147 654 rect.width = 1;
a290fa5a 655 Refresh( true, &rect );
ec47a147 656 }
aae73669
RR
657 else if (newSize.x < m_oldSize.x)
658 {
659 wxRect rect;
660 rect.x = newSize.x;
661 rect.y = 0;
662 rect.width = 1;
663 rect.height = newSize.y;
a290fa5a 664 wxWindowNative::Refresh( true, &rect );
aae73669 665 }
ec47a147
RR
666 }
667 else
668 if (HasFlag( wxSUNKEN_BORDER ) || HasFlag( wxRAISED_BORDER ))
669 {
670 if (newSize.y > m_oldSize.y)
671 {
672 wxRect rect;
673 rect.x = 0;
674 rect.width = m_oldSize.x;
aae73669 675 rect.y = m_oldSize.y-4;
ec47a147 676 rect.height = 2;
a290fa5a 677 Refresh( true, &rect );
ec47a147 678 }
aae73669
RR
679 else if (newSize.y < m_oldSize.y)
680 {
681 wxRect rect;
682 rect.y = newSize.y;
683 rect.x = 0;
684 rect.height = 2;
685 rect.width = newSize.x;
a290fa5a 686 wxWindowNative::Refresh( true, &rect );
aae73669 687 }
2b5f62a0 688
ec47a147
RR
689 if (newSize.x > m_oldSize.x)
690 {
691 wxRect rect;
692 rect.y = 0;
693 rect.height = m_oldSize.y;
aae73669 694 rect.x = m_oldSize.x-4;
ec47a147 695 rect.width = 2;
a290fa5a 696 Refresh( true, &rect );
ec47a147 697 }
aae73669
RR
698 else if (newSize.x < m_oldSize.x)
699 {
700 wxRect rect;
701 rect.x = newSize.x;
702 rect.y = 0;
703 rect.width = 2;
704 rect.height = newSize.y;
a290fa5a 705 wxWindowNative::Refresh( true, &rect );
aae73669 706 }
ec47a147 707 }
2b5f62a0 708
ec47a147
RR
709 m_oldSize = newSize;
710 }
711#endif
1e6feb95
VZ
712}
713
714wxSize wxWindow::DoGetBestSize() const
715{
716 return AdjustSize(DoGetBestClientSize());
717}
718
719wxSize wxWindow::DoGetBestClientSize() const
720{
721 return wxWindowNative::DoGetBestSize();
722}
723
724wxSize wxWindow::AdjustSize(const wxSize& size) const
725{
726 wxSize sz = size;
727 if ( m_renderer )
728 m_renderer->AdjustSize(&sz, this);
729 return sz;
730}
731
732wxPoint wxWindow::GetClientAreaOrigin() const
733{
734 wxPoint pt = wxWindowBase::GetClientAreaOrigin();
735
ab6b6b15
RR
736#if wxUSE_TWO_WINDOWS
737#else
1e6feb95
VZ
738 if ( m_renderer )
739 pt += m_renderer->GetBorderDimensions(GetBorder()).GetPosition();
ab6b6b15 740#endif
1e6feb95
VZ
741
742 return pt;
743}
744
745void wxWindow::DoGetClientSize(int *width, int *height) const
746{
3379ed37
VZ
747 // if it is a native window, we assume it handles the scrollbars itself
748 // too - and if it doesn't, there is not much we can do
749 if ( !m_renderer )
750 {
751 wxWindowNative::DoGetClientSize(width, height);
752
753 return;
754 }
755
1e6feb95
VZ
756 int w, h;
757 wxWindowNative::DoGetClientSize(&w, &h);
758
759 // we assume that the scrollbars are positioned correctly (by a previous
760 // call to PositionScrollbars()) here
761
762 wxRect rectBorder;
763 if ( m_renderer )
764 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
765
1e6feb95
VZ
766 if ( width )
767 {
9a6384ca 768#if wxUSE_SCROLLBAR
1e6feb95
VZ
769 // in any case, take account of the scrollbar
770 if ( m_scrollbarVert )
771 w -= m_scrollbarVert->GetSize().x;
9a6384ca 772#endif // wxUSE_SCROLLBAR
1e6feb95 773
4eb124f5
VS
774 // account for the left and right borders
775 *width = w - rectBorder.x - rectBorder.width;
1e6feb95
VZ
776
777 // we shouldn't return invalid width
778 if ( *width < 0 )
779 *width = 0;
780 }
781
782 if ( height )
783 {
9a6384ca 784#if wxUSE_SCROLLBAR
1e6feb95
VZ
785 if ( m_scrollbarHorz )
786 h -= m_scrollbarHorz->GetSize().y;
9a6384ca 787#endif // wxUSE_SCROLLBAR
1e6feb95 788
4eb124f5 789 *height = h - rectBorder.y - rectBorder.height;
1e6feb95
VZ
790
791 // we shouldn't return invalid height
792 if ( *height < 0 )
793 *height = 0;
794 }
795}
796
797void wxWindow::DoSetClientSize(int width, int height)
798{
799 // take into account the borders
800 wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder());
801 width += rectBorder.x;
802 height += rectBorder.y;
803
804 // and the scrollbars (as they may be offset into the border, use the
805 // scrollbar position, not size - this supposes that PositionScrollbars()
806 // had been called before)
1e6feb95 807 wxSize size = GetSize();
9a6384ca 808#if wxUSE_SCROLLBAR
1e6feb95
VZ
809 if ( m_scrollbarVert )
810 width += size.x - m_scrollbarVert->GetPosition().x;
9a6384ca 811#endif // wxUSE_SCROLLBAR
4eb124f5 812 width += rectBorder.width;
1e6feb95 813
9a6384ca 814#if wxUSE_SCROLLBAR
1e6feb95
VZ
815 if ( m_scrollbarHorz )
816 height += size.y - m_scrollbarHorz->GetPosition().y;
9a6384ca 817#endif // wxUSE_SCROLLBAR
4eb124f5 818 height += rectBorder.height;
1e6feb95
VZ
819
820 wxWindowNative::DoSetClientSize(width, height);
821}
822
823wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const
824{
825 wxHitTest ht = wxWindowNative::DoHitTest(x, y);
9a6384ca
WS
826
827#if wxUSE_SCROLLBAR
1e6feb95
VZ
828 if ( ht == wxHT_WINDOW_INSIDE )
829 {
830 if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x )
831 {
832 // it can still be changed below because it may also be the corner
833 ht = wxHT_WINDOW_VERT_SCROLLBAR;
834 }
835
836 if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y )
837 {
838 ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER
839 : wxHT_WINDOW_HORZ_SCROLLBAR;
840 }
841 }
9a6384ca 842#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
843
844 return ht;
845}
846
847// ----------------------------------------------------------------------------
848// scrolling: we implement it entirely ourselves except for ScrollWindow()
849// function which is supposed to be (efficiently) implemented by the native
850// window class
851// ----------------------------------------------------------------------------
852
853void wxWindow::RefreshScrollbars()
854{
9a6384ca 855#if wxUSE_SCROLLBAR
1e6feb95
VZ
856 if ( m_scrollbarHorz )
857 m_scrollbarHorz->Refresh();
858
859 if ( m_scrollbarVert )
860 m_scrollbarVert->Refresh();
9a6384ca 861#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
862}
863
864void wxWindow::PositionScrollbars()
865{
9a6384ca 866#if wxUSE_SCROLLBAR
1e6feb95
VZ
867 // do not use GetClientSize/Rect as it relies on the scrollbars being
868 // correctly positioned
869
870 wxSize size = GetSize();
871 wxBorder border = GetBorder();
872 wxRect rectBorder = m_renderer->GetBorderDimensions(border);
873 bool inside = m_renderer->AreScrollbarsInsideBorder();
874
875 int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0;
876 int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0;
877
878 wxRect rectBar;
879 if ( m_scrollbarVert )
880 {
881 rectBar.x = size.x - width;
882 if ( inside )
883 rectBar.x -= rectBorder.width;
884 rectBar.width = width;
885 rectBar.y = 0;
886 if ( inside )
887 rectBar.y += rectBorder.y;
888 rectBar.height = size.y - height;
889 if ( inside )
890 rectBar.height -= rectBorder.y + rectBorder.height;
891
892 m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
893 }
894
895 if ( m_scrollbarHorz )
896 {
897 rectBar.y = size.y - height;
898 if ( inside )
899 rectBar.y -= rectBorder.height;
900 rectBar.height = height;
901 rectBar.x = 0;
902 if ( inside )
903 rectBar.x += rectBorder.x;
904 rectBar.width = size.x - width;
905 if ( inside )
906 rectBar.width -= rectBorder.x + rectBorder.width;
907
908 m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
909 }
910
911 RefreshScrollbars();
9a6384ca 912#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
913}
914
915void wxWindow::SetScrollbar(int orient,
916 int pos,
917 int pageSize,
918 int range,
919 bool refresh)
920{
9a6384ca 921#if wxUSE_SCROLLBAR
3f393e3d
VZ
922 wxASSERT_MSG( pageSize <= range,
923 _T("page size can't be greater than range") );
924
a290fa5a 925 bool hasClientSizeChanged = false;
1e6feb95 926 wxScrollBar *scrollbar = GetScrollbar(orient);
3f393e3d 927 if ( range && (pageSize < range) )
1e6feb95
VZ
928 {
929 if ( !scrollbar )
930 {
931 // create it
ab6b6b15 932#if wxUSE_TWO_WINDOWS
a290fa5a 933 SetInsertIntoMain( true );
ab6b6b15 934#endif
c04c7a3d
VS
935 scrollbar = new wxWindowScrollBar(this, wxID_ANY,
936 wxDefaultPosition, wxDefaultSize,
937 orient & wxVERTICAL ? wxSB_VERTICAL
938 : wxSB_HORIZONTAL);
ab6b6b15 939#if wxUSE_TWO_WINDOWS
a290fa5a 940 SetInsertIntoMain( false );
ab6b6b15 941#endif
1e6feb95
VZ
942 if ( orient & wxVERTICAL )
943 m_scrollbarVert = scrollbar;
944 else
945 m_scrollbarHorz = scrollbar;
946
947 // the client area diminished as we created a scrollbar
a290fa5a 948 hasClientSizeChanged = true;
1e6feb95
VZ
949
950 PositionScrollbars();
951 }
952 else if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
953 {
954 // we might have disabled it before
955 scrollbar->Enable();
956 }
957
958 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
959 }
960 else // no range means no scrollbar
961 {
962 if ( scrollbar )
963 {
964 // wxALWAYS_SHOW_SB only applies to the vertical scrollbar
965 if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) )
966 {
967 // just disable the scrollbar
968 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
969 scrollbar->Disable();
970 }
971 else // really remove the scrollbar
972 {
973 delete scrollbar;
974
975 if ( orient & wxVERTICAL )
976 m_scrollbarVert = NULL;
977 else
978 m_scrollbarHorz = NULL;
979
980 // the client area increased as we removed a scrollbar
a290fa5a 981 hasClientSizeChanged = true;
1e6feb95
VZ
982
983 // the size of the remaining scrollbar must be adjusted
984 if ( m_scrollbarHorz || m_scrollbarVert )
985 {
986 PositionScrollbars();
987 }
988 }
989 }
990 }
991
992 // give the window a chance to relayout
993 if ( hasClientSizeChanged )
994 {
a17a79ba
RR
995#if wxUSE_TWO_WINDOWS
996 wxWindowNative::SetSize( GetSize() );
997#else
1e6feb95
VZ
998 wxSizeEvent event(GetSize());
999 (void)GetEventHandler()->ProcessEvent(event);
a17a79ba 1000#endif
1e6feb95 1001 }
9a6384ca
WS
1002#else
1003 wxUnusedVar(orient);
1004 wxUnusedVar(pos);
1005 wxUnusedVar(pageSize);
1006 wxUnusedVar(range);
1007 wxUnusedVar(refresh);
1008#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
1009}
1010
61fef19b 1011void wxWindow::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
1e6feb95 1012{
9a6384ca 1013#if wxUSE_SCROLLBAR
1e6feb95 1014 wxScrollBar *scrollbar = GetScrollbar(orient);
1e6feb95 1015
45e13c7f
JS
1016 if (scrollbar)
1017 scrollbar->SetThumbPosition(pos);
7d297414
VZ
1018
1019 // VZ: I think we can safely ignore this as we always refresh it
1020 // automatically whenever the value chanegs
1021#if 0
1e6feb95
VZ
1022 if ( refresh )
1023 Refresh();
7d297414 1024#endif
9a6384ca
WS
1025#else
1026 wxUnusedVar(orient);
1027 wxUnusedVar(pos);
1028#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
1029}
1030
1031int wxWindow::GetScrollPos(int orient) const
1032{
9a6384ca 1033#if wxUSE_SCROLLBAR
1e6feb95
VZ
1034 wxScrollBar *scrollbar = GetScrollbar(orient);
1035 return scrollbar ? scrollbar->GetThumbPosition() : 0;
9a6384ca
WS
1036#else
1037 wxUnusedVar(orient);
1038 return 0;
1039#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
1040}
1041
1042int wxWindow::GetScrollThumb(int orient) const
1043{
9a6384ca 1044#if wxUSE_SCROLLBAR
1e6feb95
VZ
1045 wxScrollBar *scrollbar = GetScrollbar(orient);
1046 return scrollbar ? scrollbar->GetThumbSize() : 0;
9a6384ca
WS
1047#else
1048 wxUnusedVar(orient);
1049 return 0;
1050#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
1051}
1052
1053int wxWindow::GetScrollRange(int orient) const
1054{
9a6384ca 1055#if wxUSE_SCROLLBAR
1e6feb95
VZ
1056 wxScrollBar *scrollbar = GetScrollbar(orient);
1057 return scrollbar ? scrollbar->GetRange() : 0;
9a6384ca
WS
1058#else
1059 wxUnusedVar(orient);
1060 return 0;
1061#endif // wxUSE_SCROLLBAR
1e6feb95
VZ
1062}
1063
1064void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
1065{
66a58655
VS
1066 // use native scrolling when available and do it in generic way
1067 // otherwise:
4125131b
RR
1068#ifdef __WXX11__
1069
66a58655 1070 wxWindowNative::ScrollWindow(dx, dy, rect);
4125131b 1071
2b5f62a0 1072#else // !wxX11
4125131b 1073
1e6feb95
VZ
1074 // before scrolling it, ensure that we don't have any unpainted areas
1075 Update();
1076
1077 wxRect r;
1078
1079 if ( dx )
1080 {
1081 r = ScrollNoRefresh(dx, 0, rect);
a290fa5a 1082 Refresh(true /* erase bkgnd */, &r);
1e6feb95
VZ
1083 }
1084
1085 if ( dy )
1086 {
1087 r = ScrollNoRefresh(0, dy, rect);
a290fa5a 1088 Refresh(true /* erase bkgnd */, &r);
1e6feb95 1089 }
2b5f62a0
VZ
1090
1091 // scroll children accordingly:
66a58655 1092 wxPoint offset(dx, dy);
2b5f62a0 1093
ac32ba44 1094 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
66a58655
VS
1095 node; node = node->GetNext())
1096 {
1097 wxWindow *child = node->GetData();
9a6384ca 1098#if wxUSE_SCROLLBAR
1c9f1f4d
VS
1099 if ( child == m_scrollbarVert || child == m_scrollbarHorz )
1100 continue;
9a6384ca 1101#endif // wxUSE_SCROLLBAR
1c9f1f4d
VS
1102
1103 // VS: Scrolling children has non-trivial semantics. If rect=NULL then
2b5f62a0 1104 // it is easy: we scroll all children. Otherwise it gets
1c9f1f4d
VS
1105 // complicated:
1106 // 1. if scrolling in one direction only, scroll only
1107 // those children that intersect shaft defined by the rectangle
1108 // and scrolling direction
1109 // 2. if scrolling in both axes, scroll all children
2b5f62a0 1110
6a6e2822
VS
1111 bool shouldMove = false;
1112
1c9f1f4d
VS
1113 if ( rect && (dx * dy == 0 /* moving in only one of x, y axis */) )
1114 {
1115 wxRect childRect = child->GetRect();
1116 if ( dx == 0 && (childRect.GetLeft() <= rect->GetRight() ||
1117 childRect.GetRight() >= rect->GetLeft()) )
1118 {
6a6e2822 1119 shouldMove = true;
1c9f1f4d
VS
1120 }
1121 else if ( dy == 0 && (childRect.GetTop() <= rect->GetBottom() ||
1122 childRect.GetBottom() >= rect->GetTop()) )
1123 {
6a6e2822 1124 shouldMove = true;
1c9f1f4d 1125 }
6a6e2822 1126 // else: child outside of scrolling shaft, don't move
1c9f1f4d 1127 }
6a6e2822 1128 else // scrolling in both axes or rect=NULL
66a58655 1129 {
6a6e2822 1130 shouldMove = true;
66a58655 1131 }
6a6e2822
VS
1132
1133 if ( shouldMove )
1134 child->Move(child->GetPosition() + offset, wxSIZE_ALLOW_MINUS_ONE);
2b5f62a0
VZ
1135 }
1136#endif // wxX11/!wxX11
1e6feb95
VZ
1137}
1138
1139wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal)
1140{
1141 wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") );
1142
1143 // the rect to refresh (which we will calculate)
1144 wxRect rect;
1145
1146 if ( !dx && !dy )
1147 {
1148 // nothing to do
1149 return rect;
1150 }
1151
1152 // calculate the part of the window which we can just redraw in the new
1153 // location
1154 wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize();
1155
1156 wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"),
1157 sizeTotal.x, sizeTotal.y, dx, dy);
1158
1159 // the initial and end point of the region we move in client coords
1160 wxPoint ptSource, ptDest;
1161 if ( rectTotal )
1162 {
1163 ptSource = rectTotal->GetPosition();
1164 ptDest = rectTotal->GetPosition();
1165 }
1166
1167 // the size of this region
1168 wxSize size;
1169 size.x = sizeTotal.x - abs(dx);
1170 size.y = sizeTotal.y - abs(dy);
1171 if ( size.x <= 0 || size.y <= 0 )
1172 {
1173 // just redraw everything as nothing of the displayed image will stay
1174 wxLogTrace(_T("scroll"), _T("refreshing everything"));
1175
1176 rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y);
1177 }
1178 else // move the part which doesn't change to the new location
1179 {
1180 // note that when we scroll the canvas in some direction we move the
1181 // block which doesn't need to be refreshed in the opposite direction
1182
1183 if ( dx < 0 )
1184 {
1185 // scroll to the right, move to the left
1186 ptSource.x -= dx;
1187 }
1188 else
1189 {
1190 // scroll to the left, move to the right
1191 ptDest.x += dx;
1192 }
1193
1194 if ( dy < 0 )
1195 {
1196 // scroll down, move up
1197 ptSource.y -= dy;
1198 }
1199 else
1200 {
1201 // scroll up, move down
1202 ptDest.y += dy;
1203 }
1204
1205#if wxUSE_CARET
1206 // we need to hide the caret before moving or it will erase itself at
1207 // the wrong (old) location
1208 wxCaret *caret = GetCaret();
1209 if ( caret )
1210 caret->Hide();
1211#endif // wxUSE_CARET
1212
1213 // do move
1214 wxClientDC dc(this);
1215 wxBitmap bmp(size.x, size.y);
1216 wxMemoryDC dcMem;
1217 dcMem.SelectObject(bmp);
1218
c47addef 1219 dcMem.Blit(wxPoint(0,0), size, &dc, ptSource
03147cd0 1220#if defined(__WXGTK__) && !defined(wxHAS_WORKING_GTK_DC_BLIT)
1e6feb95
VZ
1221 + GetClientAreaOrigin()
1222#endif // broken wxGTK wxDC::Blit
1223 );
c47addef 1224 dc.Blit(ptDest, size, &dcMem, wxPoint(0,0));
1e6feb95
VZ
1225
1226 wxLogTrace(_T("scroll"),
1227 _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"),
1228 ptSource.x, ptSource.y,
1229 size.x, size.y,
1230 ptDest.x, ptDest.y);
1231
1232 // and now repaint the uncovered area
1233
1234 // FIXME: We repaint the intersection of these rectangles twice - is
1235 // it bad? I don't think so as it is rare to scroll the window
1236 // diagonally anyhow and so adding extra logic to compute
1237 // rectangle intersection is probably not worth the effort
1238
1239 rect.x = ptSource.x;
1240 rect.y = ptSource.y;
1241
1242 if ( dx )
1243 {
1244 if ( dx < 0 )
1245 {
1246 // refresh the area along the right border
1247 rect.x += size.x + dx;
1248 rect.width = -dx;
1249 }
1250 else
1251 {
1252 // refresh the area along the left border
1253 rect.width = dx;
1254 }
1255
1256 rect.height = sizeTotal.y;
1257
1258 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1259 rect.x, rect.y,
1260 rect.GetRight() + 1, rect.GetBottom() + 1);
1261 }
1262
1263 if ( dy )
1264 {
1265 if ( dy < 0 )
1266 {
1267 // refresh the area along the bottom border
1268 rect.y += size.y + dy;
1269 rect.height = -dy;
1270 }
1271 else
1272 {
1273 // refresh the area along the top border
1274 rect.height = dy;
1275 }
1276
1277 rect.width = sizeTotal.x;
1278
1279 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1280 rect.x, rect.y,
1281 rect.GetRight() + 1, rect.GetBottom() + 1);
1282 }
1283
1284#if wxUSE_CARET
1285 if ( caret )
1286 caret->Show();
1287#endif // wxUSE_CARET
1288 }
1289
1290 return rect;
1291}
1292
1e6feb95
VZ
1293// ----------------------------------------------------------------------------
1294// accelerators and menu hot keys
1295// ----------------------------------------------------------------------------
1296
1297#if wxUSE_MENUS
1298 // the last window over which Alt was pressed (used by OnKeyUp)
1299 wxWindow *wxWindow::ms_winLastAltPress = NULL;
1300#endif // wxUSE_MENUS
1301
1302#if wxUSE_ACCEL || wxUSE_MENUS
1303
1304void wxWindow::OnKeyDown(wxKeyEvent& event)
1305{
1306#if wxUSE_MENUS
1307 int key = event.GetKeyCode();
f52c3131 1308 if ( !event.ControlDown() && (key == WXK_ALT || key == WXK_F10) )
1e6feb95
VZ
1309 {
1310 ms_winLastAltPress = this;
1311
1312 // it can't be an accel anyhow
1313 return;
1314 }
1315
1316 ms_winLastAltPress = NULL;
1317#endif // wxUSE_MENUS
1318
1319#if wxUSE_ACCEL
1320 for ( wxWindow *win = this; win; win = win->GetParent() )
1321 {
1322 int command = win->GetAcceleratorTable()->GetCommand(event);
1323 if ( command != -1 )
1324 {
1325 wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command);
1326 if ( win->GetEventHandler()->ProcessEvent(eventCmd) )
1327 {
1328 // skip "event.Skip()" below
1329 return;
1330 }
1331 }
1332
1333 if ( win->IsTopLevel() )
1334 {
1335 // try the frame menu bar
1336#if wxUSE_MENUS
1337 wxFrame *frame = wxDynamicCast(win, wxFrame);
1338 if ( frame )
1339 {
1340 wxMenuBar *menubar = frame->GetMenuBar();
1341 if ( menubar && menubar->ProcessAccelEvent(event) )
1342 {
1343 // skip "event.Skip()" below
1344 return;
1345 }
1346 }
1347#endif // wxUSE_MENUS
1348
2c4eefc0 1349#if wxUSE_BUTTON
81762e79
VS
1350 // if it wasn't in a menu, try to find a button
1351 if ( command != -1 )
1352 {
1353 wxWindow* child = win->FindWindow(command);
1354 if ( child && wxDynamicCast(child, wxButton) )
1355 {
1356 wxCommandEvent eventCmd(wxEVT_COMMAND_BUTTON_CLICKED, command);
1357 eventCmd.SetEventObject(child);
1358 if ( child->GetEventHandler()->ProcessEvent(eventCmd) )
1359 {
1360 // skip "event.Skip()" below
1361 return;
1362 }
1363 }
1364 }
2c4eefc0 1365#endif // wxUSE_BUTTON
81762e79 1366
1e6feb95
VZ
1367 // don't propagate accels from the child frame to the parent one
1368 break;
1369 }
1370 }
1371#endif // wxUSE_ACCEL
1372
1373 event.Skip();
1374}
1375
1376#endif // wxUSE_ACCEL
1377
1378#if wxUSE_MENUS
1379
1380wxMenuBar *wxWindow::GetParentFrameMenuBar() const
1381{
1382 for ( const wxWindow *win = this; win; win = win->GetParent() )
1383 {
1384 if ( win->IsTopLevel() )
1385 {
1386 wxFrame *frame = wxDynamicCast(win, wxFrame);
1387 if ( frame )
1388 {
1389 return frame->GetMenuBar();
1390 }
1391
1392 // don't look further - we don't want to return the menubar of the
1393 // parent frame
1394 break;
1395 }
1396 }
1397
1398 return NULL;
1399}
1400
1401void wxWindow::OnChar(wxKeyEvent& event)
1402{
1403 if ( event.AltDown() && !event.ControlDown() )
1404 {
1405 int key = event.GetKeyCode();
1406
1407 wxMenuBar *menubar = GetParentFrameMenuBar();
1408 if ( menubar )
1409 {
1410 int item = menubar->FindNextItemForAccel(-1, key);
1411 if ( item != -1 )
1412 {
1413 menubar->PopupMenu((size_t)item);
1414
1415 // skip "event.Skip()" below
1416 return;
1417 }
1418 }
1419 }
1420
28117f24
VZ
1421 // if Return was pressed, see if there's a default button to activate
1422 if ( !event.HasModifiers() && event.GetKeyCode() == WXK_RETURN )
1423 {
1424 wxTopLevelWindow *
346d8899 1425 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
28117f24
VZ
1426 if ( tlw )
1427 {
346d8899 1428 wxButton *btn = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
28117f24
VZ
1429 if ( btn )
1430 {
1431 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, btn->GetId());
1432 evt.SetEventObject(btn);
1433 btn->Command(evt);
1434 return;
1435 }
1436 }
1437 }
1438
1439
1e6feb95
VZ
1440 event.Skip();
1441}
1442
1443void wxWindow::OnKeyUp(wxKeyEvent& event)
1444{
1445 int key = event.GetKeyCode();
f52c3131 1446 if ( !event.HasModifiers() && (key == WXK_ALT || key == WXK_F10) )
1e6feb95
VZ
1447 {
1448 // only process Alt release specially if there were no other key
1449 // presses since Alt had been pressed and if both events happened in
1450 // the same window
1451 if ( ms_winLastAltPress == this )
1452 {
1453 wxMenuBar *menubar = GetParentFrameMenuBar();
1454 if ( menubar && this != menubar )
1455 {
1456 menubar->SelectMenu(0);
1457 }
1458 }
1459 }
1460 else
1461 {
1462 event.Skip();
1463 }
1464
1465 // in any case reset it
1466 ms_winLastAltPress = NULL;
1467}
1468
1469#endif // wxUSE_MENUS
1470
30827629
VZ
1471// ----------------------------------------------------------------------------
1472// MSW-specific section
1473// ----------------------------------------------------------------------------
1474
1475#ifdef __WXMSW__
1476
1477#include "wx/msw/private.h"
1478
c140b7e7 1479WXLRESULT wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
30827629
VZ
1480{
1481 if ( message == WM_NCHITTEST )
1482 {
1483 // the windows which contain the other windows should let the mouse
1484 // events through, otherwise a window inside a static box would
1485 // never get any events at all
1486 if ( IsStaticBox() )
1487 {
1488 return HTTRANSPARENT;
1489 }
1490 }
1491
1492 return wxWindowNative::MSWWindowProc(message, wParam, lParam);
1493}
1494
1495#endif // __WXMSW__