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