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