summaryrefslogblamecommitdiffstats
path: root/private/mvdm/wow16/write/ruler.c
blob: f2b76310431188836437f4e38592868a088082ed (plain) (tree)
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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                             
/************************************************************/
/* Windows Write, Copyright 1985-1992 Microsoft Corporation */
/************************************************************/

/* This file contains the routines for creating, displaying, and manipulating
the ruler for Memo. */

#define NOGDICAPMASKS
#define NOVIRTUALKEYCODES
#define NOWINMESSAGES
#define NOMENUS
#define NOICON
#define NOKEYSTATE
#define NOSYSCOMMANDS
#define NOATOM
#define NOBRUSH
#define NOCLIPBOARD
#define NOCOLOR
#define NOCREATESTRUCT
#define NOCTLMGR
#define NODRAWTEXT
#define NOMB
#define NOMEMMGR
#define NOMENUS
#define NOMETAFILE
#define NOMSG
#define NOOPENFILE
#define NOREGION
#define NOSCROLL
#define NOSOUND
#define NOWH
#define NOWINOFFSETS
#define NOWNDCLASS
#define NOCOMM
#include <windows.h>

#include "mw.h"
#include "cmddefs.h"
#include "wwdefs.h"
#include "rulerdef.h"
#include "propdefs.h"
#include "prmdefs.h"
#include "docdefs.h"
#include "bitmaps.h"

#define MERGEMARK 0x00990066

extern HWND hParentWw;
extern HANDLE hMmwModInstance;
extern HCURSOR vhcIBeam;
extern struct DOD (**hpdocdod)[];
extern struct WWD *pwwdCur;
extern struct PAP vpapAbs;
extern struct SEP vsepAbs;
extern struct SEL selCur;
extern typeCP cpMacCur;
extern int docCur;
extern int vdocParaCache;
extern int dypRuler;
extern int dxpLogInch;
extern int dypLogInch;
extern int dxpLogCm;
extern int dypLogCm;
extern int xpSelBar;
extern HWND vhWndRuler;
extern int vdxaTextRuler;
extern int mprmkdxa[rmkMARGMAX];
extern int vfTabsChanged;
extern int vfMargChanged;
extern struct WWD rgwwd[];
extern long rgbBkgrnd;
extern long rgbText;
extern HBRUSH hbrBkgrnd;
extern long ropErase;
extern BOOL vfMonochrome;
extern BOOL vfEraseWw;
extern int vfIconic;

#ifdef RULERALSO
extern HWND vhDlgIndent;
#endif /* RULERALSO */

HDC vhDCRuler = NULL;
HDC hMDCBitmap = NULL;
HDC hMDCScreen = NULL;
HBITMAP hbmBtn = NULL;
HBITMAP hbmMark = NULL;
HBITMAP hbmNullRuler = NULL;
int dxpRuler;

int viBmRuler = -1;  /* Index into [CGA/EGA/VGA/8514] bitmaps (see
                        WRITE.RC).  Set appropriately in FCreateRuler(). */

static RECT rgrcRulerBtn[btnMaxUsed];
static int mprlcbtnDown[rlcBTNMAX] = {btnNIL, btnNIL, btnNIL};
static struct TBD rgtbdRuler[itbdMax];
static int xpMinCur;
static int dxpMark;
static int dypMark;
static int btnTabSave = btnLTAB;


near UpdateRulerBtn(int, int);
BOOL near FCreateRuler(void);
int near DestroyRuler(void);
int near RulerStateFromPt(POINT, int *, int *);
int near MergeRulerMark(int, int, BOOL);
BOOL near FPointNear(unsigned, unsigned);
unsigned near XaQuantize(int);
int near DeleteRulerTab(struct TBD *);
int near InsertRulerTab(struct TBD *);
BOOL near FCloseXa(unsigned, unsigned);
#ifdef KINTL
unsigned near XaKickBackXa(unsigned);
near XpKickBackXp(int);
unsigned near XaQuantizeXa(unsigned);
#endif /* KINTL */

fnShowRuler()
    {
    /* This routine toggles the creation and the destruction of the ruler
    window. */

    StartLongOp();
    if (pwwdCur->fRuler)
        {
        /* Take down the existing ruler. */
        DestroyRuler();
        SetRulerMenu(TRUE);
        }
    else
        {
        /* There is no ruler, bring one up. */
        if (FCreateRuler())
            {
            SetRulerMenu(FALSE);
            }
        }
    EndLongOp(vhcIBeam);
    }


