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