scrolwin.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/scrolwin.h
  3. // Purpose: wxScrolledWindow, wxScrolledControl and wxScrollHelper
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 30.08.00
  7. // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SCROLWIN_H_BASE_
  11. #define _WX_SCROLWIN_H_BASE_
  12. #include "wx/panel.h"
  13. class WXDLLIMPEXP_FWD_CORE wxScrollHelperEvtHandler;
  14. class WXDLLIMPEXP_FWD_BASE wxTimer;
  15. // default scrolled window style: scroll in both directions
  16. #define wxScrolledWindowStyle (wxHSCROLL | wxVSCROLL)
  17. // values for the second argument of wxScrollHelper::ShowScrollbars()
  18. enum wxScrollbarVisibility
  19. {
  20. wxSHOW_SB_NEVER = -1, // never show the scrollbar at all
  21. wxSHOW_SB_DEFAULT, // show scrollbar only if it is needed
  22. wxSHOW_SB_ALWAYS // always show scrollbar, even if not needed
  23. };
  24. // ----------------------------------------------------------------------------
  25. // The hierarchy of scrolling classes is a bit complicated because we want to
  26. // put as much functionality as possible in a mix-in class not deriving from
  27. // wxWindow so that other classes could derive from the same base class on all
  28. // platforms irrespectively of whether they are native controls (and hence
  29. // don't use our scrolling) or not.
  30. //
  31. // So we have
  32. //
  33. // wxAnyScrollHelperBase
  34. // |
  35. // |
  36. // \|/
  37. // wxScrollHelperBase
  38. // |
  39. // |
  40. // \|/
  41. // wxWindow wxScrollHelper
  42. // | \ / /
  43. // | \ / /
  44. // | _| |_ /
  45. // | wxScrolledWindow /
  46. // | /
  47. // \|/ /
  48. // wxControl /
  49. // \ /
  50. // \ /
  51. // _| |_
  52. // wxScrolledControl
  53. //
  54. // ----------------------------------------------------------------------------
  55. // This class allows reusing some of wxScrollHelperBase functionality in
  56. // wxVarScrollHelperBase in wx/vscroll.h without duplicating its code.
  57. class WXDLLIMPEXP_CORE wxAnyScrollHelperBase
  58. {
  59. public:
  60. wxEXPLICIT wxAnyScrollHelperBase(wxWindow* win);
  61. virtual ~wxAnyScrollHelperBase() {}
  62. // Disable use of keyboard keys for scrolling. By default cursor movement
  63. // keys (including Home, End, Page Up and Down) are used to scroll the
  64. // window appropriately. If the derived class uses these keys for something
  65. // else, e.g. changing the currently selected item, this function can be
  66. // used to disable this behaviour as it's not only not necessary then but
  67. // can actually be actively harmful if another object forwards a keyboard
  68. // event corresponding to one of the above keys to us using
  69. // ProcessWindowEvent() because the event will always be processed which
  70. // can be undesirable.
  71. void DisableKeyboardScrolling() { m_kbdScrollingEnabled = false; }
  72. // Override this function to draw the graphic (or just process EVT_PAINT)
  73. virtual void OnDraw(wxDC& WXUNUSED(dc)) { }
  74. // change the DC origin according to the scroll position.
  75. virtual void DoPrepareDC(wxDC& dc) = 0;
  76. // Simple accessor for the window that is really being scrolled.
  77. wxWindow *GetTargetWindow() const { return m_targetWindow; }
  78. // The methods called from the window event handlers.
  79. void HandleOnChar(wxKeyEvent& event);
  80. void HandleOnPaint(wxPaintEvent& event);
  81. protected:
  82. // the window that receives the scroll events and the window to actually
  83. // scroll, respectively
  84. wxWindow *m_win,
  85. *m_targetWindow;
  86. // whether cursor keys should scroll the window
  87. bool m_kbdScrollingEnabled;
  88. };
  89. // This is the class containing the guts of (uniform) scrolling logic.
  90. class WXDLLIMPEXP_CORE wxScrollHelperBase : public wxAnyScrollHelperBase
  91. {
  92. public:
  93. // ctor must be given the associated window
  94. wxScrollHelperBase(wxWindow *winToScroll);
  95. virtual ~wxScrollHelperBase();
  96. // configure the scrolling
  97. virtual void SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
  98. int noUnitsX, int noUnitsY,
  99. int xPos = 0, int yPos = 0,
  100. bool noRefresh = false );
  101. // scroll to the given (in logical coords) position
  102. //
  103. // notice that for backwards compatibility reasons Scroll() is virtual as
  104. // the existing code could override it but new code should override
  105. // DoScroll() instead
  106. virtual void Scroll(int x, int y) { DoScroll(x, y); }
  107. virtual void Scroll(const wxPoint& pt) { DoScroll(pt.x, pt.y); }
  108. // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL)
  109. int GetScrollPageSize(int orient) const;
  110. void SetScrollPageSize(int orient, int pageSize);
  111. // get the number of lines the window can scroll,
  112. // returns 0 if no scrollbars are there.
  113. int GetScrollLines( int orient ) const;
  114. // Set the x, y scrolling increments.
  115. void SetScrollRate( int xstep, int ystep );
  116. // get the size of one logical unit in physical ones
  117. void GetScrollPixelsPerUnit(int *pixelsPerUnitX, int *pixelsPerUnitY) const;
  118. // Set scrollbar visibility: it is possible to show scrollbar only if it is
  119. // needed (i.e. if our virtual size is greater than the current size of the
  120. // associated window), always (as wxALWAYS_SHOW_SB style does) or never (in
  121. // which case you should provide some other way to scroll the window as the
  122. // user wouldn't be able to do it at all)
  123. void ShowScrollbars(wxScrollbarVisibility horz, wxScrollbarVisibility vert)
  124. {
  125. DoShowScrollbars(horz, vert);
  126. }
  127. // Test whether the specified scrollbar is shown.
  128. virtual bool IsScrollbarShown(int orient) const = 0;
  129. // Enable/disable Windows scrolling in either direction. If true, wxWidgets
  130. // scrolls the canvas and only a bit of the canvas is invalidated; no
  131. // Clear() is necessary. If false, the whole canvas is invalidated and a
  132. // Clear() is necessary. Disable for when the scroll increment is used to
  133. // actually scroll a non-constant distance
  134. //
  135. // Notice that calling this method with a false argument doesn't disable
  136. // scrolling the window in this direction, it just changes the mechanism by
  137. // which it is implemented to not use wxWindow::ScrollWindow().
  138. virtual void EnableScrolling(bool x_scrolling, bool y_scrolling);
  139. // Get the view start
  140. void GetViewStart(int *x, int *y) const { DoGetViewStart(x, y); }
  141. wxPoint GetViewStart() const
  142. {
  143. wxPoint pt;
  144. DoGetViewStart(&pt.x, &pt.y);
  145. return pt;
  146. }
  147. // Set the scale factor, used in PrepareDC
  148. void SetScale(double xs, double ys) { m_scaleX = xs; m_scaleY = ys; }
  149. double GetScaleX() const { return m_scaleX; }
  150. double GetScaleY() const { return m_scaleY; }
  151. // translate between scrolled and unscrolled coordinates
  152. void CalcScrolledPosition(int x, int y, int *xx, int *yy) const
  153. { DoCalcScrolledPosition(x, y, xx, yy); }
  154. wxPoint CalcScrolledPosition(const wxPoint& pt) const
  155. {
  156. wxPoint p2;
  157. DoCalcScrolledPosition(pt.x, pt.y, &p2.x, &p2.y);
  158. return p2;
  159. }
  160. void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
  161. { DoCalcUnscrolledPosition(x, y, xx, yy); }
  162. wxPoint CalcUnscrolledPosition(const wxPoint& pt) const
  163. {
  164. wxPoint p2;
  165. DoCalcUnscrolledPosition(pt.x, pt.y, &p2.x, &p2.y);
  166. return p2;
  167. }
  168. void DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const;
  169. void DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const;
  170. // Adjust the scrollbars
  171. virtual void AdjustScrollbars() = 0;
  172. // Calculate scroll increment
  173. int CalcScrollInc(wxScrollWinEvent& event);
  174. // Normally the wxScrolledWindow will scroll itself, but in some rare
  175. // occasions you might want it to scroll [part of] another window (e.g. a
  176. // child of it in order to scroll only a portion the area between the
  177. // scrollbars (spreadsheet: only cell area will move).
  178. void SetTargetWindow(wxWindow *target);
  179. void SetTargetRect(const wxRect& rect) { m_rectToScroll = rect; }
  180. wxRect GetTargetRect() const { return m_rectToScroll; }
  181. virtual void DoPrepareDC(wxDC& dc);
  182. // are we generating the autoscroll events?
  183. bool IsAutoScrolling() const { return m_timerAutoScroll != NULL; }
  184. // stop generating the scroll events when mouse is held outside the window
  185. void StopAutoScrolling();
  186. // this method can be overridden in a derived class to forbid sending the
  187. // auto scroll events - note that unlike StopAutoScrolling() it doesn't
  188. // stop the timer, so it will be called repeatedly and will typically
  189. // return different values depending on the current mouse position
  190. //
  191. // the base class version just returns true
  192. virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const;
  193. // the methods to be called from the window event handlers
  194. void HandleOnScroll(wxScrollWinEvent& event);
  195. void HandleOnSize(wxSizeEvent& event);
  196. void HandleOnMouseEnter(wxMouseEvent& event);
  197. void HandleOnMouseLeave(wxMouseEvent& event);
  198. #if wxUSE_MOUSEWHEEL
  199. void HandleOnMouseWheel(wxMouseEvent& event);
  200. #endif // wxUSE_MOUSEWHEEL
  201. void HandleOnChildFocus(wxChildFocusEvent& event);
  202. #if WXWIN_COMPATIBILITY_2_8
  203. wxDEPRECATED(
  204. void OnScroll(wxScrollWinEvent& event) { HandleOnScroll(event); }
  205. )
  206. #endif // WXWIN_COMPATIBILITY_2_8
  207. protected:
  208. // get pointer to our scroll rect if we use it or NULL
  209. const wxRect *GetScrollRect() const
  210. {
  211. return m_rectToScroll.width != 0 ? &m_rectToScroll : NULL;
  212. }
  213. // get the size of the target window
  214. wxSize GetTargetSize() const
  215. {
  216. return m_rectToScroll.width != 0 ? m_rectToScroll.GetSize()
  217. : m_targetWindow->GetClientSize();
  218. }
  219. void GetTargetSize(int *w, int *h) const
  220. {
  221. wxSize size = GetTargetSize();
  222. if ( w )
  223. *w = size.x;
  224. if ( h )
  225. *h = size.y;
  226. }
  227. // implementation of public methods with the same name
  228. virtual void DoGetViewStart(int *x, int *y) const;
  229. virtual void DoScroll(int x, int y) = 0;
  230. virtual void DoShowScrollbars(wxScrollbarVisibility horz,
  231. wxScrollbarVisibility vert) = 0;
  232. // implementations of various wxWindow virtual methods which should be
  233. // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER())
  234. bool ScrollLayout();
  235. void ScrollDoSetVirtualSize(int x, int y);
  236. wxSize ScrollGetBestVirtualSize() const;
  237. // change just the target window (unlike SetWindow which changes m_win as
  238. // well)
  239. void DoSetTargetWindow(wxWindow *target);
  240. // delete the event handler we installed
  241. void DeleteEvtHandler();
  242. // this function should be overridden to return the size available for
  243. // m_targetWindow inside m_win of the given size
  244. //
  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)
  248. {
  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
  252. //
  253. // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" );
  254. return size;
  255. }
  256. double m_scaleX;
  257. double m_scaleY;
  258. wxRect m_rectToScroll;
  259. wxTimer *m_timerAutoScroll;
  260. // The number of pixels to scroll in horizontal and vertical directions
  261. // respectively.
  262. //
  263. // If 0, means that the scrolling in the given direction is disabled.
  264. int m_xScrollPixelsPerLine;
  265. int m_yScrollPixelsPerLine;
  266. int m_xScrollPosition;
  267. int m_yScrollPosition;
  268. int m_xScrollLines;
  269. int m_yScrollLines;
  270. int m_xScrollLinesPerPage;
  271. int m_yScrollLinesPerPage;
  272. bool m_xScrollingEnabled;
  273. bool m_yScrollingEnabled;
  274. #if wxUSE_MOUSEWHEEL
  275. int m_wheelRotation;
  276. #endif // wxUSE_MOUSEWHEEL
  277. wxScrollHelperEvtHandler *m_handler;
  278. wxDECLARE_NO_COPY_CLASS(wxScrollHelperBase);
  279. };
  280. // this macro can be used in a wxScrollHelper-derived class to forward wxWindow
  281. // methods to corresponding wxScrollHelper methods
  282. #define WX_FORWARD_TO_SCROLL_HELPER() \
  283. public: \
  284. virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \
  285. virtual bool Layout() { return ScrollLayout(); } \
  286. virtual bool CanScroll(int orient) const \
  287. { return IsScrollbarShown(orient); } \
  288. virtual void DoSetVirtualSize(int x, int y) \
  289. { ScrollDoSetVirtualSize(x, y); } \
  290. virtual wxSize GetBestVirtualSize() const \
  291. { return ScrollGetBestVirtualSize(); }
  292. // include the declaration of the real wxScrollHelper
  293. #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
  294. #include "wx/gtk/scrolwin.h"
  295. #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
  296. #include "wx/gtk1/scrolwin.h"
  297. #else
  298. #define wxHAS_GENERIC_SCROLLWIN
  299. #include "wx/generic/scrolwin.h"
  300. #endif
  301. // ----------------------------------------------------------------------------
  302. // wxScrolled<T>: a wxWindow which knows how to scroll
  303. // ----------------------------------------------------------------------------
  304. // helper class for wxScrolled<T> below
  305. struct WXDLLIMPEXP_CORE wxScrolledT_Helper
  306. {
  307. static wxSize FilterBestSize(const wxWindow *win,
  308. const wxScrollHelper *helper,
  309. const wxSize& origBest);
  310. #ifdef __WXMSW__
  311. static WXLRESULT FilterMSWWindowProc(WXUINT nMsg, WXLRESULT origResult);
  312. #endif
  313. };
  314. // Scrollable window base on window type T. This used to be wxScrolledWindow,
  315. // but wxScrolledWindow includes wxControlContainer functionality and that's
  316. // not always desirable.
  317. template<class T>
  318. class wxScrolled : public T,
  319. public wxScrollHelper,
  320. private wxScrolledT_Helper
  321. {
  322. public:
  323. wxScrolled() : wxScrollHelper(this) { }
  324. wxScrolled(wxWindow *parent,
  325. wxWindowID winid = wxID_ANY,
  326. const wxPoint& pos = wxDefaultPosition,
  327. const wxSize& size = wxDefaultSize,
  328. long style = wxScrolledWindowStyle,
  329. const wxString& name = wxPanelNameStr)
  330. : wxScrollHelper(this)
  331. {
  332. Create(parent, winid, pos, size, style, name);
  333. }
  334. bool Create(wxWindow *parent,
  335. wxWindowID winid,
  336. const wxPoint& pos = wxDefaultPosition,
  337. const wxSize& size = wxDefaultSize,
  338. long style = wxScrolledWindowStyle,
  339. const wxString& name = wxPanelNameStr)
  340. {
  341. m_targetWindow = this;
  342. #ifdef __WXMAC__
  343. this->MacSetClipChildren(true);
  344. #endif
  345. // by default, we're scrollable in both directions (but if one of the
  346. // styles is specified explicitly, we shouldn't add the other one
  347. // automatically)
  348. if ( !(style & (wxHSCROLL | wxVSCROLL)) )
  349. style |= wxHSCROLL | wxVSCROLL;
  350. #ifdef __WXOSX__
  351. bool retval = T::Create(parent, winid, pos, size, style, name);
  352. if ( retval && (style & wxALWAYS_SHOW_SB) )
  353. ShowScrollbars(wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS);
  354. return retval;
  355. #else
  356. if ( style & wxALWAYS_SHOW_SB )
  357. ShowScrollbars(wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS);
  358. return T::Create(parent, winid, pos, size, style, name);
  359. #endif
  360. }
  361. #ifdef __WXMSW__
  362. // we need to return a special WM_GETDLGCODE value to process just the
  363. // arrows but let the other navigation characters through
  364. virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
  365. {
  366. return FilterMSWWindowProc(nMsg, T::MSWWindowProc(nMsg, wParam, lParam));
  367. }
  368. // Take into account the scroll origin.
  369. virtual void MSWAdjustBrushOrg(int* xOrg, int* yOrg) const
  370. {
  371. CalcUnscrolledPosition(*xOrg, *yOrg, xOrg, yOrg);
  372. }
  373. #endif // __WXMSW__
  374. WX_FORWARD_TO_SCROLL_HELPER()
  375. protected:
  376. virtual wxSize DoGetBestSize() const
  377. {
  378. return FilterBestSize(this, this, T::DoGetBestSize());
  379. }
  380. private:
  381. // VC++ 6 gives warning for the declaration of template member function
  382. // without definition
  383. #ifndef __VISUALC6__
  384. wxDECLARE_NO_COPY_CLASS(wxScrolled);
  385. #endif
  386. };
  387. #ifdef __VISUALC6__
  388. // disable the warning about non dll-interface class used as base for
  389. // dll-interface class: it's harmless in this case
  390. #pragma warning(push)
  391. #pragma warning(disable:4275)
  392. #endif
  393. // for compatibility with existing code, we provide wxScrolledWindow
  394. // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we
  395. // want wxScrolledWindow to show in wxRTTI information (the class is widely
  396. // used and likelihood of its wxRTTI information being used too is high):
  397. class WXDLLIMPEXP_CORE wxScrolledWindow : public wxScrolled<wxPanel>
  398. {
  399. public:
  400. wxScrolledWindow() : wxScrolled<wxPanel>() {}
  401. wxScrolledWindow(wxWindow *parent,
  402. wxWindowID winid = wxID_ANY,
  403. const wxPoint& pos = wxDefaultPosition,
  404. const wxSize& size = wxDefaultSize,
  405. long style = wxScrolledWindowStyle,
  406. const wxString& name = wxPanelNameStr)
  407. : wxScrolled<wxPanel>(parent, winid, pos, size, style, name) {}
  408. DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow)
  409. };
  410. typedef wxScrolled<wxWindow> wxScrolledCanvas;
  411. #ifdef __VISUALC6__
  412. #pragma warning(pop)
  413. #endif
  414. #endif // _WX_SCROLWIN_H_BASE_