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