]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
Added a file
[wxWidgets.git] / src / generic / scrlwing.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
d80cd92a 2// Name: generic/scrolwin.cpp
fa3541bd 3// Purpose: wxGenericScrolledWindow implementation
c801d85f 4// Author: Julian Smart
1e6feb95 5// Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
1e6feb95 8// Copyright: (c) wxWindows team
a58a12e9 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d80cd92a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
3a8c693a 21 #pragma implementation "genscrolwin.h"
c801d85f
KB
22#endif
23
bcd055ae
JJ
24#ifdef __VMS
25#define XtDisplay XTDISPLAY
26#endif
27
c801d85f
KB
28// For compilers that support precompilation, includes "wx.h".
29#include "wx/wxprec.h"
30
c801d85f 31#ifdef __BORLANDC__
d80cd92a 32 #pragma hdrstop
c801d85f
KB
33#endif
34
3a8c693a
VZ
35#if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
36
d80cd92a
VZ
37#include "wx/utils.h"
38#include "wx/dcclient.h"
39
1e6feb95 40#include "wx/scrolwin.h"
053f9cc1 41#include "wx/panel.h"
1e6feb95 42#include "wx/timer.h"
c801d85f 43
48d1144b 44#ifdef __WXMSW__
1e6feb95 45 #include <windows.h> // for DLGC_WANTARROWS
48d1144b
JS
46#endif
47
a91b47e8
JS
48#ifdef __WXMOTIF__
49// For wxRETAINED implementation
338dd992
JJ
50#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
51 //This code switches off the compiler warnings
52# pragma message disable nosimpint
53#endif
a91b47e8 54#include <Xm/Xm.h>
338dd992
JJ
55#ifdef __VMS__
56# pragma message enable nosimpint
57#endif
a91b47e8
JS
58#endif
59
fa3541bd 60IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
fa3541bd 61
d80cd92a 62// ----------------------------------------------------------------------------
1e6feb95
VZ
63// wxScrollHelperEvtHandler: intercept the events from the window and forward
64// them to wxScrollHelper
d80cd92a
VZ
65// ----------------------------------------------------------------------------
66
1e6feb95
VZ
67class wxScrollHelperEvtHandler : public wxEvtHandler
68{
69public:
70 wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper)
71 {
72 m_scrollHelper = scrollHelper;
73 }
d80cd92a 74
1e6feb95
VZ
75 virtual bool ProcessEvent(wxEvent& event);
76
77private:
78 wxScrollHelper *m_scrollHelper;
79};
80
81// ----------------------------------------------------------------------------
82// wxAutoScrollTimer: the timer used to generate a stream of scroll events when
83// a captured mouse is held outside the window
84// ----------------------------------------------------------------------------
85
86class wxAutoScrollTimer : public wxTimer
87{
88public:
89 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
90 wxEventType eventTypeToSend,
91 int pos, int orient);
92
93 virtual void Notify();
94
95private:
96 wxWindow *m_win;
97 wxScrollHelper *m_scrollHelper;
98 wxEventType m_eventType;
99 int m_pos,
100 m_orient;
101};
d80cd92a
VZ
102
103// ============================================================================
104// implementation
105// ============================================================================
106
107// ----------------------------------------------------------------------------
1e6feb95 108// wxAutoScrollTimer
d80cd92a
VZ
109// ----------------------------------------------------------------------------
110
1e6feb95
VZ
111wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
112 wxScrollHelper *scroll,
113 wxEventType eventTypeToSend,
114 int pos, int orient)
c801d85f 115{
1e6feb95
VZ
116 m_win = winToScroll;
117 m_scrollHelper = scroll;
118 m_eventType = eventTypeToSend;
119 m_pos = pos;
120 m_orient = orient;
c801d85f
KB
121}
122
1e6feb95 123void wxAutoScrollTimer::Notify()
c801d85f 124{
1e6feb95
VZ
125 // only do all this as long as the window is capturing the mouse
126 if ( wxWindow::GetCapture() != m_win )
127 {
128 Stop();
129 }
130 else // we still capture the mouse, continue generating events
131 {
132 // first scroll the window if we are allowed to do it
133 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
134 event1.SetEventObject(m_win);
135 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
136 m_win->GetEventHandler()->ProcessEvent(event1) )
137 {
138 // and then send a pseudo mouse-move event to refresh the selection
139 wxMouseEvent event2(wxEVT_MOTION);
140 wxGetMousePosition(&event2.m_x, &event2.m_y);
141
142 // the mouse event coordinates should be client, not screen as
143 // returned by wxGetMousePosition
144 wxWindow *parentTop = m_win;
145 while ( parentTop->GetParent() )
146 parentTop = parentTop->GetParent();
147 wxPoint ptOrig = parentTop->GetPosition();
148 event2.m_x -= ptOrig.x;
149 event2.m_y -= ptOrig.y;
150
151 event2.SetEventObject(m_win);
152
153 // FIXME: we don't fill in the other members - ok?
154
155 m_win->GetEventHandler()->ProcessEvent(event2);
156 }
157 else // can't scroll further, stop
158 {
159 Stop();
160 }
161 }
162}
163
164// ----------------------------------------------------------------------------
165// wxScrollHelperEvtHandler
166// ----------------------------------------------------------------------------
167
168bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
169{
170 if ( wxEvtHandler::ProcessEvent(event) )
171 return TRUE;
172
173 // reset the skipped flag to FALSE as it might have been set to TRUE in
174 // ProcessEvent() above
175 event.Skip(FALSE);
176
e421922f 177 wxEventType evType = event.GetEventType();
1e6feb95 178
e421922f
VZ
179 if ( evType == wxEVT_PAINT )
180 {
181 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
182 return TRUE;
183 }
1e6feb95 184
e421922f
VZ
185 if ( evType == wxEVT_SCROLLWIN_TOP ||
186 evType == wxEVT_SCROLLWIN_BOTTOM ||
187 evType == wxEVT_SCROLLWIN_LINEUP ||
188 evType == wxEVT_SCROLLWIN_LINEDOWN ||
189 evType == wxEVT_SCROLLWIN_PAGEUP ||
190 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
191 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
192 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
193 {
194 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
1e6feb95 195 return !event.GetSkipped();
e421922f 196 }
1e6feb95 197
e421922f
VZ
198 if ( evType == wxEVT_ENTER_WINDOW )
199 {
200 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
201 }
202 else if ( evType == wxEVT_LEAVE_WINDOW )
203 {
204 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
205 }
206#if wxUSE_MOUSEWHEEL
207 else if ( evType == wxEVT_MOUSEWHEEL )
208 {
209 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
210 }
211#endif // wxUSE_MOUSEWHEEL
212 else if ( evType == wxEVT_SIZE )
213 {
214 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
215 }
216 else if ( evType == wxEVT_CHAR )
217 {
218 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
219 return !event.GetSkipped();
1e6feb95
VZ
220 }
221
222 return FALSE;
223}
224
225// ----------------------------------------------------------------------------
226// wxScrollHelper construction
227// ----------------------------------------------------------------------------
228
229wxScrollHelper::wxScrollHelper(wxWindow *win)
230{
231 m_xScrollPixelsPerLine =
232 m_yScrollPixelsPerLine =
233 m_xScrollPosition =
234 m_yScrollPosition =
235 m_xScrollLines =
236 m_yScrollLines =
237 m_xScrollLinesPerPage =
139adb6a 238 m_yScrollLinesPerPage = 0;
1e6feb95
VZ
239
240 m_xScrollingEnabled =
241 m_yScrollingEnabled = TRUE;
242
243 m_scaleX =
139adb6a 244 m_scaleY = 1.0;
24ce4c18 245#if wxUSE_MOUSEWHEEL
d2c52078 246 m_wheelRotation = 0;
24ce4c18 247#endif
a58a12e9 248
1e6feb95
VZ
249 m_win =
250 m_targetWindow = (wxWindow *)NULL;
139adb6a 251
1e6feb95 252 m_timerAutoScroll = (wxTimer *)NULL;
d7da9756 253
1e6feb95
VZ
254 if ( win )
255 SetWindow(win);
256}
d7da9756 257
1e6feb95
VZ
258void wxScrollHelper::SetWindow(wxWindow *win)
259{
260 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
261
262 m_targetWindow = m_win = win;
263
264 // install the event handler which will intercept the events we're
265 // interested in
266 m_win->PushEventHandler(new wxScrollHelperEvtHandler(this));
c801d85f
KB
267}
268
1e6feb95 269wxScrollHelper::~wxScrollHelper()
d80cd92a 270{
1e6feb95
VZ
271 StopAutoScrolling();
272
273 if ( m_targetWindow )
274 m_targetWindow->PopEventHandler(TRUE /* do delete it */);
d80cd92a
VZ
275}
276
277// ----------------------------------------------------------------------------
278// setting scrolling parameters
279// ----------------------------------------------------------------------------
280
1e6feb95
VZ
281void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
282 int pixelsPerUnitY,
283 int noUnitsX,
284 int noUnitsY,
285 int xPos,
286 int yPos,
287 bool noRefresh)
c801d85f 288{
b0486e0d
SN
289 int xpos, ypos;
290
291 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
292 bool do_refresh =
293 (
c801d85f 294 (noUnitsX != 0 && m_xScrollLines == 0) ||
d2c52078 295 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
b0486e0d 296
c801d85f 297 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 298 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 299 (xPos != m_xScrollPosition) ||
b0486e0d 300 (yPos != m_yScrollPosition)
139adb6a 301 );
a58a12e9 302
139adb6a
RR
303 m_xScrollPixelsPerLine = pixelsPerUnitX;
304 m_yScrollPixelsPerLine = pixelsPerUnitY;
305 m_xScrollPosition = xPos;
306 m_yScrollPosition = yPos;
307 m_xScrollLines = noUnitsX;
308 m_yScrollLines = noUnitsY;
a91b47e8
JS
309
310#ifdef __WXMOTIF__
311 // Sorry, some Motif-specific code to implement a backing pixmap
312 // for the wxRETAINED style. Implementing a backing store can't
313 // be entirely generic because it relies on the wxWindowDC implementation
314 // to duplicate X drawing calls for the backing pixmap.
315
1e6feb95 316 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
a91b47e8 317 {
1e6feb95 318 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
a91b47e8
JS
319
320 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
321 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1e6feb95
VZ
322 if (m_targetWindow->m_backingPixmap &&
323 !((m_targetWindow->m_pixmapWidth == totalPixelWidth) &&
324 (m_targetWindow->m_pixmapHeight == totalPixelHeight)))
a91b47e8 325 {
1e6feb95
VZ
326 XFreePixmap (dpy, (Pixmap) m_targetWindow->m_backingPixmap);
327 m_targetWindow->m_backingPixmap = (WXPixmap) 0;
a91b47e8
JS
328 }
329
1e6feb95 330 if (!m_targetWindow->m_backingPixmap &&
a91b47e8
JS
331 (noUnitsX != 0) && (noUnitsY != 0))
332 {
333 int depth = wxDisplayDepth();
334 m_pixmapWidth = totalPixelWidth;
335 m_pixmapHeight = totalPixelHeight;
336 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
337 m_pixmapWidth, m_pixmapHeight, depth);
d80cd92a 338 }
a91b47e8
JS
339
340 }
d80cd92a 341#endif // Motif
a58a12e9 342
139adb6a 343 AdjustScrollbars();
a58a12e9
VZ
344
345 if (do_refresh && !noRefresh)
1e6feb95 346 m_targetWindow->Refresh(TRUE, GetRect());
a58a12e9 347
7c74e7fe 348#ifdef __WXMAC__
d80cd92a 349 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
350#endif
351}
352
d80cd92a
VZ
353// ----------------------------------------------------------------------------
354// target window handling
355// ----------------------------------------------------------------------------
ecab4dba 356
1e6feb95 357void wxScrollHelper::SetTargetWindow( wxWindow *target )
ecab4dba
RR
358{
359 wxASSERT_MSG( target, wxT("target window must not be NULL") );
360 m_targetWindow = target;
361}
362
1e6feb95 363wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
364{
365 return m_targetWindow;
366}
367
d80cd92a
VZ
368// ----------------------------------------------------------------------------
369// scrolling implementation itself
370// ----------------------------------------------------------------------------
371
1e6feb95 372void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 373{
139adb6a 374 int nScrollInc = CalcScrollInc(event);
1e6feb95 375 if ( nScrollInc == 0 )
139adb6a 376 {
1e6feb95
VZ
377 // can't scroll further
378 event.Skip();
379
380 return;
139adb6a 381 }
c801d85f 382
1e6feb95 383 int orient = event.GetOrientation();
139adb6a
RR
384 if (orient == wxHORIZONTAL)
385 {
386 m_xScrollPosition += nScrollInc;
1e6feb95 387 m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition, FALSE);
139adb6a 388 }
c801d85f 389 else
139adb6a
RR
390 {
391 m_yScrollPosition += nScrollInc;
1e6feb95 392 m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition, FALSE);
139adb6a 393 }
a58a12e9 394
1e6feb95
VZ
395 bool needsRefresh = FALSE;
396 int dx = 0,
397 dy = 0;
139adb6a
RR
398 if (orient == wxHORIZONTAL)
399 {
1e6feb95
VZ
400 if ( m_xScrollingEnabled )
401 {
402 dx = -m_xScrollPixelsPerLine * nScrollInc;
403 }
139adb6a 404 else
1e6feb95
VZ
405 {
406 needsRefresh = TRUE;
407 }
139adb6a 408 }
c801d85f 409 else
139adb6a 410 {
1e6feb95
VZ
411 if ( m_yScrollingEnabled )
412 {
413 dy = -m_yScrollPixelsPerLine * nScrollInc;
414 }
139adb6a 415 else
1e6feb95
VZ
416 {
417 needsRefresh = TRUE;
418 }
419 }
420
421 if ( needsRefresh )
422 {
423 m_targetWindow->Refresh(TRUE, GetRect());
424 }
425 else
426 {
427 m_targetWindow->ScrollWindow(dx, dy, GetRect());
3d2b9c20 428 }
1e6feb95 429
7c74e7fe 430#ifdef __WXMAC__
d80cd92a 431 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 432#endif
c801d85f
KB
433}
434
1e6feb95 435int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 436{
ecab4dba
RR
437 int pos = event.GetPosition();
438 int orient = event.GetOrientation();
c801d85f 439
ecab4dba 440 int nScrollInc = 0;
1a8caf94 441 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 442 {
ecab4dba
RR
443 if (orient == wxHORIZONTAL)
444 nScrollInc = - m_xScrollPosition;
445 else
446 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
447 } else
448 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
449 {
ecab4dba
RR
450 if (orient == wxHORIZONTAL)
451 nScrollInc = m_xScrollLines - m_xScrollPosition;
452 else
453 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
454 } else
455 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
456 {
ecab4dba 457 nScrollInc = -1;
1a8caf94
RR
458 } else
459 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
460 {
ecab4dba 461 nScrollInc = 1;
1a8caf94
RR
462 } else
463 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
464 {
ecab4dba
RR
465 if (orient == wxHORIZONTAL)
466 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
467 else
468 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
469 } else
470 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
471 {
ecab4dba
RR
472 if (orient == wxHORIZONTAL)
473 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
474 else
475 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
476 } else
477 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
478 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
479 {
ecab4dba
RR
480 if (orient == wxHORIZONTAL)
481 nScrollInc = pos - m_xScrollPosition;
482 else
483 nScrollInc = pos - m_yScrollPosition;
c801d85f 484 }
88150e60 485
ecab4dba
RR
486 if (orient == wxHORIZONTAL)
487 {
a58a12e9 488 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
489 {
490 int w, h;
1e6feb95 491 GetTargetSize(&w, &h);
ecab4dba
RR
492
493 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
494 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
495 if (noPositions < 0)
d80cd92a 496 noPositions = 0;
ecab4dba
RR
497
498 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 499 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 500 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 501 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
502 }
503 else
1e6feb95 504 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6
VZ
505 }
506 else
ecab4dba 507 {
a58a12e9 508 if (m_yScrollPixelsPerLine > 0)
d80cd92a 509 {
ecab4dba 510 int w, h;
1e6feb95 511 GetTargetSize(&w, &h);
a58a12e9 512
ecab4dba
RR
513 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
514 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
515 if (noPositions < 0)
d80cd92a 516 noPositions = 0;
a58a12e9 517
ecab4dba 518 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 519 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 520 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 521 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
522 }
523 else
1e6feb95 524 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6 525 }
9d9355c6 526
ecab4dba 527 return nScrollInc;
c801d85f
KB
528}
529
530// Adjust the scrollbars - new version.
1e6feb95 531void wxScrollHelper::AdjustScrollbars()
c801d85f 532{
aeb313f3
SC
533#ifdef __WXMAC__
534 m_targetWindow->MacUpdateImmediately();
535#endif
536
139adb6a 537 int w, h;
1e6feb95 538 GetTargetSize(&w, &h);
a58a12e9 539
27d029c7
RR
540 int oldXScroll = m_xScrollPosition;
541 int oldYScroll = m_yScrollPosition;
c801d85f 542
139adb6a
RR
543 if (m_xScrollLines > 0)
544 {
3d2b9c20
RR
545 // Calculate page size i.e. number of scroll units you get on the
546 // current client window
ecab4dba 547 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 548 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
549 if ( noPagePositions > m_xScrollLines )
550 noPagePositions = m_xScrollLines;
c801d85f 551
139adb6a 552 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
553 // the visible portion of it or if below zero
554 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 555 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 556
1e6feb95 557 m_targetWindow->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
558 // The amount by which we scroll when paging
559 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
560 }
561 else
a58a12e9 562 {
139adb6a 563 m_xScrollPosition = 0;
1e6feb95 564 m_targetWindow->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 565 }
a58a12e9 566
139adb6a
RR
567 if (m_yScrollLines > 0)
568 {
3d2b9c20
RR
569 // Calculate page size i.e. number of scroll units you get on the
570 // current client window
ecab4dba 571 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 572 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
573 if ( noPagePositions > m_yScrollLines )
574 noPagePositions = m_yScrollLines;
c801d85f 575
139adb6a 576 // Correct position if greater than extent of canvas minus
3d2b9c20 577 // the visible portion of it or if below zero
139adb6a
RR
578 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
579 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
580
1e6feb95 581 m_targetWindow->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
582 // The amount by which we scroll when paging
583 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
584 }
585 else
586 {
587 m_yScrollPosition = 0;
1e6feb95 588 m_targetWindow->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 589 }
a58a12e9 590
27d029c7
RR
591 if (oldXScroll != m_xScrollPosition)
592 {
593 if (m_xScrollingEnabled)
1e6feb95
VZ
594 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
595 GetRect() );
27d029c7 596 else
1e6feb95 597 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 598 }
a58a12e9 599
27d029c7
RR
600 if (oldYScroll != m_yScrollPosition)
601 {
602 if (m_yScrollingEnabled)
1e6feb95
VZ
603 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
604 GetRect() );
27d029c7 605 else
1e6feb95 606 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 607 }
aeb313f3
SC
608
609#ifdef __WXMAC__
610 m_targetWindow->MacUpdateImmediately();
611#endif
c801d85f
KB
612}
613
1e6feb95 614void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 615{
1e6feb95
VZ
616 wxPoint pt = dc.GetDeviceOrigin();
617 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
618 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 619 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
620}
621
1e6feb95 622void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 623{
a0bc2c1d
VZ
624 if ( x_unit )
625 *x_unit = m_xScrollPixelsPerLine;
626 if ( y_unit )
627 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
628}
629
1e6feb95 630int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
631{
632 if ( orient == wxHORIZONTAL )
633 return m_xScrollLinesPerPage;
634 else
635 return m_yScrollLinesPerPage;
636}
637
1e6feb95 638void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
639{
640 if ( orient == wxHORIZONTAL )
641 m_xScrollLinesPerPage = pageSize;
642 else
643 m_yScrollLinesPerPage = pageSize;
644}
645
646/*
647 * Scroll to given position (scroll position, not pixel position)
648 */
1e6feb95 649void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 650{
8b089c5e
JS
651 if (!m_targetWindow)
652 return;
653
a58a12e9 654 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 655 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 656
aeb313f3
SC
657#ifdef __WXMAC__
658 m_targetWindow->MacUpdateImmediately();
659#endif
660
139adb6a 661 int w, h;
1e6feb95 662 GetTargetSize(&w, &h);
c801d85f 663
8775b357 664 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 665 {
ed673c6a 666 int old_x = m_xScrollPosition;
139adb6a 667 m_xScrollPosition = x_pos;
a58a12e9 668
3d2b9c20
RR
669 // Calculate page size i.e. number of scroll units you get on the
670 // current client window
ecab4dba 671 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
672 if (noPagePositions < 1) noPagePositions = 1;
673
674 // Correct position if greater than extent of canvas minus
3d2b9c20 675 // the visible portion of it or if below zero
139adb6a
RR
676 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
677 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 678
f6bcfd97 679 if (old_x != m_xScrollPosition) {
1e6feb95
VZ
680 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, FALSE );
681 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
682 GetRect() );
f6bcfd97 683 }
c801d85f 684 }
8775b357 685 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 686 {
ed673c6a 687 int old_y = m_yScrollPosition;
139adb6a 688 m_yScrollPosition = y_pos;
a58a12e9 689
3d2b9c20
RR
690 // Calculate page size i.e. number of scroll units you get on the
691 // current client window
ecab4dba 692 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
693 if (noPagePositions < 1) noPagePositions = 1;
694
695 // Correct position if greater than extent of canvas minus
3d2b9c20 696 // the visible portion of it or if below zero
139adb6a
RR
697 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
698 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 699
f6bcfd97 700 if (old_y != m_yScrollPosition) {
1e6feb95
VZ
701 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, FALSE );
702 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
703 GetRect() );
f6bcfd97 704 }
c801d85f 705 }
a58a12e9 706
7c74e7fe 707#ifdef __WXMAC__
3d2b9c20 708 m_targetWindow->MacUpdateImmediately();
7c74e7fe 709#endif
aeb313f3 710
c801d85f
KB
711}
712
1e6feb95 713void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 714{
139adb6a
RR
715 m_xScrollingEnabled = x_scroll;
716 m_yScrollingEnabled = y_scroll;
c801d85f
KB
717}
718
1e6feb95 719void wxScrollHelper::GetVirtualSize (int *x, int *y) const
c801d85f 720{
a0bc2c1d
VZ
721 if ( x )
722 *x = m_xScrollPixelsPerLine * m_xScrollLines;
723 if ( y )
724 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
725}
726
727// Where the current view starts from
1e6feb95 728void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 729{
a0bc2c1d
VZ
730 if ( x )
731 *x = m_xScrollPosition;
732 if ( y )
733 *y = m_yScrollPosition;
c801d85f
KB
734}
735
1e6feb95 736void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 737{
a0bc2c1d
VZ
738 if ( xx )
739 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
740 if ( yy )
741 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
742}
743
1e6feb95 744void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 745{
a0bc2c1d
VZ
746 if ( xx )
747 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
748 if ( yy )
749 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 750}
d80cd92a
VZ
751
752// ----------------------------------------------------------------------------
753// event handlers
754// ----------------------------------------------------------------------------
755
756// Default OnSize resets scrollbars, if any
1e6feb95 757void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a
VZ
758{
759#if wxUSE_CONSTRAINTS
1e6feb95
VZ
760 if ( m_targetWindow->GetAutoLayout() )
761 m_targetWindow->Layout();
d80cd92a
VZ
762#endif
763
764 AdjustScrollbars();
765}
766
767// This calls OnDraw, having adjusted the origin according to the current
768// scroll position
1e6feb95 769void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 770{
1e6feb95
VZ
771 wxPaintDC dc(m_targetWindow);
772 DoPrepareDC(dc);
d80cd92a
VZ
773
774 OnDraw(dc);
775}
776
438e3558
VZ
777// kbd handling: notice that we use OnChar() and not OnKeyDown() for
778// compatibility here - if we used OnKeyDown(), the programs which process
779// arrows themselves in their OnChar() would never get the message and like
780// this they always have the priority
1e6feb95 781void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
782{
783 int stx, sty, // view origin
784 szx, szy, // view size (total)
785 clix, cliy; // view size (on screen)
786
1e6feb95
VZ
787 GetViewStart(&stx, &sty);
788 GetTargetSize(&clix, &cliy);
d80cd92a 789 GetVirtualSize(&szx, &szy);
56dade3c
RL
790
791 if( m_xScrollPixelsPerLine )
792 {
793 clix /= m_xScrollPixelsPerLine;
d7da9756 794 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
795 }
796 else
797 {
798 clix = 0;
d7da9756 799 szx = -1;
56dade3c
RL
800 }
801 if( m_yScrollPixelsPerLine )
802 {
803 cliy /= m_yScrollPixelsPerLine;
804 szy /= m_yScrollPixelsPerLine;
805 }
806 else
807 {
808 cliy = 0;
d7da9756 809 szy = -1;
56dade3c 810 }
d80cd92a 811
ecb01792 812 int dsty;
d80cd92a
VZ
813 switch ( event.KeyCode() )
814 {
815 case WXK_PAGEUP:
816 case WXK_PRIOR:
ecb01792
RL
817 dsty = sty - (5 * cliy / 6);
818 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
819 break;
820
821 case WXK_PAGEDOWN:
822 case WXK_NEXT:
823 Scroll(-1, sty + (5 * cliy / 6));
824 break;
825
d80cd92a
VZ
826 case WXK_HOME:
827 Scroll(0, event.ControlDown() ? 0 : -1);
828 break;
829
830 case WXK_END:
4acd108c 831 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
832 break;
833
834 case WXK_UP:
835 Scroll(-1, sty - 1);
836 break;
837
838 case WXK_DOWN:
839 Scroll(-1, sty + 1);
840 break;
841
842 case WXK_LEFT:
843 Scroll(stx - 1, -1);
844 break;
845
846 case WXK_RIGHT:
847 Scroll(stx + 1, -1);
848 break;
849
850 default:
851 // not for us
852 event.Skip();
853 }
854}
d2c52078 855
1e6feb95
VZ
856// ----------------------------------------------------------------------------
857// autoscroll stuff: these functions deal with sending fake scroll events when
858// a captured mouse is being held outside the window
859// ----------------------------------------------------------------------------
860
861bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
862{
863 // only send the event if the window is scrollable in this direction
864 wxWindow *win = (wxWindow *)event.GetEventObject();
865 return win->HasScrollbar(event.GetOrientation());
866}
867
868void wxScrollHelper::StopAutoScrolling()
869{
870 if ( m_timerAutoScroll )
871 {
872 delete m_timerAutoScroll;
873 m_timerAutoScroll = (wxTimer *)NULL;
874 }
875}
d2c52078 876
1e6feb95 877void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 878{
1e6feb95
VZ
879 StopAutoScrolling();
880
881 event.Skip();
882}
d2c52078 883
1e6feb95
VZ
884void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
885{
886 // don't prevent the usual processing of the event from taking place
887 event.Skip();
888
889 // when a captured mouse leave a scrolled window we start generate
890 // scrolling events to allow, for example, extending selection beyond the
891 // visible area in some controls
892 if ( wxWindow::GetCapture() == m_targetWindow )
893 {
894 // where is the mouse leaving?
895 int pos, orient;
896 wxPoint pt = event.GetPosition();
897 if ( pt.x < 0 )
898 {
899 orient = wxHORIZONTAL;
900 pos = 0;
901 }
902 else if ( pt.y < 0 )
903 {
904 orient = wxVERTICAL;
905 pos = 0;
906 }
907 else // we're lower or to the right of the window
908 {
909 wxSize size = m_targetWindow->GetClientSize();
910 if ( pt.x > size.x )
911 {
912 orient = wxHORIZONTAL;
913 pos = m_xScrollLines;
914 }
915 else if ( pt.y > size.y )
916 {
917 orient = wxVERTICAL;
918 pos = m_yScrollLines;
919 }
920 else // this should be impossible
921 {
922 // but seems to happen sometimes under wxMSW - maybe it's a bug
923 // there but for now just ignore it
924
925 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
926
927 return;
928 }
929 }
930
931 // only start the auto scroll timer if the window can be scrolled in
932 // this direction
933 if ( !m_targetWindow->HasScrollbar(orient) )
934 return;
935
936 delete m_timerAutoScroll;
937 m_timerAutoScroll = new wxAutoScrollTimer
938 (
939 m_targetWindow, this,
940 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
941 : wxEVT_SCROLLWIN_LINEDOWN,
942 pos,
943 orient
944 );
945 m_timerAutoScroll->Start(50); // FIXME: make configurable
946 }
947}
948
e421922f
VZ
949#if wxUSE_MOUSEWHEEL
950
1e6feb95
VZ
951void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
952{
d2c52078 953 m_wheelRotation += event.GetWheelRotation();
1e6feb95 954 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
955 m_wheelRotation -= lines * event.GetWheelDelta();
956
1e6feb95
VZ
957 if (lines != 0)
958 {
d2c52078 959 lines *= event.GetLinesPerAction();
1e6feb95
VZ
960
961 int vsx, vsy;
d2c52078
RD
962 GetViewStart(&vsx, &vsy);
963 Scroll(-1, vsy - lines);
964 }
965}
966
e421922f
VZ
967#endif // wxUSE_MOUSEWHEEL
968
1e6feb95
VZ
969// ----------------------------------------------------------------------------
970// wxGenericScrolledWindow implementation
971// ----------------------------------------------------------------------------
972
973IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
974
975bool wxGenericScrolledWindow::Create(wxWindow *parent,
976 wxWindowID id,
977 const wxPoint& pos,
978 const wxSize& size,
979 long style,
980 const wxString& name)
981{
982 m_targetWindow = this;
983
984 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
985
986#ifdef __WXMSW__
987 // we need to process arrows ourselves for scrolling
988 m_lDlgCode |= DLGC_WANTARROWS;
989#endif // __WXMSW__
990
991 return ok;
992}
993
994wxGenericScrolledWindow::~wxGenericScrolledWindow()
995{
996}
d2c52078 997
1e6feb95
VZ
998#if WXWIN_COMPATIBILITY
999void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1000{
1001 *x_page = GetScrollPageSize(wxHORIZONTAL);
1002 *y_page = GetScrollPageSize(wxVERTICAL);
1003}
d2c52078 1004
1e6feb95
VZ
1005void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1006{
1007 if ( xx )
1008 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1009 if ( yy )
1010 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1011}
1012#endif // WXWIN_COMPATIBILITY
d2c52078 1013
3a8c693a
VZ
1014#endif // !wxGTK
1015