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"
31 #include "wx/generic/scrolwin.h"
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
)
40 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxWindow
)
43 wxScrolledWindow::wxScrolledWindow(void)
45 m_xScrollPixelsPerLine
= 0;
46 m_yScrollPixelsPerLine
= 0;
47 m_xScrollingEnabled
= TRUE
;
48 m_yScrollingEnabled
= TRUE
;
49 m_xScrollPosition
= 0;
50 m_yScrollPosition
= 0;
53 m_xScrollLinesPerPage
= 0;
54 m_yScrollLinesPerPage
= 0;
57 bool wxScrolledWindow::Create(wxWindow
*parent
, wxWindowID id
,
63 m_xScrollPixelsPerLine
= 0;
64 m_yScrollPixelsPerLine
= 0;
65 m_xScrollingEnabled
= TRUE
;
66 m_yScrollingEnabled
= TRUE
;
67 m_xScrollPosition
= 0;
68 m_yScrollPosition
= 0;
71 m_xScrollLinesPerPage
= 0;
72 m_yScrollLinesPerPage
= 0;
74 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
78 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
79 * noUnitsX/noUnitsY: : no. units per scrollbar
81 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
82 int noUnitsX
, int noUnitsY
,
83 int xPos
, int yPos
, bool noRefresh
)
87 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
88 (noUnitsX
< m_xScrollPosition
) ||
89 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
90 (noUnitsY
< m_yScrollPosition
) ||
91 (xPos
!= m_xScrollPosition
) ||
92 (yPos
!= m_yScrollPosition
) ||
93 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
94 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
97 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
98 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
99 m_xScrollPosition
= xPos
;
100 m_yScrollPosition
= yPos
;
101 m_xScrollLines
= noUnitsX
;
102 m_yScrollLines
= noUnitsY
;
106 if (do_refresh
&& !noRefresh
) Refresh();
109 UpdateWindow ((HWND
) GetHWND());
113 void wxScrolledWindow::OnScroll(wxScrollEvent
& event
)
115 int orient
= event
.GetOrientation();
117 int nScrollInc
= CalcScrollInc(event
);
121 // TODO: should we store the scroll position here as well as in wxWindow?
122 if (orient
== wxHORIZONTAL
)
124 int newPos
= m_xScrollPosition
+ nScrollInc
;
125 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
129 int newPos
= m_yScrollPosition
+ nScrollInc
;
130 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
134 // TODO We need to multiply the ScrollWindow amount by the scaling
135 // factor, but how do we know what this is in wxWin 2.0???
139 if ( this->IsKindOf(CLASSINFO(wxCanvas)) )
141 wxDC* dc = ((wxCanvas *)this)->GetDC();
142 dc->GetUserScale(&scaleX, &scaleY);
146 if (orient
== wxHORIZONTAL
)
148 m_xScrollPosition
+= nScrollInc
;
152 m_yScrollPosition
+= nScrollInc
;
155 if (orient
== wxHORIZONTAL
)
157 if (m_xScrollingEnabled
)
158 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
164 if (m_yScrollingEnabled
)
165 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
172 int wxScrolledWindow::CalcScrollInc(wxScrollEvent
& event
)
174 int pos
= event
.GetPosition();
175 int orient
= event
.GetOrientation();
178 switch (event
.GetEventType())
180 case wxEVENT_TYPE_SCROLL_TOP
:
182 if (orient
== wxHORIZONTAL
)
183 nScrollInc
= - m_xScrollPosition
;
185 nScrollInc
= - m_yScrollPosition
;
188 case wxEVENT_TYPE_SCROLL_BOTTOM
:
190 if (orient
== wxHORIZONTAL
)
191 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
193 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
196 case wxEVENT_TYPE_SCROLL_LINEUP
:
201 case wxEVENT_TYPE_SCROLL_LINEDOWN
:
206 case wxEVENT_TYPE_SCROLL_PAGEUP
:
208 if (orient
== wxHORIZONTAL
)
209 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
211 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
214 case wxEVENT_TYPE_SCROLL_PAGEDOWN
:
216 if (orient
== wxHORIZONTAL
)
217 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
219 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
222 case wxEVENT_TYPE_SCROLL_THUMBTRACK
:
224 if (orient
== wxHORIZONTAL
)
225 nScrollInc
= pos
- m_xScrollPosition
;
227 nScrollInc
= pos
- m_yScrollPosition
;
235 if (orient
== wxHORIZONTAL
)
238 GetClientSize(&w
, &h
);
240 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
241 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
245 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
246 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
247 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
248 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
255 GetClientSize(&w
, &h
);
257 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
258 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
262 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
263 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
264 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
265 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
271 // Adjust the scrollbars - new version.
272 void wxScrolledWindow::AdjustScrollbars(void)
275 GetClientSize(&w
, &h
);
277 // Recalculate scroll bar range and position
278 if (m_xScrollLines
> 0)
280 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
281 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
285 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
287 // Calculate page size i.e. number of scroll units you get on the
288 // current client window
289 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
290 if (noPagePositions
< 1)
293 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
294 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
299 m_xScrollPosition
= 0;
300 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
303 if (m_yScrollLines
> 0)
305 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
306 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
310 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
312 // Calculate page size i.e. number of scroll units you get on the
313 // current client window
314 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
315 if (noPagePositions
< 1)
318 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
319 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
323 m_yScrollPosition
= 0;
324 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
); // Robert Roebling
329 // Default OnSize resets scrollbars, if any
330 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
340 // This calls OnDraw, having adjusted the origin according to the current
342 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
350 // Override this function if you don't want to have wxScrolledWindow
351 // automatically change the origin according to the scroll position.
352 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
354 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
357 #if WXWIN_COMPATIBILITY
358 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
360 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
361 *y_page
= GetScrollPageSize(wxVERTICAL
);
365 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
367 *x_unit
= m_xScrollPixelsPerLine
;
368 *y_unit
= m_yScrollPixelsPerLine
;
371 int wxScrolledWindow::GetScrollPageSize(int orient
) const
373 if ( orient
== wxHORIZONTAL
)
374 return m_xScrollLinesPerPage
;
376 return m_yScrollLinesPerPage
;
379 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
381 if ( orient
== wxHORIZONTAL
)
382 m_xScrollLinesPerPage
= pageSize
;
384 m_yScrollLinesPerPage
= pageSize
;
388 * Scroll to given position (scroll position, not pixel position)
390 void wxScrolledWindow::Scroll (int x_pos
, int y_pos
)
393 ViewStart (&old_x
, &old_y
);
394 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
399 m_xScrollPosition
= x_pos
;
400 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
404 m_yScrollPosition
= y_pos
;
405 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
409 ::UpdateWindow ((HWND
) GetHWND());
413 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
415 m_xScrollingEnabled
= x_scroll
;
416 m_yScrollingEnabled
= y_scroll
;
419 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
421 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
422 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
425 // Where the current view starts from
426 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
428 *x
= m_xScrollPosition
;
429 *y
= m_yScrollPosition
;
432 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
434 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
435 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
438 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
440 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
441 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);