]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
changed Refresh to take Rect as client coordinates not window coordinates
[wxWidgets.git] / src / generic / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxGenericScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
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 #ifdef __VMS
25 #define XtDisplay XTDISPLAY
26 #endif
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #include "wx/utils.h"
36 #include "wx/dcclient.h"
37
38 #include "wx/scrolwin.h"
39 #include "wx/panel.h"
40 #include "wx/timer.h"
41
42 #ifdef __WXMSW__
43 #include <windows.h> // for DLGC_WANTARROWS
44 #endif
45
46 #ifdef __WXMOTIF__
47 // For wxRETAINED implementation
48 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
49 //This code switches off the compiler warnings
50 # pragma message disable nosimpint
51 #endif
52 #include <Xm/Xm.h>
53 #ifdef __VMS__
54 # pragma message enable nosimpint
55 #endif
56 #endif
57
58 #ifndef __WXGTK__
59 IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // wxScrollHelperEvtHandler: intercept the events from the window and forward
64 // them to wxScrollHelper
65 // ----------------------------------------------------------------------------
66
67 class wxScrollHelperEvtHandler : public wxEvtHandler
68 {
69 public:
70 wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper)
71 {
72 m_scrollHelper = scrollHelper;
73 }
74
75 virtual bool ProcessEvent(wxEvent& event);
76
77 private:
78 wxScrollHelper *m_scrollHelper;
79 };
80
81 // ----------------------------------------------------------------------------
82 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
83 // a captured mouse is held outside the window
84 // ----------------------------------------------------------------------------
85
86 class wxAutoScrollTimer : public wxTimer
87 {
88 public:
89 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
90 wxEventType eventTypeToSend,
91 int pos, int orient);
92
93 virtual void Notify();
94
95 private:
96 wxWindow *m_win;
97 wxScrollHelper *m_scrollHelper;
98 wxEventType m_eventType;
99 int m_pos,
100 m_orient;
101 };
102
103 // ============================================================================
104 // implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // wxAutoScrollTimer
109 // ----------------------------------------------------------------------------
110
111 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
112 wxScrollHelper *scroll,
113 wxEventType eventTypeToSend,
114 int pos, int orient)
115 {
116 m_win = winToScroll;
117 m_scrollHelper = scroll;
118 m_eventType = eventTypeToSend;
119 m_pos = pos;
120 m_orient = orient;
121 }
122
123 void wxAutoScrollTimer::Notify()
124 {
125 // only do all this as long as the window is capturing the mouse
126 if ( wxWindow::GetCapture() != m_win )
127 {
128 Stop();
129 }
130 else // we still capture the mouse, continue generating events
131 {
132 // first scroll the window if we are allowed to do it
133 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
134 event1.SetEventObject(m_win);
135 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
136 m_win->GetEventHandler()->ProcessEvent(event1) )
137 {
138 // and then send a pseudo mouse-move event to refresh the selection
139 wxMouseEvent event2(wxEVT_MOTION);
140 wxGetMousePosition(&event2.m_x, &event2.m_y);
141
142 // the mouse event coordinates should be client, not screen as
143 // returned by wxGetMousePosition
144 wxWindow *parentTop = m_win;
145 while ( parentTop->GetParent() )
146 parentTop = parentTop->GetParent();
147 wxPoint ptOrig = parentTop->GetPosition();
148 event2.m_x -= ptOrig.x;
149 event2.m_y -= ptOrig.y;
150
151 event2.SetEventObject(m_win);
152
153 // FIXME: we don't fill in the other members - ok?
154
155 m_win->GetEventHandler()->ProcessEvent(event2);
156 }
157 else // can't scroll further, stop
158 {
159 Stop();
160 }
161 }
162 }
163
164 // ----------------------------------------------------------------------------
165 // wxScrollHelperEvtHandler
166 // ----------------------------------------------------------------------------
167
168 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
169 {
170 if ( wxEvtHandler::ProcessEvent(event) )
171 return TRUE;
172
173 // reset the skipped flag to FALSE as it might have been set to TRUE in
174 // ProcessEvent() above
175 event.Skip(FALSE);
176
177 wxEventType evType = event.GetEventType();
178
179 if ( evType == wxEVT_PAINT )
180 {
181 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
182 return TRUE;
183 }
184
185 if ( evType == wxEVT_SCROLLWIN_TOP ||
186 evType == wxEVT_SCROLLWIN_BOTTOM ||
187 evType == wxEVT_SCROLLWIN_LINEUP ||
188 evType == wxEVT_SCROLLWIN_LINEDOWN ||
189 evType == wxEVT_SCROLLWIN_PAGEUP ||
190 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
191 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
192 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
193 {
194 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
195 return !event.GetSkipped();
196 }
197
198 if ( evType == wxEVT_ENTER_WINDOW )
199 {
200 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
201 }
202 else if ( evType == wxEVT_LEAVE_WINDOW )
203 {
204 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
205 }
206 #if wxUSE_MOUSEWHEEL
207 else if ( evType == wxEVT_MOUSEWHEEL )
208 {
209 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
210 }
211 #endif // wxUSE_MOUSEWHEEL
212 else if ( evType == wxEVT_SIZE )
213 {
214 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
215 }
216 else if ( evType == wxEVT_CHAR )
217 {
218 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
219 return !event.GetSkipped();
220 }
221
222 return FALSE;
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxScrollHelper construction
227 // ----------------------------------------------------------------------------
228
229 wxScrollHelper::wxScrollHelper(wxWindow *win)
230 {
231 m_xScrollPixelsPerLine =
232 m_yScrollPixelsPerLine =
233 m_xScrollPosition =
234 m_yScrollPosition =
235 m_xScrollLines =
236 m_yScrollLines =
237 m_xScrollLinesPerPage =
238 m_yScrollLinesPerPage = 0;
239
240 m_xScrollingEnabled =
241 m_yScrollingEnabled = TRUE;
242
243 m_scaleX =
244 m_scaleY = 1.0;
245 #if wxUSE_MOUSEWHEEL
246 m_wheelRotation = 0;
247 #endif
248
249 m_win =
250 m_targetWindow = (wxWindow *)NULL;
251
252 m_timerAutoScroll = (wxTimer *)NULL;
253
254 if ( win )
255 SetWindow(win);
256 }
257
258 void wxScrollHelper::SetWindow(wxWindow *win)
259 {
260 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
261
262 m_targetWindow = m_win = win;
263
264 // install the event handler which will intercept the events we're
265 // interested in
266 m_win->PushEventHandler(new wxScrollHelperEvtHandler(this));
267 }
268
269 wxScrollHelper::~wxScrollHelper()
270 {
271 StopAutoScrolling();
272
273 if ( m_targetWindow )
274 m_targetWindow->PopEventHandler(TRUE /* do delete it */);
275 }
276
277 // ----------------------------------------------------------------------------
278 // setting scrolling parameters
279 // ----------------------------------------------------------------------------
280
281 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
282 int pixelsPerUnitY,
283 int noUnitsX,
284 int noUnitsY,
285 int xPos,
286 int yPos,
287 bool noRefresh)
288 {
289 int xpos, ypos;
290
291 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
292 bool do_refresh =
293 (
294 (noUnitsX != 0 && m_xScrollLines == 0) ||
295 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
296
297 (noUnitsY != 0 && m_yScrollLines == 0) ||
298 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
299 (xPos != m_xScrollPosition) ||
300 (yPos != m_yScrollPosition)
301 );
302
303 m_xScrollPixelsPerLine = pixelsPerUnitX;
304 m_yScrollPixelsPerLine = pixelsPerUnitY;
305 m_xScrollPosition = xPos;
306 m_yScrollPosition = yPos;
307 m_xScrollLines = noUnitsX;
308 m_yScrollLines = noUnitsY;
309
310 #ifdef __WXMOTIF__
311 // Sorry, some Motif-specific code to implement a backing pixmap
312 // for the wxRETAINED style. Implementing a backing store can't
313 // be entirely generic because it relies on the wxWindowDC implementation
314 // to duplicate X drawing calls for the backing pixmap.
315
316 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
317 {
318 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
319
320 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
321 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
322 if (m_targetWindow->m_backingPixmap &&
323 !((m_targetWindow->m_pixmapWidth == totalPixelWidth) &&
324 (m_targetWindow->m_pixmapHeight == totalPixelHeight)))
325 {
326 XFreePixmap (dpy, (Pixmap) m_targetWindow->m_backingPixmap);
327 m_targetWindow->m_backingPixmap = (WXPixmap) 0;
328 }
329
330 if (!m_targetWindow->m_backingPixmap &&
331 (noUnitsX != 0) && (noUnitsY != 0))
332 {
333 int depth = wxDisplayDepth();
334 m_pixmapWidth = totalPixelWidth;
335 m_pixmapHeight = totalPixelHeight;
336 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
337 m_pixmapWidth, m_pixmapHeight, depth);
338 }
339
340 }
341 #endif // Motif
342
343 AdjustScrollbars();
344
345 if (do_refresh && !noRefresh)
346 m_targetWindow->Refresh(TRUE, GetRect());
347
348 #ifdef __WXMAC__
349 m_targetWindow->MacUpdateImmediately() ;
350 #endif
351 }
352
353 // ----------------------------------------------------------------------------
354 // target window handling
355 // ----------------------------------------------------------------------------
356
357 void wxScrollHelper::SetTargetWindow( wxWindow *target )
358 {
359 wxASSERT_MSG( target, wxT("target window must not be NULL") );
360 m_targetWindow = target;
361 }
362
363 wxWindow *wxScrollHelper::GetTargetWindow() const
364 {
365 return m_targetWindow;
366 }
367
368 // ----------------------------------------------------------------------------
369 // scrolling implementation itself
370 // ----------------------------------------------------------------------------
371
372 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
373 {
374 int nScrollInc = CalcScrollInc(event);
375 if ( nScrollInc == 0 )
376 {
377 // can't scroll further
378 event.Skip();
379
380 return;
381 }
382
383 int orient = event.GetOrientation();
384 if (orient == wxHORIZONTAL)
385 {
386 m_xScrollPosition += nScrollInc;
387 m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition, FALSE);
388 }
389 else
390 {
391 m_yScrollPosition += nScrollInc;
392 m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition, FALSE);
393 }
394
395 bool needsRefresh = FALSE;
396 int dx = 0,
397 dy = 0;
398 if (orient == wxHORIZONTAL)
399 {
400 if ( m_xScrollingEnabled )
401 {
402 dx = -m_xScrollPixelsPerLine * nScrollInc;
403 }
404 else
405 {
406 needsRefresh = TRUE;
407 }
408 }
409 else
410 {
411 if ( m_yScrollingEnabled )
412 {
413 dy = -m_yScrollPixelsPerLine * nScrollInc;
414 }
415 else
416 {
417 needsRefresh = TRUE;
418 }
419 }
420
421 if ( needsRefresh )
422 {
423 m_targetWindow->Refresh(TRUE, GetRect());
424 }
425 else
426 {
427 m_targetWindow->ScrollWindow(dx, dy, GetRect());
428 }
429
430 #ifdef __WXMAC__
431 m_targetWindow->MacUpdateImmediately() ;
432 #endif
433 }
434
435 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
436 {
437 int pos = event.GetPosition();
438 int orient = event.GetOrientation();
439
440 int nScrollInc = 0;
441 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
442 {
443 if (orient == wxHORIZONTAL)
444 nScrollInc = - m_xScrollPosition;
445 else
446 nScrollInc = - m_yScrollPosition;
447 } else
448 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
449 {
450 if (orient == wxHORIZONTAL)
451 nScrollInc = m_xScrollLines - m_xScrollPosition;
452 else
453 nScrollInc = m_yScrollLines - m_yScrollPosition;
454 } else
455 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
456 {
457 nScrollInc = -1;
458 } else
459 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
460 {
461 nScrollInc = 1;
462 } else
463 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
464 {
465 if (orient == wxHORIZONTAL)
466 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
467 else
468 nScrollInc = -GetScrollPageSize(wxVERTICAL);
469 } else
470 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
471 {
472 if (orient == wxHORIZONTAL)
473 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
474 else
475 nScrollInc = GetScrollPageSize(wxVERTICAL);
476 } else
477 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
478 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
479 {
480 if (orient == wxHORIZONTAL)
481 nScrollInc = pos - m_xScrollPosition;
482 else
483 nScrollInc = pos - m_yScrollPosition;
484 }
485
486 if (orient == wxHORIZONTAL)
487 {
488 if (m_xScrollPixelsPerLine > 0)
489 {
490 int w, h;
491 GetTargetSize(&w, &h);
492
493 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
494 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
495 if (noPositions < 0)
496 noPositions = 0;
497
498 if ( (m_xScrollPosition + nScrollInc) < 0 )
499 nScrollInc = -m_xScrollPosition; // As -ve as we can go
500 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
501 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
502 }
503 else
504 m_targetWindow->Refresh(TRUE, GetRect());
505 }
506 else
507 {
508 if (m_yScrollPixelsPerLine > 0)
509 {
510 int w, h;
511 GetTargetSize(&w, &h);
512
513 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
514 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
515 if (noPositions < 0)
516 noPositions = 0;
517
518 if ( (m_yScrollPosition + nScrollInc) < 0 )
519 nScrollInc = -m_yScrollPosition; // As -ve as we can go
520 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
521 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
522 }
523 else
524 m_targetWindow->Refresh(TRUE, GetRect());
525 }
526
527 return nScrollInc;
528 }
529
530 // Adjust the scrollbars - new version.
531 void wxScrollHelper::AdjustScrollbars()
532 {
533 #ifdef __WXMAC__
534 m_targetWindow->MacUpdateImmediately();
535 #endif
536
537 int w, h;
538 GetTargetSize(&w, &h);
539
540 int oldXScroll = m_xScrollPosition;
541 int oldYScroll = m_yScrollPosition;
542
543 if (m_xScrollLines > 0)
544 {
545 // Calculate page size i.e. number of scroll units you get on the
546 // current client window
547 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
548 if (noPagePositions < 1) noPagePositions = 1;
549 if ( noPagePositions > m_xScrollLines )
550 noPagePositions = m_xScrollLines;
551
552 // Correct position if greater than extent of canvas minus
553 // the visible portion of it or if below zero
554 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
555 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
556
557 m_targetWindow->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
558 // The amount by which we scroll when paging
559 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
560 }
561 else
562 {
563 m_xScrollPosition = 0;
564 m_targetWindow->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
565 }
566
567 if (m_yScrollLines > 0)
568 {
569 // Calculate page size i.e. number of scroll units you get on the
570 // current client window
571 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
572 if (noPagePositions < 1) noPagePositions = 1;
573 if ( noPagePositions > m_yScrollLines )
574 noPagePositions = m_yScrollLines;
575
576 // Correct position if greater than extent of canvas minus
577 // the visible portion of it or if below zero
578 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
579 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
580
581 m_targetWindow->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
582 // The amount by which we scroll when paging
583 SetScrollPageSize(wxVERTICAL, noPagePositions);
584 }
585 else
586 {
587 m_yScrollPosition = 0;
588 m_targetWindow->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
589 }
590
591 if (oldXScroll != m_xScrollPosition)
592 {
593 if (m_xScrollingEnabled)
594 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
595 GetRect() );
596 else
597 m_targetWindow->Refresh(TRUE, GetRect());
598 }
599
600 if (oldYScroll != m_yScrollPosition)
601 {
602 if (m_yScrollingEnabled)
603 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
604 GetRect() );
605 else
606 m_targetWindow->Refresh(TRUE, GetRect());
607 }
608
609 #ifdef __WXMAC__
610 m_targetWindow->MacUpdateImmediately();
611 #endif
612 }
613
614 void wxScrollHelper::DoPrepareDC(wxDC& dc)
615 {
616 wxPoint pt = dc.GetDeviceOrigin();
617 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
618 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
619 dc.SetUserScale( m_scaleX, m_scaleY );
620 }
621
622 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
623 {
624 if ( x_unit )
625 *x_unit = m_xScrollPixelsPerLine;
626 if ( y_unit )
627 *y_unit = m_yScrollPixelsPerLine;
628 }
629
630 int wxScrollHelper::GetScrollPageSize(int orient) const
631 {
632 if ( orient == wxHORIZONTAL )
633 return m_xScrollLinesPerPage;
634 else
635 return m_yScrollLinesPerPage;
636 }
637
638 void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
639 {
640 if ( orient == wxHORIZONTAL )
641 m_xScrollLinesPerPage = pageSize;
642 else
643 m_yScrollLinesPerPage = pageSize;
644 }
645
646 /*
647 * Scroll to given position (scroll position, not pixel position)
648 */
649 void wxScrollHelper::Scroll( int x_pos, int y_pos )
650 {
651 if (!m_targetWindow)
652 return;
653
654 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
655 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
656
657 #ifdef __WXMAC__
658 m_targetWindow->MacUpdateImmediately();
659 #endif
660
661 int w, h;
662 GetTargetSize(&w, &h);
663
664 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
665 {
666 int old_x = m_xScrollPosition;
667 m_xScrollPosition = x_pos;
668
669 // Calculate page size i.e. number of scroll units you get on the
670 // current client window
671 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
672 if (noPagePositions < 1) noPagePositions = 1;
673
674 // Correct position if greater than extent of canvas minus
675 // the visible portion of it or if below zero
676 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
677 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
678
679 if (old_x != m_xScrollPosition) {
680 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, FALSE );
681 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
682 GetRect() );
683 }
684 }
685 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
686 {
687 int old_y = m_yScrollPosition;
688 m_yScrollPosition = y_pos;
689
690 // Calculate page size i.e. number of scroll units you get on the
691 // current client window
692 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
693 if (noPagePositions < 1) noPagePositions = 1;
694
695 // Correct position if greater than extent of canvas minus
696 // the visible portion of it or if below zero
697 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
698 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
699
700 if (old_y != m_yScrollPosition) {
701 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, FALSE );
702 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
703 GetRect() );
704 }
705 }
706
707 #ifdef __WXMAC__
708 m_targetWindow->MacUpdateImmediately();
709 #endif
710
711 }
712
713 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
714 {
715 m_xScrollingEnabled = x_scroll;
716 m_yScrollingEnabled = y_scroll;
717 }
718
719 void wxScrollHelper::GetVirtualSize (int *x, int *y) const
720 {
721 if ( x )
722 *x = m_xScrollPixelsPerLine * m_xScrollLines;
723 if ( y )
724 *y = m_yScrollPixelsPerLine * m_yScrollLines;
725 }
726
727 // Where the current view starts from
728 void wxScrollHelper::GetViewStart (int *x, int *y) const
729 {
730 if ( x )
731 *x = m_xScrollPosition;
732 if ( y )
733 *y = m_yScrollPosition;
734 }
735
736 void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
737 {
738 if ( xx )
739 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
740 if ( yy )
741 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
742 }
743
744 void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
745 {
746 if ( xx )
747 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
748 if ( yy )
749 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
750 }
751
752 // ----------------------------------------------------------------------------
753 // event handlers
754 // ----------------------------------------------------------------------------
755
756 // Default OnSize resets scrollbars, if any
757 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
758 {
759 #if wxUSE_CONSTRAINTS
760 if ( m_targetWindow->GetAutoLayout() )
761 m_targetWindow->Layout();
762 #endif
763
764 AdjustScrollbars();
765 }
766
767 // This calls OnDraw, having adjusted the origin according to the current
768 // scroll position
769 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
770 {
771 wxPaintDC dc(m_targetWindow);
772 DoPrepareDC(dc);
773
774 OnDraw(dc);
775 }
776
777 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
778 // compatibility here - if we used OnKeyDown(), the programs which process
779 // arrows themselves in their OnChar() would never get the message and like
780 // this they always have the priority
781 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
782 {
783 int stx, sty, // view origin
784 szx, szy, // view size (total)
785 clix, cliy; // view size (on screen)
786
787 GetViewStart(&stx, &sty);
788 GetTargetSize(&clix, &cliy);
789 GetVirtualSize(&szx, &szy);
790
791 if( m_xScrollPixelsPerLine )
792 {
793 clix /= m_xScrollPixelsPerLine;
794 szx /= m_xScrollPixelsPerLine;
795 }
796 else
797 {
798 clix = 0;
799 szx = -1;
800 }
801 if( m_yScrollPixelsPerLine )
802 {
803 cliy /= m_yScrollPixelsPerLine;
804 szy /= m_yScrollPixelsPerLine;
805 }
806 else
807 {
808 cliy = 0;
809 szy = -1;
810 }
811
812 int dsty;
813 switch ( event.KeyCode() )
814 {
815 case WXK_PAGEUP:
816 case WXK_PRIOR:
817 dsty = sty - (5 * cliy / 6);
818 Scroll(-1, (dsty == -1) ? 0 : dsty);
819 break;
820
821 case WXK_PAGEDOWN:
822 case WXK_NEXT:
823 Scroll(-1, sty + (5 * cliy / 6));
824 break;
825
826 case WXK_HOME:
827 Scroll(0, event.ControlDown() ? 0 : -1);
828 break;
829
830 case WXK_END:
831 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
832 break;
833
834 case WXK_UP:
835 Scroll(-1, sty - 1);
836 break;
837
838 case WXK_DOWN:
839 Scroll(-1, sty + 1);
840 break;
841
842 case WXK_LEFT:
843 Scroll(stx - 1, -1);
844 break;
845
846 case WXK_RIGHT:
847 Scroll(stx + 1, -1);
848 break;
849
850 default:
851 // not for us
852 event.Skip();
853 }
854 }
855
856 // ----------------------------------------------------------------------------
857 // autoscroll stuff: these functions deal with sending fake scroll events when
858 // a captured mouse is being held outside the window
859 // ----------------------------------------------------------------------------
860
861 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
862 {
863 // only send the event if the window is scrollable in this direction
864 wxWindow *win = (wxWindow *)event.GetEventObject();
865 return win->HasScrollbar(event.GetOrientation());
866 }
867
868 void wxScrollHelper::StopAutoScrolling()
869 {
870 if ( m_timerAutoScroll )
871 {
872 delete m_timerAutoScroll;
873 m_timerAutoScroll = (wxTimer *)NULL;
874 }
875 }
876
877 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
878 {
879 StopAutoScrolling();
880
881 event.Skip();
882 }
883
884 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
885 {
886 // don't prevent the usual processing of the event from taking place
887 event.Skip();
888
889 // when a captured mouse leave a scrolled window we start generate
890 // scrolling events to allow, for example, extending selection beyond the
891 // visible area in some controls
892 if ( wxWindow::GetCapture() == m_targetWindow )
893 {
894 // where is the mouse leaving?
895 int pos, orient;
896 wxPoint pt = event.GetPosition();
897 if ( pt.x < 0 )
898 {
899 orient = wxHORIZONTAL;
900 pos = 0;
901 }
902 else if ( pt.y < 0 )
903 {
904 orient = wxVERTICAL;
905 pos = 0;
906 }
907 else // we're lower or to the right of the window
908 {
909 wxSize size = m_targetWindow->GetClientSize();
910 if ( pt.x > size.x )
911 {
912 orient = wxHORIZONTAL;
913 pos = m_xScrollLines;
914 }
915 else if ( pt.y > size.y )
916 {
917 orient = wxVERTICAL;
918 pos = m_yScrollLines;
919 }
920 else // this should be impossible
921 {
922 // but seems to happen sometimes under wxMSW - maybe it's a bug
923 // there but for now just ignore it
924
925 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
926
927 return;
928 }
929 }
930
931 // only start the auto scroll timer if the window can be scrolled in
932 // this direction
933 if ( !m_targetWindow->HasScrollbar(orient) )
934 return;
935
936 delete m_timerAutoScroll;
937 m_timerAutoScroll = new wxAutoScrollTimer
938 (
939 m_targetWindow, this,
940 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
941 : wxEVT_SCROLLWIN_LINEDOWN,
942 pos,
943 orient
944 );
945 m_timerAutoScroll->Start(50); // FIXME: make configurable
946 }
947 }
948
949 #if wxUSE_MOUSEWHEEL
950
951 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
952 {
953 m_wheelRotation += event.GetWheelRotation();
954 int lines = m_wheelRotation / event.GetWheelDelta();
955 m_wheelRotation -= lines * event.GetWheelDelta();
956
957 if (lines != 0)
958 {
959 lines *= event.GetLinesPerAction();
960
961 int vsx, vsy;
962 GetViewStart(&vsx, &vsy);
963 Scroll(-1, vsy - lines);
964 }
965 }
966
967 #endif // wxUSE_MOUSEWHEEL
968
969 // ----------------------------------------------------------------------------
970 // wxGenericScrolledWindow implementation
971 // ----------------------------------------------------------------------------
972
973 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
974
975 bool wxGenericScrolledWindow::Create(wxWindow *parent,
976 wxWindowID id,
977 const wxPoint& pos,
978 const wxSize& size,
979 long style,
980 const wxString& name)
981 {
982 m_targetWindow = this;
983
984 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
985
986 #ifdef __WXMSW__
987 // we need to process arrows ourselves for scrolling
988 m_lDlgCode |= DLGC_WANTARROWS;
989 #endif // __WXMSW__
990
991 return ok;
992 }
993
994 wxGenericScrolledWindow::~wxGenericScrolledWindow()
995 {
996 }
997
998 #if WXWIN_COMPATIBILITY
999 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1000 {
1001 *x_page = GetScrollPageSize(wxHORIZONTAL);
1002 *y_page = GetScrollPageSize(wxVERTICAL);
1003 }
1004
1005 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1006 {
1007 if ( xx )
1008 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1009 if ( yy )
1010 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1011 }
1012 #endif // WXWIN_COMPATIBILITY
1013