]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
* Changed behaviour of wxTextStreams::operator(wxUint8/wxInt8). Now it writes
[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;
83
053f9cc1 84 return wxPanel::Create(parent, id, pos, size, style, name);
c801d85f
KB
85}
86
87/*
88 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
89 * noUnitsX/noUnitsY: : no. units per scrollbar
90 */
debe6624
JS
91void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
92 int noUnitsX, int noUnitsY,
93 int xPos, int yPos, bool noRefresh )
c801d85f 94{
139adb6a
RR
95 bool do_refresh =
96 (
c801d85f 97 (noUnitsX != 0 && m_xScrollLines == 0) ||
ea5c6ca7 98 (noUnitsX < m_xScrollLines) ||
c801d85f 99 (noUnitsY != 0 && m_yScrollLines == 0) ||
ea5c6ca7 100 (noUnitsY < m_yScrollLines) ||
c801d85f
KB
101 (xPos != m_xScrollPosition) ||
102 (yPos != m_yScrollPosition) ||
103 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
104 (pixelsPerUnitY != m_yScrollPixelsPerLine)
139adb6a 105 );
c801d85f 106
139adb6a
RR
107 m_xScrollPixelsPerLine = pixelsPerUnitX;
108 m_yScrollPixelsPerLine = pixelsPerUnitY;
109 m_xScrollPosition = xPos;
110 m_yScrollPosition = yPos;
111 m_xScrollLines = noUnitsX;
112 m_yScrollLines = noUnitsY;
a91b47e8
JS
113
114#ifdef __WXMOTIF__
115 // Sorry, some Motif-specific code to implement a backing pixmap
116 // for the wxRETAINED style. Implementing a backing store can't
117 // be entirely generic because it relies on the wxWindowDC implementation
118 // to duplicate X drawing calls for the backing pixmap.
119
120 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
121 {
122 Display* dpy = XtDisplay((Widget) GetMainWidget());
123
124 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
125 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
126 if (m_backingPixmap &&
127 !((m_pixmapWidth == totalPixelWidth) &&
128 (m_pixmapHeight == totalPixelHeight)))
129 {
130 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
131 m_backingPixmap = (WXPixmap) 0;
132 }
133
134 if (!m_backingPixmap &&
135 (noUnitsX != 0) && (noUnitsY != 0))
136 {
137 int depth = wxDisplayDepth();
138 m_pixmapWidth = totalPixelWidth;
139 m_pixmapHeight = totalPixelHeight;
140 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
141 m_pixmapWidth, m_pixmapHeight, depth);
142 }
143
144 }
145#endif
d4c99d6f 146
139adb6a 147 AdjustScrollbars();
c801d85f 148
ea5c6ca7
RR
149 if (do_refresh && !noRefresh)
150 Refresh();
c801d85f 151
2049ba38 152#ifdef __WXMSW__
48d1144b 153 // Necessary?
c801d85f
KB
154 UpdateWindow ((HWND) GetHWND());
155#endif
156}
157
c5b42c87 158void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
c801d85f 159{
139adb6a 160 int orient = event.GetOrientation();
c801d85f 161
139adb6a
RR
162 int nScrollInc = CalcScrollInc(event);
163 if (nScrollInc == 0) return;
c801d85f 164
139adb6a
RR
165 if (orient == wxHORIZONTAL)
166 {
167 int newPos = m_xScrollPosition + nScrollInc;
168 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
169 }
170 else
171 {
172 int newPos = m_yScrollPosition + nScrollInc;
173 SetScrollPos(wxVERTICAL, newPos, TRUE );
174 }
c801d85f 175
139adb6a
RR
176 if (orient == wxHORIZONTAL)
177 {
178 m_xScrollPosition += nScrollInc;
179 }
c801d85f 180 else
139adb6a
RR
181 {
182 m_yScrollPosition += nScrollInc;
183 }
184
185 if (orient == wxHORIZONTAL)
186 {
187 if (m_xScrollingEnabled)
188 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
189 else
190 Refresh();
191 }
c801d85f 192 else
139adb6a
RR
193 {
194 if (m_yScrollingEnabled)
195 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
196 else
197 Refresh();
c801d85f 198 }
c801d85f
KB
199}
200
c5b42c87 201int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
c801d85f
KB
202{
203 int pos = event.GetPosition();
204 int orient = event.GetOrientation();
205
206 int nScrollInc = 0;
207 switch (event.GetEventType())
208 {
c5b42c87 209 case wxEVT_SCROLLWIN_TOP:
c801d85f
KB
210 {
211 if (orient == wxHORIZONTAL)
212 nScrollInc = - m_xScrollPosition;
213 else
214 nScrollInc = - m_yScrollPosition;
215 break;
216 }
c5b42c87 217 case wxEVT_SCROLLWIN_BOTTOM:
c801d85f
KB
218 {
219 if (orient == wxHORIZONTAL)
220 nScrollInc = m_xScrollLines - m_xScrollPosition;
221 else
222 nScrollInc = m_yScrollLines - m_yScrollPosition;
223 break;
224 }
c5b42c87 225 case wxEVT_SCROLLWIN_LINEUP:
c801d85f
KB
226 {
227 nScrollInc = -1;
228 break;
229 }
c5b42c87 230 case wxEVT_SCROLLWIN_LINEDOWN:
c801d85f
KB
231 {
232 nScrollInc = 1;
233 break;
234 }
c5b42c87 235 case wxEVT_SCROLLWIN_PAGEUP:
c801d85f
KB
236 {
237 if (orient == wxHORIZONTAL)
238 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
239 else
240 nScrollInc = -GetScrollPageSize(wxVERTICAL);
241 break;
242 }
c5b42c87 243 case wxEVT_SCROLLWIN_PAGEDOWN:
c801d85f
KB
244 {
245 if (orient == wxHORIZONTAL)
246 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
247 else
248 nScrollInc = GetScrollPageSize(wxVERTICAL);
249 break;
250 }
c5b42c87 251 case wxEVT_SCROLLWIN_THUMBTRACK:
c801d85f
KB
252 {
253 if (orient == wxHORIZONTAL)
254 nScrollInc = pos - m_xScrollPosition;
255 else
256 nScrollInc = pos - m_yScrollPosition;
257 break;
258 }
259 default:
260 {
261 break;
262 }
263 }
88150e60 264
c801d85f
KB
265 if (orient == wxHORIZONTAL)
266 {
9d9355c6
VZ
267 if (m_xScrollPixelsPerLine > 0) {
268 int w, h;
269 GetClientSize(&w, &h);
c801d85f 270
9d9355c6
VZ
271 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
272 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
273 if (noPositions < 0)
274 noPositions = 0;
c801d85f 275
9d9355c6
VZ
276 if ( (m_xScrollPosition + nScrollInc) < 0 )
277 nScrollInc = -m_xScrollPosition; // As -ve as we can go
278 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
279 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
280 }
281 else
282 Refresh();
c801d85f
KB
283 }
284 else
285 {
9d9355c6
VZ
286 if (m_yScrollPixelsPerLine > 0) {
287 int w, h;
288 GetClientSize(&w, &h);
289
290 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
291 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
292 if (noPositions < 0)
293 noPositions = 0;
294
295 if ( (m_yScrollPosition + nScrollInc) < 0 )
296 nScrollInc = -m_yScrollPosition; // As -ve as we can go
297 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
298 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
299 }
300 else
301 Refresh();
c801d85f 302 }
9d9355c6
VZ
303
304 return nScrollInc;
c801d85f
KB
305}
306
307// Adjust the scrollbars - new version.
308void wxScrolledWindow::AdjustScrollbars(void)
309{
139adb6a
RR
310 int w, h;
311 GetClientSize(&w, &h);
c801d85f 312
139adb6a
RR
313 if (m_xScrollLines > 0)
314 {
c801d85f
KB
315 // Calculate page size i.e. number of scroll units you get on the
316 // current client window
139adb6a
RR
317 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
318 if (noPagePositions < 1) noPagePositions = 1;
c801d85f 319
139adb6a
RR
320 // Correct position if greater than extent of canvas minus
321 // the visible portion of it or if below zero
322 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
323 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 324
139adb6a 325 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
326 // The amount by which we scroll when paging
327 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
328 }
329 else
330 {
331 m_xScrollPosition = 0;
332 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
333 }
334
335 if (m_yScrollLines > 0)
336 {
c801d85f
KB
337 // Calculate page size i.e. number of scroll units you get on the
338 // current client window
139adb6a
RR
339 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
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_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
345 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
346
347 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
348 // The amount by which we scroll when paging
349 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
350 }
351 else
352 {
353 m_yScrollPosition = 0;
354 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
355 }
c801d85f
KB
356}
357
358// Default OnSize resets scrollbars, if any
359void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
360{
47d67540 361#if wxUSE_CONSTRAINTS
139adb6a 362 if (GetAutoLayout()) Layout();
c801d85f
KB
363#endif
364
139adb6a 365 AdjustScrollbars();
c801d85f
KB
366}
367
368// This calls OnDraw, having adjusted the origin according to the current
369// scroll position
370void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
371{
139adb6a
RR
372 wxPaintDC dc(this);
373 PrepareDC(dc);
c801d85f 374
139adb6a 375 OnDraw(dc);
c801d85f
KB
376}
377
378// Override this function if you don't want to have wxScrolledWindow
379// automatically change the origin according to the scroll position.
380void wxScrolledWindow::PrepareDC(wxDC& dc)
381{
139adb6a
RR
382 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
383 -m_yScrollPosition * m_yScrollPixelsPerLine );
384 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
385}
386
387#if WXWIN_COMPATIBILITY
388void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
389{
390 *x_page = GetScrollPageSize(wxHORIZONTAL);
391 *y_page = GetScrollPageSize(wxVERTICAL);
392}
a0bc2c1d
VZ
393
394void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
395{
396 if ( xx )
397 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
398 if ( yy )
399 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
400}
401#endif // WXWIN_COMPATIBILITY
c801d85f
KB
402
403void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
404{
a0bc2c1d
VZ
405 if ( x_unit )
406 *x_unit = m_xScrollPixelsPerLine;
407 if ( y_unit )
408 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
409}
410
411int wxScrolledWindow::GetScrollPageSize(int orient) const
412{
413 if ( orient == wxHORIZONTAL )
414 return m_xScrollLinesPerPage;
415 else
416 return m_yScrollLinesPerPage;
417}
418
419void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
420{
421 if ( orient == wxHORIZONTAL )
422 m_xScrollLinesPerPage = pageSize;
423 else
424 m_yScrollLinesPerPage = pageSize;
425}
426
427/*
428 * Scroll to given position (scroll position, not pixel position)
429 */
139adb6a 430void wxScrolledWindow::Scroll( int x_pos, int y_pos )
c801d85f 431{
139adb6a
RR
432 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
433 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
434
435 int w, h;
436 GetClientSize(&w, &h);
c801d85f 437
139adb6a 438 if (x_pos != -1)
c801d85f 439 {
139adb6a
RR
440 m_xScrollPosition = x_pos;
441
442 // Calculate page size i.e. number of scroll units you get on the
443 // current client window
444 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
445 if (noPagePositions < 1) noPagePositions = 1;
446
447 // Correct position if greater than extent of canvas minus
448 // the visible portion of it or if below zero
449 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
450 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
451
452 SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
c801d85f 453 }
139adb6a 454 if (y_pos != -1)
c801d85f 455 {
139adb6a
RR
456 m_yScrollPosition = y_pos;
457
458 // Calculate page size i.e. number of scroll units you get on the
459 // current client window
460 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
461 if (noPagePositions < 1) noPagePositions = 1;
462
463 // Correct position if greater than extent of canvas minus
464 // the visible portion of it or if below zero
465 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
466 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
467
468 SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
c801d85f 469 }
139adb6a 470
ea5c6ca7 471 // BAD, BAD, can cause event loops if called from OnPaint(). KB.
aab3e197 472 // Refresh();
139adb6a 473
2049ba38 474#ifdef __WXMSW__
48d1144b 475 // Necessary?
139adb6a 476 ::UpdateWindow ((HWND) GetHWND());
c801d85f
KB
477#endif
478}
479
debe6624 480void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 481{
139adb6a
RR
482 m_xScrollingEnabled = x_scroll;
483 m_yScrollingEnabled = y_scroll;
c801d85f
KB
484}
485
486void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
487{
a0bc2c1d
VZ
488 if ( x )
489 *x = m_xScrollPixelsPerLine * m_xScrollLines;
490 if ( y )
491 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
492}
493
494// Where the current view starts from
495void wxScrolledWindow::ViewStart (int *x, int *y) const
496{
a0bc2c1d
VZ
497 if ( x )
498 *x = m_xScrollPosition;
499 if ( y )
500 *y = m_yScrollPosition;
c801d85f
KB
501}
502
debe6624 503void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 504{
a0bc2c1d
VZ
505 if ( xx )
506 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
507 if ( yy )
508 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
509}
510
a0bc2c1d 511void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 512{
a0bc2c1d
VZ
513 if ( xx )
514 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
515 if ( yy )
516 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 517}