| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: include/wx/scrolwin.h |
| 3 | // Purpose: wxScrolledWindow, wxScrolledControl and wxScrollHelper |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 30.08.00 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_SCROLWIN_H_BASE_ |
| 13 | #define _WX_SCROLWIN_H_BASE_ |
| 14 | |
| 15 | #include "wx/panel.h" |
| 16 | |
| 17 | class WXDLLIMPEXP_FWD_CORE wxScrollHelperEvtHandler; |
| 18 | class WXDLLIMPEXP_FWD_BASE wxTimer; |
| 19 | |
| 20 | // default scrolled window style: scroll in both directions |
| 21 | #define wxScrolledWindowStyle (wxHSCROLL | wxVSCROLL) |
| 22 | |
| 23 | // values for the second argument of wxScrollHelper::ShowScrollbars() |
| 24 | enum wxScrollbarVisibility |
| 25 | { |
| 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 |
| 29 | }; |
| 30 | |
| 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. |
| 37 | // |
| 38 | // So we have |
| 39 | // |
| 40 | // wxScrollHelperBase |
| 41 | // | |
| 42 | // | |
| 43 | // \|/ |
| 44 | // wxWindow wxScrollHelper |
| 45 | // | \ / / |
| 46 | // | \ / / |
| 47 | // | _| |_ / |
| 48 | // | wxScrolledWindow / |
| 49 | // | / |
| 50 | // \|/ / |
| 51 | // wxControl / |
| 52 | // \ / |
| 53 | // \ / |
| 54 | // _| |_ |
| 55 | // wxScrolledControl |
| 56 | // |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | class WXDLLIMPEXP_CORE wxScrollHelperBase |
| 60 | { |
| 61 | public: |
| 62 | // ctor must be given the associated window |
| 63 | wxScrollHelperBase(wxWindow *winToScroll); |
| 64 | virtual ~wxScrollHelperBase(); |
| 65 | |
| 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 ); |
| 71 | |
| 72 | // scroll to the given (in logical coords) position |
| 73 | // |
| 74 | // notice that for backwards compatibility reasons Scroll() is virtual as |
| 75 | // the existing code could override it but new code should override |
| 76 | // DoScroll() instead |
| 77 | virtual void Scroll(int x, int y) { DoScroll(x, y); } |
| 78 | virtual void Scroll(const wxPoint& pt) { DoScroll(pt.x, pt.y); } |
| 79 | |
| 80 | // get/set the page size for this orientation (wxVERTICAL/wxHORIZONTAL) |
| 81 | int GetScrollPageSize(int orient) const; |
| 82 | void SetScrollPageSize(int orient, int pageSize); |
| 83 | |
| 84 | // get the number of lines the window can scroll, |
| 85 | // returns 0 if no scrollbars are there. |
| 86 | int GetScrollLines( int orient ) const; |
| 87 | |
| 88 | // Set the x, y scrolling increments. |
| 89 | void SetScrollRate( int xstep, int ystep ); |
| 90 | |
| 91 | // get the size of one logical unit in physical ones |
| 92 | void GetScrollPixelsPerUnit(int *pixelsPerUnitX, int *pixelsPerUnitY) const; |
| 93 | |
| 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) |
| 100 | { |
| 101 | DoShowScrollbars(horz, vert); |
| 102 | } |
| 103 | |
| 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); |
| 110 | |
| 111 | // Get the view start |
| 112 | void GetViewStart(int *x, int *y) const { DoGetViewStart(x, y); } |
| 113 | |
| 114 | wxPoint GetViewStart() const |
| 115 | { |
| 116 | wxPoint pt; |
| 117 | DoGetViewStart(&pt.x, &pt.y); |
| 118 | return pt; |
| 119 | } |
| 120 | |
| 121 | // Set the scale factor, used in PrepareDC |
| 122 | void SetScale(double xs, double ys) { m_scaleX = xs; m_scaleY = ys; } |
| 123 | double GetScaleX() const { return m_scaleX; } |
| 124 | double GetScaleY() const { return m_scaleY; } |
| 125 | |
| 126 | // translate between scrolled and unscrolled coordinates |
| 127 | void CalcScrolledPosition(int x, int y, int *xx, int *yy) const |
| 128 | { DoCalcScrolledPosition(x, y, xx, yy); } |
| 129 | wxPoint CalcScrolledPosition(const wxPoint& pt) const |
| 130 | { |
| 131 | wxPoint p2; |
| 132 | DoCalcScrolledPosition(pt.x, pt.y, &p2.x, &p2.y); |
| 133 | return p2; |
| 134 | } |
| 135 | |
| 136 | void CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const |
| 137 | { DoCalcUnscrolledPosition(x, y, xx, yy); } |
| 138 | wxPoint CalcUnscrolledPosition(const wxPoint& pt) const |
| 139 | { |
| 140 | wxPoint p2; |
| 141 | DoCalcUnscrolledPosition(pt.x, pt.y, &p2.x, &p2.y); |
| 142 | return p2; |
| 143 | } |
| 144 | |
| 145 | void DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const; |
| 146 | void DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const; |
| 147 | |
| 148 | // Adjust the scrollbars |
| 149 | virtual void AdjustScrollbars() = 0; |
| 150 | |
| 151 | // Calculate scroll increment |
| 152 | int CalcScrollInc(wxScrollWinEvent& event); |
| 153 | |
| 154 | // Normally the wxScrolledWindow will scroll itself, but in some rare |
| 155 | // occasions you might want it to scroll [part of] another window (e.g. a |
| 156 | // child of it in order to scroll only a portion the area between the |
| 157 | // scrollbars (spreadsheet: only cell area will move). |
| 158 | void SetTargetWindow(wxWindow *target); |
| 159 | wxWindow *GetTargetWindow() const; |
| 160 | |
| 161 | void SetTargetRect(const wxRect& rect) { m_rectToScroll = rect; } |
| 162 | wxRect GetTargetRect() const { return m_rectToScroll; } |
| 163 | |
| 164 | // Override this function to draw the graphic (or just process EVT_PAINT) |
| 165 | virtual void OnDraw(wxDC& WXUNUSED(dc)) { } |
| 166 | |
| 167 | // change the DC origin according to the scroll position. |
| 168 | virtual void DoPrepareDC(wxDC& dc); |
| 169 | |
| 170 | // are we generating the autoscroll events? |
| 171 | bool IsAutoScrolling() const { return m_timerAutoScroll != NULL; } |
| 172 | |
| 173 | // stop generating the scroll events when mouse is held outside the window |
| 174 | void StopAutoScrolling(); |
| 175 | |
| 176 | // this method can be overridden in a derived class to forbid sending the |
| 177 | // auto scroll events - note that unlike StopAutoScrolling() it doesn't |
| 178 | // stop the timer, so it will be called repeatedly and will typically |
| 179 | // return different values depending on the current mouse position |
| 180 | // |
| 181 | // the base class version just returns true |
| 182 | virtual bool SendAutoScrollEvents(wxScrollWinEvent& event) const; |
| 183 | |
| 184 | // the methods to be called from the window event handlers |
| 185 | void HandleOnScroll(wxScrollWinEvent& event); |
| 186 | void HandleOnSize(wxSizeEvent& event); |
| 187 | void HandleOnPaint(wxPaintEvent& event); |
| 188 | void HandleOnChar(wxKeyEvent& event); |
| 189 | void HandleOnMouseEnter(wxMouseEvent& event); |
| 190 | void HandleOnMouseLeave(wxMouseEvent& event); |
| 191 | #if wxUSE_MOUSEWHEEL |
| 192 | void HandleOnMouseWheel(wxMouseEvent& event); |
| 193 | #endif // wxUSE_MOUSEWHEEL |
| 194 | void HandleOnChildFocus(wxChildFocusEvent& event); |
| 195 | |
| 196 | #if WXWIN_COMPATIBILITY_2_8 |
| 197 | wxDEPRECATED( |
| 198 | void OnScroll(wxScrollWinEvent& event) { HandleOnScroll(event); } |
| 199 | ) |
| 200 | #endif // WXWIN_COMPATIBILITY_2_8 |
| 201 | |
| 202 | protected: |
| 203 | // get pointer to our scroll rect if we use it or NULL |
| 204 | const wxRect *GetScrollRect() const |
| 205 | { |
| 206 | return m_rectToScroll.width != 0 ? &m_rectToScroll : NULL; |
| 207 | } |
| 208 | |
| 209 | // get the size of the target window |
| 210 | wxSize GetTargetSize() const |
| 211 | { |
| 212 | return m_rectToScroll.width != 0 ? m_rectToScroll.GetSize() |
| 213 | : m_targetWindow->GetClientSize(); |
| 214 | } |
| 215 | |
| 216 | void GetTargetSize(int *w, int *h) const |
| 217 | { |
| 218 | wxSize size = GetTargetSize(); |
| 219 | if ( w ) |
| 220 | *w = size.x; |
| 221 | if ( h ) |
| 222 | *h = size.y; |
| 223 | } |
| 224 | |
| 225 | // implementation of public methods with the same name |
| 226 | virtual void DoGetViewStart(int *x, int *y) const; |
| 227 | virtual void DoScroll(int x, int y) = 0; |
| 228 | virtual void DoShowScrollbars(wxScrollbarVisibility horz, |
| 229 | wxScrollbarVisibility vert) = 0; |
| 230 | |
| 231 | // implementations of various wxWindow virtual methods which should be |
| 232 | // forwarded to us (this can be done by WX_FORWARD_TO_SCROLL_HELPER()) |
| 233 | bool ScrollLayout(); |
| 234 | void ScrollDoSetVirtualSize(int x, int y); |
| 235 | wxSize ScrollGetBestVirtualSize() const; |
| 236 | |
| 237 | // change just the target window (unlike SetWindow which changes m_win as |
| 238 | // well) |
| 239 | void DoSetTargetWindow(wxWindow *target); |
| 240 | |
| 241 | // delete the event handler we installed |
| 242 | void DeleteEvtHandler(); |
| 243 | |
| 244 | // calls wxScrollHelperEvtHandler::ResetDrawnFlag(), see explanation |
| 245 | // in wxScrollHelperEvtHandler::ProcessEvent() |
| 246 | void ResetDrawnFlag(); |
| 247 | |
| 248 | // this function should be overridden to return the size available for |
| 249 | // m_targetWindow inside m_win of the given size |
| 250 | // |
| 251 | // the default implementation is only good for m_targetWindow == m_win |
| 252 | // case, if we're scrolling a subwindow you must override this method |
| 253 | virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size) |
| 254 | { |
| 255 | // returning just size from here is wrong but it was decided that it is |
| 256 | // not wrong enough to break the existing code (which doesn't override |
| 257 | // this recently added function at all) by adding this assert |
| 258 | // |
| 259 | // wxASSERT_MSG( m_targetWindow == m_win, "must be overridden" ); |
| 260 | |
| 261 | return size; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | double m_scaleX; |
| 266 | double m_scaleY; |
| 267 | |
| 268 | wxWindow *m_win, |
| 269 | *m_targetWindow; |
| 270 | |
| 271 | wxRect m_rectToScroll; |
| 272 | |
| 273 | wxTimer *m_timerAutoScroll; |
| 274 | |
| 275 | int m_xScrollPixelsPerLine; |
| 276 | int m_yScrollPixelsPerLine; |
| 277 | int m_xScrollPosition; |
| 278 | int m_yScrollPosition; |
| 279 | int m_xScrollLines; |
| 280 | int m_yScrollLines; |
| 281 | int m_xScrollLinesPerPage; |
| 282 | int m_yScrollLinesPerPage; |
| 283 | |
| 284 | bool m_xScrollingEnabled; |
| 285 | bool m_yScrollingEnabled; |
| 286 | |
| 287 | #if wxUSE_MOUSEWHEEL |
| 288 | int m_wheelRotation; |
| 289 | #endif // wxUSE_MOUSEWHEEL |
| 290 | |
| 291 | wxScrollHelperEvtHandler *m_handler; |
| 292 | |
| 293 | DECLARE_NO_COPY_CLASS(wxScrollHelperBase) |
| 294 | }; |
| 295 | |
| 296 | // this macro can be used in a wxScrollHelper-derived class to forward wxWindow |
| 297 | // methods to corresponding wxScrollHelper methods |
| 298 | #define WX_FORWARD_TO_SCROLL_HELPER() \ |
| 299 | public: \ |
| 300 | virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); } \ |
| 301 | virtual bool Layout() { return ScrollLayout(); } \ |
| 302 | virtual void DoSetVirtualSize(int x, int y) \ |
| 303 | { ScrollDoSetVirtualSize(x, y); } \ |
| 304 | virtual wxSize GetBestVirtualSize() const \ |
| 305 | { return ScrollGetBestVirtualSize(); } |
| 306 | |
| 307 | // include the declaration of the real wxScrollHelper |
| 308 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) |
| 309 | #include "wx/gtk/scrolwin.h" |
| 310 | #elif defined(__WXGTK__) && !defined(__WXUNIVERSAL__) |
| 311 | #include "wx/gtk1/scrolwin.h" |
| 312 | #else |
| 313 | #define wxHAS_GENERIC_SCROLLWIN |
| 314 | #include "wx/generic/scrolwin.h" |
| 315 | #endif |
| 316 | |
| 317 | // ---------------------------------------------------------------------------- |
| 318 | // wxScrolled<T>: a wxWindow which knows how to scroll |
| 319 | // ---------------------------------------------------------------------------- |
| 320 | |
| 321 | // helper class for wxScrolled<T> below |
| 322 | struct WXDLLIMPEXP_CORE wxScrolledT_Helper |
| 323 | { |
| 324 | static wxSize FilterBestSize(const wxWindow *win, |
| 325 | const wxScrollHelper *helper, |
| 326 | const wxSize& origBest); |
| 327 | #ifdef __WXMSW__ |
| 328 | static WXLRESULT FilterMSWWindowProc(WXUINT nMsg, WXLRESULT origResult); |
| 329 | #endif |
| 330 | }; |
| 331 | |
| 332 | // Scrollable window base on window type T. This used to be wxScrolledWindow, |
| 333 | // but wxScrolledWindow includes wxControlContainer functionality and that's |
| 334 | // not always desirable. |
| 335 | template<class T> |
| 336 | class WXDLLIMPEXP_CORE wxScrolled : public T, |
| 337 | public wxScrollHelper, |
| 338 | private wxScrolledT_Helper |
| 339 | { |
| 340 | public: |
| 341 | wxScrolled() : wxScrollHelper(this) { } |
| 342 | wxScrolled(wxWindow *parent, |
| 343 | wxWindowID winid = wxID_ANY, |
| 344 | const wxPoint& pos = wxDefaultPosition, |
| 345 | const wxSize& size = wxDefaultSize, |
| 346 | long style = wxScrolledWindowStyle, |
| 347 | const wxString& name = wxPanelNameStr) |
| 348 | : wxScrollHelper(this) |
| 349 | { |
| 350 | Create(parent, winid, pos, size, style, name); |
| 351 | } |
| 352 | |
| 353 | bool Create(wxWindow *parent, |
| 354 | wxWindowID winid, |
| 355 | const wxPoint& pos = wxDefaultPosition, |
| 356 | const wxSize& size = wxDefaultSize, |
| 357 | long style = wxScrolledWindowStyle, |
| 358 | const wxString& name = wxPanelNameStr) |
| 359 | { |
| 360 | m_targetWindow = this; |
| 361 | |
| 362 | #ifdef __WXMAC__ |
| 363 | this->MacSetClipChildren(true); |
| 364 | #endif |
| 365 | |
| 366 | this->Connect(wxEVT_PAINT, wxPaintEventHandler(wxScrolled::OnPaint)); |
| 367 | |
| 368 | // by default, we're scrollable in both directions (but if one of the |
| 369 | // styles is specified explicitly, we shouldn't add the other one |
| 370 | // automatically) |
| 371 | if ( !(style & (wxHSCROLL | wxVSCROLL)) ) |
| 372 | style |= wxHSCROLL | wxVSCROLL; |
| 373 | |
| 374 | return T::Create(parent, winid, pos, size, style, name); |
| 375 | } |
| 376 | |
| 377 | // we need to return a special WM_GETDLGCODE value to process just the |
| 378 | // arrows but let the other navigation characters through |
| 379 | #ifdef __WXMSW__ |
| 380 | virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
| 381 | { |
| 382 | return FilterMSWWindowProc(nMsg, T::MSWWindowProc(nMsg, wParam, lParam)); |
| 383 | } |
| 384 | #endif // __WXMSW__ |
| 385 | |
| 386 | WX_FORWARD_TO_SCROLL_HELPER() |
| 387 | |
| 388 | protected: |
| 389 | virtual wxSize DoGetBestSize() const |
| 390 | { |
| 391 | return FilterBestSize(this, this, T::DoGetBestSize()); |
| 392 | } |
| 393 | |
| 394 | private: |
| 395 | // this is needed for wxEVT_PAINT processing hack described in |
| 396 | // wxScrollHelperEvtHandler::ProcessEvent() |
| 397 | void OnPaint(wxPaintEvent& event) |
| 398 | { |
| 399 | // the user code didn't really draw the window if we got here, so set |
| 400 | // this flag to try to call OnDraw() later |
| 401 | ResetDrawnFlag(); |
| 402 | event.Skip(); |
| 403 | } |
| 404 | |
| 405 | // VC++ 6 gives warning for the declaration of template member function |
| 406 | // without definition |
| 407 | #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7) |
| 408 | DECLARE_NO_COPY_CLASS(wxScrolled) |
| 409 | #endif |
| 410 | }; |
| 411 | |
| 412 | // VC++ <= 6 requires this; it's unlikely any other specializations would |
| 413 | // be needed by user code _and_ they were using VC6, so we list only wxWindow |
| 414 | // (typical use) and wxPanel (wxScrolledWindow use) specializations here |
| 415 | WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxPanel> ) |
| 416 | WXDLLIMPEXP_TEMPLATE_INSTANCE_CORE( wxScrolled<wxWindow> ) |
| 417 | |
| 418 | // for compatibility with existing code, we provide wxScrolledWindow |
| 419 | // "typedef" for wxScrolled<wxPanel>. It's not a real typedef because we |
| 420 | // want wxScrolledWindow to show in wxRTTI information (the class is widely |
| 421 | // used and likelihood of its wxRTTI information being used too is high): |
| 422 | class WXDLLIMPEXP_CORE wxScrolledWindow : public wxScrolled<wxPanel> |
| 423 | { |
| 424 | public: |
| 425 | wxScrolledWindow() : wxScrolled<wxPanel>() {} |
| 426 | wxScrolledWindow(wxWindow *parent, |
| 427 | wxWindowID winid = wxID_ANY, |
| 428 | const wxPoint& pos = wxDefaultPosition, |
| 429 | const wxSize& size = wxDefaultSize, |
| 430 | long style = wxScrolledWindowStyle, |
| 431 | const wxString& name = wxPanelNameStr) |
| 432 | : wxScrolled<wxPanel>(parent, winid, pos, size, style, name) {} |
| 433 | |
| 434 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxScrolledWindow) |
| 435 | }; |
| 436 | |
| 437 | typedef wxScrolled<wxWindow> wxScrolledCanvas; |
| 438 | |
| 439 | #endif // _WX_SCROLWIN_H_BASE_ |