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;
59 bool wxScrolledWindow::Create(wxWindow
*parent
, wxWindowID id
,
65 m_xScrollPixelsPerLine
= 0;
66 m_yScrollPixelsPerLine
= 0;
67 m_xScrollingEnabled
= TRUE
;
68 m_yScrollingEnabled
= TRUE
;
69 m_xScrollPosition
= 0;
70 m_yScrollPosition
= 0;
73 m_xScrollLinesPerPage
= 0;
74 m_yScrollLinesPerPage
= 0;
78 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
82 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
83 * noUnitsX/noUnitsY: : no. units per scrollbar
85 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
86 int noUnitsX
, int noUnitsY
,
87 int xPos
, int yPos
, bool noRefresh
)
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
)
101 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
102 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
103 m_xScrollPosition
= xPos
;
104 m_yScrollPosition
= yPos
;
105 m_xScrollLines
= noUnitsX
;
106 m_yScrollLines
= noUnitsY
;
110 if (do_refresh
&& !noRefresh
) Refresh();
113 UpdateWindow ((HWND
) GetHWND());
117 void wxScrolledWindow::OnScroll(wxScrollEvent
& event
)
119 int orient
= event
.GetOrientation();
121 int nScrollInc
= CalcScrollInc(event
);
125 // TODO: should we store the scroll position here as well as in wxWindow?
126 if (orient
== wxHORIZONTAL
)
128 int newPos
= m_xScrollPosition
+ nScrollInc
;
129 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
133 int newPos
= m_yScrollPosition
+ nScrollInc
;
134 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
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???
143 if ( this->IsKindOf(CLASSINFO(wxCanvas)) )
145 wxDC* dc = ((wxCanvas *)this)->GetDC();
146 dc->GetUserScale(&scaleX, &scaleY);
150 if (orient
== wxHORIZONTAL
)
152 m_xScrollPosition
+= nScrollInc
;
156 m_yScrollPosition
+= nScrollInc
;
159 if (orient
== wxHORIZONTAL
)
161 if (m_xScrollingEnabled
)
162 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
168 if (m_yScrollingEnabled
)
169 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
176 int wxScrolledWindow::CalcScrollInc(wxScrollEvent
& event
)
178 int pos
= event
.GetPosition();
179 int orient
= event
.GetOrientation();
182 switch (event
.GetEventType())
184 case wxEVENT_TYPE_SCROLL_TOP
:
186 if (orient
== wxHORIZONTAL
)
187 nScrollInc
= - m_xScrollPosition
;
189 nScrollInc
= - m_yScrollPosition
;
192 case wxEVENT_TYPE_SCROLL_BOTTOM
:
194 if (orient
== wxHORIZONTAL
)
195 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
197 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
200 case wxEVENT_TYPE_SCROLL_LINEUP
:
205 case wxEVENT_TYPE_SCROLL_LINEDOWN
:
210 case wxEVENT_TYPE_SCROLL_PAGEUP
:
212 if (orient
== wxHORIZONTAL
)
213 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
215 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
218 case wxEVENT_TYPE_SCROLL_PAGEDOWN
:
220 if (orient
== wxHORIZONTAL
)
221 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
223 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
226 case wxEVENT_TYPE_SCROLL_THUMBTRACK
:
228 if (orient
== wxHORIZONTAL
)
229 nScrollInc
= pos
- m_xScrollPosition
;
231 nScrollInc
= pos
- m_yScrollPosition
;
239 if (orient
== wxHORIZONTAL
)
242 GetClientSize(&w
, &h
);
244 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
245 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
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
259 GetClientSize(&w
, &h
);
261 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
262 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
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
275 // Adjust the scrollbars - new version.
276 void wxScrolledWindow::AdjustScrollbars(void)
279 GetClientSize(&w
, &h
);
281 // Recalculate scroll bar range and position
282 if (m_xScrollLines
> 0)
284 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
285 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
289 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
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)
297 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
298 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
303 m_xScrollPosition
= 0;
304 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
307 if (m_yScrollLines
> 0)
309 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
310 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
314 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
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)
322 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
323 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
327 m_yScrollPosition
= 0;
328 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
); // Robert Roebling
333 // Default OnSize resets scrollbars, if any
334 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
344 // This calls OnDraw, having adjusted the origin according to the current
346 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
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
)
358 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
359 dc
.SetUserScale(m_scaleX
, m_scaleY
);
362 #if WXWIN_COMPATIBILITY
363 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
365 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
366 *y_page
= GetScrollPageSize(wxVERTICAL
);
370 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
372 *x_unit
= m_xScrollPixelsPerLine
;
373 *y_unit
= m_yScrollPixelsPerLine
;
376 int wxScrolledWindow::GetScrollPageSize(int orient
) const
378 if ( orient
== wxHORIZONTAL
)
379 return m_xScrollLinesPerPage
;
381 return m_yScrollLinesPerPage
;
384 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
386 if ( orient
== wxHORIZONTAL
)
387 m_xScrollLinesPerPage
= pageSize
;
389 m_yScrollLinesPerPage
= pageSize
;
393 * Scroll to given position (scroll position, not pixel position)
395 void wxScrolledWindow::Scroll (int x_pos
, int y_pos
)
398 ViewStart (&old_x
, &old_y
);
399 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
404 m_xScrollPosition
= x_pos
;
405 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
409 m_yScrollPosition
= y_pos
;
410 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
414 ::UpdateWindow ((HWND
) GetHWND());
418 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
420 m_xScrollingEnabled
= x_scroll
;
421 m_yScrollingEnabled
= y_scroll
;
424 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
426 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
427 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
430 // Where the current view starts from
431 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
433 *x
= m_xScrollPosition
;
434 *y
= m_yScrollPosition
;
437 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
439 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
440 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
443 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
445 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
446 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);