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