]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrolwin.cpp
DP: GetBitmap() added to wxImageList
[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
2049ba38 23#ifdef __WXMSW__
c801d85f
KB
24#include "windows.h"
25#endif
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
31#include "wx/generic/scrolwin.h"
32
33#if !USE_SHARED_LIBRARY
34BEGIN_EVENT_TABLE(wxScrolledWindow, wxWindow)
35 EVT_SCROLL(wxScrolledWindow::OnScroll)
36 EVT_SIZE(wxScrolledWindow::OnSize)
37 EVT_PAINT(wxScrolledWindow::OnPaint)
38END_EVENT_TABLE()
39
40IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxWindow)
41#endif
42
43wxScrolledWindow::wxScrolledWindow(void)
44{
45 m_xScrollPixelsPerLine = 0;
46 m_yScrollPixelsPerLine = 0;
47 m_xScrollingEnabled = TRUE;
48 m_yScrollingEnabled = TRUE;
49 m_xScrollPosition = 0;
50 m_yScrollPosition = 0;
51 m_xScrollLines = 0;
52 m_yScrollLines = 0;
53 m_xScrollLinesPerPage = 0;
54 m_yScrollLinesPerPage = 0;
55}
56
debe6624 57bool wxScrolledWindow::Create(wxWindow *parent, wxWindowID id,
c801d85f
KB
58 const wxPoint& pos,
59 const wxSize& size,
debe6624 60 long style,
c801d85f
KB
61 const wxString& name)
62{
63 m_xScrollPixelsPerLine = 0;
64 m_yScrollPixelsPerLine = 0;
65 m_xScrollingEnabled = TRUE;
66 m_yScrollingEnabled = TRUE;
67 m_xScrollPosition = 0;
68 m_yScrollPosition = 0;
69 m_xScrollLines = 0;
70 m_yScrollLines = 0;
71 m_xScrollLinesPerPage = 0;
72 m_yScrollLinesPerPage = 0;
73
74 return wxWindow::Create(parent, id, pos, size, style, name);
75}
76
77/*
78 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
79 * noUnitsX/noUnitsY: : no. units per scrollbar
80 */
debe6624
JS
81void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
82 int noUnitsX, int noUnitsY,
83 int xPos, int yPos, bool noRefresh )
c801d85f
KB
84{
85 bool do_refresh =
86 (
87 (noUnitsX != 0 && m_xScrollLines == 0) ||
88 (noUnitsX < m_xScrollPosition) ||
89 (noUnitsY != 0 && m_yScrollLines == 0) ||
90 (noUnitsY < m_yScrollPosition) ||
91 (xPos != m_xScrollPosition) ||
92 (yPos != m_yScrollPosition) ||
93 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
94 (pixelsPerUnitY != m_yScrollPixelsPerLine)
95 );
96
97 m_xScrollPixelsPerLine = pixelsPerUnitX;
98 m_yScrollPixelsPerLine = pixelsPerUnitY;
11ed4f79
JS
99 m_xScrollPosition = xPos;
100 m_yScrollPosition = yPos;
c801d85f
KB
101 m_xScrollLines = noUnitsX;
102 m_yScrollLines = noUnitsY;
d4c99d6f 103
c801d85f
KB
104 AdjustScrollbars();
105
106 if (do_refresh && !noRefresh) Refresh();
107
2049ba38 108#ifdef __WXMSW__
c801d85f
KB
109 UpdateWindow ((HWND) GetHWND());
110#endif
111}
112
113void wxScrolledWindow::OnScroll(wxScrollEvent& event)
114{
115 int orient = event.GetOrientation();
116
117 int nScrollInc = CalcScrollInc(event);
118 if (nScrollInc == 0)
119 return;
120
121 // TODO: should we store the scroll position here as well as in wxWindow?
122 if (orient == wxHORIZONTAL)
123 {
124 int newPos = m_xScrollPosition + nScrollInc;
125 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
126 }
127 else
128 {
129 int newPos = m_yScrollPosition + nScrollInc;
130 SetScrollPos(wxVERTICAL, newPos, TRUE );
131 }
132
133/*
134 // TODO We need to multiply the ScrollWindow amount by the scaling
135 // factor, but how do we know what this is in wxWin 2.0???
136 float scaleX = 1.0;
137 float scaleY = 1.0;
138
139 if ( this->IsKindOf(CLASSINFO(wxCanvas)) )
140 {
141 wxDC* dc = ((wxCanvas *)this)->GetDC();
142 dc->GetUserScale(&scaleX, &scaleY);
143 }
144*/
145
146 if (orient == wxHORIZONTAL)
147 {
148 m_xScrollPosition += nScrollInc;
149 }
150 else
151 {
152 m_yScrollPosition += nScrollInc;
153 }
154
155 if (orient == wxHORIZONTAL)
156 {
157 if (m_xScrollingEnabled)
158 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
159 else
160 Refresh();
161 }
162 else
163 {
164 if (m_yScrollingEnabled)
165 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
166 else
167 Refresh();
168 }
169
170}
171
172int wxScrolledWindow::CalcScrollInc(wxScrollEvent& event)
173{
174 int pos = event.GetPosition();
175 int orient = event.GetOrientation();
176
177 int nScrollInc = 0;
178 switch (event.GetEventType())
179 {
180 case wxEVENT_TYPE_SCROLL_TOP:
181 {
182 if (orient == wxHORIZONTAL)
183 nScrollInc = - m_xScrollPosition;
184 else
185 nScrollInc = - m_yScrollPosition;
186 break;
187 }
188 case wxEVENT_TYPE_SCROLL_BOTTOM:
189 {
190 if (orient == wxHORIZONTAL)
191 nScrollInc = m_xScrollLines - m_xScrollPosition;
192 else
193 nScrollInc = m_yScrollLines - m_yScrollPosition;
194 break;
195 }
196 case wxEVENT_TYPE_SCROLL_LINEUP:
197 {
198 nScrollInc = -1;
199 break;
200 }
201 case wxEVENT_TYPE_SCROLL_LINEDOWN:
202 {
203 nScrollInc = 1;
204 break;
205 }
206 case wxEVENT_TYPE_SCROLL_PAGEUP:
207 {
208 if (orient == wxHORIZONTAL)
209 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
210 else
211 nScrollInc = -GetScrollPageSize(wxVERTICAL);
212 break;
213 }
214 case wxEVENT_TYPE_SCROLL_PAGEDOWN:
215 {
216 if (orient == wxHORIZONTAL)
217 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
218 else
219 nScrollInc = GetScrollPageSize(wxVERTICAL);
220 break;
221 }
222 case wxEVENT_TYPE_SCROLL_THUMBTRACK:
223 {
224 if (orient == wxHORIZONTAL)
225 nScrollInc = pos - m_xScrollPosition;
226 else
227 nScrollInc = pos - m_yScrollPosition;
228 break;
229 }
230 default:
231 {
232 break;
233 }
234 }
235 if (orient == wxHORIZONTAL)
236 {
237 int w, h;
238 GetClientSize(&w, &h);
239
240 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
241 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
242 if (noPositions < 0)
243 noPositions = 0;
244
245 if ( (m_xScrollPosition + nScrollInc) < 0 )
246 nScrollInc = -m_xScrollPosition; // As -ve as we can go
247 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
248 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
249
250 return nScrollInc;
251 }
252 else
253 {
254 int w, h;
255 GetClientSize(&w, &h);
256
257 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
258 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
259 if (noPositions < 0)
260 noPositions = 0;
261
262 if ( (m_yScrollPosition + nScrollInc) < 0 )
263 nScrollInc = -m_yScrollPosition; // As -ve as we can go
264 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
265 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
266
267 return nScrollInc;
268 }
269}
270
271// Adjust the scrollbars - new version.
272void wxScrolledWindow::AdjustScrollbars(void)
273{
274 int w, h;
275 GetClientSize(&w, &h);
276
277 // Recalculate scroll bar range and position
278 if (m_xScrollLines > 0)
279 {
280 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
281 int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
282 if (newRange < 0)
283 newRange = 0;
284
285 m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
286
287 // Calculate page size i.e. number of scroll units you get on the
288 // current client window
289 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
290 if (noPagePositions < 1)
291 noPagePositions = 1;
292
293 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
294 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
295 }
296 // Robert Roebling
297 else
298 {
299 m_xScrollPosition = 0;
300 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
301 }
302
303 if (m_yScrollLines > 0)
304 {
305 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
306 int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
307 if (newRange < 0)
308 newRange = 0;
309
310 m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
311
312 // Calculate page size i.e. number of scroll units you get on the
313 // current client window
314 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
315 if (noPagePositions < 1)
316 noPagePositions = 1;
317
318 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
319 SetScrollPageSize(wxVERTICAL, noPagePositions);
320 }
321 else
322 {
323 m_yScrollPosition = 0;
324 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE); // Robert Roebling
325 }
326
327}
328
329// Default OnSize resets scrollbars, if any
330void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
331{
332#if USE_CONSTRAINTS
333 if (GetAutoLayout())
334 Layout();
335#endif
336
337 AdjustScrollbars();
338}
339
340// This calls OnDraw, having adjusted the origin according to the current
341// scroll position
342void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
343{
344 wxPaintDC dc(this);
345 PrepareDC(dc);
346
347 OnDraw(dc);
348}
349
350// Override this function if you don't want to have wxScrolledWindow
351// automatically change the origin according to the scroll position.
352void wxScrolledWindow::PrepareDC(wxDC& dc)
353{
354 dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
355}
356
357#if WXWIN_COMPATIBILITY
358void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
359{
360 *x_page = GetScrollPageSize(wxHORIZONTAL);
361 *y_page = GetScrollPageSize(wxVERTICAL);
362}
363#endif
364
365void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
366{
367 *x_unit = m_xScrollPixelsPerLine;
368 *y_unit = m_yScrollPixelsPerLine;
369}
370
371int wxScrolledWindow::GetScrollPageSize(int orient) const
372{
373 if ( orient == wxHORIZONTAL )
374 return m_xScrollLinesPerPage;
375 else
376 return m_yScrollLinesPerPage;
377}
378
379void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
380{
381 if ( orient == wxHORIZONTAL )
382 m_xScrollLinesPerPage = pageSize;
383 else
384 m_yScrollLinesPerPage = pageSize;
385}
386
387/*
388 * Scroll to given position (scroll position, not pixel position)
389 */
debe6624 390void wxScrolledWindow::Scroll (int x_pos, int y_pos)
c801d85f
KB
391{
392 int old_x, old_y;
393 ViewStart (&old_x, &old_y);
394 if (((x_pos == -1) || (x_pos == old_x)) && ((y_pos == -1) || (y_pos == old_y)))
395 return;
396
397 if (x_pos > -1)
398 {
399 m_xScrollPosition = x_pos;
400 SetScrollPos (wxHORIZONTAL, x_pos, TRUE);
401 }
402 if (y_pos > -1)
403 {
404 m_yScrollPosition = y_pos;
405 SetScrollPos (wxVERTICAL, y_pos, TRUE);
406 }
407 Refresh();
2049ba38 408#ifdef __WXMSW__
c801d85f
KB
409 ::UpdateWindow ((HWND) GetHWND());
410#endif
411}
412
debe6624 413void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f
KB
414{
415 m_xScrollingEnabled = x_scroll;
416 m_yScrollingEnabled = y_scroll;
417}
418
419void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
420{
421 *x = m_xScrollPixelsPerLine * m_xScrollLines;
422 *y = m_yScrollPixelsPerLine * m_yScrollLines;
423}
424
425// Where the current view starts from
426void wxScrolledWindow::ViewStart (int *x, int *y) const
427{
428 *x = m_xScrollPosition;
429 *y = m_yScrollPosition;
430}
431
debe6624 432void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f
KB
433{
434 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
435 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
436}
437
debe6624 438void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
c801d85f
KB
439{
440 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
441 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
442}
443
444