BOOL near FCreateRuler()
    {
    /* This routine creates the ruler child window and positions it on the
    screen. */

    extern CHAR szRulerClass[];
    int xpMac = pwwdCur->xpMac;
    int ypMac = pwwdCur->ypMac;
    LOGFONT lf;
    HFONT hf;
    int dyp;
    HPEN hpen;
    RECT rc;
    TEXTMETRIC tmSys;
    HDC hdcSys;


    /* Create the ruler window. */
    if ((vhWndRuler = CreateWindow((LPSTR)szRulerClass, (LPSTR)NULL,
      WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 0, 0, hParentWw, NULL, hMmwModInstance,
      (LPSTR)NULL)) == NULL)
        {
        goto Error2;
        }

    /* Save the DC and the memory DC. */
    if ((vhDCRuler = GetDC(vhWndRuler)) == NULL || (hMDCBitmap =
      CreateCompatibleDC(vhDCRuler)) == NULL || (hMDCScreen =
      CreateCompatibleDC(vhDCRuler)) == NULL)
        {
        goto Error1;
        }

    /* Create a null bitmap for the ruler. */
    if ((hbmNullRuler = CreateBitmap(1, 1, 1, 1, (LPSTR)NULL)) == NULL)
        {
        goto Error1;
        }

    /* New for Write 3.0: we have a variety of bitmaps for the ruler buttons 
       and marks -- loaded depending on the resolution of the user's display.
       All we really want to do here is set viBmRuler, which indexes into the
       appropriate bitmaps (see bitmaps.h) ..pault 7/13/89 */

    if (viBmRuler < 0)
        {
        /* This idea of passing NULL to GetDC borrowed from WinWord ..pt */
        if ((hdcSys = GetDC(NULL)) == NULL) 
            goto Error1;
        else
            {
            int tmHeight;

            GetTextMetrics(hdcSys, (LPTEXTMETRIC) &tmSys);
            tmHeight = tmSys.tmHeight;
            ReleaseDC(NULL, hdcSys);
            
            viBmRuler = 0;
            if (tmHeight > 8)
                viBmRuler++;
            if (tmHeight > 12)
                viBmRuler++;
            if (tmHeight > 16)
                viBmRuler++;
            }
        
        Diag(CommSzNum("FCreateRuler: index into [CGA/EGA/VGA/8514] bitmaps==", viBmRuler));
        Assert(idBmBtns + viBmRuler < idBmBtnsMax);
        Assert(idBmMarks + viBmRuler < idBmMarksMax);
        }

    /* Get the bitmaps for the ruler buttons and the ruler marks. */
    if (hbmBtn == NULL || SelectObject(hMDCBitmap, hbmBtn) == NULL)
        {
        if (NULL == (hbmBtn = LoadBitmap(hMmwModInstance, 
                                         MAKEINTRESOURCE(idBmBtns+viBmRuler))))
            {
            goto Error1;
            }
        }
    if (hbmMark == NULL || SelectObject(hMDCBitmap, hbmMark) == NULL)
        {
        if (NULL == (hbmMark = LoadBitmap(hMmwModInstance, 
                                          MAKEINTRESOURCE(idBmMarks+viBmRuler))))
            {
            goto Error1;
            }
        }

    /* Get the font for labelling the ruler ticks. */
    bltbc(&lf, 0, sizeof(LOGFONT));
    lf.lfHeight = -MultDiv(czaPoint * 8, dypLogInch, czaInch);
    if ((hf = CreateFontIndirect(&lf)) != NULL)
        {
        if (SelectObject(vhDCRuler, hf) == NULL)
            {
            DeleteObject(hf);
            }
        }

    /* If this is the first time the ruler is created, then initialize the
    static variables. */
    if (dypRuler == 0)
        {
        int dxpMajor;
        int dxpMinor;
        BITMAP bm;
        int xp;
        int dxpBtn;
        int btn;
        PRECT prc;
        TEXTMETRIC tm;

        /* Initialize the starting position of the buttons. */
        dxpMinor = (dxpMajor = dxpLogInch >> 1) >> 2;
        xp = xpSelBar + dxpMajor + (dxpMajor >> 1);

        /* Get the width and height of the buttons. */
        GetObject(hbmBtn, sizeof(BITMAP), (LPSTR)&bm);
        /* Factor of 2 since we have positive and negative images 
           of each button embedded in the bitmap now ..pault */
        dxpBtn = bm.bmWidth / (btnMaxReal*2);
        dypRuler = bm.bmHeight;

        /* Position the buttons. */
        for (prc = &rgrcRulerBtn[btn = btnMIN]; btn < btnMaxUsed; btn++, prc++)
            {
            prc->left = xp;
            prc->top = 1;
            prc->right = (xp += dxpBtn);
            prc->bottom = bm.bmHeight + 1;
            xp += (btn == btnTABMAX || btn == btnSPACEMAX) ? dxpMajor :
              dxpMinor;
            }

        /* Get the width and height of the tab marks. */
        GetObject(hbmMark, sizeof(BITMAP), (LPSTR)&bm);
        dxpMark = bm.bmWidth / rmkMAX;
        dypMark = bm.bmHeight;

        /* Lastly, initialize the height of the ruler. (Four is for the two
        lines at the bottom of the ruler plus two blank lines.) */
        GetTextMetrics(vhDCRuler, (LPTEXTMETRIC)&tm);
        dypRuler += dypMark + (tm.tmAscent - tm.tmInternalLeading) + 4;
        }

    /* Move the document window to make room for the ruler. */
    pwwdCur->fRuler = TRUE;
    dyp = dypRuler - (pwwdCur->ypMin - 1);
    MoveWindow(wwdCurrentDoc.wwptr, 0, dyp, xpMac, ypMac - dyp, FALSE);

    /* Erase the top of the document window. */
    PatBlt(wwdCurrentDoc.hDC, 0, 0, xpMac, wwdCurrentDoc.ypMin, ropErase);
    rc.left = rc.top = 0;
    rc.right = xpMac;
    rc.bottom = wwdCurrentDoc.ypMin;
    ValidateRect(wwdCurrentDoc.wwptr, (LPRECT)&rc);
    UpdateWindow(wwdCurrentDoc.wwptr);

    /* Move the ruler into position. */
    MoveWindow(vhWndRuler, 0, 0, xpMac, dypRuler, FALSE);
    BringWindowToTop(vhWndRuler);

    /* Set the DC to transparent mode. */
    SetBkMode(vhDCRuler, TRANSPARENT);

    /* Set the background and foreground colors for the ruler. */
    SetBkColor(vhDCRuler, rgbBkgrnd);
    SetTextColor(vhDCRuler, rgbText);

    /* Set the brush and the pen for the ruler. */
    SelectObject(vhDCRuler, hbrBkgrnd);
    if ((hpen = CreatePen(0, 0, rgbText)) == NULL)
        {
        hpen = GetStockObject(BLACK_PEN);
        }
    SelectObject(vhDCRuler, hpen);

    /* Lastly, ensure that the ruler is painted. */
    ShowWindow(vhWndRuler, SHOW_OPENWINDOW);
    UpdateWindow(vhWndRuler);
    return (TRUE);

Error1:
    DestroyWindow(vhWndRuler);
    vhWndRuler = NULL;
Error2:
    WinFailure();
    return (FALSE);
    }


near DestroyRuler()
    {
    /* This routine destroys the ruler window and refreshes the screen. */

    /* First, erase the ruler. */
    PatBlt(vhDCRuler, 0, 0, dxpRuler, dypRuler, ropErase);

    /* Clean up the ruler window. */
    DestroyWindow(vhWndRuler);
    vhWndRuler = NULL;
    ResetRuler();

    /* Move the document window back to the top of the window. */
    pwwdCur->fRuler = FALSE;
    vfEraseWw = TRUE;
    MoveWindow(wwdCurrentDoc.wwptr, 0, 0, dxpRuler, wwdCurrentDoc.ypMac +
      dypRuler - (wwdCurrentDoc.ypMin - 1), FALSE);
    vfEraseWw = FALSE;

    /* Validate the area in the document window above the text. */
    PatBlt(wwdCurrentDoc.hDC, 0, 0, dxpRuler, wwdCurrentDoc.ypMin, ropErase);
    ValidateRect(hParentWw, (LPRECT)NULL);
    }


UpdateRuler()
    {
    /* This routine will redraw as much of the ruler as necessary to reflect the
    current selection. */

    /* Only repaint the ruler if it exists and it is not currently being
    changed. */
    if (vhWndRuler != NULL)
        {
        RulerPaint(FALSE, FALSE, FALSE);
        }
    }

ReframeRuler()
    {
    /* This routine will cause the ruler window to be redrawn,
       when units change - leave update out, since dialog box
       will repaint */

    /* Only repaint the ruler if it exists . */
    if (vhWndRuler != NULL)
        {
        InvalidateRect(vhWndRuler, (LPRECT)NULL, FALSE);
        }
    }



ResetRuler()
    {
    /* Reset the values of the ruler buttons and the ruler margins and tabs so
    they redrawn during the next paint message. */
    if ((btnTabSave = mprlcbtnDown[rlcTAB]) == btnNIL)
        {
        btnTabSave = btnLTAB;
        }

    /* Reset the buttons. */
    if (vfIconic)
        {
        /* All we have to do is reset our internal state. */
        bltc(mprlcbtnDown, btnNIL, rlcBTNMAX);
        }
    else
        {
        /* We had best reset the buttons on the screen as well. */
        UpdateRulerBtn(rlcTAB, btnNIL);
        UpdateRulerBtn(rlcSPACE, btnNIL);
        UpdateRulerBtn(rlcJUST, btnNIL);
        }

    /* Reset the margins and the tabs. */
    bltc(mprmkdxa, -1, rmkMARGMAX);
    bltc(rgtbdRuler, 0, cwTBD * itbdMax);
    }


