]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrlwing.cpp
merged MapBitmap() from 2.2 branch
[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 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->GetBackingPixmap() &&
323 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
324 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
325 {
326 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
327 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
328 }
329
330 if (!m_targetWindow->GetBackingPixmap() &&
331 (noUnitsX != 0) && (noUnitsY != 0))
332 {
333 int depth = wxDisplayDepth();
334 m_targetWindow->SetPixmapWidth(totalPixelWidth);
335 m_targetWindow->SetPixmapHeight(totalPixelHeight);
336 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
337 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), 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);
388 }
389 else
390 {
391 m_yScrollPosition += nScrollInc;
392 m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition);
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 );
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 );
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 xScrollOld = m_xScrollPosition,
813 yScrollOld = m_yScrollPosition;
814
815 int dsty;
816 switch ( event.KeyCode() )
817 {
818 case WXK_PAGEUP:
819 case WXK_PRIOR:
820 dsty = sty - (5 * cliy / 6);
821 Scroll(-1, (dsty == -1) ? 0 : dsty);
822 break;
823
824 case WXK_PAGEDOWN:
825 case WXK_NEXT:
826 Scroll(-1, sty + (5 * cliy / 6));
827 break;
828
829 case WXK_HOME:
830 Scroll(0, event.ControlDown() ? 0 : -1);
831 break;
832
833 case WXK_END:
834 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
835 break;
836
837 case WXK_UP:
838 Scroll(-1, sty - 1);
839 break;
840
841 case WXK_DOWN:
842 Scroll(-1, sty + 1);
843 break;
844
845 case WXK_LEFT:
846 Scroll(stx - 1, -1);
847 break;
848
849 case WXK_RIGHT:
850 Scroll(stx + 1, -1);
851 break;
852
853 default:
854 // not for us
855 event.Skip();
856 }
857
858 if ( m_xScrollPosition != xScrollOld )
859 {
860 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
861 wxHORIZONTAL);
862 event.SetEventObject(m_win);
863 m_win->GetEventHandler()->ProcessEvent(event);
864 }
865
866 if ( m_yScrollPosition != yScrollOld )
867 {
868 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
869 wxVERTICAL);
870 event.SetEventObject(m_win);
871 m_win->GetEventHandler()->ProcessEvent(event);
872 }
873 }
874
875 // ----------------------------------------------------------------------------
876 // autoscroll stuff: these functions deal with sending fake scroll events when
877 // a captured mouse is being held outside the window
878 // ----------------------------------------------------------------------------
879
880 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
881 {
882 // only send the event if the window is scrollable in this direction
883 wxWindow *win = (wxWindow *)event.GetEventObject();
884 return win->HasScrollbar(event.GetOrientation());
885 }
886
887 void wxScrollHelper::StopAutoScrolling()
888 {
889 if ( m_timerAutoScroll )
890 {
891 delete m_timerAutoScroll;
892 m_timerAutoScroll = (wxTimer *)NULL;
893 }
894 }
895
896 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
897 {
898 StopAutoScrolling();
899
900 event.Skip();
901 }
902
903 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
904 {
905 // don't prevent the usual processing of the event from taking place
906 event.Skip();
907
908 // when a captured mouse leave a scrolled window we start generate
909 // scrolling events to allow, for example, extending selection beyond the
910 // visible area in some controls
911 if ( wxWindow::GetCapture() == m_targetWindow )
912 {
913 // where is the mouse leaving?
914 int pos, orient;
915 wxPoint pt = event.GetPosition();
916 if ( pt.x < 0 )
917 {
918 orient = wxHORIZONTAL;
919 pos = 0;
920 }
921 else if ( pt.y < 0 )
922 {
923 orient = wxVERTICAL;
924 pos = 0;
925 }
926 else // we're lower or to the right of the window
927 {
928 wxSize size = m_targetWindow->GetClientSize();
929 if ( pt.x > size.x )
930 {
931 orient = wxHORIZONTAL;
932 pos = m_xScrollLines;
933 }
934 else if ( pt.y > size.y )
935 {
936 orient = wxVERTICAL;
937 pos = m_yScrollLines;
938 }
939 else // this should be impossible
940 {
941 // but seems to happen sometimes under wxMSW - maybe it's a bug
942 // there but for now just ignore it
943
944 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
945
946 return;
947 }
948 }
949
950 // only start the auto scroll timer if the window can be scrolled in
951 // this direction
952 if ( !m_targetWindow->HasScrollbar(orient) )
953 return;
954
955 delete m_timerAutoScroll;
956 m_timerAutoScroll = new wxAutoScrollTimer
957 (
958 m_targetWindow, this,
959 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
960 : wxEVT_SCROLLWIN_LINEDOWN,
961 pos,
962 orient
963 );
964 m_timerAutoScroll->Start(50); // FIXME: make configurable
965 }
966 }
967
968 #if wxUSE_MOUSEWHEEL
969
970 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
971 {
972 m_wheelRotation += event.GetWheelRotation();
973 int lines = m_wheelRotation / event.GetWheelDelta();
974 m_wheelRotation -= lines * event.GetWheelDelta();
975
976 if (lines != 0)
977 {
978 lines *= event.GetLinesPerAction();
979
980 int vsx, vsy;
981 GetViewStart(&vsx, &vsy);
982 Scroll(-1, vsy - lines);
983 }
984 }
985
986 #endif // wxUSE_MOUSEWHEEL
987
988 // ----------------------------------------------------------------------------
989 // wxGenericScrolledWindow implementation
990 // ----------------------------------------------------------------------------
991
992 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
993
994 bool wxGenericScrolledWindow::Create(wxWindow *parent,
995 wxWindowID id,
996 const wxPoint& pos,
997 const wxSize& size,
998 long style,
999 const wxString& name)
1000 {
1001 m_targetWindow = this;
1002
1003 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1004
1005 #ifdef __WXMSW__
1006 // we need to process arrows ourselves for scrolling
1007 m_lDlgCode |= DLGC_WANTARROWS;
1008 #endif // __WXMSW__
1009
1010 return ok;
1011 }
1012
1013 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1014 {
1015 }
1016
1017 #if WXWIN_COMPATIBILITY
1018 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1019 {
1020 *x_page = GetScrollPageSize(wxHORIZONTAL);
1021 *y_page = GetScrollPageSize(wxVERTICAL);
1022 }
1023
1024 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1025 {
1026 if ( xx )
1027 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1028 if ( yy )
1029 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1030 }
1031 #endif // WXWIN_COMPATIBILITY
1032
1033 #endif // !wxGTK
1034