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"
30 #if !USE_SHARED_LIBRARY
31 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
32 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
33 EVT_SIZE(wxScrolledWindow::OnSize
)
34 EVT_PAINT(wxScrolledWindow::OnPaint
)
37 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
45 // For wxRETAINED implementation
49 wxScrolledWindow::wxScrolledWindow()
51 m_xScrollPixelsPerLine
= 0;
52 m_yScrollPixelsPerLine
= 0;
53 m_xScrollingEnabled
= TRUE
;
54 m_yScrollingEnabled
= TRUE
;
55 m_xScrollPosition
= 0;
56 m_yScrollPosition
= 0;
59 m_xScrollLinesPerPage
= 0;
60 m_yScrollLinesPerPage
= 0;
65 bool wxScrolledWindow::Create(wxWindow
*parent
, wxWindowID id
,
71 m_xScrollPixelsPerLine
= 0;
72 m_yScrollPixelsPerLine
= 0;
73 m_xScrollingEnabled
= TRUE
;
74 m_yScrollingEnabled
= TRUE
;
75 m_xScrollPosition
= 0;
76 m_yScrollPosition
= 0;
79 m_xScrollLinesPerPage
= 0;
80 m_yScrollLinesPerPage
= 0;
84 return wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
88 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
89 * noUnitsX/noUnitsY: : no. units per scrollbar
91 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
92 int noUnitsX
, int noUnitsY
,
93 int xPos
, int yPos
, bool noRefresh
)
97 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
98 (noUnitsX
< m_xScrollLines
) ||
99 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
100 (noUnitsY
< m_yScrollLines
) ||
101 (xPos
!= m_xScrollPosition
) ||
102 (yPos
!= m_yScrollPosition
) ||
103 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
104 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
107 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
108 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
109 m_xScrollPosition
= xPos
;
110 m_yScrollPosition
= yPos
;
111 m_xScrollLines
= noUnitsX
;
112 m_yScrollLines
= noUnitsY
;
115 // Sorry, some Motif-specific code to implement a backing pixmap
116 // for the wxRETAINED style. Implementing a backing store can't
117 // be entirely generic because it relies on the wxWindowDC implementation
118 // to duplicate X drawing calls for the backing pixmap.
120 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
122 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
124 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
125 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
126 if (m_backingPixmap
&&
127 !((m_pixmapWidth
== totalPixelWidth
) &&
128 (m_pixmapHeight
== totalPixelHeight
)))
130 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
131 m_backingPixmap
= (WXPixmap
) 0;
134 if (!m_backingPixmap
&&
135 (noUnitsX
!= 0) && (noUnitsY
!= 0))
137 int depth
= wxDisplayDepth();
138 m_pixmapWidth
= totalPixelWidth
;
139 m_pixmapHeight
= totalPixelHeight
;
140 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
141 m_pixmapWidth
, m_pixmapHeight
, depth
);
149 if (do_refresh
&& !noRefresh
)
154 UpdateWindow ((HWND
) GetHWND());
158 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
160 int orient
= event
.GetOrientation();
162 int nScrollInc
= CalcScrollInc(event
);
163 if (nScrollInc
== 0) return;
165 if (orient
== wxHORIZONTAL
)
167 int newPos
= m_xScrollPosition
+ nScrollInc
;
168 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
172 int newPos
= m_yScrollPosition
+ nScrollInc
;
173 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
176 if (orient
== wxHORIZONTAL
)
178 m_xScrollPosition
+= nScrollInc
;
182 m_yScrollPosition
+= nScrollInc
;
185 if (orient
== wxHORIZONTAL
)
187 if (m_xScrollingEnabled
)
188 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
194 if (m_yScrollingEnabled
)
195 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
201 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
203 int pos
= event
.GetPosition();
204 int orient
= event
.GetOrientation();
207 switch (event
.GetEventType())
209 case wxEVT_SCROLLWIN_TOP
:
211 if (orient
== wxHORIZONTAL
)
212 nScrollInc
= - m_xScrollPosition
;
214 nScrollInc
= - m_yScrollPosition
;
217 case wxEVT_SCROLLWIN_BOTTOM
:
219 if (orient
== wxHORIZONTAL
)
220 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
222 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
225 case wxEVT_SCROLLWIN_LINEUP
:
230 case wxEVT_SCROLLWIN_LINEDOWN
:
235 case wxEVT_SCROLLWIN_PAGEUP
:
237 if (orient
== wxHORIZONTAL
)
238 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
240 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
243 case wxEVT_SCROLLWIN_PAGEDOWN
:
245 if (orient
== wxHORIZONTAL
)
246 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
248 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
251 case wxEVT_SCROLLWIN_THUMBTRACK
:
253 if (orient
== wxHORIZONTAL
)
254 nScrollInc
= pos
- m_xScrollPosition
;
256 nScrollInc
= pos
- m_yScrollPosition
;
265 if (orient
== wxHORIZONTAL
)
267 if (m_xScrollPixelsPerLine
> 0) {
269 GetClientSize(&w
, &h
);
271 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
272 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
276 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
277 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
278 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
279 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
286 if (m_yScrollPixelsPerLine
> 0) {
288 GetClientSize(&w
, &h
);
290 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
291 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
295 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
296 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
297 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
298 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
307 // Adjust the scrollbars - new version.
308 void wxScrolledWindow::AdjustScrollbars(void)
311 GetClientSize(&w
, &h
);
313 if (m_xScrollLines
> 0)
315 // Calculate page size i.e. number of scroll units you get on the
316 // current client window
317 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
318 if (noPagePositions
< 1) noPagePositions
= 1;
320 // Correct position if greater than extent of canvas minus
321 // the visible portion of it or if below zero
322 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
323 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
325 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
326 // The amount by which we scroll when paging
327 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
331 m_xScrollPosition
= 0;
332 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
335 if (m_yScrollLines
> 0)
337 // Calculate page size i.e. number of scroll units you get on the
338 // current client window
339 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
340 if (noPagePositions
< 1) noPagePositions
= 1;
342 // Correct position if greater than extent of canvas minus
343 // the visible portion of it or if below zero
344 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
345 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
347 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
348 // The amount by which we scroll when paging
349 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
353 m_yScrollPosition
= 0;
354 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
358 // Default OnSize resets scrollbars, if any
359 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
361 #if wxUSE_CONSTRAINTS
362 if (GetAutoLayout()) Layout();
368 // This calls OnDraw, having adjusted the origin according to the current
370 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
378 // Override this function if you don't want to have wxScrolledWindow
379 // automatically change the origin according to the scroll position.
380 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
382 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
383 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
384 dc
.SetUserScale( m_scaleX
, m_scaleY
);
387 #if WXWIN_COMPATIBILITY
388 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
390 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
391 *y_page
= GetScrollPageSize(wxVERTICAL
);
394 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
397 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
399 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
401 #endif // WXWIN_COMPATIBILITY
403 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
406 *x_unit
= m_xScrollPixelsPerLine
;
408 *y_unit
= m_yScrollPixelsPerLine
;
411 int wxScrolledWindow::GetScrollPageSize(int orient
) const
413 if ( orient
== wxHORIZONTAL
)
414 return m_xScrollLinesPerPage
;
416 return m_yScrollLinesPerPage
;
419 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
421 if ( orient
== wxHORIZONTAL
)
422 m_xScrollLinesPerPage
= pageSize
;
424 m_yScrollLinesPerPage
= pageSize
;
428 * Scroll to given position (scroll position, not pixel position)
430 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
432 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
433 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
436 GetClientSize(&w
, &h
);
440 m_xScrollPosition
= x_pos
;
442 // Calculate page size i.e. number of scroll units you get on the
443 // current client window
444 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
445 if (noPagePositions
< 1) noPagePositions
= 1;
447 // Correct position if greater than extent of canvas minus
448 // the visible portion of it or if below zero
449 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
450 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
452 SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
456 m_yScrollPosition
= y_pos
;
458 // Calculate page size i.e. number of scroll units you get on the
459 // current client window
460 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
461 if (noPagePositions
< 1) noPagePositions
= 1;
463 // Correct position if greater than extent of canvas minus
464 // the visible portion of it or if below zero
465 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
466 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
468 SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
471 // BAD, BAD, can cause event loops if called from OnPaint(). KB.
476 ::UpdateWindow ((HWND
) GetHWND());
480 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
482 m_xScrollingEnabled
= x_scroll
;
483 m_yScrollingEnabled
= y_scroll
;
486 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
489 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
491 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
494 // Where the current view starts from
495 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
498 *x
= m_xScrollPosition
;
500 *y
= m_yScrollPosition
;
503 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
506 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
508 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
511 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
514 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
516 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;