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