]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrlwing.cpp
fixed compilation warnings with GTK+ 2.0
[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 #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
61 IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
62
63 // ----------------------------------------------------------------------------
64 // wxScrollHelperEvtHandler: intercept the events from the window and forward
65 // them to wxScrollHelper
66 // ----------------------------------------------------------------------------
67
68 class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
69 {
70 public:
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
80 private:
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
91 class wxAutoScrollTimer : public wxTimer
92 {
93 public:
94 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
95 wxEventType eventTypeToSend,
96 int pos, int orient);
97
98 virtual void Notify();
99
100 private:
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
116 wxAutoScrollTimer::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
128 void 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
173 bool 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
273 wxScrollHelper::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
304 wxScrollHelper::~wxScrollHelper()
305 {
306 StopAutoScrolling();
307
308 DeleteEvtHandler();
309 }
310
311 // ----------------------------------------------------------------------------
312 // setting scrolling parameters
313 // ----------------------------------------------------------------------------
314
315 void 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
391 void 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
407 void 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
417 void 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
434 void 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
444 wxWindow *wxScrollHelper::GetTargetWindow() const
445 {
446 return m_targetWindow;
447 }
448
449 // ----------------------------------------------------------------------------
450 // scrolling implementation itself
451 // ----------------------------------------------------------------------------
452
453 void 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
516 int 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.
612 void 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
695 void 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
703 void 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
711 int wxScrollHelper::GetScrollPageSize(int orient) const
712 {
713 if ( orient == wxHORIZONTAL )
714 return m_xScrollLinesPerPage;
715 else
716 return m_yScrollLinesPerPage;
717 }
718
719 void 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 */
730 void 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
794 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
795 {
796 m_xScrollingEnabled = x_scroll;
797 m_yScrollingEnabled = y_scroll;
798 }
799
800 void wxScrollHelper::GetVirtualSize (int *x, int *y) const
801 {
802 if ( x )
803 *x = m_xScrollPixelsPerLine * m_xScrollLines;
804 if ( y )
805 *y = m_yScrollPixelsPerLine * m_yScrollLines;
806 }
807
808 // Where the current view starts from
809 void wxScrollHelper::GetViewStart (int *x, int *y) const
810 {
811 if ( x )
812 *x = m_xScrollPosition;
813 if ( y )
814 *y = m_yScrollPosition;
815 }
816
817 void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
818 {
819 if ( xx )
820 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
821 if ( yy )
822 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
823 }
824
825 void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
826 {
827 if ( xx )
828 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
829 if ( yy )
830 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
831 }
832
833 // ----------------------------------------------------------------------------
834 // event handlers
835 // ----------------------------------------------------------------------------
836
837 // Default OnSize resets scrollbars, if any
838 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
839 {
840 AdjustScrollbars();
841 }
842
843 // This calls OnDraw, having adjusted the origin according to the current
844 // scroll position
845 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
846 {
847 // don't use m_targetWindow here, this is always called for ourselves
848 wxPaintDC dc(m_win);
849 DoPrepareDC(dc);
850
851 OnDraw(dc);
852 }
853
854 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
855 // compatibility here - if we used OnKeyDown(), the programs which process
856 // arrows themselves in their OnChar() would never get the message and like
857 // this they always have the priority
858 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
859 {
860 int stx, sty, // view origin
861 szx, szy, // view size (total)
862 clix, cliy; // view size (on screen)
863
864 GetViewStart(&stx, &sty);
865 GetTargetSize(&clix, &cliy);
866 GetVirtualSize(&szx, &szy);
867
868 if( m_xScrollPixelsPerLine )
869 {
870 clix /= m_xScrollPixelsPerLine;
871 szx /= m_xScrollPixelsPerLine;
872 }
873 else
874 {
875 clix = 0;
876 szx = -1;
877 }
878 if( m_yScrollPixelsPerLine )
879 {
880 cliy /= m_yScrollPixelsPerLine;
881 szy /= m_yScrollPixelsPerLine;
882 }
883 else
884 {
885 cliy = 0;
886 szy = -1;
887 }
888
889 int xScrollOld = m_xScrollPosition,
890 yScrollOld = m_yScrollPosition;
891
892 int dsty;
893 switch ( event.KeyCode() )
894 {
895 case WXK_PAGEUP:
896 case WXK_PRIOR:
897 dsty = sty - (5 * cliy / 6);
898 Scroll(-1, (dsty == -1) ? 0 : dsty);
899 break;
900
901 case WXK_PAGEDOWN:
902 case WXK_NEXT:
903 Scroll(-1, sty + (5 * cliy / 6));
904 break;
905
906 case WXK_HOME:
907 Scroll(0, event.ControlDown() ? 0 : -1);
908 break;
909
910 case WXK_END:
911 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
912 break;
913
914 case WXK_UP:
915 Scroll(-1, sty - 1);
916 break;
917
918 case WXK_DOWN:
919 Scroll(-1, sty + 1);
920 break;
921
922 case WXK_LEFT:
923 Scroll(stx - 1, -1);
924 break;
925
926 case WXK_RIGHT:
927 Scroll(stx + 1, -1);
928 break;
929
930 default:
931 // not for us
932 event.Skip();
933 }
934
935 if ( m_xScrollPosition != xScrollOld )
936 {
937 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
938 wxHORIZONTAL);
939 event.SetEventObject(m_win);
940 m_win->GetEventHandler()->ProcessEvent(event);
941 }
942
943 if ( m_yScrollPosition != yScrollOld )
944 {
945 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
946 wxVERTICAL);
947 event.SetEventObject(m_win);
948 m_win->GetEventHandler()->ProcessEvent(event);
949 }
950 }
951
952 // ----------------------------------------------------------------------------
953 // autoscroll stuff: these functions deal with sending fake scroll events when
954 // a captured mouse is being held outside the window
955 // ----------------------------------------------------------------------------
956
957 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
958 {
959 // only send the event if the window is scrollable in this direction
960 wxWindow *win = (wxWindow *)event.GetEventObject();
961 return win->HasScrollbar(event.GetOrientation());
962 }
963
964 void wxScrollHelper::StopAutoScrolling()
965 {
966 if ( m_timerAutoScroll )
967 {
968 delete m_timerAutoScroll;
969 m_timerAutoScroll = (wxTimer *)NULL;
970 }
971 }
972
973 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
974 {
975 StopAutoScrolling();
976
977 event.Skip();
978 }
979
980 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
981 {
982 // don't prevent the usual processing of the event from taking place
983 event.Skip();
984
985 // when a captured mouse leave a scrolled window we start generate
986 // scrolling events to allow, for example, extending selection beyond the
987 // visible area in some controls
988 if ( wxWindow::GetCapture() == m_targetWindow )
989 {
990 // where is the mouse leaving?
991 int pos, orient;
992 wxPoint pt = event.GetPosition();
993 if ( pt.x < 0 )
994 {
995 orient = wxHORIZONTAL;
996 pos = 0;
997 }
998 else if ( pt.y < 0 )
999 {
1000 orient = wxVERTICAL;
1001 pos = 0;
1002 }
1003 else // we're lower or to the right of the window
1004 {
1005 wxSize size = m_targetWindow->GetClientSize();
1006 if ( pt.x > size.x )
1007 {
1008 orient = wxHORIZONTAL;
1009 pos = m_xScrollLines;
1010 }
1011 else if ( pt.y > size.y )
1012 {
1013 orient = wxVERTICAL;
1014 pos = m_yScrollLines;
1015 }
1016 else // this should be impossible
1017 {
1018 // but seems to happen sometimes under wxMSW - maybe it's a bug
1019 // there but for now just ignore it
1020
1021 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1022
1023 return;
1024 }
1025 }
1026
1027 // only start the auto scroll timer if the window can be scrolled in
1028 // this direction
1029 if ( !m_targetWindow->HasScrollbar(orient) )
1030 return;
1031
1032 delete m_timerAutoScroll;
1033 m_timerAutoScroll = new wxAutoScrollTimer
1034 (
1035 m_targetWindow, this,
1036 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1037 : wxEVT_SCROLLWIN_LINEDOWN,
1038 pos,
1039 orient
1040 );
1041 m_timerAutoScroll->Start(50); // FIXME: make configurable
1042 }
1043 }
1044
1045 #if wxUSE_MOUSEWHEEL
1046
1047 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1048 {
1049 m_wheelRotation += event.GetWheelRotation();
1050 int lines = m_wheelRotation / event.GetWheelDelta();
1051 m_wheelRotation -= lines * event.GetWheelDelta();
1052
1053 if (lines != 0)
1054 {
1055 lines *= event.GetLinesPerAction();
1056
1057 wxScrollWinEvent newEvent;
1058
1059 newEvent.SetPosition(0);
1060 newEvent.SetOrientation(wxVERTICAL);
1061 newEvent.m_eventObject = m_win;
1062 if (lines > 0)
1063 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1064 else
1065 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1066
1067 int times = abs(lines);
1068 for (; times > 0; times --)
1069 m_win->GetEventHandler()->ProcessEvent(newEvent);
1070
1071 /* Old Way */
1072 // int vsx, vsy;
1073 // GetViewStart(&vsx, &vsy);
1074 // Scroll(-1, vsy - lines);
1075 }
1076 }
1077
1078 #endif // wxUSE_MOUSEWHEEL
1079
1080 // ----------------------------------------------------------------------------
1081 // wxGenericScrolledWindow implementation
1082 // ----------------------------------------------------------------------------
1083
1084 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1085
1086 BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1087 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1088 END_EVENT_TABLE()
1089
1090 bool wxGenericScrolledWindow::Create(wxWindow *parent,
1091 wxWindowID id,
1092 const wxPoint& pos,
1093 const wxSize& size,
1094 long style,
1095 const wxString& name)
1096 {
1097 m_targetWindow = this;
1098
1099 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1100
1101 return ok;
1102 }
1103
1104 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1105 {
1106 }
1107
1108 bool wxGenericScrolledWindow::Layout()
1109 {
1110 if (GetSizer())
1111 {
1112 // Take into account the virtual size and scrolled position of the window
1113 int x, y, w, h;
1114 CalcScrolledPosition(0,0, &x,&y);
1115 GetVirtualSize(&w, &h);
1116 GetSizer()->SetDimension(x, y, w, h);
1117 return TRUE;
1118 }
1119
1120 // fall back to default for LayoutConstraints
1121 return wxPanel::Layout();
1122 }
1123
1124 void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1125 {
1126 // the user code didn't really draw the window if we got here, so set this
1127 // flag to try to call OnDraw() later
1128 m_handler->ResetDrawnFlag();
1129
1130 event.Skip();
1131 }
1132
1133 #ifdef __WXMSW__
1134 long
1135 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1136 WXWPARAM wParam,
1137 WXLPARAM lParam)
1138 {
1139 long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1140
1141 // we need to process arrows ourselves for scrolling
1142 if ( nMsg == WM_GETDLGCODE )
1143 {
1144 rc |= DLGC_WANTARROWS;
1145 }
1146
1147 return rc;
1148 }
1149
1150 #endif // __WXMSW__
1151
1152 #if WXWIN_COMPATIBILITY
1153
1154 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1155 {
1156 *x_page = GetScrollPageSize(wxHORIZONTAL);
1157 *y_page = GetScrollPageSize(wxVERTICAL);
1158 }
1159
1160 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1161 {
1162 if ( xx )
1163 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1164 if ( yy )
1165 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1166 }
1167
1168 #endif // WXWIN_COMPATIBILITY
1169
1170 #endif // !wxGTK
1171