| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/univ/scrarrow.cpp |
| 3 | // Purpose: wxScrollArrows class implementation |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 22.01.01 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #ifndef WX_PRECOMP |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/univ/scrtimer.h" |
| 30 | #include "wx/univ/scrarrow.h" |
| 31 | |
| 32 | #include "wx/univ/renderer.h" |
| 33 | #include "wx/univ/inphand.h" |
| 34 | #include "wx/univ/theme.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // wxScrollArrowCaptureData: contains the data used while the arrow is being |
| 38 | // pressed by the user |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | struct wxScrollArrowCaptureData |
| 42 | { |
| 43 | wxScrollArrowCaptureData() |
| 44 | { |
| 45 | m_arrowPressed = wxScrollArrows::Arrow_None; |
| 46 | m_window = NULL; |
| 47 | m_btnCapture = -1; |
| 48 | #if wxUSE_TIMER |
| 49 | m_timerScroll = NULL; |
| 50 | #endif // wxUSE_TIMER |
| 51 | } |
| 52 | |
| 53 | ~wxScrollArrowCaptureData() |
| 54 | { |
| 55 | if ( m_window ) |
| 56 | m_window->ReleaseMouse(); |
| 57 | |
| 58 | #if wxUSE_TIMER |
| 59 | delete m_timerScroll; |
| 60 | #endif // wxUSE_TIMER |
| 61 | } |
| 62 | |
| 63 | // the arrow being held pressed (may be Arrow_None) |
| 64 | wxScrollArrows::Arrow m_arrowPressed; |
| 65 | |
| 66 | // the mouse button which started the capture (-1 if none) |
| 67 | int m_btnCapture; |
| 68 | |
| 69 | // the window which has captured the mouse |
| 70 | wxWindow *m_window; |
| 71 | |
| 72 | #if wxUSE_TIMER |
| 73 | // the timer for generating the scroll events |
| 74 | wxScrollTimer *m_timerScroll; |
| 75 | #endif |
| 76 | }; |
| 77 | |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | // wxScrollArrowTimer: a wxScrollTimer which calls OnArrow() |
| 80 | // ---------------------------------------------------------------------------- |
| 81 | |
| 82 | #if wxUSE_TIMER |
| 83 | |
| 84 | class wxScrollArrowTimer : public wxScrollTimer |
| 85 | { |
| 86 | public: |
| 87 | wxScrollArrowTimer(wxControlWithArrows *control, |
| 88 | wxScrollArrows::Arrow arrow) |
| 89 | { |
| 90 | m_control = control; |
| 91 | m_arrow = arrow; |
| 92 | |
| 93 | StartAutoScroll(); |
| 94 | } |
| 95 | |
| 96 | protected: |
| 97 | virtual bool DoNotify() |
| 98 | { |
| 99 | return m_control->OnArrow(m_arrow); |
| 100 | } |
| 101 | |
| 102 | wxControlWithArrows *m_control; |
| 103 | wxScrollArrows::Arrow m_arrow; |
| 104 | }; |
| 105 | |
| 106 | #endif // wxUSE_TIMER |
| 107 | |
| 108 | // ============================================================================ |
| 109 | // implementation of wxScrollArrows |
| 110 | // ============================================================================ |
| 111 | |
| 112 | // ---------------------------------------------------------------------------- |
| 113 | // con/destruction |
| 114 | // ---------------------------------------------------------------------------- |
| 115 | |
| 116 | wxScrollArrows::wxScrollArrows(wxControlWithArrows *control) |
| 117 | { |
| 118 | m_control = control; |
| 119 | m_captureData = NULL; |
| 120 | } |
| 121 | |
| 122 | wxScrollArrows::~wxScrollArrows() |
| 123 | { |
| 124 | // it should have been destroyed |
| 125 | wxASSERT_MSG( !m_captureData, _T("memory leak in wxScrollArrows") ); |
| 126 | } |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | // drawing |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | void wxScrollArrows::DrawArrow(Arrow arrow, |
| 133 | wxDC& dc, |
| 134 | const wxRect& rect, |
| 135 | bool scrollbarLike) const |
| 136 | { |
| 137 | static const wxDirection arrowDirs[2][Arrow_Max] = |
| 138 | { |
| 139 | { wxLEFT, wxRIGHT }, |
| 140 | { wxUP, wxDOWN } |
| 141 | }; |
| 142 | |
| 143 | if ( scrollbarLike ) |
| 144 | m_control->GetRenderer()->DrawScrollbarArrow( |
| 145 | dc, |
| 146 | arrowDirs[m_control->IsVertical()][arrow], |
| 147 | rect, |
| 148 | m_control->GetArrowState(arrow)); |
| 149 | else |
| 150 | m_control->GetRenderer()->DrawArrow( |
| 151 | dc, |
| 152 | arrowDirs[m_control->IsVertical()][arrow], |
| 153 | rect, |
| 154 | m_control->GetArrowState(arrow)); |
| 155 | } |
| 156 | |
| 157 | // ---------------------------------------------------------------------------- |
| 158 | // input handling |
| 159 | // ---------------------------------------------------------------------------- |
| 160 | |
| 161 | void wxScrollArrows::UpdateCurrentFlag(Arrow arrow, Arrow arrowCur) const |
| 162 | { |
| 163 | m_control->SetArrowFlag(arrow, wxCONTROL_CURRENT, arrow == arrowCur); |
| 164 | } |
| 165 | |
| 166 | bool wxScrollArrows::HandleMouseMove(const wxMouseEvent& event) const |
| 167 | { |
| 168 | Arrow arrow; |
| 169 | if ( event.Leaving() ) |
| 170 | { |
| 171 | // no arrow has mouse if it left the window completely |
| 172 | arrow = Arrow_None; |
| 173 | } |
| 174 | else // Moving() or Entering(), treat them the same here |
| 175 | { |
| 176 | arrow = m_control->HitTest(event.GetPosition()); |
| 177 | } |
| 178 | |
| 179 | #if wxUSE_TIMER |
| 180 | if ( m_captureData && m_captureData->m_timerScroll) |
| 181 | { |
| 182 | // the mouse is captured, we may want to pause scrolling if it goes |
| 183 | // outside the arrow or to resume it if we had paused it before |
| 184 | wxTimer *timer = m_captureData->m_timerScroll; |
| 185 | if ( !timer->IsRunning() ) |
| 186 | { |
| 187 | // timer is paused |
| 188 | if ( arrow == m_captureData->m_arrowPressed ) |
| 189 | { |
| 190 | // resume now |
| 191 | m_control->SetArrowFlag(arrow, wxCONTROL_PRESSED, true); |
| 192 | timer->Start(); |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | else // if ( 1 ) FIXME: m_control->ShouldPauseScrolling() ) |
| 198 | { |
| 199 | // we may want to stop it |
| 200 | if ( arrow != m_captureData->m_arrowPressed ) |
| 201 | { |
| 202 | // stop as the mouse left the arrow |
| 203 | m_control->SetArrowFlag(m_captureData->m_arrowPressed, |
| 204 | wxCONTROL_PRESSED, false); |
| 205 | timer->Stop(); |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return false; |
| 212 | } |
| 213 | #endif // wxUSE_TIMER |
| 214 | |
| 215 | // reset the wxCONTROL_CURRENT flag for the arrows which don't have the |
| 216 | // mouse and set it for the one which has |
| 217 | UpdateCurrentFlag(Arrow_First, arrow); |
| 218 | UpdateCurrentFlag(Arrow_Second, arrow); |
| 219 | |
| 220 | // return true if it was really an event for an arrow |
| 221 | return !event.Leaving() && arrow != Arrow_None; |
| 222 | } |
| 223 | |
| 224 | bool wxScrollArrows::HandleMouse(const wxMouseEvent& event) const |
| 225 | { |
| 226 | int btn = event.GetButton(); |
| 227 | if ( btn == -1 ) |
| 228 | { |
| 229 | // we only care about button press/release events |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | if ( event.ButtonDown() || event.ButtonDClick() ) |
| 234 | { |
| 235 | if ( !m_captureData ) |
| 236 | { |
| 237 | Arrow arrow = m_control->HitTest(event.GetPosition()); |
| 238 | if ( arrow == Arrow_None ) |
| 239 | { |
| 240 | // mouse pressed over something else |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | if ( m_control->GetArrowState(arrow) & wxCONTROL_DISABLED ) |
| 245 | { |
| 246 | // don't allow to press disabled arrows |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | wxConstCast(this, wxScrollArrows)->m_captureData = |
| 251 | new wxScrollArrowCaptureData; |
| 252 | m_captureData->m_arrowPressed = arrow; |
| 253 | m_captureData->m_btnCapture = btn; |
| 254 | m_captureData->m_window = m_control->GetWindow(); |
| 255 | m_captureData->m_window->CaptureMouse(); |
| 256 | |
| 257 | #if wxUSE_TIMER |
| 258 | // start scrolling |
| 259 | wxScrollArrowTimer *tmpTimerScroll = |
| 260 | new wxScrollArrowTimer(m_control, arrow); |
| 261 | #endif // wxUSE_TIMER |
| 262 | |
| 263 | // Because in some cases wxScrollArrowTimer can cause |
| 264 | // m_captureData to be destructed we need to test if it |
| 265 | // is still valid before using. |
| 266 | if (m_captureData) |
| 267 | { |
| 268 | #if wxUSE_TIMER |
| 269 | m_captureData->m_timerScroll = tmpTimerScroll; |
| 270 | #endif // wxUSE_TIMER |
| 271 | |
| 272 | m_control->SetArrowFlag(arrow, wxCONTROL_PRESSED, true); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | #if wxUSE_TIMER |
| 277 | delete tmpTimerScroll; |
| 278 | #endif // wxUSE_TIMER |
| 279 | } |
| 280 | } |
| 281 | //else: mouse already captured, nothing to do |
| 282 | } |
| 283 | // release mouse if the *same* button went up |
| 284 | else if ( m_captureData && (btn == m_captureData->m_btnCapture) ) |
| 285 | { |
| 286 | Arrow arrow = m_captureData->m_arrowPressed; |
| 287 | |
| 288 | delete m_captureData; |
| 289 | wxConstCast(this, wxScrollArrows)->m_captureData = NULL; |
| 290 | |
| 291 | m_control->SetArrowFlag(arrow, wxCONTROL_PRESSED, false); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | // we don't process this |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |