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