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