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