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 m_targetWindow
= this;
86 return wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
90 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
91 * noUnitsX/noUnitsY: : no. units per scrollbar
93 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
94 int noUnitsX
, int noUnitsY
,
95 int xPos
, int yPos
, bool noRefresh
)
99 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
100 (noUnitsX
< m_xScrollLines
) ||
101 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
102 (noUnitsY
< m_yScrollLines
) ||
103 (xPos
!= m_xScrollPosition
) ||
104 (yPos
!= m_yScrollPosition
) ||
105 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
106 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
109 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
110 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
111 m_xScrollPosition
= xPos
;
112 m_yScrollPosition
= yPos
;
113 m_xScrollLines
= noUnitsX
;
114 m_yScrollLines
= noUnitsY
;
117 // Sorry, some Motif-specific code to implement a backing pixmap
118 // for the wxRETAINED style. Implementing a backing store can't
119 // be entirely generic because it relies on the wxWindowDC implementation
120 // to duplicate X drawing calls for the backing pixmap.
122 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
124 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
126 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
127 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
128 if (m_backingPixmap
&&
129 !((m_pixmapWidth
== totalPixelWidth
) &&
130 (m_pixmapHeight
== totalPixelHeight
)))
132 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
133 m_backingPixmap
= (WXPixmap
) 0;
136 if (!m_backingPixmap
&&
137 (noUnitsX
!= 0) && (noUnitsY
!= 0))
139 int depth
= wxDisplayDepth();
140 m_pixmapWidth
= totalPixelWidth
;
141 m_pixmapHeight
= totalPixelHeight
;
142 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
143 m_pixmapWidth
, m_pixmapHeight
, depth
);
151 if (do_refresh
&& !noRefresh
)
152 m_targetWindow
->Refresh();
156 UpdateWindow ((HWND
) GetHWND());
160 wxScrolledWindow::~wxScrolledWindow()
164 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
166 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
167 m_targetWindow
= target
;
170 wxWindow
*wxScrolledWindow::GetTargetWindow()
172 return m_targetWindow
;
175 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
177 int orient
= event
.GetOrientation();
179 int nScrollInc
= CalcScrollInc(event
);
180 if (nScrollInc
== 0) return;
182 if (orient
== wxHORIZONTAL
)
184 int newPos
= m_xScrollPosition
+ nScrollInc
;
185 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
189 int newPos
= m_yScrollPosition
+ nScrollInc
;
190 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
193 if (orient
== wxHORIZONTAL
)
195 m_xScrollPosition
+= nScrollInc
;
199 m_yScrollPosition
+= nScrollInc
;
202 if (orient
== wxHORIZONTAL
)
204 if (m_xScrollingEnabled
)
205 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
207 m_targetWindow
->Refresh();
211 if (m_yScrollingEnabled
)
212 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
214 m_targetWindow
->Refresh();
218 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
220 int pos
= event
.GetPosition();
221 int orient
= event
.GetOrientation();
224 switch (event
.GetEventType())
226 case wxEVT_SCROLLWIN_TOP
:
228 if (orient
== wxHORIZONTAL
)
229 nScrollInc
= - m_xScrollPosition
;
231 nScrollInc
= - m_yScrollPosition
;
234 case wxEVT_SCROLLWIN_BOTTOM
:
236 if (orient
== wxHORIZONTAL
)
237 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
239 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
242 case wxEVT_SCROLLWIN_LINEUP
:
247 case wxEVT_SCROLLWIN_LINEDOWN
:
252 case wxEVT_SCROLLWIN_PAGEUP
:
254 if (orient
== wxHORIZONTAL
)
255 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
257 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
260 case wxEVT_SCROLLWIN_PAGEDOWN
:
262 if (orient
== wxHORIZONTAL
)
263 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
265 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
268 case wxEVT_SCROLLWIN_THUMBTRACK
:
270 if (orient
== wxHORIZONTAL
)
271 nScrollInc
= pos
- m_xScrollPosition
;
273 nScrollInc
= pos
- m_yScrollPosition
;
282 if (orient
== wxHORIZONTAL
)
284 if (m_xScrollPixelsPerLine
> 0)
287 m_targetWindow
->GetClientSize(&w
, &h
);
289 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
290 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
294 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
295 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
296 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
297 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
300 m_targetWindow
->Refresh();
304 if (m_yScrollPixelsPerLine
> 0)
307 m_targetWindow
->GetClientSize(&w
, &h
);
309 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
310 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
314 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
315 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
316 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
317 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
320 m_targetWindow
->Refresh();
326 // Adjust the scrollbars - new version.
327 void wxScrolledWindow::AdjustScrollbars()
330 m_targetWindow
->GetClientSize(&w
, &h
);
332 int oldXScroll
= m_xScrollPosition
;
333 int oldYScroll
= m_yScrollPosition
;
335 if (m_xScrollLines
> 0)
337 // Calculate page size i.e. number of scroll units you get on the
338 // current client window
339 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 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_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
345 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
347 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
348 // The amount by which we scroll when paging
349 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
353 m_xScrollPosition
= 0;
354 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
357 if (m_yScrollLines
> 0)
359 // Calculate page size i.e. number of scroll units you get on the
360 // current client window
361 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
362 if (noPagePositions
< 1) noPagePositions
= 1;
364 // Correct position if greater than extent of canvas minus
365 // the visible portion of it or if below zero
366 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
367 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
369 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
370 // The amount by which we scroll when paging
371 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
375 m_yScrollPosition
= 0;
376 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
379 if (oldXScroll
!= m_xScrollPosition
)
381 if (m_xScrollingEnabled
)
382 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
384 m_targetWindow
->Refresh();
387 if (oldYScroll
!= m_yScrollPosition
)
389 if (m_yScrollingEnabled
)
390 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
392 m_targetWindow
->Refresh();
396 // Default OnSize resets scrollbars, if any
397 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
399 #if wxUSE_CONSTRAINTS
400 if (GetAutoLayout()) Layout();
406 // This calls OnDraw, having adjusted the origin according to the current
408 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
416 // Override this function if you don't want to have wxScrolledWindow
417 // automatically change the origin according to the scroll position.
418 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
420 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
421 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
422 dc
.SetUserScale( m_scaleX
, m_scaleY
);
425 #if WXWIN_COMPATIBILITY
426 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
428 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
429 *y_page
= GetScrollPageSize(wxVERTICAL
);
432 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
435 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
437 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
439 #endif // WXWIN_COMPATIBILITY
441 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
444 *x_unit
= m_xScrollPixelsPerLine
;
446 *y_unit
= m_yScrollPixelsPerLine
;
449 int wxScrolledWindow::GetScrollPageSize(int orient
) const
451 if ( orient
== wxHORIZONTAL
)
452 return m_xScrollLinesPerPage
;
454 return m_yScrollLinesPerPage
;
457 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
459 if ( orient
== wxHORIZONTAL
)
460 m_xScrollLinesPerPage
= pageSize
;
462 m_yScrollLinesPerPage
= pageSize
;
466 * Scroll to given position (scroll position, not pixel position)
468 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
470 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
471 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
474 m_targetWindow
->GetClientSize(&w
, &h
);
478 int old_x
= m_xScrollPosition
;
479 m_xScrollPosition
= x_pos
;
481 // Calculate page size i.e. number of scroll units you get on the
482 // current client window
483 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
484 if (noPagePositions
< 1) noPagePositions
= 1;
486 // Correct position if greater than extent of canvas minus
487 // the visible portion of it or if below zero
488 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
489 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
491 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
493 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
497 int old_y
= m_yScrollPosition
;
498 m_yScrollPosition
= y_pos
;
500 // Calculate page size i.e. number of scroll units you get on the
501 // current client window
502 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
503 if (noPagePositions
< 1) noPagePositions
= 1;
505 // Correct position if greater than extent of canvas minus
506 // the visible portion of it or if below zero
507 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
508 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
510 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
512 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
517 // ::UpdateWindow ((HWND) GetHWND());
523 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
525 m_xScrollingEnabled
= x_scroll
;
526 m_yScrollingEnabled
= y_scroll
;
529 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
532 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
534 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
537 // Where the current view starts from
538 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
541 *x
= m_xScrollPosition
;
543 *y
= m_yScrollPosition
;
546 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
549 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
551 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
554 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
557 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
559 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;