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