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