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_CHAR(wxScrolledWindow::OnChar
)
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 // we need wxWANTS_CHARS to process arrows ourselves
113 return wxPanel::Create(parent
, id
, pos
, size
, style
| wxWANTS_CHARS
, name
);
116 wxScrolledWindow::~wxScrolledWindow()
120 // ----------------------------------------------------------------------------
121 // setting scrolling parameters
122 // ----------------------------------------------------------------------------
125 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
126 * noUnitsX/noUnitsY: : no. units per scrollbar
128 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
129 int noUnitsX
, int noUnitsY
,
130 int xPos
, int yPos
, bool noRefresh
)
134 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
137 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
138 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
140 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
141 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
142 (xPos
!= m_xScrollPosition
) ||
143 (yPos
!= m_yScrollPosition
)
144 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
145 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
148 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
149 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
150 m_xScrollPosition
= xPos
;
151 m_yScrollPosition
= yPos
;
152 m_xScrollLines
= noUnitsX
;
153 m_yScrollLines
= noUnitsY
;
156 // Sorry, some Motif-specific code to implement a backing pixmap
157 // for the wxRETAINED style. Implementing a backing store can't
158 // be entirely generic because it relies on the wxWindowDC implementation
159 // to duplicate X drawing calls for the backing pixmap.
161 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
163 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
165 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
166 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
167 if (m_backingPixmap
&&
168 !((m_pixmapWidth
== totalPixelWidth
) &&
169 (m_pixmapHeight
== totalPixelHeight
)))
171 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
172 m_backingPixmap
= (WXPixmap
) 0;
175 if (!m_backingPixmap
&&
176 (noUnitsX
!= 0) && (noUnitsY
!= 0))
178 int depth
= wxDisplayDepth();
179 m_pixmapWidth
= totalPixelWidth
;
180 m_pixmapHeight
= totalPixelHeight
;
181 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
182 m_pixmapWidth
, m_pixmapHeight
, depth
);
190 if (do_refresh
&& !noRefresh
)
191 m_targetWindow
->Refresh();
194 // GRG: if this turns out to be really necessary, we could
195 // at least move it to the above if { ... } so that it is
196 // only done if noRefresh = FALSE (the default). OTOH, if
197 // this doesn't break anything, which seems to be the
198 // case, we could just leave it out.
201 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
204 m_targetWindow
->MacUpdateImmediately() ;
208 // ----------------------------------------------------------------------------
209 // target window handling
210 // ----------------------------------------------------------------------------
212 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
214 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
215 m_targetWindow
= target
;
218 wxWindow
*wxScrolledWindow::GetTargetWindow()
220 return m_targetWindow
;
223 // ----------------------------------------------------------------------------
224 // scrolling implementation itself
225 // ----------------------------------------------------------------------------
227 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
229 int orient
= event
.GetOrientation();
231 int nScrollInc
= CalcScrollInc(event
);
232 if (nScrollInc
== 0) return;
234 if (orient
== wxHORIZONTAL
)
236 int newPos
= m_xScrollPosition
+ nScrollInc
;
237 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
241 int newPos
= m_yScrollPosition
+ nScrollInc
;
242 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
245 if (orient
== wxHORIZONTAL
)
247 m_xScrollPosition
+= nScrollInc
;
251 m_yScrollPosition
+= nScrollInc
;
254 if (orient
== wxHORIZONTAL
)
256 if (m_xScrollingEnabled
)
257 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
259 m_targetWindow
->Refresh();
263 if (m_yScrollingEnabled
)
264 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
266 m_targetWindow
->Refresh();
269 m_targetWindow
->MacUpdateImmediately() ;
273 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
275 int pos
= event
.GetPosition();
276 int orient
= event
.GetOrientation();
279 switch (event
.GetEventType())
281 case wxEVT_SCROLLWIN_TOP
:
283 if (orient
== wxHORIZONTAL
)
284 nScrollInc
= - m_xScrollPosition
;
286 nScrollInc
= - m_yScrollPosition
;
289 case wxEVT_SCROLLWIN_BOTTOM
:
291 if (orient
== wxHORIZONTAL
)
292 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
294 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
297 case wxEVT_SCROLLWIN_LINEUP
:
302 case wxEVT_SCROLLWIN_LINEDOWN
:
307 case wxEVT_SCROLLWIN_PAGEUP
:
309 if (orient
== wxHORIZONTAL
)
310 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
312 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
315 case wxEVT_SCROLLWIN_PAGEDOWN
:
317 if (orient
== wxHORIZONTAL
)
318 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
320 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
323 case wxEVT_SCROLLWIN_THUMBTRACK
:
324 case wxEVT_SCROLLWIN_THUMBRELEASE
:
326 if (orient
== wxHORIZONTAL
)
327 nScrollInc
= pos
- m_xScrollPosition
;
329 nScrollInc
= pos
- m_yScrollPosition
;
338 if (orient
== wxHORIZONTAL
)
340 if (m_xScrollPixelsPerLine
> 0)
343 m_targetWindow
->GetClientSize(&w
, &h
);
345 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
346 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
350 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
351 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
352 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
353 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
356 m_targetWindow
->Refresh();
360 if (m_yScrollPixelsPerLine
> 0)
363 m_targetWindow
->GetClientSize(&w
, &h
);
365 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
366 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
370 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
371 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
372 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
373 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
376 m_targetWindow
->Refresh();
382 // Adjust the scrollbars - new version.
383 void wxScrolledWindow::AdjustScrollbars()
386 m_targetWindow
->GetClientSize(&w
, &h
);
388 int oldXScroll
= m_xScrollPosition
;
389 int oldYScroll
= m_yScrollPosition
;
391 if (m_xScrollLines
> 0)
393 // Calculate page size i.e. number of scroll units you get on the
394 // current client window
395 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
396 if (noPagePositions
< 1) noPagePositions
= 1;
398 // Correct position if greater than extent of canvas minus
399 // the visible portion of it or if below zero
400 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
401 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
403 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
404 // The amount by which we scroll when paging
405 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
409 m_xScrollPosition
= 0;
410 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
413 if (m_yScrollLines
> 0)
415 // Calculate page size i.e. number of scroll units you get on the
416 // current client window
417 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
418 if (noPagePositions
< 1) noPagePositions
= 1;
420 // Correct position if greater than extent of canvas minus
421 // the visible portion of it or if below zero
422 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
423 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
425 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
426 // The amount by which we scroll when paging
427 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
431 m_yScrollPosition
= 0;
432 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
435 if (oldXScroll
!= m_xScrollPosition
)
437 if (m_xScrollingEnabled
)
438 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
440 m_targetWindow
->Refresh();
443 if (oldYScroll
!= m_yScrollPosition
)
445 if (m_yScrollingEnabled
)
446 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
448 m_targetWindow
->Refresh();
452 // Override this function if you don't want to have wxScrolledWindow
453 // automatically change the origin according to the scroll position.
454 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
456 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
457 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
458 dc
.SetUserScale( m_scaleX
, m_scaleY
);
461 #if WXWIN_COMPATIBILITY
462 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
464 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
465 *y_page
= GetScrollPageSize(wxVERTICAL
);
468 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
471 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
473 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
475 #endif // WXWIN_COMPATIBILITY
477 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
480 *x_unit
= m_xScrollPixelsPerLine
;
482 *y_unit
= m_yScrollPixelsPerLine
;
485 int wxScrolledWindow::GetScrollPageSize(int orient
) const
487 if ( orient
== wxHORIZONTAL
)
488 return m_xScrollLinesPerPage
;
490 return m_yScrollLinesPerPage
;
493 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
495 if ( orient
== wxHORIZONTAL
)
496 m_xScrollLinesPerPage
= pageSize
;
498 m_yScrollLinesPerPage
= pageSize
;
502 * Scroll to given position (scroll position, not pixel position)
504 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
506 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
507 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
510 m_targetWindow
->GetClientSize(&w
, &h
);
514 int old_x
= m_xScrollPosition
;
515 m_xScrollPosition
= x_pos
;
517 // Calculate page size i.e. number of scroll units you get on the
518 // current client window
519 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
520 if (noPagePositions
< 1) noPagePositions
= 1;
522 // Correct position if greater than extent of canvas minus
523 // the visible portion of it or if below zero
524 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
525 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
527 if (old_x
== m_xScrollPosition
) return;
529 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
530 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
534 int old_y
= m_yScrollPosition
;
535 m_yScrollPosition
= y_pos
;
537 // Calculate page size i.e. number of scroll units you get on the
538 // current client window
539 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
540 if (noPagePositions
< 1) noPagePositions
= 1;
542 // Correct position if greater than extent of canvas minus
543 // the visible portion of it or if below zero
544 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
545 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
547 if (old_y
== m_yScrollPosition
) return;
549 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
550 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
554 m_targetWindow
->MacUpdateImmediately();
558 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
560 m_xScrollingEnabled
= x_scroll
;
561 m_yScrollingEnabled
= y_scroll
;
564 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
567 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
569 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
572 // Where the current view starts from
573 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
576 *x
= m_xScrollPosition
;
578 *y
= m_yScrollPosition
;
581 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
584 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
586 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
589 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
592 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
594 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
601 // Default OnSize resets scrollbars, if any
602 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
604 #if wxUSE_CONSTRAINTS
612 // This calls OnDraw, having adjusted the origin according to the current
614 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
622 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
623 // compatibility here - if we used OnKeyDown(), the programs which process
624 // arrows themselves in their OnChar() would never get the message and like
625 // this they always have the priority
626 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
628 int stx
, sty
, // view origin
629 szx
, szy
, // view size (total)
630 clix
, cliy
; // view size (on screen)
632 ViewStart(&stx
, &sty
);
633 GetClientSize(&clix
, &cliy
);
634 GetVirtualSize(&szx
, &szy
);
636 if( m_xScrollPixelsPerLine
)
638 clix
/= m_xScrollPixelsPerLine
;
639 szx
/= m_xScrollPixelsPerLine
;
646 if( m_yScrollPixelsPerLine
)
648 cliy
/= m_yScrollPixelsPerLine
;
649 szy
/= m_yScrollPixelsPerLine
;
657 switch ( event
.KeyCode() )
661 Scroll(-1, sty
- (5 * cliy
/ 6));
666 Scroll(-1, sty
+ (5 * cliy
/ 6));
670 Scroll(0, event
.ControlDown() ? 0 : -1);
674 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);