]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
Minor doc updates,
[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 #include "wx/panel.h"
29
30 #if !USE_SHARED_LIBRARY
31 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
32 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
33 EVT_SIZE(wxScrolledWindow::OnSize)
34 EVT_PAINT(wxScrolledWindow::OnPaint)
35 END_EVENT_TABLE()
36
37 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
38 #endif
39
40 #ifdef __WXMSW__
41 #include "windows.h"
42 #endif
43
44 #ifdef __WXMOTIF__
45 // For wxRETAINED implementation
46 #include <Xm/Xm.h>
47 #endif
48
49 wxScrolledWindow::wxScrolledWindow()
50 {
51 m_xScrollPixelsPerLine = 0;
52 m_yScrollPixelsPerLine = 0;
53 m_xScrollingEnabled = TRUE;
54 m_yScrollingEnabled = TRUE;
55 m_xScrollPosition = 0;
56 m_yScrollPosition = 0;
57 m_xScrollLines = 0;
58 m_yScrollLines = 0;
59 m_xScrollLinesPerPage = 0;
60 m_yScrollLinesPerPage = 0;
61 m_scaleX = 1.0;
62 m_scaleY = 1.0;
63 }
64
65 bool wxScrolledWindow::Create(wxWindow *parent, wxWindowID id,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 const wxString& name)
70 {
71 m_xScrollPixelsPerLine = 0;
72 m_yScrollPixelsPerLine = 0;
73 m_xScrollingEnabled = TRUE;
74 m_yScrollingEnabled = TRUE;
75 m_xScrollPosition = 0;
76 m_yScrollPosition = 0;
77 m_xScrollLines = 0;
78 m_yScrollLines = 0;
79 m_xScrollLinesPerPage = 0;
80 m_yScrollLinesPerPage = 0;
81 m_scaleX = 1.0;
82 m_scaleY = 1.0;
83
84 return wxPanel::Create(parent, id, pos, size, style, name);
85 }
86
87 /*
88 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
89 * noUnitsX/noUnitsY: : no. units per scrollbar
90 */
91 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
92 int noUnitsX, int noUnitsY,
93 int xPos, int yPos, bool noRefresh )
94 {
95 bool do_refresh =
96 (
97 (noUnitsX != 0 && m_xScrollLines == 0) ||
98 (noUnitsX < m_xScrollLines) ||
99 (noUnitsY != 0 && m_yScrollLines == 0) ||
100 (noUnitsY < m_yScrollLines) ||
101 (xPos != m_xScrollPosition) ||
102 (yPos != m_yScrollPosition) ||
103 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
104 (pixelsPerUnitY != m_yScrollPixelsPerLine)
105 );
106
107 m_xScrollPixelsPerLine = pixelsPerUnitX;
108 m_yScrollPixelsPerLine = pixelsPerUnitY;
109 m_xScrollPosition = xPos;
110 m_yScrollPosition = yPos;
111 m_xScrollLines = noUnitsX;
112 m_yScrollLines = noUnitsY;
113
114 #ifdef __WXMOTIF__
115 // Sorry, some Motif-specific code to implement a backing pixmap
116 // for the wxRETAINED style. Implementing a backing store can't
117 // be entirely generic because it relies on the wxWindowDC implementation
118 // to duplicate X drawing calls for the backing pixmap.
119
120 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
121 {
122 Display* dpy = XtDisplay((Widget) GetMainWidget());
123
124 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
125 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
126 if (m_backingPixmap &&
127 !((m_pixmapWidth == totalPixelWidth) &&
128 (m_pixmapHeight == totalPixelHeight)))
129 {
130 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
131 m_backingPixmap = (WXPixmap) 0;
132 }
133
134 if (!m_backingPixmap &&
135 (noUnitsX != 0) && (noUnitsY != 0))
136 {
137 int depth = wxDisplayDepth();
138 m_pixmapWidth = totalPixelWidth;
139 m_pixmapHeight = totalPixelHeight;
140 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
141 m_pixmapWidth, m_pixmapHeight, depth);
142 }
143
144 }
145 #endif
146
147 AdjustScrollbars();
148
149 if (do_refresh && !noRefresh)
150 Refresh();
151
152 #ifdef __WXMSW__
153 // Necessary?
154 UpdateWindow ((HWND) GetHWND());
155 #endif
156 }
157
158 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
159 {
160 int orient = event.GetOrientation();
161
162 int nScrollInc = CalcScrollInc(event);
163 if (nScrollInc == 0) return;
164
165 if (orient == wxHORIZONTAL)
166 {
167 int newPos = m_xScrollPosition + nScrollInc;
168 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
169 }
170 else
171 {
172 int newPos = m_yScrollPosition + nScrollInc;
173 SetScrollPos(wxVERTICAL, newPos, TRUE );
174 }
175
176 if (orient == wxHORIZONTAL)
177 {
178 m_xScrollPosition += nScrollInc;
179 }
180 else
181 {
182 m_yScrollPosition += nScrollInc;
183 }
184
185 if (orient == wxHORIZONTAL)
186 {
187 if (m_xScrollingEnabled)
188 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
189 else
190 Refresh();
191 }
192 else
193 {
194 if (m_yScrollingEnabled)
195 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
196 else
197 Refresh();
198 }
199 }
200
201 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
202 {
203 int pos = event.GetPosition();
204 int orient = event.GetOrientation();
205
206 int nScrollInc = 0;
207 switch (event.GetEventType())
208 {
209 case wxEVT_SCROLLWIN_TOP:
210 {
211 if (orient == wxHORIZONTAL)
212 nScrollInc = - m_xScrollPosition;
213 else
214 nScrollInc = - m_yScrollPosition;
215 break;
216 }
217 case wxEVT_SCROLLWIN_BOTTOM:
218 {
219 if (orient == wxHORIZONTAL)
220 nScrollInc = m_xScrollLines - m_xScrollPosition;
221 else
222 nScrollInc = m_yScrollLines - m_yScrollPosition;
223 break;
224 }
225 case wxEVT_SCROLLWIN_LINEUP:
226 {
227 nScrollInc = -1;
228 break;
229 }
230 case wxEVT_SCROLLWIN_LINEDOWN:
231 {
232 nScrollInc = 1;
233 break;
234 }
235 case wxEVT_SCROLLWIN_PAGEUP:
236 {
237 if (orient == wxHORIZONTAL)
238 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
239 else
240 nScrollInc = -GetScrollPageSize(wxVERTICAL);
241 break;
242 }
243 case wxEVT_SCROLLWIN_PAGEDOWN:
244 {
245 if (orient == wxHORIZONTAL)
246 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
247 else
248 nScrollInc = GetScrollPageSize(wxVERTICAL);
249 break;
250 }
251 case wxEVT_SCROLLWIN_THUMBTRACK:
252 {
253 if (orient == wxHORIZONTAL)
254 nScrollInc = pos - m_xScrollPosition;
255 else
256 nScrollInc = pos - m_yScrollPosition;
257 break;
258 }
259 default:
260 {
261 break;
262 }
263 }
264
265 if (orient == wxHORIZONTAL)
266 {
267 if (m_xScrollPixelsPerLine > 0) {
268 int w, h;
269 GetClientSize(&w, &h);
270
271 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
272 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
273 if (noPositions < 0)
274 noPositions = 0;
275
276 if ( (m_xScrollPosition + nScrollInc) < 0 )
277 nScrollInc = -m_xScrollPosition; // As -ve as we can go
278 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
279 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
280 }
281 else
282 Refresh();
283 }
284 else
285 {
286 if (m_yScrollPixelsPerLine > 0) {
287 int w, h;
288 GetClientSize(&w, &h);
289
290 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
291 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
292 if (noPositions < 0)
293 noPositions = 0;
294
295 if ( (m_yScrollPosition + nScrollInc) < 0 )
296 nScrollInc = -m_yScrollPosition; // As -ve as we can go
297 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
298 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
299 }
300 else
301 Refresh();
302 }
303
304 return nScrollInc;
305 }
306
307 // Adjust the scrollbars - new version.
308 void wxScrolledWindow::AdjustScrollbars()
309 {
310 int w, h;
311 GetClientSize(&w, &h);
312
313 int oldXScroll = m_xScrollPosition;
314 int oldYScroll = m_yScrollPosition;
315
316 if (m_xScrollLines > 0)
317 {
318 // Calculate page size i.e. number of scroll units you get on the
319 // current client window
320 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
321 if (noPagePositions < 1) noPagePositions = 1;
322
323 // Correct position if greater than extent of canvas minus
324 // the visible portion of it or if below zero
325 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
326 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
327
328 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
329 // The amount by which we scroll when paging
330 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
331 }
332 else
333 {
334 m_xScrollPosition = 0;
335 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
336 }
337
338 if (m_yScrollLines > 0)
339 {
340 // Calculate page size i.e. number of scroll units you get on the
341 // current client window
342 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
343 if (noPagePositions < 1) noPagePositions = 1;
344
345 // Correct position if greater than extent of canvas minus
346 // the visible portion of it or if below zero
347 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
348 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
349
350 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
351 // The amount by which we scroll when paging
352 SetScrollPageSize(wxVERTICAL, noPagePositions);
353 }
354 else
355 {
356 m_yScrollPosition = 0;
357 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
358 }
359
360 if (oldXScroll != m_xScrollPosition)
361 {
362 if (m_xScrollingEnabled)
363 ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
364 else
365 Refresh();
366 }
367
368 if (oldYScroll != m_yScrollPosition)
369 {
370 if (m_yScrollingEnabled)
371 ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
372 else
373 Refresh();
374 }
375 }
376
377 // Default OnSize resets scrollbars, if any
378 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
379 {
380 #if wxUSE_CONSTRAINTS
381 if (GetAutoLayout()) Layout();
382 #endif
383
384 AdjustScrollbars();
385 }
386
387 // This calls OnDraw, having adjusted the origin according to the current
388 // scroll position
389 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
390 {
391 wxPaintDC dc(this);
392 PrepareDC(dc);
393
394 OnDraw(dc);
395 }
396
397 // Override this function if you don't want to have wxScrolledWindow
398 // automatically change the origin according to the scroll position.
399 void wxScrolledWindow::PrepareDC(wxDC& dc)
400 {
401 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
402 -m_yScrollPosition * m_yScrollPixelsPerLine );
403 dc.SetUserScale( m_scaleX, m_scaleY );
404 }
405
406 #if WXWIN_COMPATIBILITY
407 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
408 {
409 *x_page = GetScrollPageSize(wxHORIZONTAL);
410 *y_page = GetScrollPageSize(wxVERTICAL);
411 }
412
413 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
414 {
415 if ( xx )
416 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
417 if ( yy )
418 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
419 }
420 #endif // WXWIN_COMPATIBILITY
421
422 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
423 {
424 if ( x_unit )
425 *x_unit = m_xScrollPixelsPerLine;
426 if ( y_unit )
427 *y_unit = m_yScrollPixelsPerLine;
428 }
429
430 int wxScrolledWindow::GetScrollPageSize(int orient) const
431 {
432 if ( orient == wxHORIZONTAL )
433 return m_xScrollLinesPerPage;
434 else
435 return m_yScrollLinesPerPage;
436 }
437
438 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
439 {
440 if ( orient == wxHORIZONTAL )
441 m_xScrollLinesPerPage = pageSize;
442 else
443 m_yScrollLinesPerPage = pageSize;
444 }
445
446 /*
447 * Scroll to given position (scroll position, not pixel position)
448 */
449 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
450 {
451 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
452 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
453
454 int w, h;
455 GetClientSize(&w, &h);
456
457 if (x_pos != -1)
458 {
459 int old_x = m_xScrollPosition;
460 m_xScrollPosition = x_pos;
461
462 // Calculate page size i.e. number of scroll units you get on the
463 // current client window
464 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
465 if (noPagePositions < 1) noPagePositions = 1;
466
467 // Correct position if greater than extent of canvas minus
468 // the visible portion of it or if below zero
469 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
470 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
471
472 SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
473
474 ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
475 }
476 if (y_pos != -1)
477 {
478 int old_y = m_yScrollPosition;
479 m_yScrollPosition = y_pos;
480
481 // Calculate page size i.e. number of scroll units you get on the
482 // current client window
483 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
484 if (noPagePositions < 1) noPagePositions = 1;
485
486 // Correct position if greater than extent of canvas minus
487 // the visible portion of it or if below zero
488 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
489 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
490
491 SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
492
493 ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
494 }
495
496
497 #ifdef __WXMSW__
498 // ::UpdateWindow ((HWND) GetHWND());
499 #else
500 // Refresh();
501 #endif
502 }
503
504 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
505 {
506 m_xScrollingEnabled = x_scroll;
507 m_yScrollingEnabled = y_scroll;
508 }
509
510 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
511 {
512 if ( x )
513 *x = m_xScrollPixelsPerLine * m_xScrollLines;
514 if ( y )
515 *y = m_yScrollPixelsPerLine * m_yScrollLines;
516 }
517
518 // Where the current view starts from
519 void wxScrolledWindow::ViewStart (int *x, int *y) const
520 {
521 if ( x )
522 *x = m_xScrollPosition;
523 if ( y )
524 *y = m_yScrollPosition;
525 }
526
527 void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
528 {
529 if ( xx )
530 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
531 if ( yy )
532 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
533 }
534
535 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
536 {
537 if ( xx )
538 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
539 if ( yy )
540 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
541 }