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