]>
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 | ||
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 | |
34 | BEGIN_EVENT_TABLE(wxScrolledWindow, wxWindow) | |
139adb6a RR |
35 | EVT_SCROLL(wxScrolledWindow::OnScroll) |
36 | EVT_SIZE(wxScrolledWindow::OnSize) | |
37 | EVT_PAINT(wxScrolledWindow::OnPaint) | |
c801d85f KB |
38 | END_EVENT_TABLE() |
39 | ||
40 | IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxWindow) | |
41 | #endif | |
42 | ||
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__ |
c801d85f KB |
113 | UpdateWindow ((HWND) GetHWND()); |
114 | #endif | |
115 | } | |
116 | ||
117 | void wxScrolledWindow::OnScroll(wxScrollEvent& event) | |
118 | { | |
139adb6a | 119 | int orient = event.GetOrientation(); |
c801d85f | 120 | |
139adb6a RR |
121 | int nScrollInc = CalcScrollInc(event); |
122 | if (nScrollInc == 0) return; | |
c801d85f KB |
123 | |
124 | // TODO: should we store the scroll position here as well as in wxWindow? | |
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 KB |
135 | |
136 | /* | |
137 | // TODO We need to multiply the ScrollWindow amount by the scaling | |
138 | // factor, but how do we know what this is in wxWin 2.0??? | |
139 | float scaleX = 1.0; | |
140 | float scaleY = 1.0; | |
141 | ||
142 | if ( this->IsKindOf(CLASSINFO(wxCanvas)) ) | |
143 | { | |
144 | wxDC* dc = ((wxCanvas *)this)->GetDC(); | |
145 | dc->GetUserScale(&scaleX, &scaleY); | |
146 | } | |
147 | */ | |
148 | ||
139adb6a RR |
149 | if (orient == wxHORIZONTAL) |
150 | { | |
151 | m_xScrollPosition += nScrollInc; | |
152 | } | |
c801d85f | 153 | else |
139adb6a RR |
154 | { |
155 | m_yScrollPosition += nScrollInc; | |
156 | } | |
157 | ||
158 | if (orient == wxHORIZONTAL) | |
159 | { | |
160 | if (m_xScrollingEnabled) | |
161 | ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL); | |
162 | else | |
163 | Refresh(); | |
164 | } | |
c801d85f | 165 | else |
139adb6a RR |
166 | { |
167 | if (m_yScrollingEnabled) | |
168 | ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL); | |
169 | else | |
170 | Refresh(); | |
c801d85f | 171 | } |
c801d85f KB |
172 | } |
173 | ||
174 | int wxScrolledWindow::CalcScrollInc(wxScrollEvent& event) | |
175 | { | |
176 | int pos = event.GetPosition(); | |
177 | int orient = event.GetOrientation(); | |
178 | ||
179 | int nScrollInc = 0; | |
180 | switch (event.GetEventType()) | |
181 | { | |
f5419957 | 182 | case wxEVT_SCROLL_TOP: |
c801d85f KB |
183 | { |
184 | if (orient == wxHORIZONTAL) | |
185 | nScrollInc = - m_xScrollPosition; | |
186 | else | |
187 | nScrollInc = - m_yScrollPosition; | |
188 | break; | |
189 | } | |
f5419957 | 190 | case wxEVT_SCROLL_BOTTOM: |
c801d85f KB |
191 | { |
192 | if (orient == wxHORIZONTAL) | |
193 | nScrollInc = m_xScrollLines - m_xScrollPosition; | |
194 | else | |
195 | nScrollInc = m_yScrollLines - m_yScrollPosition; | |
196 | break; | |
197 | } | |
f5419957 | 198 | case wxEVT_SCROLL_LINEUP: |
c801d85f KB |
199 | { |
200 | nScrollInc = -1; | |
201 | break; | |
202 | } | |
f5419957 | 203 | case wxEVT_SCROLL_LINEDOWN: |
c801d85f KB |
204 | { |
205 | nScrollInc = 1; | |
206 | break; | |
207 | } | |
f5419957 | 208 | case wxEVT_SCROLL_PAGEUP: |
c801d85f KB |
209 | { |
210 | if (orient == wxHORIZONTAL) | |
211 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); | |
212 | else | |
213 | nScrollInc = -GetScrollPageSize(wxVERTICAL); | |
214 | break; | |
215 | } | |
f5419957 | 216 | case wxEVT_SCROLL_PAGEDOWN: |
c801d85f KB |
217 | { |
218 | if (orient == wxHORIZONTAL) | |
219 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); | |
220 | else | |
221 | nScrollInc = GetScrollPageSize(wxVERTICAL); | |
222 | break; | |
223 | } | |
f5419957 | 224 | case wxEVT_SCROLL_THUMBTRACK: |
c801d85f KB |
225 | { |
226 | if (orient == wxHORIZONTAL) | |
227 | nScrollInc = pos - m_xScrollPosition; | |
228 | else | |
229 | nScrollInc = pos - m_yScrollPosition; | |
230 | break; | |
231 | } | |
232 | default: | |
233 | { | |
234 | break; | |
235 | } | |
236 | } | |
237 | if (orient == wxHORIZONTAL) | |
238 | { | |
9d9355c6 VZ |
239 | if (m_xScrollPixelsPerLine > 0) { |
240 | int w, h; | |
241 | GetClientSize(&w, &h); | |
c801d85f | 242 | |
9d9355c6 VZ |
243 | int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine; |
244 | int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 ); | |
245 | if (noPositions < 0) | |
246 | noPositions = 0; | |
c801d85f | 247 | |
9d9355c6 VZ |
248 | if ( (m_xScrollPosition + nScrollInc) < 0 ) |
249 | nScrollInc = -m_xScrollPosition; // As -ve as we can go | |
250 | else if ( (m_xScrollPosition + nScrollInc) > noPositions ) | |
251 | nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go | |
252 | } | |
253 | else | |
254 | Refresh(); | |
c801d85f KB |
255 | } |
256 | else | |
257 | { | |
9d9355c6 VZ |
258 | if (m_yScrollPixelsPerLine > 0) { |
259 | int w, h; | |
260 | GetClientSize(&w, &h); | |
261 | ||
262 | int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine; | |
263 | int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 ); | |
264 | if (noPositions < 0) | |
265 | noPositions = 0; | |
266 | ||
267 | if ( (m_yScrollPosition + nScrollInc) < 0 ) | |
268 | nScrollInc = -m_yScrollPosition; // As -ve as we can go | |
269 | else if ( (m_yScrollPosition + nScrollInc) > noPositions ) | |
270 | nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go | |
271 | } | |
272 | else | |
273 | Refresh(); | |
c801d85f | 274 | } |
9d9355c6 VZ |
275 | |
276 | return nScrollInc; | |
c801d85f KB |
277 | } |
278 | ||
279 | // Adjust the scrollbars - new version. | |
280 | void wxScrolledWindow::AdjustScrollbars(void) | |
281 | { | |
139adb6a RR |
282 | int w, h; |
283 | GetClientSize(&w, &h); | |
c801d85f | 284 | |
139adb6a RR |
285 | if (m_xScrollLines > 0) |
286 | { | |
c801d85f KB |
287 | // Calculate page size i.e. number of scroll units you get on the |
288 | // current client window | |
139adb6a RR |
289 | int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 ); |
290 | if (noPagePositions < 1) noPagePositions = 1; | |
c801d85f | 291 | |
139adb6a RR |
292 | // Correct position if greater than extent of canvas minus |
293 | // the visible portion of it or if below zero | |
294 | m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition); | |
295 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); | |
c801d85f | 296 | |
139adb6a RR |
297 | SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines); |
298 | // SetScrollPageSize(wxHORIZONTAL, noPagePositions); | |
299 | } | |
300 | else | |
301 | { | |
302 | m_xScrollPosition = 0; | |
303 | SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE); | |
304 | } | |
305 | ||
306 | if (m_yScrollLines > 0) | |
307 | { | |
c801d85f KB |
308 | // Calculate page size i.e. number of scroll units you get on the |
309 | // current client window | |
139adb6a RR |
310 | int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 ); |
311 | if (noPagePositions < 1) noPagePositions = 1; | |
c801d85f | 312 | |
139adb6a RR |
313 | // Correct position if greater than extent of canvas minus |
314 | // the visible portion of it or if below zero | |
315 | m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition ); | |
316 | m_yScrollPosition = wxMax( 0, m_yScrollPosition ); | |
317 | ||
318 | SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines); | |
319 | // SetScrollPageSize(wxVERTICAL, noPagePositions); | |
320 | } | |
321 | else | |
322 | { | |
323 | m_yScrollPosition = 0; | |
324 | SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE); | |
325 | } | |
c801d85f KB |
326 | } |
327 | ||
328 | // Default OnSize resets scrollbars, if any | |
329 | void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event)) | |
330 | { | |
47d67540 | 331 | #if wxUSE_CONSTRAINTS |
139adb6a | 332 | if (GetAutoLayout()) Layout(); |
c801d85f KB |
333 | #endif |
334 | ||
139adb6a | 335 | AdjustScrollbars(); |
c801d85f KB |
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 | { | |
139adb6a RR |
342 | wxPaintDC dc(this); |
343 | PrepareDC(dc); | |
c801d85f | 344 | |
139adb6a | 345 | OnDraw(dc); |
c801d85f KB |
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 | { | |
139adb6a RR |
352 | dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine, |
353 | -m_yScrollPosition * m_yScrollPixelsPerLine ); | |
354 | dc.SetUserScale( m_scaleX, m_scaleY ); | |
c801d85f KB |
355 | } |
356 | ||
357 | #if WXWIN_COMPATIBILITY | |
358 | void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const | |
359 | { | |
360 | *x_page = GetScrollPageSize(wxHORIZONTAL); | |
361 | *y_page = GetScrollPageSize(wxVERTICAL); | |
362 | } | |
363 | #endif | |
364 | ||
365 | void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const | |
366 | { | |
367 | *x_unit = m_xScrollPixelsPerLine; | |
368 | *y_unit = m_yScrollPixelsPerLine; | |
369 | } | |
370 | ||
371 | int wxScrolledWindow::GetScrollPageSize(int orient) const | |
372 | { | |
373 | if ( orient == wxHORIZONTAL ) | |
374 | return m_xScrollLinesPerPage; | |
375 | else | |
376 | return m_yScrollLinesPerPage; | |
377 | } | |
378 | ||
379 | void 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 | */ | |
139adb6a | 390 | void wxScrolledWindow::Scroll( int x_pos, int y_pos ) |
c801d85f | 391 | { |
139adb6a RR |
392 | if (((x_pos == -1) || (x_pos == m_xScrollPosition)) && |
393 | ((y_pos == -1) || (y_pos == m_yScrollPosition))) return; | |
394 | ||
395 | int w, h; | |
396 | GetClientSize(&w, &h); | |
c801d85f | 397 | |
139adb6a | 398 | if (x_pos != -1) |
c801d85f | 399 | { |
139adb6a RR |
400 | m_xScrollPosition = x_pos; |
401 | ||
402 | // Calculate page size i.e. number of scroll units you get on the | |
403 | // current client window | |
404 | int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 ); | |
405 | if (noPagePositions < 1) noPagePositions = 1; | |
406 | ||
407 | // Correct position if greater than extent of canvas minus | |
408 | // the visible portion of it or if below zero | |
409 | m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition ); | |
410 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); | |
411 | ||
412 | SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE ); | |
c801d85f | 413 | } |
139adb6a | 414 | if (y_pos != -1) |
c801d85f | 415 | { |
139adb6a RR |
416 | m_yScrollPosition = y_pos; |
417 | ||
418 | // Calculate page size i.e. number of scroll units you get on the | |
419 | // current client window | |
420 | int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 ); | |
421 | if (noPagePositions < 1) noPagePositions = 1; | |
422 | ||
423 | // Correct position if greater than extent of canvas minus | |
424 | // the visible portion of it or if below zero | |
425 | m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition ); | |
426 | m_yScrollPosition = wxMax( 0, m_yScrollPosition ); | |
427 | ||
428 | SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE ); | |
c801d85f | 429 | } |
139adb6a RR |
430 | |
431 | Refresh(); | |
432 | ||
2049ba38 | 433 | #ifdef __WXMSW__ |
139adb6a | 434 | ::UpdateWindow ((HWND) GetHWND()); |
c801d85f KB |
435 | #endif |
436 | } | |
437 | ||
debe6624 | 438 | void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll) |
c801d85f | 439 | { |
139adb6a RR |
440 | m_xScrollingEnabled = x_scroll; |
441 | m_yScrollingEnabled = y_scroll; | |
c801d85f KB |
442 | } |
443 | ||
444 | void wxScrolledWindow::GetVirtualSize (int *x, int *y) const | |
445 | { | |
139adb6a RR |
446 | *x = m_xScrollPixelsPerLine * m_xScrollLines; |
447 | *y = m_yScrollPixelsPerLine * m_yScrollLines; | |
c801d85f KB |
448 | } |
449 | ||
450 | // Where the current view starts from | |
451 | void wxScrolledWindow::ViewStart (int *x, int *y) const | |
452 | { | |
139adb6a RR |
453 | *x = m_xScrollPosition; |
454 | *y = m_yScrollPosition; | |
c801d85f KB |
455 | } |
456 | ||
debe6624 | 457 | void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 458 | { |
139adb6a RR |
459 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; |
460 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f KB |
461 | } |
462 | ||
debe6624 | 463 | void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const |
c801d85f | 464 | { |
139adb6a RR |
465 | *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine); |
466 | *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine); | |
c801d85f KB |
467 | } |
468 | ||
469 |