summaryrefslogtreecommitdiffstats
path: root/krecipes/src/widgets/kreruler.cpp
blob: 01b005d6912b9d9cf092b76ab5c5dc9b8e2b5746 (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
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2005 Jason Kivlighn <jkivlighn@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

// Description: Ruler (header)

/******************************************************************/

#include "kreruler.h"

#include <tdelocale.h>
#include <tdeglobalsettings.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <ntqcursor.h>
#include <ntqpainter.h>
#include <ntqpopupmenu.h>
#include <ntqtooltip.h>

#include "krepagelayout.h"

class KoRulerPrivate {
public:
    KoRulerPrivate() {
    }
    ~KoRulerPrivate() {}

    TQWidget *canvas;
    int flags;
    int oldMx, oldMy;
    bool whileMovingBorderLeft, whileMovingBorderRight;
    bool whileMovingBorderTop, whileMovingBorderBottom;
    TQPixmap pmFirst, pmLeft;
    KoPageLayout layout;
    KoTabulatorList tabList;
    // Do we have to remove a certain tab in the DC Event?
    KoTabulator removeTab;
    // The tab we're moving / clicking on - basically only valid between press and release time
    KoTabulator currTab;
    // The action we're currently doing - basically only valid between press and release time
    KoRuler::Action action;
    TQPopupMenu *rb_menu;
    int mRemoveTab, mPageLayout; // menu item ids
    int frameEnd;
    double i_right;
    bool m_bReadWrite;
    bool doubleClickedIndent;
    bool rtl;
    bool mousePressed;
};

// Equality test for tab positions in particular
static inline bool equals( double a, double b )  {
    return kAbs( a - b ) < 1E-4;
}


/******************************************************************/
/* Class: KoRuler                                                 */
/******************************************************************/

const int KoRuler::F_TABS = 1;
const int KoRuler::F_INDENTS = 2;
const int KoRuler::F_HELPLINES = 4;
const int KoRuler::F_NORESIZE = 8;

/*================================================================*/
KoRuler::KoRuler( TQWidget *_parent, TQWidget *_canvas, Orientation _orientation,
                 const KoPageLayout& _layout, int _flags, KoUnit::Unit _unit )
    : TQFrame( _parent ), buffer( width(), height() ), m_zoom(1.0), m_1_zoom(1.0),
      m_unit( _unit )
{
    setWFlags( WResizeNoErase | WRepaintNoErase );
    setFrameStyle( MenuBarPanel );

    d=new KoRulerPrivate();

    d->canvas = _canvas;
    orientation = _orientation;
    d->layout = _layout;
    d->flags = _flags;

    d->m_bReadWrite=true;
    d->doubleClickedIndent=false;
    diffx = 0;
    diffy = 0;
    i_left=0.0;
    i_first=0.0;
    d->i_right=0.0;

    setMouseTracking( true );
    d->mousePressed = false;
    d->action = A_NONE;

    d->oldMx = 0;
    d->oldMy = 0;
    d->rtl = false;

    showMPos = false;
    mposX = 0;
    mposY = 0;
    gridSize=0.0;
    hasToDelete = false;
    d->whileMovingBorderLeft = d->whileMovingBorderRight = d->whileMovingBorderTop = d->whileMovingBorderBottom = false;

    d->pmFirst = UserIcon( "koRulerFirst" );
    d->pmLeft = UserIcon( "koRulerLeft" );
    d->currTab.type = T_INVALID;

    d->removeTab.type = T_INVALID;
    if ( orientation == TQt::Horizontal ) {
        frameStart = tqRound( zoomIt(d->layout.ptLeft) );
        d->frameEnd = tqRound( zoomIt(d->layout.ptWidth - d->layout.ptRight) );
    } else {
        frameStart = tqRound( zoomIt(d->layout.ptTop) );
        d->frameEnd = tqRound( zoomIt(d->layout.ptHeight - d->layout.ptBottom) );
    }
    m_bFrameStartSet = false;

    setupMenu();

    // For compatibility, emitting doubleClicked shall emit openPageLayoutDia
    connect( this, TQ_SIGNAL( doubleClicked() ), this, TQ_SIGNAL( openPageLayoutDia() ) );
}

/*================================================================*/
KoRuler::~KoRuler()
{
    delete d->rb_menu;
    delete d;
}

void KoRuler::setPageLayoutMenuItemEnabled(bool b)
{
    d->rb_menu->setItemEnabled(d->mPageLayout, b);
}

/*================================================================*/
void KoRuler::setMousePos( int mx, int my )
{
    if ( !showMPos || ( mx == mposX && my == mposY ) ) return;

    TQPainter p( this );
    p.setRasterOp( TQt::NotROP );

    if ( orientation == TQt::Horizontal ) {
        if ( hasToDelete )
            p.drawLine( mposX, 1, mposX, height() - 1 );
        p.drawLine( mx, 1, mx, height() - 1 );
        hasToDelete = true;
    }
    else {
        if ( hasToDelete )
            p.drawLine( 1, mposY, width() - 1, mposY );
        p.drawLine( 1, my, width() - 1, my );
        hasToDelete = true;
    }
    p.end();

    mposX = mx;
    mposY = my;
}

// distance between the main lines (those with a number)
double KoRuler::lineDistance() const
{
    switch( m_unit ) {
    case KoUnit::U_INCH:
        return INCH_TO_POINT( m_zoom ); // every inch
    case KoUnit::U_PT:
        return 100.0 * m_zoom; // every 100 pt
    case KoUnit::U_MM:
    case KoUnit::U_CM:
    case KoUnit::U_DM:
        return CM_TO_POINT ( m_zoom ); // every cm
    case KoUnit::U_PI:
        return PI_TO_POINT ( 10.0 * m_zoom ); // every 10 pica
    case KoUnit::U_DD:
        return DD_TO_POINT( m_zoom ); // every diderot
    case KoUnit::U_CC:
        return CC_TO_POINT( 10.0 * m_zoom ); // every 10 cicero
    }
    // should never end up here
    return 100.0 * m_zoom;
}

/*================================================================*/
void KoRuler::drawHorizontal( TQPainter *_painter )
{
    TQFont font = TDEGlobalSettings::toolBarFont();
    TQFontMetrics fm( font );
    resize( width(), TQMAX( fm.height() + 4, 20 ) );

    // Use a double-buffer pixmap
    TQPainter p( &buffer );
    p.fillRect( 0, 0, width(), height(), TQBrush( colorGroup().brush( TQColorGroup::Background ) ) );

    int totalw = tqRound( zoomIt(d->layout.ptWidth) );
    TQString str;

    p.setBrush( colorGroup().brush( TQColorGroup::Base ) );

    // Draw white rect
    TQRect r;
    if ( !d->whileMovingBorderLeft )
        r.setLeft( -diffx + frameStart );
    else
        r.setLeft( d->oldMx );
    r.setTop( 0 );
    if ( !d->whileMovingBorderRight )
        r.setWidth(d->frameEnd-frameStart);
    else
        r.setRight( d->oldMx );
    r.setBottom( height() );

    p.drawRect( r );
    p.setFont( font );

    // Draw the numbers
    double dist = lineDistance();
    int maxwidth = 0;

    for ( double i = 0.0;i <= (double)totalw;i += dist ) {
        str = TQString::number( KoUnit::toUserValue( i / m_zoom, m_unit ) );
        int textwidth = fm.width( str );
        maxwidth = TQMAX( maxwidth, textwidth );
    }

    // Make sure that the ruler stays readable at lower zoom levels
    while( dist <= maxwidth ) {
        dist += lineDistance();
    }

    for ( double i = 0.0;i <= (double)totalw;i += dist ) {
        str = TQString::number( KoUnit::toUserValue( i / m_zoom, m_unit ) );
        int textwidth = fm.width( str );
        maxwidth = TQMAX( maxwidth, textwidth );
        p.drawText( tqRound(i) - diffx - tqRound(textwidth * 0.5),
                    tqRound(( height() - fm.height() ) * 0.5),
                    textwidth, height(), AlignLeft | AlignTop, str );
    }

    // Draw the medium-sized lines
    // Only if we have enough space (i.e. not at 33%)
    if ( dist > maxwidth + 2 )
    {
        for ( double i = dist * 0.5;i <= (double)totalw;i += dist ) {
            int ii=tqRound(i);
            p.drawLine( ii - diffx, 7, ii - diffx, height() - 7 );
        }
    }

    // Draw the small lines
    // Only if we have enough space (i.e. not at 33%)
    if ( dist * 0.5 > maxwidth + 2 )
    {
        for ( double i = dist * 0.25;i <= (double)totalw;i += dist * 0.5 ) {
            int ii=tqRound(i);
            p.drawLine( ii - diffx, 9, ii - diffx, height() - 9 );
        }
    }

    // Draw ending bar (at page width)
    //int constant=zoomIt(1);
    //p.drawLine( totalw - diffx + constant, 1, totalw - diffx + constant, height() - 1 );
    //p.setPen( colorGroup().color( TQColorGroup::Base ) );
    //p.drawLine( totalw - diffx, 1, totalw - diffx, height() - 1 );

    // Draw starting bar (at 0)
    //p.setPen( colorGroup().color( TQColorGroup::Text ) );
    //p.drawLine( -diffx, 1, -diffx, height() - 1 );
    //p.setPen( colorGroup().color( TQColorGroup::Base ) );
    //p.drawLine( -diffx - constant, 1, -diffx - constant, height() - 1 );

    // Show the mouse position
    if ( d->action == A_NONE && showMPos ) {
        p.setPen( colorGroup().color( TQColorGroup::Text ) );
        p.drawLine( mposX, 1, mposX, height() - 1 );
    }
    hasToDelete = false;

    p.end();
    _painter->drawPixmap( 0, 0, buffer );
}


/*================================================================*/
void KoRuler::drawVertical( TQPainter *_painter )
{
    TQFont font = TDEGlobalSettings::toolBarFont();
    TQFontMetrics fm( font );
    resize( TQMAX( fm.height() + 4, 20 ), height() );

    TQPainter p( &buffer );
    p.fillRect( 0, 0, width(), height(), TQBrush( colorGroup().brush( TQColorGroup::Background ) ) );

    int totalh = tqRound( zoomIt(d->layout.ptHeight) );
    // Clip rect - this gives basically always a rect like (2,2,width-2,height-2)
    TQRect paintRect = _painter->clipRegion( TQPainter::CoordPainter ).boundingRect();
    // Ruler rect
    TQRect rulerRect( 0, -diffy, width(), totalh );

    if ( paintRect.intersects( rulerRect ) )  {
        TQString str;

        p.setBrush( colorGroup().brush( TQColorGroup::Base ) );

        // Draw white rect
        TQRect r;
        if ( !d->whileMovingBorderTop )
            r.setTop( -diffy + frameStart );
        else
            r.setTop( d->oldMy );
        r.setLeft( 0 );
        if ( !d->whileMovingBorderBottom )
            r.setHeight(d->frameEnd-frameStart);
        else
            r.setBottom( d->oldMy );
        r.setRight( width() );

        p.drawRect( r );
        p.setFont( font );

        // Draw the numbers
        double dist = lineDistance();
        int maxheight = 0;

        for ( double i = 0.0;i <= (double)totalh;i += dist ) {
            str = TQString::number( KoUnit::toUserValue( i / m_zoom, m_unit ) );
            int textwidth = fm.width( str );
            maxheight = TQMAX( maxheight, textwidth );
        }

        // Make sure that the ruler stays readable at lower zoom levels
        while( dist <= maxheight ) {
            dist += lineDistance();
        }

        for ( double i = 0.0;i <= (double)totalh;i += dist ) {
            str = TQString::number( KoUnit::toUserValue( i / m_zoom, m_unit ) );
            int textheight = fm.height();
            int textwidth = fm.width( str );
            maxheight = TQMAX( maxheight, textwidth );
            p.save();
            p.translate( tqRound(( width() - textheight ) * 0.5),
                         tqRound(i) - diffy + tqRound(textwidth * 0.5) );
            p.rotate( -90 );
            p.drawText( 0, 0, textwidth + 1, textheight, AlignLeft | AlignTop, str );
            p.restore();
        }

        // Draw the medium-sized lines
        if ( dist > maxheight + 2 )
        {
            for ( double i = dist * 0.5;i <= (double)totalh;i += dist ) {
                int ii=tqRound(i);
                p.drawLine( 7, ii - diffy, width() - 7, ii - diffy );
            }
        }

        // Draw the small lines
        if ( dist * 0.5 > maxheight + 2 )
        {
            for ( double i = dist * 0.25;i <=(double)totalh;i += dist *0.5 ) {
                int ii=tqRound(i);
                p.drawLine( 9, ii - diffy, width() - 9, ii - diffy );
            }
        }

        // Draw ending bar (at page height)
        //p.drawLine( 1, totalh - diffy + 1, width() - 1, totalh - diffy + 1 );
        //p.setPen( colorGroup().color( TQColorGroup::Base ) );
        //p.drawLine( 1, totalh - diffy, width() - 1, totalh - diffy );

        // Draw starting bar (at 0)
        //p.setPen( colorGroup().color( TQColorGroup::Text ) );
        //p.drawLine( 1, -diffy, width() - 1, -diffy );
        //p.setPen( colorGroup().color( TQColorGroup::Base ) );
        //p.drawLine( 1, -diffy - 1, width() - 1, -diffy - 1 );
    }

    // Show the mouse position
    if ( d->action == A_NONE && showMPos ) {
        p.setPen( colorGroup().color( TQColorGroup::Text ) );
        p.drawLine( 1, mposY, width() - 1, mposY );
    }
    hasToDelete = false;

    p.end();
    _painter->drawPixmap( 0, 0, buffer );
}

void KoRuler::mousePressEvent( TQMouseEvent *e )
{
    if( !d->m_bReadWrite)
        return;

    d->oldMx = e->x();
    d->oldMy = e->y();
    d->mousePressed = true;
    d->removeTab.type = T_INVALID;

    switch ( e->button() ) {
    case RightButton:
        if(d->currTab.type == T_INVALID || !(d->flags & F_TABS))
            d->rb_menu->setItemEnabled(d->mRemoveTab, false);
        else
            d->rb_menu->setItemEnabled(d->mRemoveTab, true);
        d->rb_menu->popup( TQCursor::pos() );
        d->action = A_NONE;
        d->mousePressed = false;
        return;
    case MidButton:
        // MMB shall do like double-click (it opens a dialog).
        handleDoubleClick();
        return;
    case LeftButton:
        if ( d->action == A_BR_RIGHT || d->action == A_BR_LEFT ) {
            if ( d->action == A_BR_RIGHT )
                d->whileMovingBorderRight = true;
            else
                d->whileMovingBorderLeft = true;

            if ( d->canvas )
                drawLine(d->oldMx, -1);
            update();
        } else if ( d->action == A_BR_TOP || d->action == A_BR_BOTTOM ) {
            if ( d->action == A_BR_TOP )
                d->whileMovingBorderTop = true;
            else
                d->whileMovingBorderBottom = true;

            if ( d->canvas ) {
                TQPainter p( d->canvas );
                p.setRasterOp( TQt::NotROP );
                p.drawLine( 0, d->oldMy, d->canvas->width(), d->oldMy );
                p.end();
            }
            update();
        } else if ( d->action == A_FIRST_INDENT || d->action == A_LEFT_INDENT || d->action == A_RIGHT_INDENT ) {
            if ( d->canvas )
                drawLine(d->oldMx, -1);
        } else if ( d->action == A_TAB ) {
            if ( d->canvas && d->currTab.type != T_INVALID ) {
                drawLine( tqRound( applyRtlAndZoom(d->currTab.ptPos) ) + frameStart - diffx, -1 );
            }
        }
        else if ( d->flags & F_HELPLINES )
        {
	    setCursor( orientation == TQt::Horizontal ?
		       TQt::sizeVerCursor : TQt::sizeHorCursor );
            d->action = A_HELPLINES;
        }
    default:
        break;
    }
}

void KoRuler::mouseReleaseEvent( TQMouseEvent *e )
{
    d->mousePressed = false;

    // Hacky, but necessary to prevent multiple tabs with the same coordinates (Werner)
    bool fakeMovement=false;
    if(d->removeTab.type != T_INVALID) {
        mouseMoveEvent(e);
        fakeMovement=true;
    }

    if ( d->action == A_BR_RIGHT || d->action == A_BR_LEFT ) {
        d->whileMovingBorderRight = false;
        d->whileMovingBorderLeft = false;

        if ( d->canvas )
            drawLine(d->oldMx, -1);
        update();
        emit newPageLayout( d->layout );
    } else if ( d->action == A_BR_TOP || d->action == A_BR_BOTTOM ) {
        d->whileMovingBorderTop = false;
        d->whileMovingBorderBottom = false;

        if ( d->canvas ) {
            TQPainter p( d->canvas );
            p.setRasterOp( TQt::NotROP );
            p.drawLine( 0, d->oldMy, d->canvas->width(), d->oldMy );
            p.end();
        }
        update();
        emit newPageLayout( d->layout );
    } else if ( d->action == A_FIRST_INDENT ) {
        if ( d->canvas )
            drawLine(d->oldMx, -1);
        update();
        emit newFirstIndent( i_first );
    } else if ( d->action == A_LEFT_INDENT ) {
        if ( d->canvas )
            drawLine(d->oldMx, -1);
        update();
        emit newLeftIndent( i_left );
    } else if ( d->action == A_RIGHT_INDENT ) {
        if ( d->canvas )
            drawLine(d->oldMx, -1);
        update();
        emit newRightIndent( d->i_right );
    } else if ( d->action == A_TAB ) {
        if ( d->canvas && !fakeMovement ) {
            drawLine( tqRound( applyRtlAndZoom( d->currTab.ptPos ) ) + frameStart - diffx, -1);
        }
        if ( willRemoveTab( e->y() ) )
        {
            d->tabList.remove(d->currTab);
        }
        qHeapSort( d->tabList );

        // Delete the new tabulator if it is placed on top of another.
        KoTabulatorList::ConstIterator tmpTab=d->tabList.begin();
        int count=0;
        while(tmpTab!=d->tabList.end()) {
            if( equals( (*tmpTab).ptPos, d->currTab.ptPos ) ) {
                count++;
                if(count > 1) {
                    d->tabList.remove(d->currTab);
                    break;
                }
            }
            tmpTab++;
        }
        //searchTab( e->x() ); // DF: why set currTab here?
        emit tabListChanged( d->tabList );
        update();
    }
    else if( d->action == A_HELPLINES )
    {
        emit addHelpline( e->pos(), orientation == TQt::Horizontal);
        setCursor( ArrowCursor );
    }
    d->currTab.type = T_INVALID; // added (DF)
}

void KoRuler::mouseMoveEvent( TQMouseEvent *e )
{
    hasToDelete = false;

    int pw = d->frameEnd - frameStart;
    int ph = tqRound(zoomIt(d->layout.ptHeight));
    int left = frameStart - diffx;
    int top = tqRound(zoomIt(d->layout.ptTop));
    top -= diffy;
    int right = d->frameEnd - diffx;
    int bottom = tqRound(zoomIt(d->layout.ptBottom));
    bottom = ph - bottom - diffy;
    // Cumulate first-line-indent
    int ip_first = tqRound( zoomIt( i_first + ( d->rtl ? d->i_right : i_left) ) );
    int ip_left = tqRound(zoomIt(i_left));
    int ip_right = tqRound(zoomIt(d->i_right));

    int mx = e->x();
    mx = mx+diffx < 0 ? 0 : mx;
    int my = e->y();
    my = my+diffy < 0 ? 0 : my;

    TQToolTip::remove( this);
    switch ( orientation ) {
        case TQt::Horizontal: {
            if ( !d->mousePressed ) {
                setCursor( ArrowCursor );
                d->action = A_NONE;
                /////// ######
                // At the moment, moving the left and right border indicators
                // is disabled when setFrameStartEnd has been called (i.e. in KWord)
                // Changing the layout margins directly from it would be utterly wrong
                // (just try the 2-columns modes...). What needs to be done is:
                // emitting a signal frameResized in mouseReleaseEvent, when a left/right
                // border has been moved, and in kword we need to update the margins from
                // there, if the left border of the 1st column or the right border of the
                // last column was moved... and find what to do with the other borders.
                // And for normal frames, resize the frame without touching the page layout.
                // All that is too much for now -> disabling.
                if ( !m_bFrameStartSet )
                {
                    if ( mx > left - 5 && mx < left + 5 ) {
                        setCursor( TQt::sizeHorCursor );
                        d->action = A_BR_LEFT;
                    } else if ( mx > right - 5 && mx < right + 5 ) {
                        setCursor( TQt::sizeHorCursor );
                        d->action = A_BR_RIGHT;
                    }
                }
                if ( d->flags & F_TABS )
                    searchTab(mx);
            } else {
                // Calculate the new value.
                int newPos=mx;
                if( newPos!=right && gridSize!=0.0 && (e->state() & ShiftButton)==0) { // apply grid.
                    double grid=zoomIt(gridSize * 16);
                    newPos=tqRound( ((newPos * 16 / grid) * grid) / 16 );
                }
                if(newPos-left < 0) newPos=left;
                else if (right-newPos < 0) newPos=right;
                double newValue = unZoomIt(static_cast<double>(newPos) - frameStart + diffx);

                switch ( d->action ) {
                    case A_BR_LEFT: {
                        if ( d->canvas && mx < right-10 && mx+diffx-2 > 0) {
                            drawLine( d->oldMx, mx );
                            d->layout.ptLeft = unZoomIt(static_cast<double>(mx + diffx));
                            if( ip_left > right-left-15 ) {
                                ip_left=right-left-15;
                                ip_left=ip_left<0 ? 0 : ip_left;
                                i_left=unZoomIt( ip_left );
                                emit newLeftIndent( i_left );
                            }
                            if ( ip_right > right-left-15 ) {
                                ip_right=right-left-15;
                                ip_right=ip_right<0? 0 : ip_right;
                                d->i_right=unZoomIt( ip_right );
                                emit newRightIndent( d->i_right );
                            }
                            d->oldMx = mx;
                            d->oldMy = my;
                            update();
                        }
                        else
                            return;
                    } break;
                    case A_BR_RIGHT: {
                        if ( d->canvas && mx > left+10 && mx+diffx <= pw-2) {
                            drawLine( d->oldMx, mx );
                            d->layout.ptRight = unZoomIt(static_cast<double>(pw - ( mx + diffx )));
                            if( ip_left > right-left-15 ) {
                                ip_left=right-left-15;
                                ip_left=ip_left<0 ? 0 : ip_left;
                                i_left=unZoomIt( ip_left );
                                emit newLeftIndent( i_left );
                            }
                            if ( ip_right > right-left-15 ) {
                                ip_right=right-left-15;
                                ip_right=ip_right<0? 0 : ip_right;
                                d->i_right=unZoomIt( ip_right );
                                emit newRightIndent( d->i_right );
                            }
                            d->oldMx = mx;
                            d->oldMy = my;
                            update();
                        }
                        else
                            return;
                    } break;
                    case A_FIRST_INDENT: {
                        if ( d->canvas ) {
                            if (d->rtl)
                                newValue = unZoomIt(pw) - newValue - d->i_right;
                            else
                                newValue -= i_left;
                            if(newValue == i_first) break;
                            drawLine( d->oldMx, newPos);
                            d->oldMx=newPos;
                            i_first = newValue;
                            update();
                        }
                    } break;
                    case A_LEFT_INDENT: {
                        if ( d->canvas ) {
                            //if (d->rtl) newValue = unZoomIt(pw) - newValue;
                            if(newValue == i_left) break;

                            drawLine( d->oldMx, newPos);
                            i_left = newValue;
                            d->oldMx = newPos;
                            update();
                        }
                    } break;
                    case A_RIGHT_INDENT: {
                        if ( d->canvas ) {
                            double rightValue = unZoomIt(right - newPos);
                            //if (d->rtl) rightValue = unZoomIt(pw) - rightValue;
                            if(rightValue == d->i_right) break;

                            drawLine( d->oldMx, newPos);
                            d->i_right=rightValue;
                            d->oldMx = newPos;
                            update();
                        }
                    } break;
                    case A_TAB: {
                        if ( d->canvas) {
                            if (d->rtl) newValue = unZoomIt(pw) - newValue;
                            if(newValue == d->currTab.ptPos) break; // no change
                            TQPainter p( d->canvas );
                            p.setRasterOp( TQt::NotROP );
                            // prevent 1st drawLine when we just created a new tab
                            // (it's a NOT line)
                            double pt;
                            int pt_fr;
                            if( d->currTab != d->removeTab )
                            {
                                pt = applyRtlAndZoom(d->currTab.ptPos);
                                pt_fr = tqRound(pt) + frameStart - diffx;
                                p.drawLine( pt_fr, 0, pt_fr, d->canvas->height() );
                            }

                            KoTabulatorList::Iterator it = d->tabList.find( d->currTab );
                            Q_ASSERT( it != d->tabList.end() );
                            if ( it != d->tabList.end() )
                                (*it).ptPos = newValue;
                            d->currTab.ptPos = newValue;

                            pt = applyRtlAndZoom( newValue );
                            pt_fr = tqRound(pt) + frameStart - diffx;
                            p.drawLine( pt_fr, 0, pt_fr, d->canvas->height() );

                            p.end();
                            d->oldMx = mx;
                            d->oldMy = my;
                            d->removeTab.type = T_INVALID;
                            update();
                        }
                    } break;
                    default: break;
                }
            }
            if( d->action == A_HELPLINES )
            {
                emit moveHelpLines( e->pos(), orientation == TQt::Horizontal);
            }

            return;
        } break;
        case TQt::Vertical: {
            if ( !d->mousePressed ) {
                setCursor( ArrowCursor );
                d->action = A_NONE;
                if ( d->flags & F_NORESIZE )
                    break;
                if ( my > top - 5 && my < top + 5 ) {
                    TQToolTip::add( this, i18n("Top margin") );
                    setCursor( TQt::sizeVerCursor );
                    d->action = A_BR_TOP;
                } else if ( my > bottom - 5 && my < bottom + 5 ) {
                    TQToolTip::add( this, i18n("Bottom margin") );
                    setCursor( TQt::sizeVerCursor );
                    d->action = A_BR_BOTTOM;
                }
            } else {
                switch ( d->action ) {
                    case A_BR_TOP: {
                        if ( d->canvas && my < bottom-20 && my+diffy-2 > 0) {
                            TQPainter p( d->canvas );
                            p.setRasterOp( TQt::NotROP );
                            p.drawLine( 0, d->oldMy, d->canvas->width(), d->oldMy );
                            p.drawLine( 0, my, d->canvas->width(), my );
                            p.end();
                            d->layout.ptTop = unZoomIt(static_cast<double>(my + diffy));
                            d->oldMx = mx;
                            d->oldMy = my;
                            update();
                        }
                        else
                            return;
                    } break;
                    case A_BR_BOTTOM: {
                        if ( d->canvas && my > top+20 && my+diffy < ph-2) {
                            TQPainter p( d->canvas );
                            p.setRasterOp( TQt::NotROP );
                            p.drawLine( 0, d->oldMy, d->canvas->width(), d->oldMy );
                            p.drawLine( 0, my, d->canvas->width(), my );
                            p.end();
                            d->layout.ptBottom = unZoomIt(static_cast<double>(ph - ( my + diffy )));
                            d->oldMx = mx;
                            d->oldMy = my;
                            update();
                        }
                        else
                            return;
                    } break;
                    default: break;
                }
            }
        } break;
    }
    if( d->action == A_HELPLINES )
    {
        emit moveHelpLines( e->pos(), orientation == TQt::Horizontal);
    }

    d->oldMx = mx;
    d->oldMy = my;
}

void KoRuler::resizeEvent( TQResizeEvent *e )
{
    TQFrame::resizeEvent( e );
    buffer.resize( size() );
}

void KoRuler::mouseDoubleClickEvent( TQMouseEvent* )
{
    handleDoubleClick();
}

void KoRuler::handleDoubleClick()
{
    if ( !d->m_bReadWrite )
        return;

    d->doubleClickedIndent = false;

    // When Binary Compatibility is broken this will hopefully emit a
    // doubleClicked(int) to differentiate between double-clicking an
    // indent and double-clicking the ruler
    if ( d->flags & F_INDENTS ) {
        if ( d->action == A_LEFT_INDENT || d->action == A_RIGHT_INDENT || d->action == A_FIRST_INDENT ) {
            d->doubleClickedIndent = true;
            emit doubleClicked(); // usually paragraph dialog
            return;
        }
    }

    // Double-clicked nothing
    d->action = A_NONE;
    emit doubleClicked(); // usually page layout dialog
}

void KoRuler::setTabList( const KoTabulatorList & _tabList )
{
    d->tabList = _tabList;
    qHeapSort(d->tabList);   // "Trust no one." as opposed to "In David we trust."

    // Note that d->currTab and d->removeTab could now point to
    // tabs which don't exist in d->tabList

    update();
}

double KoRuler::makeIntern( double _v )
{
    return KoUnit::fromUserValue( _v, m_unit );
}

void KoRuler::setupMenu()
{
    d->rb_menu = new TQPopupMenu();
    TQ_CHECK_PTR( d->rb_menu );
    for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
    {
        KoUnit::Unit unit = static_cast<KoUnit::Unit>( i );
        d->rb_menu->insertItem( KoUnit::unitDescription( unit ), i /*as id*/ );
        if ( m_unit == unit )
            d->rb_menu->setItemChecked( i, true );
    }
    connect( d->rb_menu, TQ_SIGNAL( activated( int ) ), TQ_SLOT( slotMenuActivated( int ) ) );

    d->rb_menu->insertSeparator();
    d->mPageLayout=d->rb_menu->insertItem(i18n("Page Layout..."), this, TQ_SLOT(pageLayoutDia()));
#if 0
    d->rb_menu->insertSeparator();
    d->mRemoveTab=d->rb_menu->insertItem(i18n("Remove Tabulator"), this, TQ_SLOT(rbRemoveTab()));
    d->rb_menu->setItemEnabled( d->mRemoveTab, false );
#endif
}

void KoRuler::uncheckMenu()
{
    for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
        d->rb_menu->setItemChecked( i, false );
}

void KoRuler::setUnit( KoUnit::Unit unit )
{
    m_unit = unit;
    uncheckMenu();
    d->rb_menu->setItemChecked( m_unit, true );
    update();
}

void KoRuler::setZoom( const double& zoom )
{
    if(zoom==m_zoom)
        return;
    if(zoom < 1E-4) // Don't do 0 or negative values
        return;
    m_zoom=zoom;
    m_1_zoom=1/m_zoom;
    update();
}

bool KoRuler::willRemoveTab( int y ) const
{
    return (y < -50 || y > height() + 25) && d->currTab.type != T_INVALID;
}

void KoRuler::rbRemoveTab() {

    d->tabList.remove( d->currTab );
    d->currTab.type = T_INVALID;
    emit tabListChanged( d->tabList );
    update();
}

void KoRuler::setReadWrite(bool _readWrite)
{
    d->m_bReadWrite=_readWrite;
}

void KoRuler::searchTab(int mx) {

    int pos;
    d->currTab.type = T_INVALID;
    KoTabulatorList::ConstIterator it = d->tabList.begin();
    for ( ; it != d->tabList.end() ; ++it ) {
        pos = tqRound(applyRtlAndZoom((*it).ptPos)) - diffx + frameStart;
        if ( mx > pos - 5 && mx < pos + 5 ) {
            setCursor( TQt::sizeHorCursor );
            d->action = A_TAB;
            d->currTab = *it;
            break;
        }
    }
}

void KoRuler::drawLine(int oldX, int newX) {

    TQPainter p( d->canvas );
    p.setRasterOp( TQt::NotROP );
    p.drawLine( oldX, 0, oldX, d->canvas->height() );
    if(newX!=-1)
        p.drawLine( newX, 0, newX, d->canvas->height() );
    p.end();
}

void KoRuler::showMousePos( bool _showMPos )
{
    showMPos = _showMPos;
    hasToDelete = false;
    mposX = -1;
    mposY = -1;
    update();
}

void KoRuler::setOffset( int _diffx, int _diffy )
{
    //kdDebug() << "KoRuler::setOffset " << _diffx << "," << _diffy << endl;
    diffx = _diffx;
    diffy = _diffy;
    update();
}

void KoRuler::setFrameStartEnd( int _frameStart, int _frameEnd )
{
    if ( _frameStart != frameStart || _frameEnd != d->frameEnd || !m_bFrameStartSet )
    {
        frameStart = _frameStart;
        d->frameEnd = _frameEnd;
        // Remember that setFrameStartEnd was called. This activates a slightly
        // different mode (when moving start and end positions).
        m_bFrameStartSet = true;
        update();
    }
}

void KoRuler::setRightIndent( double _right )
{
    d->i_right = makeIntern( _right );
    update();
}

void KoRuler::setDirection( bool rtl )
{
    d->rtl = rtl;
    update();
}

void KoRuler::changeFlags(int _flags)
{
    d->flags = _flags;
}

int KoRuler::flags() const
{
    return d->flags;
}

bool KoRuler::doubleClickedIndent() const
{
    return d->doubleClickedIndent;
}

double KoRuler::applyRtlAndZoom( double value ) const
{
    int frameWidth = d->frameEnd - frameStart;
    return d->rtl ? ( frameWidth - zoomIt( value ) ) : zoomIt( value );
}

double KoRuler::unZoomItRtl( int pixValue ) const
{
    int frameWidth = d->frameEnd - frameStart;
    return d->rtl ? ( unZoomIt( (double)(frameWidth - pixValue) ) ) : unZoomIt( (double)pixValue );
}

void KoRuler::slotMenuActivated( int i )
{
    if ( i >= 0 && i <= KoUnit::U_LASTUNIT )
    {
        KoUnit::Unit unit = static_cast<KoUnit::Unit>(i);
        setUnit( unit );
        emit unitChanged( unit );
    }
}

TQSize KoRuler::minimumSizeHint() const
{
    TQSize size;
    TQFont font = TDEGlobalSettings::toolBarFont();
    TQFontMetrics fm( font );

    size.setWidth( TQMAX( fm.height() + 4, 20 ) );
    size.setHeight( TQMAX( fm.height() + 4, 20 ) );

    return size;
}

TQSize KoRuler::sizeHint() const
{
    return minimumSizeHint();
}

void KoRuler::setPageLayout( const KoPageLayout& _layout )
{
    d->layout = _layout;
    update();
}

#include "kreruler.moc"