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