]> git.saurik.com Git - wxWidgets.git/blame - src/univ/winuniv.cpp
Fix so OGL can build without wxBuffer
[wxWidgets.git] / src / univ / winuniv.cpp
CommitLineData
1e6feb95
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: univ/window.cpp
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)
1e6feb95
VZ
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#ifdef __GNUG__
196cc38b 21 #pragma implementation "univwindow.h"
1e6feb95
VZ
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/window.h"
34 #include "wx/dcclient.h"
35 #include "wx/dcmemory.h"
36 #include "wx/event.h"
37 #include "wx/scrolbar.h"
38 #include "wx/menu.h"
8cb172b4 39 #include "wx/frame.h"
1e6feb95
VZ
40#endif // WX_PRECOMP
41
42#include "wx/univ/colschem.h"
43#include "wx/univ/renderer.h"
44#include "wx/univ/theme.h"
45
46#if wxUSE_CARET
47 #include "wx/caret.h"
48#endif // wxUSE_CARET
49
50// turn Refresh() debugging on/off
51#define WXDEBUG_REFRESH
52
53#ifndef __WXDEBUG__
54 #undef WXDEBUG_REFRESH
55#endif
56
cff7ef89
VS
57#if defined(WXDEBUG_REFRESH) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
58#include "wx/msw/private.h"
59#endif
60
1e6feb95
VZ
61// ============================================================================
62// implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// event tables
67// ----------------------------------------------------------------------------
68
69// we can't use wxWindowNative here as it won't be expanded inside the macro
70#if defined(__WXMSW__)
71 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMSW)
72#elif defined(__WXGTK__)
73 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowGTK)
74#elif defined(__WXMGL__)
75 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMGL)
8354aa92
RR
76#elif defined(__WXX11__)
77 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowX11)
19193a2c
KB
78#elif defined(__WXPM__)
79 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowOS2)
1e6feb95
VZ
80#endif
81
82BEGIN_EVENT_TABLE(wxWindow, wxWindowNative)
83 EVT_SIZE(wxWindow::OnSize)
84
85#if wxUSE_ACCEL || wxUSE_MENUS
86 EVT_KEY_DOWN(wxWindow::OnKeyDown)
87#endif // wxUSE_ACCEL
88
89#if wxUSE_MENUS
90 EVT_CHAR(wxWindow::OnChar)
91 EVT_KEY_UP(wxWindow::OnKeyUp)
92#endif // wxUSE_MENUS
93
94 EVT_PAINT(wxWindow::OnPaint)
95 EVT_NC_PAINT(wxWindow::OnNcPaint)
96 EVT_ERASE_BACKGROUND(wxWindow::OnErase)
97END_EVENT_TABLE()
98
99// ----------------------------------------------------------------------------
100// creation
101// ----------------------------------------------------------------------------
102
103void wxWindow::Init()
104{
105 m_scrollbarVert =
106 m_scrollbarHorz = (wxScrollBar *)NULL;
107
108 m_isCurrent = FALSE;
109
110 m_renderer = wxTheme::Get()->GetRenderer();
111}
112
113bool wxWindow::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119{
2387541f
VZ
120 // we add wxCLIP_CHILDREN to get the same ("natural") behaviour under MSW
121 // as under the other platforms
1e6feb95 122 if ( !wxWindowNative::Create(parent, id, pos, size,
2387541f
VZ
123 style | wxCLIP_CHILDREN,
124 name) )
1e6feb95
VZ
125 {
126 return FALSE;
127 }
128
129 // if we should always have the scrollbar, do show it
130 if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
131 {
132 m_scrollbarVert = new wxScrollBar(this, -1,
133 wxDefaultPosition, wxDefaultSize,
134 wxSB_VERTICAL);
135
136 // and position it
137 PositionScrollbars();
138 }
139
1e6feb95
VZ
140 return TRUE;
141}
142
143// ----------------------------------------------------------------------------
144// background pixmap
145// ----------------------------------------------------------------------------
146
147void wxWindow::SetBackground(const wxBitmap& bitmap,
148 int alignment,
149 wxStretch stretch)
150{
151 m_bitmapBg = bitmap;
152 m_alignBgBitmap = alignment;
153 m_stretchBgBitmap = stretch;
154}
155
156const wxBitmap& wxWindow::GetBackgroundBitmap(int *alignment,
157 wxStretch *stretch) const
158{
159 if ( m_bitmapBg.Ok() )
160 {
161 if ( alignment )
162 *alignment = m_alignBgBitmap;
163 if ( stretch )
164 *stretch = m_stretchBgBitmap;
165 }
166
167 return m_bitmapBg;
168}
169
170// ----------------------------------------------------------------------------
171// painting
172// ----------------------------------------------------------------------------
173
174// the event handler executed when the window background must be painted
175void wxWindow::OnErase(wxEraseEvent& event)
176{
177 if ( !m_renderer )
178 {
179 event.Skip();
180
181 return;
182 }
183
184 DoDrawBackground(*event.GetDC());
185
186 // if we have both scrollbars, we also have a square in the corner between
187 // them which we must paint
188 if ( m_scrollbarVert && m_scrollbarHorz )
189 {
190 wxSize size = GetSize();
191 wxRect rectClient = GetClientRect(),
192 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
193
194 wxRect rectCorner;
195 rectCorner.x = rectClient.GetRight() + 1;
196 rectCorner.y = rectClient.GetBottom() + 1;
197 rectCorner.SetRight(size.x - rectBorder.width);
198 rectCorner.SetBottom(size.y - rectBorder.height);
199
200 if ( GetUpdateRegion().Contains(rectCorner) )
201 {
202 m_renderer->DrawScrollCorner(*event.GetDC(), rectCorner);
203 }
204 }
205}
206
207// the event handlers executed when the window must be repainted
208void wxWindow::OnNcPaint(wxPaintEvent& event)
209{
210 if ( m_renderer )
211 {
212 // get the window rect
213 wxRect rect;
214 wxSize size = GetSize();
215 rect.x =
216 rect.y = 0;
217 rect.width = size.x;
218 rect.height = size.y;
219
220 // if the scrollbars are outside the border, we must adjust the rect to
221 // exclude them
222 if ( !m_renderer->AreScrollbarsInsideBorder() )
223 {
224 wxScrollBar *scrollbar = GetScrollbar(wxVERTICAL);
225 if ( scrollbar )
226 rect.width -= scrollbar->GetSize().x;
227
228 scrollbar = GetScrollbar(wxHORIZONTAL);
229 if ( scrollbar )
230 rect.height -= scrollbar->GetSize().y;
231 }
232
233 // get the DC and draw the border on it
234 wxWindowDC dc(this);
235 DoDrawBorder(dc, rect);
236 }
237}
238
239void wxWindow::OnPaint(wxPaintEvent& event)
240{
241 if ( !m_renderer )
242 {
243 // it is a native control which paints itself
244 event.Skip();
245 }
246 else
247 {
248 // get the DC to use and create renderer on it
249 wxPaintDC dc(this);
250 wxControlRenderer renderer(this, dc, m_renderer);
251
252 // draw the control
253 DoDraw(&renderer);
254 }
255}
256
257bool wxWindow::DoDrawBackground(wxDC& dc)
258{
259 // FIXME: leaving this code in leads to partial bg redraws sometimes under
260 // MSW
261 wxRect rect;
262#ifndef __WXMSW__
263 rect = GetUpdateRegion().GetBox();
264 if ( !rect.width && !rect.height )
265#endif
266 {
267 wxSize size = GetSize();
268 rect.width = size.x;
269 rect.height = size.y;
270 }
271
272 if ( GetBackgroundBitmap().Ok() )
273 {
274 // get the bitmap and the flags
275 int alignment;
276 wxStretch stretch;
277 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
278 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
279 }
280 else // just fill it with bg colour if no bitmap
281 {
282 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this),
283 rect, GetStateFlags());
284 }
285
286 return TRUE;
287}
288
289void wxWindow::EraseBackground(wxDC& dc, const wxRect& rect)
290{
291 // TODO: handle bg bitmaps here!
292
293 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this), rect, GetStateFlags());
294}
295
296void wxWindow::DoDrawBorder(wxDC& dc, const wxRect& rect)
297{
298 // draw outline unless the update region is enitrely inside it in which
299 // case we don't need to do it
300#if 0 // doesn't seem to work, why?
301 if ( wxRegion(rect).Contains(GetUpdateRegion().GetBox()) != wxInRegion )
302#endif
303 {
304 m_renderer->DrawBorder(dc, GetBorder(), rect, GetStateFlags());
305 }
306}
307
308void wxWindow::DoDraw(wxControlRenderer *renderer)
309{
310}
311
312void wxWindow::Refresh(bool eraseBackground, const wxRect *rectClient)
313{
314 wxRect rectWin;
315 wxPoint pt = GetClientAreaOrigin();
316
317 wxSize size = GetClientSize();
318
319 if ( rectClient )
320 {
321 rectWin = *rectClient;
322
323 // don't refresh anything beyond the client area (scrollbars for
324 // example)
325 if ( rectWin.GetRight() > size.x )
326 rectWin.SetRight(size.x);
327 if ( rectWin.GetBottom() > size.y )
328 rectWin.SetBottom(size.y);
329
330 rectWin.Offset(pt);
331 }
332 else // refresh the entire client area
333 {
334 rectWin.x = pt.x;
335 rectWin.y = pt.y;
336 rectWin.width = size.x;
337 rectWin.height = size.y;
338 }
339
340 // debugging helper
341#ifdef WXDEBUG_REFRESH
342 static bool s_refreshDebug = FALSE;
343 if ( s_refreshDebug )
344 {
345 wxWindowDC dc(this);
346 dc.SetBrush(*wxCYAN_BRUSH);
347 dc.SetPen(*wxTRANSPARENT_PEN);
348 dc.DrawRectangle(rectWin);
349
350 // under Unix we use "--sync" X option for this
8cb172b4 351 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1e6feb95
VZ
352 ::GdiFlush();
353 ::Sleep(200);
354 #endif // __WXMSW__
355 }
356#endif // WXDEBUG_REFRESH
357
358 wxWindowNative::Refresh(eraseBackground, &rectWin);
359}
360
361// ----------------------------------------------------------------------------
362// state flags
363// ----------------------------------------------------------------------------
364
365bool wxWindow::Enable(bool enable)
366{
367 if ( !wxWindowNative::Enable(enable) )
368 return FALSE;
369
370 // disabled window can't keep focus
c4bcd8fc 371 if ( FindFocus() == this && GetParent() != NULL )
1e6feb95
VZ
372 {
373 GetParent()->SetFocus();
374 }
375
376 if ( m_renderer )
377 {
378 // a window with renderer is drawn by ourselves and it has to be
379 // refreshed to reflect its new status
380 Refresh();
381 }
382
383 return TRUE;
384}
385
386bool wxWindow::IsFocused() const
387{
388 wxWindow *self = wxConstCast(this, wxWindow);
389 return self->FindFocus() == self;
390}
391
392bool wxWindow::IsPressed() const
393{
394 return FALSE;
395}
396
397bool wxWindow::IsDefault() const
398{
399 return FALSE;
400}
401
402bool wxWindow::IsCurrent() const
403{
404 return m_isCurrent;
405}
406
407bool wxWindow::SetCurrent(bool doit)
408{
409 if ( doit == m_isCurrent )
410 return FALSE;
411
412 m_isCurrent = doit;
413
414 if ( CanBeHighlighted() )
415 Refresh();
416
417 return TRUE;
418}
419
420int wxWindow::GetStateFlags() const
421{
422 int flags = 0;
423 if ( !IsEnabled() )
424 flags |= wxCONTROL_DISABLED;
425
426 // the following states are only possible if our application is active - if
427 // it is not, even our default/focused controls shouldn't appear as such
428 if ( wxTheApp->IsActive() )
429 {
430 if ( IsCurrent() )
431 flags |= wxCONTROL_CURRENT;
432 if ( IsFocused() )
433 flags |= wxCONTROL_FOCUSED;
434 if ( IsPressed() )
435 flags |= wxCONTROL_PRESSED;
436 if ( IsDefault() )
437 flags |= wxCONTROL_ISDEFAULT;
438 }
439
440 return flags;
441}
442
443// ----------------------------------------------------------------------------
444// size
445// ----------------------------------------------------------------------------
446
447void wxWindow::OnSize(wxSizeEvent& event)
448{
449 if ( m_scrollbarVert || m_scrollbarHorz )
450 {
451 PositionScrollbars();
452 }
453
454 event.Skip();
455}
456
457wxSize wxWindow::DoGetBestSize() const
458{
459 return AdjustSize(DoGetBestClientSize());
460}
461
462wxSize wxWindow::DoGetBestClientSize() const
463{
464 return wxWindowNative::DoGetBestSize();
465}
466
467wxSize wxWindow::AdjustSize(const wxSize& size) const
468{
469 wxSize sz = size;
470 if ( m_renderer )
471 m_renderer->AdjustSize(&sz, this);
472 return sz;
473}
474
475wxPoint wxWindow::GetClientAreaOrigin() const
476{
477 wxPoint pt = wxWindowBase::GetClientAreaOrigin();
478
479 if ( m_renderer )
480 pt += m_renderer->GetBorderDimensions(GetBorder()).GetPosition();
481
482 return pt;
483}
484
485void wxWindow::DoGetClientSize(int *width, int *height) const
486{
3379ed37
VZ
487 // if it is a native window, we assume it handles the scrollbars itself
488 // too - and if it doesn't, there is not much we can do
489 if ( !m_renderer )
490 {
491 wxWindowNative::DoGetClientSize(width, height);
492
493 return;
494 }
495
1e6feb95
VZ
496 int w, h;
497 wxWindowNative::DoGetClientSize(&w, &h);
498
499 // we assume that the scrollbars are positioned correctly (by a previous
500 // call to PositionScrollbars()) here
501
502 wxRect rectBorder;
503 if ( m_renderer )
504 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
505
506 bool inside = m_renderer->AreScrollbarsInsideBorder();
507
508 if ( width )
509 {
510 // in any case, take account of the scrollbar
511 if ( m_scrollbarVert )
512 w -= m_scrollbarVert->GetSize().x;
513
514 // if we don't have scrollbar or if it is outside the border (and not
515 // blended into it), take account of the right border as well
516 if ( !m_scrollbarVert || inside )
517 w -= rectBorder.width;
518
519 // and always account for the left border
520 *width = w - rectBorder.x;
521
522 // we shouldn't return invalid width
523 if ( *width < 0 )
524 *width = 0;
525 }
526
527 if ( height )
528 {
529 if ( m_scrollbarHorz )
530 h -= m_scrollbarHorz->GetSize().y;
531
532 if ( !m_scrollbarHorz || inside )
533 h -= rectBorder.height;
534
535 *height = h - rectBorder.y;
536
537 // we shouldn't return invalid height
538 if ( *height < 0 )
539 *height = 0;
540 }
541}
542
543void wxWindow::DoSetClientSize(int width, int height)
544{
545 // take into account the borders
546 wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder());
547 width += rectBorder.x;
548 height += rectBorder.y;
549
550 // and the scrollbars (as they may be offset into the border, use the
551 // scrollbar position, not size - this supposes that PositionScrollbars()
552 // had been called before)
553 bool inside = m_renderer->AreScrollbarsInsideBorder();
554 wxSize size = GetSize();
555 if ( m_scrollbarVert )
556 width += size.x - m_scrollbarVert->GetPosition().x;
557 if ( !m_scrollbarVert || inside )
558 width += rectBorder.width;
559
560 if ( m_scrollbarHorz )
561 height += size.y - m_scrollbarHorz->GetPosition().y;
562 if ( !m_scrollbarHorz || inside )
563 height += rectBorder.height;
564
565 wxWindowNative::DoSetClientSize(width, height);
566}
567
568wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const
569{
570 wxHitTest ht = wxWindowNative::DoHitTest(x, y);
571 if ( ht == wxHT_WINDOW_INSIDE )
572 {
573 if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x )
574 {
575 // it can still be changed below because it may also be the corner
576 ht = wxHT_WINDOW_VERT_SCROLLBAR;
577 }
578
579 if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y )
580 {
581 ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER
582 : wxHT_WINDOW_HORZ_SCROLLBAR;
583 }
584 }
585
586 return ht;
587}
588
589// ----------------------------------------------------------------------------
590// scrolling: we implement it entirely ourselves except for ScrollWindow()
591// function which is supposed to be (efficiently) implemented by the native
592// window class
593// ----------------------------------------------------------------------------
594
595void wxWindow::RefreshScrollbars()
596{
597 if ( m_scrollbarHorz )
598 m_scrollbarHorz->Refresh();
599
600 if ( m_scrollbarVert )
601 m_scrollbarVert->Refresh();
602}
603
604void wxWindow::PositionScrollbars()
605{
606 // do not use GetClientSize/Rect as it relies on the scrollbars being
607 // correctly positioned
608
609 wxSize size = GetSize();
610 wxBorder border = GetBorder();
611 wxRect rectBorder = m_renderer->GetBorderDimensions(border);
612 bool inside = m_renderer->AreScrollbarsInsideBorder();
613
614 int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0;
615 int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0;
616
617 wxRect rectBar;
618 if ( m_scrollbarVert )
619 {
620 rectBar.x = size.x - width;
621 if ( inside )
622 rectBar.x -= rectBorder.width;
623 rectBar.width = width;
624 rectBar.y = 0;
625 if ( inside )
626 rectBar.y += rectBorder.y;
627 rectBar.height = size.y - height;
628 if ( inside )
629 rectBar.height -= rectBorder.y + rectBorder.height;
630
631 m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
632 }
633
634 if ( m_scrollbarHorz )
635 {
636 rectBar.y = size.y - height;
637 if ( inside )
638 rectBar.y -= rectBorder.height;
639 rectBar.height = height;
640 rectBar.x = 0;
641 if ( inside )
642 rectBar.x += rectBorder.x;
643 rectBar.width = size.x - width;
644 if ( inside )
645 rectBar.width -= rectBorder.x + rectBorder.width;
646
647 m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
648 }
649
650 RefreshScrollbars();
651}
652
653void wxWindow::SetScrollbar(int orient,
654 int pos,
655 int pageSize,
656 int range,
657 bool refresh)
658{
3f393e3d
VZ
659 wxASSERT_MSG( pageSize <= range,
660 _T("page size can't be greater than range") );
661
1e6feb95
VZ
662 bool hasClientSizeChanged = FALSE;
663 wxScrollBar *scrollbar = GetScrollbar(orient);
3f393e3d 664 if ( range && (pageSize < range) )
1e6feb95
VZ
665 {
666 if ( !scrollbar )
667 {
668 // create it
669 scrollbar = new wxScrollBar(this, -1,
670 wxDefaultPosition, wxDefaultSize,
671 orient & wxVERTICAL ? wxSB_VERTICAL
672 : wxSB_HORIZONTAL);
673 if ( orient & wxVERTICAL )
674 m_scrollbarVert = scrollbar;
675 else
676 m_scrollbarHorz = scrollbar;
677
678 // the client area diminished as we created a scrollbar
679 hasClientSizeChanged = TRUE;
680
681 PositionScrollbars();
682 }
683 else if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
684 {
685 // we might have disabled it before
686 scrollbar->Enable();
687 }
688
689 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
690 }
691 else // no range means no scrollbar
692 {
693 if ( scrollbar )
694 {
695 // wxALWAYS_SHOW_SB only applies to the vertical scrollbar
696 if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) )
697 {
698 // just disable the scrollbar
699 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
700 scrollbar->Disable();
701 }
702 else // really remove the scrollbar
703 {
704 delete scrollbar;
705
706 if ( orient & wxVERTICAL )
707 m_scrollbarVert = NULL;
708 else
709 m_scrollbarHorz = NULL;
710
711 // the client area increased as we removed a scrollbar
712 hasClientSizeChanged = TRUE;
713
714 // the size of the remaining scrollbar must be adjusted
715 if ( m_scrollbarHorz || m_scrollbarVert )
716 {
717 PositionScrollbars();
718 }
719 }
720 }
721 }
722
723 // give the window a chance to relayout
724 if ( hasClientSizeChanged )
725 {
726 wxSizeEvent event(GetSize());
727 (void)GetEventHandler()->ProcessEvent(event);
728 }
729}
730
731void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
732{
733 wxScrollBar *scrollbar = GetScrollbar(orient);
734 wxCHECK_RET( scrollbar, _T("no scrollbar to set position for") );
735
736 scrollbar->SetThumbPosition(pos);
7d297414
VZ
737
738 // VZ: I think we can safely ignore this as we always refresh it
739 // automatically whenever the value chanegs
740#if 0
1e6feb95
VZ
741 if ( refresh )
742 Refresh();
7d297414 743#endif
1e6feb95
VZ
744}
745
746int wxWindow::GetScrollPos(int orient) const
747{
748 wxScrollBar *scrollbar = GetScrollbar(orient);
749 return scrollbar ? scrollbar->GetThumbPosition() : 0;
750}
751
752int wxWindow::GetScrollThumb(int orient) const
753{
754 wxScrollBar *scrollbar = GetScrollbar(orient);
755 return scrollbar ? scrollbar->GetThumbSize() : 0;
756}
757
758int wxWindow::GetScrollRange(int orient) const
759{
760 wxScrollBar *scrollbar = GetScrollbar(orient);
761 return scrollbar ? scrollbar->GetRange() : 0;
762}
763
764void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
765{
4125131b
RR
766#ifdef __WXX11__
767
768 wxWindowX11::ScrollWindow( dx, dy, rect );
769
770#else
771
1e6feb95
VZ
772 // before scrolling it, ensure that we don't have any unpainted areas
773 Update();
774
775 wxRect r;
776
777 if ( dx )
778 {
779 r = ScrollNoRefresh(dx, 0, rect);
780 Refresh(TRUE /* erase bkgnd */, &r);
781 }
782
783 if ( dy )
784 {
785 r = ScrollNoRefresh(0, dy, rect);
786 Refresh(TRUE /* erase bkgnd */, &r);
787 }
4125131b
RR
788
789#endif
1e6feb95
VZ
790}
791
792wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal)
793{
794 wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") );
795
796 // the rect to refresh (which we will calculate)
797 wxRect rect;
798
799 if ( !dx && !dy )
800 {
801 // nothing to do
802 return rect;
803 }
804
805 // calculate the part of the window which we can just redraw in the new
806 // location
807 wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize();
808
809 wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"),
810 sizeTotal.x, sizeTotal.y, dx, dy);
811
812 // the initial and end point of the region we move in client coords
813 wxPoint ptSource, ptDest;
814 if ( rectTotal )
815 {
816 ptSource = rectTotal->GetPosition();
817 ptDest = rectTotal->GetPosition();
818 }
819
820 // the size of this region
821 wxSize size;
822 size.x = sizeTotal.x - abs(dx);
823 size.y = sizeTotal.y - abs(dy);
824 if ( size.x <= 0 || size.y <= 0 )
825 {
826 // just redraw everything as nothing of the displayed image will stay
827 wxLogTrace(_T("scroll"), _T("refreshing everything"));
828
829 rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y);
830 }
831 else // move the part which doesn't change to the new location
832 {
833 // note that when we scroll the canvas in some direction we move the
834 // block which doesn't need to be refreshed in the opposite direction
835
836 if ( dx < 0 )
837 {
838 // scroll to the right, move to the left
839 ptSource.x -= dx;
840 }
841 else
842 {
843 // scroll to the left, move to the right
844 ptDest.x += dx;
845 }
846
847 if ( dy < 0 )
848 {
849 // scroll down, move up
850 ptSource.y -= dy;
851 }
852 else
853 {
854 // scroll up, move down
855 ptDest.y += dy;
856 }
857
858#if wxUSE_CARET
859 // we need to hide the caret before moving or it will erase itself at
860 // the wrong (old) location
861 wxCaret *caret = GetCaret();
862 if ( caret )
863 caret->Hide();
864#endif // wxUSE_CARET
865
866 // do move
867 wxClientDC dc(this);
868 wxBitmap bmp(size.x, size.y);
869 wxMemoryDC dcMem;
870 dcMem.SelectObject(bmp);
871
872 dcMem.Blit(wxPoint(0, 0), size, &dc, ptSource
03147cd0 873#if defined(__WXGTK__) && !defined(wxHAS_WORKING_GTK_DC_BLIT)
1e6feb95
VZ
874 + GetClientAreaOrigin()
875#endif // broken wxGTK wxDC::Blit
876 );
877 dc.Blit(ptDest, size, &dcMem, wxPoint(0, 0));
878
879 wxLogTrace(_T("scroll"),
880 _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"),
881 ptSource.x, ptSource.y,
882 size.x, size.y,
883 ptDest.x, ptDest.y);
884
885 // and now repaint the uncovered area
886
887 // FIXME: We repaint the intersection of these rectangles twice - is
888 // it bad? I don't think so as it is rare to scroll the window
889 // diagonally anyhow and so adding extra logic to compute
890 // rectangle intersection is probably not worth the effort
891
892 rect.x = ptSource.x;
893 rect.y = ptSource.y;
894
895 if ( dx )
896 {
897 if ( dx < 0 )
898 {
899 // refresh the area along the right border
900 rect.x += size.x + dx;
901 rect.width = -dx;
902 }
903 else
904 {
905 // refresh the area along the left border
906 rect.width = dx;
907 }
908
909 rect.height = sizeTotal.y;
910
911 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
912 rect.x, rect.y,
913 rect.GetRight() + 1, rect.GetBottom() + 1);
914 }
915
916 if ( dy )
917 {
918 if ( dy < 0 )
919 {
920 // refresh the area along the bottom border
921 rect.y += size.y + dy;
922 rect.height = -dy;
923 }
924 else
925 {
926 // refresh the area along the top border
927 rect.height = dy;
928 }
929
930 rect.width = sizeTotal.x;
931
932 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
933 rect.x, rect.y,
934 rect.GetRight() + 1, rect.GetBottom() + 1);
935 }
936
937#if wxUSE_CARET
938 if ( caret )
939 caret->Show();
940#endif // wxUSE_CARET
941 }
942
943 return rect;
944}
945
1e6feb95
VZ
946// ----------------------------------------------------------------------------
947// accelerators and menu hot keys
948// ----------------------------------------------------------------------------
949
950#if wxUSE_MENUS
951 // the last window over which Alt was pressed (used by OnKeyUp)
952 wxWindow *wxWindow::ms_winLastAltPress = NULL;
953#endif // wxUSE_MENUS
954
955#if wxUSE_ACCEL || wxUSE_MENUS
956
957void wxWindow::OnKeyDown(wxKeyEvent& event)
958{
959#if wxUSE_MENUS
960 int key = event.GetKeyCode();
961 if ( !event.ControlDown() && (key == WXK_MENU || key == WXK_F10) )
962 {
963 ms_winLastAltPress = this;
964
965 // it can't be an accel anyhow
966 return;
967 }
968
969 ms_winLastAltPress = NULL;
970#endif // wxUSE_MENUS
971
972#if wxUSE_ACCEL
973 for ( wxWindow *win = this; win; win = win->GetParent() )
974 {
975 int command = win->GetAcceleratorTable()->GetCommand(event);
976 if ( command != -1 )
977 {
978 wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command);
979 if ( win->GetEventHandler()->ProcessEvent(eventCmd) )
980 {
981 // skip "event.Skip()" below
982 return;
983 }
984 }
985
986 if ( win->IsTopLevel() )
987 {
988 // try the frame menu bar
989#if wxUSE_MENUS
990 wxFrame *frame = wxDynamicCast(win, wxFrame);
991 if ( frame )
992 {
993 wxMenuBar *menubar = frame->GetMenuBar();
994 if ( menubar && menubar->ProcessAccelEvent(event) )
995 {
996 // skip "event.Skip()" below
997 return;
998 }
999 }
1000#endif // wxUSE_MENUS
1001
81762e79
VS
1002 // if it wasn't in a menu, try to find a button
1003 if ( command != -1 )
1004 {
1005 wxWindow* child = win->FindWindow(command);
1006 if ( child && wxDynamicCast(child, wxButton) )
1007 {
1008 wxCommandEvent eventCmd(wxEVT_COMMAND_BUTTON_CLICKED, command);
1009 eventCmd.SetEventObject(child);
1010 if ( child->GetEventHandler()->ProcessEvent(eventCmd) )
1011 {
1012 // skip "event.Skip()" below
1013 return;
1014 }
1015 }
1016 }
1017
1e6feb95
VZ
1018 // don't propagate accels from the child frame to the parent one
1019 break;
1020 }
1021 }
1022#endif // wxUSE_ACCEL
1023
1024 event.Skip();
1025}
1026
1027#endif // wxUSE_ACCEL
1028
1029#if wxUSE_MENUS
1030
1031wxMenuBar *wxWindow::GetParentFrameMenuBar() const
1032{
1033 for ( const wxWindow *win = this; win; win = win->GetParent() )
1034 {
1035 if ( win->IsTopLevel() )
1036 {
1037 wxFrame *frame = wxDynamicCast(win, wxFrame);
1038 if ( frame )
1039 {
1040 return frame->GetMenuBar();
1041 }
1042
1043 // don't look further - we don't want to return the menubar of the
1044 // parent frame
1045 break;
1046 }
1047 }
1048
1049 return NULL;
1050}
1051
1052void wxWindow::OnChar(wxKeyEvent& event)
1053{
1054 if ( event.AltDown() && !event.ControlDown() )
1055 {
1056 int key = event.GetKeyCode();
1057
1058 wxMenuBar *menubar = GetParentFrameMenuBar();
1059 if ( menubar )
1060 {
1061 int item = menubar->FindNextItemForAccel(-1, key);
1062 if ( item != -1 )
1063 {
1064 menubar->PopupMenu((size_t)item);
1065
1066 // skip "event.Skip()" below
1067 return;
1068 }
1069 }
1070 }
1071
1072 event.Skip();
1073}
1074
1075void wxWindow::OnKeyUp(wxKeyEvent& event)
1076{
1077 int key = event.GetKeyCode();
1078 if ( !event.HasModifiers() && (key == WXK_MENU || key == WXK_F10) )
1079 {
1080 // only process Alt release specially if there were no other key
1081 // presses since Alt had been pressed and if both events happened in
1082 // the same window
1083 if ( ms_winLastAltPress == this )
1084 {
1085 wxMenuBar *menubar = GetParentFrameMenuBar();
1086 if ( menubar && this != menubar )
1087 {
1088 menubar->SelectMenu(0);
1089 }
1090 }
1091 }
1092 else
1093 {
1094 event.Skip();
1095 }
1096
1097 // in any case reset it
1098 ms_winLastAltPress = NULL;
1099}
1100
1101#endif // wxUSE_MENUS
1102
30827629
VZ
1103// ----------------------------------------------------------------------------
1104// MSW-specific section
1105// ----------------------------------------------------------------------------
1106
1107#ifdef __WXMSW__
1108
1109#include "wx/msw/private.h"
1110
1111long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1112{
1113 if ( message == WM_NCHITTEST )
1114 {
1115 // the windows which contain the other windows should let the mouse
1116 // events through, otherwise a window inside a static box would
1117 // never get any events at all
1118 if ( IsStaticBox() )
1119 {
1120 return HTTRANSPARENT;
1121 }
1122 }
1123
1124 return wxWindowNative::MSWWindowProc(message, wParam, lParam);
1125}
1126
1127#endif // __WXMSW__
1128