]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
14 #pragma implementation "scrolwin.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
21 #include "wx/dcclient.h"
27 #include "wx/generic/scrolwin.h"
29 #if !USE_SHARED_LIBRARY
30 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxWindow
)
31 EVT_SCROLL(wxScrolledWindow::OnScroll
)
32 EVT_SIZE(wxScrolledWindow::OnSize
)
33 EVT_PAINT(wxScrolledWindow::OnPaint
)
36 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxWindow
)
44 // For wxRETAINED implementation
48 wxScrolledWindow::wxScrolledWindow(void)
50 m_xScrollPixelsPerLine
= 0;
51 m_yScrollPixelsPerLine
= 0;
52 m_xScrollingEnabled
= TRUE
;
53 m_yScrollingEnabled
= TRUE
;
54 m_xScrollPosition
= 0;
55 m_yScrollPosition
= 0;
58 m_xScrollLinesPerPage
= 0;
59 m_yScrollLinesPerPage
= 0;
64 bool wxScrolledWindow::Create(wxWindow
*parent
, wxWindowID id
,
70 m_xScrollPixelsPerLine
= 0;
71 m_yScrollPixelsPerLine
= 0;
72 m_xScrollingEnabled
= TRUE
;
73 m_yScrollingEnabled
= TRUE
;
74 m_xScrollPosition
= 0;
75 m_yScrollPosition
= 0;
78 m_xScrollLinesPerPage
= 0;
79 m_yScrollLinesPerPage
= 0;
83 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
87 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
88 * noUnitsX/noUnitsY: : no. units per scrollbar
90 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
91 int noUnitsX
, int noUnitsY
,
92 int xPos
, int yPos
, bool noRefresh
)
96 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
97 (noUnitsX
< m_xScrollPosition
) ||
98 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
99 (noUnitsY
< m_yScrollPosition
) ||
100 (xPos
!= m_xScrollPosition
) ||
101 (yPos
!= m_yScrollPosition
) ||
102 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
103 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
106 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
107 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
108 m_xScrollPosition
= xPos
;
109 m_yScrollPosition
= yPos
;
110 m_xScrollLines
= noUnitsX
;
111 m_yScrollLines
= noUnitsY
;
114 // Sorry, some Motif-specific code to implement a backing pixmap
115 // for the wxRETAINED style. Implementing a backing store can't
116 // be entirely generic because it relies on the wxWindowDC implementation
117 // to duplicate X drawing calls for the backing pixmap.
119 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
121 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
123 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
124 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
125 if (m_backingPixmap
&&
126 !((m_pixmapWidth
== totalPixelWidth
) &&
127 (m_pixmapHeight
== totalPixelHeight
)))
129 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
130 m_backingPixmap
= (WXPixmap
) 0;
133 if (!m_backingPixmap
&&
134 (noUnitsX
!= 0) && (noUnitsY
!= 0))
136 int depth
= wxDisplayDepth();
137 m_pixmapWidth
= totalPixelWidth
;
138 m_pixmapHeight
= totalPixelHeight
;
139 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
140 m_pixmapWidth
, m_pixmapHeight
, depth
);
148 if (do_refresh
&& !noRefresh
) Refresh();
152 UpdateWindow ((HWND
) GetHWND());
156 void wxScrolledWindow::OnScroll(wxScrollEvent
& event
)
158 int orient
= event
.GetOrientation();
160 int nScrollInc
= CalcScrollInc(event
);
161 if (nScrollInc
== 0) return;
163 if (orient
== wxHORIZONTAL
)
165 int newPos
= m_xScrollPosition
+ nScrollInc
;
166 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
170 int newPos
= m_yScrollPosition
+ nScrollInc
;
171 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
174 if (orient
== wxHORIZONTAL
)
176 m_xScrollPosition
+= nScrollInc
;
180 m_yScrollPosition
+= nScrollInc
;
183 if (orient
== wxHORIZONTAL
)
185 if (m_xScrollingEnabled
)
186 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
192 if (m_yScrollingEnabled
)
193 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
199 int wxScrolledWindow::CalcScrollInc(wxScrollEvent
& event
)
201 int pos
= event
.GetPosition();
202 int orient
= event
.GetOrientation();
205 switch (event
.GetEventType())
207 case wxEVT_SCROLL_TOP
:
209 if (orient
== wxHORIZONTAL
)
210 nScrollInc
= - m_xScrollPosition
;
212 nScrollInc
= - m_yScrollPosition
;
215 case wxEVT_SCROLL_BOTTOM
:
217 if (orient
== wxHORIZONTAL
)
218 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
220 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
223 case wxEVT_SCROLL_LINEUP
:
228 case wxEVT_SCROLL_LINEDOWN
:
233 case wxEVT_SCROLL_PAGEUP
:
235 if (orient
== wxHORIZONTAL
)
236 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
238 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
241 case wxEVT_SCROLL_PAGEDOWN
:
243 if (orient
== wxHORIZONTAL
)
244 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
246 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
249 case wxEVT_SCROLL_THUMBTRACK
:
251 if (orient
== wxHORIZONTAL
)
252 nScrollInc
= pos
- m_xScrollPosition
;
254 nScrollInc
= pos
- m_yScrollPosition
;
263 if (orient
== wxHORIZONTAL
)
265 if (m_xScrollPixelsPerLine
> 0) {
267 GetClientSize(&w
, &h
);
269 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
270 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
274 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
275 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
276 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
277 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
284 if (m_yScrollPixelsPerLine
> 0) {
286 GetClientSize(&w
, &h
);
288 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
289 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
293 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
294 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
295 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
296 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
305 // Adjust the scrollbars - new version.
306 void wxScrolledWindow::AdjustScrollbars(void)
309 GetClientSize(&w
, &h
);
311 if (m_xScrollLines
> 0)
313 // Calculate page size i.e. number of scroll units you get on the
314 // current client window
315 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
316 if (noPagePositions
< 1) noPagePositions
= 1;
318 // Correct position if greater than extent of canvas minus
319 // the visible portion of it or if below zero
320 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
321 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
323 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
324 // The amount by which we scroll when paging
325 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
329 m_xScrollPosition
= 0;
330 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
333 if (m_yScrollLines
> 0)
335 // Calculate page size i.e. number of scroll units you get on the
336 // current client window
337 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
338 if (noPagePositions
< 1) noPagePositions
= 1;
340 // Correct position if greater than extent of canvas minus
341 // the visible portion of it or if below zero
342 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
343 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
345 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
346 // The amount by which we scroll when paging
347 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
351 m_yScrollPosition
= 0;
352 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
356 // Default OnSize resets scrollbars, if any
357 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
359 #if wxUSE_CONSTRAINTS
360 if (GetAutoLayout()) Layout();
366 // This calls OnDraw, having adjusted the origin according to the current
368 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
376 // Override this function if you don't want to have wxScrolledWindow
377 // automatically change the origin according to the scroll position.
378 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
380 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
381 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
382 dc
.SetUserScale( m_scaleX
, m_scaleY
);
385 #if WXWIN_COMPATIBILITY
386 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
388 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
389 *y_page
= GetScrollPageSize(wxVERTICAL
);
393 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
395 *x_unit
= m_xScrollPixelsPerLine
;
396 *y_unit
= m_yScrollPixelsPerLine
;
399 int wxScrolledWindow::GetScrollPageSize(int orient
) const
401 if ( orient
== wxHORIZONTAL
)
402 return m_xScrollLinesPerPage
;
404 return m_yScrollLinesPerPage
;
407 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
409 if ( orient
== wxHORIZONTAL
)
410 m_xScrollLinesPerPage
= pageSize
;
412 m_yScrollLinesPerPage
= pageSize
;
416 * Scroll to given position (scroll position, not pixel position)
418 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
420 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
421 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
424 GetClientSize(&w
, &h
);
428 m_xScrollPosition
= x_pos
;
430 // Calculate page size i.e. number of scroll units you get on the
431 // current client window
432 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
433 if (noPagePositions
< 1) noPagePositions
= 1;
435 // Correct position if greater than extent of canvas minus
436 // the visible portion of it or if below zero
437 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
438 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
440 SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
444 m_yScrollPosition
= y_pos
;
446 // Calculate page size i.e. number of scroll units you get on the
447 // current client window
448 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
449 if (noPagePositions
< 1) noPagePositions
= 1;
451 // Correct position if greater than extent of canvas minus
452 // the visible portion of it or if below zero
453 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
454 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
456 SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
463 ::UpdateWindow ((HWND
) GetHWND());
467 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
469 m_xScrollingEnabled
= x_scroll
;
470 m_yScrollingEnabled
= y_scroll
;
473 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
475 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
476 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
479 // Where the current view starts from
480 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
482 *x
= m_xScrollPosition
;
483 *y
= m_yScrollPosition
;
486 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
488 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
489 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
492 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
494 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
495 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);