]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
added missing return on error
[wxWidgets.git] / src / generic / scrolwin.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__
d80cd92a 21 #pragma implementation "scrolwin.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
d80cd92a
VZ
35#include "wx/utils.h"
36#include "wx/dcclient.h"
37
1e6feb95 38#include "wx/scrolwin.h"
053f9cc1 39#include "wx/panel.h"
1e6feb95 40#include "wx/timer.h"
c801d85f 41
48d1144b 42#ifdef __WXMSW__
1e6feb95 43 #include <windows.h> // for DLGC_WANTARROWS
48d1144b
JS
44#endif
45
a91b47e8
JS
46#ifdef __WXMOTIF__
47// For wxRETAINED implementation
338dd992
JJ
48#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
49 //This code switches off the compiler warnings
50# pragma message disable nosimpint
51#endif
a91b47e8 52#include <Xm/Xm.h>
338dd992
JJ
53#ifdef __VMS__
54# pragma message enable nosimpint
55#endif
a91b47e8
JS
56#endif
57
fa3541bd
JS
58#ifndef __WXGTK__
59IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
60#endif
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;
d2c52078 245 m_wheelRotation = 0;
a58a12e9 246
1e6feb95
VZ
247 m_win =
248 m_targetWindow = (wxWindow *)NULL;
139adb6a 249
1e6feb95 250 m_timerAutoScroll = (wxTimer *)NULL;
d7da9756 251
1e6feb95
VZ
252 if ( win )
253 SetWindow(win);
254}
d7da9756 255
1e6feb95
VZ
256void wxScrollHelper::SetWindow(wxWindow *win)
257{
258 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
259
260 m_targetWindow = m_win = win;
261
262 // install the event handler which will intercept the events we're
263 // interested in
264 m_win->PushEventHandler(new wxScrollHelperEvtHandler(this));
c801d85f
KB
265}
266
1e6feb95 267wxScrollHelper::~wxScrollHelper()
d80cd92a 268{
1e6feb95
VZ
269 StopAutoScrolling();
270
271 if ( m_targetWindow )
272 m_targetWindow->PopEventHandler(TRUE /* do delete it */);
d80cd92a
VZ
273}
274
275// ----------------------------------------------------------------------------
276// setting scrolling parameters
277// ----------------------------------------------------------------------------
278
1e6feb95
VZ
279void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
280 int pixelsPerUnitY,
281 int noUnitsX,
282 int noUnitsY,
283 int xPos,
284 int yPos,
285 bool noRefresh)
c801d85f 286{
b0486e0d
SN
287 int xpos, ypos;
288
289 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
290 bool do_refresh =
291 (
c801d85f 292 (noUnitsX != 0 && m_xScrollLines == 0) ||
d2c52078 293 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
b0486e0d 294
c801d85f 295 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 296 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 297 (xPos != m_xScrollPosition) ||
b0486e0d 298 (yPos != m_yScrollPosition)
139adb6a 299 );
a58a12e9 300
139adb6a
RR
301 m_xScrollPixelsPerLine = pixelsPerUnitX;
302 m_yScrollPixelsPerLine = pixelsPerUnitY;
303 m_xScrollPosition = xPos;
304 m_yScrollPosition = yPos;
305 m_xScrollLines = noUnitsX;
306 m_yScrollLines = noUnitsY;
a91b47e8
JS
307
308#ifdef __WXMOTIF__
309 // Sorry, some Motif-specific code to implement a backing pixmap
310 // for the wxRETAINED style. Implementing a backing store can't
311 // be entirely generic because it relies on the wxWindowDC implementation
312 // to duplicate X drawing calls for the backing pixmap.
313
1e6feb95 314 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
a91b47e8 315 {
1e6feb95 316 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
a91b47e8
JS
317
318 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
319 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1e6feb95
VZ
320 if (m_targetWindow->m_backingPixmap &&
321 !((m_targetWindow->m_pixmapWidth == totalPixelWidth) &&
322 (m_targetWindow->m_pixmapHeight == totalPixelHeight)))
a91b47e8 323 {
1e6feb95
VZ
324 XFreePixmap (dpy, (Pixmap) m_targetWindow->m_backingPixmap);
325 m_targetWindow->m_backingPixmap = (WXPixmap) 0;
a91b47e8
JS
326 }
327
1e6feb95 328 if (!m_targetWindow->m_backingPixmap &&
a91b47e8
JS
329 (noUnitsX != 0) && (noUnitsY != 0))
330 {
331 int depth = wxDisplayDepth();
332 m_pixmapWidth = totalPixelWidth;
333 m_pixmapHeight = totalPixelHeight;
334 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
335 m_pixmapWidth, m_pixmapHeight, depth);
d80cd92a 336 }
a91b47e8
JS
337
338 }
d80cd92a 339#endif // Motif
a58a12e9 340
139adb6a 341 AdjustScrollbars();
a58a12e9
VZ
342
343 if (do_refresh && !noRefresh)
1e6feb95 344 m_targetWindow->Refresh(TRUE, GetRect());
a58a12e9 345
7c74e7fe 346#ifdef __WXMAC__
d80cd92a 347 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
348#endif
349}
350
d80cd92a
VZ
351// ----------------------------------------------------------------------------
352// target window handling
353// ----------------------------------------------------------------------------
ecab4dba 354
1e6feb95 355void wxScrollHelper::SetTargetWindow( wxWindow *target )
ecab4dba
RR
356{
357 wxASSERT_MSG( target, wxT("target window must not be NULL") );
358 m_targetWindow = target;
359}
360
1e6feb95 361wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
362{
363 return m_targetWindow;
364}
365
d80cd92a
VZ
366// ----------------------------------------------------------------------------
367// scrolling implementation itself
368// ----------------------------------------------------------------------------
369
1e6feb95 370void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 371{
139adb6a 372 int nScrollInc = CalcScrollInc(event);
1e6feb95 373 if ( nScrollInc == 0 )
139adb6a 374 {
1e6feb95
VZ
375 // can't scroll further
376 event.Skip();
377
378 return;
139adb6a 379 }
c801d85f 380
1e6feb95 381 int orient = event.GetOrientation();
139adb6a
RR
382 if (orient == wxHORIZONTAL)
383 {
384 m_xScrollPosition += nScrollInc;
1e6feb95 385 m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition, FALSE);
139adb6a 386 }
c801d85f 387 else
139adb6a
RR
388 {
389 m_yScrollPosition += nScrollInc;
1e6feb95 390 m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition, FALSE);
139adb6a 391 }
a58a12e9 392
1e6feb95
VZ
393 bool needsRefresh = FALSE;
394 int dx = 0,
395 dy = 0;
139adb6a
RR
396 if (orient == wxHORIZONTAL)
397 {
1e6feb95
VZ
398 if ( m_xScrollingEnabled )
399 {
400 dx = -m_xScrollPixelsPerLine * nScrollInc;
401 }
139adb6a 402 else
1e6feb95
VZ
403 {
404 needsRefresh = TRUE;
405 }
139adb6a 406 }
c801d85f 407 else
139adb6a 408 {
1e6feb95
VZ
409 if ( m_yScrollingEnabled )
410 {
411 dy = -m_yScrollPixelsPerLine * nScrollInc;
412 }
139adb6a 413 else
1e6feb95
VZ
414 {
415 needsRefresh = TRUE;
416 }
417 }
418
419 if ( needsRefresh )
420 {
421 m_targetWindow->Refresh(TRUE, GetRect());
422 }
423 else
424 {
425 m_targetWindow->ScrollWindow(dx, dy, GetRect());
3d2b9c20 426 }
1e6feb95 427
7c74e7fe 428#ifdef __WXMAC__
d80cd92a 429 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 430#endif
c801d85f
KB
431}
432
1e6feb95 433int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 434{
ecab4dba
RR
435 int pos = event.GetPosition();
436 int orient = event.GetOrientation();
c801d85f 437
ecab4dba 438 int nScrollInc = 0;
1a8caf94 439 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 440 {
ecab4dba
RR
441 if (orient == wxHORIZONTAL)
442 nScrollInc = - m_xScrollPosition;
443 else
444 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
445 } else
446 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
447 {
ecab4dba
RR
448 if (orient == wxHORIZONTAL)
449 nScrollInc = m_xScrollLines - m_xScrollPosition;
450 else
451 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
452 } else
453 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
454 {
ecab4dba 455 nScrollInc = -1;
1a8caf94
RR
456 } else
457 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
458 {
ecab4dba 459 nScrollInc = 1;
1a8caf94
RR
460 } else
461 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
462 {
ecab4dba
RR
463 if (orient == wxHORIZONTAL)
464 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
465 else
466 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
467 } else
468 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
469 {
ecab4dba
RR
470 if (orient == wxHORIZONTAL)
471 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
472 else
473 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
474 } else
475 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
476 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
477 {
ecab4dba
RR
478 if (orient == wxHORIZONTAL)
479 nScrollInc = pos - m_xScrollPosition;
480 else
481 nScrollInc = pos - m_yScrollPosition;
c801d85f 482 }
88150e60 483
ecab4dba
RR
484 if (orient == wxHORIZONTAL)
485 {
a58a12e9 486 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
487 {
488 int w, h;
1e6feb95 489 GetTargetSize(&w, &h);
ecab4dba
RR
490
491 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
492 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
493 if (noPositions < 0)
d80cd92a 494 noPositions = 0;
ecab4dba
RR
495
496 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 497 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 498 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 499 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
500 }
501 else
1e6feb95 502 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6
VZ
503 }
504 else
ecab4dba 505 {
a58a12e9 506 if (m_yScrollPixelsPerLine > 0)
d80cd92a 507 {
ecab4dba 508 int w, h;
1e6feb95 509 GetTargetSize(&w, &h);
a58a12e9 510
ecab4dba
RR
511 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
512 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
513 if (noPositions < 0)
d80cd92a 514 noPositions = 0;
a58a12e9 515
ecab4dba 516 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 517 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 518 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 519 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
520 }
521 else
1e6feb95 522 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6 523 }
9d9355c6 524
ecab4dba 525 return nScrollInc;
c801d85f
KB
526}
527
528// Adjust the scrollbars - new version.
1e6feb95 529void wxScrollHelper::AdjustScrollbars()
c801d85f 530{
aeb313f3
SC
531#ifdef __WXMAC__
532 m_targetWindow->MacUpdateImmediately();
533#endif
534
139adb6a 535 int w, h;
1e6feb95 536 GetTargetSize(&w, &h);
a58a12e9 537
27d029c7
RR
538 int oldXScroll = m_xScrollPosition;
539 int oldYScroll = m_yScrollPosition;
c801d85f 540
139adb6a
RR
541 if (m_xScrollLines > 0)
542 {
3d2b9c20
RR
543 // Calculate page size i.e. number of scroll units you get on the
544 // current client window
ecab4dba 545 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 546 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
547 if ( noPagePositions > m_xScrollLines )
548 noPagePositions = m_xScrollLines;
c801d85f 549
139adb6a 550 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
551 // the visible portion of it or if below zero
552 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 553 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 554
1e6feb95 555 m_targetWindow->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
556 // The amount by which we scroll when paging
557 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
558 }
559 else
a58a12e9 560 {
139adb6a 561 m_xScrollPosition = 0;
1e6feb95 562 m_targetWindow->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 563 }
a58a12e9 564
139adb6a
RR
565 if (m_yScrollLines > 0)
566 {
3d2b9c20
RR
567 // Calculate page size i.e. number of scroll units you get on the
568 // current client window
ecab4dba 569 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 570 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
571 if ( noPagePositions > m_yScrollLines )
572 noPagePositions = m_yScrollLines;
c801d85f 573
139adb6a 574 // Correct position if greater than extent of canvas minus
3d2b9c20 575 // the visible portion of it or if below zero
139adb6a
RR
576 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
577 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
578
1e6feb95 579 m_targetWindow->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
580 // The amount by which we scroll when paging
581 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
582 }
583 else
584 {
585 m_yScrollPosition = 0;
1e6feb95 586 m_targetWindow->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 587 }
a58a12e9 588
27d029c7
RR
589 if (oldXScroll != m_xScrollPosition)
590 {
591 if (m_xScrollingEnabled)
1e6feb95
VZ
592 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
593 GetRect() );
27d029c7 594 else
1e6feb95 595 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 596 }
a58a12e9 597
27d029c7
RR
598 if (oldYScroll != m_yScrollPosition)
599 {
600 if (m_yScrollingEnabled)
1e6feb95
VZ
601 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
602 GetRect() );
27d029c7 603 else
1e6feb95 604 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 605 }
aeb313f3
SC
606
607#ifdef __WXMAC__
608 m_targetWindow->MacUpdateImmediately();
609#endif
c801d85f
KB
610}
611
1e6feb95 612void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 613{
1e6feb95
VZ
614 wxPoint pt = dc.GetDeviceOrigin();
615 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
616 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 617 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
618}
619
1e6feb95 620void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 621{
a0bc2c1d
VZ
622 if ( x_unit )
623 *x_unit = m_xScrollPixelsPerLine;
624 if ( y_unit )
625 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
626}
627
1e6feb95 628int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
629{
630 if ( orient == wxHORIZONTAL )
631 return m_xScrollLinesPerPage;
632 else
633 return m_yScrollLinesPerPage;
634}
635
1e6feb95 636void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
637{
638 if ( orient == wxHORIZONTAL )
639 m_xScrollLinesPerPage = pageSize;
640 else
641 m_yScrollLinesPerPage = pageSize;
642}
643
644/*
645 * Scroll to given position (scroll position, not pixel position)
646 */
1e6feb95 647void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 648{
8b089c5e
JS
649 if (!m_targetWindow)
650 return;
651
a58a12e9 652 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 653 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 654
aeb313f3
SC
655#ifdef __WXMAC__
656 m_targetWindow->MacUpdateImmediately();
657#endif
658
139adb6a 659 int w, h;
1e6feb95 660 GetTargetSize(&w, &h);
c801d85f 661
8775b357 662 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 663 {
ed673c6a 664 int old_x = m_xScrollPosition;
139adb6a 665 m_xScrollPosition = x_pos;
a58a12e9 666
3d2b9c20
RR
667 // Calculate page size i.e. number of scroll units you get on the
668 // current client window
ecab4dba 669 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
670 if (noPagePositions < 1) noPagePositions = 1;
671
672 // Correct position if greater than extent of canvas minus
3d2b9c20 673 // the visible portion of it or if below zero
139adb6a
RR
674 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
675 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 676
f6bcfd97 677 if (old_x != m_xScrollPosition) {
1e6feb95
VZ
678 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, FALSE );
679 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
680 GetRect() );
f6bcfd97 681 }
c801d85f 682 }
8775b357 683 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 684 {
ed673c6a 685 int old_y = m_yScrollPosition;
139adb6a 686 m_yScrollPosition = y_pos;
a58a12e9 687
3d2b9c20
RR
688 // Calculate page size i.e. number of scroll units you get on the
689 // current client window
ecab4dba 690 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
691 if (noPagePositions < 1) noPagePositions = 1;
692
693 // Correct position if greater than extent of canvas minus
3d2b9c20 694 // the visible portion of it or if below zero
139adb6a
RR
695 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
696 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 697
f6bcfd97 698 if (old_y != m_yScrollPosition) {
1e6feb95
VZ
699 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, FALSE );
700 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
701 GetRect() );
f6bcfd97 702 }
c801d85f 703 }
a58a12e9 704
7c74e7fe 705#ifdef __WXMAC__
3d2b9c20 706 m_targetWindow->MacUpdateImmediately();
7c74e7fe 707#endif
aeb313f3 708
c801d85f
KB
709}
710
1e6feb95 711void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 712{
139adb6a
RR
713 m_xScrollingEnabled = x_scroll;
714 m_yScrollingEnabled = y_scroll;
c801d85f
KB
715}
716
1e6feb95 717void wxScrollHelper::GetVirtualSize (int *x, int *y) const
c801d85f 718{
a0bc2c1d
VZ
719 if ( x )
720 *x = m_xScrollPixelsPerLine * m_xScrollLines;
721 if ( y )
722 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
723}
724
725// Where the current view starts from
1e6feb95 726void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 727{
a0bc2c1d
VZ
728 if ( x )
729 *x = m_xScrollPosition;
730 if ( y )
731 *y = m_yScrollPosition;
c801d85f
KB
732}
733
1e6feb95 734void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 735{
a0bc2c1d
VZ
736 if ( xx )
737 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
738 if ( yy )
739 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
740}
741
1e6feb95 742void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 743{
a0bc2c1d
VZ
744 if ( xx )
745 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
746 if ( yy )
747 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 748}
d80cd92a
VZ
749
750// ----------------------------------------------------------------------------
751// event handlers
752// ----------------------------------------------------------------------------
753
754// Default OnSize resets scrollbars, if any
1e6feb95 755void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a
VZ
756{
757#if wxUSE_CONSTRAINTS
1e6feb95
VZ
758 if ( m_targetWindow->GetAutoLayout() )
759 m_targetWindow->Layout();
d80cd92a
VZ
760#endif
761
762 AdjustScrollbars();
763}
764
765// This calls OnDraw, having adjusted the origin according to the current
766// scroll position
1e6feb95 767void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 768{
1e6feb95
VZ
769 wxPaintDC dc(m_targetWindow);
770 DoPrepareDC(dc);
d80cd92a
VZ
771
772 OnDraw(dc);
773}
774
438e3558
VZ
775// kbd handling: notice that we use OnChar() and not OnKeyDown() for
776// compatibility here - if we used OnKeyDown(), the programs which process
777// arrows themselves in their OnChar() would never get the message and like
778// this they always have the priority
1e6feb95 779void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
780{
781 int stx, sty, // view origin
782 szx, szy, // view size (total)
783 clix, cliy; // view size (on screen)
784
1e6feb95
VZ
785 GetViewStart(&stx, &sty);
786 GetTargetSize(&clix, &cliy);
d80cd92a 787 GetVirtualSize(&szx, &szy);
56dade3c
RL
788
789 if( m_xScrollPixelsPerLine )
790 {
791 clix /= m_xScrollPixelsPerLine;
d7da9756 792 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
793 }
794 else
795 {
796 clix = 0;
d7da9756 797 szx = -1;
56dade3c
RL
798 }
799 if( m_yScrollPixelsPerLine )
800 {
801 cliy /= m_yScrollPixelsPerLine;
802 szy /= m_yScrollPixelsPerLine;
803 }
804 else
805 {
806 cliy = 0;
d7da9756 807 szy = -1;
56dade3c 808 }
d80cd92a 809
ecb01792 810 int dsty;
d80cd92a
VZ
811 switch ( event.KeyCode() )
812 {
813 case WXK_PAGEUP:
814 case WXK_PRIOR:
ecb01792
RL
815 dsty = sty - (5 * cliy / 6);
816 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
817 break;
818
819 case WXK_PAGEDOWN:
820 case WXK_NEXT:
821 Scroll(-1, sty + (5 * cliy / 6));
822 break;
823
d80cd92a
VZ
824 case WXK_HOME:
825 Scroll(0, event.ControlDown() ? 0 : -1);
826 break;
827
828 case WXK_END:
4acd108c 829 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
830 break;
831
832 case WXK_UP:
833 Scroll(-1, sty - 1);
834 break;
835
836 case WXK_DOWN:
837 Scroll(-1, sty + 1);
838 break;
839
840 case WXK_LEFT:
841 Scroll(stx - 1, -1);
842 break;
843
844 case WXK_RIGHT:
845 Scroll(stx + 1, -1);
846 break;
847
848 default:
849 // not for us
850 event.Skip();
851 }
852}
d2c52078 853
1e6feb95
VZ
854// ----------------------------------------------------------------------------
855// autoscroll stuff: these functions deal with sending fake scroll events when
856// a captured mouse is being held outside the window
857// ----------------------------------------------------------------------------
858
859bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
860{
861 // only send the event if the window is scrollable in this direction
862 wxWindow *win = (wxWindow *)event.GetEventObject();
863 return win->HasScrollbar(event.GetOrientation());
864}
865
866void wxScrollHelper::StopAutoScrolling()
867{
868 if ( m_timerAutoScroll )
869 {
870 delete m_timerAutoScroll;
871 m_timerAutoScroll = (wxTimer *)NULL;
872 }
873}
d2c52078 874
1e6feb95 875void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 876{
1e6feb95
VZ
877 StopAutoScrolling();
878
879 event.Skip();
880}
d2c52078 881
1e6feb95
VZ
882void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
883{
884 // don't prevent the usual processing of the event from taking place
885 event.Skip();
886
887 // when a captured mouse leave a scrolled window we start generate
888 // scrolling events to allow, for example, extending selection beyond the
889 // visible area in some controls
890 if ( wxWindow::GetCapture() == m_targetWindow )
891 {
892 // where is the mouse leaving?
893 int pos, orient;
894 wxPoint pt = event.GetPosition();
895 if ( pt.x < 0 )
896 {
897 orient = wxHORIZONTAL;
898 pos = 0;
899 }
900 else if ( pt.y < 0 )
901 {
902 orient = wxVERTICAL;
903 pos = 0;
904 }
905 else // we're lower or to the right of the window
906 {
907 wxSize size = m_targetWindow->GetClientSize();
908 if ( pt.x > size.x )
909 {
910 orient = wxHORIZONTAL;
911 pos = m_xScrollLines;
912 }
913 else if ( pt.y > size.y )
914 {
915 orient = wxVERTICAL;
916 pos = m_yScrollLines;
917 }
918 else // this should be impossible
919 {
920 // but seems to happen sometimes under wxMSW - maybe it's a bug
921 // there but for now just ignore it
922
923 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
924
925 return;
926 }
927 }
928
929 // only start the auto scroll timer if the window can be scrolled in
930 // this direction
931 if ( !m_targetWindow->HasScrollbar(orient) )
932 return;
933
934 delete m_timerAutoScroll;
935 m_timerAutoScroll = new wxAutoScrollTimer
936 (
937 m_targetWindow, this,
938 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
939 : wxEVT_SCROLLWIN_LINEDOWN,
940 pos,
941 orient
942 );
943 m_timerAutoScroll->Start(50); // FIXME: make configurable
944 }
945}
946
e421922f
VZ
947#if wxUSE_MOUSEWHEEL
948
1e6feb95
VZ
949void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
950{
d2c52078 951 m_wheelRotation += event.GetWheelRotation();
1e6feb95 952 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
953 m_wheelRotation -= lines * event.GetWheelDelta();
954
1e6feb95
VZ
955 if (lines != 0)
956 {
d2c52078 957 lines *= event.GetLinesPerAction();
1e6feb95
VZ
958
959 int vsx, vsy;
d2c52078
RD
960 GetViewStart(&vsx, &vsy);
961 Scroll(-1, vsy - lines);
962 }
963}
964
e421922f
VZ
965#endif // wxUSE_MOUSEWHEEL
966
1e6feb95
VZ
967// ----------------------------------------------------------------------------
968// wxGenericScrolledWindow implementation
969// ----------------------------------------------------------------------------
970
971IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
972
973bool wxGenericScrolledWindow::Create(wxWindow *parent,
974 wxWindowID id,
975 const wxPoint& pos,
976 const wxSize& size,
977 long style,
978 const wxString& name)
979{
980 m_targetWindow = this;
981
982 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
983
984#ifdef __WXMSW__
985 // we need to process arrows ourselves for scrolling
986 m_lDlgCode |= DLGC_WANTARROWS;
987#endif // __WXMSW__
988
989 return ok;
990}
991
992wxGenericScrolledWindow::~wxGenericScrolledWindow()
993{
994}
d2c52078 995
1e6feb95
VZ
996#if WXWIN_COMPATIBILITY
997void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
998{
999 *x_page = GetScrollPageSize(wxHORIZONTAL);
1000 *y_page = GetScrollPageSize(wxVERTICAL);
1001}
d2c52078 1002
1e6feb95
VZ
1003void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1004{
1005 if ( xx )
1006 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1007 if ( yy )
1008 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1009}
1010#endif // WXWIN_COMPATIBILITY
d2c52078 1011