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