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
) m_targetWindow
->GetHWND());
159 m_targetWindow
->MacUpdateImmediately() ;
163 wxScrolledWindow::~wxScrolledWindow()
167 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
169 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
170 m_targetWindow
= target
;
173 wxWindow
*wxScrolledWindow::GetTargetWindow()
175 return m_targetWindow
;
178 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
180 int orient
= event
.GetOrientation();
182 int nScrollInc
= CalcScrollInc(event
);
183 if (nScrollInc
== 0) return;
185 if (orient
== wxHORIZONTAL
)
187 int newPos
= m_xScrollPosition
+ nScrollInc
;
188 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
192 int newPos
= m_yScrollPosition
+ nScrollInc
;
193 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
196 if (orient
== wxHORIZONTAL
)
198 m_xScrollPosition
+= nScrollInc
;
202 m_yScrollPosition
+= nScrollInc
;
205 if (orient
== wxHORIZONTAL
)
207 if (m_xScrollingEnabled
)
208 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
210 m_targetWindow
->Refresh();
214 if (m_yScrollingEnabled
)
215 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
217 m_targetWindow
->Refresh();
220 m_targetWindow
->MacUpdateImmediately() ;
224 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
226 int pos
= event
.GetPosition();
227 int orient
= event
.GetOrientation();
230 switch (event
.GetEventType())
232 case wxEVT_SCROLLWIN_TOP
:
234 if (orient
== wxHORIZONTAL
)
235 nScrollInc
= - m_xScrollPosition
;
237 nScrollInc
= - m_yScrollPosition
;
240 case wxEVT_SCROLLWIN_BOTTOM
:
242 if (orient
== wxHORIZONTAL
)
243 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
245 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
248 case wxEVT_SCROLLWIN_LINEUP
:
253 case wxEVT_SCROLLWIN_LINEDOWN
:
258 case wxEVT_SCROLLWIN_PAGEUP
:
260 if (orient
== wxHORIZONTAL
)
261 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
263 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
266 case wxEVT_SCROLLWIN_PAGEDOWN
:
268 if (orient
== wxHORIZONTAL
)
269 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
271 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
274 case wxEVT_SCROLLWIN_THUMBTRACK
:
276 if (orient
== wxHORIZONTAL
)
277 nScrollInc
= pos
- m_xScrollPosition
;
279 nScrollInc
= pos
- m_yScrollPosition
;
288 if (orient
== wxHORIZONTAL
)
290 if (m_xScrollPixelsPerLine
> 0)
293 m_targetWindow
->GetClientSize(&w
, &h
);
295 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
296 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
300 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
301 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
302 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
303 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
306 m_targetWindow
->Refresh();
310 if (m_yScrollPixelsPerLine
> 0)
313 m_targetWindow
->GetClientSize(&w
, &h
);
315 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
316 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
320 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
321 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
322 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
323 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
326 m_targetWindow
->Refresh();
332 // Adjust the scrollbars - new version.
333 void wxScrolledWindow::AdjustScrollbars()
336 m_targetWindow
->GetClientSize(&w
, &h
);
338 int oldXScroll
= m_xScrollPosition
;
339 int oldYScroll
= m_yScrollPosition
;
341 if (m_xScrollLines
> 0)
343 // Calculate page size i.e. number of scroll units you get on the
344 // current client window
345 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
346 if (noPagePositions
< 1) noPagePositions
= 1;
348 // Correct position if greater than extent of canvas minus
349 // the visible portion of it or if below zero
350 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
351 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
353 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
354 // The amount by which we scroll when paging
355 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
359 m_xScrollPosition
= 0;
360 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
363 if (m_yScrollLines
> 0)
365 // Calculate page size i.e. number of scroll units you get on the
366 // current client window
367 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
368 if (noPagePositions
< 1) noPagePositions
= 1;
370 // Correct position if greater than extent of canvas minus
371 // the visible portion of it or if below zero
372 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
373 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
375 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
376 // The amount by which we scroll when paging
377 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
381 m_yScrollPosition
= 0;
382 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
385 if (oldXScroll
!= m_xScrollPosition
)
387 if (m_xScrollingEnabled
)
388 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
390 m_targetWindow
->Refresh();
393 if (oldYScroll
!= m_yScrollPosition
)
395 if (m_yScrollingEnabled
)
396 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
398 m_targetWindow
->Refresh();
402 // Default OnSize resets scrollbars, if any
403 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
405 #if wxUSE_CONSTRAINTS
406 if (GetAutoLayout()) Layout();
412 // This calls OnDraw, having adjusted the origin according to the current
414 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
422 // Override this function if you don't want to have wxScrolledWindow
423 // automatically change the origin according to the scroll position.
424 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
426 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
427 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
428 dc
.SetUserScale( m_scaleX
, m_scaleY
);
431 #if WXWIN_COMPATIBILITY
432 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
434 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
435 *y_page
= GetScrollPageSize(wxVERTICAL
);
438 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
441 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
443 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
445 #endif // WXWIN_COMPATIBILITY
447 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
450 *x_unit
= m_xScrollPixelsPerLine
;
452 *y_unit
= m_yScrollPixelsPerLine
;
455 int wxScrolledWindow::GetScrollPageSize(int orient
) const
457 if ( orient
== wxHORIZONTAL
)
458 return m_xScrollLinesPerPage
;
460 return m_yScrollLinesPerPage
;
463 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
465 if ( orient
== wxHORIZONTAL
)
466 m_xScrollLinesPerPage
= pageSize
;
468 m_yScrollLinesPerPage
= pageSize
;
472 * Scroll to given position (scroll position, not pixel position)
474 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
476 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
477 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
480 m_targetWindow
->GetClientSize(&w
, &h
);
484 int old_x
= m_xScrollPosition
;
485 m_xScrollPosition
= x_pos
;
487 // Calculate page size i.e. number of scroll units you get on the
488 // current client window
489 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
490 if (noPagePositions
< 1) noPagePositions
= 1;
492 // Correct position if greater than extent of canvas minus
493 // the visible portion of it or if below zero
494 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
495 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
497 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
499 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
503 int old_y
= m_yScrollPosition
;
504 m_yScrollPosition
= y_pos
;
506 // Calculate page size i.e. number of scroll units you get on the
507 // current client window
508 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
509 if (noPagePositions
< 1) noPagePositions
= 1;
511 // Correct position if greater than extent of canvas minus
512 // the visible portion of it or if below zero
513 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
514 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
516 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
518 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
523 // ::UpdateWindow ((HWND) GetHWND());
528 m_targetWindow
->MacUpdateImmediately() ;
532 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
534 m_xScrollingEnabled
= x_scroll
;
535 m_yScrollingEnabled
= y_scroll
;
538 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
541 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
543 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
546 // Where the current view starts from
547 void wxScrolledWindow::ViewStart (int *x
, int *y
) const
550 *x
= m_xScrollPosition
;
552 *y
= m_yScrollPosition
;
555 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
558 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
560 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
563 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
566 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
568 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;