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
)
241 if (m_xScrollPixelsPerLine
> 0) {
243 GetClientSize(&w
, &h
);
245 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
246 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
250 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
251 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
252 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
253 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
260 if (m_yScrollPixelsPerLine
> 0) {
262 GetClientSize(&w
, &h
);
264 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
265 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
269 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
270 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
271 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
272 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
281 // Adjust the scrollbars - new version.
282 void wxScrolledWindow::AdjustScrollbars(void)
285 GetClientSize(&w
, &h
);
287 // Recalculate scroll bar range and position
288 if (m_xScrollLines
> 0)
290 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
291 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
295 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
297 // Calculate page size i.e. number of scroll units you get on the
298 // current client window
299 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
300 if (noPagePositions
< 1)
303 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
304 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
309 m_xScrollPosition
= 0;
310 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
313 if (m_yScrollLines
> 0)
315 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
316 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
320 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
322 // Calculate page size i.e. number of scroll units you get on the
323 // current client window
324 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
325 if (noPagePositions
< 1)
328 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
329 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
333 m_yScrollPosition
= 0;
334 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
); // Robert Roebling
339 // Default OnSize resets scrollbars, if any
340 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
342 #if wxUSE_CONSTRAINTS
350 // This calls OnDraw, having adjusted the origin according to the current
352 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
360 // Override this function if you don't want to have wxScrolledWindow
361 // automatically change the origin according to the scroll position.
362 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
364 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
365 dc
.SetUserScale(m_scaleX
, m_scaleY
);
368 #if WXWIN_COMPATIBILITY
369 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
371 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
372 *y_page
= GetScrollPageSize(wxVERTICAL
);
376 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
378 *x_unit
= m_xScrollPixelsPerLine
;
379 *y_unit
= m_yScrollPixelsPerLine
;
382 int wxScrolledWindow::GetScrollPageSize(int orient
) const
384 if ( orient
== wxHORIZONTAL
)
385 return m_xScrollLinesPerPage
;
387 return m_yScrollLinesPerPage
;
390 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
392 if ( orient
== wxHORIZONTAL
)
393 m_xScrollLinesPerPage
= pageSize
;
395 m_yScrollLinesPerPage
= pageSize
;
399 * Scroll to given position (scroll position, not pixel position)
401 void wxScrolledWindow::Scroll (int x_pos
, int y_pos
)
404 ViewStart (&old_x
, &old_y
);
405 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
410 m_xScrollPosition
= x_pos
;
411 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
415 m_yScrollPosition
= y_pos
;
416 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
420 ::UpdateWindow ((HWND
) GetHWND());
424 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
426 m_xScrollingEnabled
= x_scroll
;
427 m_yScrollingEnabled
= y_scroll
;
430 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
432 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
433 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
436 // Where the current view starts from
437 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
439 *x
= m_xScrollPosition
;
440 *y
= m_yScrollPosition
;
443 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
445 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
446 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
449 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
451 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
452 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);