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