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 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
135 (noUnitsX
< m_xScrollLines
) ||
136 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
137 (noUnitsY
< m_yScrollLines
) ||
138 (xPos
!= m_xScrollPosition
) ||
139 (yPos
!= m_yScrollPosition
) ||
140 (pixelsPerUnitX
!= m_xScrollPixelsPerLine
) ||
141 (pixelsPerUnitY
!= m_yScrollPixelsPerLine
)
144 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
145 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
146 m_xScrollPosition
= xPos
;
147 m_yScrollPosition
= yPos
;
148 m_xScrollLines
= noUnitsX
;
149 m_yScrollLines
= noUnitsY
;
152 // Sorry, some Motif-specific code to implement a backing pixmap
153 // for the wxRETAINED style. Implementing a backing store can't
154 // be entirely generic because it relies on the wxWindowDC implementation
155 // to duplicate X drawing calls for the backing pixmap.
157 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
159 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
161 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
162 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
163 if (m_backingPixmap
&&
164 !((m_pixmapWidth
== totalPixelWidth
) &&
165 (m_pixmapHeight
== totalPixelHeight
)))
167 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
168 m_backingPixmap
= (WXPixmap
) 0;
171 if (!m_backingPixmap
&&
172 (noUnitsX
!= 0) && (noUnitsY
!= 0))
174 int depth
= wxDisplayDepth();
175 m_pixmapWidth
= totalPixelWidth
;
176 m_pixmapHeight
= totalPixelHeight
;
177 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
178 m_pixmapWidth
, m_pixmapHeight
, depth
);
186 if (do_refresh
&& !noRefresh
)
187 m_targetWindow
->Refresh();
190 // GRG: if this turns out to be really necessary, we could
191 // at least move it to the above if { ... } so that it is
192 // only done if noRefresh = FALSE (the default). OTOH, if
193 // this doesn't break anything, which seems to be the
194 // case, we could just leave it out.
197 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
200 m_targetWindow
->MacUpdateImmediately() ;
204 // ----------------------------------------------------------------------------
205 // target window handling
206 // ----------------------------------------------------------------------------
208 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
210 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
211 m_targetWindow
= target
;
214 wxWindow
*wxScrolledWindow::GetTargetWindow()
216 return m_targetWindow
;
219 // ----------------------------------------------------------------------------
220 // scrolling implementation itself
221 // ----------------------------------------------------------------------------
223 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
225 int orient
= event
.GetOrientation();
227 int nScrollInc
= CalcScrollInc(event
);
228 if (nScrollInc
== 0) return;
230 if (orient
== wxHORIZONTAL
)
232 int newPos
= m_xScrollPosition
+ nScrollInc
;
233 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
237 int newPos
= m_yScrollPosition
+ nScrollInc
;
238 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
241 if (orient
== wxHORIZONTAL
)
243 m_xScrollPosition
+= nScrollInc
;
247 m_yScrollPosition
+= nScrollInc
;
250 if (orient
== wxHORIZONTAL
)
252 if (m_xScrollingEnabled
)
253 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
255 m_targetWindow
->Refresh();
259 if (m_yScrollingEnabled
)
260 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
262 m_targetWindow
->Refresh();
265 m_targetWindow
->MacUpdateImmediately() ;
269 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
271 int pos
= event
.GetPosition();
272 int orient
= event
.GetOrientation();
275 switch (event
.GetEventType())
277 case wxEVT_SCROLLWIN_TOP
:
279 if (orient
== wxHORIZONTAL
)
280 nScrollInc
= - m_xScrollPosition
;
282 nScrollInc
= - m_yScrollPosition
;
285 case wxEVT_SCROLLWIN_BOTTOM
:
287 if (orient
== wxHORIZONTAL
)
288 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
290 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
293 case wxEVT_SCROLLWIN_LINEUP
:
298 case wxEVT_SCROLLWIN_LINEDOWN
:
303 case wxEVT_SCROLLWIN_PAGEUP
:
305 if (orient
== wxHORIZONTAL
)
306 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
308 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
311 case wxEVT_SCROLLWIN_PAGEDOWN
:
313 if (orient
== wxHORIZONTAL
)
314 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
316 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
319 case wxEVT_SCROLLWIN_THUMBTRACK
:
320 case wxEVT_SCROLLWIN_THUMBRELEASE
:
322 if (orient
== wxHORIZONTAL
)
323 nScrollInc
= pos
- m_xScrollPosition
;
325 nScrollInc
= pos
- m_yScrollPosition
;
334 if (orient
== wxHORIZONTAL
)
336 if (m_xScrollPixelsPerLine
> 0)
339 m_targetWindow
->GetClientSize(&w
, &h
);
341 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
342 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
346 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
347 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
348 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
349 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
352 m_targetWindow
->Refresh();
356 if (m_yScrollPixelsPerLine
> 0)
359 m_targetWindow
->GetClientSize(&w
, &h
);
361 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
362 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
366 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
367 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
368 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
369 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
372 m_targetWindow
->Refresh();
378 // Adjust the scrollbars - new version.
379 void wxScrolledWindow::AdjustScrollbars()
382 m_targetWindow
->GetClientSize(&w
, &h
);
384 int oldXScroll
= m_xScrollPosition
;
385 int oldYScroll
= m_yScrollPosition
;
387 if (m_xScrollLines
> 0)
389 // Calculate page size i.e. number of scroll units you get on the
390 // current client window
391 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
392 if (noPagePositions
< 1) noPagePositions
= 1;
394 // Correct position if greater than extent of canvas minus
395 // the visible portion of it or if below zero
396 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
397 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
399 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
400 // The amount by which we scroll when paging
401 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
405 m_xScrollPosition
= 0;
406 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
409 if (m_yScrollLines
> 0)
411 // Calculate page size i.e. number of scroll units you get on the
412 // current client window
413 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
414 if (noPagePositions
< 1) noPagePositions
= 1;
416 // Correct position if greater than extent of canvas minus
417 // the visible portion of it or if below zero
418 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
419 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
421 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
422 // The amount by which we scroll when paging
423 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
427 m_yScrollPosition
= 0;
428 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
431 if (oldXScroll
!= m_xScrollPosition
)
433 if (m_xScrollingEnabled
)
434 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
436 m_targetWindow
->Refresh();
439 if (oldYScroll
!= m_yScrollPosition
)
441 if (m_yScrollingEnabled
)
442 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
444 m_targetWindow
->Refresh();
448 // Override this function if you don't want to have wxScrolledWindow
449 // automatically change the origin according to the scroll position.
450 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
452 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
453 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
454 dc
.SetUserScale( m_scaleX
, m_scaleY
);
457 #if WXWIN_COMPATIBILITY
458 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
460 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
461 *y_page
= GetScrollPageSize(wxVERTICAL
);
464 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
467 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
469 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
471 #endif // WXWIN_COMPATIBILITY
473 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
476 *x_unit
= m_xScrollPixelsPerLine
;
478 *y_unit
= m_yScrollPixelsPerLine
;
481 int wxScrolledWindow::GetScrollPageSize(int orient
) const
483 if ( orient
== wxHORIZONTAL
)
484 return m_xScrollLinesPerPage
;
486 return m_yScrollLinesPerPage
;
489 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
491 if ( orient
== wxHORIZONTAL
)
492 m_xScrollLinesPerPage
= pageSize
;
494 m_yScrollLinesPerPage
= pageSize
;
498 * Scroll to given position (scroll position, not pixel position)
500 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
502 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
503 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
506 m_targetWindow
->GetClientSize(&w
, &h
);
510 int old_x
= m_xScrollPosition
;
511 m_xScrollPosition
= x_pos
;
513 // Calculate page size i.e. number of scroll units you get on the
514 // current client window
515 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
516 if (noPagePositions
< 1) noPagePositions
= 1;
518 // Correct position if greater than extent of canvas minus
519 // the visible portion of it or if below zero
520 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
521 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
523 if (old_x
== m_xScrollPosition
) return;
525 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
526 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
530 int old_y
= m_yScrollPosition
;
531 m_yScrollPosition
= y_pos
;
533 // Calculate page size i.e. number of scroll units you get on the
534 // current client window
535 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
536 if (noPagePositions
< 1) noPagePositions
= 1;
538 // Correct position if greater than extent of canvas minus
539 // the visible portion of it or if below zero
540 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
541 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
543 if (old_y
== m_yScrollPosition
) return;
545 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
546 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
550 m_targetWindow
->MacUpdateImmediately();
554 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
556 m_xScrollingEnabled
= x_scroll
;
557 m_yScrollingEnabled
= y_scroll
;
560 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
563 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
565 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
568 // Where the current view starts from
569 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
572 *x
= m_xScrollPosition
;
574 *y
= m_yScrollPosition
;
577 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
580 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
582 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
585 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
588 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
590 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
593 // ----------------------------------------------------------------------------
595 // ----------------------------------------------------------------------------
597 // Default OnSize resets scrollbars, if any
598 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
600 #if wxUSE_CONSTRAINTS
608 // This calls OnDraw, having adjusted the origin according to the current
610 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
618 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
619 // compatibility here - if we used OnKeyDown(), the programs which process
620 // arrows themselves in their OnChar() would never get the message and like
621 // this they always have the priority
622 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
624 int stx
, sty
, // view origin
625 szx
, szy
, // view size (total)
626 clix
, cliy
; // view size (on screen)
628 ViewStart(&stx
, &sty
);
629 GetClientSize(&clix
, &cliy
);
630 GetVirtualSize(&szx
, &szy
);
632 if( m_xScrollPixelsPerLine
)
634 clix
/= m_xScrollPixelsPerLine
;
635 szx
/= m_xScrollPixelsPerLine
;
642 if( m_yScrollPixelsPerLine
)
644 cliy
/= m_yScrollPixelsPerLine
;
645 szy
/= m_yScrollPixelsPerLine
;
653 switch ( event
.KeyCode() )
657 Scroll(-1, sty
- (5 * cliy
/ 6));
662 Scroll(-1, sty
+ (5 * cliy
/ 6));
666 Scroll(0, event
.ControlDown() ? 0 : -1);
670 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);