ResetTabBtn()
    {
    /* This routine resets the tab button on the ruler to the left tab button.
    */
    if (mprlcbtnDown[rlcTAB] != btnLTAB)
        {
        UpdateRulerBtn(rlcTAB, btnLTAB);
        }
    }


RulerPaint(fContentsOnly, fFrameOnly, fInit)
BOOL fContentsOnly;
BOOL fInit;
    {
    /* This routine draws the ruler in the ruler window.  If fContentsOnly is
    set, then only the tabs as they currently exist in rgtbdRuler, and the
    button settings are drawn.  If fFrameOnly is set, then only the ruler frame
    is redrawn.  If fInit is set, then the portion of the ruler to be redrawn
    (tabs, frame or all) is redrawn from scratch. */


    int xpMin = pwwdCur->xpMin;
    HBITMAP hbm;

    /* If fContentsOnly is set, then skip most of this stuff and draw only the
    tabs and the button settings.  */
    if (!fContentsOnly)
        {
        /* We only need to draw the physical ruler itself when the window has
        scrolled horizontally. */
        if (fInit || xpMinCur != xpMin)
            {
            register int xp;
            TEXTMETRIC tm;
            int dypTick;
            int ypTickEnd;
            int ypTickStart;
            int ypTick;
            int iLevel;
            CHAR rgchInch[3];
            int dxpLogUnitInc;
            int dcNextTick;
            int dxpLine;


            extern int utCur;
#define cDivisionMax 8  /* max divisions per ruler unit. e.g. 8 per inch */
            int rgypTick[cDivisionMax];
            int cxpExtra;
            int cDivision;
            int dxpLogUnit;
            int dxpMeas;
            int ypT;

            /* Initialize the y-coordinate of the ticks. */
            GetTextMetrics(vhDCRuler, (LPTEXTMETRIC)&tm);
            ypTickEnd = dypRuler - dypMark - 2;
            ypTickStart = ypTick = ypTickEnd - (dypTick = tm.tmAscent -
              tm.tmInternalLeading);

            /* set up measurements for the ruler based on current unit -
               note that only inch and cm are handled in this version */

            if (utCur == utInch)
                           {
                           dxpLogUnit = dxpLogUnitInc = dxpLogInch;
                           cDivision = 8;  /* # of divisions */
                           dxpMeas = dxpLogUnit >> 3;  /* 1/8" units */
                         /* get extra pixels to distribute if not even multiple */
                         /* note - mod done by hand */
                           cxpExtra = dxpLogUnit - (dxpMeas << 3);
                           dcNextTick = 1;
                          /* fill table of tick lengths */
                           rgypTick[0] = ypT = ypTick;
                           rgypTick[4] = ypT += (dypTick >> 2);
                           rgypTick[2] = rgypTick[6] = ypT += (dypTick >> 2);
                           rgypTick[1] = rgypTick[3] = rgypTick[5] = rgypTick[7]  =
                           ypT += (dypTick >> 2);
                           }
            else
                /* default to cm */
                           {
                           dxpLogUnit = dxpLogUnitInc = dxpLogCm;
                           cDivision = 2;  /* # of divisions */
                           dxpMeas = dxpLogUnit >> 1;  /* 1/2 cm units */
                         /* get extra pixels to distribute if not even multiple */
                           cxpExtra = dxpLogUnit - (dxpMeas << 1);
                           dcNextTick = 1;
                          /* fill table of tick lengths */
                           rgypTick[0] =  ypTick;
                           rgypTick[1] = ypTick + (dypTick >> 1);
                           }

            if (fInit)
                {
                /* Erase the area where the ruler will be drawn. */
                PatBlt(vhDCRuler, 0, 0, dxpRuler, dypRuler, ropErase);

                /* Draw a line across the bottom of the ruler. */
                MoveTo(vhDCRuler, xpSelBar, dypRuler - 1);
                LineTo(vhDCRuler, dxpRuler, dypRuler - 1);

                /* Draw the base of the ruler. */
                MoveTo(vhDCRuler, xpSelBar, ypTickEnd);
                LineTo(vhDCRuler, dxpRuler, ypTickEnd);
                }
            else
                {
                /* Erase the old tick marks. */
                PatBlt(vhDCRuler, 0, ypTickStart, dxpRuler, ypTickEnd -
                  ypTickStart, ropErase);
                }

            /* Set the clip region to be only the ruler. */
            iLevel = SaveDC(vhDCRuler);
            IntersectClipRect(vhDCRuler, xpSelBar, 0, dxpRuler, dypRuler);

            /* Draw the ticks at the each division mark. */
            /* iDivision is the current division with in a unit. It is
               used to determine when extra pixels are distributed and
               which tick mark to use */
            {
            register int iDivision = 0;

            for (xp = (xpSelBar - xpMin); xp < dxpRuler; xp +=
              dxpMeas)
                {
                  /* distribute extra pixels at front */
                if (iDivision < cxpExtra)
                   xp++;

                MoveTo(vhDCRuler, xp, rgypTick[iDivision]);
                LineTo(vhDCRuler, xp, ypTickEnd);

                if (++iDivision == cDivision)
                   iDivision = 0;
                }
            }


            /* Label the tick marks. */
            dxpLine = GetSystemMetrics(SM_CXBORDER);
            rgchInch[0] = rgchInch[1] = rgchInch[2] = '0';
            for (xp = xpSelBar - xpMin;
                 xp < dxpRuler;
                 xp += dxpLogUnitInc, rgchInch[2] += dcNextTick)
                {
                    int isz;
                    int dxpsz;

                    if (rgchInch[2] > '9')
                        {
                        rgchInch[1]++;
                        rgchInch[2] = '0' + (rgchInch[2] - (CHAR) ('9' + 1));
                        }
                    if (rgchInch[1] > '9')
                        {
                        rgchInch[0]++;
                        rgchInch[1] = '0' + (rgchInch[1] - (CHAR) ('9' + 1));
                        }
                    isz = rgchInch[0] == '0' ?
                                (rgchInch[1] == '0' ? 2 : 1):
                                0;
                    dxpsz = LOWORD(GetTextExtent(vhDCRuler,
                                                 (LPSTR)&rgchInch[isz],
                                                 3 - isz));
                    if (dxpsz + dxpLine >= dxpMeas)
                        {
                            PatBlt(vhDCRuler, xp + dxpLine, ypTickStart,
                                   dxpsz, ypTickEnd - ypTickStart, ropErase);
                        }
                    TextOut(vhDCRuler, xp + dxpLine, ypTickStart -
                            tm.tmInternalLeading, (LPSTR)&rgchInch[isz],
                            3 - isz);
                }


            /* Set the clip region back. */
            RestoreDC(vhDCRuler, iLevel);
            }

        /* Draw the buttons on the ruler. */
        if (fInit)
            {
            register PRECT prc = &rgrcRulerBtn[btnMIN];
            int btn;

            /* Ensure that we have the bitmap for the buttons. */
            if (SelectObject(hMDCBitmap, hbmBtn) == NULL)
                {
                if (NULL == (hbmBtn = LoadBitmap(hMmwModInstance, 
                                                 MAKEINTRESOURCE(idBmBtns+viBmRuler)))
                             || SelectObject(hMDCBitmap, hbmBtn) == NULL)
                    {
                    WinFailure();
                    goto NoBtns;
                    }
                }

            /* Now, draw the buttons. */
            for (btn = btnMIN; btn < btnMaxUsed; btn++)
                {
                int dxpBtn = prc->right - prc->left;

                BitBlt(vhDCRuler, prc->left, prc->top, dxpBtn, prc->bottom -
                  prc->top, hMDCBitmap, (btn - btnMIN) * dxpBtn, 0, vfMonochrome
                  ? MERGEMARK : SRCCOPY);
                prc++;
                }
            SelectObject(hMDCBitmap, hbmNullRuler);
NoBtns:;
            }
        }

    /* If fFrame only is set, then we're finished. */
    if (!fFrameOnly)
        {
        /* Lastly, draw the button settings, the margins and the tabs. */
        TSV rgtsv[itsvparaMax];
        register struct TBD *ptbd1;
        int rmk;
        int xpMarkMin = xpSelBar - (dxpMark >> 1);
        int dxpMarkMax = dxpRuler - xpSelBar - (dxpMark >> 1);
        unsigned dxa;

        if (mprlcbtnDown[rlcTAB] == btnNIL)
            {
            /* Initalize the tab button to be left tab. */
            UpdateRulerBtn(rlcTAB, btnTabSave);
            }

        /* Now for the spacing and justification. */
        GetRgtsvPapSel(rgtsv);
        UpdateRulerBtn(rlcSPACE, (rgtsv[itsvSpacing].fGray != 0) ? btnNIL :
          (rgtsv[itsvSpacing].wTsv - czaLine) / (czaLine / 2) + btnSINGLE);
        UpdateRulerBtn(rlcJUST, (rgtsv[itsvJust].fGray != 0) ? btnNIL :
          (rgtsv[itsvJust].wTsv - jcLeft) + btnLEFT);

        /* The margins and the tabs are based off of the first cp of the
        selection. */
        CacheSect(docCur, selCur.cpFirst);
        CachePara(docCur, selCur.cpFirst);

        /* If the window has scrolled horizontally or become wider, we must
        redraw the margins and the tabs. */
        if (!fInit && xpMinCur == xpMin)
            {
            /* Compare to see if the margins have changed. */
            if (mprmkdxa[rmkINDENT] != vpapAbs.dxaLeft + vpapAbs.dxaLeft1)
                {
                goto DrawMargins;
                }
            if (mprmkdxa[rmkLMARG] != vpapAbs.dxaLeft)
                {
                goto DrawMargins;
                }
            if (mprmkdxa[rmkRMARG] != vsepAbs.dxaText - vpapAbs.dxaRight)
                {
                goto DrawMargins;
                }

            /* Compare to see if the tabs has changed. */
                {
                register struct TBD *ptbd2;

                for (ptbd1 = &rgtbdRuler[0], ptbd2 = &vpapAbs.rgtbd[0];
                  ptbd1->dxa == ptbd2->dxa; ptbd1++, ptbd2++)
                    {
                    /* If the end of the list of tabs, then the lists are equal.
                    */
                    if (ptbd1->dxa == 0)
                        {
                        goto SkipTabs;
                        }

                    /* The justification codes must match if they are decimal
                    tabs (everything else collaspes to left tabs). */
                    if (ptbd1->jc != ptbd2->jc && (ptbd1->jc == (jcTabDecimal
                      - jcTabMin) || (ptbd2->jc == (jcTabDecimal - jcTabMin))))
                        {
                        goto DrawMargins;
                        }
                    }
                }
            }

DrawMargins:
#ifdef KINTL
        /* This is really an extra.  xpMinCur will get updated later on.
           But, we need this variable set up right for the MergeRulerMark()
           to draw a mark at the right place.... Oh well. */
        xpMinCur = xpMin;
#endif /* ifdef KINTL */

        /* Redraw the margins from scratch.  Set up the bitmap for hMDCScreen,
        the ruler bar in monochrome format. */
        if ((hbm = CreateBitmap(dxpRuler + dxpMark, dypMark, 1, 1,
          (LPSTR)NULL)) == NULL)
            {
            WinFailure();
            goto SkipTabs;
            }
        DeleteObject(SelectObject(hMDCScreen, hbm));
        PatBlt(hMDCScreen, 0, 0, dxpRuler + dxpMark, dypMark, vfMonochrome ?
          ropErase : WHITENESS);
        PatBlt(vhDCRuler, 0, dypRuler - dypMark - 1, dxpRuler + dxpMark,
          dypMark, ropErase);

        /* Determine the margin positions. */
        mprmkdxa[rmkINDENT] = vpapAbs.dxaLeft + vpapAbs.dxaLeft1;
        mprmkdxa[rmkLMARG] = vpapAbs.dxaLeft;
        mprmkdxa[rmkRMARG] = (vdxaTextRuler = vsepAbs.dxaText) -
          vpapAbs.dxaRight;

        /* Draw the margins marks. */
        for (rmk = rmkMARGMIN; rmk < rmkMARGMAX; rmk++)
            {
            register int dxp = MultDiv(mprmkdxa[rmk], dxpLogInch, czaInch) -
              xpMin;

            /* If the margin mark would not appear on the ruler, scrolled off to
            either end, then don't try to draw it. */
            if (dxp >= 0 && dxp < dxpMarkMax)
                {
                MergeRulerMark(rmk, xpMarkMin + dxp, FALSE);
                }
            }

        /* Redraw the tabs. */
        ptbd1 = &rgtbdRuler[0];
        if (!fInit)
            {
            /* If fInit is set, then rgtbdRuler is not changed. */
            blt(vpapAbs.rgtbd, ptbd1, cwTBD * itbdMax);
            }
        while ((dxa = ptbd1->dxa) != 0)
            {
            register int dxp = MultDiv(dxa, dxpLogInch, czaInch) - xpMin;

            /* If the tab mark would not appear on the ruler, scrolled off to
            either end, then don't try to draw it. */
            if (dxp >= 0 && dxp < dxpMarkMax)
                {
                MergeRulerMark(ptbd1->jc == (jcTabDecimal - jcTabMin) ? rmkDTAB
                  : rmkLTAB, xpMarkMin + dxp, FALSE);
                }
            ptbd1++;
            }
SkipTabs:;
        }

    /* Record the edges of the current window. */
    xpMinCur = xpMin;
    }


