]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
fixed FindOrCreatePen/Brush() for the case when the object couldn't be created succes...
[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
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
d80cd92a
VZ
57// ----------------------------------------------------------------------------
58// event tables
59// ----------------------------------------------------------------------------
60
61BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
62 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
63 EVT_SIZE(wxScrolledWindow::OnSize)
64 EVT_PAINT(wxScrolledWindow::OnPaint)
438e3558 65 EVT_CHAR(wxScrolledWindow::OnChar)
d80cd92a
VZ
66END_EVENT_TABLE()
67
68IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
69
70// ============================================================================
71// implementation
72// ============================================================================
73
74// ----------------------------------------------------------------------------
75// wxScrolledWindow creation
76// ----------------------------------------------------------------------------
77
c5b42c87 78wxScrolledWindow::wxScrolledWindow()
c801d85f 79{
139adb6a
RR
80 m_xScrollPixelsPerLine = 0;
81 m_yScrollPixelsPerLine = 0;
82 m_xScrollingEnabled = TRUE;
83 m_yScrollingEnabled = TRUE;
84 m_xScrollPosition = 0;
85 m_yScrollPosition = 0;
86 m_xScrollLines = 0;
87 m_yScrollLines = 0;
88 m_xScrollLinesPerPage = 0;
89 m_yScrollLinesPerPage = 0;
90 m_scaleX = 1.0;
91 m_scaleY = 1.0;
8b089c5e 92 m_targetWindow = (wxWindow*) NULL;
c801d85f
KB
93}
94
d80cd92a
VZ
95bool wxScrolledWindow::Create(wxWindow *parent,
96 wxWindowID id,
97 const wxPoint& pos,
98 const wxSize& size,
99 long style,
100 const wxString& name)
c801d85f 101{
139adb6a
RR
102 m_xScrollPixelsPerLine = 0;
103 m_yScrollPixelsPerLine = 0;
104 m_xScrollingEnabled = TRUE;
105 m_yScrollingEnabled = TRUE;
106 m_xScrollPosition = 0;
107 m_yScrollPosition = 0;
108 m_xScrollLines = 0;
109 m_yScrollLines = 0;
110 m_xScrollLinesPerPage = 0;
111 m_yScrollLinesPerPage = 0;
112 m_scaleX = 1.0;
113 m_scaleY = 1.0;
a58a12e9 114
ecab4dba 115 m_targetWindow = this;
139adb6a 116
d7da9756
VZ
117 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
118
119#ifdef __WXMSW__
120 // we need to process arrows ourselves for scrolling
121 m_lDlgCode |= DLGC_WANTARROWS;
122#endif // __WXMSW__
123
124 return ok;
c801d85f
KB
125}
126
d80cd92a
VZ
127wxScrolledWindow::~wxScrolledWindow()
128{
129}
130
131// ----------------------------------------------------------------------------
132// setting scrolling parameters
133// ----------------------------------------------------------------------------
134
c801d85f
KB
135/*
136 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
137 * noUnitsX/noUnitsY: : no. units per scrollbar
138 */
debe6624 139void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
d80cd92a
VZ
140 int noUnitsX, int noUnitsY,
141 int xPos, int yPos, bool noRefresh )
c801d85f 142{
b0486e0d
SN
143 int xpos, ypos;
144
145 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
146 bool do_refresh =
147 (
c801d85f 148 (noUnitsX != 0 && m_xScrollLines == 0) ||
b0486e0d
SN
149 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
150
c801d85f 151 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 152 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 153 (xPos != m_xScrollPosition) ||
b0486e0d
SN
154 (yPos != m_yScrollPosition)
155// (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
156// (pixelsPerUnitY != m_yScrollPixelsPerLine)
139adb6a 157 );
a58a12e9 158
139adb6a
RR
159 m_xScrollPixelsPerLine = pixelsPerUnitX;
160 m_yScrollPixelsPerLine = pixelsPerUnitY;
161 m_xScrollPosition = xPos;
162 m_yScrollPosition = yPos;
163 m_xScrollLines = noUnitsX;
164 m_yScrollLines = noUnitsY;
a91b47e8
JS
165
166#ifdef __WXMOTIF__
167 // Sorry, some Motif-specific code to implement a backing pixmap
168 // for the wxRETAINED style. Implementing a backing store can't
169 // be entirely generic because it relies on the wxWindowDC implementation
170 // to duplicate X drawing calls for the backing pixmap.
171
172 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
173 {
174 Display* dpy = XtDisplay((Widget) GetMainWidget());
175
176 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
177 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
178 if (m_backingPixmap &&
179 !((m_pixmapWidth == totalPixelWidth) &&
180 (m_pixmapHeight == totalPixelHeight)))
181 {
182 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
183 m_backingPixmap = (WXPixmap) 0;
184 }
185
186 if (!m_backingPixmap &&
187 (noUnitsX != 0) && (noUnitsY != 0))
188 {
189 int depth = wxDisplayDepth();
190 m_pixmapWidth = totalPixelWidth;
191 m_pixmapHeight = totalPixelHeight;
192 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
193 m_pixmapWidth, m_pixmapHeight, depth);
d80cd92a 194 }
a91b47e8
JS
195
196 }
d80cd92a 197#endif // Motif
a58a12e9 198
139adb6a 199 AdjustScrollbars();
a58a12e9
VZ
200
201 if (do_refresh && !noRefresh)
202 m_targetWindow->Refresh();
203
2049ba38 204#ifdef __WXMSW__
d9c09c79
GRG
205 // GRG: if this turns out to be really necessary, we could
206 // at least move it to the above if { ... } so that it is
207 // only done if noRefresh = FALSE (the default). OTOH, if
208 // this doesn't break anything, which seems to be the
209 // case, we could just leave it out.
210
60fe7303
JS
211 // Necessary?
212 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
7c74e7fe
SC
213#endif
214#ifdef __WXMAC__
d80cd92a 215 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
216#endif
217}
218
d80cd92a
VZ
219// ----------------------------------------------------------------------------
220// target window handling
221// ----------------------------------------------------------------------------
ecab4dba
RR
222
223void wxScrolledWindow::SetTargetWindow( wxWindow *target )
224{
225 wxASSERT_MSG( target, wxT("target window must not be NULL") );
226 m_targetWindow = target;
227}
228
229wxWindow *wxScrolledWindow::GetTargetWindow()
230{
231 return m_targetWindow;
232}
233
d80cd92a
VZ
234// ----------------------------------------------------------------------------
235// scrolling implementation itself
236// ----------------------------------------------------------------------------
237
c5b42c87 238void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 239{
139adb6a 240 int orient = event.GetOrientation();
c801d85f 241
139adb6a
RR
242 int nScrollInc = CalcScrollInc(event);
243 if (nScrollInc == 0) return;
c801d85f 244
139adb6a
RR
245 if (orient == wxHORIZONTAL)
246 {
247 int newPos = m_xScrollPosition + nScrollInc;
248 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
249 }
250 else
251 {
252 int newPos = m_yScrollPosition + nScrollInc;
253 SetScrollPos(wxVERTICAL, newPos, TRUE );
254 }
c801d85f 255
139adb6a
RR
256 if (orient == wxHORIZONTAL)
257 {
258 m_xScrollPosition += nScrollInc;
259 }
c801d85f 260 else
139adb6a
RR
261 {
262 m_yScrollPosition += nScrollInc;
263 }
a58a12e9 264
139adb6a
RR
265 if (orient == wxHORIZONTAL)
266 {
267 if (m_xScrollingEnabled)
ecab4dba 268 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
139adb6a 269 else
ecab4dba 270 m_targetWindow->Refresh();
139adb6a 271 }
c801d85f 272 else
139adb6a
RR
273 {
274 if (m_yScrollingEnabled)
ecab4dba 275 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
139adb6a 276 else
ecab4dba 277 m_targetWindow->Refresh();
3d2b9c20 278 }
7c74e7fe 279#ifdef __WXMAC__
d80cd92a 280 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 281#endif
c801d85f
KB
282}
283
c5b42c87 284int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 285{
ecab4dba
RR
286 int pos = event.GetPosition();
287 int orient = event.GetOrientation();
c801d85f 288
ecab4dba 289 int nScrollInc = 0;
1a8caf94 290 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 291 {
ecab4dba
RR
292 if (orient == wxHORIZONTAL)
293 nScrollInc = - m_xScrollPosition;
294 else
295 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
296 } else
297 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
298 {
ecab4dba
RR
299 if (orient == wxHORIZONTAL)
300 nScrollInc = m_xScrollLines - m_xScrollPosition;
301 else
302 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
303 } else
304 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
305 {
ecab4dba 306 nScrollInc = -1;
1a8caf94
RR
307 } else
308 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
309 {
ecab4dba 310 nScrollInc = 1;
1a8caf94
RR
311 } else
312 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
313 {
ecab4dba
RR
314 if (orient == wxHORIZONTAL)
315 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
316 else
317 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
318 } else
319 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
320 {
ecab4dba
RR
321 if (orient == wxHORIZONTAL)
322 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
323 else
324 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
325 } else
326 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
327 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
328 {
ecab4dba
RR
329 if (orient == wxHORIZONTAL)
330 nScrollInc = pos - m_xScrollPosition;
331 else
332 nScrollInc = pos - m_yScrollPosition;
c801d85f 333 }
88150e60 334
ecab4dba
RR
335 if (orient == wxHORIZONTAL)
336 {
a58a12e9 337 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
338 {
339 int w, h;
340 m_targetWindow->GetClientSize(&w, &h);
341
342 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
343 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
344 if (noPositions < 0)
d80cd92a 345 noPositions = 0;
ecab4dba
RR
346
347 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 348 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 349 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 350 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
351 }
352 else
353 m_targetWindow->Refresh();
9d9355c6
VZ
354 }
355 else
ecab4dba 356 {
a58a12e9 357 if (m_yScrollPixelsPerLine > 0)
d80cd92a 358 {
ecab4dba
RR
359 int w, h;
360 m_targetWindow->GetClientSize(&w, &h);
a58a12e9 361
ecab4dba
RR
362 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
363 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
364 if (noPositions < 0)
d80cd92a 365 noPositions = 0;
a58a12e9 366
ecab4dba 367 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 368 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 369 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 370 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
371 }
372 else
373 m_targetWindow->Refresh();
9d9355c6 374 }
9d9355c6 375
ecab4dba 376 return nScrollInc;
c801d85f
KB
377}
378
379// Adjust the scrollbars - new version.
27d029c7 380void wxScrolledWindow::AdjustScrollbars()
c801d85f 381{
139adb6a 382 int w, h;
ecab4dba 383 m_targetWindow->GetClientSize(&w, &h);
a58a12e9 384
27d029c7
RR
385 int oldXScroll = m_xScrollPosition;
386 int oldYScroll = m_yScrollPosition;
c801d85f 387
139adb6a
RR
388 if (m_xScrollLines > 0)
389 {
3d2b9c20
RR
390 // Calculate page size i.e. number of scroll units you get on the
391 // current client window
ecab4dba 392 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 393 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 394
139adb6a 395 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
396 // the visible portion of it or if below zero
397 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 398 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 399
139adb6a 400 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
401 // The amount by which we scroll when paging
402 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
403 }
404 else
a58a12e9 405 {
139adb6a 406 m_xScrollPosition = 0;
a58a12e9 407 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 408 }
a58a12e9 409
139adb6a
RR
410 if (m_yScrollLines > 0)
411 {
3d2b9c20
RR
412 // Calculate page size i.e. number of scroll units you get on the
413 // current client window
ecab4dba 414 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 415 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 416
139adb6a 417 // Correct position if greater than extent of canvas minus
3d2b9c20 418 // the visible portion of it or if below zero
139adb6a
RR
419 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
420 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
421
422 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
423 // The amount by which we scroll when paging
424 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
425 }
426 else
427 {
428 m_yScrollPosition = 0;
a58a12e9 429 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 430 }
a58a12e9 431
27d029c7
RR
432 if (oldXScroll != m_xScrollPosition)
433 {
434 if (m_xScrollingEnabled)
ecab4dba 435 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
27d029c7 436 else
ecab4dba 437 m_targetWindow->Refresh();
27d029c7 438 }
a58a12e9 439
27d029c7
RR
440 if (oldYScroll != m_yScrollPosition)
441 {
442 if (m_yScrollingEnabled)
ecab4dba 443 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
27d029c7 444 else
ecab4dba 445 m_targetWindow->Refresh();
27d029c7 446 }
c801d85f
KB
447}
448
c801d85f
KB
449// Override this function if you don't want to have wxScrolledWindow
450// automatically change the origin according to the scroll position.
451void wxScrolledWindow::PrepareDC(wxDC& dc)
452{
a58a12e9 453 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
139adb6a
RR
454 -m_yScrollPosition * m_yScrollPixelsPerLine );
455 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
456}
457
458#if WXWIN_COMPATIBILITY
459void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
460{
461 *x_page = GetScrollPageSize(wxHORIZONTAL);
462 *y_page = GetScrollPageSize(wxVERTICAL);
463}
a0bc2c1d
VZ
464
465void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
466{
467 if ( xx )
468 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
469 if ( yy )
470 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
471}
472#endif // WXWIN_COMPATIBILITY
c801d85f
KB
473
474void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
475{
a0bc2c1d
VZ
476 if ( x_unit )
477 *x_unit = m_xScrollPixelsPerLine;
478 if ( y_unit )
479 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
480}
481
482int wxScrolledWindow::GetScrollPageSize(int orient) const
483{
484 if ( orient == wxHORIZONTAL )
485 return m_xScrollLinesPerPage;
486 else
487 return m_yScrollLinesPerPage;
488}
489
490void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
491{
492 if ( orient == wxHORIZONTAL )
493 m_xScrollLinesPerPage = pageSize;
494 else
495 m_yScrollLinesPerPage = pageSize;
496}
497
498/*
499 * Scroll to given position (scroll position, not pixel position)
500 */
139adb6a 501void wxScrolledWindow::Scroll( int x_pos, int y_pos )
c801d85f 502{
8b089c5e
JS
503 if (!m_targetWindow)
504 return;
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
8775b357 512 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
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
f6bcfd97
BP
527 if (old_x != m_xScrollPosition) {
528 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
529 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
530 }
c801d85f 531 }
8775b357 532 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
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 546
f6bcfd97
BP
547 if (old_y != m_yScrollPosition) {
548 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
549 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
550 }
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;
d7da9756 639 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
640 }
641 else
642 {
643 clix = 0;
d7da9756 644 szx = -1;
56dade3c
RL
645 }
646 if( m_yScrollPixelsPerLine )
647 {
648 cliy /= m_yScrollPixelsPerLine;
649 szy /= m_yScrollPixelsPerLine;
650 }
651 else
652 {
653 cliy = 0;
d7da9756 654 szy = -1;
56dade3c 655 }
d80cd92a 656
ecb01792 657 int dsty;
d80cd92a
VZ
658 switch ( event.KeyCode() )
659 {
660 case WXK_PAGEUP:
661 case WXK_PRIOR:
ecb01792
RL
662 dsty = sty - (5 * cliy / 6);
663 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
664 break;
665
666 case WXK_PAGEDOWN:
667 case WXK_NEXT:
668 Scroll(-1, sty + (5 * cliy / 6));
669 break;
670
d80cd92a
VZ
671 case WXK_HOME:
672 Scroll(0, event.ControlDown() ? 0 : -1);
673 break;
674
675 case WXK_END:
4acd108c 676 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
677 break;
678
679 case WXK_UP:
680 Scroll(-1, sty - 1);
681 break;
682
683 case WXK_DOWN:
684 Scroll(-1, sty + 1);
685 break;
686
687 case WXK_LEFT:
688 Scroll(stx - 1, -1);
689 break;
690
691 case WXK_RIGHT:
692 Scroll(stx + 1, -1);
693 break;
694
695 default:
696 // not for us
697 event.Skip();
698 }
699}