]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrlwing.cpp
c17909a7094eac716811b2e668e0472398334997
[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 WXDLLEXPORT 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 void ResetDrawnFlag() { m_hasDrawnWindow = FALSE; }
78
79 private:
80 wxScrollHelper *m_scrollHelper;
81
82 bool m_hasDrawnWindow;
83 };
84
85 // ----------------------------------------------------------------------------
86 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
87 // a captured mouse is held outside the window
88 // ----------------------------------------------------------------------------
89
90 class wxAutoScrollTimer : public wxTimer
91 {
92 public:
93 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
94 wxEventType eventTypeToSend,
95 int pos, int orient);
96
97 virtual void Notify();
98
99 private:
100 wxWindow *m_win;
101 wxScrollHelper *m_scrollHelper;
102 wxEventType m_eventType;
103 int m_pos,
104 m_orient;
105 };
106
107 // ============================================================================
108 // implementation
109 // ============================================================================
110
111 // ----------------------------------------------------------------------------
112 // wxAutoScrollTimer
113 // ----------------------------------------------------------------------------
114
115 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
116 wxScrollHelper *scroll,
117 wxEventType eventTypeToSend,
118 int pos, int orient)
119 {
120 m_win = winToScroll;
121 m_scrollHelper = scroll;
122 m_eventType = eventTypeToSend;
123 m_pos = pos;
124 m_orient = orient;
125 }
126
127 void wxAutoScrollTimer::Notify()
128 {
129 // only do all this as long as the window is capturing the mouse
130 if ( wxWindow::GetCapture() != m_win )
131 {
132 Stop();
133 }
134 else // we still capture the mouse, continue generating events
135 {
136 // first scroll the window if we are allowed to do it
137 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
138 event1.SetEventObject(m_win);
139 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
140 m_win->GetEventHandler()->ProcessEvent(event1) )
141 {
142 // and then send a pseudo mouse-move event to refresh the selection
143 wxMouseEvent event2(wxEVT_MOTION);
144 wxGetMousePosition(&event2.m_x, &event2.m_y);
145
146 // the mouse event coordinates should be client, not screen as
147 // returned by wxGetMousePosition
148 wxWindow *parentTop = m_win;
149 while ( parentTop->GetParent() )
150 parentTop = parentTop->GetParent();
151 wxPoint ptOrig = parentTop->GetPosition();
152 event2.m_x -= ptOrig.x;
153 event2.m_y -= ptOrig.y;
154
155 event2.SetEventObject(m_win);
156
157 // FIXME: we don't fill in the other members - ok?
158
159 m_win->GetEventHandler()->ProcessEvent(event2);
160 }
161 else // can't scroll further, stop
162 {
163 Stop();
164 }
165 }
166 }
167
168 // ----------------------------------------------------------------------------
169 // wxScrollHelperEvtHandler
170 // ----------------------------------------------------------------------------
171
172 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
173 {
174 wxEventType evType = event.GetEventType();
175
176 // the explanation of wxEVT_PAINT processing hack: for historic reasons
177 // there are 2 ways to process this event in classes deriving from
178 // wxScrolledWindow. The user code may
179 //
180 // 1. override wxScrolledWindow::OnDraw(dc)
181 // 2. define its own OnPaint() handler
182 //
183 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
184 // always processes the draw event, so we can't just try the window
185 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
186 // (the latter would never be called in wxUniversal).
187 //
188 // So the solution is to have a flag telling us whether the user code drew
189 // anything in the window. We set it to true here but reset it to false in
190 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
191 // user code defined OnPaint() in the derived class)
192 m_hasDrawnWindow = TRUE;
193
194 // pass it on to the real handler
195 bool processed = wxEvtHandler::ProcessEvent(event);
196
197 // always process the size events ourselves, even if the user code handles
198 // them as well, as we need to AdjustScrollbars()
199 //
200 // NB: it is important to do it after processing the event in the normal
201 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
202 // scrollbar[s] (dis)appear and it should be seen by the user code
203 // after this one
204 if ( evType == wxEVT_SIZE )
205 {
206 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
207
208 return TRUE;
209 }
210
211 if ( processed )
212 {
213 // normally, nothing more to do here - except if it was a paint event
214 // which wasn't really processed, then we'll try to call our
215 // OnDraw() below (from HandleOnPaint)
216 if ( m_hasDrawnWindow )
217 {
218 return TRUE;
219 }
220 }
221
222 // reset the skipped flag to FALSE as it might have been set to TRUE in
223 // ProcessEvent() above
224 event.Skip(FALSE);
225
226 if ( evType == wxEVT_PAINT )
227 {
228 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
229 return TRUE;
230 }
231
232 if ( evType == wxEVT_SCROLLWIN_TOP ||
233 evType == wxEVT_SCROLLWIN_BOTTOM ||
234 evType == wxEVT_SCROLLWIN_LINEUP ||
235 evType == wxEVT_SCROLLWIN_LINEDOWN ||
236 evType == wxEVT_SCROLLWIN_PAGEUP ||
237 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
238 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
239 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
240 {
241 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
242 return !event.GetSkipped();
243 }
244
245 if ( evType == wxEVT_ENTER_WINDOW )
246 {
247 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
248 }
249 else if ( evType == wxEVT_LEAVE_WINDOW )
250 {
251 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
252 }
253 #if wxUSE_MOUSEWHEEL
254 else if ( evType == wxEVT_MOUSEWHEEL )
255 {
256 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
257 }
258 #endif // wxUSE_MOUSEWHEEL
259 else if ( evType == wxEVT_CHAR )
260 {
261 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
262 return !event.GetSkipped();
263 }
264
265 return FALSE;
266 }
267
268 // ----------------------------------------------------------------------------
269 // wxScrollHelper construction
270 // ----------------------------------------------------------------------------
271
272 wxScrollHelper::wxScrollHelper(wxWindow *win)
273 {
274 m_xScrollPixelsPerLine =
275 m_yScrollPixelsPerLine =
276 m_xScrollPosition =
277 m_yScrollPosition =
278 m_xScrollLines =
279 m_yScrollLines =
280 m_xScrollLinesPerPage =
281 m_yScrollLinesPerPage = 0;
282
283 m_xScrollingEnabled =
284 m_yScrollingEnabled = TRUE;
285
286 m_scaleX =
287 m_scaleY = 1.0;
288 #if wxUSE_MOUSEWHEEL
289 m_wheelRotation = 0;
290 #endif
291
292 m_win =
293 m_targetWindow = (wxWindow *)NULL;
294
295 m_timerAutoScroll = (wxTimer *)NULL;
296
297 m_handler = NULL;
298
299 if ( win )
300 SetWindow(win);
301 }
302
303 wxScrollHelper::~wxScrollHelper()
304 {
305 StopAutoScrolling();
306
307 DeleteEvtHandler();
308 }
309
310 // ----------------------------------------------------------------------------
311 // setting scrolling parameters
312 // ----------------------------------------------------------------------------
313
314 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
315 int pixelsPerUnitY,
316 int noUnitsX,
317 int noUnitsY,
318 int xPos,
319 int yPos,
320 bool noRefresh)
321 {
322 int xpos, ypos;
323
324 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
325 bool do_refresh =
326 (
327 (noUnitsX != 0 && m_xScrollLines == 0) ||
328 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
329
330 (noUnitsY != 0 && m_yScrollLines == 0) ||
331 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
332 (xPos != m_xScrollPosition) ||
333 (yPos != m_yScrollPosition)
334 );
335
336 m_xScrollPixelsPerLine = pixelsPerUnitX;
337 m_yScrollPixelsPerLine = pixelsPerUnitY;
338 m_xScrollPosition = xPos;
339 m_yScrollPosition = yPos;
340 m_xScrollLines = noUnitsX;
341 m_yScrollLines = noUnitsY;
342
343 #ifdef __WXMOTIF__
344 // Sorry, some Motif-specific code to implement a backing pixmap
345 // for the wxRETAINED style. Implementing a backing store can't
346 // be entirely generic because it relies on the wxWindowDC implementation
347 // to duplicate X drawing calls for the backing pixmap.
348
349 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
350 {
351 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
352
353 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
354 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
355 if (m_targetWindow->GetBackingPixmap() &&
356 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
357 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
358 {
359 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
360 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
361 }
362
363 if (!m_targetWindow->GetBackingPixmap() &&
364 (noUnitsX != 0) && (noUnitsY != 0))
365 {
366 int depth = wxDisplayDepth();
367 m_targetWindow->SetPixmapWidth(totalPixelWidth);
368 m_targetWindow->SetPixmapHeight(totalPixelHeight);
369 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
370 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
371 }
372
373 }
374 #endif // Motif
375
376 AdjustScrollbars();
377
378 if (do_refresh && !noRefresh)
379 m_targetWindow->Refresh(TRUE, GetRect());
380
381 #ifdef __WXMAC__
382 m_targetWindow->MacUpdateImmediately() ;
383 #endif
384 }
385
386 // ----------------------------------------------------------------------------
387 // [target] window handling
388 // ----------------------------------------------------------------------------
389
390 void wxScrollHelper::DeleteEvtHandler()
391 {
392 // search for m_handler in the handler list
393 if ( m_win && m_handler )
394 {
395 if ( m_win->RemoveEventHandler(m_handler) )
396 {
397 delete m_handler;
398 }
399 //else: something is very wrong, so better [maybe] leak memory than
400 // risk a crash because of double deletion
401
402 m_handler = NULL;
403 }
404 }
405
406 void wxScrollHelper::SetWindow(wxWindow *win)
407 {
408 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
409
410 m_win = win;
411
412 // by default, the associated window is also the target window
413 DoSetTargetWindow(win);
414 }
415
416 void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
417 {
418 m_targetWindow = target;
419
420 // install the event handler which will intercept the events we're
421 // interested in (but only do it for our real window, not the target window
422 // which we scroll - we don't need to hijack its events)
423 if ( m_targetWindow == m_win )
424 {
425 // if we already have a handler, delete it first
426 DeleteEvtHandler();
427
428 m_handler = new wxScrollHelperEvtHandler(this);
429 m_targetWindow->PushEventHandler(m_handler);
430 }
431 }
432
433 void wxScrollHelper::SetTargetWindow(wxWindow *target)
434 {
435 wxCHECK_RET( target, wxT("target window must not be NULL") );
436
437 if ( target == m_targetWindow )
438 return;
439
440 DoSetTargetWindow(target);
441 }
442
443 wxWindow *wxScrollHelper::GetTargetWindow() const
444 {
445 return m_targetWindow;
446 }
447
448 // ----------------------------------------------------------------------------
449 // scrolling implementation itself
450 // ----------------------------------------------------------------------------
451
452 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
453 {
454 int nScrollInc = CalcScrollInc(event);
455 if ( nScrollInc == 0 )
456 {
457 // can't scroll further
458 event.Skip();
459
460 return;
461 }
462
463 int orient = event.GetOrientation();
464 if (orient == wxHORIZONTAL)
465 {
466 m_xScrollPosition += nScrollInc;
467 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
468 }
469 else
470 {
471 m_yScrollPosition += nScrollInc;
472 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
473 }
474
475 bool needsRefresh = FALSE;
476 int dx = 0,
477 dy = 0;
478 if (orient == wxHORIZONTAL)
479 {
480 if ( m_xScrollingEnabled )
481 {
482 dx = -m_xScrollPixelsPerLine * nScrollInc;
483 }
484 else
485 {
486 needsRefresh = TRUE;
487 }
488 }
489 else
490 {
491 if ( m_yScrollingEnabled )
492 {
493 dy = -m_yScrollPixelsPerLine * nScrollInc;
494 }
495 else
496 {
497 needsRefresh = TRUE;
498 }
499 }
500
501 if ( needsRefresh )
502 {
503 m_targetWindow->Refresh(TRUE, GetRect());
504 }
505 else
506 {
507 m_targetWindow->ScrollWindow(dx, dy, GetRect());
508 }
509
510 #ifdef __WXMAC__
511 m_targetWindow->MacUpdateImmediately() ;
512 #endif
513 }
514
515 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
516 {
517 int pos = event.GetPosition();
518 int orient = event.GetOrientation();
519
520 int nScrollInc = 0;
521 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
522 {
523 if (orient == wxHORIZONTAL)
524 nScrollInc = - m_xScrollPosition;
525 else
526 nScrollInc = - m_yScrollPosition;
527 } else
528 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
529 {
530 if (orient == wxHORIZONTAL)
531 nScrollInc = m_xScrollLines - m_xScrollPosition;
532 else
533 nScrollInc = m_yScrollLines - m_yScrollPosition;
534 } else
535 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
536 {
537 nScrollInc = -1;
538 } else
539 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
540 {
541 nScrollInc = 1;
542 } else
543 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
544 {
545 if (orient == wxHORIZONTAL)
546 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
547 else
548 nScrollInc = -GetScrollPageSize(wxVERTICAL);
549 } else
550 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
551 {
552 if (orient == wxHORIZONTAL)
553 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
554 else
555 nScrollInc = GetScrollPageSize(wxVERTICAL);
556 } else
557 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
558 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
559 {
560 if (orient == wxHORIZONTAL)
561 nScrollInc = pos - m_xScrollPosition;
562 else
563 nScrollInc = pos - m_yScrollPosition;
564 }
565
566 if (orient == wxHORIZONTAL)
567 {
568 if (m_xScrollPixelsPerLine > 0)
569 {
570 int w, h;
571 GetTargetSize(&w, &h);
572
573 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
574 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
575 if (noPositions < 0)
576 noPositions = 0;
577
578 if ( (m_xScrollPosition + nScrollInc) < 0 )
579 nScrollInc = -m_xScrollPosition; // As -ve as we can go
580 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
581 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
582 }
583 else
584 m_targetWindow->Refresh(TRUE, GetRect());
585 }
586 else
587 {
588 if (m_yScrollPixelsPerLine > 0)
589 {
590 int w, h;
591 GetTargetSize(&w, &h);
592
593 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
594 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
595 if (noPositions < 0)
596 noPositions = 0;
597
598 if ( (m_yScrollPosition + nScrollInc) < 0 )
599 nScrollInc = -m_yScrollPosition; // As -ve as we can go
600 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
601 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
602 }
603 else
604 m_targetWindow->Refresh(TRUE, GetRect());
605 }
606
607 return nScrollInc;
608 }
609
610 // Adjust the scrollbars - new version.
611 void wxScrollHelper::AdjustScrollbars()
612 {
613 #ifdef __WXMAC__
614 m_targetWindow->MacUpdateImmediately();
615 #endif
616
617 int w, h;
618 GetTargetSize(&w, &h);
619
620 int oldXScroll = m_xScrollPosition;
621 int oldYScroll = m_yScrollPosition;
622
623 if (m_xScrollLines > 0)
624 {
625 // Calculate page size i.e. number of scroll units you get on the
626 // current client window
627 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
628 if (noPagePositions < 1) noPagePositions = 1;
629 if ( noPagePositions > m_xScrollLines )
630 noPagePositions = m_xScrollLines;
631
632 // Correct position if greater than extent of canvas minus
633 // the visible portion of it or if below zero
634 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
635 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
636
637 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
638 // The amount by which we scroll when paging
639 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
640 }
641 else
642 {
643 m_xScrollPosition = 0;
644 m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
645 }
646
647 if (m_yScrollLines > 0)
648 {
649 // Calculate page size i.e. number of scroll units you get on the
650 // current client window
651 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
652 if (noPagePositions < 1) noPagePositions = 1;
653 if ( noPagePositions > m_yScrollLines )
654 noPagePositions = m_yScrollLines;
655
656 // Correct position if greater than extent of canvas minus
657 // the visible portion of it or if below zero
658 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
659 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
660
661 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
662 // The amount by which we scroll when paging
663 SetScrollPageSize(wxVERTICAL, noPagePositions);
664 }
665 else
666 {
667 m_yScrollPosition = 0;
668 m_win->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
669 }
670
671 if (oldXScroll != m_xScrollPosition)
672 {
673 if (m_xScrollingEnabled)
674 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
675 GetRect() );
676 else
677 m_targetWindow->Refresh(TRUE, GetRect());
678 }
679
680 if (oldYScroll != m_yScrollPosition)
681 {
682 if (m_yScrollingEnabled)
683 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
684 GetRect() );
685 else
686 m_targetWindow->Refresh(TRUE, GetRect());
687 }
688
689 #ifdef __WXMAC__
690 m_targetWindow->MacUpdateImmediately();
691 #endif
692 }
693
694 void wxScrollHelper::DoPrepareDC(wxDC& dc)
695 {
696 wxPoint pt = dc.GetDeviceOrigin();
697 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
698 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
699 dc.SetUserScale( m_scaleX, m_scaleY );
700 }
701
702 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
703 {
704 if ( x_unit )
705 *x_unit = m_xScrollPixelsPerLine;
706 if ( y_unit )
707 *y_unit = m_yScrollPixelsPerLine;
708 }
709
710 int wxScrollHelper::GetScrollPageSize(int orient) const
711 {
712 if ( orient == wxHORIZONTAL )
713 return m_xScrollLinesPerPage;
714 else
715 return m_yScrollLinesPerPage;
716 }
717
718 void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
719 {
720 if ( orient == wxHORIZONTAL )
721 m_xScrollLinesPerPage = pageSize;
722 else
723 m_yScrollLinesPerPage = pageSize;
724 }
725
726 /*
727 * Scroll to given position (scroll position, not pixel position)
728 */
729 void wxScrollHelper::Scroll( int x_pos, int y_pos )
730 {
731 if (!m_targetWindow)
732 return;
733
734 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
735 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
736
737 #ifdef __WXMAC__
738 m_targetWindow->MacUpdateImmediately();
739 #endif
740
741 int w, h;
742 GetTargetSize(&w, &h);
743
744 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
745 {
746 int old_x = m_xScrollPosition;
747 m_xScrollPosition = x_pos;
748
749 // Calculate page size i.e. number of scroll units you get on the
750 // current client window
751 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
752 if (noPagePositions < 1) noPagePositions = 1;
753
754 // Correct position if greater than extent of canvas minus
755 // the visible portion of it or if below zero
756 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
757 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
758
759 if (old_x != m_xScrollPosition) {
760 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
761 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
762 GetRect() );
763 }
764 }
765 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
766 {
767 int old_y = m_yScrollPosition;
768 m_yScrollPosition = y_pos;
769
770 // Calculate page size i.e. number of scroll units you get on the
771 // current client window
772 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
773 if (noPagePositions < 1) noPagePositions = 1;
774
775 // Correct position if greater than extent of canvas minus
776 // the visible portion of it or if below zero
777 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
778 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
779
780 if (old_y != m_yScrollPosition) {
781 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
782 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
783 GetRect() );
784 }
785 }
786
787 #ifdef __WXMAC__
788 m_targetWindow->MacUpdateImmediately();
789 #endif
790
791 }
792
793 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
794 {
795 m_xScrollingEnabled = x_scroll;
796 m_yScrollingEnabled = y_scroll;
797 }
798
799 void wxScrollHelper::GetVirtualSize (int *x, int *y) const
800 {
801 if ( x )
802 *x = m_xScrollPixelsPerLine * m_xScrollLines;
803 if ( y )
804 *y = m_yScrollPixelsPerLine * m_yScrollLines;
805 }
806
807 // Where the current view starts from
808 void wxScrollHelper::GetViewStart (int *x, int *y) const
809 {
810 if ( x )
811 *x = m_xScrollPosition;
812 if ( y )
813 *y = m_yScrollPosition;
814 }
815
816 void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
817 {
818 if ( xx )
819 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
820 if ( yy )
821 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
822 }
823
824 void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
825 {
826 if ( xx )
827 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
828 if ( yy )
829 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
830 }
831
832 // ----------------------------------------------------------------------------
833 // event handlers
834 // ----------------------------------------------------------------------------
835
836 // Default OnSize resets scrollbars, if any
837 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
838 {
839 AdjustScrollbars();
840 }
841
842 // This calls OnDraw, having adjusted the origin according to the current
843 // scroll position
844 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
845 {
846 // don't use m_targetWindow here, this is always called for ourselves
847 wxPaintDC dc(m_win);
848 DoPrepareDC(dc);
849
850 OnDraw(dc);
851 }
852
853 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
854 // compatibility here - if we used OnKeyDown(), the programs which process
855 // arrows themselves in their OnChar() would never get the message and like
856 // this they always have the priority
857 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
858 {
859 int stx, sty, // view origin
860 szx, szy, // view size (total)
861 clix, cliy; // view size (on screen)
862
863 GetViewStart(&stx, &sty);
864 GetTargetSize(&clix, &cliy);
865 GetVirtualSize(&szx, &szy);
866
867 if( m_xScrollPixelsPerLine )
868 {
869 clix /= m_xScrollPixelsPerLine;
870 szx /= m_xScrollPixelsPerLine;
871 }
872 else
873 {
874 clix = 0;
875 szx = -1;
876 }
877 if( m_yScrollPixelsPerLine )
878 {
879 cliy /= m_yScrollPixelsPerLine;
880 szy /= m_yScrollPixelsPerLine;
881 }
882 else
883 {
884 cliy = 0;
885 szy = -1;
886 }
887
888 int xScrollOld = m_xScrollPosition,
889 yScrollOld = m_yScrollPosition;
890
891 int dsty;
892 switch ( event.KeyCode() )
893 {
894 case WXK_PAGEUP:
895 case WXK_PRIOR:
896 dsty = sty - (5 * cliy / 6);
897 Scroll(-1, (dsty == -1) ? 0 : dsty);
898 break;
899
900 case WXK_PAGEDOWN:
901 case WXK_NEXT:
902 Scroll(-1, sty + (5 * cliy / 6));
903 break;
904
905 case WXK_HOME:
906 Scroll(0, event.ControlDown() ? 0 : -1);
907 break;
908
909 case WXK_END:
910 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
911 break;
912
913 case WXK_UP:
914 Scroll(-1, sty - 1);
915 break;
916
917 case WXK_DOWN:
918 Scroll(-1, sty + 1);
919 break;
920
921 case WXK_LEFT:
922 Scroll(stx - 1, -1);
923 break;
924
925 case WXK_RIGHT:
926 Scroll(stx + 1, -1);
927 break;
928
929 default:
930 // not for us
931 event.Skip();
932 }
933
934 if ( m_xScrollPosition != xScrollOld )
935 {
936 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
937 wxHORIZONTAL);
938 event.SetEventObject(m_win);
939 m_win->GetEventHandler()->ProcessEvent(event);
940 }
941
942 if ( m_yScrollPosition != yScrollOld )
943 {
944 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
945 wxVERTICAL);
946 event.SetEventObject(m_win);
947 m_win->GetEventHandler()->ProcessEvent(event);
948 }
949 }
950
951 // ----------------------------------------------------------------------------
952 // autoscroll stuff: these functions deal with sending fake scroll events when
953 // a captured mouse is being held outside the window
954 // ----------------------------------------------------------------------------
955
956 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
957 {
958 // only send the event if the window is scrollable in this direction
959 wxWindow *win = (wxWindow *)event.GetEventObject();
960 return win->HasScrollbar(event.GetOrientation());
961 }
962
963 void wxScrollHelper::StopAutoScrolling()
964 {
965 if ( m_timerAutoScroll )
966 {
967 delete m_timerAutoScroll;
968 m_timerAutoScroll = (wxTimer *)NULL;
969 }
970 }
971
972 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
973 {
974 StopAutoScrolling();
975
976 event.Skip();
977 }
978
979 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
980 {
981 // don't prevent the usual processing of the event from taking place
982 event.Skip();
983
984 // when a captured mouse leave a scrolled window we start generate
985 // scrolling events to allow, for example, extending selection beyond the
986 // visible area in some controls
987 if ( wxWindow::GetCapture() == m_targetWindow )
988 {
989 // where is the mouse leaving?
990 int pos, orient;
991 wxPoint pt = event.GetPosition();
992 if ( pt.x < 0 )
993 {
994 orient = wxHORIZONTAL;
995 pos = 0;
996 }
997 else if ( pt.y < 0 )
998 {
999 orient = wxVERTICAL;
1000 pos = 0;
1001 }
1002 else // we're lower or to the right of the window
1003 {
1004 wxSize size = m_targetWindow->GetClientSize();
1005 if ( pt.x > size.x )
1006 {
1007 orient = wxHORIZONTAL;
1008 pos = m_xScrollLines;
1009 }
1010 else if ( pt.y > size.y )
1011 {
1012 orient = wxVERTICAL;
1013 pos = m_yScrollLines;
1014 }
1015 else // this should be impossible
1016 {
1017 // but seems to happen sometimes under wxMSW - maybe it's a bug
1018 // there but for now just ignore it
1019
1020 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1021
1022 return;
1023 }
1024 }
1025
1026 // only start the auto scroll timer if the window can be scrolled in
1027 // this direction
1028 if ( !m_targetWindow->HasScrollbar(orient) )
1029 return;
1030
1031 delete m_timerAutoScroll;
1032 m_timerAutoScroll = new wxAutoScrollTimer
1033 (
1034 m_targetWindow, this,
1035 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1036 : wxEVT_SCROLLWIN_LINEDOWN,
1037 pos,
1038 orient
1039 );
1040 m_timerAutoScroll->Start(50); // FIXME: make configurable
1041 }
1042 }
1043
1044 #if wxUSE_MOUSEWHEEL
1045
1046 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1047 {
1048 m_wheelRotation += event.GetWheelRotation();
1049 int lines = m_wheelRotation / event.GetWheelDelta();
1050 m_wheelRotation -= lines * event.GetWheelDelta();
1051
1052 if (lines != 0)
1053 {
1054 lines *= event.GetLinesPerAction();
1055
1056 wxScrollWinEvent newEvent;
1057
1058 newEvent.SetPosition(0);
1059 newEvent.SetOrientation(wxVERTICAL);
1060 newEvent.m_eventObject = m_win;
1061 if (lines > 0)
1062 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1063 else
1064 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1065
1066 int times = abs(lines);
1067 for (; times > 0; times --)
1068 m_win->GetEventHandler()->ProcessEvent(newEvent);
1069
1070 /* Old Way */
1071 // int vsx, vsy;
1072 // GetViewStart(&vsx, &vsy);
1073 // Scroll(-1, vsy - lines);
1074 }
1075 }
1076
1077 #endif // wxUSE_MOUSEWHEEL
1078
1079 // ----------------------------------------------------------------------------
1080 // wxGenericScrolledWindow implementation
1081 // ----------------------------------------------------------------------------
1082
1083 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1084
1085 BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1086 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1087 END_EVENT_TABLE()
1088
1089 bool wxGenericScrolledWindow::Create(wxWindow *parent,
1090 wxWindowID id,
1091 const wxPoint& pos,
1092 const wxSize& size,
1093 long style,
1094 const wxString& name)
1095 {
1096 m_targetWindow = this;
1097
1098 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1099
1100 return ok;
1101 }
1102
1103 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1104 {
1105 }
1106
1107 bool wxGenericScrolledWindow::Layout()
1108 {
1109 if (GetSizer())
1110 {
1111 // Take into account the virtual size and scrolled position of the window
1112 int x, y, w, h;
1113 CalcScrolledPosition(0,0, &x,&y);
1114 GetVirtualSize(&w, &h);
1115 GetSizer()->SetDimension(x, y, w, h);
1116 return TRUE;
1117 }
1118 else
1119 return wxPanel::Layout(); // fall back to default for LayoutConstraints
1120 }
1121
1122 void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1123 {
1124 // the user code didn't really draw the window if we got here, so set this
1125 // flag to try to call OnDraw() later
1126 m_handler->ResetDrawnFlag();
1127
1128 event.Skip();
1129 }
1130
1131 #ifdef __WXMSW__
1132 long
1133 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1134 WXWPARAM wParam,
1135 WXLPARAM lParam)
1136 {
1137 long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1138
1139 // we need to process arrows ourselves for scrolling
1140 if ( nMsg == WM_GETDLGCODE )
1141 {
1142 rc |= DLGC_WANTARROWS;
1143 }
1144
1145 return rc;
1146 }
1147
1148 #endif // __WXMSW__
1149
1150 #if WXWIN_COMPATIBILITY
1151
1152 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1153 {
1154 *x_page = GetScrollPageSize(wxHORIZONTAL);
1155 *y_page = GetScrollPageSize(wxVERTICAL);
1156 }
1157
1158 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1159 {
1160 if ( xx )
1161 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1162 if ( yy )
1163 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1164 }
1165
1166 #endif // WXWIN_COMPATIBILITY
1167
1168 #endif // !wxGTK
1169