]> git.saurik.com Git - wxWidgets.git/blame - src/generic/vscroll.cpp
updated setup.h for OpenVMS
[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
7d0cf3f9
VZ
34#include "wx/utils.h" // For wxMin/wxMax().
35
f18eaf26
VZ
36// ============================================================================
37// wxVarScrollHelperEvtHandler declaration
38// ============================================================================
39
cf7d6329 40// ----------------------------------------------------------------------------
f18eaf26
VZ
41// wxScrollHelperEvtHandler: intercept the events from the window and forward
42// them to wxVarScrollHelperBase
cf7d6329
VZ
43// ----------------------------------------------------------------------------
44
f18eaf26
VZ
45class WXDLLEXPORT wxVarScrollHelperEvtHandler : public wxEvtHandler
46{
47public:
48 wxVarScrollHelperEvtHandler(wxVarScrollHelperBase *scrollHelper)
49 {
50 m_scrollHelper = scrollHelper;
51 }
cf7d6329 52
f18eaf26
VZ
53 virtual bool ProcessEvent(wxEvent& event);
54
55private:
56 wxVarScrollHelperBase *m_scrollHelper;
57
c0c133e1 58 wxDECLARE_NO_COPY_CLASS(wxVarScrollHelperEvtHandler);
f18eaf26 59};
cf7d6329
VZ
60
61// ============================================================================
f18eaf26 62// wxVarScrollHelperEvtHandler implementation
cf7d6329
VZ
63// ============================================================================
64
f18eaf26
VZ
65bool wxVarScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
66{
67 wxEventType evType = event.GetEventType();
68
69 // pass it on to the real handler
70 bool processed = wxEvtHandler::ProcessEvent(event);
71
72 // always process the size events ourselves, even if the user code handles
73 // them as well, as we need to AdjustScrollbars()
74 //
75 // NB: it is important to do it after processing the event in the normal
76 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
77 // scrollbar[s] (dis)appear and it should be seen by the user code
78 // after this one
79 if ( evType == wxEVT_SIZE )
80 {
81 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
82
83 return !event.GetSkipped();
84 }
85
86 if ( processed )
87 {
88 // normally, nothing more to do here - except if we have a command
89 // event
90 if ( event.IsCommandEvent() )
91 {
92 return true;
93 }
94 }
95
96 // reset the skipped flag (which might have been set to true in
97 // ProcessEvent() above) to be able to test it below
98 bool wasSkipped = event.GetSkipped();
99 if ( wasSkipped )
100 event.Skip(false);
101
f18eaf26
VZ
102 if ( evType == wxEVT_SCROLLWIN_TOP ||
103 evType == wxEVT_SCROLLWIN_BOTTOM ||
104 evType == wxEVT_SCROLLWIN_LINEUP ||
105 evType == wxEVT_SCROLLWIN_LINEDOWN ||
106 evType == wxEVT_SCROLLWIN_PAGEUP ||
107 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
108 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
109 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
110 {
111 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
112 if ( !event.GetSkipped() )
113 {
114 // it makes sense to indicate that we processed the message as we
115 // did scroll the window (and also notice that wxAutoScrollTimer
116 // relies on our return value for continuous scrolling)
117 processed = true;
118 wasSkipped = false;
119 }
120 }
121#if wxUSE_MOUSEWHEEL
122 else if ( evType == wxEVT_MOUSEWHEEL )
123 {
124 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
125 }
126#endif // wxUSE_MOUSEWHEEL
127
d4a58a7c 128 event.Skip(wasSkipped);
f18eaf26
VZ
129
130 return processed;
131}
132
133
134// ============================================================================
135// wxVarScrollHelperBase implementation
136// ============================================================================
0c8392ca 137
cf7d6329 138// ----------------------------------------------------------------------------
f18eaf26 139// wxVarScrollHelperBase initialization
cf7d6329
VZ
140// ----------------------------------------------------------------------------
141
f18eaf26 142wxVarScrollHelperBase::wxVarScrollHelperBase(wxWindow *win)
cf7d6329 143{
9a83f860 144 wxASSERT_MSG( win, wxT("associated window can't be NULL in wxVarScrollHelperBase") );
4719e58d
RD
145
146#if wxUSE_MOUSEWHEEL
147 m_sumWheelRotation = 0;
148#endif
f18eaf26
VZ
149
150 m_unitMax = 0;
151 m_sizeTotal = 0;
152 m_unitFirst = 0;
153
154 m_win =
d3b9f782 155 m_targetWindow = NULL;
f18eaf26 156
9845ffce 157 m_physicalScrolling = true;
f18eaf26
VZ
158 m_handler = NULL;
159
160 m_win = win;
161
162 // by default, the associated window is also the target window
163 DoSetTargetWindow(win);
164
165}
166
167wxVarScrollHelperBase::~wxVarScrollHelperBase()
168{
169 DeleteEvtHandler();
cf7d6329
VZ
170}
171
172// ----------------------------------------------------------------------------
f18eaf26 173// wxVarScrollHelperBase various helpers
cf7d6329
VZ
174// ----------------------------------------------------------------------------
175
f18eaf26
VZ
176void
177wxVarScrollHelperBase::AssignOrient(wxCoord& x,
178 wxCoord& y,
179 wxCoord first,
180 wxCoord second)
181{
182 if ( GetOrientation() == wxVERTICAL )
183 {
184 x = first;
185 y = second;
186 }
187 else // horizontal
188 {
189 x = second;
190 y = first;
191 }
192}
193
194void
195wxVarScrollHelperBase::IncOrient(wxCoord& x, wxCoord& y, wxCoord inc)
196{
197 if ( GetOrientation() == wxVERTICAL )
198 y += inc;
199 else
200 x += inc;
201}
202
203wxCoord wxVarScrollHelperBase::DoEstimateTotalSize() const
1e0af0bc
VZ
204{
205 // estimate the total height: it is impossible to call
f18eaf26
VZ
206 // OnGetUnitSize() for every unit because there may be too many of
207 // them, so we just make a guess using some units in the beginning,
1e0af0bc 208 // some in the end and some in the middle
f18eaf26 209 static const size_t NUM_UNITS_TO_SAMPLE = 10;
1e0af0bc 210
f18eaf26
VZ
211 wxCoord sizeTotal;
212 if ( m_unitMax < 3*NUM_UNITS_TO_SAMPLE )
1e0af0bc 213 {
f18eaf26 214 // in this case, full calculations are faster and more correct than
1e0af0bc 215 // guessing
f18eaf26 216 sizeTotal = GetUnitsSize(0, m_unitMax);
1e0af0bc 217 }
f18eaf26 218 else // too many units to calculate exactly
1e0af0bc 219 {
f18eaf26
VZ
220 // look at some units in the beginning/middle/end
221 sizeTotal =
222 GetUnitsSize(0, NUM_UNITS_TO_SAMPLE) +
223 GetUnitsSize(m_unitMax - NUM_UNITS_TO_SAMPLE,
224 m_unitMax) +
225 GetUnitsSize(m_unitMax/2 - NUM_UNITS_TO_SAMPLE/2,
226 m_unitMax/2 + NUM_UNITS_TO_SAMPLE/2);
227
228 // use the height of the units we looked as the average
229 sizeTotal = (wxCoord)
230 (((float)sizeTotal / (3*NUM_UNITS_TO_SAMPLE)) * m_unitMax);
1e0af0bc
VZ
231 }
232
f18eaf26 233 return sizeTotal;
1e0af0bc
VZ
234}
235
f18eaf26 236wxCoord wxVarScrollHelperBase::GetUnitsSize(size_t unitMin, size_t unitMax) const
cf7d6329 237{
f18eaf26 238 if ( unitMin == unitMax )
cf7d6329 239 return 0;
f18eaf26
VZ
240 else if ( unitMin > unitMax )
241 return -GetUnitsSize(unitMax, unitMin);
242 //else: unitMin < unitMax
cf7d6329 243
f18eaf26
VZ
244 // let the user code know that we're going to need all these units
245 OnGetUnitsSizeHint(unitMin, unitMax);
cf7d6329 246
f18eaf26
VZ
247 // sum up their sizes
248 wxCoord size = 0;
249 for ( size_t unit = unitMin; unit < unitMax; ++unit )
cf7d6329 250 {
f18eaf26 251 size += OnGetUnitSize(unit);
cf7d6329
VZ
252 }
253
f18eaf26 254 return size;
cf7d6329
VZ
255}
256
f18eaf26 257size_t wxVarScrollHelperBase::FindFirstVisibleFromLast(size_t unitLast, bool full) const
cf7d6329 258{
f18eaf26 259 const wxCoord sWindow = GetOrientationTargetSize();
cf7d6329 260
f18eaf26 261 // go upwards until we arrive at a unit such that unitLast is not visible
cf7d6329 262 // any more when it is shown
f18eaf26
VZ
263 size_t unitFirst = unitLast;
264 wxCoord s = 0;
cf7d6329
VZ
265 for ( ;; )
266 {
f18eaf26 267 s += OnGetUnitSize(unitFirst);
cf7d6329 268
f18eaf26 269 if ( s > sWindow )
cf7d6329 270 {
f18eaf26 271 // for this unit to be fully visible we need to go one unit
0b49ccf8 272 // down, but if it is enough for it to be only partly visible then
f18eaf26 273 // this unit will do as well
0b49ccf8
VZ
274 if ( full )
275 {
f18eaf26 276 ++unitFirst;
0b49ccf8 277 }
cf7d6329
VZ
278
279 break;
280 }
281
f18eaf26 282 if ( !unitFirst )
cf7d6329
VZ
283 break;
284
f18eaf26 285 --unitFirst;
cf7d6329
VZ
286 }
287
f18eaf26 288 return unitFirst;
cf7d6329
VZ
289}
290
f18eaf26 291size_t wxVarScrollHelperBase::GetNewScrollPosition(wxScrollWinEvent& event) const
c1aa5517 292{
f18eaf26
VZ
293 wxEventType evtType = event.GetEventType();
294
295 if ( evtType == wxEVT_SCROLLWIN_TOP )
296 {
297 return 0;
298 }
299 else if ( evtType == wxEVT_SCROLLWIN_BOTTOM )
300 {
301 return m_unitMax;
302 }
303 else if ( evtType == wxEVT_SCROLLWIN_LINEUP )
304 {
305 return m_unitFirst ? m_unitFirst - 1 : 0;
306 }
307 else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN )
308 {
309 return m_unitFirst + 1;
310 }
311 else if ( evtType == wxEVT_SCROLLWIN_PAGEUP )
312 {
7d0cf3f9
VZ
313 // Page up should do at least as much as line up.
314 return wxMin(FindFirstVisibleFromLast(m_unitFirst),
315 m_unitFirst ? m_unitFirst - 1 : 0);
f18eaf26
VZ
316 }
317 else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN )
318 {
7d0cf3f9 319 // And page down should do at least as much as line down.
f18eaf26 320 if ( GetVisibleEnd() )
7d0cf3f9 321 return wxMax(GetVisibleEnd() - 1, m_unitFirst + 1);
f18eaf26 322 else
7d0cf3f9 323 return wxMax(GetVisibleEnd(), m_unitFirst + 1);
f18eaf26
VZ
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?
9a83f860 335 wxFAIL_MSG( wxT("unknown scroll event type?") );
f18eaf26 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{
9a83f860 479 wxASSERT_MSG( from <= to, wxT("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 676{
0fa9b2ed
JS
677 if ( m_unitMax )
678 {
679 // sometimes change in varscrollable window's size can result in
680 // unused empty space after the last item. Fix it by decrementing
681 // first visible item position according to the available space.
682
683 // determine free space
684 const wxCoord sWindow = GetOrientationTargetSize();
685 wxCoord s = 0;
686 size_t unit;
687 for ( unit = m_unitFirst; unit < m_unitMax; ++unit )
688 {
689 if ( s > sWindow )
690 break;
691
692 s += OnGetUnitSize(unit);
693 }
694 wxCoord freeSpace = sWindow - s;
695
696 // decrement first visible item index as long as there is free space
697 size_t idealUnitFirst;
698 for ( idealUnitFirst = m_unitFirst;
699 idealUnitFirst > 0;
700 idealUnitFirst-- )
701 {
702 wxCoord us = OnGetUnitSize(idealUnitFirst-1);
703 if ( freeSpace < us )
704 break;
705 freeSpace -= us;
706 }
707 m_unitFirst = idealUnitFirst;
708 }
709
cf7d6329
VZ
710 UpdateScrollbar();
711
712 event.Skip();
713}
714
f18eaf26 715void wxVarScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
cf7d6329 716{
f18eaf26
VZ
717 if (GetOrientation() != event.GetOrientation())
718 {
719 event.Skip();
720 return;
721 }
cf7d6329 722
f18eaf26 723 DoScrollToUnit(GetNewScrollPosition(event));
5d2ad055 724
f18eaf26
VZ
725#ifdef __WXMAC__
726 UpdateMacScrollWindow();
727#endif // __WXMAC__
728}
729
730void wxVarScrollHelperBase::DoPrepareDC(wxDC& dc)
731{
732 if ( m_physicalScrolling )
cf7d6329 733 {
f18eaf26
VZ
734 wxPoint pt = dc.GetDeviceOrigin();
735
736 IncOrient(pt.x, pt.y, -GetScrollOffset());
737
738 dc.SetDeviceOrigin(pt.x, pt.y);
cf7d6329 739 }
f18eaf26
VZ
740}
741
742int wxVarScrollHelperBase::DoCalcScrolledPosition(int coord) const
743{
744 return coord - GetScrollOffset();
745}
746
747int wxVarScrollHelperBase::DoCalcUnscrolledPosition(int coord) const
748{
749 return coord + GetScrollOffset();
750}
751
752#if wxUSE_MOUSEWHEEL
753
754void wxVarScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
755{
756 // we only want to process wheel events for vertical implementations.
757 // There is no way to determine wheel orientation (and on MSW horizontal
758 // wheel rotation just fakes scroll events, rather than sending a MOUSEWHEEL
759 // event).
760 if ( GetOrientation() != wxVERTICAL )
761 return;
762
763 m_sumWheelRotation += event.GetWheelRotation();
764 int delta = event.GetWheelDelta();
765
766 // how much to scroll this time
767 int units_to_scroll = -(m_sumWheelRotation/delta);
768 if ( !units_to_scroll )
769 return;
770
771 m_sumWheelRotation += units_to_scroll*delta;
772
773 if ( !event.IsPageScroll() )
774 DoScrollUnits( units_to_scroll*event.GetLinesPerAction() );
775 else // scroll pages instead of units
776 DoScrollPages( units_to_scroll );
777}
778
779#endif // wxUSE_MOUSEWHEEL
780
781
782// ============================================================================
783// wxVarHVScrollHelper implementation
784// ============================================================================
785
786// ----------------------------------------------------------------------------
787// wxVarHVScrollHelper operations
788// ----------------------------------------------------------------------------
789
790void wxVarHVScrollHelper::SetRowColumnCount(size_t rowCount, size_t columnCount)
791{
792 SetRowCount(rowCount);
793 SetColumnCount(columnCount);
794}
795
796bool wxVarHVScrollHelper::ScrollToRowColumn(size_t row, size_t column)
797{
798 bool result = false;
799 result |= ScrollToRow(row);
800 result |= ScrollToColumn(column);
801 return result;
802}
803
804void wxVarHVScrollHelper::RefreshRowColumn(size_t row, size_t column)
805{
806 // is this unit visible?
807 if ( !IsRowVisible(row) || !IsColumnVisible(column) )
cf7d6329 808 {
f18eaf26
VZ
809 // no, it is useless to do anything
810 return;
cf7d6329 811 }
f18eaf26
VZ
812
813 // calculate the rect occupied by this cell on screen
814 wxRect v_rect, h_rect;
815 v_rect.height = OnGetRowHeight(row);
816 h_rect.width = OnGetColumnWidth(column);
817
818 size_t n;
819
820 for ( n = GetVisibleRowsBegin(); n < row; n++ )
cf7d6329 821 {
f18eaf26 822 v_rect.y += OnGetRowHeight(n);
cf7d6329 823 }
f18eaf26
VZ
824
825 for ( n = GetVisibleColumnsBegin(); n < column; n++ )
cf7d6329 826 {
f18eaf26 827 h_rect.x += OnGetColumnWidth(n);
cf7d6329 828 }
f18eaf26 829
4c51a665 830 // refresh but specialize the behaviour if we have a single target window
f18eaf26 831 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
cf7d6329 832 {
f18eaf26
VZ
833 v_rect.x = h_rect.x;
834 v_rect.width = h_rect.width;
835 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
cf7d6329 836 }
f18eaf26 837 else
cf7d6329 838 {
f18eaf26
VZ
839 v_rect.x = 0;
840 v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize();
841 h_rect.y = 0;
842 h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize();
843
844 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
845 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect);
cf7d6329 846 }
f18eaf26
VZ
847}
848
849void wxVarHVScrollHelper::RefreshRowsColumns(size_t fromRow, size_t toRow,
850 size_t fromColumn, size_t toColumn)
851{
852 wxASSERT_MSG( fromRow <= toRow || fromColumn <= toColumn,
9a83f860 853 wxT("RefreshRowsColumns(): empty range") );
f18eaf26
VZ
854
855 // clump the range to just the visible units -- it is useless to refresh
856 // the other ones
857 if ( fromRow < GetVisibleRowsBegin() )
858 fromRow = GetVisibleRowsBegin();
859
860 if ( toRow > GetVisibleRowsEnd() )
861 toRow = GetVisibleRowsEnd();
862
863 if ( fromColumn < GetVisibleColumnsBegin() )
864 fromColumn = GetVisibleColumnsBegin();
865
866 if ( toColumn > GetVisibleColumnsEnd() )
867 toColumn = GetVisibleColumnsEnd();
868
869 // calculate the rect occupied by these units on screen
870 wxRect v_rect, h_rect;
871 size_t nBefore, nBetween;
872
873 for ( nBefore = GetVisibleRowsBegin();
874 nBefore < fromRow;
875 nBefore++ )
5d2ad055 876 {
f18eaf26 877 v_rect.y += OnGetRowHeight(nBefore);
5d2ad055 878 }
f18eaf26
VZ
879
880 for ( nBetween = fromRow; nBetween <= toRow; nBetween++ )
5d2ad055 881 {
f18eaf26 882 v_rect.height += OnGetRowHeight(nBetween);
5d2ad055 883 }
ca65c044 884
f18eaf26
VZ
885 for ( nBefore = GetVisibleColumnsBegin();
886 nBefore < fromColumn;
887 nBefore++ )
cf7d6329 888 {
f18eaf26 889 h_rect.x += OnGetColumnWidth(nBefore);
cf7d6329
VZ
890 }
891
f18eaf26
VZ
892 for ( nBetween = fromColumn; nBetween <= toColumn; nBetween++ )
893 {
894 h_rect.width += OnGetColumnWidth(nBetween);
895 }
b544a278 896
4c51a665 897 // refresh but specialize the behaviour if we have a single target window
f18eaf26
VZ
898 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
899 {
900 v_rect.x = h_rect.x;
901 v_rect.width = h_rect.width;
902 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
903 }
904 else
905 {
906 v_rect.x = 0;
907 v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize();
908 h_rect.y = 0;
909 h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize();
910
911 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect);
912 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect);
913 }
cf7d6329
VZ
914}
915
1c6c52fd 916wxPosition wxVarHVScrollHelper::VirtualHitTest(wxCoord x, wxCoord y) const
f18eaf26 917{
1c6c52fd
CE
918 return wxPosition(wxVarVScrollHelper::VirtualHitTest(y),
919 wxVarHScrollHelper::VirtualHitTest(x));
f18eaf26 920}
4719e58d 921
f18eaf26 922void wxVarHVScrollHelper::DoPrepareDC(wxDC& dc)
4719e58d 923{
f18eaf26
VZ
924 wxVarVScrollHelper::DoPrepareDC(dc);
925 wxVarHScrollHelper::DoPrepareDC(dc);
926}
4719e58d 927
f18eaf26
VZ
928bool wxVarHVScrollHelper::ScrollLayout()
929{
930 bool layout_result = false;
931 layout_result |= wxVarVScrollHelper::ScrollLayout();
932 layout_result |= wxVarHScrollHelper::ScrollLayout();
933 return layout_result;
934}
4719e58d 935
f18eaf26
VZ
936wxSize wxVarHVScrollHelper::GetRowColumnCount() const
937{
938 return wxSize(GetColumnCount(), GetRowCount());
939}
4719e58d 940
f18eaf26
VZ
941wxPosition wxVarHVScrollHelper::GetVisibleBegin() const
942{
943 return wxPosition(GetVisibleRowsBegin(), GetVisibleColumnsBegin());
4719e58d
RD
944}
945
f18eaf26
VZ
946wxPosition wxVarHVScrollHelper::GetVisibleEnd() const
947{
948 return wxPosition(GetVisibleRowsEnd(), GetVisibleColumnsEnd());
949}
950
951bool wxVarHVScrollHelper::IsVisible(size_t row, size_t column) const
952{
953 return IsRowVisible(row) && IsColumnVisible(column);
954}
955
956
957// ============================================================================
958// wx[V/H/HV]ScrolledWindow implementations
959// ============================================================================
960
961IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel)
962IMPLEMENT_ABSTRACT_CLASS(wxHScrolledWindow, wxPanel)
963IMPLEMENT_ABSTRACT_CLASS(wxHVScrolledWindow, wxPanel)
d77836e4 964
9bb3f212
RR
965
966#if WXWIN_COMPATIBILITY_2_8
967
968// ===========================================================================
969// wxVarVScrollLegacyAdaptor
970// ===========================================================================
971
03647350 972size_t wxVarVScrollLegacyAdaptor::GetFirstVisibleLine() const
9bb3f212
RR
973{ return GetVisibleRowsBegin(); }
974
975size_t wxVarVScrollLegacyAdaptor::GetLastVisibleLine() const
976{ return GetVisibleRowsEnd() - 1; }
977
978size_t wxVarVScrollLegacyAdaptor::GetLineCount() const
979{ return GetRowCount(); }
980
981void wxVarVScrollLegacyAdaptor::SetLineCount(size_t count)
982{ SetRowCount(count); }
983
984void wxVarVScrollLegacyAdaptor::RefreshLine(size_t line)
985{ RefreshRow(line); }
986
987void wxVarVScrollLegacyAdaptor::RefreshLines(size_t from, size_t to)
988{ RefreshRows(from, to); }
03647350 989
9bb3f212
RR
990bool wxVarVScrollLegacyAdaptor::ScrollToLine(size_t line)
991{ return ScrollToRow(line); }
992
993bool wxVarVScrollLegacyAdaptor::ScrollLines(int lines)
994{ return ScrollRows(lines); }
03647350 995
9bb3f212
RR
996bool wxVarVScrollLegacyAdaptor::ScrollPages(int pages)
997{ return ScrollRowPages(pages); }
998
999wxCoord wxVarVScrollLegacyAdaptor::OnGetLineHeight(size_t WXUNUSED(n)) const
1000{
9a83f860 1001 wxFAIL_MSG( wxT("OnGetLineHeight() must be overridden if OnGetRowHeight() isn't!") );
9bb3f212
RR
1002 return -1;
1003}
1004
cf4a8b26
VZ
1005void wxVarVScrollLegacyAdaptor::OnGetLinesHint(size_t WXUNUSED(lineMin),
1006 size_t WXUNUSED(lineMax)) const
1007{
1008}
9bb3f212 1009
cf4a8b26
VZ
1010wxCoord wxVarVScrollLegacyAdaptor::OnGetRowHeight(size_t n) const
1011{
1012 return OnGetLineHeight(n);
1013}
1014
1015void wxVarVScrollLegacyAdaptor::OnGetRowsHeightHint(size_t rowMin,
1016 size_t rowMax) const
1017{
1018 OnGetLinesHint(rowMin, rowMax);
1019}
1020
1021#endif // WXWIN_COMPATIBILITY_2_8