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