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