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