summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/src/widgets/tqscrollbar.cpp
blob: 09d1575f70a44ac42b97b28fc36f5536add53eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
/****************************************************************************
**
** Implementation of TQScrollBar class
**
** Created : 940427
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the widgets module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "tqscrollbar.h"
#ifndef TQT_NO_SCROLLBAR
#include "tqpainter.h"
#include "tqbitmap.h"
#include "tqapplication.h"
#include "tqtimer.h"
#include "tqstyle.h"
#ifndef TQT_NO_CURSOR
#include <tqcursor.h>
#endif
#if defined(TQT_ACCESSIBILITY_SUPPORT)
#include "tqaccessible.h"
#endif
#include <limits.h>

/*!
    \class TQScrollBar
    \brief The TQScrollBar widget provides a vertical or horizontal scroll bar.

    \ingroup basic

    A scroll bar allows the user to control a value within a
    program-definable range and gives users a visible indication of
    the current value of a \link TQRangeControl range control \endlink.

    Scroll bars include four separate controls:

    \list

    \i The \e line-up and \e line-down controls are little buttons
    which the user can use to move one "line" up or down. The meaning
    of line is configurable. In editors and list boxes it means one
    line of text; in an image viewer it might mean 20 pixels.

    \i The \e slider is the handle that indicates the current value of
    the scroll bar, which the user can drag to change the value. This
    part of the scroll bar is sometimes called the "thumb".

    \i The \e page-up/page-down control is the area on which the
    slider slides (the scroll bar's background). Clicking here moves
    the scroll bar towards the click. The meaning of "page" is also
    configurable: in editors and list boxes it means as many lines as
    there is space for in the widget.

    \endlist

    TQScrollBar has very few of its own functions; it mostly relies on
    TQRangeControl. The most useful functions are setValue() to set the
    scroll bar directly to some value; addPage(), addLine(),
    subtractPage(), and subtractLine() to simulate the effects of
    clicking (useful for accelerator keys); setSteps() to define the
    values of pageStep() and lineStep(); and setRange() to set the
    minValue() and maxValue() of the scroll bar. TQScrollBar has a
    convenience constructor with which you can set most of these
    properties.

    Some GUI styles (for example, the Windows and Motif styles
    provided with TQt), also use the pageStep() value to calculate the
    size of the slider.

    In addition to the access functions from TQRangeControl, TQScrollBar
    provides a comprehensive set of Q_SIGNALS:
    \table
    \header \i Signal \i Emitted when
    \row \i \l valueChanged()
	 \i the scroll bar's value has changed. The tracking()
	    determines whether this signal is emitted during user
	    interaction.
    \row \i \l sliderPressed()
	 \i the user starts to drag the slider.
    \row \i \l sliderMoved()
	 \i the user drags the slider.
    \row \i \l sliderReleased()
	 \i the user releases the slider.
    \row \i \l nextLine()
	 \i the scroll bar has moved one line down or right. Line is
	    defined in TQRangeControl.
    \row \i \l prevLine()
	 \i the scroll bar has moved one line up or left.
    \row \i \l nextPage()
	 \i the scroll bar has moved one page down or right.
    \row \i \l prevPage()
	 \i the scroll bar has moved one page up or left.
    \endtable

    TQScrollBar only provides integer ranges. Note that although
    TQScrollBar handles very large numbers, scroll bars on current
    screens cannot usefully control ranges above about 100,000 pixels.
    Beyond that, it becomes difficult for the user to control the
    scroll bar using either the keyboard or the mouse.

    A scroll bar can be controlled by the keyboard, but it has a
    default focusPolicy() of \c NoFocus. Use setFocusPolicy() to
    enable keyboard focus. See keyPressEvent() for a list of key
    bindings.

    If you need to add scroll bars to an interface, consider using the
    TQScrollView class, which encapsulates the common uses for scroll
    bars.

    <img src=qscrbar-m.png> <img src=qscrbar-w.png>

    \sa TQSlider TQSpinBox TQScrollView
    \link guibooks.html#fowler GUI Design Handbook: Scroll Bar\endlink
*/


/*!
    \fn void TQScrollBar::valueChanged( int value )

    This signal is emitted when the scroll bar value has changed, with
    the new scroll bar \a value as an argument.
*/

/*!
    \fn void TQScrollBar::sliderPressed()

    This signal is emitted when the user presses the slider with the
    mouse.
*/

