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