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