1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/scrolwin.h
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
73 void Scroll(int x
, int y
) { DoScroll(x
, y
); }
74 void Scroll(const wxPoint
& pt
) { DoScroll(pt
.x
, pt
.y
); }
76 // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
77 int GetScrollPageSize(int orient
) const;
78 void SetScrollPageSize(int orient
, int pageSize
);
80 // get the number of lines the window can scroll,
81 // returns 0 if no scrollbars are there.
82 int GetScrollLines( int orient
) const;
84 // Set the x, y scrolling increments.
85 void SetScrollRate( int xstep
, int ystep
);
87 // get the size of one logical unit in physical ones
88 void GetScrollPixelsPerUnit(int *pixelsPerUnitX
, int *pixelsPerUnitY
) const;
90 // Set scrollbar visibility: it is possible to show scrollbar only if it is
91 // needed (i.e. if our virtual size is greater than the current size of the
92 // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
93 // which case you should provide some other way to scroll the window as the
94 // user wouldn't be able to do it at all)
95 void ShowScrollbars(wxScrollbarVisibility horz
, wxScrollbarVisibility vert
)
97 DoShowScrollbars(horz
, vert
);
100 // Enable/disable Windows scrolling in either direction. If true, wxWidgets
101 // scrolls the canvas and only a bit of the canvas is invalidated; no
102 // Clear() is necessary. If false, the whole canvas is invalidated and a
103 // Clear() is necessary. Disable for when the scroll increment is used to
104 // actually scroll a non-constant distance
105 virtual void EnableScrolling(bool x_scrolling
, bool y_scrolling
);
107 // Get the view start
108 void GetViewStart(int *x
, int *y
) const { DoGetViewStart(x
, y
); }
110 wxPoint
GetViewStart() const
113 DoGetViewStart(&pt
.x
, &pt
.y
);
117 // Set the scale factor, used in PrepareDC
118 void SetScale(double xs
, double ys
) { m_scaleX
= xs
; m_scaleY
= ys
; }
119 double GetScaleX() const { return m_scaleX
; }
120 double GetScaleY() const { return m_scaleY
; }
122 // translate between scrolled and unscrolled coordinates
123 void CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
124 { DoCalcScrolledPosition(x
, y
, xx
, yy
); }
125 wxPoint
CalcScrolledPosition(const wxPoint
& pt
) const
128 DoCalcScrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
132 void CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
133 { DoCalcUnscrolledPosition(x
, y
, xx
, yy
); }
134 wxPoint
CalcUnscrolledPosition(const wxPoint
& pt
) const
137 DoCalcUnscrolledPosition(pt
.x
, pt
.y
, &p2
.x
, &p2
.y
);
141 void DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
142 void DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const;
144 // Adjust the scrollbars
145 virtual void AdjustScrollbars() = 0;
147 // Calculate scroll increment
148 int CalcScrollInc(wxScrollWinEvent
& event
);
150 // Normally the wxScrolledWindow will scroll itself, but in some rare
151 // occasions you might want it to scroll [part of] another window (e.g. a
152 // child of it in order to scroll only a portion the area between the
153 // scrollbars (spreadsheet: only cell area will move).
154 void SetTargetWindow(wxWindow
*target
);
155 wxWindow
*GetTargetWindow() const;
157 void SetTargetRect(const wxRect
& rect
) { m_rectToScroll
= rect
; }
158 wxRect
GetTargetRect() const { return m_rectToScroll
; }
160 // Override this function to draw the graphic (or just process EVT_PAINT)
161 virtual void OnDraw(wxDC
& WXUNUSED(dc
)) { }
163 // change the DC origin according to the scroll position.
164 virtual void DoPrepareDC(wxDC
& dc
);
166 // are we generating the autoscroll events?
167 bool IsAutoScrolling() const { return m_timerAutoScroll
!= NULL
; }
169 // stop generating the scroll events when mouse is held outside the window
170 void StopAutoScrolling();
172 // this method can be overridden in a derived class to forbid sending the
173 // auto scroll events - note that unlike StopAutoScrolling() it doesn't
174 // stop the timer, so it will be called repeatedly and will typically
175 // return different values depending on the current mouse position
177 // the base class version just returns true
178 virtual bool SendAutoScrollEvents(wxScrollWinEvent
& event
) const;
180 // the methods to be called from the window event handlers
181 void HandleOnScroll(wxScrollWinEvent
& event
);
182 void HandleOnSize(wxSizeEvent
& event
);
183 void HandleOnPaint(wxPaintEvent
& event
);
184 void HandleOnChar(wxKeyEvent
& event
);
185 void HandleOnMouseEnter(wxMouseEvent
& event
);
186 void HandleOnMouseLeave(wxMouseEvent
& event
);
188 void HandleOnMouseWheel(wxMouseEvent
& event
);
189 #endif // wxUSE_MOUSEWHEEL
190 void HandleOnChildFocus(wxChildFocusEvent
& event
);
192 // FIXME: this is needed for now for wxPlot compilation, should be removed
194 void OnScroll(wxScrollWinEvent
& event
) { HandleOnScroll(event
); }
197 // get pointer to our scroll rect if we use it or NULL
198 const wxRect
*GetScrollRect() const
200 return m_rectToScroll
.width
!= 0 ? &m_rectToScroll
: NULL
;
203 // get the size of the target window
204 wxSize
GetTargetSize() const
206 return m_rectToScroll
.width
!= 0 ? m_rectToScroll
.GetSize()
207 : m_targetWindow
->GetClientSize();
210 void GetTargetSize(int *w
, int *h
) const
212 wxSize size
= GetTargetSize();
219 // implementation of public methods with the same name
220 virtual void DoGetViewStart(int *x
, int *y
) const;
221 virtual void DoScroll(int x
, int y
) = 0;
222 virtual void DoShowScrollbars(wxScrollbarVisibility horz
,
223 wxScrollbarVisibility vert
) = 0;
225 // implementations of various wxWindow virtual methods which should be
226 // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
228 void ScrollDoSetVirtualSize(int x
, int y
);
229 wxSize
ScrollGetBestVirtualSize() const;
231 // change just the target window (unlike SetWindow which changes m_win as
233 void DoSetTargetWindow(wxWindow
*target
);
235 // delete the event handler we installed
236 void DeleteEvtHandler();
238 // calls wxScrollHelperEvtHandler::ResetDrawnFlag(), see explanation
239 // in wxScrollHelperEvtHandler::ProcessEvent()
240 void ResetDrawnFlag();
242 // this function should be overridden to return the size available for
243 // m_targetWindow inside m_win of the given size
245 // the default implementation is only good for m_targetWindow == m_win
246 // case, if we're scrolling a subwindow you must override this method
247 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
)
249 // returning just size from here is wrong but it was decided that it is
250 // not wrong enough to break the existing code (which doesn't override
251 // this recently added function at all) by adding this assert
253 // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
265 wxRect m_rectToScroll
;
267 wxTimer
*m_timerAutoScroll
;
269 int m_xScrollPixelsPerLine
;
270 int m_yScrollPixelsPerLine
;
271 int m_xScrollPosition
;
272 int m_yScrollPosition
;
275 int m_xScrollLinesPerPage
;
276 int m_yScrollLinesPerPage
;
278 bool m_xScrollingEnabled
;
279 bool m_yScrollingEnabled
;
283 #endif // wxUSE_MOUSEWHEEL
285 wxScrollHelperEvtHandler
*m_handler
;
287 DECLARE_NO_COPY_CLASS(wxScrollHelperBase
)
290 // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
291 // methods to corresponding wxScrollHelper methods
292 #define WX_FORWARD_TO_SCROLL_HELPER() \
294 virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
295 virtual bool Layout() { return ScrollLayout(); } \
296 virtual void DoSetVirtualSize(int x, int y) \
297 { ScrollDoSetVirtualSize(x, y); } \
298 virtual wxSize GetBestVirtualSize() const \
299 { return ScrollGetBestVirtualSize(); }
301 // include the declaration of the real wxScrollHelper
302 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
303 #include "wx/gtk/scrolwin.h"
304 #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
305 #include "wx/gtk1/scrolwin.h"
307 #define wxHAS_GENERIC_SCROLLWIN
308 #include "wx/generic/scrolwin.h"
311 // ----------------------------------------------------------------------------
312 // wxScrolled<T>: a wxWindow which knows how to scroll
313 // ----------------------------------------------------------------------------
315 // helper class for wxScrolled<T> below
316 struct WXDLLIMPEXP_CORE wxScrolledT_Helper
318 static wxSize
FilterBestSize(const wxWindow
*win
,
319 const wxScrollHelper
*helper
,
320 const wxSize
& origBest
);
322 static WXLRESULT
FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT origResult
);
326 // Scrollable window base on window type T. This used to be wxScrolledWindow,
327 // but wxScrolledWindow includes wxControlContainer functionality and that's
328 // not always desirable.
330 class WXDLLIMPEXP_CORE wxScrolled
: public T
,
331 public wxScrollHelper
,
332 private wxScrolledT_Helper
335 wxScrolled() : wxScrollHelper(this) { }
336 wxScrolled(wxWindow
*parent
,
337 wxWindowID winid
= wxID_ANY
,
338 const wxPoint
& pos
= wxDefaultPosition
,
339 const wxSize
& size
= wxDefaultSize
,
340 long style
= wxScrolledWindowStyle
,
341 const wxString
& name
= wxPanelNameStr
)
342 : wxScrollHelper(this)
344 Create(parent
, winid
, pos
, size
, style
, name
);
347 bool Create(wxWindow
*parent
,
349 const wxPoint
& pos
= wxDefaultPosition
,
350 const wxSize
& size
= wxDefaultSize
,
351 long style
= wxScrolledWindowStyle
,
352 const wxString
& name
= wxPanelNameStr
)
354 m_targetWindow
= this;
357 this->MacSetClipChildren(true);
360 this->Connect(wxEVT_PAINT
, wxPaintEventHandler(wxScrolled::OnPaint
));
362 // by default, we're scrollable in both directions (but if one of the
363 // styles is specified explicitly, we shouldn't add the other one
365 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
366 style
|= wxHSCROLL
| wxVSCROLL
;
368 return T::Create(parent
, winid
, pos
, size
, style
, name
);
371 // we need to return a special WM_GETDLGCODE value to process just the
372 // arrows but let the other navigation characters through
374 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
376 return FilterMSWWindowProc(nMsg
, T::MSWWindowProc(nMsg
, wParam
, lParam
));
380 WX_FORWARD_TO_SCROLL_HELPER()
383 virtual wxSize
DoGetBestSize() const
385 return FilterBestSize(this, this, T::DoGetBestSize());
389 // this is needed for wxEVT_PAINT processing hack described in
390 // wxScrollHelperEvtHandler::ProcessEvent()
391 void OnPaint(wxPaintEvent
& event
)
393 // the user code didn't really draw the window if we got here, so set
394 // this flag to try to call OnDraw() later
399 // VC++ 6 gives warning for the declaration of template member function
400 // without definition
401 #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
402 DECLARE_NO_COPY_CLASS(wxScrolled
)
406 // VC++ <= 6 requires this; it's unlikely any other specializations would
407 // be needed by user code _and_ they were using VC6, so we list only wxWindow
408 // (typical use) and wxPanel (wxScrolledWindow use) specializations here
409 WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled
<wxPanel
> )
410 WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled
<wxWindow
> )
412 // for compatibility with existing code, we provide wxScrolledWindow
413 // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
414 // want wxScrolledWindow to show in wxRTTI information (the class is widely
415 // used and likelihood of its wxRTTI information being used too is high):
416 class WXDLLIMPEXP_CORE wxScrolledWindow
: public wxScrolled
<wxPanel
>
419 wxScrolledWindow() : wxScrolled
<wxPanel
>() {}
420 wxScrolledWindow(wxWindow
*parent
,
421 wxWindowID winid
= wxID_ANY
,
422 const wxPoint
& pos
= wxDefaultPosition
,
423 const wxSize
& size
= wxDefaultSize
,
424 long style
= wxScrolledWindowStyle
,
425 const wxString
& name
= wxPanelNameStr
)
426 : wxScrolled
<wxPanel
>(parent
, winid
, pos
, size
, style
, name
) {}
428 DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow
)
431 typedef wxScrolled
<wxWindow
> wxScrolledCanvas
;
433 #endif // _WX_SCROLWIN_H_BASE_