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