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