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