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 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
66 EVT_SCROLLWIN(wxGenericScrolledWindow::OnScroll
)
67 EVT_SIZE(wxGenericScrolledWindow::OnSize
)
68 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
69 EVT_CHAR(wxGenericScrolledWindow::OnChar
)
72 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wxGenericScrolledWindow creation
80 // ----------------------------------------------------------------------------
82 wxGenericScrolledWindow::wxGenericScrolledWindow()
84 m_xScrollPixelsPerLine
= 0;
85 m_yScrollPixelsPerLine
= 0;
86 m_xScrollingEnabled
= TRUE
;
87 m_yScrollingEnabled
= TRUE
;
88 m_xScrollPosition
= 0;
89 m_yScrollPosition
= 0;
92 m_xScrollLinesPerPage
= 0;
93 m_yScrollLinesPerPage
= 0;
96 m_targetWindow
= (wxWindow
*) NULL
;
99 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
104 const wxString
& name
)
106 m_xScrollPixelsPerLine
= 0;
107 m_yScrollPixelsPerLine
= 0;
108 m_xScrollingEnabled
= TRUE
;
109 m_yScrollingEnabled
= TRUE
;
110 m_xScrollPosition
= 0;
111 m_yScrollPosition
= 0;
114 m_xScrollLinesPerPage
= 0;
115 m_yScrollLinesPerPage
= 0;
119 m_targetWindow
= this;
121 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
124 // we need to process arrows ourselves for scrolling
125 m_lDlgCode
|= DLGC_WANTARROWS
;
131 wxGenericScrolledWindow::~wxGenericScrolledWindow()
135 // ----------------------------------------------------------------------------
136 // setting scrolling parameters
137 // ----------------------------------------------------------------------------
140 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
141 * noUnitsX/noUnitsY: : no. units per scrollbar
143 void wxGenericScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
144 int noUnitsX
, int noUnitsY
,
145 int xPos
, int yPos
, bool noRefresh
)
149 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
152 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
153 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
155 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
156 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
157 (xPos
!= m_xScrollPosition
) ||
158 (yPos
!= m_yScrollPosition
)
159 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
160 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
163 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
164 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
165 m_xScrollPosition
= xPos
;
166 m_yScrollPosition
= yPos
;
167 m_xScrollLines
= noUnitsX
;
168 m_yScrollLines
= noUnitsY
;
171 // Sorry, some Motif-specific code to implement a backing pixmap
172 // for the wxRETAINED style. Implementing a backing store can't
173 // be entirely generic because it relies on the wxWindowDC implementation
174 // to duplicate X drawing calls for the backing pixmap.
176 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
178 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
180 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
181 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
182 if (m_backingPixmap
&&
183 !((m_pixmapWidth
== totalPixelWidth
) &&
184 (m_pixmapHeight
== totalPixelHeight
)))
186 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
187 m_backingPixmap
= (WXPixmap
) 0;
190 if (!m_backingPixmap
&&
191 (noUnitsX
!= 0) && (noUnitsY
!= 0))
193 int depth
= wxDisplayDepth();
194 m_pixmapWidth
= totalPixelWidth
;
195 m_pixmapHeight
= totalPixelHeight
;
196 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
197 m_pixmapWidth
, m_pixmapHeight
, depth
);
205 if (do_refresh
&& !noRefresh
)
206 m_targetWindow
->Refresh();
209 // GRG: if this turns out to be really necessary, we could
210 // at least move it to the above if { ... } so that it is
211 // only done if noRefresh = FALSE (the default). OTOH, if
212 // this doesn't break anything, which seems to be the
213 // case, we could just leave it out.
216 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
219 m_targetWindow
->MacUpdateImmediately() ;
223 // ----------------------------------------------------------------------------
224 // target window handling
225 // ----------------------------------------------------------------------------
227 void wxGenericScrolledWindow::SetTargetWindow( wxWindow
*target
)
229 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
230 m_targetWindow
= target
;
233 wxWindow
*wxGenericScrolledWindow::GetTargetWindow()
235 return m_targetWindow
;
238 // ----------------------------------------------------------------------------
239 // scrolling implementation itself
240 // ----------------------------------------------------------------------------
242 void wxGenericScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
244 int orient
= event
.GetOrientation();
246 int nScrollInc
= CalcScrollInc(event
);
247 if (nScrollInc
== 0) return;
249 if (orient
== wxHORIZONTAL
)
251 int newPos
= m_xScrollPosition
+ nScrollInc
;
252 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
256 int newPos
= m_yScrollPosition
+ nScrollInc
;
257 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
260 if (orient
== wxHORIZONTAL
)
262 m_xScrollPosition
+= nScrollInc
;
266 m_yScrollPosition
+= nScrollInc
;
269 if (orient
== wxHORIZONTAL
)
271 if (m_xScrollingEnabled
)
272 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
274 m_targetWindow
->Refresh();
278 if (m_yScrollingEnabled
)
279 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
281 m_targetWindow
->Refresh();
284 m_targetWindow
->MacUpdateImmediately() ;
288 int wxGenericScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
290 int pos
= event
.GetPosition();
291 int orient
= event
.GetOrientation();
294 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
296 if (orient
== wxHORIZONTAL
)
297 nScrollInc
= - m_xScrollPosition
;
299 nScrollInc
= - m_yScrollPosition
;
301 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
303 if (orient
== wxHORIZONTAL
)
304 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
306 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
308 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
312 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
316 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
318 if (orient
== wxHORIZONTAL
)
319 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
321 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
323 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
325 if (orient
== wxHORIZONTAL
)
326 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
328 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
330 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
331 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
333 if (orient
== wxHORIZONTAL
)
334 nScrollInc
= pos
- m_xScrollPosition
;
336 nScrollInc
= pos
- m_yScrollPosition
;
339 if (orient
== wxHORIZONTAL
)
341 if (m_xScrollPixelsPerLine
> 0)
344 m_targetWindow
->GetClientSize(&w
, &h
);
346 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
347 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
351 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
352 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
353 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
354 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
357 m_targetWindow
->Refresh();
361 if (m_yScrollPixelsPerLine
> 0)
364 m_targetWindow
->GetClientSize(&w
, &h
);
366 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
367 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
371 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
372 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
373 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
374 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
377 m_targetWindow
->Refresh();
383 // Adjust the scrollbars - new version.
384 void wxGenericScrolledWindow::AdjustScrollbars()
387 m_targetWindow
->GetClientSize(&w
, &h
);
389 int oldXScroll
= m_xScrollPosition
;
390 int oldYScroll
= m_yScrollPosition
;
392 if (m_xScrollLines
> 0)
394 // Calculate page size i.e. number of scroll units you get on the
395 // current client window
396 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
397 if (noPagePositions
< 1) noPagePositions
= 1;
399 // Correct position if greater than extent of canvas minus
400 // the visible portion of it or if below zero
401 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
402 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
404 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
405 // The amount by which we scroll when paging
406 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
410 m_xScrollPosition
= 0;
411 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
414 if (m_yScrollLines
> 0)
416 // Calculate page size i.e. number of scroll units you get on the
417 // current client window
418 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
419 if (noPagePositions
< 1) noPagePositions
= 1;
421 // Correct position if greater than extent of canvas minus
422 // the visible portion of it or if below zero
423 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
424 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
426 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
427 // The amount by which we scroll when paging
428 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
432 m_yScrollPosition
= 0;
433 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
436 if (oldXScroll
!= m_xScrollPosition
)
438 if (m_xScrollingEnabled
)
439 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
441 m_targetWindow
->Refresh();
444 if (oldYScroll
!= m_yScrollPosition
)
446 if (m_yScrollingEnabled
)
447 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
449 m_targetWindow
->Refresh();
453 // Override this function if you don't want to have wxGenericScrolledWindow
454 // automatically change the origin according to the scroll position.
455 void wxGenericScrolledWindow::PrepareDC(wxDC
& dc
)
457 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
458 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
459 dc
.SetUserScale( m_scaleX
, m_scaleY
);
462 #if WXWIN_COMPATIBILITY
463 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
465 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
466 *y_page
= GetScrollPageSize(wxVERTICAL
);
469 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
472 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
474 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
476 #endif // WXWIN_COMPATIBILITY
478 void wxGenericScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
481 *x_unit
= m_xScrollPixelsPerLine
;
483 *y_unit
= m_yScrollPixelsPerLine
;
486 int wxGenericScrolledWindow::GetScrollPageSize(int orient
) const
488 if ( orient
== wxHORIZONTAL
)
489 return m_xScrollLinesPerPage
;
491 return m_yScrollLinesPerPage
;
494 void wxGenericScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
496 if ( orient
== wxHORIZONTAL
)
497 m_xScrollLinesPerPage
= pageSize
;
499 m_yScrollLinesPerPage
= pageSize
;
503 * Scroll to given position (scroll position, not pixel position)
505 void wxGenericScrolledWindow::Scroll( int x_pos
, int y_pos
)
510 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
511 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
514 m_targetWindow
->GetClientSize(&w
, &h
);
516 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
518 int old_x
= m_xScrollPosition
;
519 m_xScrollPosition
= x_pos
;
521 // Calculate page size i.e. number of scroll units you get on the
522 // current client window
523 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
524 if (noPagePositions
< 1) noPagePositions
= 1;
526 // Correct position if greater than extent of canvas minus
527 // the visible portion of it or if below zero
528 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
529 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
531 if (old_x
!= m_xScrollPosition
) {
532 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
533 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
536 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
538 int old_y
= m_yScrollPosition
;
539 m_yScrollPosition
= y_pos
;
541 // Calculate page size i.e. number of scroll units you get on the
542 // current client window
543 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
544 if (noPagePositions
< 1) noPagePositions
= 1;
546 // Correct position if greater than extent of canvas minus
547 // the visible portion of it or if below zero
548 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
549 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
551 if (old_y
!= m_yScrollPosition
) {
552 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
553 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
558 m_targetWindow
->MacUpdateImmediately();
562 void wxGenericScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
564 m_xScrollingEnabled
= x_scroll
;
565 m_yScrollingEnabled
= y_scroll
;
568 void wxGenericScrolledWindow::GetVirtualSize (int *x
, int *y
) const
571 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
573 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
576 // Where the current view starts from
577 void wxGenericScrolledWindow::GetViewStart (int *x
, int *y
) const
580 *x
= m_xScrollPosition
;
582 *y
= m_yScrollPosition
;
585 void wxGenericScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
588 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
590 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
593 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
596 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
598 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 // Default OnSize resets scrollbars, if any
606 void wxGenericScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
608 #if wxUSE_CONSTRAINTS
616 // This calls OnDraw, having adjusted the origin according to the current
618 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
626 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
627 // compatibility here - if we used OnKeyDown(), the programs which process
628 // arrows themselves in their OnChar() would never get the message and like
629 // this they always have the priority
630 void wxGenericScrolledWindow::OnChar(wxKeyEvent
& event
)
632 int stx
, sty
, // view origin
633 szx
, szy
, // view size (total)
634 clix
, cliy
; // view size (on screen)
636 ViewStart(&stx
, &sty
);
637 GetClientSize(&clix
, &cliy
);
638 GetVirtualSize(&szx
, &szy
);
640 if( m_xScrollPixelsPerLine
)
642 clix
/= m_xScrollPixelsPerLine
;
643 szx
/= m_xScrollPixelsPerLine
;
650 if( m_yScrollPixelsPerLine
)
652 cliy
/= m_yScrollPixelsPerLine
;
653 szy
/= m_yScrollPixelsPerLine
;
662 switch ( event
.KeyCode() )
666 dsty
= sty
- (5 * cliy
/ 6);
667 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
672 Scroll(-1, sty
+ (5 * cliy
/ 6));
676 Scroll(0, event
.ControlDown() ? 0 : -1);
680 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);