/*!
    \fn void TQScrollBar::sliderMoved( int value )

    This signal is emitted when the slider is dragged by the user, with
    the new scroll bar \a value as an argument.

    This signal is emitted even when tracking is turned off.

    \sa tracking() valueChanged() nextLine() prevLine() nextPage()
    prevPage()
*/

/*!
    \fn void TQScrollBar::sliderReleased()

    This signal is emitted when the user releases the slider with the
    mouse.
*/

/*!
    \fn void TQScrollBar::nextLine()

    This signal is emitted when the scroll bar scrolls one line down
    or right.
*/

/*!
    \fn void TQScrollBar::prevLine()

    This signal is emitted when the scroll bar scrolls one line up or
    left.
*/

/*!
    \fn void TQScrollBar::nextPage()

    This signal is emitted when the scroll bar scrolls one page down
    or right.
*/

/*!
    \fn void TQScrollBar::prevPage()

    This signal is emitted when the scroll bar scrolls one page up or
    left.
*/



static const int thresholdTime = 500;
static const int repeatTime	= 50;

#define HORIZONTAL	(orientation() == Qt::Horizontal)
#define VERTICAL	!HORIZONTAL
#define MOTIF_BORDER	2
#define SLIDER_MIN	9


/*!
    Constructs a vertical scroll bar.

    The \a tqparent and \a name arguments are sent on to the TQWidget
    constructor.

    The \c minValue defaults to 0, the \c maxValue to 99, with a \c
    lineStep size of 1 and a \c pageStep size of 10, and an initial
    \c value of 0.
*/

TQScrollBar::TQScrollBar( TQWidget *tqparent, const char *name )
    : TQWidget( tqparent, name ), orient( Qt::Vertical )
{
    init();
}

/*!
    Constructs a scroll bar.

    The \a orientation must be \c Qt::Vertical or \c Qt::Horizontal.

    The \a tqparent and \a name arguments are sent on to the TQWidget
    constructor.

    The \c minValue defaults to 0, the \c maxValue to 99, with a \c
    lineStep size of 1 and a \c pageStep size of 10, and an initial
    \c value of 0.
*/

TQScrollBar::TQScrollBar( Qt::Orientation orientation, TQWidget *tqparent,
			const char *name )
    : TQWidget( tqparent, name ), orient( orientation )
{
    init();
}

/*!
    Constructs a scroll bar whose value can never be smaller than \a
    minValue or greater than \a maxValue, whose line step size is \a
    lineStep and page step size is \a pageStep and whose value is
    initially \a value (which is guaranteed to be in range using
    bound()).

    If \a orientation is \c Vertical the scroll bar is vertical and if
    it is \c Horizontal the scroll bar is horizontal.

    The \a tqparent and \a name arguments are sent on to the TQWidget
    constructor.
*/

TQScrollBar::TQScrollBar( int minValue, int maxValue, int lineStep, int pageStep,
			int value,  Qt::Orientation orientation,
			TQWidget *tqparent, const char *name )
    : TQWidget( tqparent, name ),
      TQRangeControl( minValue, maxValue, lineStep, pageStep, value ),
      orient( orientation )
{
    init();
}

/*!
    Destructor.
*/
TQScrollBar::~TQScrollBar()
{
}

void TQScrollBar::init()
{
    track = TRUE;
    sliderPos = 0;
    pressedControl = TQStyle::SC_None;
    clickedAt = FALSE;
    setFocusPolicy( Qt::NoFocus );

    repeater = 0;

    setBackgroundMode((TQt::BackgroundMode)
		      tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_BackgroundMode));

    TQSizePolicy sp( TQSizePolicy::Minimum, TQSizePolicy::Fixed );
    if ( orient == Qt::Vertical )
	sp.transpose();
    tqsetSizePolicy( sp );
    clearWState( TQt::WState_OwnSizePolicy );
}


/*!
    \property TQScrollBar::orientation
    \brief the orientation of the scroll bar

    The orientation must be \l Qt::Vertical (the default) or \l
    Qt::Horizontal.
*/

void TQScrollBar::setOrientation( Qt::Orientation orientation )
{
    if ( orientation == orient )
	return;
    if ( !testWState( TQt::WState_OwnSizePolicy ) ) {
	TQSizePolicy sp = tqsizePolicy();
	sp.transpose();
	tqsetSizePolicy( sp );
	clearWState( TQt::WState_OwnSizePolicy );
    }

    orient = orientation;

    positionSliderFromValue();
    update();
    updateGeometry();
}

