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
)
73 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // wxGenericScrolledWindow creation
81 // ----------------------------------------------------------------------------
83 wxGenericScrolledWindow::wxGenericScrolledWindow()
85 m_xScrollPixelsPerLine
= 0;
86 m_yScrollPixelsPerLine
= 0;
87 m_xScrollingEnabled
= TRUE
;
88 m_yScrollingEnabled
= TRUE
;
89 m_xScrollPosition
= 0;
90 m_yScrollPosition
= 0;
93 m_xScrollLinesPerPage
= 0;
94 m_yScrollLinesPerPage
= 0;
97 m_targetWindow
= (wxWindow
*) NULL
;
100 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
105 const wxString
& name
)
107 m_xScrollPixelsPerLine
= 0;
108 m_yScrollPixelsPerLine
= 0;
109 m_xScrollingEnabled
= TRUE
;
110 m_yScrollingEnabled
= TRUE
;
111 m_xScrollPosition
= 0;
112 m_yScrollPosition
= 0;
115 m_xScrollLinesPerPage
= 0;
116 m_yScrollLinesPerPage
= 0;
120 m_targetWindow
= this;
122 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
125 // we need to process arrows ourselves for scrolling
126 m_lDlgCode
|= DLGC_WANTARROWS
;
132 wxGenericScrolledWindow::~wxGenericScrolledWindow()
136 // ----------------------------------------------------------------------------
137 // setting scrolling parameters
138 // ----------------------------------------------------------------------------
141 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
142 * noUnitsX/noUnitsY: : no. units per scrollbar
144 void wxGenericScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
145 int noUnitsX
, int noUnitsY
,
146 int xPos
, int yPos
, bool noRefresh
)
150 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
153 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
154 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
156 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
157 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
158 (xPos
!= m_xScrollPosition
) ||
159 (yPos
!= m_yScrollPosition
)
160 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
161 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
164 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
165 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
166 m_xScrollPosition
= xPos
;
167 m_yScrollPosition
= yPos
;
168 m_xScrollLines
= noUnitsX
;
169 m_yScrollLines
= noUnitsY
;
172 // Sorry, some Motif-specific code to implement a backing pixmap
173 // for the wxRETAINED style. Implementing a backing store can't
174 // be entirely generic because it relies on the wxWindowDC implementation
175 // to duplicate X drawing calls for the backing pixmap.
177 if ((m_windowStyle
& wxRETAINED
) == wxRETAINED
)
179 Display
* dpy
= XtDisplay((Widget
) GetMainWidget());
181 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
182 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
183 if (m_backingPixmap
&&
184 !((m_pixmapWidth
== totalPixelWidth
) &&
185 (m_pixmapHeight
== totalPixelHeight
)))
187 XFreePixmap (dpy
, (Pixmap
) m_backingPixmap
);
188 m_backingPixmap
= (WXPixmap
) 0;
191 if (!m_backingPixmap
&&
192 (noUnitsX
!= 0) && (noUnitsY
!= 0))
194 int depth
= wxDisplayDepth();
195 m_pixmapWidth
= totalPixelWidth
;
196 m_pixmapHeight
= totalPixelHeight
;
197 m_backingPixmap
= (WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
198 m_pixmapWidth
, m_pixmapHeight
, depth
);
206 if (do_refresh
&& !noRefresh
)
207 m_targetWindow
->Refresh();
210 // GRG: if this turns out to be really necessary, we could
211 // at least move it to the above if { ... } so that it is
212 // only done if noRefresh = FALSE (the default). OTOH, if
213 // this doesn't break anything, which seems to be the
214 // case, we could just leave it out.
217 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
220 m_targetWindow
->MacUpdateImmediately() ;
224 // ----------------------------------------------------------------------------
225 // target window handling
226 // ----------------------------------------------------------------------------
228 void wxGenericScrolledWindow::SetTargetWindow( wxWindow
*target
)
230 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
231 m_targetWindow
= target
;
234 wxWindow
*wxGenericScrolledWindow::GetTargetWindow()
236 return m_targetWindow
;
239 // ----------------------------------------------------------------------------
240 // scrolling implementation itself
241 // ----------------------------------------------------------------------------
243 void wxGenericScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
245 int orient
= event
.GetOrientation();
247 int nScrollInc
= CalcScrollInc(event
);
248 if (nScrollInc
== 0) return;
250 if (orient
== wxHORIZONTAL
)
252 int newPos
= m_xScrollPosition
+ nScrollInc
;
253 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
257 int newPos
= m_yScrollPosition
+ nScrollInc
;
258 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
261 if (orient
== wxHORIZONTAL
)
263 m_xScrollPosition
+= nScrollInc
;
267 m_yScrollPosition
+= nScrollInc
;
270 if (orient
== wxHORIZONTAL
)
272 if (m_xScrollingEnabled
)
273 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
275 m_targetWindow
->Refresh();
279 if (m_yScrollingEnabled
)
280 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
282 m_targetWindow
->Refresh();
285 m_targetWindow
->MacUpdateImmediately() ;
289 int wxGenericScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
291 int pos
= event
.GetPosition();
292 int orient
= event
.GetOrientation();
295 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
297 if (orient
== wxHORIZONTAL
)
298 nScrollInc
= - m_xScrollPosition
;
300 nScrollInc
= - m_yScrollPosition
;
302 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
304 if (orient
== wxHORIZONTAL
)
305 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
307 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
309 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
313 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
317 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
319 if (orient
== wxHORIZONTAL
)
320 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
322 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
324 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
326 if (orient
== wxHORIZONTAL
)
327 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
329 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
331 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
332 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
334 if (orient
== wxHORIZONTAL
)
335 nScrollInc
= pos
- m_xScrollPosition
;
337 nScrollInc
= pos
- m_yScrollPosition
;
340 if (orient
== wxHORIZONTAL
)
342 if (m_xScrollPixelsPerLine
> 0)
345 m_targetWindow
->GetClientSize(&w
, &h
);
347 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
348 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
352 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
353 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
354 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
355 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
358 m_targetWindow
->Refresh();
362 if (m_yScrollPixelsPerLine
> 0)
365 m_targetWindow
->GetClientSize(&w
, &h
);
367 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
368 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
372 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
373 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
374 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
375 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
378 m_targetWindow
->Refresh();
384 // Adjust the scrollbars - new version.
385 void wxGenericScrolledWindow::AdjustScrollbars()
388 m_targetWindow
->GetClientSize(&w
, &h
);
390 int oldXScroll
= m_xScrollPosition
;
391 int oldYScroll
= m_yScrollPosition
;
393 if (m_xScrollLines
> 0)
395 // Calculate page size i.e. number of scroll units you get on the
396 // current client window
397 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
398 if (noPagePositions
< 1) noPagePositions
= 1;
400 // Correct position if greater than extent of canvas minus
401 // the visible portion of it or if below zero
402 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
403 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
405 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
406 // The amount by which we scroll when paging
407 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
411 m_xScrollPosition
= 0;
412 SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
415 if (m_yScrollLines
> 0)
417 // Calculate page size i.e. number of scroll units you get on the
418 // current client window
419 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
420 if (noPagePositions
< 1) noPagePositions
= 1;
422 // Correct position if greater than extent of canvas minus
423 // the visible portion of it or if below zero
424 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
425 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
427 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
428 // The amount by which we scroll when paging
429 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
433 m_yScrollPosition
= 0;
434 SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
437 if (oldXScroll
!= m_xScrollPosition
)
439 if (m_xScrollingEnabled
)
440 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0, (const wxRect
*) NULL
);
442 m_targetWindow
->Refresh();
445 if (oldYScroll
!= m_yScrollPosition
)
447 if (m_yScrollingEnabled
)
448 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
), (const wxRect
*) NULL
);
450 m_targetWindow
->Refresh();
454 // Override this function if you don't want to have wxGenericScrolledWindow
455 // automatically change the origin according to the scroll position.
456 void wxGenericScrolledWindow::PrepareDC(wxDC
& dc
)
458 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
459 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
460 dc
.SetUserScale( m_scaleX
, m_scaleY
);
463 #if WXWIN_COMPATIBILITY
464 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
466 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
467 *y_page
= GetScrollPageSize(wxVERTICAL
);
470 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
473 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
475 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
477 #endif // WXWIN_COMPATIBILITY
479 void wxGenericScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
482 *x_unit
= m_xScrollPixelsPerLine
;
484 *y_unit
= m_yScrollPixelsPerLine
;
487 int wxGenericScrolledWindow::GetScrollPageSize(int orient
) const
489 if ( orient
== wxHORIZONTAL
)
490 return m_xScrollLinesPerPage
;
492 return m_yScrollLinesPerPage
;
495 void wxGenericScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
497 if ( orient
== wxHORIZONTAL
)
498 m_xScrollLinesPerPage
= pageSize
;
500 m_yScrollLinesPerPage
= pageSize
;
504 * Scroll to given position (scroll position, not pixel position)
506 void wxGenericScrolledWindow::Scroll( int x_pos
, int y_pos
)
511 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
512 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
515 m_targetWindow
->GetClientSize(&w
, &h
);
517 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
519 int old_x
= m_xScrollPosition
;
520 m_xScrollPosition
= x_pos
;
522 // Calculate page size i.e. number of scroll units you get on the
523 // current client window
524 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
525 if (noPagePositions
< 1) noPagePositions
= 1;
527 // Correct position if greater than extent of canvas minus
528 // the visible portion of it or if below zero
529 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
530 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
532 if (old_x
!= m_xScrollPosition
) {
533 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
534 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
537 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
539 int old_y
= m_yScrollPosition
;
540 m_yScrollPosition
= y_pos
;
542 // Calculate page size i.e. number of scroll units you get on the
543 // current client window
544 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
545 if (noPagePositions
< 1) noPagePositions
= 1;
547 // Correct position if greater than extent of canvas minus
548 // the visible portion of it or if below zero
549 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
550 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
552 if (old_y
!= m_yScrollPosition
) {
553 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
, TRUE
);
554 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
559 m_targetWindow
->MacUpdateImmediately();
563 void wxGenericScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
565 m_xScrollingEnabled
= x_scroll
;
566 m_yScrollingEnabled
= y_scroll
;
569 void wxGenericScrolledWindow::GetVirtualSize (int *x
, int *y
) const
572 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
574 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
577 // Where the current view starts from
578 void wxGenericScrolledWindow::GetViewStart (int *x
, int *y
) const
581 *x
= m_xScrollPosition
;
583 *y
= m_yScrollPosition
;
586 void wxGenericScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
589 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
591 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
594 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
597 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
599 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
602 // ----------------------------------------------------------------------------
604 // ----------------------------------------------------------------------------
606 // Default OnSize resets scrollbars, if any
607 void wxGenericScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
609 #if wxUSE_CONSTRAINTS
617 // This calls OnDraw, having adjusted the origin according to the current
619 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
627 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
628 // compatibility here - if we used OnKeyDown(), the programs which process
629 // arrows themselves in their OnChar() would never get the message and like
630 // this they always have the priority
631 void wxGenericScrolledWindow::OnChar(wxKeyEvent
& event
)
633 int stx
, sty
, // view origin
634 szx
, szy
, // view size (total)
635 clix
, cliy
; // view size (on screen)
637 ViewStart(&stx
, &sty
);
638 GetClientSize(&clix
, &cliy
);
639 GetVirtualSize(&szx
, &szy
);
641 if( m_xScrollPixelsPerLine
)
643 clix
/= m_xScrollPixelsPerLine
;
644 szx
/= m_xScrollPixelsPerLine
;
651 if( m_yScrollPixelsPerLine
)
653 cliy
/= m_yScrollPixelsPerLine
;
654 szy
/= m_yScrollPixelsPerLine
;
663 switch ( event
.KeyCode() )
667 dsty
= sty
- (5 * cliy
/ 6);
668 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
673 Scroll(-1, sty
+ (5 * cliy
/ 6));
677 Scroll(0, event
.ControlDown() ? 0 : -1);
681 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);