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