/*!
    \property TQScrollBar::tracking
    \brief whether scroll bar tracking is enabled

    If tracking is enabled (the default), the scroll bar emits the
    valueChanged() signal while the slider is being dragged. If
    tracking is disabled, the scroll bar emits the valueChanged()
    signal only when the user releases the mouse button after moving
    the slider.
*/


/*!
    \property TQScrollBar::draggingSlider
    \brief whether the user has clicked the mouse on the slider and is currently dragging it
*/

bool TQScrollBar::draggingSlider() const
{
    return pressedControl == TQStyle::SC_ScrollBarSlider;
}


/*!
    Reimplements the virtual function TQWidget::setPalette().

    Sets the background color to the mid color for Motif style scroll
    bars using palette \a p.
*/

void TQScrollBar::setPalette( const TQPalette &p )
{
    TQWidget::setPalette( p );
    setBackgroundMode((TQt::BackgroundMode)
		      tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_BackgroundMode));
}


/*! \reimp */
TQSize TQScrollBar::tqsizeHint() const
{
    constPolish();
    int sbextent = tqstyle().tqpixelMetric(TQStyle::PM_ScrollBarExtent, this);

    if ( orient == Qt::Horizontal ) {
	return TQSize( 30, sbextent );
    } else {
	return TQSize( sbextent, 30 );
    }
}

/*! \fn void TQScrollBar::tqsetSizePolicy( TQSizePolicy::SizeType, TQSizePolicy::SizeType, bool )
    \reimp
*/

/*! \reimp */
void TQScrollBar::tqsetSizePolicy( TQSizePolicy sp )
{
    //## remove 4.0
    TQWidget::tqsetSizePolicy( sp );
}

/*!
  \internal
  Implements the virtual TQRangeControl function.
*/

void TQScrollBar::valueChange()
{
    int tmp = sliderPos;
    positionSliderFromValue();
    if ( tmp != sliderPos && isVisible() )
	tqdrawControls(TQStyle::SC_ScrollBarAddPage |
		     TQStyle::SC_ScrollBarSubPage |
		     TQStyle::SC_ScrollBarSlider,
		     pressedControl );
    emit valueChanged(value());
#if defined(TQT_ACCESSIBILITY_SUPPORT)
    TQAccessible::updateAccessibility( this, 0, TQAccessible::ValueChanged );
#endif
}

/*!
  \internal
  Implements the virtual TQRangeControl function.
*/

void TQScrollBar::stepChange()
{
    rangeChange();
}

/*!
  \internal
  Implements the virtual TQRangeControl function.
*/

void TQScrollBar::rangeChange()
{
    positionSliderFromValue();

    if ( isVisible() )
	tqdrawControls(TQStyle::SC_ScrollBarAddLine |
		     TQStyle::SC_ScrollBarSubLine |
		     TQStyle::SC_ScrollBarAddPage |
		     TQStyle::SC_ScrollBarSubPage |
		     TQStyle::SC_ScrollBarFirst   |
		     TQStyle::SC_ScrollBarLast    |
		     TQStyle::SC_ScrollBarSlider,
		     pressedControl );
}


/*!
    Handles timer events for the scroll bar.
*/

void TQScrollBar::doAutoRepeat()
{
    bool sendRepeat = clickedAt;
#if !defined( TQT_NO_CURSOR ) && !defined( TQT_NO_STYLE )
    if(sendRepeat && (pressedControl == TQStyle::SC_ScrollBarAddPage ||
                      pressedControl == TQStyle::SC_ScrollBarSubPage) &&
       tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_StopMouseOverSlider, this) &&
       tqstyle().querySubControl(TQStyle::CC_ScrollBar, this,
                               mapFromGlobal(TQCursor::pos()) ) == TQStyle::SC_ScrollBarSlider)
        sendRepeat = FALSE;
#endif
    if ( sendRepeat ){
        if ( repeater )
            repeater->changeInterval( repeatTime );
        action( (TQStyle::SubControl) pressedControl );
        TQApplication::syncX();
    } else {
        stopAutoRepeat();
    }
}


/*!
    Starts the auto-repeat logic. Some time after this function is
    called, the auto-repeat starts taking effect and from then on
    repeats until stopAutoRepeat() is called.
*/

