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