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