]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
the notebook now has wxTAB_TRAVERSAL style
[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 int w, h;
242 GetClientSize(&w, &h);
243
244 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
245 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
246 if (noPositions < 0)
247 noPositions = 0;
248
249 if ( (m_xScrollPosition + nScrollInc) < 0 )
250 nScrollInc = -m_xScrollPosition; // As -ve as we can go
251 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
252 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
253
254 return nScrollInc;
255 }
256 else
257 {
258 int w, h;
259 GetClientSize(&w, &h);
260
261 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
262 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
263 if (noPositions < 0)
264 noPositions = 0;
265
266 if ( (m_yScrollPosition + nScrollInc) < 0 )
267 nScrollInc = -m_yScrollPosition; // As -ve as we can go
268 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
269 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
270
271 return nScrollInc;
272 }
273 }
274
275 // Adjust the scrollbars - new version.
276 void wxScrolledWindow::AdjustScrollbars(void)
277 {
278 int w, h;
279 GetClientSize(&w, &h);
280
281 // Recalculate scroll bar range and position
282 if (m_xScrollLines > 0)
283 {
284 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
285 int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
286 if (newRange < 0)
287 newRange = 0;
288
289 m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
290
291 // Calculate page size i.e. number of scroll units you get on the
292 // current client window
293 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
294 if (noPagePositions < 1)
295 noPagePositions = 1;
296
297 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
298 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
299 }
300 // Robert Roebling
301 else
302 {
303 m_xScrollPosition = 0;
304 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
305 }
306
307 if (m_yScrollLines > 0)
308 {
309 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
310 int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
311 if (newRange < 0)
312 newRange = 0;
313
314 m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
315
316 // Calculate page size i.e. number of scroll units you get on the
317 // current client window
318 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
319 if (noPagePositions < 1)
320 noPagePositions = 1;
321
322 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
323 SetScrollPageSize(wxVERTICAL, noPagePositions);
324 }
325 else
326 {
327 m_yScrollPosition = 0;
328 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE); // Robert Roebling
329 }
330
331 }
332
333 // Default OnSize resets scrollbars, if any
334 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
335 {
336 #if USE_CONSTRAINTS
337 if (GetAutoLayout())
338 Layout();
339 #endif
340
341 AdjustScrollbars();
342 }
343
344 // This calls OnDraw, having adjusted the origin according to the current
345 // scroll position
346 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
347 {
348 wxPaintDC dc(this);
349 PrepareDC(dc);
350
351 OnDraw(dc);
352 }
353
354 // Override this function if you don't want to have wxScrolledWindow
355 // automatically change the origin according to the scroll position.
356 void wxScrolledWindow::PrepareDC(wxDC& dc)
357 {
358 dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
359 dc.SetUserScale(m_scaleX, m_scaleY);
360 }
361
362 #if WXWIN_COMPATIBILITY
363 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
364 {
365 *x_page = GetScrollPageSize(wxHORIZONTAL);
366 *y_page = GetScrollPageSize(wxVERTICAL);
367 }
368 #endif
369
370 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
371 {
372 *x_unit = m_xScrollPixelsPerLine;
373 *y_unit = m_yScrollPixelsPerLine;
374 }
375
376 int wxScrolledWindow::GetScrollPageSize(int orient) const
377 {
378 if ( orient == wxHORIZONTAL )
379 return m_xScrollLinesPerPage;
380 else
381 return m_yScrollLinesPerPage;
382 }
383
384 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
385 {
386 if ( orient == wxHORIZONTAL )
387 m_xScrollLinesPerPage = pageSize;
388 else
389 m_yScrollLinesPerPage = pageSize;
390 }
391
392 /*
393 * Scroll to given position (scroll position, not pixel position)
394 */
395 void wxScrolledWindow::Scroll (int x_pos, int y_pos)
396 {
397 int old_x, old_y;
398 ViewStart (&old_x, &old_y);
399 if (((x_pos == -1) || (x_pos == old_x)) && ((y_pos == -1) || (y_pos == old_y)))
400 return;
401
402 if (x_pos > -1)
403 {
404 m_xScrollPosition = x_pos;
405 SetScrollPos (wxHORIZONTAL, x_pos, TRUE);
406 }
407 if (y_pos > -1)
408 {
409 m_yScrollPosition = y_pos;
410 SetScrollPos (wxVERTICAL, y_pos, TRUE);
411 }
412 Refresh();
413 #ifdef __WXMSW__
414 ::UpdateWindow ((HWND) GetHWND());
415 #endif
416 }
417
418 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
419 {
420 m_xScrollingEnabled = x_scroll;
421 m_yScrollingEnabled = y_scroll;
422 }
423
424 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
425 {
426 *x = m_xScrollPixelsPerLine * m_xScrollLines;
427 *y = m_yScrollPixelsPerLine * m_yScrollLines;
428 }
429
430 // Where the current view starts from
431 void wxScrolledWindow::ViewStart (int *x, int *y) const
432 {
433 *x = m_xScrollPosition;
434 *y = m_yScrollPosition;
435 }
436
437 void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
438 {
439 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
440 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
441 }
442
443 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
444 {
445 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
446 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
447 }
448
449