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"
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
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
62 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
63 EVT_SIZE(wxScrolledWindow::OnSize
)
64 EVT_PAINT(wxScrolledWindow::OnPaint
)
65 EVT_CHAR(wxScrolledWindow::OnChar
)
68 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
75 // wxScrolledWindow creation
76 // ----------------------------------------------------------------------------
78 wxScrolledWindow::wxScrolledWindow()
80 m_xScrollPixelsPerLine
= 0;
81 m_yScrollPixelsPerLine
= 0;
82 m_xScrollingEnabled
= TRUE
;
83 m_yScrollingEnabled
= TRUE
;
84 m_xScrollPosition
= 0;
85 m_yScrollPosition
= 0;
88 m_xScrollLinesPerPage
= 0;
89 m_yScrollLinesPerPage
= 0;
92 m_targetWindow
= (wxWindow
*) NULL
;
95 bool wxScrolledWindow::Create(wxWindow
*parent
,
100 const wxString
& name
)
102 m_xScrollPixelsPerLine
= 0;
103 m_yScrollPixelsPerLine
= 0;
104 m_xScrollingEnabled
= TRUE
;
105 m_yScrollingEnabled
= TRUE
;
106 m_xScrollPosition
= 0;
107 m_yScrollPosition
= 0;
110 m_xScrollLinesPerPage
= 0;
111 m_yScrollLinesPerPage
= 0;
115 m_targetWindow
= this;
117 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
120 // we need to process arrows ourselves for scrolling
121 m_lDlgCode
|= DLGC_WANTARROWS
;
127 wxScrolledWindow::~wxScrolledWindow()
131 // ----------------------------------------------------------------------------
132 // setting scrolling parameters
133 // ----------------------------------------------------------------------------
136 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
137 * noUnitsX/noUnitsY: : no. units per scrollbar
139 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
140 int noUnitsX
, int noUnitsY
,
141 int xPos
, int yPos
, bool noRefresh
)
145 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
148 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
149 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
151 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
152 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
153 (xPos
!= m_xScrollPosition
) ||
154 (yPos
!= m_yScrollPosition
)
155 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
156 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
159 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
160 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
161 m_xScrollPosition
= xPos
;
162 m_yScrollPosition
= yPos
;
163 m_xScrollLines
= noUnitsX
;
164 m_yScrollLines
= noUnitsY
;
167 // Sorry, some Motif-specific code to implement a backing pixmap
168 // for the wxRETAINED style. Implementing a backing store can't
169 // be entirely generic because it relies on the wxWindowDC implementation
170 // to duplicate X drawing calls for the backing pixmap.
172 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
174 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
176 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
177 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
178 if (m_backingPixmap
&&
179 !((m_pixmapWidth
== totalPixelWidth
) &&
180 (m_pixmapHeight
== totalPixelHeight
)))
182 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
183 m_backingPixmap
= (WXPixmap
) 0;
186 if (!m_backingPixmap
&&
187 (noUnitsX
!= 0) && (noUnitsY
!= 0))
189 int depth
= wxDisplayDepth();
190 m_pixmapWidth
= totalPixelWidth
;
191 m_pixmapHeight
= totalPixelHeight
;
192 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
193 m_pixmapWidth
, m_pixmapHeight
, depth
);
201 if (do_refresh
&& !noRefresh
)
202 m_targetWindow
->Refresh();
205 // GRG: if this turns out to be really necessary, we could
206 // at least move it to the above if { ... } so that it is
207 // only done if noRefresh = FALSE (the default). OTOH, if
208 // this doesn't break anything, which seems to be the
209 // case, we could just leave it out.
212 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
215 m_targetWindow
->MacUpdateImmediately() ;
219 // ----------------------------------------------------------------------------
220 // target window handling
221 // ----------------------------------------------------------------------------
223 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
225 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
226 m_targetWindow
= target
;
229 wxWindow
*wxScrolledWindow::GetTargetWindow()
231 return m_targetWindow
;
234 // ----------------------------------------------------------------------------
235 // scrolling implementation itself
236 // ----------------------------------------------------------------------------
238 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
240 int orient
= event
.GetOrientation();
242 int nScrollInc
= CalcScrollInc(event
);
243 if (nScrollInc
== 0) return;
245 if (orient
== wxHORIZONTAL
)
247 int newPos
= m_xScrollPosition
+ nScrollInc
;
248 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
252 int newPos
= m_yScrollPosition
+ nScrollInc
;
253 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
256 if (orient
== wxHORIZONTAL
)
258 m_xScrollPosition
+= nScrollInc
;
262 m_yScrollPosition
+= nScrollInc
;
265 if (orient
== wxHORIZONTAL
)
267 if (m_xScrollingEnabled
)
268 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
270 m_targetWindow
->Refresh();
274 if (m_yScrollingEnabled
)
275 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
277 m_targetWindow
->Refresh();
280 m_targetWindow
->MacUpdateImmediately() ;
284 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
286 int pos
= event
.GetPosition();
287 int orient
= event
.GetOrientation();
290 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
292 if (orient
== wxHORIZONTAL
)
293 nScrollInc
= - m_xScrollPosition
;
295 nScrollInc
= - m_yScrollPosition
;
297 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
299 if (orient
== wxHORIZONTAL
)
300 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
302 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
304 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
308 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
312 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
314 if (orient
== wxHORIZONTAL
)
315 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
317 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
319 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
321 if (orient
== wxHORIZONTAL
)
322 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
324 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
326 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
327 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
329 if (orient
== wxHORIZONTAL
)
330 nScrollInc
= pos
- m_xScrollPosition
;
332 nScrollInc
= pos
- m_yScrollPosition
;
335 if (orient
== wxHORIZONTAL
)
337 if (m_xScrollPixelsPerLine
> 0)
340 m_targetWindow
->GetClientSize(&w
, &h
);
342 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
343 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
347 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
348 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
349 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
350 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
353 m_targetWindow
->Refresh();
357 if (m_yScrollPixelsPerLine
> 0)
360 m_targetWindow
->GetClientSize(&w
, &h
);
362 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
363 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
367 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
368 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
369 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
370 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
373 m_targetWindow
->Refresh();
379 // Adjust the scrollbars - new version.
380 void wxScrolledWindow::AdjustScrollbars()
383 m_targetWindow
->GetClientSize(&w
, &h
);
385 int oldXScroll
= m_xScrollPosition
;
386 int oldYScroll
= m_yScrollPosition
;
388 if (m_xScrollLines
> 0)
390 // Calculate page size i.e. number of scroll units you get on the
391 // current client window
392 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
393 if (noPagePositions
< 1) noPagePositions
= 1;
395 // Correct position if greater than extent of canvas minus
396 // the visible portion of it or if below zero
397 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
398 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
400 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
401 // The amount by which we scroll when paging
402 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
406 m_xScrollPosition
= 0;
407 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
410 if (m_yScrollLines
> 0)
412 // Calculate page size i.e. number of scroll units you get on the
413 // current client window
414 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
415 if (noPagePositions
< 1) noPagePositions
= 1;
417 // Correct position if greater than extent of canvas minus
418 // the visible portion of it or if below zero
419 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
420 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
422 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
423 // The amount by which we scroll when paging
424 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
428 m_yScrollPosition
= 0;
429 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
432 if (oldXScroll
!= m_xScrollPosition
)
434 if (m_xScrollingEnabled
)
435 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
437 m_targetWindow
->Refresh();
440 if (oldYScroll
!= m_yScrollPosition
)
442 if (m_yScrollingEnabled
)
443 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
445 m_targetWindow
->Refresh();
449 // Override this function if you don't want to have wxScrolledWindow
450 // automatically change the origin according to the scroll position.
451 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
453 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
454 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
455 dc
.SetUserScale( m_scaleX
, m_scaleY
);
458 #if WXWIN_COMPATIBILITY
459 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
461 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
462 *y_page
= GetScrollPageSize(wxVERTICAL
);
465 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
468 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
470 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
472 #endif // WXWIN_COMPATIBILITY
474 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
477 *x_unit
= m_xScrollPixelsPerLine
;
479 *y_unit
= m_yScrollPixelsPerLine
;
482 int wxScrolledWindow::GetScrollPageSize(int orient
) const
484 if ( orient
== wxHORIZONTAL
)
485 return m_xScrollLinesPerPage
;
487 return m_yScrollLinesPerPage
;
490 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
492 if ( orient
== wxHORIZONTAL
)
493 m_xScrollLinesPerPage
= pageSize
;
495 m_yScrollLinesPerPage
= pageSize
;
499 * Scroll to given position (scroll position, not pixel position)
501 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
);
512 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
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
) {
528 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
529 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
532 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
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
) {
548 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
549 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
;
658 switch ( event
.KeyCode() )
662 dsty
= sty
- (5 * cliy
/ 6);
663 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
668 Scroll(-1, sty
+ (5 * cliy
/ 6));
672 Scroll(0, event
.ControlDown() ? 0 : -1);
676 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);