1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxScrolledWindow, wxScrolledControl and wxScrollHelper
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_SCROLWIN_H_BASE_
13 #define _WX_SCROLWIN_H_BASE_
17 class WXDLLIMPEXP_FWD_CORE wxScrollHelperEvtHandler
;
18 class WXDLLIMPEXP_FWD_BASE wxTimer
;
20 // default scrolled window style: scroll in both directions
21 #define wxScrolledWindowStyle (wxHSCROLL | wxVSCROLL)
23 // values for the second argument of wxScrollHelper::ShowScrollbars()
24 enum wxScrollbarVisibility
26 wxSHOW_SB_NEVER
= -1, // never show the scrollbar at all
27 wxSHOW_SB_DEFAULT
, // show scrollbar only if it is needed
28 wxSHOW_SB_ALWAYS
// always show scrollbar, even if not needed
31 // ----------------------------------------------------------------------------
32 // The hierarchy of scrolling classes is a bit complicated because we want to
33 // put as much functionality as possible in a mix-in class not deriving from
34 // wxWindow so that other classes could derive from the same base class on all
35 // platforms irrespectively of whether they are native controls (and hence
36 // don't use our scrolling) or not.
44 // wxWindow wxScrollHelper
48 // | wxScrolledWindow /
57 // ----------------------------------------------------------------------------
59 class WXDLLIMPEXP_CORE wxScrollHelperBase
62 // ctor must be given the associated window
63 wxScrollHelperBase(wxWindow
*winToScroll
);
64 virtual ~wxScrollHelperBase();
66 // configure the scrolling
67 virtual void SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
68 int noUnitsX
, int noUnitsY
,
69 int xPos
= 0, int yPos
= 0,
70 bool noRefresh
= false );
72 // scroll to the given (in logical coords) position
74 // notice that for backwards compatibility reasons Scroll() is virtual as
75 // the existing code could override it but new code should override
77 virtual void Scroll(int x
, int y
) { DoScroll(x
, y
); }
78 virtual void Scroll(const wxPoint
& pt
) { DoScroll(pt
.x
, pt
.y
); }
80 // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
81 int GetScrollPageSize(int orient
) const;
82 void SetScrollPageSize(int orient
, int pageSize
);
84 // get the number of lines the window can scroll,
85 // returns 0 if no scrollbars are there.
86 int GetScrollLines( int orient
) const;
88 // Set the x, y scrolling increments.
89 void SetScrollRate( int xstep
, int ystep
);
91 // get the size of one logical unit in physical ones
92 void GetScrollPixelsPerUnit(int *pixelsPerUnitX
, int *pixelsPerUnitY
) const;
94 // Set scrollbar visibility: it is possible to show scrollbar only if it is
95 // needed (i.e. if our virtual size is greater than the current size of the
96 // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
97 // which case you should provide some other way to scroll the window as the
98 // user wouldn't be able to do it at all)
99 void ShowScrollbars(wxScrollbarVisibility horz
, wxScrollbarVisibility vert
)
101 DoShowScrollbars(horz
, vert
);
104 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
105 // scrolls the canvas and only a bit of the canvas is invalidated; no
106 // Clear() is necessary. If false, the whole canvas is invalidated and a
107 // Clear() is necessary. Disable for when the scroll increment is used to
108 // actually scroll a non-constant distance
109 virtual void EnableScrolling(bool x_scrolling
, bool y_scrolling
);
111 // Disable use of keyboard keys for scrolling. By default cursor movement
112 // keys (including Home, End, Page Up and Down) are used to scroll the
113 // window appropriately. If the derived class uses these keys for something
114 // else, e.g. changing the currently selected item, this function can be
115 // used to disable this behaviour as it's not only not necessary then but
116 // can actually be actively harmful if another object forwards a keyboard
117 // event corresponding to one of the above keys to us using
118 // ProcessWindowEvent() because the event will always be processed which
119 // can be undesirable.
120 void DisableKeyboardScrolling() { m_kbdScrollingEnabled
= false; }
122 // Get the view start
123 void GetViewStart(int *x
, int *y
) const { DoGetViewStart(x
, y
); }
125 wxPoint
GetViewStart() const
128 DoGetViewStart(&pt
.x
, &pt
.y
);
132 // Set the scale factor, used in PrepareDC
133 void SetScale(double xs
, double ys
) { m_scaleX
= xs
; m_scaleY
= ys
; }
134 double GetScaleX() const { return m_scaleX
; }
135 double GetScaleY() const { return m_scaleY
; }
137 // translate between scrolled and unscrolled coordinates
138 void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
139 { DoCalcScrolledPosition(x
, y
, xx
, yy
); }
140 wxPoint
CalcScrolledPosition(const wxPoint
& pt
) const
143 DoCalcScrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
147 void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
148 { DoCalcUnscrolledPosition(x
, y
, xx
, yy
); }
149 wxPoint
CalcUnscrolledPosition(const wxPoint
& pt
) const
152 DoCalcUnscrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
156 void DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
157 void DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
159 // Adjust the scrollbars
160 virtual void AdjustScrollbars() = 0;
162 // Calculate scroll increment
163 int CalcScrollInc(wxScrollWinEvent
& event
);
165 // Normally the wxScrolledWindow will scroll itself, but in some rare
166 // occasions you might want it to scroll [part of] another window (e.g. a
167 // child of it in order to scroll only a portion the area between the
168 // scrollbars (spreadsheet: only cell area will move).
169 void SetTargetWindow(wxWindow
*target
);
170 wxWindow
*GetTargetWindow() const;
172 void SetTargetRect(const wxRect
& rect
) { m_rectToScroll
= rect
; }
173 wxRect
GetTargetRect() const { return m_rectToScroll
; }
175 // Override this function to draw the graphic (or just process EVT_PAINT)
176 virtual void OnDraw(wxDC
& WXUNUSED(dc
)) { }
178 // change the DC origin according to the scroll position.
179 virtual void DoPrepareDC(wxDC
& dc
);
181 // are we generating the autoscroll events?
182 bool IsAutoScrolling() const { return m_timerAutoScroll
!= NULL
; }
184 // stop generating the scroll events when mouse is held outside the window
185 void StopAutoScrolling();
187 // this method can be overridden in a derived class to forbid sending the
188 // auto scroll events - note that unlike StopAutoScrolling() it doesn't
189 // stop the timer, so it will be called repeatedly and will typically
190 // return different values depending on the current mouse position
192 // the base class version just returns true
193 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
195 // the methods to be called from the window event handlers
196 void HandleOnScroll(wxScrollWinEvent
& event
);
197 void HandleOnSize(wxSizeEvent
& event
);
198 void HandleOnPaint(wxPaintEvent
& event
);
199 void HandleOnChar(wxKeyEvent
& event
);
200 void HandleOnMouseEnter(wxMouseEvent
& event
);
201 void HandleOnMouseLeave(wxMouseEvent
& event
);
203 void HandleOnMouseWheel(wxMouseEvent
& event
);
204 #endif // wxUSE_MOUSEWHEEL
205 void HandleOnChildFocus(wxChildFocusEvent
& event
);
207 #if WXWIN_COMPATIBILITY_2_8
209 void OnScroll(wxScrollWinEvent
& event
) { HandleOnScroll(event
); }
211 #endif // WXWIN_COMPATIBILITY_2_8
214 // get pointer to our scroll rect if we use it or NULL
215 const wxRect
*GetScrollRect() const
217 return m_rectToScroll
.width
!= 0 ? &m_rectToScroll
: NULL
;
220 // get the size of the target window
221 wxSize
GetTargetSize() const
223 return m_rectToScroll
.width
!= 0 ? m_rectToScroll
.GetSize()
224 : m_targetWindow
->GetClientSize();
227 void GetTargetSize(int *w
, int *h
) const
229 wxSize size
= GetTargetSize();
236 // implementation of public methods with the same name
237 virtual void DoGetViewStart(int *x
, int *y
) const;
238 virtual void DoScroll(int x
, int y
) = 0;
239 virtual void DoShowScrollbars(wxScrollbarVisibility horz
,
240 wxScrollbarVisibility vert
) = 0;
242 // implementations of various wxWindow virtual methods which should be
243 // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
245 void ScrollDoSetVirtualSize(int x
, int y
);
246 wxSize
ScrollGetBestVirtualSize() const;
248 // change just the target window (unlike SetWindow which changes m_win as
250 void DoSetTargetWindow(wxWindow
*target
);
252 // delete the event handler we installed
253 void DeleteEvtHandler();
255 // calls wxScrollHelperEvtHandler::ResetDrawnFlag(), see explanation
256 // in wxScrollHelperEvtHandler::ProcessEvent()
257 void ResetDrawnFlag();
259 // this function should be overridden to return the size available for
260 // m_targetWindow inside m_win of the given size
262 // the default implementation is only good for m_targetWindow == m_win
263 // case, if we're scrolling a subwindow you must override this method
264 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
)
266 // returning just size from here is wrong but it was decided that it is
267 // not wrong enough to break the existing code (which doesn't override
268 // this recently added function at all) by adding this assert
270 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
282 wxRect m_rectToScroll
;
284 wxTimer
*m_timerAutoScroll
;
286 int m_xScrollPixelsPerLine
;
287 int m_yScrollPixelsPerLine
;
288 int m_xScrollPosition
;
289 int m_yScrollPosition
;
292 int m_xScrollLinesPerPage
;
293 int m_yScrollLinesPerPage
;
295 bool m_xScrollingEnabled
;
296 bool m_yScrollingEnabled
;
298 bool m_kbdScrollingEnabled
;
302 #endif // wxUSE_MOUSEWHEEL
304 wxScrollHelperEvtHandler
*m_handler
;
306 wxDECLARE_NO_COPY_CLASS(wxScrollHelperBase
);
309 // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
310 // methods to corresponding wxScrollHelper methods
311 #define WX_FORWARD_TO_SCROLL_HELPER() \
313 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
314 virtual bool Layout() { return ScrollLayout(); } \
315 virtual void DoSetVirtualSize(int x, int y) \
316 { ScrollDoSetVirtualSize(x, y); } \
317 virtual wxSize GetBestVirtualSize() const \
318 { return ScrollGetBestVirtualSize(); }
320 // include the declaration of the real wxScrollHelper
321 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
322 #include "wx/gtk/scrolwin.h"
323 #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
324 #include "wx/gtk1/scrolwin.h"
326 #define wxHAS_GENERIC_SCROLLWIN
327 #include "wx/generic/scrolwin.h"
330 // ----------------------------------------------------------------------------
331 // wxScrolled<T>: a wxWindow which knows how to scroll
332 // ----------------------------------------------------------------------------
334 // helper class for wxScrolled<T> below
335 struct WXDLLIMPEXP_CORE wxScrolledT_Helper
337 static wxSize
FilterBestSize(const wxWindow
*win
,
338 const wxScrollHelper
*helper
,
339 const wxSize
& origBest
);
341 static WXLRESULT
FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT origResult
);
345 // Scrollable window base on window type T. This used to be wxScrolledWindow,
346 // but wxScrolledWindow includes wxControlContainer functionality and that's
347 // not always desirable.
349 class wxScrolled
: public T
,
350 public wxScrollHelper
,
351 private wxScrolledT_Helper
354 wxScrolled() : wxScrollHelper(this) { }
355 wxScrolled(wxWindow
*parent
,
356 wxWindowID winid
= wxID_ANY
,
357 const wxPoint
& pos
= wxDefaultPosition
,
358 const wxSize
& size
= wxDefaultSize
,
359 long style
= wxScrolledWindowStyle
,
360 const wxString
& name
= wxPanelNameStr
)
361 : wxScrollHelper(this)
363 Create(parent
, winid
, pos
, size
, style
, name
);
366 bool Create(wxWindow
*parent
,
368 const wxPoint
& pos
= wxDefaultPosition
,
369 const wxSize
& size
= wxDefaultSize
,
370 long style
= wxScrolledWindowStyle
,
371 const wxString
& name
= wxPanelNameStr
)
373 m_targetWindow
= this;
376 this->MacSetClipChildren(true);
379 this->Connect(wxEVT_PAINT
, wxPaintEventHandler(wxScrolled::OnPaint
));
381 // by default, we're scrollable in both directions (but if one of the
382 // styles is specified explicitly, we shouldn't add the other one
384 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
385 style
|= wxHSCROLL
| wxVSCROLL
;
387 if ( style
& wxALWAYS_SHOW_SB
)
388 ShowScrollbars(wxSHOW_SB_ALWAYS
, wxSHOW_SB_ALWAYS
);
390 return T::Create(parent
, winid
, pos
, size
, style
, name
);
393 // we need to return a special WM_GETDLGCODE value to process just the
394 // arrows but let the other navigation characters through
396 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
398 return FilterMSWWindowProc(nMsg
, T::MSWWindowProc(nMsg
, wParam
, lParam
));
402 WX_FORWARD_TO_SCROLL_HELPER()
405 virtual wxSize
DoGetBestSize() const
407 return FilterBestSize(this, this, T::DoGetBestSize());
411 // this is needed for wxEVT_PAINT processing hack described in
412 // wxScrollHelperEvtHandler::ProcessEvent()
413 void OnPaint(wxPaintEvent
& event
)
415 // the user code didn't really draw the window if we got here, so set
416 // this flag to try to call OnDraw() later
421 // VC++ 6 gives warning for the declaration of template member function
422 // without definition
424 wxDECLARE_NO_COPY_CLASS(wxScrolled
);
429 // disable the warning about non dll-interface class used as base for
430 // dll-interface class: it's harmless in this case
431 #pragma warning(push)
432 #pragma warning(disable:4275)
435 // for compatibility with existing code, we provide wxScrolledWindow
436 // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
437 // want wxScrolledWindow to show in wxRTTI information (the class is widely
438 // used and likelihood of its wxRTTI information being used too is high):
439 class WXDLLIMPEXP_CORE wxScrolledWindow
: public wxScrolled
<wxPanel
>
442 wxScrolledWindow() : wxScrolled
<wxPanel
>() {}
443 wxScrolledWindow(wxWindow
*parent
,
444 wxWindowID winid
= wxID_ANY
,
445 const wxPoint
& pos
= wxDefaultPosition
,
446 const wxSize
& size
= wxDefaultSize
,
447 long style
= wxScrolledWindowStyle
,
448 const wxString
& name
= wxPanelNameStr
)
449 : wxScrolled
<wxPanel
>(parent
, winid
, pos
, size
, style
, name
) {}
451 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow
)
454 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;
460 #endif // _WX_SCROLWIN_H_BASE_