]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
Changed to use current wxSocket flag names instead of old ones
[wxWidgets.git] / src / generic / scrolwin.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
d80cd92a 2// Name: generic/scrolwin.cpp
c801d85f
KB
3// Purpose: wxScrolledWindow implementation
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
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
c801d85f 27#ifdef __BORLANDC__
d80cd92a 28 #pragma hdrstop
c801d85f
KB
29#endif
30
d80cd92a
VZ
31#include "wx/utils.h"
32#include "wx/dcclient.h"
33
c801d85f 34#include "wx/generic/scrolwin.h"
053f9cc1 35#include "wx/panel.h"
c801d85f 36
48d1144b 37#ifdef __WXMSW__
d80cd92a 38 #include "windows.h"
48d1144b
JS
39#endif
40
a91b47e8
JS
41#ifdef __WXMOTIF__
42// For wxRETAINED implementation
338dd992
JJ
43#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
44 //This code switches off the compiler warnings
45# pragma message disable nosimpint
46#endif
a91b47e8 47#include <Xm/Xm.h>
338dd992
JJ
48#ifdef __VMS__
49# pragma message enable nosimpint
50#endif
a91b47e8
JS
51#endif
52
d80cd92a
VZ
53// ----------------------------------------------------------------------------
54// event tables
55// ----------------------------------------------------------------------------
56
57BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
58 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
59 EVT_SIZE(wxScrolledWindow::OnSize)
60 EVT_PAINT(wxScrolledWindow::OnPaint)
438e3558 61 EVT_CHAR(wxScrolledWindow::OnChar)
d80cd92a
VZ
62END_EVENT_TABLE()
63
64IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
65
66// ============================================================================
67// implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// wxScrolledWindow creation
72// ----------------------------------------------------------------------------
73
c5b42c87 74wxScrolledWindow::wxScrolledWindow()
c801d85f 75{
139adb6a
RR
76 m_xScrollPixelsPerLine = 0;
77 m_yScrollPixelsPerLine = 0;
78 m_xScrollingEnabled = TRUE;
79 m_yScrollingEnabled = TRUE;
80 m_xScrollPosition = 0;
81 m_yScrollPosition = 0;
82 m_xScrollLines = 0;
83 m_yScrollLines = 0;
84 m_xScrollLinesPerPage = 0;
85 m_yScrollLinesPerPage = 0;
86 m_scaleX = 1.0;
87 m_scaleY = 1.0;
8b089c5e 88 m_targetWindow = (wxWindow*) NULL;
c801d85f
KB
89}
90
d80cd92a
VZ
91bool wxScrolledWindow::Create(wxWindow *parent,
92 wxWindowID id,
93 const wxPoint& pos,
94 const wxSize& size,
95 long style,
96 const wxString& name)
c801d85f 97{
139adb6a
RR
98 m_xScrollPixelsPerLine = 0;
99 m_yScrollPixelsPerLine = 0;
100 m_xScrollingEnabled = TRUE;
101 m_yScrollingEnabled = TRUE;
102 m_xScrollPosition = 0;
103 m_yScrollPosition = 0;
104 m_xScrollLines = 0;
105 m_yScrollLines = 0;
106 m_xScrollLinesPerPage = 0;
107 m_yScrollLinesPerPage = 0;
108 m_scaleX = 1.0;
109 m_scaleY = 1.0;
a58a12e9 110
ecab4dba 111 m_targetWindow = this;
139adb6a 112
a58a12e9
VZ
113 // we need wxWANTS_CHARS to process arrows ourselves
114 return wxPanel::Create(parent, id, pos, size, style | wxWANTS_CHARS, name);
c801d85f
KB
115}
116
d80cd92a
VZ
117wxScrolledWindow::~wxScrolledWindow()
118{
119}
120
121// ----------------------------------------------------------------------------
122// setting scrolling parameters
123// ----------------------------------------------------------------------------
124
c801d85f
KB
125/*
126 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
127 * noUnitsX/noUnitsY: : no. units per scrollbar
128 */
debe6624 129void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
d80cd92a
VZ
130 int noUnitsX, int noUnitsY,
131 int xPos, int yPos, bool noRefresh )
c801d85f 132{
b0486e0d
SN
133 int xpos, ypos;
134
135 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
136 bool do_refresh =
137 (
c801d85f 138 (noUnitsX != 0 && m_xScrollLines == 0) ||
b0486e0d
SN
139 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
140
c801d85f 141 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 142 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 143 (xPos != m_xScrollPosition) ||
b0486e0d
SN
144 (yPos != m_yScrollPosition)
145// (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
146// (pixelsPerUnitY != m_yScrollPixelsPerLine)
139adb6a 147 );
a58a12e9 148
139adb6a
RR
149 m_xScrollPixelsPerLine = pixelsPerUnitX;
150 m_yScrollPixelsPerLine = pixelsPerUnitY;
151 m_xScrollPosition = xPos;
152 m_yScrollPosition = yPos;
153 m_xScrollLines = noUnitsX;
154 m_yScrollLines = noUnitsY;
a91b47e8
JS
155
156#ifdef __WXMOTIF__
157 // Sorry, some Motif-specific code to implement a backing pixmap
158 // for the wxRETAINED style. Implementing a backing store can't
159 // be entirely generic because it relies on the wxWindowDC implementation
160 // to duplicate X drawing calls for the backing pixmap.
161
162 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
163 {
164 Display* dpy = XtDisplay((Widget) GetMainWidget());
165
166 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
167 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
168 if (m_backingPixmap &&
169 !((m_pixmapWidth == totalPixelWidth) &&
170 (m_pixmapHeight == totalPixelHeight)))
171 {
172 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
173 m_backingPixmap = (WXPixmap) 0;
174 }
175
176 if (!m_backingPixmap &&
177 (noUnitsX != 0) && (noUnitsY != 0))
178 {
179 int depth = wxDisplayDepth();
180 m_pixmapWidth = totalPixelWidth;
181 m_pixmapHeight = totalPixelHeight;
182 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
183 m_pixmapWidth, m_pixmapHeight, depth);
d80cd92a 184 }
a91b47e8
JS
185
186 }
d80cd92a 187#endif // Motif
a58a12e9 188
139adb6a 189 AdjustScrollbars();
a58a12e9
VZ
190
191 if (do_refresh && !noRefresh)
192 m_targetWindow->Refresh();
193
2049ba38 194#ifdef __WXMSW__
d9c09c79
GRG
195 // GRG: if this turns out to be really necessary, we could
196 // at least move it to the above if { ... } so that it is
197 // only done if noRefresh = FALSE (the default). OTOH, if
198 // this doesn't break anything, which seems to be the
199 // case, we could just leave it out.
200
60fe7303
JS
201 // Necessary?
202 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
7c74e7fe
SC
203#endif
204#ifdef __WXMAC__
d80cd92a 205 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
206#endif
207}
208
d80cd92a
VZ
209// ----------------------------------------------------------------------------
210// target window handling
211// ----------------------------------------------------------------------------
ecab4dba
RR
212
213void wxScrolledWindow::SetTargetWindow( wxWindow *target )
214{
215 wxASSERT_MSG( target, wxT("target window must not be NULL") );
216 m_targetWindow = target;
217}
218
219wxWindow *wxScrolledWindow::GetTargetWindow()
220{
221 return m_targetWindow;
222}
223
d80cd92a
VZ
224// ----------------------------------------------------------------------------
225// scrolling implementation itself
226// ----------------------------------------------------------------------------
227
c5b42c87 228void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 229{
139adb6a 230 int orient = event.GetOrientation();
c801d85f 231
139adb6a
RR
232 int nScrollInc = CalcScrollInc(event);
233 if (nScrollInc == 0) return;
c801d85f 234
139adb6a
RR
235 if (orient == wxHORIZONTAL)
236 {
237 int newPos = m_xScrollPosition + nScrollInc;
238 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
239 }
240 else
241 {
242 int newPos = m_yScrollPosition + nScrollInc;
243 SetScrollPos(wxVERTICAL, newPos, TRUE );
244 }
c801d85f 245
139adb6a
RR
246 if (orient == wxHORIZONTAL)
247 {
248 m_xScrollPosition += nScrollInc;
249 }
c801d85f 250 else
139adb6a
RR
251 {
252 m_yScrollPosition += nScrollInc;
253 }
a58a12e9 254
139adb6a
RR
255 if (orient == wxHORIZONTAL)
256 {
257 if (m_xScrollingEnabled)
ecab4dba 258 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
139adb6a 259 else
ecab4dba 260 m_targetWindow->Refresh();
139adb6a 261 }
c801d85f 262 else
139adb6a
RR
263 {
264 if (m_yScrollingEnabled)
ecab4dba 265 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
139adb6a 266 else
ecab4dba 267 m_targetWindow->Refresh();
3d2b9c20 268 }
7c74e7fe 269#ifdef __WXMAC__
d80cd92a 270 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 271#endif
c801d85f
KB
272}
273
c5b42c87 274int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 275{
ecab4dba
RR
276 int pos = event.GetPosition();
277 int orient = event.GetOrientation();
c801d85f 278
ecab4dba
RR
279 int nScrollInc = 0;
280 switch (event.GetEventType())
c801d85f 281 {
ecab4dba
RR
282 case wxEVT_SCROLLWIN_TOP:
283 {
284 if (orient == wxHORIZONTAL)
285 nScrollInc = - m_xScrollPosition;
286 else
287 nScrollInc = - m_yScrollPosition;
288 break;
289 }
290 case wxEVT_SCROLLWIN_BOTTOM:
291 {
292 if (orient == wxHORIZONTAL)
293 nScrollInc = m_xScrollLines - m_xScrollPosition;
294 else
295 nScrollInc = m_yScrollLines - m_yScrollPosition;
296 break;
297 }
298 case wxEVT_SCROLLWIN_LINEUP:
299 {
300 nScrollInc = -1;
301 break;
302 }
303 case wxEVT_SCROLLWIN_LINEDOWN:
304 {
305 nScrollInc = 1;
306 break;
307 }
308 case wxEVT_SCROLLWIN_PAGEUP:
309 {
310 if (orient == wxHORIZONTAL)
311 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
312 else
313 nScrollInc = -GetScrollPageSize(wxVERTICAL);
314 break;
315 }
316 case wxEVT_SCROLLWIN_PAGEDOWN:
317 {
318 if (orient == wxHORIZONTAL)
319 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
320 else
321 nScrollInc = GetScrollPageSize(wxVERTICAL);
322 break;
323 }
324 case wxEVT_SCROLLWIN_THUMBTRACK:
530a7383 325 case wxEVT_SCROLLWIN_THUMBRELEASE:
ecab4dba
RR
326 {
327 if (orient == wxHORIZONTAL)
328 nScrollInc = pos - m_xScrollPosition;
329 else
330 nScrollInc = pos - m_yScrollPosition;
331 break;
332 }
333 default:
334 {
335 break;
336 }
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.
27d029c7 384void wxScrolledWindow::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
c801d85f
KB
453// Override this function if you don't want to have wxScrolledWindow
454// automatically change the origin according to the scroll position.
455void wxScrolledWindow::PrepareDC(wxDC& dc)
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
463void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
464{
465 *x_page = GetScrollPageSize(wxHORIZONTAL);
466 *y_page = GetScrollPageSize(wxVERTICAL);
467}
a0bc2c1d
VZ
468
469void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
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
KB
477
478void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
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
486int wxScrolledWindow::GetScrollPageSize(int orient) const
487{
488 if ( orient == wxHORIZONTAL )
489 return m_xScrollLinesPerPage;
490 else
491 return m_yScrollLinesPerPage;
492}
493
494void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
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 */
139adb6a 505void wxScrolledWindow::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
139adb6a 516 if (x_pos != -1)
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
3d2b9c20 531 if (old_x == m_xScrollPosition) return;
a58a12e9 532
3d2b9c20
RR
533 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
534 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
c801d85f 535 }
139adb6a 536 if (y_pos != -1)
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
RR
550
551 if (old_y == m_yScrollPosition) return;
a58a12e9 552
ecab4dba 553 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
d80cd92a 554 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
c801d85f 555 }
a58a12e9 556
7c74e7fe 557#ifdef __WXMAC__
3d2b9c20 558 m_targetWindow->MacUpdateImmediately();
7c74e7fe 559#endif
c801d85f
KB
560}
561
debe6624 562void wxScrolledWindow::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
568void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
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
cf3da716 577void wxScrolledWindow::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
debe6624 585void wxScrolledWindow::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
a0bc2c1d 593void wxScrolledWindow::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
606void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
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
618void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
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
630void wxScrolledWindow::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;
643 szx /= m_xScrollPixelsPerLine;
644 }
645 else
646 {
647 clix = 0;
648 szx = -1;
649 }
650 if( m_yScrollPixelsPerLine )
651 {
652 cliy /= m_yScrollPixelsPerLine;
653 szy /= m_yScrollPixelsPerLine;
654 }
655 else
656 {
657 cliy = 0;
658 szy = -1;
659 }
d80cd92a
VZ
660
661 switch ( event.KeyCode() )
662 {
663 case WXK_PAGEUP:
664 case WXK_PRIOR:
665 Scroll(-1, sty - (5 * cliy / 6));
666 break;
667
668 case WXK_PAGEDOWN:
669 case WXK_NEXT:
670 Scroll(-1, sty + (5 * cliy / 6));
671 break;
672
d80cd92a
VZ
673 case WXK_HOME:
674 Scroll(0, event.ControlDown() ? 0 : -1);
675 break;
676
677 case WXK_END:
4acd108c 678 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
679 break;
680
681 case WXK_UP:
682 Scroll(-1, sty - 1);
683 break;
684
685 case WXK_DOWN:
686 Scroll(-1, sty + 1);
687 break;
688
689 case WXK_LEFT:
690 Scroll(stx - 1, -1);
691 break;
692
693 case WXK_RIGHT:
694 Scroll(stx + 1, -1);
695 break;
696
697 default:
698 // not for us
699 event.Skip();
700 }
701}