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
);
68 virtual ~wxAnyScrollHelperBase() {}
70 // Disable use of keyboard keys for scrolling. By default cursor movement
71 // keys (including Home, End, Page Up and Down) are used to scroll the
72 // window appropriately. If the derived class uses these keys for something
73 // else, e.g. changing the currently selected item, this function can be
74 // used to disable this behaviour as it's not only not necessary then but
75 // can actually be actively harmful if another object forwards a keyboard
76 // event corresponding to one of the above keys to us using
77 // ProcessWindowEvent() because the event will always be processed which
78 // can be undesirable.
79 void DisableKeyboardScrolling() { m_kbdScrollingEnabled
= false; }
81 // Override this function to draw the graphic (or just process EVT_PAINT)
82 virtual void OnDraw(wxDC
& WXUNUSED(dc
)) { }
84 // change the DC origin according to the scroll position.
85 virtual void DoPrepareDC(wxDC
& dc
) = 0;
87 // Simple accessor for the window that is really being scrolled.
88 wxWindow
*GetTargetWindow() const { return m_targetWindow
; }
91 // The methods called from the window event handlers.
92 void HandleOnChar(wxKeyEvent
& event
);
93 void HandleOnPaint(wxPaintEvent
& event
);
96 // the window that receives the scroll events and the window to actually
97 // scroll, respectively
101 // whether cursor keys should scroll the window
102 bool m_kbdScrollingEnabled
;
105 // This is the class containing the guts of (uniform) scrolling logic.
106 class WXDLLIMPEXP_CORE wxScrollHelperBase
: public wxAnyScrollHelperBase
109 // ctor must be given the associated window
110 wxScrollHelperBase(wxWindow
*winToScroll
);
111 virtual ~wxScrollHelperBase();
113 // configure the scrolling
114 virtual void SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
115 int noUnitsX
, int noUnitsY
,
116 int xPos
= 0, int yPos
= 0,
117 bool noRefresh
= false );
119 // scroll to the given (in logical coords) position
121 // notice that for backwards compatibility reasons Scroll() is virtual as
122 // the existing code could override it but new code should override
123 // DoScroll() instead
124 virtual void Scroll(int x
, int y
) { DoScroll(x
, y
); }
125 virtual void Scroll(const wxPoint
& pt
) { DoScroll(pt
.x
, pt
.y
); }
127 // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
128 int GetScrollPageSize(int orient
) const;
129 void SetScrollPageSize(int orient
, int pageSize
);
131 // get the number of lines the window can scroll,
132 // returns 0 if no scrollbars are there.
133 int GetScrollLines( int orient
) const;
135 // Set the x, y scrolling increments.
136 void SetScrollRate( int xstep
, int ystep
);
138 // get the size of one logical unit in physical ones
139 void GetScrollPixelsPerUnit(int *pixelsPerUnitX
, int *pixelsPerUnitY
) const;
141 // Set scrollbar visibility: it is possible to show scrollbar only if it is
142 // needed (i.e. if our virtual size is greater than the current size of the
143 // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
144 // which case you should provide some other way to scroll the window as the
145 // user wouldn't be able to do it at all)
146 void ShowScrollbars(wxScrollbarVisibility horz
, wxScrollbarVisibility vert
)
148 DoShowScrollbars(horz
, vert
);
151 // Test whether the specified scrollbar is shown.
152 virtual bool IsScrollbarShown(int orient
) const = 0;
154 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
155 // scrolls the canvas and only a bit of the canvas is invalidated; no
156 // Clear() is necessary. If false, the whole canvas is invalidated and a
157 // Clear() is necessary. Disable for when the scroll increment is used to
158 // actually scroll a non-constant distance
160 // Notice that calling this method with a false argument doesn't disable
161 // scrolling the window in this direction, it just changes the mechanism by
162 // which it is implemented to not use wxWindow::ScrollWindow().
163 virtual void EnableScrolling(bool x_scrolling
, bool y_scrolling
);
165 // Get the view start
166 void GetViewStart(int *x
, int *y
) const { DoGetViewStart(x
, y
); }
168 wxPoint
GetViewStart() const
171 DoGetViewStart(&pt
.x
, &pt
.y
);
175 // Set the scale factor, used in PrepareDC
176 void SetScale(double xs
, double ys
) { m_scaleX
= xs
; m_scaleY
= ys
; }
177 double GetScaleX() const { return m_scaleX
; }
178 double GetScaleY() const { return m_scaleY
; }
180 // translate between scrolled and unscrolled coordinates
181 void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
182 { DoCalcScrolledPosition(x
, y
, xx
, yy
); }
183 wxPoint
CalcScrolledPosition(const wxPoint
& pt
) const
186 DoCalcScrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
190 void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
191 { DoCalcUnscrolledPosition(x
, y
, xx
, yy
); }
192 wxPoint
CalcUnscrolledPosition(const wxPoint
& pt
) const
195 DoCalcUnscrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
199 void DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
200 void DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
202 // Adjust the scrollbars
203 virtual void AdjustScrollbars() = 0;
205 // Calculate scroll increment
206 int CalcScrollInc(wxScrollWinEvent
& event
);
208 // Normally the wxScrolledWindow will scroll itself, but in some rare
209 // occasions you might want it to scroll [part of] another window (e.g. a
210 // child of it in order to scroll only a portion the area between the
211 // scrollbars (spreadsheet: only cell area will move).
212 void SetTargetWindow(wxWindow
*target
);
214 void SetTargetRect(const wxRect
& rect
) { m_rectToScroll
= rect
; }
215 wxRect
GetTargetRect() const { return m_rectToScroll
; }
217 virtual void DoPrepareDC(wxDC
& dc
);
219 // are we generating the autoscroll events?
220 bool IsAutoScrolling() const { return m_timerAutoScroll
!= NULL
; }
222 // stop generating the scroll events when mouse is held outside the window
223 void StopAutoScrolling();
225 // this method can be overridden in a derived class to forbid sending the
226 // auto scroll events - note that unlike StopAutoScrolling() it doesn't
227 // stop the timer, so it will be called repeatedly and will typically
228 // return different values depending on the current mouse position
230 // the base class version just returns true
231 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
233 // the methods to be called from the window event handlers
234 void HandleOnScroll(wxScrollWinEvent
& event
);
235 void HandleOnSize(wxSizeEvent
& event
);
236 void HandleOnMouseEnter(wxMouseEvent
& event
);
237 void HandleOnMouseLeave(wxMouseEvent
& event
);
239 void HandleOnMouseWheel(wxMouseEvent
& event
);
240 #endif // wxUSE_MOUSEWHEEL
241 void HandleOnChildFocus(wxChildFocusEvent
& event
);
243 #if WXWIN_COMPATIBILITY_2_8
245 void OnScroll(wxScrollWinEvent
& event
) { HandleOnScroll(event
); }
247 #endif // WXWIN_COMPATIBILITY_2_8
250 // get pointer to our scroll rect if we use it or NULL
251 const wxRect
*GetScrollRect() const
253 return m_rectToScroll
.width
!= 0 ? &m_rectToScroll
: NULL
;
256 // get the size of the target window
257 wxSize
GetTargetSize() const
259 return m_rectToScroll
.width
!= 0 ? m_rectToScroll
.GetSize()
260 : m_targetWindow
->GetClientSize();
263 void GetTargetSize(int *w
, int *h
) const
265 wxSize size
= GetTargetSize();
272 // implementation of public methods with the same name
273 virtual void DoGetViewStart(int *x
, int *y
) const;
274 virtual void DoScroll(int x
, int y
) = 0;
275 virtual void DoShowScrollbars(wxScrollbarVisibility horz
,
276 wxScrollbarVisibility vert
) = 0;
278 // implementations of various wxWindow virtual methods which should be
279 // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
281 void ScrollDoSetVirtualSize(int x
, int y
);
282 wxSize
ScrollGetBestVirtualSize() const;
284 // change just the target window (unlike SetWindow which changes m_win as
286 void DoSetTargetWindow(wxWindow
*target
);
288 // delete the event handler we installed
289 void DeleteEvtHandler();
291 // this function should be overridden to return the size available for
292 // m_targetWindow inside m_win of the given size
294 // the default implementation is only good for m_targetWindow == m_win
295 // case, if we're scrolling a subwindow you must override this method
296 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
)
298 // returning just size from here is wrong but it was decided that it is
299 // not wrong enough to break the existing code (which doesn't override
300 // this recently added function at all) by adding this assert
302 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
311 wxRect m_rectToScroll
;
313 wxTimer
*m_timerAutoScroll
;
315 // The number of pixels to scroll in horizontal and vertical directions
318 // If 0, means that the scrolling in the given direction is disabled.
319 int m_xScrollPixelsPerLine
;
320 int m_yScrollPixelsPerLine
;
321 int m_xScrollPosition
;
322 int m_yScrollPosition
;
325 int m_xScrollLinesPerPage
;
326 int m_yScrollLinesPerPage
;
328 bool m_xScrollingEnabled
;
329 bool m_yScrollingEnabled
;
333 #endif // wxUSE_MOUSEWHEEL
335 wxScrollHelperEvtHandler
*m_handler
;
337 wxDECLARE_NO_COPY_CLASS(wxScrollHelperBase
);
340 // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
341 // methods to corresponding wxScrollHelper methods
342 #define WX_FORWARD_TO_SCROLL_HELPER() \
344 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
345 virtual bool Layout() { return ScrollLayout(); } \
346 virtual bool CanScroll(int orient) const \
347 { return IsScrollbarShown(orient); } \
348 virtual void DoSetVirtualSize(int x, int y) \
349 { ScrollDoSetVirtualSize(x, y); } \
350 virtual wxSize GetBestVirtualSize() const \
351 { return ScrollGetBestVirtualSize(); }
353 // include the declaration of the real wxScrollHelper
354 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
355 #include "wx/gtk/scrolwin.h"
356 #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
357 #include "wx/gtk1/scrolwin.h"
359 #define wxHAS_GENERIC_SCROLLWIN
360 #include "wx/generic/scrolwin.h"
363 // ----------------------------------------------------------------------------
364 // wxScrolled<T>: a wxWindow which knows how to scroll
365 // ----------------------------------------------------------------------------
367 // helper class for wxScrolled<T> below
368 struct WXDLLIMPEXP_CORE wxScrolledT_Helper
370 static wxSize
FilterBestSize(const wxWindow
*win
,
371 const wxScrollHelper
*helper
,
372 const wxSize
& origBest
);
374 static WXLRESULT
FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT origResult
);
378 // Scrollable window base on window type T. This used to be wxScrolledWindow,
379 // but wxScrolledWindow includes wxControlContainer functionality and that's
380 // not always desirable.
382 class wxScrolled
: public T
,
383 public wxScrollHelper
,
384 private wxScrolledT_Helper
387 wxScrolled() : wxScrollHelper(this) { }
388 wxScrolled(wxWindow
*parent
,
389 wxWindowID winid
= wxID_ANY
,
390 const wxPoint
& pos
= wxDefaultPosition
,
391 const wxSize
& size
= wxDefaultSize
,
392 long style
= wxScrolledWindowStyle
,
393 const wxString
& name
= wxPanelNameStr
)
394 : wxScrollHelper(this)
396 Create(parent
, winid
, pos
, size
, style
, name
);
399 bool Create(wxWindow
*parent
,
401 const wxPoint
& pos
= wxDefaultPosition
,
402 const wxSize
& size
= wxDefaultSize
,
403 long style
= wxScrolledWindowStyle
,
404 const wxString
& name
= wxPanelNameStr
)
406 m_targetWindow
= this;
409 this->MacSetClipChildren(true);
412 // by default, we're scrollable in both directions (but if one of the
413 // styles is specified explicitly, we shouldn't add the other one
415 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
416 style
|= wxHSCROLL
| wxVSCROLL
;
419 bool retval
= T::Create(parent
, winid
, pos
, size
, style
, name
);
420 if ( retval
&& (style
& wxALWAYS_SHOW_SB
) )
421 ShowScrollbars(wxSHOW_SB_ALWAYS
, wxSHOW_SB_ALWAYS
);
424 if ( style
& wxALWAYS_SHOW_SB
)
425 ShowScrollbars(wxSHOW_SB_ALWAYS
, wxSHOW_SB_ALWAYS
);
427 return T::Create(parent
, winid
, pos
, size
, style
, name
);
432 // we need to return a special WM_GETDLGCODE value to process just the
433 // arrows but let the other navigation characters through
434 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
436 return FilterMSWWindowProc(nMsg
, T::MSWWindowProc(nMsg
, wParam
, lParam
));
439 // Take into account the scroll origin.
440 virtual void MSWAdjustBrushOrg(int* xOrg
, int* yOrg
) const
442 CalcUnscrolledPosition(*xOrg
, *yOrg
, xOrg
, yOrg
);
446 WX_FORWARD_TO_SCROLL_HELPER()
449 virtual wxSize
DoGetBestSize() const
451 return FilterBestSize(this, this, T::DoGetBestSize());
455 // VC++ 6 gives warning for the declaration of template member function
456 // without definition
458 wxDECLARE_NO_COPY_CLASS(wxScrolled
);
463 // disable the warning about non dll-interface class used as base for
464 // dll-interface class: it's harmless in this case
465 #pragma warning(push)
466 #pragma warning(disable:4275)
469 // for compatibility with existing code, we provide wxScrolledWindow
470 // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
471 // want wxScrolledWindow to show in wxRTTI information (the class is widely
472 // used and likelihood of its wxRTTI information being used too is high):
473 class WXDLLIMPEXP_CORE wxScrolledWindow
: public wxScrolled
<wxPanel
>
476 wxScrolledWindow() : wxScrolled
<wxPanel
>() {}
477 wxScrolledWindow(wxWindow
*parent
,
478 wxWindowID winid
= wxID_ANY
,
479 const wxPoint
& pos
= wxDefaultPosition
,
480 const wxSize
& size
= wxDefaultSize
,
481 long style
= wxScrolledWindowStyle
,
482 const wxString
& name
= wxPanelNameStr
)
483 : wxScrolled
<wxPanel
>(parent
, winid
, pos
, size
, style
, name
) {}
485 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow
)
488 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;
494 #endif // _WX_SCROLWIN_H_BASE_