void TQScrollBar::startAutoRepeat()
{
    if ( !repeater ) {
	repeater = new TQTimer( this, "auto-repeat timer" );
	connect( repeater, TQT_SIGNAL(timeout()),
		 this, TQT_SLOT(doAutoRepeat()) );
    }
    repeater->start( thresholdTime, FALSE );
}


/*!
    Stops the auto-repeat logic.
*/

void TQScrollBar::stopAutoRepeat()
{
    delete repeater;
    repeater = 0;
}


/*!
    \reimp
*/
#ifndef TQT_NO_WHEELEVENT
void TQScrollBar::wheelEvent( TQWheelEvent *e )
{
    static float offset = 0;
    static TQScrollBar* offset_owner = 0;
    if (offset_owner != this){
	offset_owner = this;
	offset = 0;
    }
    if ( e->orientation() != orient && !TQT_TQRECT_OBJECT(rect()).tqcontains(e->pos()) )
	return;
    e->accept();
    int step = TQMIN( TQApplication::wheelScrollLines()*lineStep(),
		     pageStep() );
    if ( ( e->state() & ControlButton ) || ( e->state() & ShiftButton ) )
	step = pageStep();
    offset += -e->delta()*step/120;
    if (TQABS(offset)<1)
	return;
    setValue( value() + int(offset) );
    offset -= int(offset);
}
#endif

/*!
    \reimp
*/
void TQScrollBar::keyPressEvent( TQKeyEvent *e )
{
    //   \list
    //     \i Left/Right move a horizontal scrollbar by one line.
    //     \i Up/Down move a vertical scrollbar by one line.
    //     \i PageUp moves up one page.
    //     \i PageDown moves down one page.
    //     \i Home moves to the start (minValue()).
    //     \i End moves to the end (maxValue()).
    //   \endlist

    //   Note that unless you call setFocusPolicy(), the default NoFocus
    //   will apply and the user will not be able to use the keyboard to
    //   interact with the scrollbar.
    switch ( e->key() ) {
    case Qt::Key_Left:
	if ( orient == Qt::Horizontal )
	    subtractLine();
	break;
    case Qt::Key_Right:
	if ( orient == Qt::Horizontal )
	    addLine();
	break;
    case Qt::Key_Up:
	if ( orient == Qt::Vertical )
	    subtractLine();
	break;
    case Qt::Key_Down:
	if ( orient == Qt::Vertical )
	    addLine();
	break;
    case Qt::Key_PageUp:
	subtractPage();
	break;
    case Qt::Key_PageDown:
	addPage();
	break;
    case Qt::Key_Home:
	setValue( minValue() );
	break;
    case Qt::Key_End:
	setValue( maxValue() );
	break;
    default:
	e->ignore();
	break;
    }
}


/*!
    \reimp
*/
void TQScrollBar::resizeEvent( TQResizeEvent * )
{
    positionSliderFromValue();
}


/*!
    \reimp
*/
void TQScrollBar::paintEvent( TQPaintEvent * )
{
    TQPainter p( this );
    tqdrawControls(TQStyle::SC_ScrollBarAddLine |
		 TQStyle::SC_ScrollBarSubLine |
		 TQStyle::SC_ScrollBarAddPage |
		 TQStyle::SC_ScrollBarSubPage |
		 TQStyle::SC_ScrollBarFirst   |
		 TQStyle::SC_ScrollBarLast    |
		 TQStyle::SC_ScrollBarSlider,
		 pressedControl, &p );
}

static TQCOORD sliderStartPos = 0;

/*!
    \reimp
 */
void TQScrollBar::contextMenuEvent( TQContextMenuEvent *e )
{
    if(clickedAt)
	e->consume();
    else
	e->ignore();
}