RulerMouse(pt)
POINT pt;
    {
    /* Process all mouse messages from a down-click at point pt until the
    corresponding mouse up-click. */

    int btn;
    int rlc;
    int rlcCur;
    int rmkCur;
    int xp;
    int xpCur;
    unsigned xa;
    struct TBD *ptbd;
    struct TBD tbd;
    BOOL fMarkMove = FALSE;
    BOOL fDeleteMark = FALSE;
    BOOL fBtnChanged = FALSE;

    if (!FWriteOk(fwcNil))
        {
        return;
        }

    /* Translate the point into a button group and a button. */
    RulerStateFromPt(pt, &rlcCur, &btn);

    /* Down clicking on the tab rule is a special case. */
    if (rlcCur == rlcRULER)
        {
        unsigned dxa = MultDiv(pt.x - xpSelBar + xpMinCur, czaInch, dxpLogInch);
        int rmk;
        int itbd;

        /* Have we moused down on a margin? */
        for (rmk = rmkMARGMIN; rmk < rmkMARGMAX; rmk++)
            {
#ifdef KINTL
            if (FPointNear(mprmkdxa[rmk], dxa - XaKickBackXa(dxa)))
#else
            if (FPointNear(mprmkdxa[rmk], dxa))
#endif /* if-else-def KINTL */
                {
                int     xpT;

                /* Remember this mark and its position. */
                rmkCur = rmk;
                xpCur = xpSelBar + MultDiv(mprmkdxa[rmk], dxpLogInch, czaInch) -
                  (dxpMark >> 1) - xpMinCur;

InvertMark:
#ifdef KINTL
                /* Adjust for the kick-backs.  */
                /* But don't modify the xpCur. */
                xpT = xpCur + XpKickBackXp(xpCur);
#else
                xpT = xpCur;
#endif /* if-else-def KINTL */

                /* Time to invert the selected mark. */
                PatBlt(vhDCRuler, xpT, dypRuler - dypMark - 1, dxpMark,
                  dypMark, DSTINVERT);
                goto GotMark;
                }
            }

        /* Have we moused down on an existing tab? */
        for (itbd = 0, ptbd = &rgtbdRuler[0]; ; itbd++, ptbd++)
            {
            /* The end of the tabs have been found. */
            if (ptbd->dxa == 0)
                {
                break;
                }

            /* Have we moused down on this tab? */
#ifdef KINTL
            if (FPointNear(ptbd->dxa, dxa - XaKickBackXa(dxa)))
#else
            if (FPointNear(ptbd->dxa, dxa))
#endif /* if-else-def KANJI */
                {
                /* Save this tab descriptor and its location. */
                tbd = *ptbd;
                rmkCur = (tbd.jc + jcTabMin) == jcTabDecimal ? rmkDTAB :
                  rmkLTAB;
                xpCur = xpSelBar + MultDiv(tbd.dxa, dxpLogInch, czaInch) -
                  (dxpMark >> 1) - xpMinCur;
                goto InvertMark;
                }
            }

        /* If one more tab would be too many, then beep and return. */
        if (itbd >= itbdMax - 1)
            {
            _beep();
            return;
            }

        /* Create a tab descriptor for this new tab. */
        bltc(&tbd, 0, cwTBD);
        tbd.dxa = XaQuantize(pt.x);
        tbd.jc = (mprlcbtnDown[rlcTAB] == btnLTAB ? jcTabLeft : jcTabDecimal) -
          jcTabMin;
        rmkCur = (mprlcbtnDown[rlcTAB] - btnLTAB) + rmkLTAB;

        /* A mark for the new tab needs to be drawn. */
        MergeRulerMark(rmkCur, xpCur = xpSelBar + MultDiv(tbd.dxa, dxpLogInch,
          czaInch) - (dxpMark >> 1) - xpMinCur, TRUE);

        /* Inserting a tab is like moving an existing tab. */
        fMarkMove = TRUE;

GotMark:;

#ifdef RULERALSO
        /* Update dialog box */
        if (vhDlgIndent && rmkCur < rmkMARGMAX)
            {
            SetIndentText(rmkCur, dxa);
            }
#endif /* RULERALSO */

        }
    else if (rlcCur != rlcNIL)
        {
        /* Otherwise, if a button has been selected, the reflect the change on
        the ruler. */
        UpdateRulerBtn(rlcCur, btn);
        }
    else
        {
        /* The user has moused down on nothing of importance. */
        return;
        }

    /* Get all of the mouse events until further notice. */
    SetCapture(vhWndRuler);

    /* Process all of the mouse move messages. */
    while (FStillDown(&pt))
        {
        /* Movement on the tab ruler must be handled special. */
        if (rlcCur == rlcRULER)
            {
#ifdef KINTL
            unsigned xaT;
#endif /* ifdef KINTL */

            /* Guarantee that xp is in the range xpSelBar <= xp <= dxpRuler. */
            if ((xp = pt.x) > dxpRuler)
                {
                xp = dxpRuler;
                }
            else if (xp < xpSelBar)
                {
                xp = xpSelBar;
                }

            /* Convert the mouse position to twips. */
#ifdef KINTL
            if ((xa = XaQuantize(xp)) > (xaT = XaQuantizeXa(vdxaTextRuler))
#else
            if ((xa = XaQuantize(xp)) > vdxaTextRuler
#endif /* if-else-def KINTL */
                && rmkCur < rmkMARGMAX)
                {
                /* Margins are confined to the page. */
#ifdef KINTL
                xa = xaT;
#else
                xa = vdxaTextRuler;
#endif
                }

            /* If the cursor is on the ruler, then we may move a tab, but we
            always move the margins. */
            if ((rmkCur < rmkMARGMAX) || (pt.y >= 0 && pt.y < dypRuler + dypMark
              && xa != 0))
                {
                /* If the current mark has not moved, then there is nothing to
                do.  */
                if (fDeleteMark || xa != XaQuantize(xpCur + (dxpMark >> 1)))
                    {
                    /* Indicate that the mark has moved. */
                    fMarkMove = TRUE;

                    /* Restore the screen under the current mark. */
                    if (!fDeleteMark)
                        {
                        MergeRulerMark(rmkCur, xpCur, FALSE);
                        }

                    /* Draw the mark at the new location. */
                    MergeRulerMark(rmkCur, xpCur = MultDiv(xa, dxpLogInch,
                      czaInch) + xpSelBar - xpMinCur - (dxpMark >> 1), TRUE);

                    /* Show this is a valid mark. */
                    fDeleteMark = FALSE;

#ifdef RULERALSO
                    /* Update dialog box */
                    if (vhDlgIndent && rmkCur < rmkMARGMAX)
                        {
                        SetIndentText(rmkCur, xa);
                        }
#endif /* RULERALSO */

                    }
                }
            else
                {
                /* Restore the screen under the current mark. */
                if (!fDeleteMark)
                    {
                    MergeRulerMark(rmkCur, xpCur, FALSE);
                    }

                /* This mark is being deleted. */
                fDeleteMark = TRUE;
                }
            }
        else
            {
            /* If the mouse is on a button within the same button group, then
            reflect the change. */
            RulerStateFromPt(pt, &rlc, &btn);
            if (rlc == rlcCur)
                {
                UpdateRulerBtn(rlc, btn);
                }
            }
        }

    /* We are capturing all mouse events; we can now release them. */
    ReleaseCapture();

    /* Up-clicking on the tab ruler is a special case. */
    if (rlcCur == rlcRULER)
        {
        if (!fDeleteMark)
            {
            /* Restore the screen under the current mark. */
            MergeRulerMark(rmkCur, xpCur, FALSE);
            }

        if (fMarkMove)
            {
            /* Guarantee that xp is in the range xpSelBar <= xp <= dxpRuler. */
            if ((xp = pt.x) > dxpRuler)
                {
                xp = dxpRuler;
                }
            else if (xp < xpSelBar)
                {
                xp = xpSelBar;
                }
            }
        else
            {
            xp = xpCur + (dxpMark >> 1);
            }

        /* Convert the mouse position to twips. */
        if ((xa = XaQuantize(xp)) > vdxaTextRuler && rmkCur < rmkMARGMAX)
            {
            /* Margins are confined to the page. */
            xa = vdxaTextRuler;
            }

        /* If the cursor is on the ruler then we may insert/move a tab, but we
        always move the margins. */
        if ((rmkCur < rmkMARGMAX) || (pt.y >= 0 && pt.y < dypRuler + dypMark &&
          xa != 0))
            {
            /* Draw the mark at the new location. */
            MergeRulerMark(rmkCur, MultDiv(xa, dxpLogInch, czaInch) + xpSelBar -
              xpMinCur - (dxpMark >> 1), FALSE);

            /* We are moving one of the margins. */
            if (rmkCur < rmkMARGMAX)
                {
                if (vfMargChanged = mprmkdxa[rmkCur] != xa)
                    {
                    mprmkdxa[rmkCur] = xa;
                    }

#ifdef RULERALSO
                /* Update dialog box */
                if (vhDlgIndent)
                    {
                    SetIndentText(rmkCur, xa);
                    }
#endif /* RULERALSO */

                }

            /* It is a tab we are inserting/deleting. */
            else
                {
                tbd.dxa = xa;

                /* Is this a new tab? */
                if (ptbd->dxa == 0)
                    {
                    /* Insert the new tab. */
                    InsertRulerTab(&tbd);
                    }

                /* We are moving a tab; if it hasn't really moved, then do
                nothing.  */
                else if (!FCloseXa(ptbd->dxa, xa))
                    {
                    DeleteRulerTab(ptbd);
                    InsertRulerTab(&tbd);
                    }
                }
            }

        /* We are deleting the tab; if its a new, there's nothing to do. */
        else if (ptbd->dxa != 0)
            {
            DeleteRulerTab(ptbd);
            }
        }
    else
        {
        /* If the mouse is on a button within the same button group, then
        reflect the change. */

        int btnT;

        RulerStateFromPt(pt, &rlc, &btnT);
        if (rlc == rlcCur)
            {
            UpdateRulerBtn(rlc, btn = btnT);
            }
        fBtnChanged = btn != mprlcbtnDown[btn];
        }

    /* Do the format only if a button changed */
    if ((fBtnChanged && rlcCur != rlcTAB) || vfMargChanged || vfTabsChanged)
        {
        struct SEL selSave;
        typeCP dcp;
        typeCP dcp2;
        CHAR rgb[1 + cchINT];
        CHAR *pch;
        int sprm;
        int val;
        struct TBD (**hgtbd)[];

        /* Set the selection to cover all of the paragraphs selected. */
        ExpandCurSel(&selSave);
        dcp2 = (dcp = selCur.cpLim - selCur.cpFirst) - (selCur.cpLim > cpMacCur
          ? ccpEol : 0);
        SetUndo(uacRulerChange, docCur, selCur.cpFirst, (rlcCur != rlcRULER ||
          rmkCur < rmkMARGMAX) ? dcp : dcp2, docNil, cpNil, dcp2, 0);

        /* Set the sprm and it's value for the ruler change. */
        switch (rlcCur)
            {
        case rlcSPACE:
            sprm = sprmPDyaLine;
            val = (mprlcbtnDown[rlcSPACE] - btnSINGLE) * (czaLine / 2) +
              czaLine;
            break;

        case rlcJUST:
            sprm = sprmPJc;
            val = mprlcbtnDown[rlcJUST] - btnLEFT + jcLeft;
            break;

        case rlcRULER:
            switch (rmkCur)
                {
            case rmkINDENT:
                sprm = sprmPFIndent;
                val = mprmkdxa[rmkINDENT] - mprmkdxa[rmkLMARG];
                break;

            case rmkLMARG:
                /* Changing the left margin changes the first indent as well.
                First, the indent... */
                val = mprmkdxa[rmkINDENT] - mprmkdxa[rmkLMARG];
                pch = &rgb[0];
                *pch++ = sprmPFIndent;
                bltbyte(&val, pch, cchINT);
                AddOneSprm(rgb, FALSE);

                /* Now for the left margin... */
                sprm = sprmPLMarg;
                val = mprmkdxa[rmkLMARG];
                break;

            case rmkRMARG:
                sprm = sprmPRMarg;
                val = vdxaTextRuler - mprmkdxa[rmkRMARG];
                break;

            case rmkLTAB:
            case rmkDTAB:
                /* Tabs are different.  The change is made by blting the new tab
                table on top of the old. */
                vfTabsChanged = FALSE;
                if ((hgtbd = (**hpdocdod)[docCur].hgtbd) == NULL)
                    {
                    if (FNoHeap(hgtbd = (struct TBD (**)[])HAllocate(itbdMax *
                      cwTBD)))
                        {
                        return;
                        }
                    (**hpdocdod)[docCur].hgtbd = hgtbd;
                    }
                blt(rgtbdRuler, *hgtbd, itbdMax * cwTBD);

                /* Changing the tabs makes everything dirty. */
                (**hpdocdod)[docCur].fDirty = TRUE;
                vdocParaCache = docNil;
                TrashAllWws();
                goto ChangeMade;
                }

            /* Indicate that the margins have been set. */
            vfMargChanged = FALSE;
            }

        /* Now, lets set the sprm to the new value. */
        pch = &rgb[0];
        *pch++ = sprm;
        bltbyte(&val, pch, cchINT);
        AddOneSprm(rgb, FALSE);

ChangeMade:
        /* Reset the selection to it's old value. */
        EndLookSel(&selSave, TRUE);
        }
    }


near RulerStateFromPt(pt, prlc, pbtn)
POINT pt;
int *prlc;
int *pbtn;
    {
    /* This routine return in *prlc and *pbtn, the button group and the button
    at point pt.  The only button in group rlcRULER is btnNIL. */

    int btn;

    /* First check if the point is in a button. */
    for (btn = btnMIN; btn < btnMaxUsed; btn++)
        {
        if (PtInRect((LPRECT)&rgrcRulerBtn[btn], pt))
            {
            goto ButtonFound;
            }
        }

    /* The point is either on the tab ruler or nowhere of any interest. */
    *prlc = (pt.y >= dypRuler - dypMark - 2 && pt.x > xpSelBar - (dxpMark >> 1)
      && pt.x < dxpRuler + (dxpMark >> 1)) ? rlcRULER : rlcNIL;
    *pbtn = btnNIL;
    return;

ButtonFound:
    /* The point is in a button, we just have to decide which button group. */
    switch (btn)
        {
        case btnLTAB:
        case btnDTAB:
            *prlc = rlcTAB;
            break;

        case btnSINGLE:
        case btnSP15:
        case btnDOUBLE:
            *prlc = rlcSPACE;
            break;

        case btnLEFT:
        case btnCENTER:
        case btnRIGHT:
        case btnJUST:
            *prlc = rlcJUST;
            break;
        }
    *pbtn = btn;
    }


void near HighlightButton(fOn, btn)
BOOL fOn; /* true if we should highlight this button, false = unhighlight */
int btn;
    {
    register PRECT prc = &rgrcRulerBtn[btn];
    int dxpBtn = prc->right - prc->left;
    
    /* If we're highlighting, then get the black-on-white button from
       the right group; otherwise copy the white-on-black button ..pt */
    int btnFromBM = btn - btnMIN + (fOn ? btnMaxReal : 0);

    /* Ensure that we have the bitmap for the buttons. */
    if (SelectObject(hMDCBitmap, hbmBtn) == NULL)
        {
        if ((hbmBtn = LoadBitmap(hMmwModInstance, MAKEINTRESOURCE(idBmBtns+viBmRuler))) ==
          NULL || SelectObject(hMDCBitmap, hbmBtn) == NULL)
            {
            WinFailure();
            goto NoBtns;
            }
        }

    BitBlt(vhDCRuler, prc->left, prc->top, dxpBtn, prc->bottom - prc->top, 
           hMDCBitmap, btnFromBM * dxpBtn, 0, SRCCOPY);
    
    SelectObject(hMDCBitmap, hbmNullRuler);
NoBtns:;
    }


near UpdateRulerBtn(rlc, btn)
int rlc;
int btn;
    {
    /* This routine turns off the currently selected button in button group rlc
    and turns on button btn.  It is assumed that rlc is neither rlcNIL nor
    rlcRULER, since neither group has buttons to update. */

    int *pbtnOld = &mprlcbtnDown[rlc];
    int btnOld = *pbtnOld;

    Assert(rlc != rlcNIL && rlc != rlcRULER);

    /* If the button hasn't changed, then there is nothing to do. */
    if (btn != btnOld)
        {
        if (vhDCRuler != NULL)
            {
            /* Invert the old button (back to normal), and then invert the new
            button. */
            if (btnOld != btnNIL)
                {
                /* If there is no old button, then, of course, we can't invert
                it.  */
                HighlightButton(fFalse, btnOld);
                }

            if (btn != btnNIL)
                {
                /* If the new button is not btnNIL, then invert it. */
                HighlightButton(fTrue, btn);
                }
            }

        /* Record whic button is now set. */
        *pbtnOld = btn;
        }
    }

#ifdef KINTL
/* Given xa for a mouse position in a ruler, return the amount of xa for
   a display adjustment. */
unsigned near XaKickBackXa(xa)
    unsigned        xa;
{
    extern int      utCur;
    extern int      dxaAdjustPerCm;
    int             cCm, cCh;

    switch (utCur) {
        case utCm:
            cCm = xa / czaCm;
            return (dxaAdjustPerCm * cCm);
        case utInch:
            return (0);
        default:
            Assert(FALSE);
            return (0);
        }
}

near XpKickBackXp(xp)
    int xp;
{
    /* Computes the amount of a necessary kick-back in xp, if
       a ruler marker is to be drawn at a given xp. */
    extern int utCur;
    extern int dxaAdjustPerCm;

    int        cCm, cCh;

    switch (utCur) {
        case utInch:
            return 0;
        case utCm:
            /* For every cm, we are off by dxaAdjustPerCm twips. */
            cCm = (xp - xpSelBar + xpMinCur + (dxpMark >> 1)) / dxpLogCm;
            return (MultDiv(dxaAdjustPerCm * cCm, dxpLogInch, czaInch));
        default:
            Assert(FALSE);
            return 0;
        }
}
#endif /* ifdef KINTL */


near MergeRulerMark(rmk, xpMark, fHighlight)
int rmk;
int xpMark;
BOOL fHighlight;
    {
    /* This routine merges the ruler mark, rmk, with the contents of the ruler
    bar at xpMark.  To accomodate color, the merging of the mark with the
    background must be done first in a monochrome memory bitmap, then converted
    back to color.  The mark is highlighed if fHighlight is set. */

    int ypMark = dypRuler - dypMark - 1;

    /* Ensure that we have the bitmap for the ruler marks. */
    if (SelectObject(hMDCBitmap, hbmMark) == NULL)
        {
        if ((hbmMark = LoadBitmap(hMmwModInstance, MAKEINTRESOURCE(idBmMarks+viBmRuler))) == NULL
          || SelectObject(hMDCBitmap, hbmMark) == NULL)
            {
            WinFailure();
            return;
            }
        }

#ifdef KINTL
    /* Adjust for the kick back */
    xpMark += XpKickBackXp(xpMark);
#endif /* ifdef KINTL */

    /* Merge the mark into the monochrome bitmap. */
    BitBlt(hMDCScreen, xpMark, 0, dxpMark, dypMark, hMDCBitmap, (rmk - rmkMIN) *
      dxpMark, 0, MERGEMARK);

    /* Display the bitmap on the ruler bar. */
    BitBlt(vhDCRuler, xpMark, ypMark, dxpMark, dypMark, hMDCScreen, xpMark, 0,
      fHighlight ? NOTSRCCOPY : SRCCOPY);

    SelectObject(hMDCBitmap, hbmNullRuler);
    }


BOOL near FPointNear(xaTarget, xaProbe)
unsigned xaTarget;
unsigned xaProbe;
    {
    /* This routine returns TRUE if and only if xaProbe is sufficiently close to
    xaTarget for selection purposes. */

    int dxa;

    if ((dxa = xaTarget - xaProbe) < 0)
        {
        dxa = -dxa;
        }
    return (dxa < MultDiv(dxpMark, czaInch, dxpLogInch) >> 1);
    }


unsigned near XaQuantize(xp)
int xp;
    {
#ifdef KINTL
     /* This routine converts an x-coordinate from the ruler to twips
        rounding it to the nearest sixteenth of an inch if utCur = utInch,
        or to the nearest eighth of a centimeter if utCur = utCm. */
    unsigned xa = MultDiv(xp - xpSelBar + xpMinCur, czaInch, dxpLogInch);
    return (XaQuantizeXa(xa));
#else
    /* This routine converts an x-coordinate from the ruler to twips rounding it
    to the nearest sixteenth of an inch. */

    unsigned xa = MultDiv(xp - xpSelBar + xpMinCur, czaInch, dxpLogInch);

    /* NOTE: This code has been simplified because we "know" czaInch is a
    multiple of 32. */
    return ((xa + czaInch / 32) / (czaInch / 16) * (czaInch / 16));
#endif /* not KINTL */
    }

#ifdef KINTL
unsigned near XaQuantizeXa(xa)
    unsigned xa;
{
    extern int utCur;
    long    xaL;

    switch (utCur) {
        case utInch:
            /* NOTE: This code has been simplified because we "know" czaInch is a
                     multiple of 32. */
            return ((xa + czaInch / 32) / (czaInch / 16) * (czaInch / 16));
        case utCm:
            /* NOTE: Actually, we are calculating:
                     (xa + czaCm / 16) / (czaCm / 8) * (czaCm / 8)
                     but calculated in 16*twips, so that there will
                     be the least rounding error. */
            xaL = ((long) xa) << 4;
            xaL = (xaL + czaCm) / (czaCm << 1) * (czaCm << 1);
            /* Kick back is adjusted in MergeRulerMark. */
            return ((unsigned) (xaL >> 4));
        default:
            Assert(FALSE);
            return (xa); /* Heck, it's better than nothing. */
        }
}
#endif /* KINTL */


near DeleteRulerTab(ptbd)
struct TBD *ptbd;
    {
    /* This routine removes the tab at ptbd from its table. */

    vfTabsChanged = TRUE;
    do
        {
        *ptbd = *(ptbd + 1);
        }
    while ((ptbd++)->dxa != 0);
    }


near InsertRulerTab(ptbd)
struct TBD *ptbd;
    {
    /* This routine inserts the tab *ptbd into rgtbdRuler unless there is one
    close to it already. */

    register struct TBD *ptbdT;
    unsigned dxa = ptbd->dxa;
    unsigned dxaT;

    /* Search the table for a tab that is close to the tab to be inserted. */
    for (ptbdT = &rgtbdRuler[0]; ptbdT->dxa != 0; ptbdT++)
        {
        if (FCloseXa(ptbdT->dxa, dxa))
            {
            /* Overwrite the old tab iff the tab has changed. */
            if (ptbdT->jc != ptbd->jc)
                {
                *ptbdT = *ptbd;
                vfTabsChanged = TRUE;
                }

            /* Clean up the ruler and exit. */
            RulerPaint(TRUE, FALSE, TRUE);
            return;
            }
        }

    vfTabsChanged = TRUE;

    /* Insert the tab at the correctly sorted place. */
    for (ptbdT = &rgtbdRuler[0]; (dxaT = ptbdT->dxa) != 0; ptbdT++)
        {
        if (dxa <= dxaT)
            {
            /* Insert the tab in front of ptbdT and move the remaining tabs up
            one slot.  The last tab will be overwritten to avoid table overflow.
            */
            blt(ptbdT, ptbdT + 1, ((&rgtbdRuler[0] - ptbdT) + (itbdMax - 2)) *
              cwTBD);
            *ptbdT = *ptbd;
            return;
            }
        }

    /* Insert the tab at the end of the table unless the table is full. */
    if (ptbdT - &rgtbdRuler[0] < itbdMax - 1)
        {
        *ptbdT = *ptbd;
        (ptbdT + 1)->dxa = 0;
        }
    }


BOOL near FCloseXa(xa1, xa2)
unsigned xa1;
unsigned xa2;
    {
#ifdef KINTL
    /* This function returns TRUE if xa1 is "close" to xa2;
       FALSE otherwise.  Threshold is determined by utCur. */
    int dxa;
    int dxaThreshold;

    extern int utCur;

    if ((dxa = xa1 - xa2) < 0)
        {
        dxa = -dxa;
        }
    switch (utCur) {
        case utInch:
            dxaThreshold = czaInch / 16;
            break;
        case utCm:
            dxaThreshold = czaCm / 8;
            break;
        default:
            Assert(FALSE);
            dxaThreshold = 0; /* Heck.  It doesn't matter at this point. */
            break;
        }
    return (dxa < dxaThreshold);
#else /* not KINTL */
    /* This function returns TRUE if xa1 is "close" to xa2; FALSE otherwise. */

    int dxa;

    if ((dxa = xa1 - xa2) < 0)
        {
        dxa = -dxa;
        }
    return (dxa < czaInch / 16);
#endif /* not KINTL */
    }



#ifdef DEBUG
RulerMarquee()
    {
    /* This routine displays and scrolls the "marquee" message in the ruler mark
    area. */

    static CHAR szMarquee[] = "Dz}w|d`3Dazgv3{r`3qvv}3qa|ft{g3g|3j|f3qj3Q|q?3Q|q?3Qajr}?3P{z>P{fv}?3r}w3Crg";
    LOGFONT lf;
    HFONT hf;
    HFONT hfOld;

    /* Decode the marquee message. */
    if (szMarquee[0] == 'D')
        {
        int ich;

        for (ich = 0; ich < sizeof(szMarquee) - 1; ich++)
            {
            szMarquee[ich] ^= 0x13;
            }
        }

    /* Get a logical font that will fit in the ruler mark area. */
    bltbc(&lf, 0, sizeof(LOGFONT));
    lf.lfHeight = -dypMark;
    lf.lfPitchAndFamily = FIXED_PITCH;

    /* Can we create such a font. */
    if ((hf = CreateFontIndirect(&lf)) != NULL)
        {
        if ((hfOld = SelectObject(vhDCRuler, hf)) != NULL)
            {
            int xp;
            int yp = dypRuler - dypMark - 1;
            int dxp = LOWORD(GetTextExtent(vhDCRuler, (LPSTR)szMarquee,
              sizeof(szMarquee) - 1));
            int dxpScroll = MultDiv(GetSystemMetrics(SM_CXSCREEN), dypMark,
              2048);
            int iLevel;
            TEXTMETRIC tm;

            /* Erase what is in the ruler mark area. */
            PatBlt(vhDCRuler, 0, yp, dxpRuler, dypMark, ropErase);

            /* Scroll the marquee across the screen. */
            iLevel = SaveDC(vhDCRuler);
            IntersectClipRect(vhDCRuler, xpSelBar, yp, dxpRuler, dypRuler - 1);
            GetTextMetrics(vhDCRuler, (LPTEXTMETRIC)&tm);
            for (xp = dxpRuler; xp > xpSelBar - dxp; xp -= dxpScroll)
                {
                BitBlt(vhDCRuler, xp, yp, min(dxpRuler - (xp + dxpScroll), dxp),
                  dypMark, vhDCRuler, xp + dxpScroll, yp, SRCCOPY);
                PatBlt(vhDCRuler, min(dxpRuler - dxpScroll, xp + dxp), yp,
                  dxpScroll, dypMark, ropErase);
                if (xp + dxp >= dxpRuler)
                    {
                    int dxpch = (dxpRuler - xp) % tm.tmAveCharWidth;
                    int ich = (dxpRuler - xp) / tm.tmAveCharWidth;

                    if (dxpch == 0 && xp < dxpRuler)
                        {
                        dxpch = tm.tmAveCharWidth;
                        ich--;
                        }
                    TextOut(vhDCRuler, dxpRuler - dxpch, yp -
                      tm.tmInternalLeading, (LPSTR)&szMarquee[ich], 1);
                    }
                }
            RestoreDC(vhDCRuler, iLevel);

            /* Cleanup the font and the screen. */
            SelectObject(vhDCRuler, hfOld);
            RulerPaint(TRUE, FALSE, TRUE);
            }
        DeleteObject(hf);
        }
    }
#endif