]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
compilation fix
[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{
248d771c
VZ
392 // search for m_handler in the handler list
393 if ( m_win && m_handler )
349efbaa 394 {
74f6bbf9 395 if ( m_win->RemoveEventHandler(m_handler) )
248d771c 396 {
74f6bbf9 397 delete m_handler;
248d771c 398 }
74f6bbf9
VZ
399 //else: something is very wrong, so better [maybe] leak memory than
400 // risk a crash because of double deletion
248d771c 401
74f6bbf9 402 m_handler = NULL;
349efbaa
VZ
403 }
404}
405
406void wxScrollHelper::SetWindow(wxWindow *win)
407{
408 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
409
410 m_win = win;
411
af4088f1
VZ
412 // by default, the associated window is also the target window
413 DoSetTargetWindow(win);
349efbaa
VZ
414}
415
af4088f1 416void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
349efbaa 417{
ecab4dba 418 m_targetWindow = target;
349efbaa
VZ
419
420 // install the event handler which will intercept the events we're
af4088f1
VZ
421 // interested in (but only do it for our real window, not the target window
422 // which we scroll - we don't need to hijack its events)
423 if ( m_targetWindow == m_win )
13ff9344 424 {
248d771c
VZ
425 // if we already have a handler, delete it first
426 DeleteEvtHandler();
427
13ff9344
JS
428 m_handler = new wxScrollHelperEvtHandler(this);
429 m_targetWindow->PushEventHandler(m_handler);
430 }
349efbaa
VZ
431}
432
af4088f1 433void wxScrollHelper::SetTargetWindow(wxWindow *target)
349efbaa
VZ
434{
435 wxCHECK_RET( target, wxT("target window must not be NULL") );
436
437 if ( target == m_targetWindow )
438 return;
439
af4088f1 440 DoSetTargetWindow(target);
ecab4dba
RR
441}
442
1e6feb95 443wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
444{
445 return m_targetWindow;
446}
447
d80cd92a
VZ
448// ----------------------------------------------------------------------------
449// scrolling implementation itself
450// ----------------------------------------------------------------------------
451
1e6feb95 452void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 453{
139adb6a 454 int nScrollInc = CalcScrollInc(event);
1e6feb95 455 if ( nScrollInc == 0 )
139adb6a 456 {
1e6feb95
VZ
457 // can't scroll further
458 event.Skip();
459
460 return;
139adb6a 461 }
c801d85f 462
1e6feb95 463 int orient = event.GetOrientation();
139adb6a
RR
464 if (orient == wxHORIZONTAL)
465 {
466 m_xScrollPosition += nScrollInc;
5f3286d1 467 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
139adb6a 468 }
c801d85f 469 else
139adb6a
RR
470 {
471 m_yScrollPosition += nScrollInc;
5f3286d1 472 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
139adb6a 473 }
a58a12e9 474
1e6feb95
VZ
475 bool needsRefresh = FALSE;
476 int dx = 0,
477 dy = 0;
139adb6a
RR
478 if (orient == wxHORIZONTAL)
479 {
1e6feb95
VZ
480 if ( m_xScrollingEnabled )
481 {
482 dx = -m_xScrollPixelsPerLine * nScrollInc;
483 }
139adb6a 484 else
1e6feb95
VZ
485 {
486 needsRefresh = TRUE;
487 }
139adb6a 488 }
c801d85f 489 else
139adb6a 490 {
1e6feb95
VZ
491 if ( m_yScrollingEnabled )
492 {
493 dy = -m_yScrollPixelsPerLine * nScrollInc;
494 }
139adb6a 495 else
1e6feb95
VZ
496 {
497 needsRefresh = TRUE;
498 }
499 }
500
501 if ( needsRefresh )
502 {
503 m_targetWindow->Refresh(TRUE, GetRect());
504 }
505 else
506 {
507 m_targetWindow->ScrollWindow(dx, dy, GetRect());
3d2b9c20 508 }
1e6feb95 509
7c74e7fe 510#ifdef __WXMAC__
d80cd92a 511 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 512#endif
c801d85f
KB
513}
514
1e6feb95 515int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 516{
ecab4dba
RR
517 int pos = event.GetPosition();
518 int orient = event.GetOrientation();
c801d85f 519
ecab4dba 520 int nScrollInc = 0;
1a8caf94 521 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 522 {
ecab4dba
RR
523 if (orient == wxHORIZONTAL)
524 nScrollInc = - m_xScrollPosition;
525 else
526 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
527 } else
528 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
529 {
ecab4dba
RR
530 if (orient == wxHORIZONTAL)
531 nScrollInc = m_xScrollLines - m_xScrollPosition;
532 else
533 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
534 } else
535 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
536 {
ecab4dba 537 nScrollInc = -1;
1a8caf94
RR
538 } else
539 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
540 {
ecab4dba 541 nScrollInc = 1;
1a8caf94
RR
542 } else
543 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
544 {
ecab4dba
RR
545 if (orient == wxHORIZONTAL)
546 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
547 else
548 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
549 } else
550 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
551 {
ecab4dba
RR
552 if (orient == wxHORIZONTAL)
553 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
554 else
555 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
556 } else
557 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
558 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
559 {
ecab4dba
RR
560 if (orient == wxHORIZONTAL)
561 nScrollInc = pos - m_xScrollPosition;
562 else
563 nScrollInc = pos - m_yScrollPosition;
c801d85f 564 }
88150e60 565
ecab4dba
RR
566 if (orient == wxHORIZONTAL)
567 {
a58a12e9 568 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
569 {
570 int w, h;
1e6feb95 571 GetTargetSize(&w, &h);
ecab4dba
RR
572
573 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
574 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
575 if (noPositions < 0)
d80cd92a 576 noPositions = 0;
ecab4dba
RR
577
578 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 579 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 580 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 581 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
582 }
583 else
1e6feb95 584 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6
VZ
585 }
586 else
ecab4dba 587 {
a58a12e9 588 if (m_yScrollPixelsPerLine > 0)
d80cd92a 589 {
ecab4dba 590 int w, h;
1e6feb95 591 GetTargetSize(&w, &h);
a58a12e9 592
ecab4dba
RR
593 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
594 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
595 if (noPositions < 0)
d80cd92a 596 noPositions = 0;
a58a12e9 597
ecab4dba 598 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 599 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 600 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 601 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
602 }
603 else
1e6feb95 604 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6 605 }
9d9355c6 606
ecab4dba 607 return nScrollInc;
c801d85f
KB
608}
609
610// Adjust the scrollbars - new version.
1e6feb95 611void wxScrollHelper::AdjustScrollbars()
c801d85f 612{
aeb313f3
SC
613#ifdef __WXMAC__
614 m_targetWindow->MacUpdateImmediately();
615#endif
616
139adb6a 617 int w, h;
1e6feb95 618 GetTargetSize(&w, &h);
a58a12e9 619
27d029c7
RR
620 int oldXScroll = m_xScrollPosition;
621 int oldYScroll = m_yScrollPosition;
c801d85f 622
139adb6a
RR
623 if (m_xScrollLines > 0)
624 {
3d2b9c20
RR
625 // Calculate page size i.e. number of scroll units you get on the
626 // current client window
ecab4dba 627 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 628 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
629 if ( noPagePositions > m_xScrollLines )
630 noPagePositions = m_xScrollLines;
c801d85f 631
139adb6a 632 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
633 // the visible portion of it or if below zero
634 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 635 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 636
5f3286d1 637 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
638 // The amount by which we scroll when paging
639 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
640 }
641 else
a58a12e9 642 {
139adb6a 643 m_xScrollPosition = 0;
5f3286d1 644 m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 645 }
a58a12e9 646
139adb6a
RR
647 if (m_yScrollLines > 0)
648 {
3d2b9c20
RR
649 // Calculate page size i.e. number of scroll units you get on the
650 // current client window
ecab4dba 651 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 652 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
653 if ( noPagePositions > m_yScrollLines )
654 noPagePositions = m_yScrollLines;
c801d85f 655
139adb6a 656 // Correct position if greater than extent of canvas minus
3d2b9c20 657 // the visible portion of it or if below zero
139adb6a
RR
658 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
659 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
660
5f3286d1 661 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
662 // The amount by which we scroll when paging
663 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
664 }
665 else
666 {
667 m_yScrollPosition = 0;
5f3286d1 668 m_win->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 669 }
a58a12e9 670
27d029c7
RR
671 if (oldXScroll != m_xScrollPosition)
672 {
673 if (m_xScrollingEnabled)
1e6feb95
VZ
674 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
675 GetRect() );
27d029c7 676 else
1e6feb95 677 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 678 }
a58a12e9 679
27d029c7
RR
680 if (oldYScroll != m_yScrollPosition)
681 {
682 if (m_yScrollingEnabled)
1e6feb95
VZ
683 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
684 GetRect() );
27d029c7 685 else
1e6feb95 686 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 687 }
aeb313f3
SC
688
689#ifdef __WXMAC__
690 m_targetWindow->MacUpdateImmediately();
691#endif
c801d85f
KB
692}
693
1e6feb95 694void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 695{
1e6feb95
VZ
696 wxPoint pt = dc.GetDeviceOrigin();
697 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
698 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 699 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
700}
701
1e6feb95 702void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 703{
a0bc2c1d
VZ
704 if ( x_unit )
705 *x_unit = m_xScrollPixelsPerLine;
706 if ( y_unit )
707 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
708}
709
1e6feb95 710int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
711{
712 if ( orient == wxHORIZONTAL )
713 return m_xScrollLinesPerPage;
714 else
715 return m_yScrollLinesPerPage;
716}
717
1e6feb95 718void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
719{
720 if ( orient == wxHORIZONTAL )
721 m_xScrollLinesPerPage = pageSize;
722 else
723 m_yScrollLinesPerPage = pageSize;
724}
725
726/*
727 * Scroll to given position (scroll position, not pixel position)
728 */
1e6feb95 729void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 730{
8b089c5e
JS
731 if (!m_targetWindow)
732 return;
733
a58a12e9 734 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 735 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 736
aeb313f3
SC
737#ifdef __WXMAC__
738 m_targetWindow->MacUpdateImmediately();
739#endif
740
139adb6a 741 int w, h;
1e6feb95 742 GetTargetSize(&w, &h);
c801d85f 743
8775b357 744 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 745 {
ed673c6a 746 int old_x = m_xScrollPosition;
139adb6a 747 m_xScrollPosition = x_pos;
a58a12e9 748
3d2b9c20
RR
749 // Calculate page size i.e. number of scroll units you get on the
750 // current client window
ecab4dba 751 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
752 if (noPagePositions < 1) noPagePositions = 1;
753
754 // Correct position if greater than extent of canvas minus
3d2b9c20 755 // the visible portion of it or if below zero
139adb6a
RR
756 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
757 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 758
f6bcfd97 759 if (old_x != m_xScrollPosition) {
5f3286d1 760 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
1e6feb95
VZ
761 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
762 GetRect() );
f6bcfd97 763 }
c801d85f 764 }
8775b357 765 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 766 {
ed673c6a 767 int old_y = m_yScrollPosition;
139adb6a 768 m_yScrollPosition = y_pos;
a58a12e9 769
3d2b9c20
RR
770 // Calculate page size i.e. number of scroll units you get on the
771 // current client window
ecab4dba 772 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
773 if (noPagePositions < 1) noPagePositions = 1;
774
775 // Correct position if greater than extent of canvas minus
3d2b9c20 776 // the visible portion of it or if below zero
139adb6a
RR
777 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
778 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 779
f6bcfd97 780 if (old_y != m_yScrollPosition) {
5f3286d1 781 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
1e6feb95
VZ
782 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
783 GetRect() );
f6bcfd97 784 }
c801d85f 785 }
a58a12e9 786
7c74e7fe 787#ifdef __WXMAC__
3d2b9c20 788 m_targetWindow->MacUpdateImmediately();
7c74e7fe 789#endif
aeb313f3 790
c801d85f
KB
791}
792
1e6feb95 793void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 794{
139adb6a
RR
795 m_xScrollingEnabled = x_scroll;
796 m_yScrollingEnabled = y_scroll;
c801d85f
KB
797}
798
1e6feb95 799void wxScrollHelper::GetVirtualSize (int *x, int *y) const
c801d85f 800{
a0bc2c1d
VZ
801 if ( x )
802 *x = m_xScrollPixelsPerLine * m_xScrollLines;
803 if ( y )
804 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
805}
806
807// Where the current view starts from
1e6feb95 808void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 809{
a0bc2c1d
VZ
810 if ( x )
811 *x = m_xScrollPosition;
812 if ( y )
813 *y = m_yScrollPosition;
c801d85f
KB
814}
815
1e6feb95 816void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 817{
a0bc2c1d
VZ
818 if ( xx )
819 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
820 if ( yy )
821 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
822}
823
1e6feb95 824void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 825{
a0bc2c1d
VZ
826 if ( xx )
827 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
828 if ( yy )
829 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 830}
d80cd92a
VZ
831
832// ----------------------------------------------------------------------------
833// event handlers
834// ----------------------------------------------------------------------------
835
836// Default OnSize resets scrollbars, if any
1e6feb95 837void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a
VZ
838{
839#if wxUSE_CONSTRAINTS
5f3286d1
VZ
840 if ( m_win->GetAutoLayout() )
841 m_win->Layout();
d80cd92a
VZ
842#endif
843
844 AdjustScrollbars();
845}
846
847// This calls OnDraw, having adjusted the origin according to the current
848// scroll position
1e6feb95 849void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 850{
af4088f1
VZ
851 // don't use m_targetWindow here, this is always called for ourselves
852 wxPaintDC dc(m_win);
1e6feb95 853 DoPrepareDC(dc);
d80cd92a
VZ
854
855 OnDraw(dc);
856}
857
438e3558
VZ
858// kbd handling: notice that we use OnChar() and not OnKeyDown() for
859// compatibility here - if we used OnKeyDown(), the programs which process
860// arrows themselves in their OnChar() would never get the message and like
861// this they always have the priority
1e6feb95 862void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
863{
864 int stx, sty, // view origin
865 szx, szy, // view size (total)
866 clix, cliy; // view size (on screen)
867
1e6feb95
VZ
868 GetViewStart(&stx, &sty);
869 GetTargetSize(&clix, &cliy);
d80cd92a 870 GetVirtualSize(&szx, &szy);
56dade3c
RL
871
872 if( m_xScrollPixelsPerLine )
873 {
874 clix /= m_xScrollPixelsPerLine;
d7da9756 875 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
876 }
877 else
878 {
879 clix = 0;
d7da9756 880 szx = -1;
56dade3c
RL
881 }
882 if( m_yScrollPixelsPerLine )
883 {
884 cliy /= m_yScrollPixelsPerLine;
885 szy /= m_yScrollPixelsPerLine;
886 }
887 else
888 {
889 cliy = 0;
d7da9756 890 szy = -1;
56dade3c 891 }
d80cd92a 892
39c869a6
VZ
893 int xScrollOld = m_xScrollPosition,
894 yScrollOld = m_yScrollPosition;
895
ecb01792 896 int dsty;
d80cd92a
VZ
897 switch ( event.KeyCode() )
898 {
899 case WXK_PAGEUP:
900 case WXK_PRIOR:
ecb01792
RL
901 dsty = sty - (5 * cliy / 6);
902 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
903 break;
904
905 case WXK_PAGEDOWN:
906 case WXK_NEXT:
907 Scroll(-1, sty + (5 * cliy / 6));
908 break;
909
d80cd92a
VZ
910 case WXK_HOME:
911 Scroll(0, event.ControlDown() ? 0 : -1);
912 break;
913
914 case WXK_END:
4acd108c 915 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
916 break;
917
918 case WXK_UP:
919 Scroll(-1, sty - 1);
920 break;
921
922 case WXK_DOWN:
923 Scroll(-1, sty + 1);
924 break;
925
926 case WXK_LEFT:
927 Scroll(stx - 1, -1);
928 break;
929
930 case WXK_RIGHT:
931 Scroll(stx + 1, -1);
932 break;
933
934 default:
935 // not for us
936 event.Skip();
937 }
39c869a6
VZ
938
939 if ( m_xScrollPosition != xScrollOld )
940 {
941 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
942 wxHORIZONTAL);
943 event.SetEventObject(m_win);
944 m_win->GetEventHandler()->ProcessEvent(event);
945 }
946
947 if ( m_yScrollPosition != yScrollOld )
948 {
949 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
950 wxVERTICAL);
951 event.SetEventObject(m_win);
952 m_win->GetEventHandler()->ProcessEvent(event);
953 }
d80cd92a 954}
d2c52078 955
1e6feb95
VZ
956// ----------------------------------------------------------------------------
957// autoscroll stuff: these functions deal with sending fake scroll events when
958// a captured mouse is being held outside the window
959// ----------------------------------------------------------------------------
960
961bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
962{
963 // only send the event if the window is scrollable in this direction
964 wxWindow *win = (wxWindow *)event.GetEventObject();
965 return win->HasScrollbar(event.GetOrientation());
966}
967
968void wxScrollHelper::StopAutoScrolling()
969{
970 if ( m_timerAutoScroll )
971 {
972 delete m_timerAutoScroll;
973 m_timerAutoScroll = (wxTimer *)NULL;
974 }
975}
d2c52078 976
1e6feb95 977void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 978{
1e6feb95
VZ
979 StopAutoScrolling();
980
981 event.Skip();
982}
d2c52078 983
1e6feb95
VZ
984void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
985{
986 // don't prevent the usual processing of the event from taking place
987 event.Skip();
988
989 // when a captured mouse leave a scrolled window we start generate
990 // scrolling events to allow, for example, extending selection beyond the
991 // visible area in some controls
992 if ( wxWindow::GetCapture() == m_targetWindow )
993 {
994 // where is the mouse leaving?
995 int pos, orient;
996 wxPoint pt = event.GetPosition();
997 if ( pt.x < 0 )
998 {
999 orient = wxHORIZONTAL;
1000 pos = 0;
1001 }
1002 else if ( pt.y < 0 )
1003 {
1004 orient = wxVERTICAL;
1005 pos = 0;
1006 }
1007 else // we're lower or to the right of the window
1008 {
1009 wxSize size = m_targetWindow->GetClientSize();
1010 if ( pt.x > size.x )
1011 {
1012 orient = wxHORIZONTAL;
1013 pos = m_xScrollLines;
1014 }
1015 else if ( pt.y > size.y )
1016 {
1017 orient = wxVERTICAL;
1018 pos = m_yScrollLines;
1019 }
1020 else // this should be impossible
1021 {
1022 // but seems to happen sometimes under wxMSW - maybe it's a bug
1023 // there but for now just ignore it
1024
1025 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1026
1027 return;
1028 }
1029 }
1030
1031 // only start the auto scroll timer if the window can be scrolled in
1032 // this direction
1033 if ( !m_targetWindow->HasScrollbar(orient) )
1034 return;
1035
1036 delete m_timerAutoScroll;
1037 m_timerAutoScroll = new wxAutoScrollTimer
1038 (
1039 m_targetWindow, this,
1040 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1041 : wxEVT_SCROLLWIN_LINEDOWN,
1042 pos,
1043 orient
1044 );
1045 m_timerAutoScroll->Start(50); // FIXME: make configurable
1046 }
1047}
1048
e421922f
VZ
1049#if wxUSE_MOUSEWHEEL
1050
1e6feb95
VZ
1051void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1052{
d2c52078 1053 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1054 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1055 m_wheelRotation -= lines * event.GetWheelDelta();
1056
1e6feb95
VZ
1057 if (lines != 0)
1058 {
d2c52078 1059 lines *= event.GetLinesPerAction();
1e6feb95 1060
b6b85bdc
JS
1061 wxScrollWinEvent newEvent;
1062
ac6f6ffc 1063 newEvent.SetPosition(0);
b6b85bdc
JS
1064 newEvent.SetOrientation(wxVERTICAL);
1065 newEvent.m_eventObject = m_win;
1066 if (lines > 0)
1067 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1068 else
1069 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1070
1071 int times = abs(lines);
1072 for (; times > 0; times --)
1073 m_win->GetEventHandler()->ProcessEvent(newEvent);
1074
1075 /* Old Way */
1076 // int vsx, vsy;
1077 // GetViewStart(&vsx, &vsy);
ac6f6ffc 1078 // Scroll(-1, vsy - lines);
d2c52078
RD
1079 }
1080}
1081
e421922f
VZ
1082#endif // wxUSE_MOUSEWHEEL
1083
1e6feb95
VZ
1084// ----------------------------------------------------------------------------
1085// wxGenericScrolledWindow implementation
1086// ----------------------------------------------------------------------------
1087
1088IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1089
349efbaa
VZ
1090BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1091 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1092END_EVENT_TABLE()
1093
1e6feb95
VZ
1094bool wxGenericScrolledWindow::Create(wxWindow *parent,
1095 wxWindowID id,
1096 const wxPoint& pos,
1097 const wxSize& size,
1098 long style,
1099 const wxString& name)
1100{
1101 m_targetWindow = this;
1102
1103 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1104
1e6feb95
VZ
1105 return ok;
1106}
1107
1108wxGenericScrolledWindow::~wxGenericScrolledWindow()
1109{
1110}
d2c52078 1111
349efbaa
VZ
1112void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1113{
1114 // the user code didn't really draw the window if we got here, so set this
1115 // flag to try to call OnDraw() later
1116 m_handler->ResetDrawnFlag();
1117
1118 event.Skip();
1119}
1120
0cf5b099
VZ
1121#ifdef __WXMSW__
1122long
1123wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1124 WXWPARAM wParam,
1125 WXLPARAM lParam)
1126{
1127 long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1128
1129 // we need to process arrows ourselves for scrolling
1130 if ( nMsg == WM_GETDLGCODE )
1131 {
1132 rc |= DLGC_WANTARROWS;
1133 }
1134
1135 return rc;
1136}
1137
1138#endif // __WXMSW__
1139
1e6feb95 1140#if WXWIN_COMPATIBILITY
349efbaa 1141
1e6feb95
VZ
1142void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1143{
1144 *x_page = GetScrollPageSize(wxHORIZONTAL);
1145 *y_page = GetScrollPageSize(wxVERTICAL);
1146}
d2c52078 1147
1e6feb95
VZ
1148void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1149{
1150 if ( xx )
1151 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1152 if ( yy )
1153 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1154}
349efbaa 1155
1e6feb95 1156#endif // WXWIN_COMPATIBILITY
d2c52078 1157
3a8c693a
VZ
1158#endif // !wxGTK
1159