/*!
    \reimp
*/
void TQScrollBar::mousePressEvent( TQMouseEvent *e )
{
    bool midButtonAbsPos =
	tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_MiddleClickAbsolutePosition,
			  this);

    if ( !(e->button() == Qt::LeftButton ||
	   (midButtonAbsPos && e->button() == Qt::MidButton) ) )
	return;

    if ( maxValue() == minValue() ) // nothing to be done
	return;

    if ( e->state() & Qt::MouseButtonMask ) // another button was already pressed
	return;

    clickedAt	   = TRUE;
    pressedControl = tqstyle().querySubControl(TQStyle::CC_ScrollBar, this, e->pos() );

    if ( (pressedControl == TQStyle::SC_ScrollBarAddPage ||
	  pressedControl == TQStyle::SC_ScrollBarSubPage ||
	  pressedControl == TQStyle::SC_ScrollBarSlider ) &&
	 ((midButtonAbsPos && e->button() == Qt::MidButton) ||
	  (tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_LeftClickAbsolutePosition) && e->button() == Qt::LeftButton))) {

	TQRect sr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
						  TQStyle::SC_ScrollBarSlider ),
	      gr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
						  TQStyle::SC_ScrollBarGroove );
	int sliderMin, sliderMax, sliderLength;
	sliderMin = sliderMax = sliderLength = 0;
	if (HORIZONTAL) {
	    sliderMin = gr.x();
	    sliderMax = sliderMin + gr.width();
	    sliderLength = sr.width();
	} else {
	    sliderMin = gr.y();
	    sliderMax = sliderMin + gr.height();
	    sliderLength = sr.height();
	}

	int newSliderPos = (HORIZONTAL ? e->pos().x() : e->pos().y())
			   - sliderLength/2;
	newSliderPos = TQMIN( newSliderPos, sliderMax - sliderLength );
	newSliderPos = TQMAX( newSliderPos, sliderMin );
	setValue( sliderPosToRangeValue(newSliderPos) );
	sliderPos = newSliderPos;
	pressedControl = TQStyle::SC_ScrollBarSlider;
    }

    if ( pressedControl == TQStyle::SC_ScrollBarSlider ) {
	clickOffset = (TQCOORD)( (HORIZONTAL ? e->pos().x() : e->pos().y())
				- sliderPos );
	slidePrevVal   = value();
	sliderStartPos = sliderPos;
	tqdrawControls( pressedControl, pressedControl );
	emit sliderPressed();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( this, 0, TQAccessible::ScrollingStart );
#endif
    } else if ( pressedControl != TQStyle::SC_None ) {
	tqdrawControls( pressedControl, pressedControl );
	action( (TQStyle::SubControl) pressedControl );
	startAutoRepeat();
    }
}


/*!
    \reimp
*/
void TQScrollBar::mouseReleaseEvent( TQMouseEvent *e )
{
    if ( !clickedAt )
	return;

    if ( e->stateAfter() & Qt::MouseButtonMask ) // some other button is still pressed
	return;

    TQStyle::SubControl tmp = (TQStyle::SubControl) pressedControl;
    clickedAt = FALSE;
    stopAutoRepeat();
    mouseMoveEvent( e );  // Might have moved since last mouse move event.
    pressedControl = TQStyle::SC_None;

    if (tmp == TQStyle::SC_ScrollBarSlider) {
	directSetValue( calculateValueFromSlider() );
	emit sliderReleased();
	if ( value() != prevValue() ) {
	    emit valueChanged( value() );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	    TQAccessible::updateAccessibility( this, 0, TQAccessible::ValueChanged );
#endif
	}
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( this, 0, TQAccessible::ScrollingEnd );
#endif
    }
    tqdrawControls( tmp, pressedControl );
    if ( e->button() == Qt::MidButton )
	tqrepaint( FALSE );
}


