]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
corrected event handling according to current msw/gtk implementation
[wxWidgets.git] / src / generic / scrolwin.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
d80cd92a 2// Name: generic/scrolwin.cpp
fa3541bd 3// Purpose: wxGenericScrolledWindow implementation
c801d85f
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
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__
d80cd92a 21 #pragma implementation "scrolwin.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
d80cd92a
VZ
35#include "wx/utils.h"
36#include "wx/dcclient.h"
37
c801d85f 38#include "wx/generic/scrolwin.h"
053f9cc1 39#include "wx/panel.h"
c801d85f 40
48d1144b 41#ifdef __WXMSW__
d80cd92a 42 #include "windows.h"
48d1144b
JS
43#endif
44
a91b47e8
JS
45#ifdef __WXMOTIF__
46// For wxRETAINED implementation
338dd992
JJ
47#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
48 //This code switches off the compiler warnings
49# pragma message disable nosimpint
50#endif
a91b47e8 51#include <Xm/Xm.h>
338dd992
JJ
52#ifdef __VMS__
53# pragma message enable nosimpint
54#endif
a91b47e8
JS
55#endif
56
fa3541bd
JS
57#ifndef __WXGTK__
58IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
59#endif
60
d80cd92a
VZ
61// ----------------------------------------------------------------------------
62// event tables
63// ----------------------------------------------------------------------------
64
fa3541bd
JS
65BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
66 EVT_SCROLLWIN(wxGenericScrolledWindow::OnScroll)
67 EVT_SIZE(wxGenericScrolledWindow::OnSize)
68 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
69 EVT_CHAR(wxGenericScrolledWindow::OnChar)
d80cd92a
VZ
70END_EVENT_TABLE()
71
fa3541bd 72IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
d80cd92a
VZ
73
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
fa3541bd 79// wxGenericScrolledWindow creation
d80cd92a
VZ
80// ----------------------------------------------------------------------------
81
fa3541bd 82wxGenericScrolledWindow::wxGenericScrolledWindow()
c801d85f 83{
139adb6a
RR
84 m_xScrollPixelsPerLine = 0;
85 m_yScrollPixelsPerLine = 0;
86 m_xScrollingEnabled = TRUE;
87 m_yScrollingEnabled = TRUE;
88 m_xScrollPosition = 0;
89 m_yScrollPosition = 0;
90 m_xScrollLines = 0;
91 m_yScrollLines = 0;
92 m_xScrollLinesPerPage = 0;
93 m_yScrollLinesPerPage = 0;
94 m_scaleX = 1.0;
95 m_scaleY = 1.0;
8b089c5e 96 m_targetWindow = (wxWindow*) NULL;
c801d85f
KB
97}
98
fa3541bd 99bool wxGenericScrolledWindow::Create(wxWindow *parent,
d80cd92a
VZ
100 wxWindowID id,
101 const wxPoint& pos,
102 const wxSize& size,
103 long style,
104 const wxString& name)
c801d85f 105{
139adb6a
RR
106 m_xScrollPixelsPerLine = 0;
107 m_yScrollPixelsPerLine = 0;
108 m_xScrollingEnabled = TRUE;
109 m_yScrollingEnabled = TRUE;
110 m_xScrollPosition = 0;
111 m_yScrollPosition = 0;
112 m_xScrollLines = 0;
113 m_yScrollLines = 0;
114 m_xScrollLinesPerPage = 0;
115 m_yScrollLinesPerPage = 0;
116 m_scaleX = 1.0;
117 m_scaleY = 1.0;
a58a12e9 118
ecab4dba 119 m_targetWindow = this;
139adb6a 120
d7da9756
VZ
121 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
122
123#ifdef __WXMSW__
124 // we need to process arrows ourselves for scrolling
125 m_lDlgCode |= DLGC_WANTARROWS;
126#endif // __WXMSW__
127
128 return ok;
c801d85f
KB
129}
130
fa3541bd 131wxGenericScrolledWindow::~wxGenericScrolledWindow()
d80cd92a
VZ
132{
133}
134
135// ----------------------------------------------------------------------------
136// setting scrolling parameters
137// ----------------------------------------------------------------------------
138
c801d85f
KB
139/*
140 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
141 * noUnitsX/noUnitsY: : no. units per scrollbar
142 */
fa3541bd 143void wxGenericScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
d80cd92a
VZ
144 int noUnitsX, int noUnitsY,
145 int xPos, int yPos, bool noRefresh )
c801d85f 146{
b0486e0d
SN
147 int xpos, ypos;
148
149 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
150 bool do_refresh =
151 (
c801d85f 152 (noUnitsX != 0 && m_xScrollLines == 0) ||
b0486e0d
SN
153 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
154
c801d85f 155 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 156 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 157 (xPos != m_xScrollPosition) ||
b0486e0d
SN
158 (yPos != m_yScrollPosition)
159// (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
160// (pixelsPerUnitY != m_yScrollPixelsPerLine)
139adb6a 161 );
a58a12e9 162
139adb6a
RR
163 m_xScrollPixelsPerLine = pixelsPerUnitX;
164 m_yScrollPixelsPerLine = pixelsPerUnitY;
165 m_xScrollPosition = xPos;
166 m_yScrollPosition = yPos;
167 m_xScrollLines = noUnitsX;
168 m_yScrollLines = noUnitsY;
a91b47e8
JS
169
170#ifdef __WXMOTIF__
171 // Sorry, some Motif-specific code to implement a backing pixmap
172 // for the wxRETAINED style. Implementing a backing store can't
173 // be entirely generic because it relies on the wxWindowDC implementation
174 // to duplicate X drawing calls for the backing pixmap.
175
176 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
177 {
178 Display* dpy = XtDisplay((Widget) GetMainWidget());
179
180 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
181 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
182 if (m_backingPixmap &&
183 !((m_pixmapWidth == totalPixelWidth) &&
184 (m_pixmapHeight == totalPixelHeight)))
185 {
186 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
187 m_backingPixmap = (WXPixmap) 0;
188 }
189
190 if (!m_backingPixmap &&
191 (noUnitsX != 0) && (noUnitsY != 0))
192 {
193 int depth = wxDisplayDepth();
194 m_pixmapWidth = totalPixelWidth;
195 m_pixmapHeight = totalPixelHeight;
196 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
197 m_pixmapWidth, m_pixmapHeight, depth);
d80cd92a 198 }
a91b47e8
JS
199
200 }
d80cd92a 201#endif // Motif
a58a12e9 202
139adb6a 203 AdjustScrollbars();
a58a12e9
VZ
204
205 if (do_refresh && !noRefresh)
206 m_targetWindow->Refresh();
207
2049ba38 208#ifdef __WXMSW__
d9c09c79
GRG
209 // GRG: if this turns out to be really necessary, we could
210 // at least move it to the above if { ... } so that it is
211 // only done if noRefresh = FALSE (the default). OTOH, if
212 // this doesn't break anything, which seems to be the
213 // case, we could just leave it out.
214
60fe7303
JS
215 // Necessary?
216 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
7c74e7fe
SC
217#endif
218#ifdef __WXMAC__
d80cd92a 219 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
220#endif
221}
222
d80cd92a
VZ
223// ----------------------------------------------------------------------------
224// target window handling
225// ----------------------------------------------------------------------------
ecab4dba 226
fa3541bd 227void wxGenericScrolledWindow::SetTargetWindow( wxWindow *target )
ecab4dba
RR
228{
229 wxASSERT_MSG( target, wxT("target window must not be NULL") );
230 m_targetWindow = target;
231}
232
fa3541bd 233wxWindow *wxGenericScrolledWindow::GetTargetWindow()
ecab4dba
RR
234{
235 return m_targetWindow;
236}
237
d80cd92a
VZ
238// ----------------------------------------------------------------------------
239// scrolling implementation itself
240// ----------------------------------------------------------------------------
241
fa3541bd 242void wxGenericScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 243{
139adb6a 244 int orient = event.GetOrientation();
c801d85f 245
139adb6a
RR
246 int nScrollInc = CalcScrollInc(event);
247 if (nScrollInc == 0) return;
c801d85f 248
139adb6a
RR
249 if (orient == wxHORIZONTAL)
250 {
251 int newPos = m_xScrollPosition + nScrollInc;
252 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
253 }
254 else
255 {
256 int newPos = m_yScrollPosition + nScrollInc;
257 SetScrollPos(wxVERTICAL, newPos, TRUE );
258 }
c801d85f 259
139adb6a
RR
260 if (orient == wxHORIZONTAL)
261 {
262 m_xScrollPosition += nScrollInc;
263 }
c801d85f 264 else
139adb6a
RR
265 {
266 m_yScrollPosition += nScrollInc;
267 }
a58a12e9 268
139adb6a
RR
269 if (orient == wxHORIZONTAL)
270 {
271 if (m_xScrollingEnabled)
ecab4dba 272 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
139adb6a 273 else
ecab4dba 274 m_targetWindow->Refresh();
139adb6a 275 }
c801d85f 276 else
139adb6a
RR
277 {
278 if (m_yScrollingEnabled)
ecab4dba 279 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
139adb6a 280 else
ecab4dba 281 m_targetWindow->Refresh();
3d2b9c20 282 }
7c74e7fe 283#ifdef __WXMAC__
d80cd92a 284 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 285#endif
c801d85f
KB
286}
287
fa3541bd 288int wxGenericScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 289{
ecab4dba
RR
290 int pos = event.GetPosition();
291 int orient = event.GetOrientation();
c801d85f 292
ecab4dba 293 int nScrollInc = 0;
1a8caf94 294 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 295 {
ecab4dba
RR
296 if (orient == wxHORIZONTAL)
297 nScrollInc = - m_xScrollPosition;
298 else
299 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
300 } else
301 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
302 {
ecab4dba
RR
303 if (orient == wxHORIZONTAL)
304 nScrollInc = m_xScrollLines - m_xScrollPosition;
305 else
306 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
307 } else
308 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
309 {
ecab4dba 310 nScrollInc = -1;
1a8caf94
RR
311 } else
312 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
313 {
ecab4dba 314 nScrollInc = 1;
1a8caf94
RR
315 } else
316 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
317 {
ecab4dba
RR
318 if (orient == wxHORIZONTAL)
319 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
320 else
321 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
322 } else
323 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
324 {
ecab4dba
RR
325 if (orient == wxHORIZONTAL)
326 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
327 else
328 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
329 } else
330 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
331 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
332 {
ecab4dba
RR
333 if (orient == wxHORIZONTAL)
334 nScrollInc = pos - m_xScrollPosition;
335 else
336 nScrollInc = pos - m_yScrollPosition;
c801d85f 337 }
88150e60 338
ecab4dba
RR
339 if (orient == wxHORIZONTAL)
340 {
a58a12e9 341 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
342 {
343 int w, h;
344 m_targetWindow->GetClientSize(&w, &h);
345
346 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
347 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
348 if (noPositions < 0)
d80cd92a 349 noPositions = 0;
ecab4dba
RR
350
351 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 352 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 353 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 354 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
355 }
356 else
357 m_targetWindow->Refresh();
9d9355c6
VZ
358 }
359 else
ecab4dba 360 {
a58a12e9 361 if (m_yScrollPixelsPerLine > 0)
d80cd92a 362 {
ecab4dba
RR
363 int w, h;
364 m_targetWindow->GetClientSize(&w, &h);
a58a12e9 365
ecab4dba
RR
366 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
367 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
368 if (noPositions < 0)
d80cd92a 369 noPositions = 0;
a58a12e9 370
ecab4dba 371 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 372 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 373 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 374 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
375 }
376 else
377 m_targetWindow->Refresh();
9d9355c6 378 }
9d9355c6 379
ecab4dba 380 return nScrollInc;
c801d85f
KB
381}
382
383// Adjust the scrollbars - new version.
fa3541bd 384void wxGenericScrolledWindow::AdjustScrollbars()
c801d85f 385{
139adb6a 386 int w, h;
ecab4dba 387 m_targetWindow->GetClientSize(&w, &h);
a58a12e9 388
27d029c7
RR
389 int oldXScroll = m_xScrollPosition;
390 int oldYScroll = m_yScrollPosition;
c801d85f 391
139adb6a
RR
392 if (m_xScrollLines > 0)
393 {
3d2b9c20
RR
394 // Calculate page size i.e. number of scroll units you get on the
395 // current client window
ecab4dba 396 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 397 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 398
139adb6a 399 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
400 // the visible portion of it or if below zero
401 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 402 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 403
139adb6a 404 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
405 // The amount by which we scroll when paging
406 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
407 }
408 else
a58a12e9 409 {
139adb6a 410 m_xScrollPosition = 0;
a58a12e9 411 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 412 }
a58a12e9 413
139adb6a
RR
414 if (m_yScrollLines > 0)
415 {
3d2b9c20
RR
416 // Calculate page size i.e. number of scroll units you get on the
417 // current client window
ecab4dba 418 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 419 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 420
139adb6a 421 // Correct position if greater than extent of canvas minus
3d2b9c20 422 // the visible portion of it or if below zero
139adb6a
RR
423 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
424 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
425
426 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
427 // The amount by which we scroll when paging
428 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
429 }
430 else
431 {
432 m_yScrollPosition = 0;
a58a12e9 433 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 434 }
a58a12e9 435
27d029c7
RR
436 if (oldXScroll != m_xScrollPosition)
437 {
438 if (m_xScrollingEnabled)
ecab4dba 439 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
27d029c7 440 else
ecab4dba 441 m_targetWindow->Refresh();
27d029c7 442 }
a58a12e9 443
27d029c7
RR
444 if (oldYScroll != m_yScrollPosition)
445 {
446 if (m_yScrollingEnabled)
ecab4dba 447 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
27d029c7 448 else
ecab4dba 449 m_targetWindow->Refresh();
27d029c7 450 }
c801d85f
KB
451}
452
fa3541bd 453// Override this function if you don't want to have wxGenericScrolledWindow
c801d85f 454// automatically change the origin according to the scroll position.
fa3541bd 455void wxGenericScrolledWindow::PrepareDC(wxDC& dc)
c801d85f 456{
a58a12e9 457 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
139adb6a
RR
458 -m_yScrollPosition * m_yScrollPixelsPerLine );
459 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
460}
461
462#if WXWIN_COMPATIBILITY
fa3541bd 463void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
c801d85f
KB
464{
465 *x_page = GetScrollPageSize(wxHORIZONTAL);
466 *y_page = GetScrollPageSize(wxVERTICAL);
467}
a0bc2c1d 468
fa3541bd 469void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
a0bc2c1d
VZ
470{
471 if ( xx )
472 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
473 if ( yy )
474 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
475}
476#endif // WXWIN_COMPATIBILITY
c801d85f 477
fa3541bd 478void wxGenericScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 479{
a0bc2c1d
VZ
480 if ( x_unit )
481 *x_unit = m_xScrollPixelsPerLine;
482 if ( y_unit )
483 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
484}
485
fa3541bd 486int wxGenericScrolledWindow::GetScrollPageSize(int orient) const
c801d85f
KB
487{
488 if ( orient == wxHORIZONTAL )
489 return m_xScrollLinesPerPage;
490 else
491 return m_yScrollLinesPerPage;
492}
493
fa3541bd 494void wxGenericScrolledWindow::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
495{
496 if ( orient == wxHORIZONTAL )
497 m_xScrollLinesPerPage = pageSize;
498 else
499 m_yScrollLinesPerPage = pageSize;
500}
501
502/*
503 * Scroll to given position (scroll position, not pixel position)
504 */
fa3541bd 505void wxGenericScrolledWindow::Scroll( int x_pos, int y_pos )
c801d85f 506{
8b089c5e
JS
507 if (!m_targetWindow)
508 return;
509
a58a12e9 510 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 511 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 512
139adb6a 513 int w, h;
ecab4dba 514 m_targetWindow->GetClientSize(&w, &h);
c801d85f 515
8775b357 516 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 517 {
ed673c6a 518 int old_x = m_xScrollPosition;
139adb6a 519 m_xScrollPosition = x_pos;
a58a12e9 520
3d2b9c20
RR
521 // Calculate page size i.e. number of scroll units you get on the
522 // current client window
ecab4dba 523 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
524 if (noPagePositions < 1) noPagePositions = 1;
525
526 // Correct position if greater than extent of canvas minus
3d2b9c20 527 // the visible portion of it or if below zero
139adb6a
RR
528 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
529 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 530
f6bcfd97
BP
531 if (old_x != m_xScrollPosition) {
532 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
533 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
534 }
c801d85f 535 }
8775b357 536 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 537 {
ed673c6a 538 int old_y = m_yScrollPosition;
139adb6a 539 m_yScrollPosition = y_pos;
a58a12e9 540
3d2b9c20
RR
541 // Calculate page size i.e. number of scroll units you get on the
542 // current client window
ecab4dba 543 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
544 if (noPagePositions < 1) noPagePositions = 1;
545
546 // Correct position if greater than extent of canvas minus
3d2b9c20 547 // the visible portion of it or if below zero
139adb6a
RR
548 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
549 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
3d2b9c20 550
f6bcfd97
BP
551 if (old_y != m_yScrollPosition) {
552 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
553 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
554 }
c801d85f 555 }
a58a12e9 556
7c74e7fe 557#ifdef __WXMAC__
3d2b9c20 558 m_targetWindow->MacUpdateImmediately();
7c74e7fe 559#endif
c801d85f
KB
560}
561
fa3541bd 562void wxGenericScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 563{
139adb6a
RR
564 m_xScrollingEnabled = x_scroll;
565 m_yScrollingEnabled = y_scroll;
c801d85f
KB
566}
567
fa3541bd 568void wxGenericScrolledWindow::GetVirtualSize (int *x, int *y) const
c801d85f 569{
a0bc2c1d
VZ
570 if ( x )
571 *x = m_xScrollPixelsPerLine * m_xScrollLines;
572 if ( y )
573 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
574}
575
576// Where the current view starts from
fa3541bd 577void wxGenericScrolledWindow::GetViewStart (int *x, int *y) const
c801d85f 578{
a0bc2c1d
VZ
579 if ( x )
580 *x = m_xScrollPosition;
581 if ( y )
582 *y = m_yScrollPosition;
c801d85f
KB
583}
584
fa3541bd 585void wxGenericScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 586{
a0bc2c1d
VZ
587 if ( xx )
588 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
589 if ( yy )
590 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
591}
592
fa3541bd 593void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 594{
a0bc2c1d
VZ
595 if ( xx )
596 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
597 if ( yy )
598 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 599}
d80cd92a
VZ
600
601// ----------------------------------------------------------------------------
602// event handlers
603// ----------------------------------------------------------------------------
604
605// Default OnSize resets scrollbars, if any
fa3541bd 606void wxGenericScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a
VZ
607{
608#if wxUSE_CONSTRAINTS
609 if (GetAutoLayout())
610 Layout();
611#endif
612
613 AdjustScrollbars();
614}
615
616// This calls OnDraw, having adjusted the origin according to the current
617// scroll position
fa3541bd 618void wxGenericScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a
VZ
619{
620 wxPaintDC dc(this);
621 PrepareDC(dc);
622
623 OnDraw(dc);
624}
625
438e3558
VZ
626// kbd handling: notice that we use OnChar() and not OnKeyDown() for
627// compatibility here - if we used OnKeyDown(), the programs which process
628// arrows themselves in their OnChar() would never get the message and like
629// this they always have the priority
fa3541bd 630void wxGenericScrolledWindow::OnChar(wxKeyEvent& event)
d80cd92a
VZ
631{
632 int stx, sty, // view origin
633 szx, szy, // view size (total)
634 clix, cliy; // view size (on screen)
635
636 ViewStart(&stx, &sty);
637 GetClientSize(&clix, &cliy);
d80cd92a 638 GetVirtualSize(&szx, &szy);
56dade3c
RL
639
640 if( m_xScrollPixelsPerLine )
641 {
642 clix /= m_xScrollPixelsPerLine;
d7da9756 643 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
644 }
645 else
646 {
647 clix = 0;
d7da9756 648 szx = -1;
56dade3c
RL
649 }
650 if( m_yScrollPixelsPerLine )
651 {
652 cliy /= m_yScrollPixelsPerLine;
653 szy /= m_yScrollPixelsPerLine;
654 }
655 else
656 {
657 cliy = 0;
d7da9756 658 szy = -1;
56dade3c 659 }
d80cd92a 660
ecb01792 661 int dsty;
d80cd92a
VZ
662 switch ( event.KeyCode() )
663 {
664 case WXK_PAGEUP:
665 case WXK_PRIOR:
ecb01792
RL
666 dsty = sty - (5 * cliy / 6);
667 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
668 break;
669
670 case WXK_PAGEDOWN:
671 case WXK_NEXT:
672 Scroll(-1, sty + (5 * cliy / 6));
673 break;
674
d80cd92a
VZ
675 case WXK_HOME:
676 Scroll(0, event.ControlDown() ? 0 : -1);
677 break;
678
679 case WXK_END:
4acd108c 680 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
681 break;
682
683 case WXK_UP:
684 Scroll(-1, sty - 1);
685 break;
686
687 case WXK_DOWN:
688 Scroll(-1, sty + 1);
689 break;
690
691 case WXK_LEFT:
692 Scroll(stx - 1, -1);
693 break;
694
695 case WXK_RIGHT:
696 Scroll(stx + 1, -1);
697 break;
698
699 default:
700 // not for us
701 event.Skip();
702 }
703}