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