]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: univ/scrolbar.cpp | |
3 | // Purpose: wxScrollBar implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 20.08.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 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 | #if wxUSE_SCROLLBAR | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/timer.h" | |
30 | ||
31 | #include "wx/dcclient.h" | |
32 | #include "wx/scrolbar.h" | |
33 | #include "wx/validate.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/univ/scrtimer.h" | |
37 | ||
38 | #include "wx/univ/renderer.h" | |
39 | #include "wx/univ/inphand.h" | |
40 | #include "wx/univ/theme.h" | |
41 | #include "wx/log.h" | |
42 | ||
43 | #define WXDEBUG_SCROLLBAR | |
44 | ||
45 | #ifndef __WXDEBUG__ | |
46 | #undef WXDEBUG_SCROLLBAR | |
47 | #endif // !__WXDEBUG__ | |
48 | ||
49 | #if defined(WXDEBUG_SCROLLBAR) && defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
50 | #include "wx/msw/private.h" | |
51 | #endif | |
52 | ||
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) | |
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 | ||
132 | m_dirty = false; | |
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 | ||
146 | if ( !wxControl::Create(parent, id, pos, size, style, validator, name) ) | |
147 | return false; | |
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 | ||
156 | return true; | |
157 | } | |
158 | ||
159 | wxScrollBar::~wxScrollBar() | |
160 | { | |
161 | } | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // misc accessors | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | bool wxScrollBar::IsStandalone() const | |
168 | { | |
169 | wxWindow *parent = GetParent(); | |
170 | if ( !parent ) | |
171 | { | |
172 | return true; | |
173 | } | |
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(); | |
183 | } | |
184 | ||
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; | |
221 | m_dirty = true; | |
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 | { | |
255 | // we only refresh everything when the range changes, thumb position | |
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 | ||
320 | void wxScrollBar::OnInternalIdle() | |
321 | { | |
322 | UpdateThumb(); | |
323 | wxControl::OnInternalIdle(); | |
324 | } | |
325 | ||
326 | void wxScrollBar::UpdateThumb() | |
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 | |
375 | static bool s_refreshDebug = false; | |
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 | |
384 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
385 | ::GdiFlush(); | |
386 | ::Sleep(200); | |
387 | #endif // __WXMSW__ | |
388 | } | |
389 | #endif // WXDEBUG_SCROLLBAR | |
390 | ||
391 | Refresh(false, &rect); | |
392 | } | |
393 | ||
394 | m_elementsState[n] &= ~wxCONTROL_DIRTY; | |
395 | } | |
396 | } | |
397 | ||
398 | m_dirty = false; | |
399 | } | |
400 | } | |
401 | ||
402 | void wxScrollBar::DoDraw(wxControlRenderer *renderer) | |
403 | { | |
404 | renderer->DrawScrollbar(this, m_thumbPosOld); | |
405 | ||
406 | // clear all dirty flags | |
407 | m_dirty = false; | |
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 | ||
455 | m_dirty = true; | |
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 | ||
480 | bool notify = false; // send an event about the change? | |
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 | ||
489 | // VS: we have to force redraw here, otherwise the thumb will lack | |
490 | // behind mouse cursor | |
491 | UpdateThumb(); | |
492 | ||
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 | |
535 | notify = true; | |
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 | { | |
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; | |
553 | wxScrollEvent event(scrollType, this->GetId(), m_thumbPos, | |
554 | IsVertical() ? wxVERTICAL : wxHORIZONTAL); | |
555 | event.SetEventObject(this); | |
556 | GetParent()->GetEventHandler()->ProcessEvent(event); | |
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); | |
564 | } | |
565 | } | |
566 | ||
567 | return true; | |
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 | ||
580 | bool wxScrollBar::ScrollLines(int nLines) | |
581 | { | |
582 | DoSetThumb(m_thumbPos + nLines); | |
583 | return true; | |
584 | } | |
585 | ||
586 | bool wxScrollBar::ScrollPages(int nPages) | |
587 | { | |
588 | DoSetThumb(m_thumbPos + nPages*m_pageSize); | |
589 | return true; | |
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 ) | |
659 | return true; | |
660 | ||
661 | // we scrolled till the end | |
662 | m_timerScroll->Stop(); | |
663 | ||
664 | return false; | |
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 | |
685 | Press(control, false); | |
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 | ||
704 | bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer *consumer, | |
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; | |
720 | case WXK_PAGEUP: | |
721 | case WXK_PRIOR: action = wxACTION_SCROLL_PAGE_UP; break; | |
722 | case WXK_PAGEDOWN: | |
723 | case WXK_NEXT: action = wxACTION_SCROLL_PAGE_DOWN; break; | |
724 | } | |
725 | ||
726 | if ( !action.IsEmpty() ) | |
727 | { | |
728 | consumer->PerformAction(action); | |
729 | ||
730 | return true; | |
731 | } | |
732 | } | |
733 | ||
734 | return wxStdInputHandler::HandleKey(consumer, event, pressed); | |
735 | } | |
736 | ||
737 | bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer *consumer, | |
738 | const wxMouseEvent& event) | |
739 | { | |
740 | // is this a click event from an acceptable button? | |
741 | int btn = event.GetButton(); | |
742 | if ( (btn != -1) && IsAllowedButton(btn) ) | |
743 | { | |
744 | // determine which part of the window mouse is in | |
745 | wxScrollBar *scrollbar = wxStaticCast(consumer->GetInputWindow(), wxScrollBar); | |
746 | wxHitTest ht = m_renderer->HitTestScrollbar | |
747 | ( | |
748 | scrollbar, | |
749 | event.GetPosition() | |
750 | ); | |
751 | ||
752 | // when the mouse is pressed on any scrollbar element, we capture it | |
753 | // and hold capture until the same mouse button is released | |
754 | if ( event.ButtonDown() || event.ButtonDClick() ) | |
755 | { | |
756 | if ( !m_winCapture ) | |
757 | { | |
758 | m_btnCapture = btn; | |
759 | m_winCapture = consumer->GetInputWindow(); | |
760 | m_winCapture->CaptureMouse(); | |
761 | ||
762 | // generate the command | |
763 | bool hasAction = true; | |
764 | wxControlAction action; | |
765 | switch ( ht ) | |
766 | { | |
767 | case wxHT_SCROLLBAR_ARROW_LINE_1: | |
768 | action = wxACTION_SCROLL_LINE_UP; | |
769 | break; | |
770 | ||
771 | case wxHT_SCROLLBAR_ARROW_LINE_2: | |
772 | action = wxACTION_SCROLL_LINE_DOWN; | |
773 | break; | |
774 | ||
775 | case wxHT_SCROLLBAR_BAR_1: | |
776 | action = wxACTION_SCROLL_PAGE_UP; | |
777 | m_ptStartScrolling = event.GetPosition(); | |
778 | break; | |
779 | ||
780 | case wxHT_SCROLLBAR_BAR_2: | |
781 | action = wxACTION_SCROLL_PAGE_DOWN; | |
782 | m_ptStartScrolling = event.GetPosition(); | |
783 | break; | |
784 | ||
785 | case wxHT_SCROLLBAR_THUMB: | |
786 | consumer->PerformAction(wxACTION_SCROLL_THUMB_DRAG); | |
787 | m_ofsMouse = GetMouseCoord(scrollbar, event) - | |
788 | m_renderer->ScrollbarToPixel(scrollbar); | |
789 | ||
790 | // fall through: there is no immediate action | |
791 | ||
792 | default: | |
793 | hasAction = false; | |
794 | } | |
795 | ||
796 | // remove highlighting | |
797 | Highlight(scrollbar, false); | |
798 | m_htLast = ht; | |
799 | ||
800 | // and press the arrow or highlight thumb now instead | |
801 | if ( m_htLast == wxHT_SCROLLBAR_THUMB ) | |
802 | Highlight(scrollbar, true); | |
803 | else | |
804 | Press(scrollbar, true); | |
805 | ||
806 | // start dragging | |
807 | if ( hasAction ) | |
808 | { | |
809 | m_timerScroll = new wxScrollBarTimer(this, action, | |
810 | scrollbar); | |
811 | m_timerScroll->StartAutoScroll(); | |
812 | } | |
813 | //else: no (immediate) action | |
814 | ||
815 | } | |
816 | //else: mouse already captured, nothing to do | |
817 | } | |
818 | // release mouse if the *same* button went up | |
819 | else if ( btn == m_btnCapture ) | |
820 | { | |
821 | if ( m_winCapture ) | |
822 | { | |
823 | StopScrolling(scrollbar); | |
824 | ||
825 | // if we were dragging the thumb, send the last event | |
826 | if ( m_htLast == wxHT_SCROLLBAR_THUMB ) | |
827 | { | |
828 | scrollbar->PerformAction(wxACTION_SCROLL_THUMB_RELEASE); | |
829 | } | |
830 | ||
831 | m_htLast = ht; | |
832 | Highlight(scrollbar, true); | |
833 | } | |
834 | else | |
835 | { | |
836 | // this is not supposed to happen as the button can't go up | |
837 | // without going down previously and then we'd have | |
838 | // m_winCapture by now | |
839 | wxFAIL_MSG( _T("logic error in mouse capturing code") ); | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | return wxStdInputHandler::HandleMouse(consumer, event); | |
845 | } | |
846 | ||
847 | bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer *consumer, | |
848 | const wxMouseEvent& event) | |
849 | { | |
850 | wxScrollBar *scrollbar = wxStaticCast(consumer->GetInputWindow(), wxScrollBar); | |
851 | ||
852 | if ( m_winCapture ) | |
853 | { | |
854 | if ( (m_htLast == wxHT_SCROLLBAR_THUMB) && event.Dragging() ) | |
855 | { | |
856 | // make the thumb follow the mouse by keeping the same offset | |
857 | // between the mouse position and the top/left of the thumb | |
858 | HandleThumbMove(scrollbar, event); | |
859 | ||
860 | return true; | |
861 | } | |
862 | ||
863 | // no other changes are possible while the mouse is captured | |
864 | return false; | |
865 | } | |
866 | ||
867 | bool isArrow = scrollbar->GetArrows().HandleMouseMove(event); | |
868 | ||
869 | if ( event.Dragging() ) | |
870 | { | |
871 | wxHitTest ht = m_renderer->HitTestScrollbar | |
872 | ( | |
873 | scrollbar, | |
874 | event.GetPosition() | |
875 | ); | |
876 | if ( ht == m_htLast ) | |
877 | { | |
878 | // nothing changed | |
879 | return false; | |
880 | } | |
881 | ||
882 | #ifdef DEBUG_MOUSE | |
883 | wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht); | |
884 | #endif // DEBUG_MOUSE | |
885 | ||
886 | Highlight(scrollbar, false); | |
887 | m_htLast = ht; | |
888 | ||
889 | if ( !isArrow ) | |
890 | Highlight(scrollbar, true); | |
891 | //else: already done by wxScrollArrows::HandleMouseMove | |
892 | } | |
893 | else if ( event.Leaving() ) | |
894 | { | |
895 | if ( !isArrow ) | |
896 | Highlight(scrollbar, false); | |
897 | ||
898 | m_htLast = wxHT_NOWHERE; | |
899 | } | |
900 | else // event.Entering() | |
901 | { | |
902 | // we don't process this event | |
903 | return false; | |
904 | } | |
905 | ||
906 | // we did something | |
907 | return true; | |
908 | } | |
909 | ||
910 | #endif // wxUSE_SCROLLBAR | |
911 | ||
912 | // ---------------------------------------------------------------------------- | |
913 | // wxScrollTimer | |
914 | // ---------------------------------------------------------------------------- | |
915 | ||
916 | wxScrollTimer::wxScrollTimer() | |
917 | { | |
918 | m_skipNext = false; | |
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 | |
933 | m_skipNext = true; | |
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 | ||
945 | m_skipNext = false; | |
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 | } | |
954 |