1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "scrolwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
34 #include "wx/generic/scrolwin.h"
42 // For wxRETAINED implementation
43 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
44 //This code switches off the compiler warnings
45 # pragma message disable nosimpint
49 # pragma message enable nosimpint
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
58 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
59 EVT_SIZE(wxScrolledWindow::OnSize
)
60 EVT_PAINT(wxScrolledWindow::OnPaint
)
61 EVT_KEY_DOWN(wxScrolledWindow::OnKeyDown
)
64 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxScrolledWindow creation
72 // ----------------------------------------------------------------------------
74 wxScrolledWindow::wxScrolledWindow()
76 m_xScrollPixelsPerLine
= 0;
77 m_yScrollPixelsPerLine
= 0;
78 m_xScrollingEnabled
= TRUE
;
79 m_yScrollingEnabled
= TRUE
;
80 m_xScrollPosition
= 0;
81 m_yScrollPosition
= 0;
84 m_xScrollLinesPerPage
= 0;
85 m_yScrollLinesPerPage
= 0;
90 bool wxScrolledWindow::Create(wxWindow
*parent
,
97 m_xScrollPixelsPerLine
= 0;
98 m_yScrollPixelsPerLine
= 0;
99 m_xScrollingEnabled
= TRUE
;
100 m_yScrollingEnabled
= TRUE
;
101 m_xScrollPosition
= 0;
102 m_yScrollPosition
= 0;
105 m_xScrollLinesPerPage
= 0;
106 m_yScrollLinesPerPage
= 0;
110 m_targetWindow
= this;
112 return wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
115 wxScrolledWindow::~wxScrolledWindow()
119 // ----------------------------------------------------------------------------
120 // setting scrolling parameters
121 // ----------------------------------------------------------------------------
124 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
125 * noUnitsX/noUnitsY: : no. units per scrollbar
127 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
128 int noUnitsX
, int noUnitsY
,
129 int xPos
, int yPos
, bool noRefresh
)
133 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
134 (noUnitsX
< m_xScrollLines
) ||
135 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
136 (noUnitsY
< m_yScrollLines
) ||
137 (xPos
!= m_xScrollPosition
) ||
138 (yPos
!= m_yScrollPosition
) ||
139 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
140 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
143 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
144 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
145 m_xScrollPosition
= xPos
;
146 m_yScrollPosition
= yPos
;
147 m_xScrollLines
= noUnitsX
;
148 m_yScrollLines
= noUnitsY
;
151 // Sorry, some Motif-specific code to implement a backing pixmap
152 // for the wxRETAINED style. Implementing a backing store can't
153 // be entirely generic because it relies on the wxWindowDC implementation
154 // to duplicate X drawing calls for the backing pixmap.
156 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
158 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
160 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
161 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
162 if (m_backingPixmap
&&
163 !((m_pixmapWidth
== totalPixelWidth
) &&
164 (m_pixmapHeight
== totalPixelHeight
)))
166 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
167 m_backingPixmap
= (WXPixmap
) 0;
170 if (!m_backingPixmap
&&
171 (noUnitsX
!= 0) && (noUnitsY
!= 0))
173 int depth
= wxDisplayDepth();
174 m_pixmapWidth
= totalPixelWidth
;
175 m_pixmapHeight
= totalPixelHeight
;
176 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
177 m_pixmapWidth
, m_pixmapHeight
, depth
);
185 if (do_refresh
&& !noRefresh
)
186 m_targetWindow
->Refresh();
189 // GRG: if this turns out to be really necessary, we could
190 // at least move it to the above if { ... } so that it is
191 // only done if noRefresh = FALSE (the default). OTOH, if
192 // this doesn't break anything, which seems to be the
193 // case, we could just leave it out.
196 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
199 m_targetWindow
->MacUpdateImmediately() ;
203 // ----------------------------------------------------------------------------
204 // target window handling
205 // ----------------------------------------------------------------------------
207 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
209 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
210 m_targetWindow
= target
;
213 wxWindow
*wxScrolledWindow::GetTargetWindow()
215 return m_targetWindow
;
218 // ----------------------------------------------------------------------------
219 // scrolling implementation itself
220 // ----------------------------------------------------------------------------
222 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
224 int orient
= event
.GetOrientation();
226 int nScrollInc
= CalcScrollInc(event
);
227 if (nScrollInc
== 0) return;
229 if (orient
== wxHORIZONTAL
)
231 int newPos
= m_xScrollPosition
+ nScrollInc
;
232 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
236 int newPos
= m_yScrollPosition
+ nScrollInc
;
237 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
240 if (orient
== wxHORIZONTAL
)
242 m_xScrollPosition
+= nScrollInc
;
246 m_yScrollPosition
+= nScrollInc
;
249 if (orient
== wxHORIZONTAL
)
251 if (m_xScrollingEnabled
)
252 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
254 m_targetWindow
->Refresh();
258 if (m_yScrollingEnabled
)
259 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
261 m_targetWindow
->Refresh();
264 m_targetWindow
->MacUpdateImmediately() ;
268 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
270 int pos
= event
.GetPosition();
271 int orient
= event
.GetOrientation();
274 switch (event
.GetEventType())
276 case wxEVT_SCROLLWIN_TOP
:
278 if (orient
== wxHORIZONTAL
)
279 nScrollInc
= - m_xScrollPosition
;
281 nScrollInc
= - m_yScrollPosition
;
284 case wxEVT_SCROLLWIN_BOTTOM
:
286 if (orient
== wxHORIZONTAL
)
287 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
289 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
292 case wxEVT_SCROLLWIN_LINEUP
:
297 case wxEVT_SCROLLWIN_LINEDOWN
:
302 case wxEVT_SCROLLWIN_PAGEUP
:
304 if (orient
== wxHORIZONTAL
)
305 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
307 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
310 case wxEVT_SCROLLWIN_PAGEDOWN
:
312 if (orient
== wxHORIZONTAL
)
313 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
315 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
318 case wxEVT_SCROLLWIN_THUMBTRACK
:
320 if (orient
== wxHORIZONTAL
)
321 nScrollInc
= pos
- m_xScrollPosition
;
323 nScrollInc
= pos
- m_yScrollPosition
;
332 if (orient
== wxHORIZONTAL
)
334 if (m_xScrollPixelsPerLine
> 0)
337 m_targetWindow
->GetClientSize(&w
, &h
);
339 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
340 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
344 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
345 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
346 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
347 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
350 m_targetWindow
->Refresh();
354 if (m_yScrollPixelsPerLine
> 0)
357 m_targetWindow
->GetClientSize(&w
, &h
);
359 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
360 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
364 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
365 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
366 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
367 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
370 m_targetWindow
->Refresh();
376 // Adjust the scrollbars - new version.
377 void wxScrolledWindow::AdjustScrollbars()
380 m_targetWindow
->GetClientSize(&w
, &h
);
382 int oldXScroll
= m_xScrollPosition
;
383 int oldYScroll
= m_yScrollPosition
;
385 if (m_xScrollLines
> 0)
387 // Calculate page size i.e. number of scroll units you get on the
388 // current client window
389 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
390 if (noPagePositions
< 1) noPagePositions
= 1;
392 // Correct position if greater than extent of canvas minus
393 // the visible portion of it or if below zero
394 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
395 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
397 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
398 // The amount by which we scroll when paging
399 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
403 m_xScrollPosition
= 0;
404 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
407 if (m_yScrollLines
> 0)
409 // Calculate page size i.e. number of scroll units you get on the
410 // current client window
411 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
412 if (noPagePositions
< 1) noPagePositions
= 1;
414 // Correct position if greater than extent of canvas minus
415 // the visible portion of it or if below zero
416 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
417 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
419 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
420 // The amount by which we scroll when paging
421 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
425 m_yScrollPosition
= 0;
426 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
429 if (oldXScroll
!= m_xScrollPosition
)
431 if (m_xScrollingEnabled
)
432 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
434 m_targetWindow
->Refresh();
437 if (oldYScroll
!= m_yScrollPosition
)
439 if (m_yScrollingEnabled
)
440 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
442 m_targetWindow
->Refresh();
446 // Override this function if you don't want to have wxScrolledWindow
447 // automatically change the origin according to the scroll position.
448 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
450 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
451 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
452 dc
.SetUserScale( m_scaleX
, m_scaleY
);
455 #if WXWIN_COMPATIBILITY
456 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
458 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
459 *y_page
= GetScrollPageSize(wxVERTICAL
);
462 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
465 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
467 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
469 #endif // WXWIN_COMPATIBILITY
471 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
474 *x_unit
= m_xScrollPixelsPerLine
;
476 *y_unit
= m_yScrollPixelsPerLine
;
479 int wxScrolledWindow::GetScrollPageSize(int orient
) const
481 if ( orient
== wxHORIZONTAL
)
482 return m_xScrollLinesPerPage
;
484 return m_yScrollLinesPerPage
;
487 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
489 if ( orient
== wxHORIZONTAL
)
490 m_xScrollLinesPerPage
= pageSize
;
492 m_yScrollLinesPerPage
= pageSize
;
496 * Scroll to given position (scroll position, not pixel position)
498 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
500 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
501 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
504 m_targetWindow
->GetClientSize(&w
, &h
);
508 int old_x
= m_xScrollPosition
;
509 m_xScrollPosition
= x_pos
;
511 // Calculate page size i.e. number of scroll units you get on the
512 // current client window
513 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
514 if (noPagePositions
< 1) noPagePositions
= 1;
516 // Correct position if greater than extent of canvas minus
517 // the visible portion of it or if below zero
518 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
519 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
521 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
523 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
527 int old_y
= m_yScrollPosition
;
528 m_yScrollPosition
= y_pos
;
530 // Calculate page size i.e. number of scroll units you get on the
531 // current client window
532 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
533 if (noPagePositions
< 1) noPagePositions
= 1;
535 // Correct position if greater than extent of canvas minus
536 // the visible portion of it or if below zero
537 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
538 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
540 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
542 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
547 m_targetWindow
->MacUpdateImmediately() ;
551 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
553 m_xScrollingEnabled
= x_scroll
;
554 m_yScrollingEnabled
= y_scroll
;
557 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
560 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
562 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
565 // Where the current view starts from
566 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
569 *x
= m_xScrollPosition
;
571 *y
= m_yScrollPosition
;
574 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
577 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
579 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
582 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
585 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
587 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
594 // Default OnSize resets scrollbars, if any
595 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
597 #if wxUSE_CONSTRAINTS
605 // This calls OnDraw, having adjusted the origin according to the current
607 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
616 void wxScrolledWindow::OnKeyDown(wxKeyEvent
& event
)
618 int stx
, sty
, // view origin
619 szx
, szy
, // view size (total)
620 clix
, cliy
; // view size (on screen)
622 ViewStart(&stx
, &sty
);
623 GetClientSize(&clix
, &cliy
);
624 clix
/= m_xScrollPixelsPerLine
;
625 cliy
/= m_yScrollPixelsPerLine
;
626 GetVirtualSize(&szx
, &szy
);
627 szx
/= m_xScrollPixelsPerLine
;
628 szy
/= m_yScrollPixelsPerLine
;
630 switch ( event
.KeyCode() )
634 Scroll(-1, sty
- (5 * cliy
/ 6));
639 Scroll(-1, sty
+ (5 * cliy
/ 6));
642 // notice that handling of Ctrl-Home/End is asymmetrical: Home goes to
643 // the beginning of the current line, Ctrl-Home returns to the origin
644 // while End goes to the bottom without changing horizontal position
645 // and only Ctrl-End does go to the most rightmost position as well
647 Scroll(0, event
.ControlDown() ? 0 : -1);
651 Scroll(event
.ControlDown() ? szx
- clix
: -1, szy
- cliy
);