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