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