1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxGenericScrolledWindow 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"
25 #define XtDisplay XTDISPLAY
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
36 #include "wx/dcclient.h"
38 #include "wx/generic/scrolwin.h"
46 // For wxRETAINED implementation
47 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
48 //This code switches off the compiler warnings
49 # pragma message disable nosimpint
53 # pragma message enable nosimpint
58 #include "wx/scrolwin.h"
59 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
67 EVT_SCROLLWIN(wxGenericScrolledWindow::OnScroll
)
68 EVT_SIZE(wxGenericScrolledWindow::OnSize
)
69 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
70 EVT_CHAR(wxGenericScrolledWindow::OnChar
)
71 EVT_MOUSEWHEEL(wxGenericScrolledWindow::OnMouseWheel
)
74 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // wxGenericScrolledWindow creation
82 // ----------------------------------------------------------------------------
84 wxGenericScrolledWindow::wxGenericScrolledWindow()
86 m_xScrollPixelsPerLine
= 0;
87 m_yScrollPixelsPerLine
= 0;
88 m_xScrollingEnabled
= TRUE
;
89 m_yScrollingEnabled
= TRUE
;
90 m_xScrollPosition
= 0;
91 m_yScrollPosition
= 0;
94 m_xScrollLinesPerPage
= 0;
95 m_yScrollLinesPerPage
= 0;
99 m_targetWindow
= (wxWindow
*) NULL
;
102 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
107 const wxString
& name
)
109 m_xScrollPixelsPerLine
= 0;
110 m_yScrollPixelsPerLine
= 0;
111 m_xScrollingEnabled
= TRUE
;
112 m_yScrollingEnabled
= TRUE
;
113 m_xScrollPosition
= 0;
114 m_yScrollPosition
= 0;
117 m_xScrollLinesPerPage
= 0;
118 m_yScrollLinesPerPage
= 0;
123 m_targetWindow
= this;
125 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
128 // we need to process arrows ourselves for scrolling
129 m_lDlgCode
|= DLGC_WANTARROWS
;
135 wxGenericScrolledWindow::~wxGenericScrolledWindow()
139 // ----------------------------------------------------------------------------
140 // setting scrolling parameters
141 // ----------------------------------------------------------------------------
144 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
145 * noUnitsX/noUnitsY: : no. units per scrollbar
147 void wxGenericScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
148 int noUnitsX
, int noUnitsY
,
149 int xPos
, int yPos
, bool noRefresh
)
153 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
156 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
157 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
159 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
160 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
161 (xPos
!= m_xScrollPosition
) ||
162 (yPos
!= m_yScrollPosition
)
163 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
164 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
167 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
168 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
169 m_xScrollPosition
= xPos
;
170 m_yScrollPosition
= yPos
;
171 m_xScrollLines
= noUnitsX
;
172 m_yScrollLines
= noUnitsY
;
175 // Sorry, some Motif-specific code to implement a backing pixmap
176 // for the wxRETAINED style. Implementing a backing store can't
177 // be entirely generic because it relies on the wxWindowDC implementation
178 // to duplicate X drawing calls for the backing pixmap.
180 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
182 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
184 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
185 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
186 if (m_backingPixmap
&&
187 !((m_pixmapWidth
== totalPixelWidth
) &&
188 (m_pixmapHeight
== totalPixelHeight
)))
190 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
191 m_backingPixmap
= (WXPixmap
) 0;
194 if (!m_backingPixmap
&&
195 (noUnitsX
!= 0) && (noUnitsY
!= 0))
197 int depth
= wxDisplayDepth();
198 m_pixmapWidth
= totalPixelWidth
;
199 m_pixmapHeight
= totalPixelHeight
;
200 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
201 m_pixmapWidth
, m_pixmapHeight
, depth
);
209 if (do_refresh
&& !noRefresh
)
210 m_targetWindow
->Refresh();
213 // GRG: if this turns out to be really necessary, we could
214 // at least move it to the above if { ... } so that it is
215 // only done if noRefresh = FALSE (the default). OTOH, if
216 // this doesn't break anything, which seems to be the
217 // case, we could just leave it out.
220 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
223 m_targetWindow
->MacUpdateImmediately() ;
227 // ----------------------------------------------------------------------------
228 // target window handling
229 // ----------------------------------------------------------------------------
231 void wxGenericScrolledWindow::SetTargetWindow( wxWindow
*target
)
233 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
234 m_targetWindow
= target
;
237 wxWindow
*wxGenericScrolledWindow::GetTargetWindow()
239 return m_targetWindow
;
242 // ----------------------------------------------------------------------------
243 // scrolling implementation itself
244 // ----------------------------------------------------------------------------
246 void wxGenericScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
248 int orient
= event
.GetOrientation();
250 int nScrollInc
= CalcScrollInc(event
);
251 if (nScrollInc
== 0) return;
253 if (orient
== wxHORIZONTAL
)
255 int newPos
= m_xScrollPosition
+ nScrollInc
;
256 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
260 int newPos
= m_yScrollPosition
+ nScrollInc
;
261 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
264 if (orient
== wxHORIZONTAL
)
266 m_xScrollPosition
+= nScrollInc
;
270 m_yScrollPosition
+= nScrollInc
;
273 if (orient
== wxHORIZONTAL
)
275 if (m_xScrollingEnabled
)
276 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
278 m_targetWindow
->Refresh();
282 if (m_yScrollingEnabled
)
283 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
285 m_targetWindow
->Refresh();
288 m_targetWindow
->MacUpdateImmediately() ;
292 int wxGenericScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
294 int pos
= event
.GetPosition();
295 int orient
= event
.GetOrientation();
298 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
300 if (orient
== wxHORIZONTAL
)
301 nScrollInc
= - m_xScrollPosition
;
303 nScrollInc
= - m_yScrollPosition
;
305 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
307 if (orient
== wxHORIZONTAL
)
308 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
310 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
312 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
316 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
320 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
322 if (orient
== wxHORIZONTAL
)
323 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
325 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
327 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
329 if (orient
== wxHORIZONTAL
)
330 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
332 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
334 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
335 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
337 if (orient
== wxHORIZONTAL
)
338 nScrollInc
= pos
- m_xScrollPosition
;
340 nScrollInc
= pos
- m_yScrollPosition
;
343 if (orient
== wxHORIZONTAL
)
345 if (m_xScrollPixelsPerLine
> 0)
348 m_targetWindow
->GetClientSize(&w
, &h
);
350 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
351 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
355 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
356 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
357 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
358 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
361 m_targetWindow
->Refresh();
365 if (m_yScrollPixelsPerLine
> 0)
368 m_targetWindow
->GetClientSize(&w
, &h
);
370 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
371 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
375 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
376 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
377 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
378 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
381 m_targetWindow
->Refresh();
387 // Adjust the scrollbars - new version.
388 void wxGenericScrolledWindow::AdjustScrollbars()
391 m_targetWindow
->GetClientSize(&w
, &h
);
393 int oldXScroll
= m_xScrollPosition
;
394 int oldYScroll
= m_yScrollPosition
;
396 if (m_xScrollLines
> 0)
398 // Calculate page size i.e. number of scroll units you get on the
399 // current client window
400 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
401 if (noPagePositions
< 1) noPagePositions
= 1;
403 // Correct position if greater than extent of canvas minus
404 // the visible portion of it or if below zero
405 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
406 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
408 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
409 // The amount by which we scroll when paging
410 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
414 m_xScrollPosition
= 0;
415 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
418 if (m_yScrollLines
> 0)
420 // Calculate page size i.e. number of scroll units you get on the
421 // current client window
422 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
423 if (noPagePositions
< 1) noPagePositions
= 1;
425 // Correct position if greater than extent of canvas minus
426 // the visible portion of it or if below zero
427 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
428 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
430 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
431 // The amount by which we scroll when paging
432 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
436 m_yScrollPosition
= 0;
437 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
440 if (oldXScroll
!= m_xScrollPosition
)
442 if (m_xScrollingEnabled
)
443 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
445 m_targetWindow
->Refresh();
448 if (oldYScroll
!= m_yScrollPosition
)
450 if (m_yScrollingEnabled
)
451 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
453 m_targetWindow
->Refresh();
457 // Override this function if you don't want to have wxGenericScrolledWindow
458 // automatically change the origin according to the scroll position.
459 void wxGenericScrolledWindow::PrepareDC(wxDC
& dc
)
461 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
462 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
463 dc
.SetUserScale( m_scaleX
, m_scaleY
);
466 #if WXWIN_COMPATIBILITY
467 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
469 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
470 *y_page
= GetScrollPageSize(wxVERTICAL
);
473 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
476 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
478 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
480 #endif // WXWIN_COMPATIBILITY
482 void wxGenericScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
485 *x_unit
= m_xScrollPixelsPerLine
;
487 *y_unit
= m_yScrollPixelsPerLine
;
490 int wxGenericScrolledWindow::GetScrollPageSize(int orient
) const
492 if ( orient
== wxHORIZONTAL
)
493 return m_xScrollLinesPerPage
;
495 return m_yScrollLinesPerPage
;
498 void wxGenericScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
500 if ( orient
== wxHORIZONTAL
)
501 m_xScrollLinesPerPage
= pageSize
;
503 m_yScrollLinesPerPage
= pageSize
;
507 * Scroll to given position (scroll position, not pixel position)
509 void wxGenericScrolledWindow::Scroll( int x_pos
, int y_pos
)
514 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
515 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
518 m_targetWindow
->GetClientSize(&w
, &h
);
520 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
522 int old_x
= m_xScrollPosition
;
523 m_xScrollPosition
= x_pos
;
525 // Calculate page size i.e. number of scroll units you get on the
526 // current client window
527 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
528 if (noPagePositions
< 1) noPagePositions
= 1;
530 // Correct position if greater than extent of canvas minus
531 // the visible portion of it or if below zero
532 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
533 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
535 if (old_x
!= m_xScrollPosition
) {
536 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
537 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
540 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
542 int old_y
= m_yScrollPosition
;
543 m_yScrollPosition
= y_pos
;
545 // Calculate page size i.e. number of scroll units you get on the
546 // current client window
547 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
548 if (noPagePositions
< 1) noPagePositions
= 1;
550 // Correct position if greater than extent of canvas minus
551 // the visible portion of it or if below zero
552 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
553 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
555 if (old_y
!= m_yScrollPosition
) {
556 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
557 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
562 m_targetWindow
->MacUpdateImmediately();
566 void wxGenericScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
568 m_xScrollingEnabled
= x_scroll
;
569 m_yScrollingEnabled
= y_scroll
;
572 void wxGenericScrolledWindow::GetVirtualSize (int *x
, int *y
) const
575 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
577 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
580 // Where the current view starts from
581 void wxGenericScrolledWindow::GetViewStart (int *x
, int *y
) const
584 *x
= m_xScrollPosition
;
586 *y
= m_yScrollPosition
;
589 void wxGenericScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
592 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
594 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
597 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
600 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
602 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
605 // ----------------------------------------------------------------------------
607 // ----------------------------------------------------------------------------
609 // Default OnSize resets scrollbars, if any
610 void wxGenericScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
612 #if wxUSE_CONSTRAINTS
620 // This calls OnDraw, having adjusted the origin according to the current
622 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
630 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
631 // compatibility here - if we used OnKeyDown(), the programs which process
632 // arrows themselves in their OnChar() would never get the message and like
633 // this they always have the priority
634 void wxGenericScrolledWindow::OnChar(wxKeyEvent
& event
)
636 int stx
, sty
, // view origin
637 szx
, szy
, // view size (total)
638 clix
, cliy
; // view size (on screen)
640 ViewStart(&stx
, &sty
);
641 GetClientSize(&clix
, &cliy
);
642 GetVirtualSize(&szx
, &szy
);
644 if( m_xScrollPixelsPerLine
)
646 clix
/= m_xScrollPixelsPerLine
;
647 szx
/= m_xScrollPixelsPerLine
;
654 if( m_yScrollPixelsPerLine
)
656 cliy
/= m_yScrollPixelsPerLine
;
657 szy
/= m_yScrollPixelsPerLine
;
666 switch ( event
.KeyCode() )
670 dsty
= sty
- (5 * cliy
/ 6);
671 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
676 Scroll(-1, sty
+ (5 * cliy
/ 6));
680 Scroll(0, event
.ControlDown() ? 0 : -1);
684 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
710 void wxGenericScrolledWindow::OnMouseWheel(wxMouseEvent
& event
)
715 m_wheelRotation
+= event
.GetWheelRotation();
716 lines
= m_wheelRotation
/ event
.GetWheelDelta();
717 m_wheelRotation
-= lines
* event
.GetWheelDelta();
720 lines
*= event
.GetLinesPerAction();
721 GetViewStart(&vsx
, &vsy
);
722 Scroll(-1, vsy
- lines
);