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