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