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