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