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