/*!
    \reimp
*/
void TQScrollBar::mouseMoveEvent( TQMouseEvent *e )
{
    if ( !isVisible() ) {
	clickedAt = FALSE;
	return;
    }

    bool mcab = tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_MiddleClickAbsolutePosition,
				  this);
    if ( ! clickedAt || ! (e->state() & Qt::LeftButton ||
			   ((e->state() & Qt::MidButton) && mcab)))
	return;

    int newSliderPos;
    if ( pressedControl == TQStyle::SC_ScrollBarSlider ) {
	TQRect gr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
						  TQStyle::SC_ScrollBarGroove ),
	      sr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
						  TQStyle::SC_ScrollBarSlider );
	int sliderMin, sliderMax, sliderLength;

	if (HORIZONTAL) {
	    sliderLength = sr.width();
	    sliderMin = gr.x();
	    sliderMax = gr.right() - sliderLength + 1;
	} else {
	    sliderLength = sr.height();
	    sliderMin = gr.y();
	    sliderMax = gr.bottom() - sliderLength + 1;
	}

	TQRect r = rect();
	int m = tqstyle().tqpixelMetric(TQStyle::PM_MaximumDragDistance, this);
	if ( m >= 0 ) {
	    if ( orientation() == Qt::Horizontal )
		r.setRect( r.x() - m, r.y() - 2*m, r.width() + 2*m, r.height() + 4*m );
	    else
		r.setRect( r.x() - 2*m, r.y() - m, r.width() + 4*m, r.height() + 2*m );
	    if (! r.tqcontains( e->pos()))
		newSliderPos = sliderStartPos;
	    else
		newSliderPos = (HORIZONTAL ? e->pos().x() :
				e->pos().y()) -clickOffset;
	} else
	    newSliderPos = (HORIZONTAL ? e->pos().x() :
			    e->pos().y()) -clickOffset;

	if ( newSliderPos < sliderMin )
	    newSliderPos = sliderMin;
	else if ( newSliderPos > sliderMax )
	    newSliderPos = sliderMax;
	int newVal = sliderPosToRangeValue(newSliderPos);
	if ( newVal != slidePrevVal )
	    emit sliderMoved( newVal );
	if ( track && newVal != value() ) {
	    directSetValue( newVal ); // Set directly, painting done below
	    emit valueChanged( value() );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	    TQAccessible::updateAccessibility( this, 0, TQAccessible::ValueChanged );
#endif
	}
	slidePrevVal = newVal;
	sliderPos = (TQCOORD)newSliderPos;
	tqdrawControls( TQStyle::SC_ScrollBarAddPage |
		      TQStyle::SC_ScrollBarSlider  |
		      TQStyle::SC_ScrollBarSubPage,
		      pressedControl );
    } else if (! tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_ScrollWhenPointerLeavesControl)) {
	// stop scrolling when the mouse pointer leaves a control
	// similar to push buttons
	if ( pressedControl != (uint)tqstyle().querySubControl(TQStyle::CC_ScrollBar, this, e->pos() ) ) {
	    tqdrawControls( pressedControl, TQStyle::SC_None );
	    stopAutoRepeat();
	} else if ( !repeater ) {
	    tqdrawControls( pressedControl, pressedControl );
	    action( (TQStyle::SubControl) pressedControl );
	    startAutoRepeat();
	}
    }
}


/*!
    \fn int TQScrollBar::sliderStart() const

    Returns the pixel position where the scroll bar slider starts.

    This is equivalent to sliderRect().y() for vertical scroll bars or
    sliderRect().x() for horizontal scroll bars.
*/

/*!
    Returns the scroll bar slider rectangle.

    \sa sliderStart()
*/

TQRect TQScrollBar::sliderRect() const
{
    return tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
					  TQStyle::SC_ScrollBarSlider );
}

void TQScrollBar::positionSliderFromValue()
{
    sliderPos = (TQCOORD)rangeValueToSliderPos( value() );
}

int TQScrollBar::calculateValueFromSlider() const
{
    return sliderPosToRangeValue( sliderPos );
}

int TQScrollBar::rangeValueToSliderPos( int v ) const
{
    TQRect gr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
					      TQStyle::SC_ScrollBarGroove );
    TQRect sr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
					      TQStyle::SC_ScrollBarSlider );
    int sliderMin, sliderMax, sliderLength;

    if (HORIZONTAL) {
	sliderLength = sr.width();
	sliderMin = gr.x();
	sliderMax = gr.right() - sliderLength + 1;
    } else {
	sliderLength = sr.height();
	sliderMin = gr.y();
	sliderMax = gr.bottom() - sliderLength + 1;
    }

    return positionFromValue( v, sliderMax-sliderMin ) + sliderMin;
}

int TQScrollBar::sliderPosToRangeValue( int pos ) const
{
    TQRect gr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
					      TQStyle::SC_ScrollBarGroove );
    TQRect sr = tqstyle().querySubControlMetrics(TQStyle::CC_ScrollBar, this,
					      TQStyle::SC_ScrollBarSlider );
    int sliderMin, sliderMax, sliderLength;

    if (HORIZONTAL) {
	sliderLength = sr.width();
	sliderMin = gr.x();
	sliderMax = gr.right() - sliderLength + 1;
    } else {
	sliderLength = sr.height();
	sliderMin = gr.y();
	sliderMax = gr.bottom() - sliderLength + 1;
    }

    return  valueFromPosition( pos - sliderMin, sliderMax - sliderMin );
}


