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