| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: generic/scrolwin.cpp |
| 3 | // Purpose: wxScrolledWindow implementation |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "scrolwin.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/utils.h" |
| 32 | #include "wx/dcclient.h" |
| 33 | |
| 34 | #include "wx/generic/scrolwin.h" |
| 35 | #include "wx/panel.h" |
| 36 | |
| 37 | #ifdef __WXMSW__ |
| 38 | #include "windows.h" |
| 39 | #endif |
| 40 | |
| 41 | #ifdef __WXMOTIF__ |
| 42 | // For wxRETAINED implementation |
| 43 | #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++ |
| 44 | //This code switches off the compiler warnings |
| 45 | # pragma message disable nosimpint |
| 46 | #endif |
| 47 | #include <Xm/Xm.h> |
| 48 | #ifdef __VMS__ |
| 49 | # pragma message enable nosimpint |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // event tables |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel) |
| 58 | EVT_SCROLLWIN(wxScrolledWindow::OnScroll) |
| 59 | EVT_SIZE(wxScrolledWindow::OnSize) |
| 60 | EVT_PAINT(wxScrolledWindow::OnPaint) |
| 61 | EVT_CHAR(wxScrolledWindow::OnChar) |
| 62 | END_EVENT_TABLE() |
| 63 | |
| 64 | IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel) |
| 65 | |
| 66 | // ============================================================================ |
| 67 | // implementation |
| 68 | // ============================================================================ |
| 69 | |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | // wxScrolledWindow creation |
| 72 | // ---------------------------------------------------------------------------- |
| 73 | |
| 74 | wxScrolledWindow::wxScrolledWindow() |
| 75 | { |
| 76 | m_xScrollPixelsPerLine = 0; |
| 77 | m_yScrollPixelsPerLine = 0; |
| 78 | m_xScrollingEnabled = TRUE; |
| 79 | m_yScrollingEnabled = TRUE; |
| 80 | m_xScrollPosition = 0; |
| 81 | m_yScrollPosition = 0; |
| 82 | m_xScrollLines = 0; |
| 83 | m_yScrollLines = 0; |
| 84 | m_xScrollLinesPerPage = 0; |
| 85 | m_yScrollLinesPerPage = 0; |
| 86 | m_scaleX = 1.0; |
| 87 | m_scaleY = 1.0; |
| 88 | m_targetWindow = (wxWindow*) NULL; |
| 89 | } |
| 90 | |
| 91 | bool wxScrolledWindow::Create(wxWindow *parent, |
| 92 | wxWindowID id, |
| 93 | const wxPoint& pos, |
| 94 | const wxSize& size, |
| 95 | long style, |
| 96 | const wxString& name) |
| 97 | { |
| 98 | m_xScrollPixelsPerLine = 0; |
| 99 | m_yScrollPixelsPerLine = 0; |
| 100 | m_xScrollingEnabled = TRUE; |
| 101 | m_yScrollingEnabled = TRUE; |
| 102 | m_xScrollPosition = 0; |
| 103 | m_yScrollPosition = 0; |
| 104 | m_xScrollLines = 0; |
| 105 | m_yScrollLines = 0; |
| 106 | m_xScrollLinesPerPage = 0; |
| 107 | m_yScrollLinesPerPage = 0; |
| 108 | m_scaleX = 1.0; |
| 109 | m_scaleY = 1.0; |
| 110 | |
| 111 | m_targetWindow = this; |
| 112 | |
| 113 | bool ok = wxPanel::Create(parent, id, pos, size, style, name); |
| 114 | |
| 115 | #ifdef __WXMSW__ |
| 116 | // we need to process arrows ourselves for scrolling |
| 117 | m_lDlgCode |= DLGC_WANTARROWS; |
| 118 | #endif // __WXMSW__ |
| 119 | |
| 120 | return ok; |
| 121 | } |
| 122 | |
| 123 | wxScrolledWindow::~wxScrolledWindow() |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | // ---------------------------------------------------------------------------- |
| 128 | // setting scrolling parameters |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | |
| 131 | /* |
| 132 | * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line) |
| 133 | * noUnitsX/noUnitsY: : no. units per scrollbar |
| 134 | */ |
| 135 | void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY, |
| 136 | int noUnitsX, int noUnitsY, |
| 137 | int xPos, int yPos, bool noRefresh ) |
| 138 | { |
| 139 | int xpos, ypos; |
| 140 | |
| 141 | CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos); |
| 142 | bool do_refresh = |
| 143 | ( |
| 144 | (noUnitsX != 0 && m_xScrollLines == 0) || |
| 145 | (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) || |
| 146 | |
| 147 | (noUnitsY != 0 && m_yScrollLines == 0) || |
| 148 | (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) || |
| 149 | (xPos != m_xScrollPosition) || |
| 150 | (yPos != m_yScrollPosition) |
| 151 | // (pixelsPerUnitX != m_xScrollPixelsPerLine) || |
| 152 | // (pixelsPerUnitY != m_yScrollPixelsPerLine) |
| 153 | ); |
| 154 | |
| 155 | m_xScrollPixelsPerLine = pixelsPerUnitX; |
| 156 | m_yScrollPixelsPerLine = pixelsPerUnitY; |
| 157 | m_xScrollPosition = xPos; |
| 158 | m_yScrollPosition = yPos; |
| 159 | m_xScrollLines = noUnitsX; |
| 160 | m_yScrollLines = noUnitsY; |
| 161 | |
| 162 | #ifdef __WXMOTIF__ |
| 163 | // Sorry, some Motif-specific code to implement a backing pixmap |
| 164 | // for the wxRETAINED style. Implementing a backing store can't |
| 165 | // be entirely generic because it relies on the wxWindowDC implementation |
| 166 | // to duplicate X drawing calls for the backing pixmap. |
| 167 | |
| 168 | if ((m_windowStyle & wxRETAINED) == wxRETAINED) |
| 169 | { |
| 170 | Display* dpy = XtDisplay((Widget) GetMainWidget()); |
| 171 | |
| 172 | int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine; |
| 173 | int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine; |
| 174 | if (m_backingPixmap && |
| 175 | !((m_pixmapWidth == totalPixelWidth) && |
| 176 | (m_pixmapHeight == totalPixelHeight))) |
| 177 | { |
| 178 | XFreePixmap (dpy, (Pixmap) m_backingPixmap); |
| 179 | m_backingPixmap = (WXPixmap) 0; |
| 180 | } |
| 181 | |
| 182 | if (!m_backingPixmap && |
| 183 | (noUnitsX != 0) && (noUnitsY != 0)) |
| 184 | { |
| 185 | int depth = wxDisplayDepth(); |
| 186 | m_pixmapWidth = totalPixelWidth; |
| 187 | m_pixmapHeight = totalPixelHeight; |
| 188 | m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), |
| 189 | m_pixmapWidth, m_pixmapHeight, depth); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | #endif // Motif |
| 194 | |
| 195 | AdjustScrollbars(); |
| 196 | |
| 197 | if (do_refresh && !noRefresh) |
| 198 | m_targetWindow->Refresh(); |
| 199 | |
| 200 | #ifdef __WXMSW__ |
| 201 | // GRG: if this turns out to be really necessary, we could |
| 202 | // at least move it to the above if { ... } so that it is |
| 203 | // only done if noRefresh = FALSE (the default). OTOH, if |
| 204 | // this doesn't break anything, which seems to be the |
| 205 | // case, we could just leave it out. |
| 206 | |
| 207 | // Necessary? |
| 208 | // UpdateWindow ((HWND) m_targetWindow->GetHWND()); |
| 209 | #endif |
| 210 | #ifdef __WXMAC__ |
| 211 | m_targetWindow->MacUpdateImmediately() ; |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | // target window handling |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | |
| 219 | void wxScrolledWindow::SetTargetWindow( wxWindow *target ) |
| 220 | { |
| 221 | wxASSERT_MSG( target, wxT("target window must not be NULL") ); |
| 222 | m_targetWindow = target; |
| 223 | } |
| 224 | |
| 225 | wxWindow *wxScrolledWindow::GetTargetWindow() |
| 226 | { |
| 227 | return m_targetWindow; |
| 228 | } |
| 229 | |
| 230 | // ---------------------------------------------------------------------------- |
| 231 | // scrolling implementation itself |
| 232 | // ---------------------------------------------------------------------------- |
| 233 | |
| 234 | void wxScrolledWindow::OnScroll(wxScrollWinEvent& event) |
| 235 | { |
| 236 | int orient = event.GetOrientation(); |
| 237 | |
| 238 | int nScrollInc = CalcScrollInc(event); |
| 239 | if (nScrollInc == 0) return; |
| 240 | |
| 241 | if (orient == wxHORIZONTAL) |
| 242 | { |
| 243 | int newPos = m_xScrollPosition + nScrollInc; |
| 244 | SetScrollPos(wxHORIZONTAL, newPos, TRUE ); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | int newPos = m_yScrollPosition + nScrollInc; |
| 249 | SetScrollPos(wxVERTICAL, newPos, TRUE ); |
| 250 | } |
| 251 | |
| 252 | if (orient == wxHORIZONTAL) |
| 253 | { |
| 254 | m_xScrollPosition += nScrollInc; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | m_yScrollPosition += nScrollInc; |
| 259 | } |
| 260 | |
| 261 | if (orient == wxHORIZONTAL) |
| 262 | { |
| 263 | if (m_xScrollingEnabled) |
| 264 | m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL); |
| 265 | else |
| 266 | m_targetWindow->Refresh(); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | if (m_yScrollingEnabled) |
| 271 | m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL); |
| 272 | else |
| 273 | m_targetWindow->Refresh(); |
| 274 | } |
| 275 | #ifdef __WXMAC__ |
| 276 | m_targetWindow->MacUpdateImmediately() ; |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event) |
| 281 | { |
| 282 | int pos = event.GetPosition(); |
| 283 | int orient = event.GetOrientation(); |
| 284 | |
| 285 | int nScrollInc = 0; |
| 286 | switch (event.GetEventType()) |
| 287 | { |
| 288 | case wxEVT_SCROLLWIN_TOP: |
| 289 | { |
| 290 | if (orient == wxHORIZONTAL) |
| 291 | nScrollInc = - m_xScrollPosition; |
| 292 | else |
| 293 | nScrollInc = - m_yScrollPosition; |
| 294 | break; |
| 295 | } |
| 296 | case wxEVT_SCROLLWIN_BOTTOM: |
| 297 | { |
| 298 | if (orient == wxHORIZONTAL) |
| 299 | nScrollInc = m_xScrollLines - m_xScrollPosition; |
| 300 | else |
| 301 | nScrollInc = m_yScrollLines - m_yScrollPosition; |
| 302 | break; |
| 303 | } |
| 304 | case wxEVT_SCROLLWIN_LINEUP: |
| 305 | { |
| 306 | nScrollInc = -1; |
| 307 | break; |
| 308 | } |
| 309 | case wxEVT_SCROLLWIN_LINEDOWN: |
| 310 | { |
| 311 | nScrollInc = 1; |
| 312 | break; |
| 313 | } |
| 314 | case wxEVT_SCROLLWIN_PAGEUP: |
| 315 | { |
| 316 | if (orient == wxHORIZONTAL) |
| 317 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); |
| 318 | else |
| 319 | nScrollInc = -GetScrollPageSize(wxVERTICAL); |
| 320 | break; |
| 321 | } |
| 322 | case wxEVT_SCROLLWIN_PAGEDOWN: |
| 323 | { |
| 324 | if (orient == wxHORIZONTAL) |
| 325 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); |
| 326 | else |
| 327 | nScrollInc = GetScrollPageSize(wxVERTICAL); |
| 328 | break; |
| 329 | } |
| 330 | case wxEVT_SCROLLWIN_THUMBTRACK: |
| 331 | case wxEVT_SCROLLWIN_THUMBRELEASE: |
| 332 | { |
| 333 | if (orient == wxHORIZONTAL) |
| 334 | nScrollInc = pos - m_xScrollPosition; |
| 335 | else |
| 336 | nScrollInc = pos - m_yScrollPosition; |
| 337 | break; |
| 338 | } |
| 339 | default: |
| 340 | { |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (orient == wxHORIZONTAL) |
| 346 | { |
| 347 | if (m_xScrollPixelsPerLine > 0) |
| 348 | { |
| 349 | int w, h; |
| 350 | m_targetWindow->GetClientSize(&w, &h); |
| 351 | |
| 352 | int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine; |
| 353 | int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 ); |
| 354 | if (noPositions < 0) |
| 355 | noPositions = 0; |
| 356 | |
| 357 | if ( (m_xScrollPosition + nScrollInc) < 0 ) |
| 358 | nScrollInc = -m_xScrollPosition; // As -ve as we can go |
| 359 | else if ( (m_xScrollPosition + nScrollInc) > noPositions ) |
| 360 | nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go |
| 361 | } |
| 362 | else |
| 363 | m_targetWindow->Refresh(); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | if (m_yScrollPixelsPerLine > 0) |
| 368 | { |
| 369 | int w, h; |
| 370 | m_targetWindow->GetClientSize(&w, &h); |
| 371 | |
| 372 | int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine; |
| 373 | int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 ); |
| 374 | if (noPositions < 0) |
| 375 | noPositions = 0; |
| 376 | |
| 377 | if ( (m_yScrollPosition + nScrollInc) < 0 ) |
| 378 | nScrollInc = -m_yScrollPosition; // As -ve as we can go |
| 379 | else if ( (m_yScrollPosition + nScrollInc) > noPositions ) |
| 380 | nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go |
| 381 | } |
| 382 | else |
| 383 | m_targetWindow->Refresh(); |
| 384 | } |
| 385 | |
| 386 | return nScrollInc; |
| 387 | } |
| 388 | |
| 389 | // Adjust the scrollbars - new version. |
| 390 | void wxScrolledWindow::AdjustScrollbars() |
| 391 | { |
| 392 | int w, h; |
| 393 | m_targetWindow->GetClientSize(&w, &h); |
| 394 | |
| 395 | int oldXScroll = m_xScrollPosition; |
| 396 | int oldYScroll = m_yScrollPosition; |
| 397 | |
| 398 | if (m_xScrollLines > 0) |
| 399 | { |
| 400 | // Calculate page size i.e. number of scroll units you get on the |
| 401 | // current client window |
| 402 | int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 ); |
| 403 | if (noPagePositions < 1) noPagePositions = 1; |
| 404 | |
| 405 | // Correct position if greater than extent of canvas minus |
| 406 | // the visible portion of it or if below zero |
| 407 | m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition); |
| 408 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); |
| 409 | |
| 410 | SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines); |
| 411 | // The amount by which we scroll when paging |
| 412 | SetScrollPageSize(wxHORIZONTAL, noPagePositions); |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | m_xScrollPosition = 0; |
| 417 | SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE); |
| 418 | } |
| 419 | |
| 420 | if (m_yScrollLines > 0) |
| 421 | { |
| 422 | // Calculate page size i.e. number of scroll units you get on the |
| 423 | // current client window |
| 424 | int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 ); |
| 425 | if (noPagePositions < 1) noPagePositions = 1; |
| 426 | |
| 427 | // Correct position if greater than extent of canvas minus |
| 428 | // the visible portion of it or if below zero |
| 429 | m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition ); |
| 430 | m_yScrollPosition = wxMax( 0, m_yScrollPosition ); |
| 431 | |
| 432 | SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines); |
| 433 | // The amount by which we scroll when paging |
| 434 | SetScrollPageSize(wxVERTICAL, noPagePositions); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | m_yScrollPosition = 0; |
| 439 | SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE); |
| 440 | } |
| 441 | |
| 442 | if (oldXScroll != m_xScrollPosition) |
| 443 | { |
| 444 | if (m_xScrollingEnabled) |
| 445 | m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL ); |
| 446 | else |
| 447 | m_targetWindow->Refresh(); |
| 448 | } |
| 449 | |
| 450 | if (oldYScroll != m_yScrollPosition) |
| 451 | { |
| 452 | if (m_yScrollingEnabled) |
| 453 | m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL ); |
| 454 | else |
| 455 | m_targetWindow->Refresh(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Override this function if you don't want to have wxScrolledWindow |
| 460 | // automatically change the origin according to the scroll position. |
| 461 | void wxScrolledWindow::PrepareDC(wxDC& dc) |
| 462 | { |
| 463 | dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine, |
| 464 | -m_yScrollPosition * m_yScrollPixelsPerLine ); |
| 465 | dc.SetUserScale( m_scaleX, m_scaleY ); |
| 466 | } |
| 467 | |
| 468 | #if WXWIN_COMPATIBILITY |
| 469 | void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const |
| 470 | { |
| 471 | *x_page = GetScrollPageSize(wxHORIZONTAL); |
| 472 | *y_page = GetScrollPageSize(wxVERTICAL); |
| 473 | } |
| 474 | |
| 475 | void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const |
| 476 | { |
| 477 | if ( xx ) |
| 478 | *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine); |
| 479 | if ( yy ) |
| 480 | *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine); |
| 481 | } |
| 482 | #endif // WXWIN_COMPATIBILITY |
| 483 | |
| 484 | void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const |
| 485 | { |
| 486 | if ( x_unit ) |
| 487 | *x_unit = m_xScrollPixelsPerLine; |
| 488 | if ( y_unit ) |
| 489 | *y_unit = m_yScrollPixelsPerLine; |
| 490 | } |
| 491 | |
| 492 | int wxScrolledWindow::GetScrollPageSize(int orient) const |
| 493 | { |
| 494 | if ( orient == wxHORIZONTAL ) |
| 495 | return m_xScrollLinesPerPage; |
| 496 | else |
| 497 | return m_yScrollLinesPerPage; |
| 498 | } |
| 499 | |
| 500 | void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize) |
| 501 | { |
| 502 | if ( orient == wxHORIZONTAL ) |
| 503 | m_xScrollLinesPerPage = pageSize; |
| 504 | else |
| 505 | m_yScrollLinesPerPage = pageSize; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Scroll to given position (scroll position, not pixel position) |
| 510 | */ |
| 511 | void wxScrolledWindow::Scroll( int x_pos, int y_pos ) |
| 512 | { |
| 513 | if (!m_targetWindow) |
| 514 | return; |
| 515 | |
| 516 | if (((x_pos == -1) || (x_pos == m_xScrollPosition)) && |
| 517 | ((y_pos == -1) || (y_pos == m_yScrollPosition))) return; |
| 518 | |
| 519 | int w, h; |
| 520 | m_targetWindow->GetClientSize(&w, &h); |
| 521 | |
| 522 | if ((x_pos != -1) && (m_xScrollPixelsPerLine)) |
| 523 | { |
| 524 | int old_x = m_xScrollPosition; |
| 525 | m_xScrollPosition = x_pos; |
| 526 | |
| 527 | // Calculate page size i.e. number of scroll units you get on the |
| 528 | // current client window |
| 529 | int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 ); |
| 530 | if (noPagePositions < 1) noPagePositions = 1; |
| 531 | |
| 532 | // Correct position if greater than extent of canvas minus |
| 533 | // the visible portion of it or if below zero |
| 534 | m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition ); |
| 535 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); |
| 536 | |
| 537 | if (old_x == m_xScrollPosition) return; |
| 538 | |
| 539 | m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE ); |
| 540 | m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 ); |
| 541 | } |
| 542 | if ((y_pos != -1) && (m_yScrollPixelsPerLine)) |
| 543 | { |
| 544 | int old_y = m_yScrollPosition; |
| 545 | m_yScrollPosition = y_pos; |
| 546 | |
| 547 | // Calculate page size i.e. number of scroll units you get on the |
| 548 | // current client window |
| 549 | int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 ); |
| 550 | if (noPagePositions < 1) noPagePositions = 1; |
| 551 | |
| 552 | // Correct position if greater than extent of canvas minus |
| 553 | // the visible portion of it or if below zero |
| 554 | m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition ); |
| 555 | m_yScrollPosition = wxMax( 0, m_yScrollPosition ); |
| 556 | |
| 557 | if (old_y == m_yScrollPosition) return; |
| 558 | |
| 559 | m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE ); |
| 560 | m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine ); |
| 561 | } |
| 562 | |
| 563 | #ifdef __WXMAC__ |
| 564 | m_targetWindow->MacUpdateImmediately(); |
| 565 | #endif |
| 566 | } |
| 567 | |
| 568 | void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll) |
| 569 | { |
| 570 | m_xScrollingEnabled = x_scroll; |
| 571 | m_yScrollingEnabled = y_scroll; |
| 572 | } |
| 573 | |
| 574 | void wxScrolledWindow::GetVirtualSize (int *x, int *y) const |
| 575 | { |
| 576 | if ( x ) |
| 577 | *x = m_xScrollPixelsPerLine * m_xScrollLines; |
| 578 | if ( y ) |
| 579 | *y = m_yScrollPixelsPerLine * m_yScrollLines; |
| 580 | } |
| 581 | |
| 582 | // Where the current view starts from |
| 583 | void wxScrolledWindow::GetViewStart (int *x, int *y) const |
| 584 | { |
| 585 | if ( x ) |
| 586 | *x = m_xScrollPosition; |
| 587 | if ( y ) |
| 588 | *y = m_yScrollPosition; |
| 589 | } |
| 590 | |
| 591 | void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const |
| 592 | { |
| 593 | if ( xx ) |
| 594 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; |
| 595 | if ( yy ) |
| 596 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; |
| 597 | } |
| 598 | |
| 599 | void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const |
| 600 | { |
| 601 | if ( xx ) |
| 602 | *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine; |
| 603 | if ( yy ) |
| 604 | *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine; |
| 605 | } |
| 606 | |
| 607 | // ---------------------------------------------------------------------------- |
| 608 | // event handlers |
| 609 | // ---------------------------------------------------------------------------- |
| 610 | |
| 611 | // Default OnSize resets scrollbars, if any |
| 612 | void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event)) |
| 613 | { |
| 614 | #if wxUSE_CONSTRAINTS |
| 615 | if (GetAutoLayout()) |
| 616 | Layout(); |
| 617 | #endif |
| 618 | |
| 619 | AdjustScrollbars(); |
| 620 | } |
| 621 | |
| 622 | // This calls OnDraw, having adjusted the origin according to the current |
| 623 | // scroll position |
| 624 | void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) |
| 625 | { |
| 626 | wxPaintDC dc(this); |
| 627 | PrepareDC(dc); |
| 628 | |
| 629 | OnDraw(dc); |
| 630 | } |
| 631 | |
| 632 | // kbd handling: notice that we use OnChar() and not OnKeyDown() for |
| 633 | // compatibility here - if we used OnKeyDown(), the programs which process |
| 634 | // arrows themselves in their OnChar() would never get the message and like |
| 635 | // this they always have the priority |
| 636 | void wxScrolledWindow::OnChar(wxKeyEvent& event) |
| 637 | { |
| 638 | int stx, sty, // view origin |
| 639 | szx, szy, // view size (total) |
| 640 | clix, cliy; // view size (on screen) |
| 641 | |
| 642 | ViewStart(&stx, &sty); |
| 643 | GetClientSize(&clix, &cliy); |
| 644 | GetVirtualSize(&szx, &szy); |
| 645 | |
| 646 | if( m_xScrollPixelsPerLine ) |
| 647 | { |
| 648 | clix /= m_xScrollPixelsPerLine; |
| 649 | szx /= m_xScrollPixelsPerLine; |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | clix = 0; |
| 654 | szx = -1; |
| 655 | } |
| 656 | if( m_yScrollPixelsPerLine ) |
| 657 | { |
| 658 | cliy /= m_yScrollPixelsPerLine; |
| 659 | szy /= m_yScrollPixelsPerLine; |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | cliy = 0; |
| 664 | szy = -1; |
| 665 | } |
| 666 | |
| 667 | int dsty; |
| 668 | switch ( event.KeyCode() ) |
| 669 | { |
| 670 | case WXK_PAGEUP: |
| 671 | case WXK_PRIOR: |
| 672 | dsty = sty - (5 * cliy / 6); |
| 673 | Scroll(-1, (dsty == -1) ? 0 : dsty); |
| 674 | break; |
| 675 | |
| 676 | case WXK_PAGEDOWN: |
| 677 | case WXK_NEXT: |
| 678 | Scroll(-1, sty + (5 * cliy / 6)); |
| 679 | break; |
| 680 | |
| 681 | case WXK_HOME: |
| 682 | Scroll(0, event.ControlDown() ? 0 : -1); |
| 683 | break; |
| 684 | |
| 685 | case WXK_END: |
| 686 | Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1); |
| 687 | break; |
| 688 | |
| 689 | case WXK_UP: |
| 690 | Scroll(-1, sty - 1); |
| 691 | break; |
| 692 | |
| 693 | case WXK_DOWN: |
| 694 | Scroll(-1, sty + 1); |
| 695 | break; |
| 696 | |
| 697 | case WXK_LEFT: |
| 698 | Scroll(stx - 1, -1); |
| 699 | break; |
| 700 | |
| 701 | case WXK_RIGHT: |
| 702 | Scroll(stx + 1, -1); |
| 703 | break; |
| 704 | |
| 705 | default: |
| 706 | // not for us |
| 707 | event.Skip(); |
| 708 | } |
| 709 | } |