]>
Commit | Line | Data |
---|---|---|
1e6feb95 | 1 | ///////////////////////////////////////////////////////////////////////////// |
fbb526cc | 2 | // Name: src/univ/scrolbar.cpp |
1e6feb95 VZ |
3 | // Purpose: wxScrollBar implementation |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 20.08.00 | |
7 | // RCS-ID: $Id$ | |
442b35b5 | 8 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) |
65571936 | 9 | // Licence: wxWindows licence |
1e6feb95 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
1e6feb95 VZ |
20 | #include "wx/wxprec.h" |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_SCROLLBAR | |
27 | ||
e4db172a WS |
28 | #include "wx/scrolbar.h" |
29 | ||
1e6feb95 VZ |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/timer.h" | |
1e6feb95 | 32 | #include "wx/dcclient.h" |
1e6feb95 | 33 | #include "wx/validate.h" |
e4db172a | 34 | #include "wx/log.h" |
1e6feb95 VZ |
35 | #endif |
36 | ||
37 | #include "wx/univ/scrtimer.h" | |
38 | ||
39 | #include "wx/univ/renderer.h" | |
40 | #include "wx/univ/inphand.h" | |
41 | #include "wx/univ/theme.h" | |
42 | ||
43 | #define WXDEBUG_SCROLLBAR | |
44 | ||
45 | #ifndef __WXDEBUG__ | |
46 | #undef WXDEBUG_SCROLLBAR | |
47 | #endif // !__WXDEBUG__ | |
48 | ||
cff7ef89 VS |
49 | #if defined(WXDEBUG_SCROLLBAR) && defined(__WXMSW__) && !defined(__WXMICROWIN__) |
50 | #include "wx/msw/private.h" | |
51 | #endif | |
52 | ||
1e6feb95 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxScrollBarTimer: this class is used to repeatedly scroll the scrollbar | |
55 | // when the mouse is help pressed on the arrow or on the bar. It generates the | |
56 | // given scroll action command periodically. | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | class wxScrollBarTimer : public wxScrollTimer | |
60 | { | |
61 | public: | |
62 | wxScrollBarTimer(wxStdScrollBarInputHandler *handler, | |
63 | const wxControlAction& action, | |
64 | wxScrollBar *control); | |
65 | ||
66 | protected: | |
67 | virtual bool DoNotify(); | |
68 | ||
69 | private: | |
70 | wxStdScrollBarInputHandler *m_handler; | |
71 | wxControlAction m_action; | |
72 | wxScrollBar *m_control; | |
73 | }; | |
74 | ||
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
79 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl) | |
80 | ||
81 | BEGIN_EVENT_TABLE(wxScrollBar, wxScrollBarBase) | |
1e6feb95 VZ |
82 | END_EVENT_TABLE() |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // creation | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | #ifdef __VISUALC__ | |
89 | // warning C4355: 'this' : used in base member initializer list | |
90 | #pragma warning(disable:4355) // so what? disable it... | |
91 | #endif | |
92 | ||
93 | wxScrollBar::wxScrollBar() | |
94 | : m_arrows(this) | |
95 | { | |
96 | Init(); | |
97 | } | |
98 | ||
99 | wxScrollBar::wxScrollBar(wxWindow *parent, | |
100 | wxWindowID id, | |
101 | const wxPoint& pos, | |
102 | const wxSize& size, | |
103 | long style, | |
104 | const wxValidator& validator, | |
105 | const wxString& name) | |
106 | : m_arrows(this) | |
107 | { | |
108 | Init(); | |
109 | ||
110 | (void)Create(parent, id, pos, size, style, validator, name); | |
111 | } | |
112 | ||
113 | #ifdef __VISUALC__ | |
114 | // warning C4355: 'this' : used in base member initializer list | |
115 | #pragma warning(default:4355) | |
116 | #endif | |
117 | ||
118 | void wxScrollBar::Init() | |
119 | { | |
120 | m_range = | |
121 | m_thumbSize = | |
122 | m_thumbPos = | |
123 | m_pageSize = 0; | |
124 | ||
125 | m_thumbPosOld = -1; | |
126 | ||
127 | for ( size_t n = 0; n < WXSIZEOF(m_elementsState); n++ ) | |
128 | { | |
129 | m_elementsState[n] = 0; | |
130 | } | |
131 | ||
a290fa5a | 132 | m_dirty = false; |
1e6feb95 VZ |
133 | } |
134 | ||
135 | bool wxScrollBar::Create(wxWindow *parent, | |
136 | wxWindowID id, | |
137 | const wxPoint &pos, | |
138 | const wxSize &size, | |
139 | long style, | |
140 | const wxValidator& validator, | |
141 | const wxString &name) | |
142 | { | |
143 | // the scrollbars never have the border | |
144 | style &= ~wxBORDER_MASK; | |
145 | ||
61fef19b | 146 | if ( !wxControl::Create(parent, id, pos, size, style, validator, name) ) |
a290fa5a | 147 | return false; |
1e6feb95 VZ |
148 | |
149 | SetBestSize(size); | |
150 | ||
151 | // override the cursor of the target window (if any) | |
152 | SetCursor(wxCURSOR_ARROW); | |
153 | ||
154 | CreateInputHandler(wxINP_HANDLER_SCROLLBAR); | |
155 | ||
a290fa5a | 156 | return true; |
1e6feb95 VZ |
157 | } |
158 | ||
159 | wxScrollBar::~wxScrollBar() | |
160 | { | |
161 | } | |
162 | ||
c7beb048 VZ |
163 | // ---------------------------------------------------------------------------- |
164 | // misc accessors | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | bool wxScrollBar::IsStandalone() const | |
f809133f | 168 | { |
c7beb048 VZ |
169 | wxWindow *parent = GetParent(); |
170 | if ( !parent ) | |
f809133f | 171 | { |
a290fa5a | 172 | return true; |
f809133f | 173 | } |
c7beb048 VZ |
174 | |
175 | return (parent->GetScrollbar(wxHORIZONTAL) != this) && | |
176 | (parent->GetScrollbar(wxVERTICAL) != this); | |
177 | } | |
178 | ||
179 | bool wxScrollBar::AcceptsFocus() const | |
180 | { | |
181 | // the window scrollbars never accept focus | |
182 | return wxScrollBarBase::AcceptsFocus() && IsStandalone(); | |
f809133f RR |
183 | } |
184 | ||
1e6feb95 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // scrollbar API | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | void wxScrollBar::DoSetThumb(int pos) | |
190 | { | |
191 | // don't assert hecks here, we're a private function which is meant to be | |
192 | // called with any args at all | |
193 | if ( pos < 0 ) | |
194 | { | |
195 | pos = 0; | |
196 | } | |
197 | else if ( pos > m_range - m_thumbSize ) | |
198 | { | |
199 | pos = m_range - m_thumbSize; | |
200 | } | |
201 | ||
202 | if ( m_thumbPos == pos ) | |
203 | { | |
204 | // nothing changed, avoid refreshes which would provoke flicker | |
205 | return; | |
206 | } | |
207 | ||
208 | if ( m_thumbPosOld == -1 ) | |
209 | { | |
210 | // remember the old thumb position | |
211 | m_thumbPosOld = m_thumbPos; | |
212 | } | |
213 | ||
214 | m_thumbPos = pos; | |
215 | ||
216 | // we have to refresh the part of the bar which was under the thumb and the | |
217 | // thumb itself | |
218 | m_elementsState[Element_Thumb] |= wxCONTROL_DIRTY; | |
219 | m_elementsState[m_thumbPos > m_thumbPosOld | |
220 | ? Element_Bar_1 : Element_Bar_2] |= wxCONTROL_DIRTY; | |
a290fa5a | 221 | m_dirty = true; |
1e6feb95 VZ |
222 | } |
223 | ||
224 | int wxScrollBar::GetThumbPosition() const | |
225 | { | |
226 | return m_thumbPos; | |
227 | } | |
228 | ||
229 | int wxScrollBar::GetThumbSize() const | |
230 | { | |
231 | return m_thumbSize; | |
232 | } | |
233 | ||
234 | int wxScrollBar::GetPageSize() const | |
235 | { | |
236 | return m_pageSize; | |
237 | } | |
238 | ||
239 | int wxScrollBar::GetRange() const | |
240 | { | |
241 | return m_range; | |
242 | } | |
243 | ||
244 | void wxScrollBar::SetThumbPosition(int pos) | |
245 | { | |
246 | wxCHECK_RET( pos >= 0 && pos <= m_range, _T("thumb position out of range") ); | |
247 | ||
248 | DoSetThumb(pos); | |
249 | } | |
250 | ||
251 | void wxScrollBar::SetScrollbar(int position, int thumbSize, | |
252 | int range, int pageSize, | |
253 | bool refresh) | |
254 | { | |
d6922577 | 255 | // we only refresh everything when the range changes, thumb position |
1e6feb95 VZ |
256 | // changes are handled in OnIdle |
257 | bool needsRefresh = (range != m_range) || | |
258 | (thumbSize != m_thumbSize) || | |
259 | (pageSize != m_pageSize); | |
260 | ||
261 | // set all parameters | |
262 | m_range = range; | |
263 | m_thumbSize = thumbSize; | |
264 | SetThumbPosition(position); | |
265 | m_pageSize = pageSize; | |
266 | ||
267 | // ignore refresh parameter unless we really need to refresh everything - | |
268 | // there ir a lot of existing code which just calls SetScrollbar() without | |
269 | // specifying the last parameter even though it doesn't need at all to | |
270 | // refresh the window immediately | |
271 | if ( refresh && needsRefresh ) | |
272 | { | |
273 | // and update the window | |
274 | Refresh(); | |
275 | Update(); | |
276 | } | |
277 | } | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // geometry | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | wxSize wxScrollBar::DoGetBestClientSize() const | |
284 | { | |
285 | // this dimension is completely arbitrary | |
286 | static const wxCoord SIZE = 140; | |
287 | ||
288 | wxSize size = m_renderer->GetScrollbarArrowSize(); | |
289 | if ( IsVertical() ) | |
290 | { | |
291 | size.y = SIZE; | |
292 | } | |
293 | else // horizontal | |
294 | { | |
295 | size.x = SIZE; | |
296 | } | |
297 | ||
298 | return size; | |
299 | } | |
300 | ||
301 | wxScrollArrows::Arrow wxScrollBar::HitTest(const wxPoint& pt) const | |
302 | { | |
303 | switch ( m_renderer->HitTestScrollbar(this, pt) ) | |
304 | { | |
305 | case wxHT_SCROLLBAR_ARROW_LINE_1: | |
306 | return wxScrollArrows::Arrow_First; | |
307 | ||
308 | case wxHT_SCROLLBAR_ARROW_LINE_2: | |
309 | return wxScrollArrows::Arrow_Second; | |
310 | ||
311 | default: | |
312 | return wxScrollArrows::Arrow_None; | |
313 | } | |
314 | } | |
315 | ||
316 | // ---------------------------------------------------------------------------- | |
317 | // drawing | |
318 | // ---------------------------------------------------------------------------- | |
319 | ||
e39af974 | 320 | void wxScrollBar::OnInternalIdle() |
21c3670f VS |
321 | { |
322 | UpdateThumb(); | |
e28c2d15 | 323 | wxControl::OnInternalIdle(); |
21c3670f VS |
324 | } |
325 | ||
326 | void wxScrollBar::UpdateThumb() | |
1e6feb95 VZ |
327 | { |
328 | if ( m_dirty ) | |
329 | { | |
330 | for ( size_t n = 0; n < WXSIZEOF(m_elementsState); n++ ) | |
331 | { | |
332 | if ( m_elementsState[n] & wxCONTROL_DIRTY ) | |
333 | { | |
334 | wxRect rect = GetRenderer()->GetScrollbarRect(this, (Element)n); | |
335 | ||
336 | if ( rect.width && rect.height ) | |
337 | { | |
338 | // we try to avoid redrawing the entire shaft (which might | |
339 | // be quite long) if possible by only redrawing the area | |
340 | // wich really changed | |
341 | if ( (n == Element_Bar_1 || n == Element_Bar_2) && | |
342 | (m_thumbPosOld != -1) ) | |
343 | { | |
344 | // the less efficient but more reliable (i.e. this will | |
345 | // probably work everywhere) version: refresh the | |
346 | // distance covered by thumb since the last update | |
347 | #if 0 | |
348 | wxRect rectOld = | |
349 | GetRenderer()->GetScrollbarRect(this, | |
350 | (Element)n, | |
351 | m_thumbPosOld); | |
352 | if ( IsVertical() ) | |
353 | { | |
354 | if ( n == Element_Bar_1 ) | |
355 | rect.SetTop(rectOld.GetBottom()); | |
356 | else | |
357 | rect.SetBottom(rectOld.GetBottom()); | |
358 | } | |
359 | else // horizontal | |
360 | { | |
361 | if ( n == Element_Bar_1 ) | |
362 | rect.SetLeft(rectOld.GetRight()); | |
363 | else | |
364 | rect.SetRight(rectOld.GetRight()); | |
365 | } | |
366 | #else // efficient version: only repaint the area occupied by | |
367 | // the thumb previously - we can't do better than this | |
368 | rect = GetRenderer()->GetScrollbarRect(this, | |
369 | Element_Thumb, | |
370 | m_thumbPosOld); | |
371 | #endif // 0/1 | |
372 | } | |
373 | ||
374 | #ifdef WXDEBUG_SCROLLBAR | |
a290fa5a | 375 | static bool s_refreshDebug = false; |
1e6feb95 VZ |
376 | if ( s_refreshDebug ) |
377 | { | |
378 | wxClientDC dc(this); | |
379 | dc.SetBrush(*wxCYAN_BRUSH); | |
380 | dc.SetPen(*wxTRANSPARENT_PEN); | |
381 | dc.DrawRectangle(rect); | |
382 | ||
383 | // under Unix we use "--sync" X option for this | |
8cb172b4 | 384 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
1e6feb95 VZ |
385 | ::GdiFlush(); |
386 | ::Sleep(200); | |
387 | #endif // __WXMSW__ | |
388 | } | |
389 | #endif // WXDEBUG_SCROLLBAR | |
390 | ||
a290fa5a | 391 | Refresh(false, &rect); |
1e6feb95 VZ |
392 | } |
393 | ||
394 | m_elementsState[n] &= ~wxCONTROL_DIRTY; | |
395 | } | |
396 | } | |
397 | ||
a290fa5a | 398 | m_dirty = false; |
1e6feb95 | 399 | } |
1e6feb95 VZ |
400 | } |
401 | ||
402 | void wxScrollBar::DoDraw(wxControlRenderer *renderer) | |
403 | { | |
404 | renderer->DrawScrollbar(this, m_thumbPosOld); | |
405 | ||
406 | // clear all dirty flags | |
a290fa5a | 407 | m_dirty = false; |
1e6feb95 VZ |
408 | m_thumbPosOld = -1; |
409 | } | |
410 | ||
411 | // ---------------------------------------------------------------------------- | |
412 | // state flags | |
413 | // ---------------------------------------------------------------------------- | |
414 | ||
415 | static inline wxScrollBar::Element ElementForArrow(wxScrollArrows::Arrow arrow) | |
416 | { | |
417 | return arrow == wxScrollArrows::Arrow_First | |
418 | ? wxScrollBar::Element_Arrow_Line_1 | |
419 | : wxScrollBar::Element_Arrow_Line_2; | |
420 | } | |
421 | ||
422 | int wxScrollBar::GetArrowState(wxScrollArrows::Arrow arrow) const | |
423 | { | |
424 | return GetState(ElementForArrow(arrow)); | |
425 | } | |
426 | ||
427 | void wxScrollBar::SetArrowFlag(wxScrollArrows::Arrow arrow, int flag, bool set) | |
428 | { | |
429 | Element which = ElementForArrow(arrow); | |
430 | int state = GetState(which); | |
431 | if ( set ) | |
432 | state |= flag; | |
433 | else | |
434 | state &= ~flag; | |
435 | ||
436 | SetState(which, state); | |
437 | } | |
438 | ||
439 | int wxScrollBar::GetState(Element which) const | |
440 | { | |
441 | // if the entire scrollbar is disabled, all of its elements are too | |
442 | int flags = m_elementsState[which]; | |
443 | if ( !IsEnabled() ) | |
444 | flags |= wxCONTROL_DISABLED; | |
445 | ||
446 | return flags; | |
447 | } | |
448 | ||
449 | void wxScrollBar::SetState(Element which, int flags) | |
450 | { | |
451 | if ( (int)(m_elementsState[which] & ~wxCONTROL_DIRTY) != flags ) | |
452 | { | |
453 | m_elementsState[which] = flags | wxCONTROL_DIRTY; | |
454 | ||
a290fa5a | 455 | m_dirty = true; |
1e6feb95 VZ |
456 | } |
457 | } | |
458 | ||
459 | // ---------------------------------------------------------------------------- | |
460 | // input processing | |
461 | // ---------------------------------------------------------------------------- | |
462 | ||
463 | bool wxScrollBar::OnArrow(wxScrollArrows::Arrow arrow) | |
464 | { | |
465 | int oldThumbPos = GetThumbPosition(); | |
466 | PerformAction(arrow == wxScrollArrows::Arrow_First | |
467 | ? wxACTION_SCROLL_LINE_UP | |
468 | : wxACTION_SCROLL_LINE_DOWN); | |
469 | ||
470 | // did we scroll till the end? | |
471 | return GetThumbPosition() != oldThumbPos; | |
472 | } | |
473 | ||
474 | bool wxScrollBar::PerformAction(const wxControlAction& action, | |
475 | long numArg, | |
476 | const wxString& strArg) | |
477 | { | |
478 | int thumbOld = m_thumbPos; | |
479 | ||
a290fa5a | 480 | bool notify = false; // send an event about the change? |
1e6feb95 VZ |
481 | |
482 | wxEventType scrollType; | |
483 | ||
484 | // test for thumb move first as these events happen in quick succession | |
485 | if ( action == wxACTION_SCROLL_THUMB_MOVE ) | |
486 | { | |
487 | DoSetThumb(numArg); | |
488 | ||
21c3670f VS |
489 | // VS: we have to force redraw here, otherwise the thumb will lack |
490 | // behind mouse cursor | |
491 | UpdateThumb(); | |
492 | ||
1e6feb95 VZ |
493 | scrollType = wxEVT_SCROLLWIN_THUMBTRACK; |
494 | } | |
495 | else if ( action == wxACTION_SCROLL_LINE_UP ) | |
496 | { | |
497 | scrollType = wxEVT_SCROLLWIN_LINEUP; | |
498 | ScrollLines(-1); | |
499 | } | |
500 | else if ( action == wxACTION_SCROLL_LINE_DOWN ) | |
501 | { | |
502 | scrollType = wxEVT_SCROLLWIN_LINEDOWN; | |
503 | ScrollLines(1); | |
504 | } | |
505 | else if ( action == wxACTION_SCROLL_PAGE_UP ) | |
506 | { | |
507 | scrollType = wxEVT_SCROLLWIN_PAGEUP; | |
508 | ScrollPages(-1); | |
509 | } | |
510 | else if ( action == wxACTION_SCROLL_PAGE_DOWN ) | |
511 | { | |
512 | scrollType = wxEVT_SCROLLWIN_PAGEDOWN; | |
513 | ScrollPages(1); | |
514 | } | |
515 | else if ( action == wxACTION_SCROLL_START ) | |
516 | { | |
517 | scrollType = wxEVT_SCROLLWIN_THUMBRELEASE; // anything better? | |
518 | ScrollToStart(); | |
519 | } | |
520 | else if ( action == wxACTION_SCROLL_END ) | |
521 | { | |
522 | scrollType = wxEVT_SCROLLWIN_THUMBRELEASE; // anything better? | |
523 | ScrollToEnd(); | |
524 | } | |
525 | else if ( action == wxACTION_SCROLL_THUMB_DRAG ) | |
526 | { | |
527 | // we won't use it but this line suppresses the compiler | |
528 | // warning about "variable may be used without having been | |
529 | // initialized" | |
530 | scrollType = wxEVT_NULL; | |
531 | } | |
532 | else if ( action == wxACTION_SCROLL_THUMB_RELEASE ) | |
533 | { | |
534 | // always notify about this | |
a290fa5a | 535 | notify = true; |
1e6feb95 VZ |
536 | scrollType = wxEVT_SCROLLWIN_THUMBRELEASE; |
537 | } | |
538 | else | |
539 | return wxControl::PerformAction(action, numArg, strArg); | |
540 | ||
541 | // has scrollbar position changed? | |
542 | bool changed = m_thumbPos != thumbOld; | |
543 | if ( notify || changed ) | |
544 | { | |
c7beb048 VZ |
545 | if ( IsStandalone() ) |
546 | { | |
547 | // we should generate EVT_SCROLL events for the standalone | |
548 | // scrollbars and not the EVT_SCROLLWIN ones | |
549 | // | |
550 | // NB: we assume that scrollbar events are sequentially numbered | |
551 | // but this should be ok as other code relies on this as well | |
552 | scrollType += wxEVT_SCROLL_TOP - wxEVT_SCROLLWIN_TOP; | |
93c96bab VZ |
553 | wxScrollEvent event(scrollType, this->GetId(), m_thumbPos, |
554 | IsVertical() ? wxVERTICAL : wxHORIZONTAL); | |
555 | event.SetEventObject(this); | |
35932e05 | 556 | GetEventHandler()->ProcessEvent(event); |
93c96bab VZ |
557 | } |
558 | else // part of the window | |
559 | { | |
560 | wxScrollWinEvent event(scrollType, m_thumbPos, | |
561 | IsVertical() ? wxVERTICAL : wxHORIZONTAL); | |
562 | event.SetEventObject(this); | |
563 | GetParent()->GetEventHandler()->ProcessEvent(event); | |
c7beb048 | 564 | } |
1e6feb95 VZ |
565 | } |
566 | ||
a290fa5a | 567 | return true; |
1e6feb95 VZ |
568 | } |
569 | ||
570 | void wxScrollBar::ScrollToStart() | |
571 | { | |
572 | DoSetThumb(0); | |
573 | } | |
574 | ||
575 | void wxScrollBar::ScrollToEnd() | |
576 | { | |
577 | DoSetThumb(m_range - m_thumbSize); | |
578 | } | |
579 | ||
2b92b572 | 580 | bool wxScrollBar::ScrollLines(int nLines) |
1e6feb95 VZ |
581 | { |
582 | DoSetThumb(m_thumbPos + nLines); | |
a290fa5a | 583 | return true; |
1e6feb95 VZ |
584 | } |
585 | ||
2b92b572 | 586 | bool wxScrollBar::ScrollPages(int nPages) |
1e6feb95 VZ |
587 | { |
588 | DoSetThumb(m_thumbPos + nPages*m_pageSize); | |
a290fa5a | 589 | return true; |
1e6feb95 VZ |
590 | } |
591 | ||
592 | // ============================================================================ | |
593 | // scroll bar input handler | |
594 | // ============================================================================ | |
595 | ||
596 | // ---------------------------------------------------------------------------- | |
597 | // wxScrollBarTimer | |
598 | // ---------------------------------------------------------------------------- | |
599 | ||
600 | wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler *handler, | |
601 | const wxControlAction& action, | |
602 | wxScrollBar *control) | |
603 | { | |
604 | m_handler = handler; | |
605 | m_action = action; | |
606 | m_control = control; | |
607 | } | |
608 | ||
609 | bool wxScrollBarTimer::DoNotify() | |
610 | { | |
611 | return m_handler->OnScrollTimer(m_control, m_action); | |
612 | } | |
613 | ||
614 | // ---------------------------------------------------------------------------- | |
615 | // wxStdScrollBarInputHandler | |
616 | // ---------------------------------------------------------------------------- | |
617 | ||
618 | wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer *renderer, | |
619 | wxInputHandler *handler) | |
620 | : wxStdInputHandler(handler) | |
621 | { | |
622 | m_renderer = renderer; | |
623 | m_winCapture = NULL; | |
624 | m_htLast = wxHT_NOWHERE; | |
625 | m_timerScroll = NULL; | |
626 | } | |
627 | ||
628 | wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler() | |
629 | { | |
630 | // normally, it's NULL by now but just in case the user somehow managed to | |
631 | // keep the mouse captured until now... | |
632 | delete m_timerScroll; | |
633 | } | |
634 | ||
635 | void wxStdScrollBarInputHandler::SetElementState(wxScrollBar *control, | |
636 | int flag, | |
637 | bool doIt) | |
638 | { | |
639 | if ( m_htLast > wxHT_SCROLLBAR_FIRST && m_htLast < wxHT_SCROLLBAR_LAST ) | |
640 | { | |
641 | wxScrollBar::Element | |
642 | elem = (wxScrollBar::Element)(m_htLast - wxHT_SCROLLBAR_FIRST - 1); | |
643 | ||
644 | int flags = control->GetState(elem); | |
645 | if ( doIt ) | |
646 | flags |= flag; | |
647 | else | |
648 | flags &= ~flag; | |
649 | control->SetState(elem, flags); | |
650 | } | |
651 | } | |
652 | ||
653 | bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar *scrollbar, | |
654 | const wxControlAction& action) | |
655 | { | |
656 | int oldThumbPos = scrollbar->GetThumbPosition(); | |
657 | scrollbar->PerformAction(action); | |
658 | if ( scrollbar->GetThumbPosition() != oldThumbPos ) | |
a290fa5a | 659 | return true; |
1e6feb95 VZ |
660 | |
661 | // we scrolled till the end | |
662 | m_timerScroll->Stop(); | |
663 | ||
a290fa5a | 664 | return false; |
1e6feb95 VZ |
665 | } |
666 | ||
667 | void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar *control) | |
668 | { | |
669 | // return everything to the normal state | |
670 | if ( m_winCapture ) | |
671 | { | |
672 | m_winCapture->ReleaseMouse(); | |
673 | m_winCapture = NULL; | |
674 | } | |
675 | ||
676 | m_btnCapture = -1; | |
677 | ||
678 | if ( m_timerScroll ) | |
679 | { | |
680 | delete m_timerScroll; | |
681 | m_timerScroll = NULL; | |
682 | } | |
683 | ||
684 | // unpress the arrow and highlight the current element | |
a290fa5a | 685 | Press(control, false); |
1e6feb95 VZ |
686 | } |
687 | ||
688 | wxCoord | |
689 | wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar *scrollbar, | |
690 | const wxMouseEvent& event) const | |
691 | { | |
692 | wxPoint pt = event.GetPosition(); | |
693 | return scrollbar->GetWindowStyle() & wxVERTICAL ? pt.y : pt.x; | |
694 | } | |
695 | ||
696 | void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar *scrollbar, | |
697 | const wxMouseEvent& event) | |
698 | { | |
699 | int thumbPos = GetMouseCoord(scrollbar, event) - m_ofsMouse; | |
700 | thumbPos = m_renderer->PixelToScrollbar(scrollbar, thumbPos); | |
701 | scrollbar->PerformAction(wxACTION_SCROLL_THUMB_MOVE, thumbPos); | |
702 | } | |
703 | ||
23645bfa | 704 | bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer *consumer, |
1e6feb95 VZ |
705 | const wxKeyEvent& event, |
706 | bool pressed) | |
707 | { | |
708 | // we only react to the key presses here | |
709 | if ( pressed ) | |
710 | { | |
711 | wxControlAction action; | |
712 | switch ( event.GetKeyCode() ) | |
713 | { | |
714 | case WXK_DOWN: | |
715 | case WXK_RIGHT: action = wxACTION_SCROLL_LINE_DOWN; break; | |
716 | case WXK_UP: | |
717 | case WXK_LEFT: action = wxACTION_SCROLL_LINE_UP; break; | |
718 | case WXK_HOME: action = wxACTION_SCROLL_START; break; | |
719 | case WXK_END: action = wxACTION_SCROLL_END; break; | |
fbb526cc WS |
720 | case WXK_PAGEUP: action = wxACTION_SCROLL_PAGE_UP; break; |
721 | case WXK_PAGEDOWN: action = wxACTION_SCROLL_PAGE_DOWN; break; | |
1e6feb95 VZ |
722 | } |
723 | ||
a290fa5a | 724 | if ( !action.IsEmpty() ) |
1e6feb95 | 725 | { |
23645bfa | 726 | consumer->PerformAction(action); |
1e6feb95 | 727 | |
a290fa5a | 728 | return true; |
1e6feb95 VZ |
729 | } |
730 | } | |
731 | ||
23645bfa | 732 | return wxStdInputHandler::HandleKey(consumer, event, pressed); |
1e6feb95 VZ |
733 | } |
734 | ||
23645bfa | 735 | bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer *consumer, |
1e6feb95 VZ |
736 | const wxMouseEvent& event) |
737 | { | |
738 | // is this a click event from an acceptable button? | |
739 | int btn = event.GetButton(); | |
740 | if ( (btn != -1) && IsAllowedButton(btn) ) | |
741 | { | |
742 | // determine which part of the window mouse is in | |
23645bfa | 743 | wxScrollBar *scrollbar = wxStaticCast(consumer->GetInputWindow(), wxScrollBar); |
1e6feb95 VZ |
744 | wxHitTest ht = m_renderer->HitTestScrollbar |
745 | ( | |
746 | scrollbar, | |
747 | event.GetPosition() | |
748 | ); | |
749 | ||
750 | // when the mouse is pressed on any scrollbar element, we capture it | |
751 | // and hold capture until the same mouse button is released | |
752 | if ( event.ButtonDown() || event.ButtonDClick() ) | |
753 | { | |
754 | if ( !m_winCapture ) | |
755 | { | |
756 | m_btnCapture = btn; | |
23645bfa | 757 | m_winCapture = consumer->GetInputWindow(); |
1e6feb95 VZ |
758 | m_winCapture->CaptureMouse(); |
759 | ||
760 | // generate the command | |
a290fa5a | 761 | bool hasAction = true; |
1e6feb95 VZ |
762 | wxControlAction action; |
763 | switch ( ht ) | |
764 | { | |
765 | case wxHT_SCROLLBAR_ARROW_LINE_1: | |
766 | action = wxACTION_SCROLL_LINE_UP; | |
767 | break; | |
768 | ||
769 | case wxHT_SCROLLBAR_ARROW_LINE_2: | |
770 | action = wxACTION_SCROLL_LINE_DOWN; | |
771 | break; | |
772 | ||
773 | case wxHT_SCROLLBAR_BAR_1: | |
774 | action = wxACTION_SCROLL_PAGE_UP; | |
775 | m_ptStartScrolling = event.GetPosition(); | |
776 | break; | |
777 | ||
778 | case wxHT_SCROLLBAR_BAR_2: | |
779 | action = wxACTION_SCROLL_PAGE_DOWN; | |
780 | m_ptStartScrolling = event.GetPosition(); | |
781 | break; | |
782 | ||
783 | case wxHT_SCROLLBAR_THUMB: | |
23645bfa | 784 | consumer->PerformAction(wxACTION_SCROLL_THUMB_DRAG); |
1e6feb95 VZ |
785 | m_ofsMouse = GetMouseCoord(scrollbar, event) - |
786 | m_renderer->ScrollbarToPixel(scrollbar); | |
787 | ||
788 | // fall through: there is no immediate action | |
789 | ||
790 | default: | |
a290fa5a | 791 | hasAction = false; |
1e6feb95 VZ |
792 | } |
793 | ||
794 | // remove highlighting | |
a290fa5a | 795 | Highlight(scrollbar, false); |
1e6feb95 VZ |
796 | m_htLast = ht; |
797 | ||
798 | // and press the arrow or highlight thumb now instead | |
799 | if ( m_htLast == wxHT_SCROLLBAR_THUMB ) | |
a290fa5a | 800 | Highlight(scrollbar, true); |
1e6feb95 | 801 | else |
a290fa5a | 802 | Press(scrollbar, true); |
1e6feb95 VZ |
803 | |
804 | // start dragging | |
805 | if ( hasAction ) | |
806 | { | |
807 | m_timerScroll = new wxScrollBarTimer(this, action, | |
808 | scrollbar); | |
809 | m_timerScroll->StartAutoScroll(); | |
810 | } | |
811 | //else: no (immediate) action | |
812 | ||
813 | } | |
814 | //else: mouse already captured, nothing to do | |
815 | } | |
816 | // release mouse if the *same* button went up | |
817 | else if ( btn == m_btnCapture ) | |
818 | { | |
819 | if ( m_winCapture ) | |
820 | { | |
821 | StopScrolling(scrollbar); | |
822 | ||
823 | // if we were dragging the thumb, send the last event | |
824 | if ( m_htLast == wxHT_SCROLLBAR_THUMB ) | |
825 | { | |
826 | scrollbar->PerformAction(wxACTION_SCROLL_THUMB_RELEASE); | |
827 | } | |
828 | ||
829 | m_htLast = ht; | |
a290fa5a | 830 | Highlight(scrollbar, true); |
1e6feb95 VZ |
831 | } |
832 | else | |
833 | { | |
834 | // this is not supposed to happen as the button can't go up | |
835 | // without going down previously and then we'd have | |
836 | // m_winCapture by now | |
837 | wxFAIL_MSG( _T("logic error in mouse capturing code") ); | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
23645bfa | 842 | return wxStdInputHandler::HandleMouse(consumer, event); |
1e6feb95 VZ |
843 | } |
844 | ||
23645bfa | 845 | bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer *consumer, |
1e6feb95 VZ |
846 | const wxMouseEvent& event) |
847 | { | |
23645bfa | 848 | wxScrollBar *scrollbar = wxStaticCast(consumer->GetInputWindow(), wxScrollBar); |
1e6feb95 VZ |
849 | |
850 | if ( m_winCapture ) | |
851 | { | |
ca1ecff4 | 852 | if ( (m_htLast == wxHT_SCROLLBAR_THUMB) && event.Dragging() ) |
1e6feb95 VZ |
853 | { |
854 | // make the thumb follow the mouse by keeping the same offset | |
855 | // between the mouse position and the top/left of the thumb | |
856 | HandleThumbMove(scrollbar, event); | |
857 | ||
a290fa5a | 858 | return true; |
1e6feb95 VZ |
859 | } |
860 | ||
861 | // no other changes are possible while the mouse is captured | |
a290fa5a | 862 | return false; |
1e6feb95 VZ |
863 | } |
864 | ||
865 | bool isArrow = scrollbar->GetArrows().HandleMouseMove(event); | |
866 | ||
ca1ecff4 | 867 | if ( event.Dragging() ) |
1e6feb95 VZ |
868 | { |
869 | wxHitTest ht = m_renderer->HitTestScrollbar | |
870 | ( | |
871 | scrollbar, | |
872 | event.GetPosition() | |
873 | ); | |
874 | if ( ht == m_htLast ) | |
875 | { | |
876 | // nothing changed | |
a290fa5a | 877 | return false; |
1e6feb95 VZ |
878 | } |
879 | ||
880 | #ifdef DEBUG_MOUSE | |
881 | wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht); | |
882 | #endif // DEBUG_MOUSE | |
883 | ||
a290fa5a | 884 | Highlight(scrollbar, false); |
1e6feb95 VZ |
885 | m_htLast = ht; |
886 | ||
887 | if ( !isArrow ) | |
a290fa5a | 888 | Highlight(scrollbar, true); |
1e6feb95 VZ |
889 | //else: already done by wxScrollArrows::HandleMouseMove |
890 | } | |
891 | else if ( event.Leaving() ) | |
892 | { | |
893 | if ( !isArrow ) | |
a290fa5a | 894 | Highlight(scrollbar, false); |
1e6feb95 VZ |
895 | |
896 | m_htLast = wxHT_NOWHERE; | |
897 | } | |
898 | else // event.Entering() | |
899 | { | |
900 | // we don't process this event | |
a290fa5a | 901 | return false; |
1e6feb95 VZ |
902 | } |
903 | ||
904 | // we did something | |
a290fa5a | 905 | return true; |
1e6feb95 VZ |
906 | } |
907 | ||
908 | #endif // wxUSE_SCROLLBAR | |
909 | ||
9a6384ca WS |
910 | #if wxUSE_TIMER |
911 | ||
1e6feb95 VZ |
912 | // ---------------------------------------------------------------------------- |
913 | // wxScrollTimer | |
914 | // ---------------------------------------------------------------------------- | |
915 | ||
916 | wxScrollTimer::wxScrollTimer() | |
917 | { | |
a290fa5a | 918 | m_skipNext = false; |
1e6feb95 VZ |
919 | } |
920 | ||
921 | void wxScrollTimer::StartAutoScroll() | |
922 | { | |
923 | // start scrolling immediately | |
924 | if ( !DoNotify() ) | |
925 | { | |
926 | // ... and end it too | |
927 | return; | |
928 | } | |
929 | ||
930 | // there is an initial delay before the scrollbar starts scrolling - | |
931 | // implement it by ignoring the first timer expiration and only start | |
932 | // scrolling from the second one | |
a290fa5a | 933 | m_skipNext = true; |
1e6feb95 VZ |
934 | Start(200); // FIXME: hardcoded delay |
935 | } | |
936 | ||
937 | void wxScrollTimer::Notify() | |
938 | { | |
939 | if ( m_skipNext ) | |
940 | { | |
941 | // scroll normally now - reduce the delay | |
942 | Stop(); | |
943 | Start(50); // FIXME: hardcoded delay | |
944 | ||
a290fa5a | 945 | m_skipNext = false; |
1e6feb95 VZ |
946 | } |
947 | else | |
948 | { | |
949 | // if DoNotify() returns false, we're already deleted by the timer | |
950 | // event handler, so don't do anything else here | |
951 | (void)DoNotify(); | |
952 | } | |
953 | } | |
9a6384ca WS |
954 | |
955 | #endif // wxUSE_TIMER |