]>
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 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/dc.h" | |
28 | #include "wx/sizer.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/vscroll.h" | |
32 | ||
33 | #include "wx/utils.h" // For wxMin/wxMax(). | |
34 | ||
35 | // ============================================================================ | |
36 | // wxVarScrollHelperEvtHandler declaration | |
37 | // ============================================================================ | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // wxScrollHelperEvtHandler: intercept the events from the window and forward | |
41 | // them to wxVarScrollHelperBase | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | class WXDLLEXPORT wxVarScrollHelperEvtHandler : public wxEvtHandler | |
45 | { | |
46 | public: | |
47 | wxVarScrollHelperEvtHandler(wxVarScrollHelperBase *scrollHelper) | |
48 | { | |
49 | m_scrollHelper = scrollHelper; | |
50 | } | |
51 | ||
52 | virtual bool ProcessEvent(wxEvent& event); | |
53 | ||
54 | private: | |
55 | wxVarScrollHelperBase *m_scrollHelper; | |
56 | ||
57 | wxDECLARE_NO_COPY_CLASS(wxVarScrollHelperEvtHandler); | |
58 | }; | |
59 | ||
60 | // ============================================================================ | |
61 | // wxVarScrollHelperEvtHandler implementation | |
62 | // ============================================================================ | |
63 | ||
64 | bool 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 | ||
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 | ||
127 | event.Skip(wasSkipped); | |
128 | ||
129 | return processed; | |
130 | } | |
131 | ||
132 | ||
133 | // ============================================================================ | |
134 | // wxVarScrollHelperBase implementation | |
135 | // ============================================================================ | |
136 | ||
137 | // ---------------------------------------------------------------------------- | |
138 | // wxVarScrollHelperBase initialization | |
139 | // ---------------------------------------------------------------------------- | |
140 | ||
141 | wxVarScrollHelperBase::wxVarScrollHelperBase(wxWindow *win) | |
142 | { | |
143 | wxASSERT_MSG( win, wxT("associated window can't be NULL in wxVarScrollHelperBase") ); | |
144 | ||
145 | #if wxUSE_MOUSEWHEEL | |
146 | m_sumWheelRotation = 0; | |
147 | #endif | |
148 | ||
149 | m_unitMax = 0; | |
150 | m_sizeTotal = 0; | |
151 | m_unitFirst = 0; | |
152 | ||
153 | m_win = | |
154 | m_targetWindow = NULL; | |
155 | ||
156 | m_physicalScrolling = true; | |
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 | ||
166 | wxVarScrollHelperBase::~wxVarScrollHelperBase() | |
167 | { | |
168 | DeleteEvtHandler(); | |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // wxVarScrollHelperBase various helpers | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | void | |
176 | wxVarScrollHelperBase::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 | ||
193 | void | |
194 | wxVarScrollHelperBase::IncOrient(wxCoord& x, wxCoord& y, wxCoord inc) | |
195 | { | |
196 | if ( GetOrientation() == wxVERTICAL ) | |
197 | y += inc; | |
198 | else | |
199 | x += inc; | |
200 | } | |
201 | ||
202 | wxCoord wxVarScrollHelperBase::DoEstimateTotalSize() const | |
203 | { | |
204 | // estimate the total height: it is impossible to call | |
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, | |
207 | // some in the end and some in the middle | |
208 | static const size_t NUM_UNITS_TO_SAMPLE = 10; | |
209 | ||
210 | wxCoord sizeTotal; | |
211 | if ( m_unitMax < 3*NUM_UNITS_TO_SAMPLE ) | |
212 | { | |
213 | // in this case, full calculations are faster and more correct than | |
214 | // guessing | |
215 | sizeTotal = GetUnitsSize(0, m_unitMax); | |
216 | } | |
217 | else // too many units to calculate exactly | |
218 | { | |
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); | |
230 | } | |
231 | ||
232 | return sizeTotal; | |
233 | } | |
234 | ||
235 | wxCoord wxVarScrollHelperBase::GetUnitsSize(size_t unitMin, size_t unitMax) const | |
236 | { | |
237 | if ( unitMin == unitMax ) | |
238 | return 0; | |
239 | else if ( unitMin > unitMax ) | |
240 | return -GetUnitsSize(unitMax, unitMin); | |
241 | //else: unitMin < unitMax | |
242 | ||
243 | // let the user code know that we're going to need all these units | |
244 | OnGetUnitsSizeHint(unitMin, unitMax); | |
245 | ||
246 | // sum up their sizes | |
247 | wxCoord size = 0; | |
248 | for ( size_t unit = unitMin; unit < unitMax; ++unit ) | |
249 | { | |
250 | size += OnGetUnitSize(unit); | |
251 | } | |
252 | ||
253 | return size; | |
254 | } | |
255 | ||
256 | size_t wxVarScrollHelperBase::FindFirstVisibleFromLast(size_t unitLast, bool full) const | |
257 | { | |
258 | const wxCoord sWindow = GetOrientationTargetSize(); | |
259 | ||
260 | // go upwards until we arrive at a unit such that unitLast is not visible | |
261 | // any more when it is shown | |
262 | size_t unitFirst = unitLast; | |
263 | wxCoord s = 0; | |
264 | for ( ;; ) | |
265 | { | |
266 | s += OnGetUnitSize(unitFirst); | |
267 | ||
268 | if ( s > sWindow ) | |
269 | { | |
270 | // for this unit to be fully visible we need to go one unit | |
271 | // down, but if it is enough for it to be only partly visible then | |
272 | // this unit will do as well | |
273 | if ( full ) | |
274 | { | |
275 | ++unitFirst; | |
276 | } | |
277 | ||
278 | break; | |
279 | } | |
280 | ||
281 | if ( !unitFirst ) | |
282 | break; | |
283 | ||
284 | --unitFirst; | |
285 | } | |
286 | ||
287 | return unitFirst; | |
288 | } | |
289 | ||
290 | size_t wxVarScrollHelperBase::GetNewScrollPosition(wxScrollWinEvent& event) const | |
291 | { | |
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 | { | |
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); | |
315 | } | |
316 | else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN ) | |
317 | { | |
318 | // And page down should do at least as much as line down. | |
319 | if ( GetVisibleEnd() ) | |
320 | return wxMax(GetVisibleEnd() - 1, m_unitFirst + 1); | |
321 | else | |
322 | return wxMax(GetVisibleEnd(), m_unitFirst + 1); | |
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? | |
334 | wxFAIL_MSG( wxT("unknown scroll event type?") ); | |
335 | return 0; | |
336 | } | |
337 | ||
338 | void wxVarScrollHelperBase::UpdateScrollbar() | |
339 | { | |
340 | // if there is nothing to scroll, remove the scrollbar | |
341 | if ( !m_unitMax ) | |
342 | { | |
343 | RemoveScrollbar(); | |
344 | return; | |
345 | } | |
346 | ||
347 | // see how many units can we fit on screen | |
348 | const wxCoord sWindow = GetOrientationTargetSize(); | |
349 | ||
350 | // do vertical calculations | |
351 | wxCoord s = 0; | |
352 | size_t unit; | |
353 | for ( unit = m_unitFirst; unit < m_unitMax; ++unit ) | |
354 | { | |
355 | if ( s > sWindow ) | |
356 | break; | |
357 | ||
358 | s += OnGetUnitSize(unit); | |
359 | } | |
360 | ||
361 | m_nUnitsVisible = unit - m_unitFirst; | |
362 | ||
363 | int unitsPageSize = m_nUnitsVisible; | |
364 | if ( s > sWindow ) | |
365 | { | |
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 | ||
376 | void wxVarScrollHelperBase::RemoveScrollbar() | |
377 | { | |
378 | m_unitFirst = 0; | |
379 | m_nUnitsVisible = m_unitMax; | |
380 | m_win->SetScrollbar(GetOrientation(), 0, 0, 0); | |
381 | } | |
382 | ||
383 | void 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) ) | |
389 | { | |
390 | delete m_handler; | |
391 | } | |
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; | |
396 | } | |
397 | } | |
398 | ||
399 | void wxVarScrollHelperBase::DoSetTargetWindow(wxWindow *target) | |
400 | { | |
401 | m_targetWindow = target; | |
402 | #ifdef __WXMAC__ | |
403 | target->MacSetClipChildren( true ) ; | |
404 | #endif | |
405 | ||
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 ) | |
410 | { | |
411 | // if we already have a handler, delete it first | |
412 | DeleteEvtHandler(); | |
413 | ||
414 | m_handler = new wxVarScrollHelperEvtHandler(this); | |
415 | m_targetWindow->PushEventHandler(m_handler); | |
416 | } | |
417 | } | |
418 | ||
419 | // ---------------------------------------------------------------------------- | |
420 | // wxVarScrollHelperBase operations | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | void wxVarScrollHelperBase::SetTargetWindow(wxWindow *target) | |
424 | { | |
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 | ||
433 | void wxVarScrollHelperBase::SetUnitCount(size_t count) | |
434 | { | |
435 | // save the number of units | |
436 | m_unitMax = count; | |
437 | ||
438 | // and our estimate for their total height | |
439 | m_sizeTotal = EstimateTotalSize(); | |
440 | ||
441 | // ScrollToUnit() will update the scrollbar itself if it changes the unit | |
442 | // we pass to it because it's out of [new] range | |
443 | size_t oldScrollPos = m_unitFirst; | |
444 | DoScrollToUnit(m_unitFirst); | |
445 | if ( oldScrollPos == m_unitFirst ) | |
446 | { | |
447 | // but if it didn't do it, we still need to update the scrollbar to | |
448 | // reflect the changed number of units ourselves | |
449 | UpdateScrollbar(); | |
450 | } | |
451 | } | |
452 | ||
453 | void wxVarScrollHelperBase::RefreshUnit(size_t unit) | |
454 | { | |
455 | // is this unit visible? | |
456 | if ( !IsVisible(unit) ) | |
457 | { | |
458 | // no, it is useless to do anything | |
459 | return; | |
460 | } | |
461 | ||
462 | // calculate the rect occupied by this unit on screen | |
463 | wxRect rect; | |
464 | AssignOrient(rect.width, rect.height, | |
465 | GetNonOrientationTargetSize(), OnGetUnitSize(unit)); | |
466 | ||
467 | for ( size_t n = GetVisibleBegin(); n < unit; ++n ) | |
468 | { | |
469 | IncOrient(rect.x, rect.y, OnGetUnitSize(n)); | |
470 | } | |
471 | ||
472 | // do refresh it | |
473 | m_targetWindow->RefreshRect(rect); | |
474 | } | |
475 | ||
476 | void wxVarScrollHelperBase::RefreshUnits(size_t from, size_t to) | |
477 | { | |
478 | wxASSERT_MSG( from <= to, wxT("RefreshUnits(): empty range") ); | |
479 | ||
480 | // clump the range to just the visible units -- it is useless to refresh | |
481 | // the other ones | |
482 | if ( from < GetVisibleBegin() ) | |
483 | from = GetVisibleBegin(); | |
484 | ||
485 | if ( to > GetVisibleEnd() ) | |
486 | to = GetVisibleEnd(); | |
487 | ||
488 | // calculate the rect occupied by these units on screen | |
489 | int orient_size = 0, | |
490 | orient_pos = 0; | |
491 | ||
492 | int nonorient_size = GetNonOrientationTargetSize(); | |
493 | ||
494 | for ( size_t nBefore = GetVisibleBegin(); | |
495 | nBefore < from; | |
496 | nBefore++ ) | |
497 | { | |
498 | orient_pos += OnGetUnitSize(nBefore); | |
499 | } | |
500 | ||
501 | for ( size_t nBetween = from; nBetween <= to; nBetween++ ) | |
502 | { | |
503 | orient_size += OnGetUnitSize(nBetween); | |
504 | } | |
505 | ||
506 | wxRect rect; | |
507 | AssignOrient(rect.x, rect.y, 0, orient_pos); | |
508 | AssignOrient(rect.width, rect.height, nonorient_size, orient_size); | |
509 | ||
510 | // do refresh it | |
511 | m_targetWindow->RefreshRect(rect); | |
512 | } | |
513 | ||
514 | void wxVarScrollHelperBase::RefreshAll() | |
515 | { | |
516 | UpdateScrollbar(); | |
517 | ||
518 | m_targetWindow->Refresh(); | |
519 | } | |
520 | ||
521 | bool wxVarScrollHelperBase::ScrollLayout() | |
522 | { | |
523 | if ( m_targetWindow->GetSizer() && m_physicalScrolling ) | |
524 | { | |
525 | // adjust the sizer dimensions/position taking into account the | |
526 | // virtual size and scrolled position of the window. | |
527 | ||
528 | int x, y; | |
529 | AssignOrient(x, y, 0, -GetScrollOffset()); | |
530 | ||
531 | int w, h; | |
532 | m_targetWindow->GetVirtualSize(&w, &h); | |
533 | ||
534 | m_targetWindow->GetSizer()->SetDimension(x, y, w, h); | |
535 | return true; | |
536 | } | |
537 | ||
538 | // fall back to default for LayoutConstraints | |
539 | return m_targetWindow->wxWindow::Layout(); | |
540 | } | |
541 | ||
542 | int wxVarScrollHelperBase::VirtualHitTest(wxCoord coord) const | |
543 | { | |
544 | const size_t unitMax = GetVisibleEnd(); | |
545 | for ( size_t unit = GetVisibleBegin(); unit < unitMax; ++unit ) | |
546 | { | |
547 | coord -= OnGetUnitSize(unit); | |
548 | if ( coord < 0 ) | |
549 | return unit; | |
550 | } | |
551 | ||
552 | return wxNOT_FOUND; | |
553 | } | |
554 | ||
555 | // ---------------------------------------------------------------------------- | |
556 | // wxVarScrollHelperBase scrolling | |
557 | // ---------------------------------------------------------------------------- | |
558 | ||
559 | bool wxVarScrollHelperBase::DoScrollToUnit(size_t unit) | |
560 | { | |
561 | if ( !m_unitMax ) | |
562 | { | |
563 | // we're empty, code below doesn't make sense in this case | |
564 | return false; | |
565 | } | |
566 | ||
567 | // determine the real first unit to scroll to: we shouldn't scroll beyond | |
568 | // the end | |
569 | size_t unitFirstLast = FindFirstVisibleFromLast(m_unitMax - 1, true); | |
570 | if ( unit > unitFirstLast ) | |
571 | unit = unitFirstLast; | |
572 | ||
573 | // anything to do? | |
574 | if ( unit == m_unitFirst ) | |
575 | { | |
576 | // no | |
577 | return false; | |
578 | } | |
579 | ||
580 | ||
581 | // remember the currently shown units for the refresh code below | |
582 | size_t unitFirstOld = GetVisibleBegin(), | |
583 | unitLastOld = GetVisibleEnd(); | |
584 | ||
585 | m_unitFirst = unit; | |
586 | ||
587 | ||
588 | // the size of scrollbar thumb could have changed | |
589 | UpdateScrollbar(); | |
590 | ||
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 | |
594 | if ( m_targetWindow->GetChildren().empty() && | |
595 | (GetVisibleBegin() >= unitLastOld || GetVisibleEnd() <= unitFirstOld) ) | |
596 | { | |
597 | // the simplest case: we don't have any old units left, just redraw | |
598 | // everything | |
599 | m_targetWindow->Refresh(); | |
600 | } | |
601 | else // scroll the window | |
602 | { | |
603 | // Avoid scrolling visible parts of the screen on Mac | |
604 | #ifdef __WXMAC__ | |
605 | if (m_physicalScrolling && m_targetWindow->IsShownOnScreen()) | |
606 | #else | |
607 | if ( m_physicalScrolling ) | |
608 | #endif | |
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 | } | |
630 | } | |
631 | ||
632 | return true; | |
633 | } | |
634 | ||
635 | bool wxVarScrollHelperBase::DoScrollUnits(int units) | |
636 | { | |
637 | units += m_unitFirst; | |
638 | if ( units < 0 ) | |
639 | units = 0; | |
640 | ||
641 | return DoScrollToUnit(units); | |
642 | } | |
643 | ||
644 | bool wxVarScrollHelperBase::DoScrollPages(int pages) | |
645 | { | |
646 | bool didSomething = false; | |
647 | ||
648 | while ( pages ) | |
649 | { | |
650 | int unit; | |
651 | if ( pages > 0 ) | |
652 | { | |
653 | unit = GetVisibleEnd(); | |
654 | if ( unit ) | |
655 | --unit; | |
656 | --pages; | |
657 | } | |
658 | else // pages < 0 | |
659 | { | |
660 | unit = FindFirstVisibleFromLast(GetVisibleEnd()); | |
661 | ++pages; | |
662 | } | |
663 | ||
664 | didSomething = DoScrollToUnit(unit); | |
665 | } | |
666 | ||
667 | return didSomething; | |
668 | } | |
669 | ||
670 | // ---------------------------------------------------------------------------- | |
671 | // event handling | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
674 | void wxVarScrollHelperBase::HandleOnSize(wxSizeEvent& event) | |
675 | { | |
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 | ||
709 | UpdateScrollbar(); | |
710 | ||
711 | event.Skip(); | |
712 | } | |
713 | ||
714 | void wxVarScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event) | |
715 | { | |
716 | if (GetOrientation() != event.GetOrientation()) | |
717 | { | |
718 | event.Skip(); | |
719 | return; | |
720 | } | |
721 | ||
722 | DoScrollToUnit(GetNewScrollPosition(event)); | |
723 | ||
724 | #ifdef __WXMAC__ | |
725 | UpdateMacScrollWindow(); | |
726 | #endif // __WXMAC__ | |
727 | } | |
728 | ||
729 | void wxVarScrollHelperBase::DoPrepareDC(wxDC& dc) | |
730 | { | |
731 | if ( m_physicalScrolling ) | |
732 | { | |
733 | wxPoint pt = dc.GetDeviceOrigin(); | |
734 | ||
735 | IncOrient(pt.x, pt.y, -GetScrollOffset()); | |
736 | ||
737 | dc.SetDeviceOrigin(pt.x, pt.y); | |
738 | } | |
739 | } | |
740 | ||
741 | int wxVarScrollHelperBase::DoCalcScrolledPosition(int coord) const | |
742 | { | |
743 | return coord - GetScrollOffset(); | |
744 | } | |
745 | ||
746 | int wxVarScrollHelperBase::DoCalcUnscrolledPosition(int coord) const | |
747 | { | |
748 | return coord + GetScrollOffset(); | |
749 | } | |
750 | ||
751 | #if wxUSE_MOUSEWHEEL | |
752 | ||
753 | void 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 | ||
789 | void wxVarHVScrollHelper::SetRowColumnCount(size_t rowCount, size_t columnCount) | |
790 | { | |
791 | SetRowCount(rowCount); | |
792 | SetColumnCount(columnCount); | |
793 | } | |
794 | ||
795 | bool 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 | ||
803 | void wxVarHVScrollHelper::RefreshRowColumn(size_t row, size_t column) | |
804 | { | |
805 | // is this unit visible? | |
806 | if ( !IsRowVisible(row) || !IsColumnVisible(column) ) | |
807 | { | |
808 | // no, it is useless to do anything | |
809 | return; | |
810 | } | |
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++ ) | |
820 | { | |
821 | v_rect.y += OnGetRowHeight(n); | |
822 | } | |
823 | ||
824 | for ( n = GetVisibleColumnsBegin(); n < column; n++ ) | |
825 | { | |
826 | h_rect.x += OnGetColumnWidth(n); | |
827 | } | |
828 | ||
829 | // refresh but specialize the behaviour if we have a single target window | |
830 | if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() ) | |
831 | { | |
832 | v_rect.x = h_rect.x; | |
833 | v_rect.width = h_rect.width; | |
834 | wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect); | |
835 | } | |
836 | else | |
837 | { | |
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); | |
845 | } | |
846 | } | |
847 | ||
848 | void wxVarHVScrollHelper::RefreshRowsColumns(size_t fromRow, size_t toRow, | |
849 | size_t fromColumn, size_t toColumn) | |
850 | { | |
851 | wxASSERT_MSG( fromRow <= toRow || fromColumn <= toColumn, | |
852 | wxT("RefreshRowsColumns(): empty range") ); | |
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++ ) | |
875 | { | |
876 | v_rect.y += OnGetRowHeight(nBefore); | |
877 | } | |
878 | ||
879 | for ( nBetween = fromRow; nBetween <= toRow; nBetween++ ) | |
880 | { | |
881 | v_rect.height += OnGetRowHeight(nBetween); | |
882 | } | |
883 | ||
884 | for ( nBefore = GetVisibleColumnsBegin(); | |
885 | nBefore < fromColumn; | |
886 | nBefore++ ) | |
887 | { | |
888 | h_rect.x += OnGetColumnWidth(nBefore); | |
889 | } | |
890 | ||
891 | for ( nBetween = fromColumn; nBetween <= toColumn; nBetween++ ) | |
892 | { | |
893 | h_rect.width += OnGetColumnWidth(nBetween); | |
894 | } | |
895 | ||
896 | // refresh but specialize the behaviour if we have a single target window | |
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 | } | |
913 | } | |
914 | ||
915 | wxPosition wxVarHVScrollHelper::VirtualHitTest(wxCoord x, wxCoord y) const | |
916 | { | |
917 | return wxPosition(wxVarVScrollHelper::VirtualHitTest(y), | |
918 | wxVarHScrollHelper::VirtualHitTest(x)); | |
919 | } | |
920 | ||
921 | void wxVarHVScrollHelper::DoPrepareDC(wxDC& dc) | |
922 | { | |
923 | wxVarVScrollHelper::DoPrepareDC(dc); | |
924 | wxVarHScrollHelper::DoPrepareDC(dc); | |
925 | } | |
926 | ||
927 | bool wxVarHVScrollHelper::ScrollLayout() | |
928 | { | |
929 | bool layout_result = false; | |
930 | layout_result |= wxVarVScrollHelper::ScrollLayout(); | |
931 | layout_result |= wxVarHScrollHelper::ScrollLayout(); | |
932 | return layout_result; | |
933 | } | |
934 | ||
935 | wxSize wxVarHVScrollHelper::GetRowColumnCount() const | |
936 | { | |
937 | return wxSize(GetColumnCount(), GetRowCount()); | |
938 | } | |
939 | ||
940 | wxPosition wxVarHVScrollHelper::GetVisibleBegin() const | |
941 | { | |
942 | return wxPosition(GetVisibleRowsBegin(), GetVisibleColumnsBegin()); | |
943 | } | |
944 | ||
945 | wxPosition wxVarHVScrollHelper::GetVisibleEnd() const | |
946 | { | |
947 | return wxPosition(GetVisibleRowsEnd(), GetVisibleColumnsEnd()); | |
948 | } | |
949 | ||
950 | bool 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 | ||
960 | IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel) | |
961 | IMPLEMENT_ABSTRACT_CLASS(wxHScrolledWindow, wxPanel) | |
962 | IMPLEMENT_ABSTRACT_CLASS(wxHVScrolledWindow, wxPanel) | |
963 | ||
964 | ||
965 | #if WXWIN_COMPATIBILITY_2_8 | |
966 | ||
967 | // =========================================================================== | |
968 | // wxVarVScrollLegacyAdaptor | |
969 | // =========================================================================== | |
970 | ||
971 | size_t wxVarVScrollLegacyAdaptor::GetFirstVisibleLine() const | |
972 | { return GetVisibleRowsBegin(); } | |
973 | ||
974 | size_t wxVarVScrollLegacyAdaptor::GetLastVisibleLine() const | |
975 | { return GetVisibleRowsEnd() - 1; } | |
976 | ||
977 | size_t wxVarVScrollLegacyAdaptor::GetLineCount() const | |
978 | { return GetRowCount(); } | |
979 | ||
980 | void wxVarVScrollLegacyAdaptor::SetLineCount(size_t count) | |
981 | { SetRowCount(count); } | |
982 | ||
983 | void wxVarVScrollLegacyAdaptor::RefreshLine(size_t line) | |
984 | { RefreshRow(line); } | |
985 | ||
986 | void wxVarVScrollLegacyAdaptor::RefreshLines(size_t from, size_t to) | |
987 | { RefreshRows(from, to); } | |
988 | ||
989 | bool wxVarVScrollLegacyAdaptor::ScrollToLine(size_t line) | |
990 | { return ScrollToRow(line); } | |
991 | ||
992 | bool wxVarVScrollLegacyAdaptor::ScrollLines(int lines) | |
993 | { return ScrollRows(lines); } | |
994 | ||
995 | bool wxVarVScrollLegacyAdaptor::ScrollPages(int pages) | |
996 | { return ScrollRowPages(pages); } | |
997 | ||
998 | wxCoord wxVarVScrollLegacyAdaptor::OnGetLineHeight(size_t WXUNUSED(n)) const | |
999 | { | |
1000 | wxFAIL_MSG( wxT("OnGetLineHeight() must be overridden if OnGetRowHeight() isn't!") ); | |
1001 | return -1; | |
1002 | } | |
1003 | ||
1004 | void wxVarVScrollLegacyAdaptor::OnGetLinesHint(size_t WXUNUSED(lineMin), | |
1005 | size_t WXUNUSED(lineMax)) const | |
1006 | { | |
1007 | } | |
1008 | ||
1009 | wxCoord wxVarVScrollLegacyAdaptor::OnGetRowHeight(size_t n) const | |
1010 | { | |
1011 | return OnGetLineHeight(n); | |
1012 | } | |
1013 | ||
1014 | void wxVarVScrollLegacyAdaptor::OnGetRowsHeightHint(size_t rowMin, | |
1015 | size_t rowMax) const | |
1016 | { | |
1017 | OnGetLinesHint(rowMin, rowMax); | |
1018 | } | |
1019 | ||
1020 | #endif // WXWIN_COMPATIBILITY_2_8 |