]>
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
)
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();
114 UpdateWindow ((HWND
) GetHWND());
118 void wxScrolledWindow::OnScroll(wxScrollEvent
& event
)
120 int orient
= event
.GetOrientation();
122 int nScrollInc
= CalcScrollInc(event
);
123 if (nScrollInc
== 0) return;
125 if (orient
== wxHORIZONTAL
)
127 int newPos
= m_xScrollPosition
+ nScrollInc
;
128 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
132 int newPos
= m_yScrollPosition
+ nScrollInc
;
133 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
136 if (orient
== wxHORIZONTAL
)
138 m_xScrollPosition
+= nScrollInc
;
142 m_yScrollPosition
+= nScrollInc
;
145 if (orient
== wxHORIZONTAL
)
147 if (m_xScrollingEnabled
)
148 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
154 if (m_yScrollingEnabled
)
155 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
161 int wxScrolledWindow::CalcScrollInc(wxScrollEvent
& event
)
163 int pos
= event
.GetPosition();
164 int orient
= event
.GetOrientation();
167 switch (event
.GetEventType())
169 case wxEVT_SCROLL_TOP
:
171 if (orient
== wxHORIZONTAL
)
172 nScrollInc
= - m_xScrollPosition
;
174 nScrollInc
= - m_yScrollPosition
;
177 case wxEVT_SCROLL_BOTTOM
:
179 if (orient
== wxHORIZONTAL
)
180 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
182 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
185 case wxEVT_SCROLL_LINEUP
:
190 case wxEVT_SCROLL_LINEDOWN
:
195 case wxEVT_SCROLL_PAGEUP
:
197 if (orient
== wxHORIZONTAL
)
198 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
200 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
203 case wxEVT_SCROLL_PAGEDOWN
:
205 if (orient
== wxHORIZONTAL
)
206 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
208 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
211 case wxEVT_SCROLL_THUMBTRACK
:
213 if (orient
== wxHORIZONTAL
)
214 nScrollInc
= pos
- m_xScrollPosition
;
216 nScrollInc
= pos
- m_yScrollPosition
;
225 if (orient
== wxHORIZONTAL
)
227 if (m_xScrollPixelsPerLine
> 0) {
229 GetClientSize(&w
, &h
);
231 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
232 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
236 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
237 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
238 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
239 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
246 if (m_yScrollPixelsPerLine
> 0) {
248 GetClientSize(&w
, &h
);
250 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
251 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
255 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
256 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
257 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
258 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
267 // Adjust the scrollbars - new version.
268 void wxScrolledWindow::AdjustScrollbars(void)
271 GetClientSize(&w
, &h
);
273 if (m_xScrollLines
> 0)
275 // Calculate page size i.e. number of scroll units you get on the
276 // current client window
277 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
278 if (noPagePositions
< 1) noPagePositions
= 1;
280 // Correct position if greater than extent of canvas minus
281 // the visible portion of it or if below zero
282 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
283 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
285 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
286 // The amount by which we scroll when paging
287 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
291 m_xScrollPosition
= 0;
292 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
295 if (m_yScrollLines
> 0)
297 // Calculate page size i.e. number of scroll units you get on the
298 // current client window
299 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
300 if (noPagePositions
< 1) noPagePositions
= 1;
302 // Correct position if greater than extent of canvas minus
303 // the visible portion of it or if below zero
304 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
305 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
307 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
308 // The amount by which we scroll when paging
309 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
313 m_yScrollPosition
= 0;
314 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
318 // Default OnSize resets scrollbars, if any
319 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
321 #if wxUSE_CONSTRAINTS
322 if (GetAutoLayout()) Layout();
328 // This calls OnDraw, having adjusted the origin according to the current
330 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
338 // Override this function if you don't want to have wxScrolledWindow
339 // automatically change the origin according to the scroll position.
340 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
342 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
343 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
344 dc
.SetUserScale( m_scaleX
, m_scaleY
);
347 #if WXWIN_COMPATIBILITY
348 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
350 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
351 *y_page
= GetScrollPageSize(wxVERTICAL
);
355 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
357 *x_unit
= m_xScrollPixelsPerLine
;
358 *y_unit
= m_yScrollPixelsPerLine
;
361 int wxScrolledWindow::GetScrollPageSize(int orient
) const
363 if ( orient
== wxHORIZONTAL
)
364 return m_xScrollLinesPerPage
;
366 return m_yScrollLinesPerPage
;
369 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
371 if ( orient
== wxHORIZONTAL
)
372 m_xScrollLinesPerPage
= pageSize
;
374 m_yScrollLinesPerPage
= pageSize
;
378 * Scroll to given position (scroll position, not pixel position)
380 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
382 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
383 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
386 GetClientSize(&w
, &h
);
390 m_xScrollPosition
= x_pos
;
392 // Calculate page size i.e. number of scroll units you get on the
393 // current client window
394 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
395 if (noPagePositions
< 1) noPagePositions
= 1;
397 // Correct position if greater than extent of canvas minus
398 // the visible portion of it or if below zero
399 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
400 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
402 SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
406 m_yScrollPosition
= y_pos
;
408 // Calculate page size i.e. number of scroll units you get on the
409 // current client window
410 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
411 if (noPagePositions
< 1) noPagePositions
= 1;
413 // Correct position if greater than extent of canvas minus
414 // the visible portion of it or if below zero
415 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
416 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
418 SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
425 ::UpdateWindow ((HWND
) GetHWND());
429 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
431 m_xScrollingEnabled
= x_scroll
;
432 m_yScrollingEnabled
= y_scroll
;
435 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
437 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
438 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
441 // Where the current view starts from
442 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
444 *x
= m_xScrollPosition
;
445 *y
= m_yScrollPosition
;
448 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
450 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
451 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
454 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
456 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
457 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);