]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / generic / scrolwin.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: scrolwin.cpp
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
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma implementation "scrolwin.h"
15#endif
16
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
20#include "wx/utils.h"
21#include "wx/dcclient.h"
22
c801d85f
KB
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#include "wx/generic/scrolwin.h"
053f9cc1 28#include "wx/panel.h"
c801d85f
KB
29
30#if !USE_SHARED_LIBRARY
053f9cc1 31BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
c5b42c87 32 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
139adb6a
RR
33 EVT_SIZE(wxScrolledWindow::OnSize)
34 EVT_PAINT(wxScrolledWindow::OnPaint)
c801d85f
KB
35END_EVENT_TABLE()
36
053f9cc1 37IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
c801d85f
KB
38#endif
39
48d1144b
JS
40#ifdef __WXMSW__
41#include "windows.h"
42#endif
43
a91b47e8
JS
44#ifdef __WXMOTIF__
45// For wxRETAINED implementation
46#include <Xm/Xm.h>
47#endif
48
c5b42c87 49wxScrolledWindow::wxScrolledWindow()
c801d85f 50{
139adb6a
RR
51 m_xScrollPixelsPerLine = 0;
52 m_yScrollPixelsPerLine = 0;
53 m_xScrollingEnabled = TRUE;
54 m_yScrollingEnabled = TRUE;
55 m_xScrollPosition = 0;
56 m_yScrollPosition = 0;
57 m_xScrollLines = 0;
58 m_yScrollLines = 0;
59 m_xScrollLinesPerPage = 0;
60 m_yScrollLinesPerPage = 0;
61 m_scaleX = 1.0;
62 m_scaleY = 1.0;
c801d85f
KB
63}
64
debe6624 65bool wxScrolledWindow::Create(wxWindow *parent, wxWindowID id,
c801d85f
KB
66 const wxPoint& pos,
67 const wxSize& size,
debe6624 68 long style,
c801d85f
KB
69 const wxString& name)
70{
139adb6a
RR
71 m_xScrollPixelsPerLine = 0;
72 m_yScrollPixelsPerLine = 0;
73 m_xScrollingEnabled = TRUE;
74 m_yScrollingEnabled = TRUE;
75 m_xScrollPosition = 0;
76 m_yScrollPosition = 0;
77 m_xScrollLines = 0;
78 m_yScrollLines = 0;
79 m_xScrollLinesPerPage = 0;
80 m_yScrollLinesPerPage = 0;
81 m_scaleX = 1.0;
82 m_scaleY = 1.0;
ecab4dba
RR
83
84 m_targetWindow = this;
139adb6a 85
053f9cc1 86 return wxPanel::Create(parent, id, pos, size, style, name);
c801d85f
KB
87}
88
89/*
90 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
91 * noUnitsX/noUnitsY: : no. units per scrollbar
92 */
debe6624
JS
93void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
94 int noUnitsX, int noUnitsY,
95 int xPos, int yPos, bool noRefresh )
c801d85f 96{
139adb6a
RR
97 bool do_refresh =
98 (
c801d85f 99 (noUnitsX != 0 && m_xScrollLines == 0) ||
ea5c6ca7 100 (noUnitsX < m_xScrollLines) ||
c801d85f 101 (noUnitsY != 0 && m_yScrollLines == 0) ||
ea5c6ca7 102 (noUnitsY < m_yScrollLines) ||
c801d85f
KB
103 (xPos != m_xScrollPosition) ||
104 (yPos != m_yScrollPosition) ||
105 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
106 (pixelsPerUnitY != m_yScrollPixelsPerLine)
139adb6a 107 );
c801d85f 108
139adb6a
RR
109 m_xScrollPixelsPerLine = pixelsPerUnitX;
110 m_yScrollPixelsPerLine = pixelsPerUnitY;
111 m_xScrollPosition = xPos;
112 m_yScrollPosition = yPos;
113 m_xScrollLines = noUnitsX;
114 m_yScrollLines = noUnitsY;
a91b47e8
JS
115
116#ifdef __WXMOTIF__
117 // Sorry, some Motif-specific code to implement a backing pixmap
118 // for the wxRETAINED style. Implementing a backing store can't
119 // be entirely generic because it relies on the wxWindowDC implementation
120 // to duplicate X drawing calls for the backing pixmap.
121
122 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
123 {
124 Display* dpy = XtDisplay((Widget) GetMainWidget());
125
126 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
127 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
128 if (m_backingPixmap &&
129 !((m_pixmapWidth == totalPixelWidth) &&
130 (m_pixmapHeight == totalPixelHeight)))
131 {
132 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
133 m_backingPixmap = (WXPixmap) 0;
134 }
135
136 if (!m_backingPixmap &&
137 (noUnitsX != 0) && (noUnitsY != 0))
138 {
139 int depth = wxDisplayDepth();
140 m_pixmapWidth = totalPixelWidth;
141 m_pixmapHeight = totalPixelHeight;
142 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
143 m_pixmapWidth, m_pixmapHeight, depth);
144 }
145
146 }
147#endif
d4c99d6f 148
139adb6a 149 AdjustScrollbars();
c801d85f 150
ea5c6ca7 151 if (do_refresh && !noRefresh)
ecab4dba 152 m_targetWindow->Refresh();
c801d85f 153
2049ba38 154#ifdef __WXMSW__
48d1144b 155 // Necessary?
7c74e7fe
SC
156 UpdateWindow ((HWND) m_targetWindow->GetHWND());
157#endif
158#ifdef __WXMAC__
159 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
160#endif
161}
162
ecab4dba
RR
163wxScrolledWindow::~wxScrolledWindow()
164{
165}
166
167void wxScrolledWindow::SetTargetWindow( wxWindow *target )
168{
169 wxASSERT_MSG( target, wxT("target window must not be NULL") );
170 m_targetWindow = target;
171}
172
173wxWindow *wxScrolledWindow::GetTargetWindow()
174{
175 return m_targetWindow;
176}
177
c5b42c87 178void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 179{
139adb6a 180 int orient = event.GetOrientation();
c801d85f 181
139adb6a
RR
182 int nScrollInc = CalcScrollInc(event);
183 if (nScrollInc == 0) return;
c801d85f 184
139adb6a
RR
185 if (orient == wxHORIZONTAL)
186 {
187 int newPos = m_xScrollPosition + nScrollInc;
188 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
189 }
190 else
191 {
192 int newPos = m_yScrollPosition + nScrollInc;
193 SetScrollPos(wxVERTICAL, newPos, TRUE );
194 }
c801d85f 195
139adb6a
RR
196 if (orient == wxHORIZONTAL)
197 {
198 m_xScrollPosition += nScrollInc;
199 }
c801d85f 200 else
139adb6a
RR
201 {
202 m_yScrollPosition += nScrollInc;
203 }
204
205 if (orient == wxHORIZONTAL)
206 {
207 if (m_xScrollingEnabled)
ecab4dba 208 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
139adb6a 209 else
ecab4dba 210 m_targetWindow->Refresh();
139adb6a 211 }
c801d85f 212 else
139adb6a
RR
213 {
214 if (m_yScrollingEnabled)
ecab4dba 215 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
139adb6a 216 else
ecab4dba 217 m_targetWindow->Refresh();
c801d85f 218 }
7c74e7fe
SC
219#ifdef __WXMAC__
220 m_targetWindow->MacUpdateImmediately() ;
221#endif
c801d85f
KB
222}
223
c5b42c87 224int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 225{
ecab4dba
RR
226 int pos = event.GetPosition();
227 int orient = event.GetOrientation();
c801d85f 228
ecab4dba
RR
229 int nScrollInc = 0;
230 switch (event.GetEventType())
c801d85f 231 {
ecab4dba
RR
232 case wxEVT_SCROLLWIN_TOP:
233 {
234 if (orient == wxHORIZONTAL)
235 nScrollInc = - m_xScrollPosition;
236 else
237 nScrollInc = - m_yScrollPosition;
238 break;
239 }
240 case wxEVT_SCROLLWIN_BOTTOM:
241 {
242 if (orient == wxHORIZONTAL)
243 nScrollInc = m_xScrollLines - m_xScrollPosition;
244 else
245 nScrollInc = m_yScrollLines - m_yScrollPosition;
246 break;
247 }
248 case wxEVT_SCROLLWIN_LINEUP:
249 {
250 nScrollInc = -1;
251 break;
252 }
253 case wxEVT_SCROLLWIN_LINEDOWN:
254 {
255 nScrollInc = 1;
256 break;
257 }
258 case wxEVT_SCROLLWIN_PAGEUP:
259 {
260 if (orient == wxHORIZONTAL)
261 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
262 else
263 nScrollInc = -GetScrollPageSize(wxVERTICAL);
264 break;
265 }
266 case wxEVT_SCROLLWIN_PAGEDOWN:
267 {
268 if (orient == wxHORIZONTAL)
269 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
270 else
271 nScrollInc = GetScrollPageSize(wxVERTICAL);
272 break;
273 }
274 case wxEVT_SCROLLWIN_THUMBTRACK:
275 {
276 if (orient == wxHORIZONTAL)
277 nScrollInc = pos - m_xScrollPosition;
278 else
279 nScrollInc = pos - m_yScrollPosition;
280 break;
281 }
282 default:
283 {
284 break;
285 }
c801d85f 286 }
88150e60 287
ecab4dba
RR
288 if (orient == wxHORIZONTAL)
289 {
290 if (m_xScrollPixelsPerLine > 0)
291 {
292 int w, h;
293 m_targetWindow->GetClientSize(&w, &h);
294
295 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
296 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
297 if (noPositions < 0)
298 noPositions = 0;
299
300 if ( (m_xScrollPosition + nScrollInc) < 0 )
301 nScrollInc = -m_xScrollPosition; // As -ve as we can go
302 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
303 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
304 }
305 else
306 m_targetWindow->Refresh();
9d9355c6
VZ
307 }
308 else
ecab4dba
RR
309 {
310 if (m_yScrollPixelsPerLine > 0)
311 {
312 int w, h;
313 m_targetWindow->GetClientSize(&w, &h);
9d9355c6 314
ecab4dba
RR
315 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
316 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
317 if (noPositions < 0)
318 noPositions = 0;
9d9355c6 319
ecab4dba
RR
320 if ( (m_yScrollPosition + nScrollInc) < 0 )
321 nScrollInc = -m_yScrollPosition; // As -ve as we can go
322 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
323 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
324 }
325 else
326 m_targetWindow->Refresh();
9d9355c6 327 }
9d9355c6 328
ecab4dba 329 return nScrollInc;
c801d85f
KB
330}
331
332// Adjust the scrollbars - new version.
27d029c7 333void wxScrolledWindow::AdjustScrollbars()
c801d85f 334{
139adb6a 335 int w, h;
ecab4dba 336 m_targetWindow->GetClientSize(&w, &h);
27d029c7
RR
337
338 int oldXScroll = m_xScrollPosition;
339 int oldYScroll = m_yScrollPosition;
c801d85f 340
139adb6a
RR
341 if (m_xScrollLines > 0)
342 {
c801d85f
KB
343 // Calculate page size i.e. number of scroll units you get on the
344 // current client window
ecab4dba 345 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 346 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 347
139adb6a
RR
348 // Correct position if greater than extent of canvas minus
349 // the visible portion of it or if below zero
350 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
351 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 352
139adb6a 353 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
354 // The amount by which we scroll when paging
355 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
356 }
357 else
358 {
359 m_xScrollPosition = 0;
360 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
361 }
362
363 if (m_yScrollLines > 0)
364 {
c801d85f
KB
365 // Calculate page size i.e. number of scroll units you get on the
366 // current client window
ecab4dba 367 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 368 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 369
139adb6a
RR
370 // Correct position if greater than extent of canvas minus
371 // the visible portion of it or if below zero
372 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
373 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
374
375 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
376 // The amount by which we scroll when paging
377 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
378 }
379 else
380 {
381 m_yScrollPosition = 0;
382 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
383 }
27d029c7
RR
384
385 if (oldXScroll != m_xScrollPosition)
386 {
387 if (m_xScrollingEnabled)
ecab4dba 388 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
27d029c7 389 else
ecab4dba 390 m_targetWindow->Refresh();
27d029c7
RR
391 }
392
393 if (oldYScroll != m_yScrollPosition)
394 {
395 if (m_yScrollingEnabled)
ecab4dba 396 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
27d029c7 397 else
ecab4dba 398 m_targetWindow->Refresh();
27d029c7 399 }
c801d85f
KB
400}
401
402// Default OnSize resets scrollbars, if any
403void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
404{
47d67540 405#if wxUSE_CONSTRAINTS
139adb6a 406 if (GetAutoLayout()) Layout();
c801d85f
KB
407#endif
408
139adb6a 409 AdjustScrollbars();
c801d85f
KB
410}
411
412// This calls OnDraw, having adjusted the origin according to the current
413// scroll position
414void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
415{
139adb6a
RR
416 wxPaintDC dc(this);
417 PrepareDC(dc);
c801d85f 418
139adb6a 419 OnDraw(dc);
c801d85f
KB
420}
421
422// Override this function if you don't want to have wxScrolledWindow
423// automatically change the origin according to the scroll position.
424void wxScrolledWindow::PrepareDC(wxDC& dc)
425{
139adb6a
RR
426 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
427 -m_yScrollPosition * m_yScrollPixelsPerLine );
428 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
429}
430
431#if WXWIN_COMPATIBILITY
432void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
433{
434 *x_page = GetScrollPageSize(wxHORIZONTAL);
435 *y_page = GetScrollPageSize(wxVERTICAL);
436}
a0bc2c1d
VZ
437
438void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
439{
440 if ( xx )
441 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
442 if ( yy )
443 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
444}
445#endif // WXWIN_COMPATIBILITY
c801d85f
KB
446
447void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
448{
a0bc2c1d
VZ
449 if ( x_unit )
450 *x_unit = m_xScrollPixelsPerLine;
451 if ( y_unit )
452 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
453}
454
455int wxScrolledWindow::GetScrollPageSize(int orient) const
456{
457 if ( orient == wxHORIZONTAL )
458 return m_xScrollLinesPerPage;
459 else
460 return m_yScrollLinesPerPage;
461}
462
463void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
464{
465 if ( orient == wxHORIZONTAL )
466 m_xScrollLinesPerPage = pageSize;
467 else
468 m_yScrollLinesPerPage = pageSize;
469}
470
471/*
472 * Scroll to given position (scroll position, not pixel position)
473 */
139adb6a 474void wxScrolledWindow::Scroll( int x_pos, int y_pos )
c801d85f 475{
139adb6a
RR
476 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
477 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
478
479 int w, h;
ecab4dba 480 m_targetWindow->GetClientSize(&w, &h);
c801d85f 481
139adb6a 482 if (x_pos != -1)
c801d85f 483 {
ed673c6a 484 int old_x = m_xScrollPosition;
139adb6a
RR
485 m_xScrollPosition = x_pos;
486
487 // Calculate page size i.e. number of scroll units you get on the
488 // current client window
ecab4dba 489 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
490 if (noPagePositions < 1) noPagePositions = 1;
491
492 // Correct position if greater than extent of canvas minus
493 // the visible portion of it or if below zero
494 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
495 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
496
ecab4dba 497 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
ed673c6a 498
ecab4dba 499 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
c801d85f 500 }
139adb6a 501 if (y_pos != -1)
c801d85f 502 {
ed673c6a 503 int old_y = m_yScrollPosition;
139adb6a
RR
504 m_yScrollPosition = y_pos;
505
506 // Calculate page size i.e. number of scroll units you get on the
507 // current client window
ecab4dba 508 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
509 if (noPagePositions < 1) noPagePositions = 1;
510
511 // Correct position if greater than extent of canvas minus
512 // the visible portion of it or if below zero
513 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
514 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
515
ecab4dba 516 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
ed673c6a 517
ecab4dba 518 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
c801d85f 519 }
139adb6a 520
139adb6a 521
2049ba38 522#ifdef __WXMSW__
ed673c6a 523// ::UpdateWindow ((HWND) GetHWND());
5e014a0c 524#else
ed673c6a 525// Refresh();
c801d85f 526#endif
7c74e7fe
SC
527#ifdef __WXMAC__
528 m_targetWindow->MacUpdateImmediately() ;
529#endif
c801d85f
KB
530}
531
debe6624 532void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 533{
139adb6a
RR
534 m_xScrollingEnabled = x_scroll;
535 m_yScrollingEnabled = y_scroll;
c801d85f
KB
536}
537
538void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
539{
a0bc2c1d
VZ
540 if ( x )
541 *x = m_xScrollPixelsPerLine * m_xScrollLines;
542 if ( y )
543 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
544}
545
546// Where the current view starts from
547void wxScrolledWindow::ViewStart (int *x, int *y) const
548{
a0bc2c1d
VZ
549 if ( x )
550 *x = m_xScrollPosition;
551 if ( y )
552 *y = m_yScrollPosition;
c801d85f
KB
553}
554
debe6624 555void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 556{
a0bc2c1d
VZ
557 if ( xx )
558 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
559 if ( yy )
560 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
561}
562
a0bc2c1d 563void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 564{
a0bc2c1d
VZ
565 if ( xx )
566 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
567 if ( yy )
568 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 569}