]>
Commit | Line | Data |
---|---|---|
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" | |
28 | ||
29 | #if !USE_SHARED_LIBRARY | |
30 | BEGIN_EVENT_TABLE(wxScrolledWindow, wxWindow) | |
139adb6a RR |
31 | EVT_SCROLL(wxScrolledWindow::OnScroll) |
32 | EVT_SIZE(wxScrolledWindow::OnSize) | |
33 | EVT_PAINT(wxScrolledWindow::OnPaint) | |
c801d85f KB |
34 | END_EVENT_TABLE() |
35 | ||
36 | IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxWindow) | |
37 | #endif | |
38 | ||
48d1144b JS |
39 | #ifdef __WXMSW__ |
40 | #include "windows.h" | |
41 | #endif | |
42 | ||
c801d85f KB |
43 | wxScrolledWindow::wxScrolledWindow(void) |
44 | { | |
139adb6a RR |
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; | |
c801d85f KB |
57 | } |
58 | ||
debe6624 | 59 | bool wxScrolledWindow::Create(wxWindow *parent, wxWindowID id, |
c801d85f KB |
60 | const wxPoint& pos, |
61 | const wxSize& size, | |
debe6624 | 62 | long style, |
c801d85f KB |
63 | const wxString& name) |
64 | { | |
139adb6a RR |
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); | |
c801d85f KB |
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 | */ | |
debe6624 JS |
85 | void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY, |
86 | int noUnitsX, int noUnitsY, | |
87 | int xPos, int yPos, bool noRefresh ) | |
c801d85f | 88 | { |
139adb6a RR |
89 | bool do_refresh = |
90 | ( | |
c801d85f KB |
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) | |
139adb6a | 99 | ); |
c801d85f | 100 | |
139adb6a RR |
101 | m_xScrollPixelsPerLine = pixelsPerUnitX; |
102 | m_yScrollPixelsPerLine = pixelsPerUnitY; | |
103 | m_xScrollPosition = xPos; | |
104 | m_yScrollPosition = yPos; | |
105 | m_xScrollLines = noUnitsX; | |
106 | m_yScrollLines = noUnitsY; | |
d4c99d6f | 107 | |
139adb6a | 108 | AdjustScrollbars(); |
c801d85f KB |
109 | |
110 | if (do_refresh && !noRefresh) Refresh(); | |
111 | ||
2049ba38 | 112 | #ifdef __WXMSW__ |
48d1144b | 113 | // Necessary? |
c801d85f KB |
114 | UpdateWindow ((HWND) GetHWND()); |
115 | #endif | |
116 | } | |
117 | ||
118 | void wxScrolledWindow::OnScroll(wxScrollEvent& event) | |
119 | { | |
139adb6a | 120 | int orient = event.GetOrientation(); |
c801d85f | 121 | |
139adb6a RR |
122 | int nScrollInc = CalcScrollInc(event); |
123 | if (nScrollInc == 0) return; | |
c801d85f | 124 | |
139adb6a RR |
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 | } | |
c801d85f | 135 | |
139adb6a RR |
136 | if (orient == wxHORIZONTAL) |
137 | { | |
138 | m_xScrollPosition += nScrollInc; | |
139 | } | |
c801d85f | 140 | else |
139adb6a RR |
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 | } | |
c801d85f | 152 | else |
139adb6a RR |
153 | { |
154 | if (m_yScrollingEnabled) | |
155 | ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL); | |
156 | else | |
157 | Refresh(); | |
c801d85f | 158 | } |
c801d85f KB |
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 | { | |
f5419957 | 169 | case wxEVT_SCROLL_TOP: |
c801d85f KB |
170 | { |
171 | if (orient == wxHORIZONTAL) | |
172 | nScrollInc = - m_xScrollPosition; | |
173 | else | |
174 | nScrollInc = - m_yScrollPosition; | |
175 | break; | |
176 | } | |
f5419957 | 177 | case wxEVT_SCROLL_BOTTOM: |
c801d85f KB |
178 | { |
179 | if (orient == wxHORIZONTAL) | |
180 | nScrollInc = m_xScrollLines - m_xScrollPosition; | |
181 | else | |
182 | nScrollInc = m_yScrollLines - m_yScrollPosition; | |
183 | break; | |
184 | } | |
f5419957 | 185 | case wxEVT_SCROLL_LINEUP: |
c801d85f KB |
186 | { |
187 | nScrollInc = -1; | |
188 | break; | |
189 | } | |
f5419957 | 190 | case wxEVT_SCROLL_LINEDOWN: |
c801d85f KB |
191 | { |
192 | nScrollInc = 1; | |
193 | break; | |
194 | } | |
f5419957 | 195 | case wxEVT_SCROLL_PAGEUP: |
c801d85f KB |
196 | { |
197 | if (orient == wxHORIZONTAL) | |
198 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); | |
199 | else | |
200 | nScrollInc = -GetScrollPageSize(wxVERTICAL); | |
201 | break; | |
202 | } | |
f5419957 | 203 | case wxEVT_SCROLL_PAGEDOWN: |
c801d85f KB |
204 | { |
205 | if (orient == wxHORIZONTAL) | |
206 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); | |
207 | else | |
208 | nScrollInc = GetScrollPageSize(wxVERTICAL); | |
209 | break; | |
210 | } | |
f5419957 | 211 | case wxEVT_SCROLL_THUMBTRACK: |
c801d85f KB |
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 | } | |
88150e60 | 224 | |
c801d85f KB |
225 | if (orient == wxHORIZONTAL) |
226 | { | |
9d9355c6 VZ |
227 | if (m_xScrollPixelsPerLine > 0) { |
228 | int w, h; | |
229 | GetClientSize(&w, &h); | |
c801d85f | 230 | |
9d9355c6 VZ |
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; | |
c801d85f | 235 | |
9d9355c6 VZ |
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(); | |
c801d85f KB |
243 | } |
244 | else | |
245 | { | |
9d9355c6 VZ |
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(); | |
c801d85f | 262 | } |
9d9355c6 VZ |
263 | |
264 | return nScrollInc; | |
c801d85f KB |
265 | } |
266 | ||
267 | // Adjust the scrollbars - new version. | |
268 | void wxScrolledWindow::AdjustScrollbars(void) | |
269 | { | |
139adb6a RR |
270 | int w, h; |
271 | GetClientSize(&w, &h); | |
c801d85f | 272 | |
139adb6a RR |
273 | if (m_xScrollLines > 0) |
274 | { | |
c801d85f KB |
275 | // Calculate page size i.e. number of scroll units you get on the |
276 | // current client window | |
139adb6a RR |
277 | int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 ); |
278 | if (noPagePositions < 1) noPagePositions = 1; | |
c801d85f | 279 | |
139adb6a RR |
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 ); | |
c801d85f | 284 | |
139adb6a | 285 | SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines); |
88150e60 JS |
286 | // The amount by which we scroll when paging |
287 | SetScrollPageSize(wxHORIZONTAL, noPagePositions); | |
139adb6a RR |
288 | } |
289 | else | |
290 | { | |
291 | m_xScrollPosition = 0; | |
292 | SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE); | |
293 | } | |
294 | ||
295 | if (m_yScrollLines > 0) | |
296 | { | |
c801d85f KB |
297 | // Calculate page size i.e. number of scroll units you get on the |
298 | // current client window | |
139adb6a RR |
299 | int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 ); |
300 | if (noPagePositions < 1) noPagePositions = 1; | |
c801d85f | 301 | |
139adb6a RR |
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); | |
88150e60 JS |
308 | // The amount by which we scroll when paging |
309 | SetScrollPageSize(wxVERTICAL, noPagePositions); | |
139adb6a RR |
310 | } |
311 | else | |
312 | { | |
313 | m_yScrollPosition = 0; | |
314 | SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE); | |
315 | } | |
c801d85f KB |
316 | } |
317 | ||
318 | // Default OnSize resets scrollbars, if any | |
319 | void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event)) | |
320 | { | |
47d67540 | 321 | #if wxUSE_CONSTRAINTS |
139adb6a | 322 | if (GetAutoLayout()) Layout(); |
c801d85f KB |
323 | #endif |
324 | ||
139adb6a | 325 | AdjustScrollbars(); |
c801d85f KB |
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 | { | |
139adb6a RR |
332 | wxPaintDC dc(this); |
333 | PrepareDC(dc); | |
c801d85f | 334 | |
139adb6a | 335 | OnDraw(dc); |
c801d85f KB |
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 | { | |
139adb6a RR |
342 | dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine, |
343 | -m_yScrollPosition * m_yScrollPixelsPerLine ); | |
344 | dc.SetUserScale( m_scaleX, m_scaleY ); | |
c801d85f KB |
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 | */ | |
139adb6a | 380 | void wxScrolledWindow::Scroll( int x_pos, int y_pos ) |
c801d85f | 381 | { |
139adb6a RR |
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); | |
c801d85f | 387 | |
139adb6a | 388 | if (x_pos != -1) |
c801d85f | 389 | { |
139adb6a RR |
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 ); | |
c801d85f | 403 | } |
139adb6a | 404 | if (y_pos != -1) |
c801d85f | 405 | { |
139adb6a RR |
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 ); | |
c801d85f | 419 | } |
139adb6a RR |
420 | |
421 | Refresh(); | |
422 | ||
2049ba38 | 423 | #ifdef __WXMSW__ |
48d1144b | 424 | // Necessary? |
139adb6a | 425 | ::UpdateWindow ((HWND) GetHWND()); |
c801d85f KB |
426 | #endif |
427 | } | |
428 | ||
debe6624 | 429 | void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll) |
c801d85f | 430 | { |
139adb6a RR |
431 | m_xScrollingEnabled = x_scroll; |
432 | m_yScrollingEnabled = y_scroll; | |
c801d85f KB |
433 | } |
434 | ||
435 | void wxScrolledWindow::GetVirtualSize (int *x, int *y) const | |
436 | { | |
139adb6a RR |
437 | *x = m_xScrollPixelsPerLine * m_xScrollLines; |
438 | *y = m_yScrollPixelsPerLine * m_yScrollLines; | |
c801d85f KB |
439 | } |
440 | ||
441 | // Where the current view starts from | |
442 | void wxScrolledWindow::ViewStart (int *x, int *y) const | |
443 | { | |
139adb6a RR |
444 | *x = m_xScrollPosition; |
445 | *y = m_yScrollPosition; | |
c801d85f KB |
446 | } |
447 | ||
debe6624 | 448 | void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 449 | { |
139adb6a RR |
450 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; |
451 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f KB |
452 | } |
453 | ||
debe6624 | 454 | void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const |
c801d85f | 455 | { |
139adb6a RR |
456 | *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine); |
457 | *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine); | |
c801d85f KB |
458 | } |
459 | ||
460 |