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