Make default keyboard handling available in wxVarScrollHelperEvtHandler too.
[wxWidgets.git] / src / generic / vscroll.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vscroll.cpp
3 // Purpose: wxVScrolledWindow implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Brad Anderson, David Warkentin
6 // Created: 30.05.03
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/dc.h"
28 #include "wx/sizer.h"
29 #endif
30
31 #include "wx/vscroll.h"
32
33 #include "wx/utils.h" // For wxMin/wxMax().
34
35 // ============================================================================
36 // wxVarScrollHelperEvtHandler declaration
37 // ============================================================================
38
39 // ----------------------------------------------------------------------------
40 // wxScrollHelperEvtHandler: intercept the events from the window and forward
41 // them to wxVarScrollHelperBase
42 // ----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxVarScrollHelperEvtHandler : public wxEvtHandler
45 {
46 public:
47 wxVarScrollHelperEvtHandler(wxVarScrollHelperBase *scrollHelper)
48 {
49 m_scrollHelper = scrollHelper;
50 }
51
52 virtual bool ProcessEvent(wxEvent& event);
53
54 private:
55 wxVarScrollHelperBase *m_scrollHelper;
56
57 wxDECLARE_NO_COPY_CLASS(wxVarScrollHelperEvtHandler);
58 };
59
60 // ============================================================================
61 // wxVarScrollHelperEvtHandler implementation
62 // ============================================================================
63
64 // FIXME: This method totally duplicates a method with the same name in
65 // wxScrollHelperEvtHandler, we really should merge them by reusing the
66 // common parts in wxAnyScrollHelperBase.
67 bool wxVarScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
68 {
69 wxEventType evType = event.GetEventType();
70
71 // Pass it on to the real handler: notice that we must not call
72 // ProcessEvent() on this object itself as it wouldn't pass it to the next
73 // handler (i.e. the real window) if we're called from a previous handler
74 // (as indicated by "process here only" flag being set) and we do want to
75 // execute the handler defined in the window we're associated with right
76 // now, without waiting until TryAfter() is called from wxEvtHandler.
77 bool processed = m_nextHandler->ProcessEvent(event);
78
79 // always process the size events ourselves, even if the user code handles
80 // them as well, as we need to AdjustScrollbars()
81 //
82 // NB: it is important to do it after processing the event in the normal
83 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
84 // scrollbar[s] (dis)appear and it should be seen by the user code
85 // after this one
86 if ( evType == wxEVT_SIZE )
87 {
88 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
89 return true;
90 }
91
92 if ( processed && event.IsCommandEvent())
93 return true;
94
95 // For wxEVT_PAINT the user code can either handle this event as usual or
96 // override virtual OnDraw(), so if the event hasn't been handled we need
97 // to call this virtual function ourselves.
98 if (
99 #ifndef __WXUNIVERSAL__
100 // in wxUniversal "processed" will always be true, because
101 // all windows use the paint event to draw themselves.
102 // In this case we can't use this flag to determine if a custom
103 // paint event handler already drew our window and we just
104 // call OnDraw() anyway.
105 !processed &&
106 #endif // !__WXUNIVERSAL__
107 evType == wxEVT_PAINT )
108 {
109 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
110 return true;
111 }
112
113 // reset the skipped flag (which might have been set to true in
114 // ProcessEvent() above) to be able to test it below
115 bool wasSkipped = event.GetSkipped();
116 if ( wasSkipped )
117 event.Skip(false);
118
119 if ( evType == wxEVT_SCROLLWIN_TOP ||
120 evType == wxEVT_SCROLLWIN_BOTTOM ||
121 evType == wxEVT_SCROLLWIN_LINEUP ||
122 evType == wxEVT_SCROLLWIN_LINEDOWN ||
123 evType == wxEVT_SCROLLWIN_PAGEUP ||
124 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
125 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
126 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
127 {
128 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
129 if ( !event.GetSkipped() )
130 {
131 // it makes sense to indicate that we processed the message as we
132 // did scroll the window (and also notice that wxAutoScrollTimer
133 // relies on our return value to stop scrolling when we are at top
134 // or bottom already)
135 processed = true;
136 wasSkipped = false;
137 }
138 }
139 #if wxUSE_MOUSEWHEEL
140 else if ( evType == wxEVT_MOUSEWHEEL )
141 {
142 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
143 }
144 #endif // wxUSE_MOUSEWHEEL
145 else if ( evType == wxEVT_CHAR &&
146 (m_scrollHelper->GetOrientation() == wxVERTICAL) )
147 {
148 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
149 if ( !event.GetSkipped() )
150 {
151 processed = true;
152 wasSkipped = false;
153 }
154 }
155
156 event.Skip(wasSkipped);
157
158 // We called ProcessEvent() on the next handler, meaning that we explicitly
159 // worked around the request to process the event in this handler only. As
160 // explained above, this is unfortunately really necessary but the trouble
161 // is that the event will continue to be post-processed by the previous
162 // handler resulting in duplicate calls to event handlers. Call the special
163 // function below to prevent this from happening, base class DoTryChain()
164 // will check for it and behave accordingly.
165 //
166 // And if we're not called from DoTryChain(), this won't do anything anyhow.
167 event.DidntHonourProcessOnlyIn();
168
169 return processed;
170 }
171
172
173 // ============================================================================
174 // wxVarScrollHelperBase implementation
175 // ============================================================================
176
177 // ----------------------------------------------------------------------------
178 // wxVarScrollHelperBase initialization
179 // ----------------------------------------------------------------------------
180
181 wxVarScrollHelperBase::wxVarScrollHelperBase(wxWindow *win)
182 : wxAnyScrollHelperBase(win)
183 {
184 #if wxUSE_MOUSEWHEEL
185 m_sumWheelRotation = 0;
186 #endif
187
188 m_unitMax = 0;
189 m_sizeTotal = 0;
190 m_unitFirst = 0;
191
192 m_physicalScrolling = true;
193 m_handler = NULL;
194
195 // by default, the associated window is also the target window
196 DoSetTargetWindow(win);
197 }
198
199 wxVarScrollHelperBase::~wxVarScrollHelperBase()
200 {
201 DeleteEvtHandler();
202 }
203
204 // ----------------------------------------------------------------------------
205 // wxVarScrollHelperBase various helpers
206 // ----------------------------------------------------------------------------
207
208 void
209 wxVarScrollHelperBase::AssignOrient(wxCoord& x,
210 wxCoord& y,
211 wxCoord first,
212 wxCoord second)
213 {
214 if ( GetOrientation() == wxVERTICAL )
215 {
216 x = first;
217 y = second;
218 }
219 else // horizontal
220 {
221 x = second;
222 y = first;
223 }
224 }
225
226 void
227 wxVarScrollHelperBase::IncOrient(wxCoord& x, wxCoord& y, wxCoord inc)
228 {
229 if ( GetOrientation() == wxVERTICAL )
230 y += inc;
231 else
232 x += inc;
233 }
234
235 wxCoord wxVarScrollHelperBase::DoEstimateTotalSize() const
236 {
237 // estimate the total height: it is impossible to call
238 // OnGetUnitSize() for every unit because there may be too many of
239 // them, so we just make a guess using some units in the beginning,
240 // some in the end and some in the middle
241 static const size_t NUM_UNITS_TO_SAMPLE = 10;
242
243 wxCoord sizeTotal;
244 if ( m_unitMax < 3*NUM_UNITS_TO_SAMPLE )
245 {
246 // in this case, full calculations are faster and more correct than
247 // guessing
248 sizeTotal = GetUnitsSize(0, m_unitMax);
249 }
250 else // too many units to calculate exactly
251 {
252 // look at some units in the beginning/middle/end
253 sizeTotal =
254 GetUnitsSize(0, NUM_UNITS_TO_SAMPLE) +
255 GetUnitsSize(m_unitMax - NUM_UNITS_TO_SAMPLE,
256 m_unitMax) +
257 GetUnitsSize(m_unitMax/2 - NUM_UNITS_TO_SAMPLE/2,
258 m_unitMax/2 + NUM_UNITS_TO_SAMPLE/2);
259
260 // use the height of the units we looked as the average
261 sizeTotal = (wxCoord)
262 (((float)sizeTotal / (3*NUM_UNITS_TO_SAMPLE)) * m_unitMax);
263 }
264
265 return sizeTotal;
266 }
267
268 wxCoord wxVarScrollHelperBase::GetUnitsSize(size_t unitMin, size_t unitMax) const
269 {
270 if ( unitMin == unitMax )
271 return 0;
272 else if ( unitMin > unitMax )
273 return -GetUnitsSize(unitMax, unitMin);
274 //else: unitMin < unitMax
275
276 // let the user code know that we're going to need all these units
277 OnGetUnitsSizeHint(unitMin, unitMax);
278
279 // sum up their sizes
280 wxCoord size = 0;
281 for ( size_t unit = unitMin; unit < unitMax; ++unit )
282 {
283 size += OnGetUnitSize(unit);
284 }
285
286 return size;
287 }
288
289 size_t wxVarScrollHelperBase::FindFirstVisibleFromLast(size_t unitLast, bool full) const
290 {
291 const wxCoord sWindow = GetOrientationTargetSize();
292
293 // go upwards until we arrive at a unit such that unitLast is not visible
294 // any more when it is shown
295 size_t unitFirst = unitLast;
296 wxCoord s = 0;
297 for ( ;; )
298 {
299 s += OnGetUnitSize(unitFirst);
300
301 if ( s > sWindow )
302 {
303 // for this unit to be fully visible we need to go one unit
304 // down, but if it is enough for it to be only partly visible then
305 // this unit will do as well
306 if ( full )
307 {
308 ++unitFirst;
309 }
310
311 break;
312 }
313
314 if ( !unitFirst )
315 break;
316
317 --unitFirst;
318 }
319
320 return unitFirst;
321 }
322
323 size_t wxVarScrollHelperBase::GetNewScrollPosition(wxScrollWinEvent& event) const
324 {
325 wxEventType evtType = event.GetEventType();
326
327 if ( evtType == wxEVT_SCROLLWIN_TOP )
328 {
329 return 0;
330 }
331 else if ( evtType == wxEVT_SCROLLWIN_BOTTOM )
332 {
333 return m_unitMax;
334 }
335 else if ( evtType == wxEVT_SCROLLWIN_LINEUP )
336 {
337 return m_unitFirst ? m_unitFirst - 1 : 0;
338 }
339 else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN )
340 {
341 return m_unitFirst + 1;
342 }
343 else if ( evtType == wxEVT_SCROLLWIN_PAGEUP )
344 {
345 // Page up should do at least as much as line up.
346 return wxMin(FindFirstVisibleFromLast(m_unitFirst),
347 m_unitFirst ? m_unitFirst - 1 : 0);
348 }
349 else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN )
350 {
351 // And page down should do at least as much as line down.
352 if ( GetVisibleEnd() )
353 return wxMax(GetVisibleEnd() - 1, m_unitFirst + 1);
354 else
355 return wxMax(GetVisibleEnd(), m_unitFirst + 1);
356 }
357 else if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE )
358 {
359 return event.GetPosition();
360 }
361 else if ( evtType == wxEVT_SCROLLWIN_THUMBTRACK )
362 {
363 return event.GetPosition();
364 }
365
366 // unknown scroll event?
367 wxFAIL_MSG( wxT("unknown scroll event type?") );
368 return 0;
369 }
370
371 void wxVarScrollHelperBase::UpdateScrollbar()
372 {
373 // if there is nothing to scroll, remove the scrollbar
374 if ( !m_unitMax )
375 {
376 RemoveScrollbar();
377 return;
378 }
379
380 // see how many units can we fit on screen
381 const wxCoord sWindow = GetOrientationTargetSize();
382
383 // do vertical calculations
384 wxCoord s = 0;
385 size_t unit;
386 for ( unit = m_unitFirst; unit < m_unitMax; ++unit )
387 {
388 if ( s > sWindow )
389 break;
390
391 s += OnGetUnitSize(unit);
392 }
393
394 m_nUnitsVisible = unit - m_unitFirst;
395
396 int unitsPageSize = m_nUnitsVisible;
397 if ( s > sWindow )
398 {
399 // last unit is only partially visible, we still need the scrollbar and
400 // so we have to "fix" pageSize because if it is equal to m_unitMax
401 // the scrollbar is not shown at all under MSW
402 --unitsPageSize;
403 }
404
405 // set the scrollbar parameters to reflect this
406 m_win->SetScrollbar(GetOrientation(), m_unitFirst, unitsPageSize, m_unitMax);
407 }
408
409 void wxVarScrollHelperBase::RemoveScrollbar()
410 {
411 m_unitFirst = 0;
412 m_nUnitsVisible = m_unitMax;
413 m_win->SetScrollbar(GetOrientation(), 0, 0, 0);
414 }
415
416 void wxVarScrollHelperBase::DeleteEvtHandler()
417 {
418 // search for m_handler in the handler list
419 if ( m_win && m_handler )
420 {
421 if ( m_win->RemoveEventHandler(m_handler) )
422 {
423 delete m_handler;
424 }
425 //else: something is very wrong, so better [maybe] leak memory than
426 // risk a crash because of double deletion
427
428 m_handler = NULL;
429 }
430 }
431
432 void wxVarScrollHelperBase::DoSetTargetWindow(wxWindow *target)
433 {
434 m_targetWindow = target;
435 #ifdef __WXMAC__
436 target->MacSetClipChildren( true ) ;
437 #endif
438
439 // install the event handler which will intercept the events we're
440 // interested in (but only do it for our real window, not the target window
441 // which we scroll - we don't need to hijack its events)
442 if ( m_targetWindow == m_win )
443 {
444 // if we already have a handler, delete it first
445 DeleteEvtHandler();
446
447 m_handler = new wxVarScrollHelperEvtHandler(this);
448 m_targetWindow->PushEventHandler(m_handler);
449 }
450 }
451
452 // ----------------------------------------------------------------------------
453 // wxVarScrollHelperBase operations
454 // ----------------------------------------------------------------------------
455
456 void wxVarScrollHelperBase::SetTargetWindow(wxWindow *target)
457 {
458 wxCHECK_RET( target, wxT("target window must not be NULL") );
459
460 if ( target == m_targetWindow )
461 return;
462
463 DoSetTargetWindow(target);
464 }
465
466 void wxVarScrollHelperBase::SetUnitCount(size_t count)
467 {
468 // save the number of units
469 m_unitMax = count;
470
471 // and our estimate for their total height
472 m_sizeTotal = EstimateTotalSize();
473
474 // ScrollToUnit() will update the scrollbar itself if it changes the unit
475 // we pass to it because it's out of [new] range
476 size_t oldScrollPos = m_unitFirst;
477 DoScrollToUnit(m_unitFirst);
478 if ( oldScrollPos == m_unitFirst )
479 {
480 // but if it didn't do it, we still need to update the scrollbar to
481 // reflect the changed number of units ourselves
482 UpdateScrollbar();
483 }
484 }
485
486 void wxVarScrollHelperBase::RefreshUnit(size_t unit)
487 {
488 // is this unit visible?
489 if ( !IsVisible(unit) )
490 {
491 // no, it is useless to do anything
492 return;
493 }
494
495 // calculate the rect occupied by this unit on screen
496 wxRect rect;
497 AssignOrient(rect.width, rect.height,
498 GetNonOrientationTargetSize(), OnGetUnitSize(unit));
499
500 for ( size_t n = GetVisibleBegin(); n < unit; ++n )
501 {
502 IncOrient(rect.x, rect.y, OnGetUnitSize(n));
503 }
504
505 // do refresh it
506 m_targetWindow->RefreshRect(rect);
507 }
508
509 void wxVarScrollHelperBase::RefreshUnits(size_t from, size_t to)
510 {
511 wxASSERT_MSG( from <= to, wxT("RefreshUnits(): empty range") );
512
513 // clump the range to just the visible units -- it is useless to refresh
514 // the other ones
515 if ( from < GetVisibleBegin() )
516 from = GetVisibleBegin();
517
518 if ( to > GetVisibleEnd() )
519 to = GetVisibleEnd();
520
521 // calculate the rect occupied by these units on screen
522 int orient_size = 0,
523 orient_pos = 0;
524
525 int nonorient_size = GetNonOrientationTargetSize();
526
527 for ( size_t nBefore = GetVisibleBegin();
528 nBefore < from;
529 nBefore++ )
530 {
531 orient_pos += OnGetUnitSize(nBefore);
532 }
533
534 for ( size_t nBetween = from; nBetween <= to; nBetween++ )
535 {
536 orient_size += OnGetUnitSize(nBetween);
537 }
538
539 wxRect rect;
540 AssignOrient(rect.x, rect.y, 0, orient_pos);
541 AssignOrient(rect.width, rect.height, nonorient_size, orient_size);
542
543 // do refresh it
544 m_targetWindow->RefreshRect(rect);
545 }
546
547 void wxVarScrollHelperBase::RefreshAll()
548 {
549 UpdateScrollbar();
550
551 m_targetWindow->Refresh();
552 }
553
554 bool wxVarScrollHelperBase::ScrollLayout()
555 {
556 if ( m_targetWindow->GetSizer() && m_physicalScrolling )
557 {
558 // adjust the sizer dimensions/position taking into account the
559 // virtual size and scrolled position of the window.
560
561 int x, y;
562 AssignOrient(x, y, 0, -GetScrollOffset());
563
564 int w, h;
565 m_targetWindow->GetVirtualSize(&w, &h);
566
567 m_targetWindow->GetSizer()->SetDimension(x, y, w, h);
568 return true;
569 }
570
571 // fall back to default for LayoutConstraints
572 return m_targetWindow->wxWindow::Layout();
573 }
574
575 int wxVarScrollHelperBase::VirtualHitTest(wxCoord coord) const
576 {
577 const size_t unitMax = GetVisibleEnd();
578 for ( size_t unit = GetVisibleBegin(); unit < unitMax; ++unit )
579 {
580 coord -= OnGetUnitSize(unit);
581 if ( coord < 0 )
582 return unit;
583 }
584
585 return wxNOT_FOUND;
586 }
587
588 // ----------------------------------------------------------------------------
589 // wxVarScrollHelperBase scrolling
590 // ----------------------------------------------------------------------------
591
592 bool wxVarScrollHelperBase::DoScrollToUnit(size_t unit)
593 {
594 if ( !m_unitMax )
595 {
596 // we're empty, code below doesn't make sense in this case
597 return false;
598 }
599
600 // determine the real first unit to scroll to: we shouldn't scroll beyond
601 // the end
602 size_t unitFirstLast = FindFirstVisibleFromLast(m_unitMax - 1, true);
603 if ( unit > unitFirstLast )
604 unit = unitFirstLast;
605
606 // anything to do?
607 if ( unit == m_unitFirst )
608 {
609 // no
610 return false;
611 }
612
613
614 // remember the currently shown units for the refresh code below
615 size_t unitFirstOld = GetVisibleBegin(),
616 unitLastOld = GetVisibleEnd();
617
618 m_unitFirst = unit;
619
620
621 // the size of scrollbar thumb could have changed
622 UpdateScrollbar();
623
624 // finally refresh the display -- but only redraw as few units as possible
625 // to avoid flicker. We can't do this if we have children because they
626 // won't be scrolled
627 if ( m_targetWindow->GetChildren().empty() &&
628 (GetVisibleBegin() >= unitLastOld || GetVisibleEnd() <= unitFirstOld) )
629 {
630 // the simplest case: we don't have any old units left, just redraw
631 // everything
632 m_targetWindow->Refresh();
633 }
634 else // scroll the window
635 {
636 // Avoid scrolling visible parts of the screen on Mac
637 #ifdef __WXMAC__
638 if (m_physicalScrolling && m_targetWindow->IsShownOnScreen())
639 #else
640 if ( m_physicalScrolling )
641 #endif
642 {
643 wxCoord dx = 0,
644 dy = GetUnitsSize(GetVisibleBegin(), unitFirstOld);
645
646 if ( GetOrientation() == wxHORIZONTAL )
647 {
648 wxCoord tmp = dx;
649 dx = dy;
650 dy = tmp;
651 }
652
653 m_targetWindow->ScrollWindow(dx, dy);
654 }
655 else // !m_physicalScrolling
656 {
657 // we still need to invalidate but we can't use ScrollWindow
658 // because physical scrolling is disabled (the user either didn't
659 // want children scrolled and/or doesn't want pixels to be
660 // physically scrolled).
661 m_targetWindow->Refresh();
662 }
663 }
664
665 return true;
666 }
667
668 bool wxVarScrollHelperBase::DoScrollUnits(int units)
669 {
670 units += m_unitFirst;
671 if ( units < 0 )
672 units = 0;
673
674 return DoScrollToUnit(units);
675 }
676
677 bool wxVarScrollHelperBase::DoScrollPages(int pages)
678 {
679 bool didSomething = false;
680
681 while ( pages )
682 {
683 int unit;
684 if ( pages > 0 )
685 {
686 unit = GetVisibleEnd();
687 if ( unit )
688 --unit;
689 --pages;
690 }
691 else // pages < 0
692 {
693 unit = FindFirstVisibleFromLast(GetVisibleEnd());
694 ++pages;
695 }
696
697 didSomething = DoScrollToUnit(unit);
698 }
699
700 return didSomething;
701 }
702
703 // ----------------------------------------------------------------------------
704 // event handling
705 // ----------------------------------------------------------------------------
706
707 void wxVarScrollHelperBase::HandleOnSize(wxSizeEvent& event)
708 {
709 if ( m_unitMax )
710 {
711 // sometimes change in varscrollable window's size can result in
712 // unused empty space after the last item. Fix it by decrementing
713 // first visible item position according to the available space.
714
715 // determine free space
716 const wxCoord sWindow = GetOrientationTargetSize();
717 wxCoord s = 0;
718 size_t unit;
719 for ( unit = m_unitFirst; unit < m_unitMax; ++unit )
720 {
721 if ( s > sWindow )
722 break;
723
724 s += OnGetUnitSize(unit);
725 }
726 wxCoord freeSpace = sWindow - s;
727
728 // decrement first visible item index as long as there is free space
729 size_t idealUnitFirst;
730 for ( idealUnitFirst = m_unitFirst;
731 idealUnitFirst > 0;
732 idealUnitFirst-- )
733 {
734 wxCoord us = OnGetUnitSize(idealUnitFirst-1);
735 if ( freeSpace < us )
736 break;
737 freeSpace -= us;
738 }
739 m_unitFirst = idealUnitFirst;
740 }
741
742 UpdateScrollbar();
743
744 event.Skip();
745 }
746
747 void wxVarScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
748 {
749 if (GetOrientation() != event.GetOrientation())
750 {
751 event.Skip();
752 return;
753 }
754
755 DoScrollToUnit(GetNewScrollPosition(event));
756
757 #ifdef __WXMAC__
758 UpdateMacScrollWindow();
759 #endif // __WXMAC__
760 }
761
762 void wxVarScrollHelperBase::DoPrepareDC(wxDC& dc)
763 {
764 if ( m_physicalScrolling )
765 {
766 wxPoint pt = dc.GetDeviceOrigin();
767
768 IncOrient(pt.x, pt.y, -GetScrollOffset());
769
770 dc.SetDeviceOrigin(pt.x, pt.y);
771 }
772 }
773
774 int wxVarScrollHelperBase::DoCalcScrolledPosition(int coord) const
775 {
776 return coord - GetScrollOffset();
777 }
778
779 int wxVarScrollHelperBase::DoCalcUnscrolledPosition(int coord) const
780 {
781 return coord + GetScrollOffset();
782 }
783
784 #if wxUSE_MOUSEWHEEL
785
786 void wxVarScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
787 {
788 // we only want to process wheel events for vertical implementations.
789 // There is no way to determine wheel orientation (and on MSW horizontal
790 // wheel rotation just fakes scroll events, rather than sending a MOUSEWHEEL
791 // event).
792 if ( GetOrientation() != wxVERTICAL )
793 return;
794
795 m_sumWheelRotation += event.GetWheelRotation();
796 int delta = event.GetWheelDelta();
797
798 // how much to scroll this time
799 int units_to_scroll = -(m_sumWheelRotation/delta);
800 if ( !units_to_scroll )
801 return;
802
803 m_sumWheelRotation += units_to_scroll*delta;
804
805 if ( !event.IsPageScroll() )
806 DoScrollUnits( units_to_scroll*event.GetLinesPerAction() );
807 else // scroll pages instead of units
808 DoScrollPages( units_to_scroll );
809 }
810
811 #endif // wxUSE_MOUSEWHEEL
812
813
814 // ============================================================================
815 // wxVarHVScrollHelper implementation
816 // ============================================================================
817
818 // ----------------------------------------------------------------------------
819 // wxVarHVScrollHelper operations
820 // ----------------------------------------------------------------------------
821
822 void wxVarHVScrollHelper::SetRowColumnCount(size_t rowCount, size_t columnCount)
823 {
824 SetRowCount(rowCount);
825 SetColumnCount(columnCount);
826 }
827
828 bool wxVarHVScrollHelper::ScrollToRowColumn(size_t row, size_t column)
829 {
830 bool result = false;
831 result |= ScrollToRow(row);
832 result |= ScrollToColumn(column);
833 return result;
834 }
835
836 void wxVarHVScrollHelper::RefreshRowColumn(size_t row, size_t column)
837 {
838 // is this unit visible?
839 if ( !IsRowVisible(row) || !IsColumnVisible(column) )
840 {
841 // no, it is useless to do anything
842 return;
843 }
844
845 // calculate the rect occupied by this cell on screen
846 wxRect v_rect, h_rect;
847 v_rect.height = OnGetRowHeight(row);
848 h_rect.width = OnGetColumnWidth(column);
849
850 size_t n;
851
852 for ( n = GetVisibleRowsBegin(); n < row; n++ )
853 {
854 v_rect.y += OnGetRowHeight(n);
855 }
856
857 for ( n = GetVisibleColumnsBegin(); n < column; n++ )
858 {
859 h_rect.x += OnGetColumnWidth(n);
860 }
861
862 // refresh but specialize the behaviour if we have a single target window
863 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
864 {
865 v_rect.x = h_rect.x;
866 v_rect.width = h_rect.width;
867 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
868 }
869 else
870 {
871 v_rect.x = 0;
872 v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize();
873 h_rect.y = 0;
874 h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize();
875
876 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
877 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect);
878 }
879 }
880
881 void wxVarHVScrollHelper::RefreshRowsColumns(size_t fromRow, size_t toRow,
882 size_t fromColumn, size_t toColumn)
883 {
884 wxASSERT_MSG( fromRow <= toRow || fromColumn <= toColumn,
885 wxT("RefreshRowsColumns(): empty range") );
886
887 // clump the range to just the visible units -- it is useless to refresh
888 // the other ones
889 if ( fromRow < GetVisibleRowsBegin() )
890 fromRow = GetVisibleRowsBegin();
891
892 if ( toRow > GetVisibleRowsEnd() )
893 toRow = GetVisibleRowsEnd();
894
895 if ( fromColumn < GetVisibleColumnsBegin() )
896 fromColumn = GetVisibleColumnsBegin();
897
898 if ( toColumn > GetVisibleColumnsEnd() )
899 toColumn = GetVisibleColumnsEnd();
900
901 // calculate the rect occupied by these units on screen
902 wxRect v_rect, h_rect;
903 size_t nBefore, nBetween;
904
905 for ( nBefore = GetVisibleRowsBegin();
906 nBefore < fromRow;
907 nBefore++ )
908 {
909 v_rect.y += OnGetRowHeight(nBefore);
910 }
911
912 for ( nBetween = fromRow; nBetween <= toRow; nBetween++ )
913 {
914 v_rect.height += OnGetRowHeight(nBetween);
915 }
916
917 for ( nBefore = GetVisibleColumnsBegin();
918 nBefore < fromColumn;
919 nBefore++ )
920 {
921 h_rect.x += OnGetColumnWidth(nBefore);
922 }
923
924 for ( nBetween = fromColumn; nBetween <= toColumn; nBetween++ )
925 {
926 h_rect.width += OnGetColumnWidth(nBetween);
927 }
928
929 // refresh but specialize the behaviour if we have a single target window
930 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
931 {
932 v_rect.x = h_rect.x;
933 v_rect.width = h_rect.width;
934 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
935 }
936 else
937 {
938 v_rect.x = 0;
939 v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize();
940 h_rect.y = 0;
941 h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize();
942
943 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
944 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect);
945 }
946 }
947
948 wxPosition wxVarHVScrollHelper::VirtualHitTest(wxCoord x, wxCoord y) const
949 {
950 return wxPosition(wxVarVScrollHelper::VirtualHitTest(y),
951 wxVarHScrollHelper::VirtualHitTest(x));
952 }
953
954 void wxVarHVScrollHelper::DoPrepareDC(wxDC& dc)
955 {
956 wxVarVScrollHelper::DoPrepareDC(dc);
957 wxVarHScrollHelper::DoPrepareDC(dc);
958 }
959
960 bool wxVarHVScrollHelper::ScrollLayout()
961 {
962 bool layout_result = false;
963 layout_result |= wxVarVScrollHelper::ScrollLayout();
964 layout_result |= wxVarHScrollHelper::ScrollLayout();
965 return layout_result;
966 }
967
968 wxSize wxVarHVScrollHelper::GetRowColumnCount() const
969 {
970 return wxSize(GetColumnCount(), GetRowCount());
971 }
972
973 wxPosition wxVarHVScrollHelper::GetVisibleBegin() const
974 {
975 return wxPosition(GetVisibleRowsBegin(), GetVisibleColumnsBegin());
976 }
977
978 wxPosition wxVarHVScrollHelper::GetVisibleEnd() const
979 {
980 return wxPosition(GetVisibleRowsEnd(), GetVisibleColumnsEnd());
981 }
982
983 bool wxVarHVScrollHelper::IsVisible(size_t row, size_t column) const
984 {
985 return IsRowVisible(row) && IsColumnVisible(column);
986 }
987
988
989 // ============================================================================
990 // wx[V/H/HV]ScrolledWindow implementations
991 // ============================================================================
992
993 IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel)
994 IMPLEMENT_ABSTRACT_CLASS(wxHScrolledWindow, wxPanel)
995 IMPLEMENT_ABSTRACT_CLASS(wxHVScrolledWindow, wxPanel)
996
997
998 #if WXWIN_COMPATIBILITY_2_8
999
1000 // ===========================================================================
1001 // wxVarVScrollLegacyAdaptor
1002 // ===========================================================================
1003
1004 size_t wxVarVScrollLegacyAdaptor::GetFirstVisibleLine() const
1005 { return GetVisibleRowsBegin(); }
1006
1007 size_t wxVarVScrollLegacyAdaptor::GetLastVisibleLine() const
1008 { return GetVisibleRowsEnd() - 1; }
1009
1010 size_t wxVarVScrollLegacyAdaptor::GetLineCount() const
1011 { return GetRowCount(); }
1012
1013 void wxVarVScrollLegacyAdaptor::SetLineCount(size_t count)
1014 { SetRowCount(count); }
1015
1016 void wxVarVScrollLegacyAdaptor::RefreshLine(size_t line)
1017 { RefreshRow(line); }
1018
1019 void wxVarVScrollLegacyAdaptor::RefreshLines(size_t from, size_t to)
1020 { RefreshRows(from, to); }
1021
1022 bool wxVarVScrollLegacyAdaptor::ScrollToLine(size_t line)
1023 { return ScrollToRow(line); }
1024
1025 bool wxVarVScrollLegacyAdaptor::ScrollLines(int lines)
1026 { return ScrollRows(lines); }
1027
1028 bool wxVarVScrollLegacyAdaptor::ScrollPages(int pages)
1029 { return ScrollRowPages(pages); }
1030
1031 wxCoord wxVarVScrollLegacyAdaptor::OnGetLineHeight(size_t WXUNUSED(n)) const
1032 {
1033 wxFAIL_MSG( wxT("OnGetLineHeight() must be overridden if OnGetRowHeight() isn't!") );
1034 return -1;
1035 }
1036
1037 void wxVarVScrollLegacyAdaptor::OnGetLinesHint(size_t WXUNUSED(lineMin),
1038 size_t WXUNUSED(lineMax)) const
1039 {
1040 }
1041
1042 wxCoord wxVarVScrollLegacyAdaptor::OnGetRowHeight(size_t n) const
1043 {
1044 return OnGetLineHeight(n);
1045 }
1046
1047 void wxVarVScrollLegacyAdaptor::OnGetRowsHeightHint(size_t rowMin,
1048 size_t rowMax) const
1049 {
1050 OnGetLinesHint(rowMin, rowMax);
1051 }
1052
1053 #endif // WXWIN_COMPATIBILITY_2_8