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