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