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