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