merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / control / slider.cxx
blobca623483da1067a0504fd1fd517eb9655a8b3ea9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: slider.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #ifndef _SV_RC_H
35 #include <tools/rc.h>
36 #endif
37 #include <vcl/event.hxx>
38 #include <vcl/decoview.hxx>
39 #include <vcl/slider.hxx>
43 // =======================================================================
45 static long ImplMulDiv( long nNumber, long nNumerator, long nDenominator )
47 double n = ((double)nNumber * (double)nNumerator) / (double)nDenominator;
48 return (long)n;
51 // =======================================================================
53 #define SLIDER_DRAW_THUMB ((USHORT)0x0001)
54 #define SLIDER_DRAW_CHANNEL1 ((USHORT)0x0002)
55 #define SLIDER_DRAW_CHANNEL2 ((USHORT)0x0004)
56 #define SLIDER_DRAW_CHANNEL (SLIDER_DRAW_CHANNEL1 | SLIDER_DRAW_CHANNEL2)
57 #define SLIDER_DRAW_ALL (SLIDER_DRAW_THUMB | SLIDER_DRAW_CHANNEL)
59 #define SLIDER_STATE_CHANNEL1_DOWN ((USHORT)0x0001)
60 #define SLIDER_STATE_CHANNEL2_DOWN ((USHORT)0x0002)
61 #define SLIDER_STATE_THUMB_DOWN ((USHORT)0x0004)
63 #define SLIDER_THUMB_SIZE 9
64 #define SLIDER_THUMB_HALFSIZE 4
65 #define SLIDER_CHANNEL_OFFSET 0
66 #define SLIDER_CHANNEL_SIZE 4
67 #define SLIDER_CHANNEL_HALFSIZE 2
69 #define SLIDER_HEIGHT 16
71 #define SLIDER_VIEW_STYLE (WB_3DLOOK | WB_HORZ | WB_VERT)
73 // =======================================================================
75 void Slider::ImplInit( Window* pParent, WinBits nStyle )
77 mnThumbPixOffset = 0;
78 mnThumbPixRange = 0;
79 mnThumbPixPos = 0; // between mnThumbPixOffset and mnThumbPixOffset+mnThumbPixRange
80 mnChannelPixOffset = 0;
81 mnChannelPixRange = 0;
82 mnChannelPixTop = 0;
83 mnChannelPixBottom = 0;
85 mnMinRange = 0;
86 mnMaxRange = 100;
87 mnThumbPos = 0;
88 mnLineSize = 1;
89 mnPageSize = 1;
90 mnDelta = 0;
91 mnDragDraw = 0;
92 mnStateFlags = 0;
93 meScrollType = SCROLL_DONTKNOW;
94 mbCalcSize = TRUE;
95 mbFullDrag = TRUE;
97 Control::ImplInit( pParent, nStyle, NULL );
99 ImplInitSettings();
100 SetSizePixel( CalcWindowSizePixel() );
103 // -----------------------------------------------------------------------
105 Slider::Slider( Window* pParent, WinBits nStyle ) :
106 Control( WINDOW_SLIDER )
108 ImplInit( pParent, nStyle );
111 // -----------------------------------------------------------------------
113 Slider::Slider( Window* pParent, const ResId& rResId ) :
114 Control( WINDOW_SLIDER )
116 rResId.SetRT( RSC_SCROLLBAR );
117 WinBits nStyle = ImplInitRes( rResId );
118 ImplInit( pParent, nStyle );
119 ImplLoadRes( rResId );
121 if ( !(nStyle & WB_HIDE) )
122 Show();
125 // -----------------------------------------------------------------------
127 void Slider::ImplLoadRes( const ResId& rResId )
129 Control::ImplLoadRes( rResId );
131 INT16 nMin = ReadShortRes();
132 INT16 nMax = ReadShortRes();
133 INT16 nThumbPos = ReadShortRes();
134 INT16 nPage = ReadShortRes();
135 INT16 nStep = ReadShortRes();
136 /* INT16 nVisibleSize = */ ReadShortRes();
138 SetRange( Range( nMin, nMax ) );
139 SetLineSize( nStep );
140 SetPageSize( nPage );
141 SetThumbPos( nThumbPos );
144 // -----------------------------------------------------------------------
146 void Slider::ImplInitSettings()
148 Window* pParent = GetParent();
149 if ( pParent->IsChildTransparentModeEnabled() && !IsControlBackground() )
151 EnableChildTransparentMode( TRUE );
152 SetParentClipMode( PARENTCLIPMODE_NOCLIP );
153 SetPaintTransparent( TRUE );
154 SetBackground();
156 else
158 EnableChildTransparentMode( FALSE );
159 SetParentClipMode( 0 );
160 SetPaintTransparent( FALSE );
162 if ( IsControlBackground() )
163 SetBackground( GetControlBackground() );
164 else
165 SetBackground( pParent->GetBackground() );
169 // -----------------------------------------------------------------------
171 void Slider::ImplUpdateRects( BOOL bUpdate )
173 Rectangle aOldThumbRect = maThumbRect;
175 if ( mnThumbPixRange )
177 if ( GetStyle() & WB_HORZ )
179 maThumbRect.Left() = mnThumbPixPos-SLIDER_THUMB_HALFSIZE;
180 maThumbRect.Right() = maThumbRect.Left()+SLIDER_THUMB_SIZE-1;
181 if ( mnChannelPixOffset < maThumbRect.Left() )
183 maChannel1Rect.Left() = mnChannelPixOffset;
184 maChannel1Rect.Right() = maThumbRect.Left()-1;
185 maChannel1Rect.Top() = mnChannelPixTop;
186 maChannel1Rect.Bottom() = mnChannelPixBottom;
188 else
189 maChannel1Rect.SetEmpty();
190 if ( mnChannelPixOffset+mnChannelPixRange-1 > maThumbRect.Right() )
192 maChannel2Rect.Left() = maThumbRect.Right()+1;
193 maChannel2Rect.Right() = mnChannelPixOffset+mnChannelPixRange-1;
194 maChannel2Rect.Top() = mnChannelPixTop;
195 maChannel2Rect.Bottom() = mnChannelPixBottom;
197 else
198 maChannel2Rect.SetEmpty();
200 else
202 maThumbRect.Top() = mnThumbPixPos-SLIDER_THUMB_HALFSIZE;
203 maThumbRect.Bottom() = maThumbRect.Top()+SLIDER_THUMB_SIZE-1;
204 if ( mnChannelPixOffset < maThumbRect.Top() )
206 maChannel1Rect.Top() = mnChannelPixOffset;
207 maChannel1Rect.Bottom() = maThumbRect.Top()-1;
208 maChannel1Rect.Left() = mnChannelPixTop;
209 maChannel1Rect.Right() = mnChannelPixBottom;
211 else
212 maChannel1Rect.SetEmpty();
213 if ( mnChannelPixOffset+mnChannelPixRange-1 > maThumbRect.Bottom() )
215 maChannel2Rect.Top() = maThumbRect.Bottom()+1;
216 maChannel2Rect.Bottom() = mnChannelPixOffset+mnChannelPixRange-1;
217 maChannel2Rect.Left() = mnChannelPixTop;
218 maChannel2Rect.Right() = mnChannelPixBottom;
220 else
221 maChannel2Rect.SetEmpty();
224 else
226 maChannel1Rect.SetEmpty();
227 maChannel2Rect.SetEmpty();
228 maThumbRect.SetEmpty();
231 if ( bUpdate )
233 if ( aOldThumbRect != maThumbRect )
235 Region aInvalidRegion( aOldThumbRect );
236 aInvalidRegion.Union( maThumbRect );
238 if( !IsBackground() && GetParent() )
240 const Point aPos( GetPosPixel() );
241 aInvalidRegion.Move( aPos.X(), aPos.Y() );
242 GetParent()->Invalidate( aInvalidRegion, INVALIDATE_TRANSPARENT | INVALIDATE_UPDATE );
244 else
245 Invalidate( aInvalidRegion );
250 // -----------------------------------------------------------------------
252 long Slider::ImplCalcThumbPos( long nPixPos )
254 // Position berechnen
255 long nCalcThumbPos;
256 nCalcThumbPos = ImplMulDiv( nPixPos-mnThumbPixOffset, mnMaxRange-mnMinRange, mnThumbPixRange-1 );
257 nCalcThumbPos += mnMinRange;
258 return nCalcThumbPos;
261 // -----------------------------------------------------------------------
263 long Slider::ImplCalcThumbPosPix( long nPos )
265 // Position berechnen
266 long nCalcThumbPos;
267 nCalcThumbPos = ImplMulDiv( nPos-mnMinRange, mnThumbPixRange-1, mnMaxRange-mnMinRange );
268 // Am Anfang und Ende des Sliders versuchen wir die Anzeige korrekt
269 // anzuzeigen
270 if ( !nCalcThumbPos && (mnThumbPos > mnMinRange) )
271 nCalcThumbPos = 1;
272 if ( nCalcThumbPos &&
273 (nCalcThumbPos == mnThumbPixRange-1) &&
274 (mnThumbPos < mnMaxRange) )
275 nCalcThumbPos--;
276 return nCalcThumbPos+mnThumbPixOffset;
279 // -----------------------------------------------------------------------
281 void Slider::ImplCalc( BOOL bUpdate )
283 BOOL bInvalidateAll = FALSE;
285 if ( mbCalcSize )
287 long nOldChannelPixOffset = mnChannelPixOffset;
288 long nOldChannelPixRange = mnChannelPixRange;
289 long nOldChannelPixTop = mnChannelPixTop;
290 long nOldChannelPixBottom = mnChannelPixBottom;
291 long nCalcWidth;
292 long nCalcHeight;
294 maChannel1Rect.SetEmpty();
295 maChannel2Rect.SetEmpty();
296 maThumbRect.SetEmpty();
298 Size aSize = GetOutputSizePixel();
299 if ( GetStyle() & WB_HORZ )
301 nCalcWidth = aSize.Width();
302 nCalcHeight = aSize.Height();
303 maThumbRect.Top() = 0;
304 maThumbRect.Bottom()= aSize.Height()-1;
306 else
308 nCalcWidth = aSize.Height();
309 nCalcHeight = aSize.Width();
310 maThumbRect.Left() = 0;
311 maThumbRect.Right() = aSize.Width()-1;
314 if ( nCalcWidth >= SLIDER_THUMB_SIZE )
316 mnThumbPixOffset = SLIDER_THUMB_HALFSIZE;
317 mnThumbPixRange = nCalcWidth-(SLIDER_THUMB_HALFSIZE*2);
318 mnThumbPixPos = 0;
319 mnChannelPixOffset = SLIDER_CHANNEL_OFFSET;
320 mnChannelPixRange = nCalcWidth-(SLIDER_CHANNEL_OFFSET*2);
321 mnChannelPixTop = (nCalcHeight/2)-SLIDER_CHANNEL_HALFSIZE;
322 mnChannelPixBottom = mnChannelPixTop+SLIDER_CHANNEL_SIZE-1;
324 else
326 mnThumbPixRange = 0;
327 mnChannelPixRange = 0;
330 if ( (nOldChannelPixOffset != mnChannelPixOffset) ||
331 (nOldChannelPixRange != mnChannelPixRange) ||
332 (nOldChannelPixTop != mnChannelPixTop) ||
333 (nOldChannelPixBottom != mnChannelPixBottom) )
334 bInvalidateAll = TRUE;
336 mbCalcSize = FALSE;
339 if ( mnThumbPixRange )
340 mnThumbPixPos = ImplCalcThumbPosPix( mnThumbPos );
342 if ( bUpdate && bInvalidateAll )
344 Invalidate();
345 bUpdate = FALSE;
347 ImplUpdateRects( bUpdate );
350 // -----------------------------------------------------------------------
352 void Slider::ImplDraw( USHORT nDrawFlags )
354 DecorationView aDecoView( this );
355 USHORT nStyle;
356 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
357 BOOL bEnabled = IsEnabled();
359 // Evt. noch offene Berechnungen nachholen
360 if ( mbCalcSize )
361 ImplCalc( FALSE );
363 if ( (nDrawFlags & SLIDER_DRAW_CHANNEL1) && !maChannel1Rect.IsEmpty() )
365 long nRectSize;
366 Rectangle aRect = maChannel1Rect;
367 SetLineColor( rStyleSettings.GetShadowColor() );
368 if ( GetStyle() & WB_HORZ )
370 DrawLine( aRect.TopLeft(), Point( aRect.Left(), aRect.Bottom()-1 ) );
371 DrawLine( aRect.TopLeft(), aRect.TopRight() );
373 else
375 DrawLine( aRect.TopLeft(), Point( aRect.Right()-1, aRect.Top() ) );
376 DrawLine( aRect.TopLeft(), aRect.BottomLeft() );
378 SetLineColor( rStyleSettings.GetLightColor() );
379 if ( GetStyle() & WB_HORZ )
381 DrawLine( aRect.BottomLeft(), aRect.BottomRight() );
382 nRectSize = aRect.GetWidth();
384 else
386 DrawLine( aRect.TopRight(), aRect.BottomRight() );
387 nRectSize = aRect.GetHeight();
390 if ( nRectSize > 1 )
392 aRect.Left()++;
393 aRect.Top()++;
394 if ( GetStyle() & WB_HORZ )
395 aRect.Bottom()--;
396 else
397 aRect.Right()--;
398 SetLineColor();
399 if ( mnStateFlags & SLIDER_STATE_CHANNEL1_DOWN )
400 SetFillColor( rStyleSettings.GetShadowColor() );
401 else
402 SetFillColor( rStyleSettings.GetCheckedColor() );
403 DrawRect( aRect );
407 if ( (nDrawFlags & SLIDER_DRAW_CHANNEL2) && !maChannel2Rect.IsEmpty() )
409 long nRectSize;
410 Rectangle aRect = maChannel2Rect;
411 SetLineColor( rStyleSettings.GetLightColor() );
412 if ( GetStyle() & WB_HORZ )
414 DrawLine( aRect.TopRight(), aRect.BottomRight() );
415 DrawLine( aRect.BottomLeft(), aRect.BottomRight() );
416 nRectSize = aRect.GetWidth();
418 else
420 DrawLine( aRect.BottomLeft(), aRect.BottomRight() );
421 DrawLine( aRect.TopRight(), aRect.BottomRight() );
422 nRectSize = aRect.GetHeight();
425 if ( nRectSize > 1 )
427 SetLineColor( rStyleSettings.GetShadowColor() );
428 if ( GetStyle() & WB_HORZ )
429 DrawLine( aRect.TopLeft(), Point( aRect.Right()-1, aRect.Top() ) );
430 else
431 DrawLine( aRect.TopLeft(), Point( aRect.Left(), aRect.Bottom()-1 ) );
433 aRect.Right()--;
434 aRect.Bottom()--;
435 if ( GetStyle() & WB_HORZ )
436 aRect.Top()++;
437 else
438 aRect.Left()++;
439 SetLineColor();
440 if ( mnStateFlags & SLIDER_STATE_CHANNEL2_DOWN )
441 SetFillColor( rStyleSettings.GetShadowColor() );
442 else
443 SetFillColor( rStyleSettings.GetCheckedColor() );
444 DrawRect( aRect );
448 if ( nDrawFlags & SLIDER_DRAW_THUMB )
450 if ( !maThumbRect.IsEmpty() )
452 if ( bEnabled )
454 nStyle = 0;
455 if ( mnStateFlags & SLIDER_STATE_THUMB_DOWN )
456 nStyle |= BUTTON_DRAW_PRESSED;
457 aDecoView.DrawButton( maThumbRect, nStyle );
459 else
461 SetLineColor( rStyleSettings.GetShadowColor() );
462 SetFillColor( rStyleSettings.GetCheckedColor() );
463 DrawRect( maThumbRect );
469 // -----------------------------------------------------------------------
471 BOOL Slider::ImplIsPageUp( const Point& rPos )
473 Size aSize = GetOutputSizePixel();
474 Rectangle aRect = maChannel1Rect;
475 if ( GetStyle() & WB_HORZ )
477 aRect.Top() = 0;
478 aRect.Bottom() = aSize.Height()-1;
480 else
482 aRect.Left() = 0;
483 aRect.Right() = aSize.Width()-1;
485 return aRect.IsInside( rPos );
488 // -----------------------------------------------------------------------
490 BOOL Slider::ImplIsPageDown( const Point& rPos )
492 Size aSize = GetOutputSizePixel();
493 Rectangle aRect = maChannel2Rect;
494 if ( GetStyle() & WB_HORZ )
496 aRect.Top() = 0;
497 aRect.Bottom() = aSize.Height()-1;
499 else
501 aRect.Left() = 0;
502 aRect.Right() = aSize.Width()-1;
504 return aRect.IsInside( rPos );
507 // -----------------------------------------------------------------------
509 long Slider::ImplSlide( long nNewPos, BOOL bCallEndSlide )
511 long nOldPos = mnThumbPos;
512 SetThumbPos( nNewPos );
513 long nDelta = mnThumbPos-nOldPos;
514 if ( nDelta )
516 mnDelta = nDelta;
517 Slide();
518 if ( bCallEndSlide )
519 EndSlide();
520 mnDelta = 0;
522 return nDelta;
525 // -----------------------------------------------------------------------
527 long Slider::ImplDoAction( BOOL bCallEndSlide )
529 long nDelta = 0;
531 switch ( meScrollType )
533 case SCROLL_LINEUP:
534 nDelta = ImplSlide( mnThumbPos-mnLineSize, bCallEndSlide );
535 break;
537 case SCROLL_LINEDOWN:
538 nDelta = ImplSlide( mnThumbPos+mnLineSize, bCallEndSlide );
539 break;
541 case SCROLL_PAGEUP:
542 nDelta = ImplSlide( mnThumbPos-mnPageSize, bCallEndSlide );
543 break;
545 case SCROLL_PAGEDOWN:
546 nDelta = ImplSlide( mnThumbPos+mnPageSize, bCallEndSlide );
547 break;
549 case SCROLL_SET:
550 nDelta = ImplSlide( ImplCalcThumbPos( GetPointerPosPixel().X() ), bCallEndSlide );
551 break;
552 default:
553 break;
556 return nDelta;
559 // -----------------------------------------------------------------------
561 void Slider::ImplDoMouseAction( const Point& rMousePos, BOOL bCallAction )
563 USHORT nOldStateFlags = mnStateFlags;
564 BOOL bAction = FALSE;
566 switch ( meScrollType )
568 case( SCROLL_SET ):
570 const bool bUp = ImplIsPageUp( rMousePos ), bDown = ImplIsPageDown( rMousePos );
572 if ( bUp || bDown )
574 bAction = bCallAction;
575 mnStateFlags |= ( bUp ? SLIDER_STATE_CHANNEL1_DOWN : SLIDER_STATE_CHANNEL2_DOWN );
577 else
578 mnStateFlags &= ~( SLIDER_STATE_CHANNEL1_DOWN | SLIDER_STATE_CHANNEL2_DOWN );
579 break;
582 case SCROLL_PAGEUP:
583 if ( ImplIsPageUp( rMousePos ) )
585 bAction = bCallAction;
586 mnStateFlags |= SLIDER_STATE_CHANNEL1_DOWN;
588 else
589 mnStateFlags &= ~SLIDER_STATE_CHANNEL1_DOWN;
590 break;
592 case SCROLL_PAGEDOWN:
593 if ( ImplIsPageDown( rMousePos ) )
595 bAction = bCallAction;
596 mnStateFlags |= SLIDER_STATE_CHANNEL2_DOWN;
598 else
599 mnStateFlags &= ~SLIDER_STATE_CHANNEL2_DOWN;
600 break;
601 default:
602 break;
605 if ( bAction )
607 if ( ImplDoAction( FALSE ) )
609 // Update the channel complete
610 if ( mnDragDraw & SLIDER_DRAW_CHANNEL )
612 Update();
613 ImplDraw( mnDragDraw );
617 else if ( nOldStateFlags != mnStateFlags )
618 ImplDraw( mnDragDraw );
621 // -----------------------------------------------------------------------
623 long Slider::ImplDoSlide( long nNewPos )
625 if ( meScrollType != SCROLL_DONTKNOW )
626 return 0;
628 meScrollType = SCROLL_DRAG;
629 long nDelta = ImplSlide( nNewPos, TRUE );
630 meScrollType = SCROLL_DONTKNOW;
631 return nDelta;
634 // -----------------------------------------------------------------------
636 long Slider::ImplDoSlideAction( ScrollType eScrollType )
638 if ( (meScrollType != SCROLL_DONTKNOW) ||
639 (eScrollType == SCROLL_DONTKNOW) ||
640 (eScrollType == SCROLL_DRAG) )
641 return 0;
643 meScrollType = eScrollType;
644 long nDelta = ImplDoAction( TRUE );
645 meScrollType = SCROLL_DONTKNOW;
646 return nDelta;
649 // -----------------------------------------------------------------------
651 void Slider::MouseButtonDown( const MouseEvent& rMEvt )
653 if ( rMEvt.IsLeft() )
655 const Point& rMousePos = rMEvt.GetPosPixel();
656 USHORT nTrackFlags = 0;
658 if ( maThumbRect.IsInside( rMousePos ) )
660 nTrackFlags = 0;
661 meScrollType = SCROLL_DRAG;
662 mnDragDraw = SLIDER_DRAW_THUMB;
664 // Zusaetzliche Daten berechnen
665 Point aCenterPos = maThumbRect.Center();
666 if ( GetStyle() & WB_HORZ )
667 mnMouseOff = rMousePos.X()-aCenterPos.X();
668 else
669 mnMouseOff = rMousePos.Y()-aCenterPos.Y();
671 // Im OS2-Look geben wir den Thumb gedrueckt aus
672 if ( GetSettings().GetStyleSettings().GetOptions() & STYLE_OPTION_OS2STYLE )
674 mnStateFlags |= SLIDER_STATE_THUMB_DOWN;
675 ImplDraw( SLIDER_DRAW_THUMB );
678 else if ( ImplIsPageUp( rMousePos ) )
680 if( GetStyle() & WB_SLIDERSET )
681 meScrollType = SCROLL_SET;
682 else
684 nTrackFlags = STARTTRACK_BUTTONREPEAT;
685 meScrollType = SCROLL_PAGEUP;
688 mnDragDraw = SLIDER_DRAW_CHANNEL;
690 else if ( ImplIsPageDown( rMousePos ) )
692 if( GetStyle() & WB_SLIDERSET )
693 meScrollType = SCROLL_SET;
694 else
696 nTrackFlags = STARTTRACK_BUTTONREPEAT;
697 meScrollType = SCROLL_PAGEDOWN;
700 mnDragDraw = SLIDER_DRAW_CHANNEL;
703 // Soll Tracking gestartet werden
704 if( meScrollType != SCROLL_DONTKNOW )
706 // Startposition merken fuer Abbruch und EndScroll-Delta
707 mnStartPos = mnThumbPos;
708 ImplDoMouseAction( rMousePos, meScrollType != SCROLL_SET );
709 Update();
711 if( meScrollType != SCROLL_SET )
712 StartTracking( nTrackFlags );
717 // -----------------------------------------------------------------------
719 void Slider::MouseButtonUp( const MouseEvent& )
721 if( SCROLL_SET == meScrollType )
723 // Button und PageRect-Status wieder herstellen
724 const USHORT nOldStateFlags = mnStateFlags;
726 mnStateFlags &= ~( SLIDER_STATE_CHANNEL1_DOWN | SLIDER_STATE_CHANNEL2_DOWN | SLIDER_STATE_THUMB_DOWN );
728 if ( nOldStateFlags != mnStateFlags )
729 ImplDraw( mnDragDraw );
731 mnDragDraw = 0;
732 ImplDoAction( TRUE );
733 meScrollType = SCROLL_DONTKNOW;
737 // -----------------------------------------------------------------------
739 void Slider::Tracking( const TrackingEvent& rTEvt )
741 if ( rTEvt.IsTrackingEnded() )
743 // Button und PageRect-Status wieder herstellen
744 USHORT nOldStateFlags = mnStateFlags;
745 mnStateFlags &= ~(SLIDER_STATE_CHANNEL1_DOWN | SLIDER_STATE_CHANNEL2_DOWN |
746 SLIDER_STATE_THUMB_DOWN);
747 if ( nOldStateFlags != mnStateFlags )
748 ImplDraw( mnDragDraw );
749 mnDragDraw = 0;
751 // Bei Abbruch, die alte ThumbPosition wieder herstellen
752 if ( rTEvt.IsTrackingCanceled() )
754 long nOldPos = mnThumbPos;
755 SetThumbPos( mnStartPos );
756 mnDelta = mnThumbPos-nOldPos;
757 Slide();
760 if ( meScrollType == SCROLL_DRAG )
762 // Wenn gedragt wurde, berechnen wir den Thumb neu, damit
763 // er wieder auf einer gerundeten ThumbPosition steht
764 ImplCalc();
765 Update();
767 if ( !mbFullDrag && (mnStartPos != mnThumbPos) )
769 mnDelta = mnThumbPos-mnStartPos;
770 Slide();
771 mnDelta = 0;
775 mnDelta = mnThumbPos-mnStartPos;
776 EndSlide();
777 mnDelta = 0;
778 meScrollType = SCROLL_DONTKNOW;
780 else
782 const Point rMousePos = rTEvt.GetMouseEvent().GetPosPixel();
784 // Dragging wird speziell behandelt
785 if ( meScrollType == SCROLL_DRAG )
787 long nMovePix;
788 Point aCenterPos = maThumbRect.Center();
789 if ( GetStyle() & WB_HORZ )
790 nMovePix = rMousePos.X()-(aCenterPos.X()+mnMouseOff);
791 else
792 nMovePix = rMousePos.Y()-(aCenterPos.Y()+mnMouseOff);
793 // Nur wenn sich Maus in die Scrollrichtung bewegt, muessen
794 // wir etwas tun
795 if ( nMovePix )
797 mnThumbPixPos += nMovePix;
798 if ( mnThumbPixPos < mnThumbPixOffset )
799 mnThumbPixPos = mnThumbPixOffset;
800 if ( mnThumbPixPos > (mnThumbPixOffset+mnThumbPixRange-1) )
801 mnThumbPixPos = mnThumbPixOffset+mnThumbPixRange-1;
802 long nOldPos = mnThumbPos;
803 mnThumbPos = ImplCalcThumbPos( mnThumbPixPos );
804 if ( nOldPos != mnThumbPos )
806 ImplUpdateRects();
807 Update();
808 if ( mbFullDrag && (nOldPos != mnThumbPos) )
810 mnDelta = mnThumbPos-nOldPos;
811 Slide();
812 mnDelta = 0;
817 else
818 ImplDoMouseAction( rMousePos, rTEvt.IsTrackingRepeat() );
820 // Wenn Slider-Werte so umgesetzt wurden, das es nichts
821 // mehr zum Tracking gibt, dann berechen wir hier ab
822 if ( !IsVisible() )
823 EndTracking();
827 // -----------------------------------------------------------------------
829 void Slider::KeyInput( const KeyEvent& rKEvt )
831 if ( !rKEvt.GetKeyCode().GetModifier() )
833 switch ( rKEvt.GetKeyCode().GetCode() )
835 case KEY_HOME:
836 ImplDoSlide( GetRangeMin() );
837 break;
838 case KEY_END:
839 ImplDoSlide( GetRangeMax() );
840 break;
842 case KEY_LEFT:
843 case KEY_UP:
844 ImplDoSlideAction( SCROLL_LINEUP );
845 break;
847 case KEY_RIGHT:
848 case KEY_DOWN:
849 ImplDoSlideAction( SCROLL_LINEDOWN );
850 break;
852 case KEY_PAGEUP:
853 ImplDoSlideAction( SCROLL_PAGEUP );
854 break;
856 case KEY_PAGEDOWN:
857 ImplDoSlideAction( SCROLL_PAGEDOWN );
858 break;
860 default:
861 Control::KeyInput( rKEvt );
862 break;
865 else
866 Control::KeyInput( rKEvt );
869 // -----------------------------------------------------------------------
871 void Slider::Paint( const Rectangle& )
873 ImplDraw( SLIDER_DRAW_ALL );
876 // -----------------------------------------------------------------------
878 void Slider::Resize()
880 Control::Resize();
881 mbCalcSize = TRUE;
882 if ( IsReallyVisible() )
883 ImplCalc( FALSE );
884 Invalidate();
887 // -----------------------------------------------------------------------
889 void Slider::RequestHelp( const HelpEvent& rHEvt )
891 Control::RequestHelp( rHEvt );
894 // -----------------------------------------------------------------------
896 void Slider::StateChanged( StateChangedType nType )
898 Control::StateChanged( nType );
900 if ( nType == STATE_CHANGE_INITSHOW )
901 ImplCalc( FALSE );
902 else if ( nType == STATE_CHANGE_DATA )
904 if ( IsReallyVisible() && IsUpdateMode() )
905 ImplCalc( TRUE );
907 else if ( nType == STATE_CHANGE_UPDATEMODE )
909 if ( IsReallyVisible() && IsUpdateMode() )
911 ImplCalc( FALSE );
912 Invalidate();
915 else if ( nType == STATE_CHANGE_ENABLE )
917 if ( IsReallyVisible() && IsUpdateMode() )
918 Invalidate();
920 else if ( nType == STATE_CHANGE_STYLE )
922 if ( IsReallyVisible() && IsUpdateMode() )
924 if ( (GetPrevStyle() & SLIDER_VIEW_STYLE) !=
925 (GetStyle() & SLIDER_VIEW_STYLE) )
927 mbCalcSize = TRUE;
928 ImplCalc( FALSE );
929 Invalidate();
933 else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
935 ImplInitSettings();
936 Invalidate();
940 // -----------------------------------------------------------------------
942 void Slider::DataChanged( const DataChangedEvent& rDCEvt )
944 Control::DataChanged( rDCEvt );
946 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
947 (rDCEvt.GetFlags() & SETTINGS_STYLE) )
949 ImplInitSettings();
950 Invalidate();
954 // -----------------------------------------------------------------------
956 void Slider::Slide()
958 maSlideHdl.Call( this );
961 // -----------------------------------------------------------------------
963 void Slider::EndSlide()
965 maEndSlideHdl.Call( this );
968 // -----------------------------------------------------------------------
970 void Slider::SetRangeMin( long nNewRange )
972 SetRange( Range( nNewRange, GetRangeMax() ) );
975 // -----------------------------------------------------------------------
977 void Slider::SetRangeMax( long nNewRange )
979 SetRange( Range( GetRangeMin(), nNewRange ) );
982 // -----------------------------------------------------------------------
984 void Slider::SetRange( const Range& rRange )
986 // Range einpassen
987 Range aRange = rRange;
988 aRange.Justify();
989 long nNewMinRange = aRange.Min();
990 long nNewMaxRange = aRange.Max();
992 // Wenn Range sich unterscheidet, dann neuen setzen
993 if ( (mnMinRange != nNewMinRange) ||
994 (mnMaxRange != nNewMaxRange) )
996 mnMinRange = nNewMinRange;
997 mnMaxRange = nNewMaxRange;
999 // Thumb einpassen
1000 if ( mnThumbPos > mnMaxRange )
1001 mnThumbPos = mnMaxRange;
1002 if ( mnThumbPos < mnMinRange )
1003 mnThumbPos = mnMinRange;
1005 StateChanged( STATE_CHANGE_DATA );
1009 // -----------------------------------------------------------------------
1011 void Slider::SetThumbPos( long nNewThumbPos )
1013 if ( nNewThumbPos < mnMinRange )
1014 nNewThumbPos = mnMinRange;
1015 if ( nNewThumbPos > mnMaxRange )
1016 nNewThumbPos = mnMaxRange;
1018 if ( mnThumbPos != nNewThumbPos )
1020 mnThumbPos = nNewThumbPos;
1021 StateChanged( STATE_CHANGE_DATA );
1025 // -----------------------------------------------------------------------
1027 Size Slider::CalcWindowSizePixel()
1029 long nWidth = mnMaxRange-mnMinRange+(SLIDER_THUMB_HALFSIZE*2)+1;
1030 long nHeight = SLIDER_HEIGHT;
1031 Size aSize;
1032 if ( GetStyle() & WB_HORZ )
1034 aSize.Width() = nWidth;
1035 aSize.Height() = nHeight;
1037 else
1039 aSize.Height() = nWidth;
1040 aSize.Width() = nHeight;
1042 return aSize;