]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
Compile fix for wxDataFormat cast,
[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?
c801d85f
KB
156 UpdateWindow ((HWND) GetHWND());
157#endif
158}
159
ecab4dba
RR
160wxScrolledWindow::~wxScrolledWindow()
161{
162}
163
164void wxScrolledWindow::SetTargetWindow( wxWindow *target )
165{
166 wxASSERT_MSG( target, wxT("target window must not be NULL") );
167 m_targetWindow = target;
168}
169
170wxWindow *wxScrolledWindow::GetTargetWindow()
171{
172 return m_targetWindow;
173}
174
c5b42c87 175void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 176{
139adb6a 177 int orient = event.GetOrientation();
c801d85f 178
139adb6a
RR
179 int nScrollInc = CalcScrollInc(event);
180 if (nScrollInc == 0) return;
c801d85f 181
139adb6a
RR
182 if (orient == wxHORIZONTAL)
183 {
184 int newPos = m_xScrollPosition + nScrollInc;
185 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
186 }
187 else
188 {
189 int newPos = m_yScrollPosition + nScrollInc;
190 SetScrollPos(wxVERTICAL, newPos, TRUE );
191 }
c801d85f 192
139adb6a
RR
193 if (orient == wxHORIZONTAL)
194 {
195 m_xScrollPosition += nScrollInc;
196 }
c801d85f 197 else
139adb6a
RR
198 {
199 m_yScrollPosition += nScrollInc;
200 }
201
202 if (orient == wxHORIZONTAL)
203 {
204 if (m_xScrollingEnabled)
ecab4dba 205 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
139adb6a 206 else
ecab4dba 207 m_targetWindow->Refresh();
139adb6a 208 }
c801d85f 209 else
139adb6a
RR
210 {
211 if (m_yScrollingEnabled)
ecab4dba 212 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
139adb6a 213 else
ecab4dba 214 m_targetWindow->Refresh();
c801d85f 215 }
c801d85f
KB
216}
217
c5b42c87 218int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 219{
ecab4dba
RR
220 int pos = event.GetPosition();
221 int orient = event.GetOrientation();
c801d85f 222
ecab4dba
RR
223 int nScrollInc = 0;
224 switch (event.GetEventType())
c801d85f 225 {
ecab4dba
RR
226 case wxEVT_SCROLLWIN_TOP:
227 {
228 if (orient == wxHORIZONTAL)
229 nScrollInc = - m_xScrollPosition;
230 else
231 nScrollInc = - m_yScrollPosition;
232 break;
233 }
234 case wxEVT_SCROLLWIN_BOTTOM:
235 {
236 if (orient == wxHORIZONTAL)
237 nScrollInc = m_xScrollLines - m_xScrollPosition;
238 else
239 nScrollInc = m_yScrollLines - m_yScrollPosition;
240 break;
241 }
242 case wxEVT_SCROLLWIN_LINEUP:
243 {
244 nScrollInc = -1;
245 break;
246 }
247 case wxEVT_SCROLLWIN_LINEDOWN:
248 {
249 nScrollInc = 1;
250 break;
251 }
252 case wxEVT_SCROLLWIN_PAGEUP:
253 {
254 if (orient == wxHORIZONTAL)
255 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
256 else
257 nScrollInc = -GetScrollPageSize(wxVERTICAL);
258 break;
259 }
260 case wxEVT_SCROLLWIN_PAGEDOWN:
261 {
262 if (orient == wxHORIZONTAL)
263 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
264 else
265 nScrollInc = GetScrollPageSize(wxVERTICAL);
266 break;
267 }
268 case wxEVT_SCROLLWIN_THUMBTRACK:
269 {
270 if (orient == wxHORIZONTAL)
271 nScrollInc = pos - m_xScrollPosition;
272 else
273 nScrollInc = pos - m_yScrollPosition;
274 break;
275 }
276 default:
277 {
278 break;
279 }
c801d85f 280 }
88150e60 281
ecab4dba
RR
282 if (orient == wxHORIZONTAL)
283 {
284 if (m_xScrollPixelsPerLine > 0)
285 {
286 int w, h;
287 m_targetWindow->GetClientSize(&w, &h);
288
289 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
290 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
291 if (noPositions < 0)
292 noPositions = 0;
293
294 if ( (m_xScrollPosition + nScrollInc) < 0 )
295 nScrollInc = -m_xScrollPosition; // As -ve as we can go
296 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
297 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
298 }
299 else
300 m_targetWindow->Refresh();
9d9355c6
VZ
301 }
302 else
ecab4dba
RR
303 {
304 if (m_yScrollPixelsPerLine > 0)
305 {
306 int w, h;
307 m_targetWindow->GetClientSize(&w, &h);
9d9355c6 308
ecab4dba
RR
309 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
310 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
311 if (noPositions < 0)
312 noPositions = 0;
9d9355c6 313
ecab4dba
RR
314 if ( (m_yScrollPosition + nScrollInc) < 0 )
315 nScrollInc = -m_yScrollPosition; // As -ve as we can go
316 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
317 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
318 }
319 else
320 m_targetWindow->Refresh();
9d9355c6 321 }
9d9355c6 322
ecab4dba 323 return nScrollInc;
c801d85f
KB
324}
325
326// Adjust the scrollbars - new version.
27d029c7 327void wxScrolledWindow::AdjustScrollbars()
c801d85f 328{
139adb6a 329 int w, h;
ecab4dba 330 m_targetWindow->GetClientSize(&w, &h);
27d029c7
RR
331
332 int oldXScroll = m_xScrollPosition;
333 int oldYScroll = m_yScrollPosition;
c801d85f 334
139adb6a
RR
335 if (m_xScrollLines > 0)
336 {
c801d85f
KB
337 // Calculate page size i.e. number of scroll units you get on the
338 // current client window
ecab4dba 339 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 340 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 341
139adb6a
RR
342 // Correct position if greater than extent of canvas minus
343 // the visible portion of it or if below zero
344 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
345 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 346
139adb6a 347 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
348 // The amount by which we scroll when paging
349 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
350 }
351 else
352 {
353 m_xScrollPosition = 0;
354 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
355 }
356
357 if (m_yScrollLines > 0)
358 {
c801d85f
KB
359 // Calculate page size i.e. number of scroll units you get on the
360 // current client window
ecab4dba 361 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 362 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 363
139adb6a
RR
364 // Correct position if greater than extent of canvas minus
365 // the visible portion of it or if below zero
366 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
367 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
368
369 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
370 // The amount by which we scroll when paging
371 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
372 }
373 else
374 {
375 m_yScrollPosition = 0;
376 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
377 }
27d029c7
RR
378
379 if (oldXScroll != m_xScrollPosition)
380 {
381 if (m_xScrollingEnabled)
ecab4dba 382 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
27d029c7 383 else
ecab4dba 384 m_targetWindow->Refresh();
27d029c7
RR
385 }
386
387 if (oldYScroll != m_yScrollPosition)
388 {
389 if (m_yScrollingEnabled)
ecab4dba 390 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
27d029c7 391 else
ecab4dba 392 m_targetWindow->Refresh();
27d029c7 393 }
c801d85f
KB
394}
395
396// Default OnSize resets scrollbars, if any
397void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
398{
47d67540 399#if wxUSE_CONSTRAINTS
139adb6a 400 if (GetAutoLayout()) Layout();
c801d85f
KB
401#endif
402
139adb6a 403 AdjustScrollbars();
c801d85f
KB
404}
405
406// This calls OnDraw, having adjusted the origin according to the current
407// scroll position
408void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
409{
139adb6a
RR
410 wxPaintDC dc(this);
411 PrepareDC(dc);
c801d85f 412
139adb6a 413 OnDraw(dc);
c801d85f
KB
414}
415
416// Override this function if you don't want to have wxScrolledWindow
417// automatically change the origin according to the scroll position.
418void wxScrolledWindow::PrepareDC(wxDC& dc)
419{
139adb6a
RR
420 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
421 -m_yScrollPosition * m_yScrollPixelsPerLine );
422 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
423}
424
425#if WXWIN_COMPATIBILITY
426void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
427{
428 *x_page = GetScrollPageSize(wxHORIZONTAL);
429 *y_page = GetScrollPageSize(wxVERTICAL);
430}
a0bc2c1d
VZ
431
432void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
433{
434 if ( xx )
435 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
436 if ( yy )
437 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
438}
439#endif // WXWIN_COMPATIBILITY
c801d85f
KB
440
441void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
442{
a0bc2c1d
VZ
443 if ( x_unit )
444 *x_unit = m_xScrollPixelsPerLine;
445 if ( y_unit )
446 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
447}
448
449int wxScrolledWindow::GetScrollPageSize(int orient) const
450{
451 if ( orient == wxHORIZONTAL )
452 return m_xScrollLinesPerPage;
453 else
454 return m_yScrollLinesPerPage;
455}
456
457void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
458{
459 if ( orient == wxHORIZONTAL )
460 m_xScrollLinesPerPage = pageSize;
461 else
462 m_yScrollLinesPerPage = pageSize;
463}
464
465/*
466 * Scroll to given position (scroll position, not pixel position)
467 */
139adb6a 468void wxScrolledWindow::Scroll( int x_pos, int y_pos )
c801d85f 469{
139adb6a
RR
470 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
471 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
472
473 int w, h;
ecab4dba 474 m_targetWindow->GetClientSize(&w, &h);
c801d85f 475
139adb6a 476 if (x_pos != -1)
c801d85f 477 {
ed673c6a 478 int old_x = m_xScrollPosition;
139adb6a
RR
479 m_xScrollPosition = x_pos;
480
481 // Calculate page size i.e. number of scroll units you get on the
482 // current client window
ecab4dba 483 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
484 if (noPagePositions < 1) noPagePositions = 1;
485
486 // Correct position if greater than extent of canvas minus
487 // the visible portion of it or if below zero
488 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
489 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
490
ecab4dba 491 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
ed673c6a 492
ecab4dba 493 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
c801d85f 494 }
139adb6a 495 if (y_pos != -1)
c801d85f 496 {
ed673c6a 497 int old_y = m_yScrollPosition;
139adb6a
RR
498 m_yScrollPosition = y_pos;
499
500 // Calculate page size i.e. number of scroll units you get on the
501 // current client window
ecab4dba 502 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
503 if (noPagePositions < 1) noPagePositions = 1;
504
505 // Correct position if greater than extent of canvas minus
506 // the visible portion of it or if below zero
507 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
508 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
509
ecab4dba 510 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
ed673c6a 511
ecab4dba 512 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
c801d85f 513 }
139adb6a 514
139adb6a 515
2049ba38 516#ifdef __WXMSW__
ed673c6a 517// ::UpdateWindow ((HWND) GetHWND());
5e014a0c 518#else
ed673c6a 519// Refresh();
c801d85f
KB
520#endif
521}
522
debe6624 523void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 524{
139adb6a
RR
525 m_xScrollingEnabled = x_scroll;
526 m_yScrollingEnabled = y_scroll;
c801d85f
KB
527}
528
529void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
530{
a0bc2c1d
VZ
531 if ( x )
532 *x = m_xScrollPixelsPerLine * m_xScrollLines;
533 if ( y )
534 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
535}
536
537// Where the current view starts from
538void wxScrolledWindow::ViewStart (int *x, int *y) const
539{
a0bc2c1d
VZ
540 if ( x )
541 *x = m_xScrollPosition;
542 if ( y )
543 *y = m_yScrollPosition;
c801d85f
KB
544}
545
debe6624 546void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 547{
a0bc2c1d
VZ
548 if ( xx )
549 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
550 if ( yy )
551 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
552}
553
a0bc2c1d 554void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 555{
a0bc2c1d
VZ
556 if ( xx )
557 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
558 if ( yy )
559 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 560}