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