1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxScrolledWindow, wxScrolledControl and wxScrollHelper
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_SCROLWIN_H_BASE_
12 #define _WX_SCROLWIN_H_BASE_
16 class WXDLLIMPEXP_FWD_CORE wxScrollHelperEvtHandler
;
17 class WXDLLIMPEXP_FWD_BASE wxTimer
;
19 // default scrolled window style: scroll in both directions
20 #define wxScrolledWindowStyle (wxHSCROLL | wxVSCROLL)
22 // values for the second argument of wxScrollHelper::ShowScrollbars()
23 enum wxScrollbarVisibility
25 wxSHOW_SB_NEVER
= -1, // never show the scrollbar at all
26 wxSHOW_SB_DEFAULT
, // show scrollbar only if it is needed
27 wxSHOW_SB_ALWAYS
// always show scrollbar, even if not needed
30 // ----------------------------------------------------------------------------
31 // The hierarchy of scrolling classes is a bit complicated because we want to
32 // put as much functionality as possible in a mix-in class not deriving from
33 // wxWindow so that other classes could derive from the same base class on all
34 // platforms irrespectively of whether they are native controls (and hence
35 // don't use our scrolling) or not.
39 // wxAnyScrollHelperBase
47 // wxWindow wxScrollHelper
51 // | wxScrolledWindow /
60 // ----------------------------------------------------------------------------
62 // This class allows reusing some of wxScrollHelperBase functionality in
63 // wxVarScrollHelperBase in wx/vscroll.h without duplicating its code.
64 class WXDLLIMPEXP_CORE wxAnyScrollHelperBase
67 wxEXPLICIT
wxAnyScrollHelperBase(wxWindow
* win
);
69 // Simple accessor for the window that is really being scrolled.
70 wxWindow
*GetTargetWindow() const { return m_targetWindow
; }
73 // the window that receives the scroll events and the window to actually
74 // scroll, respectively
79 // This is the class containing the guts of (uniform) scrolling logic.
80 class WXDLLIMPEXP_CORE wxScrollHelperBase
: public wxAnyScrollHelperBase
83 // ctor must be given the associated window
84 wxScrollHelperBase(wxWindow
*winToScroll
);
85 virtual ~wxScrollHelperBase();
87 // configure the scrolling
88 virtual void SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
89 int noUnitsX
, int noUnitsY
,
90 int xPos
= 0, int yPos
= 0,
91 bool noRefresh
= false );
93 // scroll to the given (in logical coords) position
95 // notice that for backwards compatibility reasons Scroll() is virtual as
96 // the existing code could override it but new code should override
98 virtual void Scroll(int x
, int y
) { DoScroll(x
, y
); }
99 virtual void Scroll(const wxPoint
& pt
) { DoScroll(pt
.x
, pt
.y
); }
101 // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
102 int GetScrollPageSize(int orient
) const;
103 void SetScrollPageSize(int orient
, int pageSize
);
105 // get the number of lines the window can scroll,
106 // returns 0 if no scrollbars are there.
107 int GetScrollLines( int orient
) const;
109 // Set the x, y scrolling increments.
110 void SetScrollRate( int xstep
, int ystep
);
112 // get the size of one logical unit in physical ones
113 void GetScrollPixelsPerUnit(int *pixelsPerUnitX
, int *pixelsPerUnitY
) const;
115 // Set scrollbar visibility: it is possible to show scrollbar only if it is
116 // needed (i.e. if our virtual size is greater than the current size of the
117 // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
118 // which case you should provide some other way to scroll the window as the
119 // user wouldn't be able to do it at all)
120 void ShowScrollbars(wxScrollbarVisibility horz
, wxScrollbarVisibility vert
)
122 DoShowScrollbars(horz
, vert
);
125 // Test whether the specified scrollbar is shown.
126 virtual bool IsScrollbarShown(int orient
) const = 0;
128 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
129 // scrolls the canvas and only a bit of the canvas is invalidated; no
130 // Clear() is necessary. If false, the whole canvas is invalidated and a
131 // Clear() is necessary. Disable for when the scroll increment is used to
132 // actually scroll a non-constant distance
134 // Notice that calling this method with a false argument doesn't disable
135 // scrolling the window in this direction, it just changes the mechanism by
136 // which it is implemented to not use wxWindow::ScrollWindow().
137 virtual void EnableScrolling(bool x_scrolling
, bool y_scrolling
);
139 // Disable use of keyboard keys for scrolling. By default cursor movement
140 // keys (including Home, End, Page Up and Down) are used to scroll the
141 // window appropriately. If the derived class uses these keys for something
142 // else, e.g. changing the currently selected item, this function can be
143 // used to disable this behaviour as it's not only not necessary then but
144 // can actually be actively harmful if another object forwards a keyboard
145 // event corresponding to one of the above keys to us using
146 // ProcessWindowEvent() because the event will always be processed which
147 // can be undesirable.
148 void DisableKeyboardScrolling() { m_kbdScrollingEnabled
= false; }
150 // Get the view start
151 void GetViewStart(int *x
, int *y
) const { DoGetViewStart(x
, y
); }
153 wxPoint
GetViewStart() const
156 DoGetViewStart(&pt
.x
, &pt
.y
);
160 // Set the scale factor, used in PrepareDC
161 void SetScale(double xs
, double ys
) { m_scaleX
= xs
; m_scaleY
= ys
; }
162 double GetScaleX() const { return m_scaleX
; }
163 double GetScaleY() const { return m_scaleY
; }
165 // translate between scrolled and unscrolled coordinates
166 void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
167 { DoCalcScrolledPosition(x
, y
, xx
, yy
); }
168 wxPoint
CalcScrolledPosition(const wxPoint
& pt
) const
171 DoCalcScrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
175 void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
176 { DoCalcUnscrolledPosition(x
, y
, xx
, yy
); }
177 wxPoint
CalcUnscrolledPosition(const wxPoint
& pt
) const
180 DoCalcUnscrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
184 void DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
185 void DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
187 // Adjust the scrollbars
188 virtual void AdjustScrollbars() = 0;
190 // Calculate scroll increment
191 int CalcScrollInc(wxScrollWinEvent
& event
);
193 // Normally the wxScrolledWindow will scroll itself, but in some rare
194 // occasions you might want it to scroll [part of] another window (e.g. a
195 // child of it in order to scroll only a portion the area between the
196 // scrollbars (spreadsheet: only cell area will move).
197 void SetTargetWindow(wxWindow
*target
);
199 void SetTargetRect(const wxRect
& rect
) { m_rectToScroll
= rect
; }
200 wxRect
GetTargetRect() const { return m_rectToScroll
; }
202 // Override this function to draw the graphic (or just process EVT_PAINT)
203 virtual void OnDraw(wxDC
& WXUNUSED(dc
)) { }
205 // change the DC origin according to the scroll position.
206 virtual void DoPrepareDC(wxDC
& dc
);
208 // are we generating the autoscroll events?
209 bool IsAutoScrolling() const { return m_timerAutoScroll
!= NULL
; }
211 // stop generating the scroll events when mouse is held outside the window
212 void StopAutoScrolling();
214 // this method can be overridden in a derived class to forbid sending the
215 // auto scroll events - note that unlike StopAutoScrolling() it doesn't
216 // stop the timer, so it will be called repeatedly and will typically
217 // return different values depending on the current mouse position
219 // the base class version just returns true
220 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
222 // the methods to be called from the window event handlers
223 void HandleOnScroll(wxScrollWinEvent
& event
);
224 void HandleOnSize(wxSizeEvent
& event
);
225 void HandleOnPaint(wxPaintEvent
& event
);
226 void HandleOnChar(wxKeyEvent
& event
);
227 void HandleOnMouseEnter(wxMouseEvent
& event
);
228 void HandleOnMouseLeave(wxMouseEvent
& event
);
230 void HandleOnMouseWheel(wxMouseEvent
& event
);
231 #endif // wxUSE_MOUSEWHEEL
232 void HandleOnChildFocus(wxChildFocusEvent
& event
);
234 #if WXWIN_COMPATIBILITY_2_8
236 void OnScroll(wxScrollWinEvent
& event
) { HandleOnScroll(event
); }
238 #endif // WXWIN_COMPATIBILITY_2_8
241 // get pointer to our scroll rect if we use it or NULL
242 const wxRect
*GetScrollRect() const
244 return m_rectToScroll
.width
!= 0 ? &m_rectToScroll
: NULL
;
247 // get the size of the target window
248 wxSize
GetTargetSize() const
250 return m_rectToScroll
.width
!= 0 ? m_rectToScroll
.GetSize()
251 : m_targetWindow
->GetClientSize();
254 void GetTargetSize(int *w
, int *h
) const
256 wxSize size
= GetTargetSize();
263 // implementation of public methods with the same name
264 virtual void DoGetViewStart(int *x
, int *y
) const;
265 virtual void DoScroll(int x
, int y
) = 0;
266 virtual void DoShowScrollbars(wxScrollbarVisibility horz
,
267 wxScrollbarVisibility vert
) = 0;
269 // implementations of various wxWindow virtual methods which should be
270 // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
272 void ScrollDoSetVirtualSize(int x
, int y
);
273 wxSize
ScrollGetBestVirtualSize() const;
275 // change just the target window (unlike SetWindow which changes m_win as
277 void DoSetTargetWindow(wxWindow
*target
);
279 // delete the event handler we installed
280 void DeleteEvtHandler();
282 // this function should be overridden to return the size available for
283 // m_targetWindow inside m_win of the given size
285 // the default implementation is only good for m_targetWindow == m_win
286 // case, if we're scrolling a subwindow you must override this method
287 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
)
289 // returning just size from here is wrong but it was decided that it is
290 // not wrong enough to break the existing code (which doesn't override
291 // this recently added function at all) by adding this assert
293 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
302 wxRect m_rectToScroll
;
304 wxTimer
*m_timerAutoScroll
;
306 // The number of pixels to scroll in horizontal and vertical directions
309 // If 0, means that the scrolling in the given direction is disabled.
310 int m_xScrollPixelsPerLine
;
311 int m_yScrollPixelsPerLine
;
312 int m_xScrollPosition
;
313 int m_yScrollPosition
;
316 int m_xScrollLinesPerPage
;
317 int m_yScrollLinesPerPage
;
319 bool m_xScrollingEnabled
;
320 bool m_yScrollingEnabled
;
322 bool m_kbdScrollingEnabled
;
326 #endif // wxUSE_MOUSEWHEEL
328 wxScrollHelperEvtHandler
*m_handler
;
330 wxDECLARE_NO_COPY_CLASS(wxScrollHelperBase
);
333 // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
334 // methods to corresponding wxScrollHelper methods
335 #define WX_FORWARD_TO_SCROLL_HELPER() \
337 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
338 virtual bool Layout() { return ScrollLayout(); } \
339 virtual bool CanScroll(int orient) const \
340 { return IsScrollbarShown(orient); } \
341 virtual void DoSetVirtualSize(int x, int y) \
342 { ScrollDoSetVirtualSize(x, y); } \
343 virtual wxSize GetBestVirtualSize() const \
344 { return ScrollGetBestVirtualSize(); }
346 // include the declaration of the real wxScrollHelper
347 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
348 #include "wx/gtk/scrolwin.h"
349 #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
350 #include "wx/gtk1/scrolwin.h"
352 #define wxHAS_GENERIC_SCROLLWIN
353 #include "wx/generic/scrolwin.h"
356 // ----------------------------------------------------------------------------
357 // wxScrolled<T>: a wxWindow which knows how to scroll
358 // ----------------------------------------------------------------------------
360 // helper class for wxScrolled<T> below
361 struct WXDLLIMPEXP_CORE wxScrolledT_Helper
363 static wxSize
FilterBestSize(const wxWindow
*win
,
364 const wxScrollHelper
*helper
,
365 const wxSize
& origBest
);
367 static WXLRESULT
FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT origResult
);
371 // Scrollable window base on window type T. This used to be wxScrolledWindow,
372 // but wxScrolledWindow includes wxControlContainer functionality and that's
373 // not always desirable.
375 class wxScrolled
: public T
,
376 public wxScrollHelper
,
377 private wxScrolledT_Helper
380 wxScrolled() : wxScrollHelper(this) { }
381 wxScrolled(wxWindow
*parent
,
382 wxWindowID winid
= wxID_ANY
,
383 const wxPoint
& pos
= wxDefaultPosition
,
384 const wxSize
& size
= wxDefaultSize
,
385 long style
= wxScrolledWindowStyle
,
386 const wxString
& name
= wxPanelNameStr
)
387 : wxScrollHelper(this)
389 Create(parent
, winid
, pos
, size
, style
, name
);
392 bool Create(wxWindow
*parent
,
394 const wxPoint
& pos
= wxDefaultPosition
,
395 const wxSize
& size
= wxDefaultSize
,
396 long style
= wxScrolledWindowStyle
,
397 const wxString
& name
= wxPanelNameStr
)
399 m_targetWindow
= this;
402 this->MacSetClipChildren(true);
405 // by default, we're scrollable in both directions (but if one of the
406 // styles is specified explicitly, we shouldn't add the other one
408 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
409 style
|= wxHSCROLL
| wxVSCROLL
;
412 bool retval
= T::Create(parent
, winid
, pos
, size
, style
, name
);
413 if ( retval
&& (style
& wxALWAYS_SHOW_SB
) )
414 ShowScrollbars(wxSHOW_SB_ALWAYS
, wxSHOW_SB_ALWAYS
);
417 if ( style
& wxALWAYS_SHOW_SB
)
418 ShowScrollbars(wxSHOW_SB_ALWAYS
, wxSHOW_SB_ALWAYS
);
420 return T::Create(parent
, winid
, pos
, size
, style
, name
);
425 // we need to return a special WM_GETDLGCODE value to process just the
426 // arrows but let the other navigation characters through
427 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
429 return FilterMSWWindowProc(nMsg
, T::MSWWindowProc(nMsg
, wParam
, lParam
));
432 // Take into account the scroll origin.
433 virtual void MSWAdjustBrushOrg(int* xOrg
, int* yOrg
) const
435 CalcUnscrolledPosition(*xOrg
, *yOrg
, xOrg
, yOrg
);
439 WX_FORWARD_TO_SCROLL_HELPER()
442 virtual wxSize
DoGetBestSize() const
444 return FilterBestSize(this, this, T::DoGetBestSize());
448 // VC++ 6 gives warning for the declaration of template member function
449 // without definition
451 wxDECLARE_NO_COPY_CLASS(wxScrolled
);
456 // disable the warning about non dll-interface class used as base for
457 // dll-interface class: it's harmless in this case
458 #pragma warning(push)
459 #pragma warning(disable:4275)
462 // for compatibility with existing code, we provide wxScrolledWindow
463 // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
464 // want wxScrolledWindow to show in wxRTTI information (the class is widely
465 // used and likelihood of its wxRTTI information being used too is high):
466 class WXDLLIMPEXP_CORE wxScrolledWindow
: public wxScrolled
<wxPanel
>
469 wxScrolledWindow() : wxScrolled
<wxPanel
>() {}
470 wxScrolledWindow(wxWindow
*parent
,
471 wxWindowID winid
= wxID_ANY
,
472 const wxPoint
& pos
= wxDefaultPosition
,
473 const wxSize
& size
= wxDefaultSize
,
474 long style
= wxScrolledWindowStyle
,
475 const wxString
& name
= wxPanelNameStr
)
476 : wxScrolled
<wxPanel
>(parent
, winid
, pos
, size
, style
, name
) {}
478 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow
)
481 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;
487 #endif // _WX_SCROLWIN_H_BASE_