more efficient version of the wxUniv scrolling fix: don't call Update() in cases...
[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 bool needsRefresh = false;
466 int dx = 0,
467 dy = 0;
468 int orient = event.GetOrientation();
469 if (orient == wxHORIZONTAL)
470 {
471 if ( m_xScrollingEnabled )
472 {
473 dx = -m_xScrollPixelsPerLine * nScrollInc;
474 }
475 else
476 {
477 needsRefresh = true;
478 }
479 }
480 else
481 {
482 if ( m_yScrollingEnabled )
483 {
484 dy = -m_yScrollPixelsPerLine * nScrollInc;
485 }
486 else
487 {
488 needsRefresh = true;
489 }
490 }
491
492 if ( !needsRefresh )
493 {
494 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
495 // otherwise invalidated area could be updated incorrectly later when
496 // ScrollWindow() makes sure they're repainted before scrolling them
497 m_targetWindow->Update();
498 }
499
500 if (orient == wxHORIZONTAL)
501 {
502 m_xScrollPosition += nScrollInc;
503 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
504 }
505 else
506 {
507 m_yScrollPosition += nScrollInc;
508 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
509 }
510
511 if ( needsRefresh )
512 {
513 m_targetWindow->Refresh(true, GetScrollRect());
514 }
515 else
516 {
517 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
518 }
519 }
520
521 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
522 {
523 int pos = event.GetPosition();
524 int orient = event.GetOrientation();
525
526 int nScrollInc = 0;
527 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
528 {
529 if (orient == wxHORIZONTAL)
530 nScrollInc = - m_xScrollPosition;
531 else
532 nScrollInc = - m_yScrollPosition;
533 } else
534 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
535 {
536 if (orient == wxHORIZONTAL)
537 nScrollInc = m_xScrollLines - m_xScrollPosition;
538 else
539 nScrollInc = m_yScrollLines - m_yScrollPosition;
540 } else
541 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
542 {
543 nScrollInc = -1;
544 } else
545 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
546 {
547 nScrollInc = 1;
548 } else
549 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
550 {
551 if (orient == wxHORIZONTAL)
552 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
553 else
554 nScrollInc = -GetScrollPageSize(wxVERTICAL);
555 } else
556 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
557 {
558 if (orient == wxHORIZONTAL)
559 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
560 else
561 nScrollInc = GetScrollPageSize(wxVERTICAL);
562 } else
563 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
564 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
565 {
566 if (orient == wxHORIZONTAL)
567 nScrollInc = pos - m_xScrollPosition;
568 else
569 nScrollInc = pos - m_yScrollPosition;
570 }
571
572 if (orient == wxHORIZONTAL)
573 {
574 if (m_xScrollPixelsPerLine > 0)
575 {
576 if ( m_xScrollPosition + nScrollInc < 0 )
577 {
578 // As -ve as we can go
579 nScrollInc = -m_xScrollPosition;
580 }
581 else // check for the other bound
582 {
583 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
584 if ( m_xScrollPosition + nScrollInc > posMax )
585 {
586 // As +ve as we can go
587 nScrollInc = posMax - m_xScrollPosition;
588 }
589 }
590 }
591 else
592 m_targetWindow->Refresh(true, GetScrollRect());
593 }
594 else
595 {
596 if ( m_yScrollPixelsPerLine > 0 )
597 {
598 if ( m_yScrollPosition + nScrollInc < 0 )
599 {
600 // As -ve as we can go
601 nScrollInc = -m_yScrollPosition;
602 }
603 else // check for the other bound
604 {
605 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
606 if ( m_yScrollPosition + nScrollInc > posMax )
607 {
608 // As +ve as we can go
609 nScrollInc = posMax - m_yScrollPosition;
610 }
611 }
612 }
613 else
614 {
615 // VZ: why do we do this? (FIXME)
616 m_targetWindow->Refresh(true, GetScrollRect());
617 }
618 }
619
620 return nScrollInc;
621 }
622
623 // Adjust the scrollbars - new version.
624 void wxScrollHelper::AdjustScrollbars()
625 {
626 static wxRecursionGuardFlag s_flagReentrancy;
627 wxRecursionGuard guard(s_flagReentrancy);
628 if ( guard.IsInside() )
629 {
630 // don't reenter AdjustScrollbars() while another call to
631 // AdjustScrollbars() is in progress because this may lead to calling
632 // ScrollWindow() twice and this can really happen under MSW if
633 // SetScrollbar() call below adds or removes the scrollbar which
634 // changes the window size and hence results in another
635 // AdjustScrollbars() call
636 return;
637 }
638
639 int w = 0, h = 0;
640 int oldw, oldh;
641
642 int oldXScroll = m_xScrollPosition;
643 int oldYScroll = m_yScrollPosition;
644
645 // VZ: at least under Windows this loop is useless because when scrollbars
646 // [dis]appear we get a WM_SIZE resulting in another call to
647 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
648 // it here for now but it would be better to ensure that all ports
649 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
650 // necessary, and remove it later
651 // JACS: Stop potential infinite loop by limiting number of iterations
652 int iterationCount = 0;
653 const int iterationMax = 5;
654 do
655 {
656 iterationCount ++;
657
658 GetTargetSize(&w, 0);
659
660 // scroll lines per page: if 0, no scrolling is needed
661 int linesPerPage;
662
663 if ( m_xScrollPixelsPerLine == 0 )
664 {
665 // scrolling is disabled
666 m_xScrollLines = 0;
667 m_xScrollPosition = 0;
668 linesPerPage = 0;
669 }
670 else // might need scrolling
671 {
672 // Round up integer division to catch any "leftover" client space.
673 const int wVirt = m_targetWindow->GetVirtualSize().GetWidth();
674 m_xScrollLines = (wVirt + m_xScrollPixelsPerLine - 1) / m_xScrollPixelsPerLine;
675
676 // Calculate page size i.e. number of scroll units you get on the
677 // current client window.
678 linesPerPage = w / m_xScrollPixelsPerLine;
679
680 // Special case. When client and virtual size are very close but
681 // the client is big enough, kill scrollbar.
682 if ((linesPerPage < m_xScrollLines) && (w >= wVirt)) ++linesPerPage;
683
684 if (linesPerPage >= m_xScrollLines)
685 {
686 // we're big enough to not need scrolling
687 linesPerPage =
688 m_xScrollLines =
689 m_xScrollPosition = 0;
690 }
691 else // we do need a scrollbar
692 {
693 if ( linesPerPage < 1 )
694 linesPerPage = 1;
695
696 // Correct position if greater than extent of canvas minus
697 // the visible portion of it or if below zero
698 const int posMax = m_xScrollLines - linesPerPage;
699 if ( m_xScrollPosition > posMax )
700 m_xScrollPosition = posMax;
701 else if ( m_xScrollPosition < 0 )
702 m_xScrollPosition = 0;
703 }
704 }
705
706 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition,
707 linesPerPage, m_xScrollLines);
708
709 // The amount by which we scroll when paging
710 SetScrollPageSize(wxHORIZONTAL, linesPerPage);
711
712 GetTargetSize(0, &h);
713
714 if ( m_yScrollPixelsPerLine == 0 )
715 {
716 // scrolling is disabled
717 m_yScrollLines = 0;
718 m_yScrollPosition = 0;
719 linesPerPage = 0;
720 }
721 else // might need scrolling
722 {
723 // Round up integer division to catch any "leftover" client space.
724 const int hVirt = m_targetWindow->GetVirtualSize().GetHeight();
725 m_yScrollLines = ( hVirt + m_yScrollPixelsPerLine - 1 ) / m_yScrollPixelsPerLine;
726
727 // Calculate page size i.e. number of scroll units you get on the
728 // current client window.
729 linesPerPage = h / m_yScrollPixelsPerLine;
730
731 // Special case. When client and virtual size are very close but
732 // the client is big enough, kill scrollbar.
733 if ((linesPerPage < m_yScrollLines) && (h >= hVirt)) ++linesPerPage;
734
735 if (linesPerPage >= m_yScrollLines)
736 {
737 // we're big enough to not need scrolling
738 linesPerPage =
739 m_yScrollLines =
740 m_yScrollPosition = 0;
741 }
742 else // we do need a scrollbar
743 {
744 if ( linesPerPage < 1 )
745 linesPerPage = 1;
746
747 // Correct position if greater than extent of canvas minus
748 // the visible portion of it or if below zero
749 const int posMax = m_yScrollLines - linesPerPage;
750 if ( m_yScrollPosition > posMax )
751 m_yScrollPosition = posMax;
752 else if ( m_yScrollPosition < 0 )
753 m_yScrollPosition = 0;
754 }
755 }
756
757 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition,
758 linesPerPage, m_yScrollLines);
759
760 // The amount by which we scroll when paging
761 SetScrollPageSize(wxVERTICAL, linesPerPage);
762
763
764 // If a scrollbar (dis)appeared as a result of this, adjust them again.
765 oldw = w;
766 oldh = h;
767
768 GetTargetSize( &w, &h );
769 } while ( (w != oldw || h != oldh) && (iterationCount < iterationMax) );
770
771 #ifdef __WXMOTIF__
772 // Sorry, some Motif-specific code to implement a backing pixmap
773 // for the wxRETAINED style. Implementing a backing store can't
774 // be entirely generic because it relies on the wxWindowDC implementation
775 // to duplicate X drawing calls for the backing pixmap.
776
777 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
778 {
779 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
780
781 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
782 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
783 if (m_targetWindow->GetBackingPixmap() &&
784 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
785 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
786 {
787 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
788 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
789 }
790
791 if (!m_targetWindow->GetBackingPixmap() &&
792 (m_xScrollLines != 0) && (m_yScrollLines != 0))
793 {
794 int depth = wxDisplayDepth();
795 m_targetWindow->SetPixmapWidth(totalPixelWidth);
796 m_targetWindow->SetPixmapHeight(totalPixelHeight);
797 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
798 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
799 }
800
801 }
802 #endif // Motif
803
804 if (oldXScroll != m_xScrollPosition)
805 {
806 if (m_xScrollingEnabled)
807 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
808 GetScrollRect() );
809 else
810 m_targetWindow->Refresh(true, GetScrollRect());
811 }
812
813 if (oldYScroll != m_yScrollPosition)
814 {
815 if (m_yScrollingEnabled)
816 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
817 GetScrollRect() );
818 else
819 m_targetWindow->Refresh(true, GetScrollRect());
820 }
821 }
822
823 void wxScrollHelper::DoPrepareDC(wxDC& dc)
824 {
825 wxPoint pt = dc.GetDeviceOrigin();
826 #ifdef __WXGTK__
827 // It may actually be correct to always query
828 // the m_sign from the DC here, but I leve the
829 // #ifdef GTK for now.
830 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
831 dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
832 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
833 else
834 #endif
835 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
836 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
837 dc.SetUserScale( m_scaleX, m_scaleY );
838 }
839
840 void wxScrollHelper::SetScrollRate( int xstep, int ystep )
841 {
842 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
843 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
844
845 m_xScrollPixelsPerLine = xstep;
846 m_yScrollPixelsPerLine = ystep;
847
848 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
849 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
850
851 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
852 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
853 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
854
855 AdjustScrollbars();
856 }
857
858 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
859 {
860 if ( x_unit )
861 *x_unit = m_xScrollPixelsPerLine;
862 if ( y_unit )
863 *y_unit = m_yScrollPixelsPerLine;
864 }
865
866
867 int wxScrollHelper::GetScrollLines( int orient ) const
868 {
869 if ( orient == wxHORIZONTAL )
870 return m_xScrollLines;
871 else
872 return m_yScrollLines;
873 }
874
875 int wxScrollHelper::GetScrollPageSize(int orient) const
876 {
877 if ( orient == wxHORIZONTAL )
878 return m_xScrollLinesPerPage;
879 else
880 return m_yScrollLinesPerPage;
881 }
882
883 void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
884 {
885 if ( orient == wxHORIZONTAL )
886 m_xScrollLinesPerPage = pageSize;
887 else
888 m_yScrollLinesPerPage = pageSize;
889 }
890
891 /*
892 * Scroll to given position (scroll position, not pixel position)
893 */
894 void wxScrollHelper::Scroll( int x_pos, int y_pos )
895 {
896 if (!m_targetWindow)
897 return;
898
899 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
900 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
901
902 int w = 0, h = 0;
903 GetTargetSize(&w, &h);
904
905 // compute new position:
906 int new_x = m_xScrollPosition;
907 int new_y = m_yScrollPosition;
908
909 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
910 {
911 new_x = x_pos;
912
913 // Calculate page size i.e. number of scroll units you get on the
914 // current client window
915 int noPagePositions = w/m_xScrollPixelsPerLine;
916 if (noPagePositions < 1) noPagePositions = 1;
917
918 // Correct position if greater than extent of canvas minus
919 // the visible portion of it or if below zero
920 new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
921 new_x = wxMax( 0, new_x );
922 }
923 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
924 {
925 new_y = y_pos;
926
927 // Calculate page size i.e. number of scroll units you get on the
928 // current client window
929 int noPagePositions = h/m_yScrollPixelsPerLine;
930 if (noPagePositions < 1) noPagePositions = 1;
931
932 // Correct position if greater than extent of canvas minus
933 // the visible portion of it or if below zero
934 new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
935 new_y = wxMax( 0, new_y );
936 }
937
938 if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
939 return; // nothing to do, the position didn't change
940
941 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
942 // otherwise invalidated area could be updated incorrectly later when
943 // ScrollWindow() makes sure they're repainted before scrolling them
944 m_targetWindow->Update();
945
946 // update the position and scroll the window now:
947 if (m_xScrollPosition != new_x)
948 {
949 int old_x = m_xScrollPosition;
950 m_xScrollPosition = new_x;
951 m_win->SetScrollPos( wxHORIZONTAL, new_x );
952 m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
953 GetScrollRect() );
954 }
955
956 if (m_yScrollPosition != new_y)
957 {
958 int old_y = m_yScrollPosition;
959 m_yScrollPosition = new_y;
960 m_win->SetScrollPos( wxVERTICAL, new_y );
961 m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
962 GetScrollRect() );
963 }
964 }
965
966 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
967 {
968 m_xScrollingEnabled = x_scroll;
969 m_yScrollingEnabled = y_scroll;
970 }
971
972 // Where the current view starts from
973 void wxScrollHelper::GetViewStart (int *x, int *y) const
974 {
975 if ( x )
976 *x = m_xScrollPosition;
977 if ( y )
978 *y = m_yScrollPosition;
979 }
980
981 void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
982 {
983 if ( xx )
984 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
985 if ( yy )
986 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
987 }
988
989 void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
990 {
991 if ( xx )
992 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
993 if ( yy )
994 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
995 }
996
997 // ----------------------------------------------------------------------------
998 // geometry
999 // ----------------------------------------------------------------------------
1000
1001 bool wxScrollHelper::ScrollLayout()
1002 {
1003 if ( m_win->GetSizer() && m_targetWindow == m_win )
1004 {
1005 // If we're the scroll target, take into account the
1006 // virtual size and scrolled position of the window.
1007
1008 int x = 0, y = 0, w = 0, h = 0;
1009 CalcScrolledPosition(0,0, &x,&y);
1010 m_win->GetVirtualSize(&w, &h);
1011 m_win->GetSizer()->SetDimension(x, y, w, h);
1012 return true;
1013 }
1014
1015 // fall back to default for LayoutConstraints
1016 return m_win->wxWindow::Layout();
1017 }
1018
1019 void wxScrollHelper::ScrollDoSetVirtualSize(int x, int y)
1020 {
1021 m_win->wxWindow::DoSetVirtualSize( x, y );
1022 AdjustScrollbars();
1023
1024 if (m_win->GetAutoLayout())
1025 m_win->Layout();
1026 }
1027
1028 // wxWindow's GetBestVirtualSize returns the actual window size,
1029 // whereas we want to return the virtual size
1030 wxSize wxScrollHelper::ScrollGetBestVirtualSize() const
1031 {
1032 wxSize clientSize(m_win->GetClientSize());
1033 if ( m_win->GetSizer() )
1034 clientSize.IncTo(m_win->GetSizer()->CalcMin());
1035
1036 return clientSize;
1037 }
1038
1039 // return the window best size from the given best virtual size
1040 wxSize
1041 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize& size) const
1042 {
1043 // Only use the content to set the window size in the direction
1044 // where there's no scrolling; otherwise we're going to get a huge
1045 // window in the direction in which scrolling is enabled
1046 int ppuX, ppuY;
1047 GetScrollPixelsPerUnit(&ppuX, &ppuY);
1048
1049 wxSize minSize = m_win->GetMinSize();
1050 if ( !minSize.IsFullySpecified() )
1051 minSize = m_win->GetSize();
1052
1053 wxSize best(size);
1054 if (ppuX > 0)
1055 best.x = minSize.x;
1056 if (ppuY > 0)
1057 best.y = minSize.y;
1058
1059 return best;
1060 }
1061
1062 // ----------------------------------------------------------------------------
1063 // event handlers
1064 // ----------------------------------------------------------------------------
1065
1066 // Default OnSize resets scrollbars, if any
1067 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
1068 {
1069 if ( m_targetWindow->GetAutoLayout() )
1070 {
1071 wxSize size = m_targetWindow->GetBestVirtualSize();
1072
1073 // This will call ::Layout() and ::AdjustScrollbars()
1074 m_win->SetVirtualSize( size );
1075 }
1076 else
1077 {
1078 AdjustScrollbars();
1079 }
1080 }
1081
1082 // This calls OnDraw, having adjusted the origin according to the current
1083 // scroll position
1084 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
1085 {
1086 // don't use m_targetWindow here, this is always called for ourselves
1087 wxPaintDC dc(m_win);
1088 DoPrepareDC(dc);
1089
1090 OnDraw(dc);
1091 }
1092
1093 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1094 // compatibility here - if we used OnKeyDown(), the programs which process
1095 // arrows themselves in their OnChar() would never get the message and like
1096 // this they always have the priority
1097 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
1098 {
1099 int stx = 0, sty = 0, // view origin
1100 szx = 0, szy = 0, // view size (total)
1101 clix = 0, cliy = 0; // view size (on screen)
1102
1103 GetViewStart(&stx, &sty);
1104 GetTargetSize(&clix, &cliy);
1105 m_targetWindow->GetVirtualSize(&szx, &szy);
1106
1107 if( m_xScrollPixelsPerLine )
1108 {
1109 clix /= m_xScrollPixelsPerLine;
1110 szx /= m_xScrollPixelsPerLine;
1111 }
1112 else
1113 {
1114 clix = 0;
1115 szx = -1;
1116 }
1117 if( m_yScrollPixelsPerLine )
1118 {
1119 cliy /= m_yScrollPixelsPerLine;
1120 szy /= m_yScrollPixelsPerLine;
1121 }
1122 else
1123 {
1124 cliy = 0;
1125 szy = -1;
1126 }
1127
1128 int xScrollOld = m_xScrollPosition,
1129 yScrollOld = m_yScrollPosition;
1130
1131 int dsty;
1132 switch ( event.GetKeyCode() )
1133 {
1134 case WXK_PAGEUP:
1135 dsty = sty - (5 * cliy / 6);
1136 Scroll(-1, (dsty == -1) ? 0 : dsty);
1137 break;
1138
1139 case WXK_PAGEDOWN:
1140 Scroll(-1, sty + (5 * cliy / 6));
1141 break;
1142
1143 case WXK_HOME:
1144 Scroll(0, event.ControlDown() ? 0 : -1);
1145 break;
1146
1147 case WXK_END:
1148 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
1149 break;
1150
1151 case WXK_UP:
1152 Scroll(-1, sty - 1);
1153 break;
1154
1155 case WXK_DOWN:
1156 Scroll(-1, sty + 1);
1157 break;
1158
1159 case WXK_LEFT:
1160 Scroll(stx - 1, -1);
1161 break;
1162
1163 case WXK_RIGHT:
1164 Scroll(stx + 1, -1);
1165 break;
1166
1167 default:
1168 // not for us
1169 event.Skip();
1170 }
1171
1172 if ( m_xScrollPosition != xScrollOld )
1173 {
1174 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1175 wxHORIZONTAL);
1176 event.SetEventObject(m_win);
1177 m_win->GetEventHandler()->ProcessEvent(event);
1178 }
1179
1180 if ( m_yScrollPosition != yScrollOld )
1181 {
1182 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1183 wxVERTICAL);
1184 event.SetEventObject(m_win);
1185 m_win->GetEventHandler()->ProcessEvent(event);
1186 }
1187 }
1188
1189 // ----------------------------------------------------------------------------
1190 // autoscroll stuff: these functions deal with sending fake scroll events when
1191 // a captured mouse is being held outside the window
1192 // ----------------------------------------------------------------------------
1193
1194 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1195 {
1196 // only send the event if the window is scrollable in this direction
1197 wxWindow *win = (wxWindow *)event.GetEventObject();
1198 return win->HasScrollbar(event.GetOrientation());
1199 }
1200
1201 void wxScrollHelper::StopAutoScrolling()
1202 {
1203 #if wxUSE_TIMER
1204 if ( m_timerAutoScroll )
1205 {
1206 delete m_timerAutoScroll;
1207 m_timerAutoScroll = (wxTimer *)NULL;
1208 }
1209 #endif
1210 }
1211
1212 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
1213 {
1214 StopAutoScrolling();
1215
1216 event.Skip();
1217 }
1218
1219 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1220 {
1221 // don't prevent the usual processing of the event from taking place
1222 event.Skip();
1223
1224 // when a captured mouse leave a scrolled window we start generate
1225 // scrolling events to allow, for example, extending selection beyond the
1226 // visible area in some controls
1227 if ( wxWindow::GetCapture() == m_targetWindow )
1228 {
1229 // where is the mouse leaving?
1230 int pos, orient;
1231 wxPoint pt = event.GetPosition();
1232 if ( pt.x < 0 )
1233 {
1234 orient = wxHORIZONTAL;
1235 pos = 0;
1236 }
1237 else if ( pt.y < 0 )
1238 {
1239 orient = wxVERTICAL;
1240 pos = 0;
1241 }
1242 else // we're lower or to the right of the window
1243 {
1244 wxSize size = m_targetWindow->GetClientSize();
1245 if ( pt.x > size.x )
1246 {
1247 orient = wxHORIZONTAL;
1248 pos = m_xScrollLines;
1249 }
1250 else if ( pt.y > size.y )
1251 {
1252 orient = wxVERTICAL;
1253 pos = m_yScrollLines;
1254 }
1255 else // this should be impossible
1256 {
1257 // but seems to happen sometimes under wxMSW - maybe it's a bug
1258 // there but for now just ignore it
1259
1260 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1261
1262 return;
1263 }
1264 }
1265
1266 // only start the auto scroll timer if the window can be scrolled in
1267 // this direction
1268 if ( !m_targetWindow->HasScrollbar(orient) )
1269 return;
1270
1271 #if wxUSE_TIMER
1272 delete m_timerAutoScroll;
1273 m_timerAutoScroll = new wxAutoScrollTimer
1274 (
1275 m_targetWindow, this,
1276 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1277 : wxEVT_SCROLLWIN_LINEDOWN,
1278 pos,
1279 orient
1280 );
1281 m_timerAutoScroll->Start(50); // FIXME: make configurable
1282 #else
1283 wxUnusedVar(pos);
1284 #endif
1285 }
1286 }
1287
1288 #if wxUSE_MOUSEWHEEL
1289
1290 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1291 {
1292 m_wheelRotation += event.GetWheelRotation();
1293 int lines = m_wheelRotation / event.GetWheelDelta();
1294 m_wheelRotation -= lines * event.GetWheelDelta();
1295
1296 if (lines != 0)
1297 {
1298
1299 wxScrollWinEvent newEvent;
1300
1301 newEvent.SetPosition(0);
1302 newEvent.SetOrientation(wxVERTICAL);
1303 newEvent.SetEventObject(m_win);
1304
1305 if (event.IsPageScroll())
1306 {
1307 if (lines > 0)
1308 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
1309 else
1310 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
1311
1312 m_win->GetEventHandler()->ProcessEvent(newEvent);
1313 }
1314 else
1315 {
1316 lines *= event.GetLinesPerAction();
1317 if (lines > 0)
1318 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
1319 else
1320 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
1321
1322 int times = abs(lines);
1323 for (; times > 0; times--)
1324 m_win->GetEventHandler()->ProcessEvent(newEvent);
1325 }
1326 }
1327 }
1328
1329 #endif // wxUSE_MOUSEWHEEL
1330
1331 // ----------------------------------------------------------------------------
1332 // wxScrolledWindow implementation
1333 // ----------------------------------------------------------------------------
1334
1335 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
1336
1337 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
1338 EVT_PAINT(wxScrolledWindow::OnPaint)
1339 END_EVENT_TABLE()
1340
1341 bool wxScrolledWindow::Create(wxWindow *parent,
1342 wxWindowID id,
1343 const wxPoint& pos,
1344 const wxSize& size,
1345 long style,
1346 const wxString& name)
1347 {
1348 m_targetWindow = this;
1349 #ifdef __WXMAC__
1350 MacSetClipChildren( true ) ;
1351 #endif
1352
1353 bool ok = wxPanel::Create(parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name);
1354
1355 return ok;
1356 }
1357
1358 wxScrolledWindow::~wxScrolledWindow()
1359 {
1360 }
1361
1362 void wxScrolledWindow::OnPaint(wxPaintEvent& event)
1363 {
1364 // the user code didn't really draw the window if we got here, so set this
1365 // flag to try to call OnDraw() later
1366 m_handler->ResetDrawnFlag();
1367
1368 event.Skip();
1369 }
1370
1371 #ifdef __WXMSW__
1372 WXLRESULT wxScrolledWindow::MSWWindowProc(WXUINT nMsg,
1373 WXWPARAM wParam,
1374 WXLPARAM lParam)
1375 {
1376 WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1377
1378 #ifndef __WXWINCE__
1379 // we need to process arrows ourselves for scrolling
1380 if ( nMsg == WM_GETDLGCODE )
1381 {
1382 rc |= DLGC_WANTARROWS;
1383 }
1384 #endif
1385
1386 return rc;
1387 }
1388
1389 #endif // __WXMSW__