void TQScrollBar::action( int control )
{
    switch( control ) {
    case TQStyle::SC_ScrollBarAddLine:
	addLine();
	emit nextLine();
	break;
    case TQStyle::SC_ScrollBarSubLine:
	subtractLine();
	emit prevLine();
	break;
    case TQStyle::SC_ScrollBarAddPage:
	addPage();
	emit nextPage();
	break;
    case TQStyle::SC_ScrollBarSubPage:
	subtractPage();
	emit prevPage();
	break;
    case TQStyle::SC_ScrollBarFirst:
	setValue( minValue() );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( this, 0, TQAccessible::ValueChanged );
#endif
	emit valueChanged( minValue() );
	break;
    case TQStyle::SC_ScrollBarLast:
	setValue( maxValue() );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( this, 0, TQAccessible::ValueChanged );
#endif
	emit valueChanged( maxValue() );
	break;
    default:
	break;
    }
}


void TQScrollBar::tqdrawControls( uint controls, uint activeControl ) const
{
    TQPainter p ( this );
    tqdrawControls( controls, activeControl, &p );
}


void TQScrollBar::tqdrawControls( uint controls, uint activeControl,
			       TQPainter *p ) const
{
    if ( !isUpdatesEnabled() )
	return;

    TQStyle::SFlags flags = TQStyle::Style_Default;
    if (isEnabled())
	flags |= TQStyle::Style_Enabled;
    if (hasFocus())
	flags |= TQStyle::Style_HasFocus;
    if ( orientation() == Qt::Horizontal )
	flags |= TQStyle::Style_Horizontal;

    tqstyle().tqdrawComplexControl(TQStyle::CC_ScrollBar, p, this, rect(), tqcolorGroup(),
			       flags, (TQStyle::SubControl) controls,
			       (TQStyle::SubControl) activeControl );
}

/*!
    \reimp
*/
void TQScrollBar::styleChange( TQStyle& old )
{
    positionSliderFromValue();
    setBackgroundMode((TQt::BackgroundMode)
		      tqstyle().tqstyleHint(TQStyle::SH_ScrollBar_BackgroundMode));
    TQWidget::styleChange( old );
}

/*!
    \property TQScrollBar::minValue
    \brief the scroll bar's minimum value

    When setting this property, the \l TQScrollBar::maxValue is
    adjusted if necessary to ensure that the range remains valid.

    \sa setRange()
*/
int TQScrollBar::minValue() const
{
    return TQRangeControl::minValue();
}

void TQScrollBar::setMinValue( int minVal )
{
    TQRangeControl::setMinValue( minVal );
}

/*!
    \property TQScrollBar::maxValue
    \brief the scroll bar's maximum value

    When setting this property, the \l TQScrollBar::minValue is
    adjusted if necessary to ensure that the range remains valid.

    \sa setRange()
*/
int TQScrollBar::maxValue() const
{
    return TQRangeControl::maxValue();
}

void TQScrollBar::setMaxValue( int maxVal )
{
    TQRangeControl::setMaxValue( maxVal );
}

/*!
    \property TQScrollBar::lineStep
    \brief the line step

    When setting lineStep, the virtual stepChange() function will be
    called if the new line step is different from the previous
    setting.

    \sa setSteps() TQRangeControl::pageStep() setRange()
*/

int TQScrollBar::lineStep() const
{
    return TQRangeControl::lineStep();
}

/*!
    \property TQScrollBar::pageStep
    \brief the page step

    When setting pageStep, the virtual stepChange() function will be
    called if the new page step is different from the previous
    setting.

    \sa TQRangeControl::setSteps() setLineStep() setRange()
*/

int TQScrollBar::pageStep() const
{
    return TQRangeControl::pageStep();
}

void TQScrollBar::setLineStep( int i )
{
    setSteps( i, pageStep() );
}

void TQScrollBar::setPageStep( int i )
{
    setSteps( lineStep(), i );
}

/*!
    \property TQScrollBar::value
    \brief the scroll bar's value

    \sa TQRangeControl::value() prevValue()
*/

int TQScrollBar::value() const
{
    return TQRangeControl::value();
}

void TQScrollBar::setValue( int i )
{
    TQRangeControl::setValue( i );
}


/*!
    This function is called when the scrollbar is hidden.
*/
void TQScrollBar::hideEvent( TQHideEvent* )
{
    pressedControl = TQStyle::SC_None;
    clickedAt = FALSE;
}


#undef ADD_LINE_ACTIVE
#undef SUB_LINE_ACTIVE
#endif