File: Utils.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (1700 lines) | stat: -rw-r--r-- 51,711 bytes parent folder | download | duplicates (6)
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
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
/*
 * Copyright(c) 1992 Bell Communications Research, Inc. (Bellcore)
 * Copyright(c) 1995-99 Andrew Lister
 *                        All rights reserved
 * Permission to use, copy, modify and distribute this material for
 * any purpose and without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies, and that the name of Bellcore not be used in advertising
 * or publicity pertaining to this material without the specific,
 * prior written permission of an authorized representative of
 * Bellcore.
 *
 * BELLCORE MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES, EX-
 * PRESS OR IMPLIED, WITH RESPECT TO THE SOFTWARE, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR ANY PARTICULAR PURPOSE, AND THE WARRANTY AGAINST IN-
 * FRINGEMENT OF PATENTS OR OTHER INTELLECTUAL PROPERTY RIGHTS.  THE
 * SOFTWARE IS PROVIDED "AS IS", AND IN NO EVENT SHALL BELLCORE OR
 * ANY OF ITS AFFILIATES BE LIABLE FOR ANY DAMAGES, INCLUDING ANY
 * LOST PROFITS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES RELAT-
 * ING TO THE SOFTWARE.
 *
 * MatrixWidget Author: Andrew Wason, Bellcore, aw@bae.bellcore.com
 *
 * $Id: Utils.c,v 1.1 2001-07-18 11:06:00 root Exp $
 */

/*
 * Utils.c created by Andrew Lister (7 August, 1995)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <Xm/Xm.h>
#include "MatrixP.h"
#include "Macros.h"
#include "Utils.h"
#include "Actions.h"
#include <Xm/ScrollBar.h>
/*
 * Return the top and bottom-most visible non-fixed row
 */
void
xbaeGetVisibleRows(mw, top_row, bottom_row)
XbaeMatrixWidget mw;
int *top_row, *bottom_row;
{
    *top_row = VERT_ORIGIN(mw) + mw->matrix.fixed_rows;
    *bottom_row = *top_row + (VISIBLE_HEIGHT(mw) - 1) / ROW_HEIGHT(mw);
    SANITY_CHECK_ROW(mw, *bottom_row);
}

/*
 * Return the left and right-most visible non-fixed column
 */
void
xbaeGetVisibleColumns(mw, left_column, right_column)
XbaeMatrixWidget mw;
int *left_column, *right_column;
{
    *left_column = xbaeXtoCol(mw, FIXED_COLUMN_WIDTH(mw) + HORIZ_ORIGIN(mw));
    *right_column = xbaeXtoCol(mw, FIXED_COLUMN_WIDTH(mw) + HORIZ_ORIGIN(mw) +
                               VISIBLE_WIDTH(mw) - 1);
}

/*
 * Return the top and bottom row and left and right column of
 * the visible non-fixed cells
 */
void
xbaeGetVisibleCells(mw, top_row, bottom_row, left_column, right_column)
XbaeMatrixWidget mw;
int *top_row, *bottom_row, *left_column, *right_column;
{
    xbaeGetVisibleRows(mw, top_row, bottom_row);
    xbaeGetVisibleColumns(mw, left_column, right_column);
}

/*
 * Try to make the column specified by the leftColumn resource
 * be the left column. The column is relative to fixed_columns - so 0 would
 * be the first non-fixed column.
 * If we can't make leftColumn the left column, make it as close as possible.
 */
void
xbaeAdjustLeftColumn(mw)
XbaeMatrixWidget mw;
{
    int y;
    int i;
    int required_width;
    int visible_width = VISIBLE_WIDTH(mw);
    int dynamic_columns;

    if (visible_width < 0)        /* will happen on initialisation */
        return;

    /* Adjust the column if it is out of bounds */

    dynamic_columns =  mw->matrix.columns - mw->matrix.fixed_columns -
        mw->matrix.trailing_fixed_columns;

    if (mw->matrix.left_column < 0)
        mw->matrix.left_column = 0;
    else if (mw->matrix.left_column > dynamic_columns-1)
        mw->matrix.left_column =  dynamic_columns-1;

    /* Find out where the horiz_origin will be if we tried setting the
       given left column and adjust till it all fits */
    do
    {
        required_width = 0;
        HORIZ_ORIGIN(mw) = 0;
        xbaeRowColToXY(mw, mw->matrix.fixed_rows,
                       mw->matrix.left_column + mw->matrix.fixed_columns,
                       &mw->matrix.horiz_origin, &y);
        /* Check how much space is remaining */
        for (i = mw->matrix.left_column + mw->matrix.fixed_columns;
             i < mw->matrix.columns -
                 (int)mw->matrix.trailing_fixed_columns; i++)
        {
            required_width += COLUMN_WIDTH(mw, i);
             if (required_width >= visible_width)
                break;
        }
        if (required_width < visible_width)
            mw->matrix.left_column--;
    }
    while (required_width < visible_width);
}

/*
 * Try to make the row specified by the topRow resource (VERT_ORIGIN)
 * be the top row. The row is relative to fixed_rows - so 0 would
 * be the first non-fixed row.
 * If we can't make topRow the top row, make it as close as possible.
 */
void
xbaeAdjustTopRow(mw)
XbaeMatrixWidget mw;
{
    int rows_visible = VISIBLE_HEIGHT(mw) / ROW_HEIGHT(mw);

    /*
     * If we have less than one full row visible, then count it as a full row
     */
    if (rows_visible <= 0)
        rows_visible = 1;
    /*
     * rows_visible might be inaccurate since Clip may not have been resized
     */
    else if (rows_visible > mw->matrix.rows)
        rows_visible = mw->matrix.rows;
    
    if (VERT_ORIGIN(mw) > (int)(mw->matrix.rows - rows_visible -
                                mw->matrix.fixed_rows -
                                mw->matrix.trailing_fixed_rows))
        mw->matrix.top_row = mw->matrix.rows - rows_visible -
            mw->matrix.fixed_rows - mw->matrix.trailing_fixed_rows;
    else if (VERT_ORIGIN(mw) < 0)
        mw->matrix.top_row = 0;
}

/*
 * Utility function to clear a cell so we can draw something new in it.
 * Does not generate expose events on the cell.
 * Does not check if the cell is actually visible before clearing it.
 */
void
xbaeClearCell(mw, row, column)
XbaeMatrixWidget mw;
int row, column;
{
    int x, y;
    Boolean fixed = IS_FIXED(mw, row, column);
    Window win = fixed ? XtWindow(mw) : XtWindow(ClipChild(mw));

    if (!win || mw->matrix.disable_redisplay)
        return;

    xbaeRowColToXY(mw, row, column, &x, &y);

    /*
     * Make sure y coord is valid
     */
#if 0
    if ((win == XtWindow(mw)) &&
        ((y > (int)(CLIP_VERT_VISIBLE_SPACE(mw) +
                    ROW_LABEL_OFFSET(mw) - 1)) ||
         (y < (int)ROW_LABEL_OFFSET(mw))))
        return;
#endif
    XClearArea(XtDisplay(mw), win, x, y, COLUMN_WIDTH(mw, column),
               ROW_HEIGHT(mw), fixed);
}

/*
 * Return True if a row is visible on the screen (not scrolled totally off)
 */
Boolean
xbaeIsRowVisible(mw, row)
XbaeMatrixWidget mw;
int row;
{
    /*
     * If we are not in a fixed row or trailing fixed row,
     * see if we are on the screen vertically
     * (fixed rows are always on the screen)
     */
    if (! IS_FIXED_ROW(mw, row))
    {
        row -= mw->matrix.fixed_rows;
        
        if (row >= VERT_ORIGIN(mw))
        {
            double height = ((double)ClipChild(mw)->core.height /
                             ROW_HEIGHT(mw)) + VERT_ORIGIN(mw);
            if (row < height)
                return True;
            
            if ((int)ClipChild(mw)->core.height >
                (int)TEXT_HEIGHT_OFFSET(mw) &&
                (int)ClipChild(mw)->core.height < ROW_HEIGHT(mw) &&
                row == VERT_ORIGIN(mw))

                return True;
        }
    }
    else
        return True;

    return False;
}

/*
 * Return True if a column is visible on the screen (not scrolled totally off)
 */
Boolean
xbaeIsColumnVisible(mw, column)
XbaeMatrixWidget mw;
int column;
{
    /*
     * If we are not in a fixed column, see if we are on the screen
     * horizontally (fixed columns are always on the screen)
     */
    if (! IS_FIXED_COLUMN(mw, column))
    {
        int x;

        /*
         * Calculate the x endpoints of this column
         */
        x = COLUMN_POSITION(mw, column) -
            COLUMN_POSITION(mw, mw->matrix.fixed_columns);

        /*
         * Check if we are visible horizontally
         */
        if (x + COLUMN_WIDTH(mw, column) > HORIZ_ORIGIN(mw) &&
            x < (int)(ClipChild(mw)->core.width) + HORIZ_ORIGIN(mw))
            return True;
    }
    else
        return True;

    return False;
}

/*
 * Return True if a cell is visible on the screen (not scrolled totally off)
 */
Boolean
xbaeIsCellVisible(mw, row, column)
XbaeMatrixWidget mw;
int row, column;
{
    return xbaeIsRowVisible(mw, row) && xbaeIsColumnVisible(mw, column);
}

/*
 * Scroll a row so it is visible on the screen.
 */
void
xbaeMakeRowVisible(mw, row)
XbaeMatrixWidget mw;
int row;
{
    int rows_visible;
    int value, slider_size, increment, page_increment, vert_value;

    /*
     * If we are in a fixed row, we are already visible.
     */
    if (IS_FIXED_ROW(mw, row))
        return;

    /*
     * Take into account fixed_rows.
     * Calculate the number of rows visible. If less than one full
     * row is visible, use one full row.
     */
    row -= mw->matrix.fixed_rows;
    rows_visible = VISIBLE_HEIGHT(mw) / ROW_HEIGHT(mw);
    if (rows_visible == 0)
        rows_visible = 1;

    /*
     * Figure out the new value of the VSB to scroll this cell
     * onto the screen (the VSB uses row coordinates instead of pixels)
     */
    if (row < VERT_ORIGIN(mw))
        vert_value = row;
    else if (row >= rows_visible + VERT_ORIGIN(mw))
        vert_value = row - rows_visible + 1;
    else
        vert_value = VERT_ORIGIN(mw);

    /*
     * Give the VSB the new value and pass a flag to make it call
     * our scroll callbacks
     */
    if (vert_value != VERT_ORIGIN(mw))
    {
        XmScrollBarGetValues(VertScrollChild(mw), &value,
                             &slider_size, &increment, &page_increment);
        XmScrollBarSetValues(VertScrollChild(mw), vert_value,
                             slider_size, increment, page_increment, True);
    }
}

/*
 * Scroll a column so it is visible on the screen.
 */
void
xbaeMakeColumnVisible(mw, column)
XbaeMatrixWidget mw;
int column;
{
    int value, slider_size, increment, page_increment, x, horiz_value;

    /*
     * If we are in a fixed column, we are already visible.
     */
    if (IS_FIXED_COLUMN(mw, column))
        return;

    /*
     * Calculate the x position of this column
     */
    x = COLUMN_POSITION(mw, column) -
        COLUMN_POSITION(mw, mw->matrix.fixed_columns);

    /*
     * Figure out the new value of the HSB to scroll this cell
     * onto the screen. If the whole cell won't fit, scroll so its
     * left edge is visible.
     */
    if (x < HORIZ_ORIGIN(mw))
        horiz_value = x;
    else if (x + COLUMN_WIDTH(mw, column) >
             VISIBLE_WIDTH(mw) + HORIZ_ORIGIN(mw))
    {
        int off = (x + COLUMN_WIDTH(mw, column)) - (VISIBLE_WIDTH(mw) +
                                                    HORIZ_ORIGIN(mw));

        if (x - off < HORIZ_ORIGIN(mw))
            horiz_value = x;
        else
            horiz_value = HORIZ_ORIGIN(mw) + off;
    }
    else
        horiz_value = HORIZ_ORIGIN(mw);

    /*
     * Give the HSB the new value and pass a flag to make it
     * call our scroll callbacks
     */
    if (horiz_value != HORIZ_ORIGIN(mw))
    {
        XmScrollBarGetValues(HorizScrollChild(mw), &value,
                             &slider_size, &increment, &page_increment);
        XmScrollBarSetValues(HorizScrollChild(mw), horiz_value,
                             slider_size, increment, page_increment, True);
    }
}

/*
 * Scrolls a fixed or non-fixed cell so it is visible on the screen.
 */
void
xbaeMakeCellVisible(mw, row, column)
XbaeMatrixWidget mw;
int row, column;
{
    if (!xbaeIsRowVisible(mw, row))
        xbaeMakeRowVisible(mw, row);
    if (!xbaeIsColumnVisible(mw, column))
        xbaeMakeColumnVisible(mw, column);
}

/*
 * Set the clip_mask in our draw and shadow GCs.  This is necessary for
 * drawing non-fixed column labels and fixed rows.
 */
void
xbaeSetClipMask(mw, clip_reason)
XbaeMatrixWidget mw;
unsigned int clip_reason;
{
    XRectangle r[2];
    int n = 1;
    /*
     * Set new clip reason
     */
    mw->matrix.current_clip = clip_reason;
    
    /*
     * XRectangle enclosing column labels and fixed rows
     */
    if ((CLIP_FIXED_COLUMNS & clip_reason) && mw->matrix.fixed_columns)
    {
        r[0].x = COLUMN_LABEL_OFFSET(mw);
        r[0].width = FIXED_COLUMN_WIDTH(mw);
    }
    else if ((CLIP_TRAILING_FIXED_COLUMNS & clip_reason) &&
             (mw->matrix.trailing_fixed_columns || mw->matrix.fill))
    {
        r[0].x = TRAILING_FIXED_COLUMN_LABEL_OFFSET(mw);
        r[0].width = TRAILING_FIXED_COLUMN_WIDTH(mw);
        if (NEED_HORIZ_FILL(mw))
            r[0].width += FILL_HORIZ_WIDTH(mw);
    }
    else
    {
        r[0].x = FIXED_COLUMN_LABEL_OFFSET(mw);
        r[0].width = ClipChild(mw)->core.width;
        if (NEED_HORIZ_FILL(mw))
            r[0].width += FILL_HORIZ_WIDTH(mw);
    }
        
    if (CLIP_VISIBLE_HEIGHT & clip_reason)
    {
        r[0].y = ROW_LABEL_OFFSET(mw);
        r[0].height = ClipChild(mw)->core.height +
            FIXED_ROW_HEIGHT(mw) + TRAILING_FIXED_ROW_HEIGHT(mw);
        if (NEED_VERT_FILL(mw))
              r[0].height += FILL_VERT_HEIGHT(mw);
    }
    else if ((CLIP_TRAILING_FIXED_ROWS & clip_reason) &&
             (mw->matrix.trailing_fixed_rows || mw->matrix.fill))
    {
         r[0].y = TRAILING_FIXED_ROW_LABEL_OFFSET(mw);
         r[0].height = TRAILING_FIXED_ROW_HEIGHT(mw);
          if (NEED_VERT_FILL(mw))
              r[0].height += FILL_VERT_HEIGHT(mw);
    }
    else if (CLIP_BETWEEN_FIXED_ROWS & clip_reason)
    {
        r[0].y = FIXED_ROW_LABEL_OFFSET(mw);
        r[0].height = ClipChild(mw)->core.height;
    }
    else    /* clip fixed rows & clip_reason */
    {
        r[0].y = HORIZ_SB_OFFSET(mw);
        r[0].height = FIXED_ROW_LABEL_OFFSET(mw);
    }
    if (mw->matrix.row_labels)
    {
        r[1].x = VERT_SB_OFFSET(mw);
        r[1].y = FIXED_ROW_LABEL_OFFSET(mw);
        r[1].width = ROW_LABEL_WIDTH(mw);
        r[1].height = ClipChild(mw)->core.height;
        n = 2;
    }
    /*
     * Reset the clip_mask in our clipping GCs
     */
    XSetClipRectangles(XtDisplay(mw), mw->matrix.cell_grid_line_gc,
                       0, 0, r, n, Unsorted);
    XSetClipRectangles(XtDisplay(mw), mw->matrix.cell_top_shadow_clip_gc,
                       0, 0, r, n, Unsorted);
    XSetClipRectangles(XtDisplay(mw), mw->matrix.cell_bottom_shadow_clip_gc,
                       0, 0, r, n, Unsorted);
    XSetClipRectangles(XtDisplay(mw), mw->matrix.label_clip_gc,
                       0, 0, r, n, Unsorted);
}


/*
 * Get the total pixel width of the non-fixed cell area
 */
void
xbaeGetCellTotalWidth(mw)
XbaeMatrixWidget mw;
{
    int i, columns;

    /*
     * Calculate width of non-fixed cell area.
     */
    columns = TRAILING_HORIZ_ORIGIN(mw);
    for (i = mw->matrix.fixed_columns, mw->matrix.non_fixed_total_width = 0;
         i < columns;
         i++)
        mw->matrix.non_fixed_total_width += COLUMN_WIDTH(mw, i);
}

/*
 * Cache the pixel position of each column
 */
void
xbaeGetColumnPositions(mw)
XbaeMatrixWidget mw;
{
    int i, x;

    for (i = 0, x = 0;
         i < mw->matrix.columns;
         x += COLUMN_WIDTH(mw, i), i++)
        COLUMN_POSITION(mw, i) = x;
}

void
#if NeedFunctionPrototypes
xbaeComputeSize(XbaeMatrixWidget mw, Boolean compute_width,
                Boolean compute_height)
#else
xbaeComputeSize(mw, compute_width, compute_height)
XbaeMatrixWidget mw;
Boolean compute_width;
Boolean compute_height;
#endif
{
    unsigned long full_width = NON_FIXED_TOTAL_WIDTH(mw) +
        FIXED_COLUMN_WIDTH(mw) + TRAILING_FIXED_COLUMN_WIDTH(mw) +
        ROW_LABEL_WIDTH(mw) + 2 * mw->manager.shadow_thickness;
    unsigned long full_height = CELL_TOTAL_HEIGHT(mw) +
        FIXED_ROW_HEIGHT(mw) + TRAILING_FIXED_ROW_HEIGHT(mw) +
        COLUMN_LABEL_HEIGHT(mw) + 2 * mw->manager.shadow_thickness;
    unsigned long width, height;

    /*
     * Calculate our width.
     * If visible_columns is set, then base it on that.
     * Otherwise, if the compute_width flag is set, then we are full width.
     * Otherwise we keep whatever width we are.
     */
    if (mw->matrix.visible_columns)
        width = ROW_LABEL_WIDTH(mw) + 2 * mw->manager.shadow_thickness +
            COLUMN_WIDTH(mw, (mw->matrix.visible_columns +
                              mw->matrix.fixed_columns) - 1) +
            COLUMN_POSITION(mw, mw->matrix.fixed_columns +
                            mw->matrix.visible_columns - 1) +
            TRAILING_FIXED_COLUMN_WIDTH(mw);
    else if (compute_width)
        width = full_width;
    else
        width = mw->core.width;

    /*
     * Calculate our height.
     * If visible_rows is set, then base it on that.
     * Otherwise, if the compute_height flag is set, then we are full height.
     * Otherwise we keep whatever height we are.
     */
    if (mw->matrix.visible_rows)
        height = mw->matrix.visible_rows * ROW_HEIGHT(mw) +
            TRAILING_FIXED_ROW_HEIGHT(mw) + FIXED_ROW_HEIGHT(mw) +
            COLUMN_LABEL_HEIGHT(mw) + 2 * mw->manager.shadow_thickness;
    else if (compute_height)
        height = full_height;
    else
        height = mw->core.height;

    /*
     * Store our calculated size.
     */
    mw->core.width = width;
    mw->core.height = height;

    /*
     * If we are less than full width or our horizontal display policy is
     * constant, then we need an HSB, so increment our height by the size
     * of the HSB (if we are allowed to modify our height and we are allowed
     * to have an HSB).
     */
    if (((width < full_width) ||
         (XmDISPLAY_STATIC == mw->matrix.hsb_display_policy)) &&
        (compute_height || mw->matrix.visible_rows) &&
        (XmDISPLAY_NONE != mw->matrix.hsb_display_policy))
        mw->core.height += HORIZ_SB_HEIGHT(mw);

    /*
     * If we are less than full height or our vertical display policy is
     * constant, then we need a VSB, so increment our width by the size
     * of the VSB (if we are allowed to modify our width and we are allowed
     * to have a VSB).
     */
    if (((height < full_height) ||
         (XmDISPLAY_STATIC == mw->matrix.vsb_display_policy)) &&
        (compute_width || mw->matrix.visible_columns) &&
        (XmDISPLAY_NONE != mw->matrix.vsb_display_policy))
        mw->core.width += VERT_SB_WIDTH(mw);

    /*
     * Save our calculated size for use in our query_geometry method.
     * This is the size we really want to be (not necessarily the size
     * we will end up being).
     */
    mw->matrix.desired_width = mw->core.width;
    mw->matrix.desired_height = mw->core.height;
}

/*
 * Return the length of the longest row label
 */
short
xbaeMaxRowLabel(mw)
XbaeMatrixWidget mw;
{
    int i;
    short max = 0, len;

    /*
     * Determine the length of the longest row label
     */
    for (i = 0; i < mw->matrix.rows; i++)
    {
        len = strlen(mw->matrix.row_labels[i]);
        if (len > max)
            max = len;
    }
    return max;
}

void
xbaeParseColumnLabel(label, lines)
String label;
ColumnLabelLines lines;
{
    char *nl;

    /*
     * First count the number of lines in the label
     */
    lines->lines = 1;
    nl = label;
    while ((nl = strchr(nl, '\n')) != NULL)
    {
        nl++;
        lines->lines++;
    }

    /*
     * Now malloc a lengths array of the correct size.
     */
    lines->lengths = (int *) XtMalloc(lines->lines * sizeof(int));

    /*
     * An entry in the lengths array is the length of that line (substring).
     */

    /*
     * Handle the case of one line (no strchr() needed)
     */
    if (lines->lines == 1)
        lines->lengths[0] = strlen(label);
    else
    {
        int i;

        nl = label;
        i = 0;
        while ((nl = strchr(nl, '\n')) != NULL)
        {
            lines->lengths[i] = nl - label;
            nl++;
            label = nl;
            i++;
        }
        lines->lengths[i] = strlen(label);
    }
}

/*
 * Convert an x/y pixel position to the row/column cell position it picks.
 * 'cell' specifies whether the x/y coord is relative to the fixed cells
 * window or the non-fixed cells window.
 * The coords x,y are adjusted so they are relative to the origin of the
 * picked cell.
 * If we are "out of bounds" on the ``low'' side, go ahead and return False
 * to show an error, but also set the row/column to -1 to indicate this could
 * be a row/column header.  This is currently undocumented behaviour, but
 * may be useful nevertheless.
 */
Boolean
xbaeXYToRowCol(mw, x, y, row, column, cell)
XbaeMatrixWidget mw;
int *x, *y;
int *row, *column;
CellType cell;
{
    Rectangle rect;
    unsigned int inBox = CLIP_NONE;
    Boolean need_vert_dead_space_fill = NEED_VERT_DEAD_SPACE_FILL(mw);
    int horiz_sb_offset = HORIZ_SB_OFFSET(mw);
    int vert_sb_offset = VERT_SB_OFFSET(mw);
    int column_label_height = COLUMN_LABEL_HEIGHT(mw);
    int row_label_offset = ROW_LABEL_OFFSET(mw);
    int row_label_width = ROW_LABEL_WIDTH(mw);
    int fixed_row_label_offset = FIXED_ROW_LABEL_OFFSET(mw);
    int trailing_fixed_row_label_offset = TRAILING_FIXED_ROW_LABEL_OFFSET(mw);
    int trailing_fixed_row_height = TRAILING_FIXED_ROW_HEIGHT(mw);
    int fixed_column_label_offset = FIXED_COLUMN_LABEL_OFFSET(mw);
    int trailing_fixed_column_label_offset =
        TRAILING_FIXED_COLUMN_LABEL_OFFSET(mw);
    int trailing_fixed_column_width = TRAILING_FIXED_COLUMN_WIDTH(mw);
    int column_label_offset = COLUMN_LABEL_OFFSET(mw);
    int row_height = ROW_HEIGHT(mw);
    int unattached_trailing_rows_offset = UNATTACHED_TRAILING_ROWS_OFFSET(mw);
    
    *row = -1;
    *column = -1;

    switch (cell)
    {
    case FixedCell:
        /*
         * Check the various rectangles enclosing the cells in fixed rows
         * or columns.
         *
         * If we don't have fixed rows or columns, then we didn't hit a cell.
         */

        /*
         * Upper left
         */
        if (!inBox && mw->matrix.fixed_columns && mw->matrix.fixed_rows)
        {
            SETRECT(rect,
                    column_label_offset, row_label_offset,
                    fixed_column_label_offset - 1,
                    fixed_row_label_offset - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_FIXED_COLUMNS |
                                         CLIP_FIXED_ROWS;
        }

        /*
         * Lower left
         */
        if (!inBox && mw->matrix.fixed_columns &&
            mw->matrix.trailing_fixed_rows)
        {
            SETRECT(rect,
                    column_label_offset,
                    trailing_fixed_row_label_offset,
                    fixed_column_label_offset - 1,
                    trailing_fixed_row_label_offset +
                    trailing_fixed_row_height - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_FIXED_COLUMNS |
                                         CLIP_TRAILING_FIXED_ROWS;
        }

        /*
         * Upper right
         */
        if (!inBox && mw->matrix.trailing_fixed_columns &&
            mw->matrix.fixed_rows)
        {
            SETRECT(rect,
                    trailing_fixed_column_label_offset,
                    row_label_offset,
                    trailing_fixed_column_label_offset +
                    trailing_fixed_column_width - 1,
                    fixed_row_label_offset - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_FIXED_COLUMNS |
                                         CLIP_FIXED_ROWS;
        }

        /*
         * Lower right
         */
        if (!inBox && mw->matrix.trailing_fixed_columns &&
            mw->matrix.trailing_fixed_rows)
        {
            SETRECT(rect,
                    trailing_fixed_column_label_offset,
                    trailing_fixed_row_label_offset,
                    trailing_fixed_column_label_offset +
                    trailing_fixed_column_width - 1,
                    trailing_fixed_row_label_offset +
                    trailing_fixed_row_height - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_FIXED_COLUMNS |
                                         CLIP_TRAILING_FIXED_ROWS;
        }

        /*
         * Right (mid)
         */
        if (!inBox && mw->matrix.fixed_columns)
        {
            SETRECT(rect,
                    column_label_offset, fixed_row_label_offset,
                    fixed_column_label_offset - 1,
                    need_vert_dead_space_fill ?
                    unattached_trailing_rows_offset :
                    trailing_fixed_row_label_offset - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_FIXED_COLUMNS;
        }

        /*
         * Upper (mid)
         */
        if (!inBox && mw->matrix.fixed_rows)
        {
            SETRECT(rect,
                    fixed_column_label_offset, row_label_offset,
                    trailing_fixed_column_label_offset - 1,
                    fixed_row_label_offset - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_FIXED_ROWS;
        }

        /*
         * Left (mid)
         */
        if (!inBox && mw->matrix.trailing_fixed_columns)
        {
            SETRECT(rect,
                    trailing_fixed_column_label_offset,
                    fixed_row_label_offset,
                    trailing_fixed_column_label_offset +
                    trailing_fixed_column_width - 1,
                    need_vert_dead_space_fill ?
                    unattached_trailing_rows_offset :
                    trailing_fixed_row_label_offset - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_FIXED_COLUMNS;
        }

        /*
         * Lower (mid)
         */
        if (!inBox && mw->matrix.trailing_fixed_rows)
        {
            SETRECT(rect,
                    fixed_column_label_offset,
                    trailing_fixed_row_label_offset,
                    trailing_fixed_column_label_offset - 1,
                    trailing_fixed_row_label_offset +
                    trailing_fixed_row_height - 1);

            if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_FIXED_ROWS;
        }

        /*
         * Trailing horizontal fill. This is trickier because
         * even though we're in the fixed cell section, this really
         * might not be a fixed cell.
         */
        if (!inBox && IN_GRID_ROW_MODE(mw) && NEED_HORIZ_FILL(mw))
        {
            int rx = trailing_fixed_column_label_offset +
                trailing_fixed_column_width;

            /*
             * Upper fill
             */
            if (!inBox && mw->matrix.fixed_rows)
            {
                SETRECT(rect,
                        rx, row_label_offset,
                        rx + FILL_HORIZ_WIDTH(mw) - 1,
                        fixed_row_label_offset - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_HORIZ_FILL |
                                             CLIP_FIXED_ROWS;
            }

            /*
             * Lower fill
             */
            if (!inBox && mw->matrix.trailing_fixed_rows)
            {
                SETRECT(rect,
                        rx, trailing_fixed_row_label_offset,
                        rx + FILL_HORIZ_WIDTH(mw) - 1,
                        trailing_fixed_row_label_offset +
                        trailing_fixed_row_height - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_HORIZ_FILL |
                                             CLIP_TRAILING_FIXED_ROWS;
            }

            /*
             * Mid fill
             */
            if (!inBox)
            {
                SETRECT(rect,
                        rx, row_label_offset,
                        rx + FILL_HORIZ_WIDTH(mw) - 1,
                        trailing_fixed_row_label_offset +
                        trailing_fixed_row_height - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_HORIZ_FILL;
            }
        }

        /*
         * Trailing vertical fill
         */
        if (!inBox && IN_GRID_COLUMN_MODE(mw) && NEED_VERT_FILL(mw))
        {
            int ry = trailing_fixed_row_label_offset +
                trailing_fixed_row_height;

            /*
             * Left fill
             */
            if (mw->matrix.fixed_columns)
            {
                SETRECT(rect,
                        column_label_offset, ry,
                        fixed_column_label_offset - 1,
                        ry + FILL_VERT_HEIGHT(mw) - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_VERT_FILL |
                                             CLIP_FIXED_COLUMNS;
            }

            /*
             * Right fill
             */
            if (!inBox && mw->matrix.trailing_fixed_columns)
            {
                SETRECT(rect,
                        trailing_fixed_column_label_offset, ry,
                        trailing_fixed_column_label_offset +
                        trailing_fixed_column_width - 1,
                        ry + FILL_VERT_HEIGHT(mw) - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_VERT_FILL |
                                             CLIP_TRAILING_FIXED_COLUMNS;
            }

            /*
             * Mid fill
             */
            if (!inBox)
            {
                SETRECT(rect,
                        column_label_offset, ry,
                        trailing_fixed_column_label_offset +
                        trailing_fixed_column_width - 1,
                        ry + FILL_VERT_HEIGHT(mw) - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_TRAILING_VERT_FILL;
            }
        }

        /*
         * If the point is in this rectangle, calculate the row/column
         * it hits. Otherwise we didn't hit a cell.
         */
        if (inBox)
        {
            /*
             * Translate the point to rect's coord system
             */
            if (mw->matrix.fixed_columns &&
                (CLIP_TRAILING_FIXED_ROWS == inBox ||
                 CLIP_FIXED_ROWS == inBox))
                *x -= column_label_offset;
            else
                *x -= rect.x1;
            *y -= rect.y1;

            /*
             * Convert this point to a row/column.  We need to take into
             * account the scrolling origins depending on which fixed
             * areas the point is located in.
             */
            if (CLIP_TRAILING_VERT_FILL & inBox)
            {
                *row = mw->matrix.rows - 1;
                *y += row_height;
            }
            else
                *row = YtoRow(mw, *y) +
                    ((((CLIP_FIXED_COLUMNS & inBox) ||
                       (CLIP_TRAILING_FIXED_COLUMNS & inBox))) &&
                     !((CLIP_FIXED_ROWS & inBox) ||
                       (CLIP_TRAILING_FIXED_ROWS & inBox))
                     ? mw->matrix.fixed_rows : 0) +
                    ((CLIP_FIXED_ROWS & inBox) ? 0 : VERT_ORIGIN(mw)) +
                    ((CLIP_TRAILING_FIXED_ROWS & inBox) ?
                     TRAILING_VERT_ORIGIN(mw) - VERT_ORIGIN(mw) : 0);

            if ((CLIP_TRAILING_FIXED_COLUMNS & inBox) ||
                (CLIP_TRAILING_HORIZ_FILL & inBox))
                *column = xbaeXtoTrailingCol(mw, *x);
            else
                *column = xbaeXtoCol(
                    mw, *x + ((CLIP_FIXED_COLUMNS & inBox) ? 0 :
                              HORIZ_ORIGIN(mw)));

            /*
             * Sanity check the result, making sure it is in fixed location
             */
            if (((*row < 0) || (*column < 0)) ||
                (((mw->matrix.fixed_rows &&
                   (*row >= (int)mw->matrix.fixed_rows) &&
                   (!mw->matrix.trailing_fixed_rows ||
                    (mw->matrix.trailing_fixed_rows &&
                     (*row < TRAILING_VERT_ORIGIN(mw))))) ||
                  (!mw->matrix.fixed_rows &&
                   mw->matrix.trailing_fixed_rows &&
                   (*row < TRAILING_VERT_ORIGIN(mw)))) &&
                 ((mw->matrix.fixed_columns &&
                   (*column >= (int)mw->matrix.fixed_columns) &&
                   (!mw->matrix.trailing_fixed_columns ||
                    (mw->matrix.trailing_fixed_columns &&
                     (*column < TRAILING_HORIZ_ORIGIN(mw))))) ||
                  (!mw->matrix.fixed_columns &&
                   mw->matrix.trailing_fixed_columns &&
                   (*column < TRAILING_HORIZ_ORIGIN(mw))))))
                return False;

            /*
             * Adjust x,y so they are relative to this cells origin.
             */
            if (CLIP_TRAILING_HORIZ_FILL & inBox)
                *x += COLUMN_WIDTH(mw, *column);
            else
                *x -= COLUMN_POSITION(mw, *column) -
                    (mw->matrix.fixed_columns ||
                     (CLIP_TRAILING_FIXED_COLUMNS & inBox) ?
                     0 : HORIZ_ORIGIN(mw)) -
                    ((CLIP_TRAILING_FIXED_COLUMNS & inBox) ?
                     COLUMN_POSITION(mw, TRAILING_HORIZ_ORIGIN(mw)) : 0);
            if (!(CLIP_TRAILING_VERT_FILL & inBox))
                *y %= row_height;

            return True;
        }
        else
            return False;

        /* NOTREACHED */
        break;

    case RowLabelCell:
        if (!inBox && mw->matrix.row_labels)
        {
            /* Check the various regions of the matrix to make sure we
               get the correct row label */
            if (mw->matrix.fixed_rows)
            {
                SETRECT(rect,
                        vert_sb_offset, row_label_offset,
                        row_label_width + vert_sb_offset,
                        fixed_row_label_offset - 1);
                if (INBOX(rect, *x, *y)) inBox = CLIP_ROW_LABELS |
                                             CLIP_FIXED_ROWS;
            }
            
            if (!inBox && mw->matrix.trailing_fixed_rows)
            {
                SETRECT(rect,
                        vert_sb_offset,
                        trailing_fixed_row_label_offset,
                        row_label_width + vert_sb_offset,
                        trailing_fixed_row_label_offset +
                        trailing_fixed_row_height - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_ROW_LABELS |
                                             CLIP_TRAILING_FIXED_ROWS;
            }

            /* check the rows in the non fixed regions */
            if (!inBox)
            {
                SETRECT(rect,
                        vert_sb_offset,
                        fixed_row_label_offset,
                        row_label_width + vert_sb_offset,
                        trailing_fixed_row_label_offset - 1);

                if (INBOX(rect, *x, *y)) inBox = CLIP_ROW_LABELS;
            }
        }            
        
        if (CLIP_ROW_LABELS & inBox)
        {
            *y -= row_label_offset;
            *row = YtoRow(mw, *y);
            *row += (CLIP_FIXED_ROWS & inBox ? 0 : mw->matrix.top_row);
            *row += ((CLIP_TRAILING_FIXED_ROWS & inBox) ?
                     (CELL_TOTAL_HEIGHT(mw) - VISIBLE_HEIGHT(mw)) /
                     row_height - mw->matrix.top_row: 0);
                        
            *column = -1;
        }
        else
        {
            *row = -1;
            *column = -1;
        }

        /* Sanity check - make sure we don't over step the mark */
        if (*row >= mw->matrix.rows || *column >= mw->matrix.columns)
        {
            *row = -1;
            *column = -1;
        }

        return False;

        /*NOTREACHED*/
        break;

    case ColumnLabelCell:
        if (!inBox && mw->matrix.column_labels)
        {
            if (mw->matrix.fixed_columns)
            {
                SETRECT(rect,
                        column_label_offset, horiz_sb_offset,
                        fixed_column_label_offset - 1,
                        column_label_height + horiz_sb_offset);

                if (INBOX(rect, *x, *y)) inBox = CLIP_FIXED_COLUMNS |
                                             CLIP_COLUMN_LABELS;
            }
            if (!inBox && mw->matrix.trailing_fixed_columns)
            {
                SETRECT(rect,
                        trailing_fixed_column_label_offset,
                        horiz_sb_offset,
                        trailing_fixed_column_label_offset +
                        trailing_fixed_column_width - 1,
                        column_label_height + horiz_sb_offset);

                if (INBOX(rect, *x, *y)) inBox = CLIP_COLUMN_LABELS |
                                             CLIP_TRAILING_FIXED_COLUMNS;
            }
            /* check the columns in the non fixed regions */
            if (!inBox)
            {
                SETRECT(rect,
                        fixed_column_label_offset, horiz_sb_offset,
                        trailing_fixed_column_label_offset,
                        column_label_height + horiz_sb_offset);

                if (INBOX(rect, *x, *y)) inBox = CLIP_COLUMN_LABELS;
            }
        }
        
        if (CLIP_COLUMN_LABELS & inBox)
        {
            *x -= column_label_offset;

            if (CLIP_TRAILING_FIXED_COLUMNS & inBox)
                *column = xbaeXtoTrailingCol(mw, *x);
            else
                *column = xbaeXtoCol(mw, *x +
                                     ((CLIP_FIXED_COLUMNS & inBox) ? 0 :
                                      HORIZ_ORIGIN(mw)));            
            *row = -1;
        }
        else
        {
            *row = -1;
            *column = -1;
        }
        /* Sanity check - make sure we don't overstep the mark */
        if (*row >= mw->matrix.rows || *column >= mw->matrix.columns)
        {
            *row = -1;
            *column = -1;
        }

        return False;

        /* NOTREACHED */
        break;

    case NonFixedCell:

        /*
         * Translate the point to take into account fixed rows or columns.
         */
        *x += FIXED_COLUMN_WIDTH(mw);
        *y += FIXED_ROW_HEIGHT(mw);

        /*
         * Convert the new point to a row/column position
         */
        *row = YtoRow(mw, *y + mw->matrix.first_row_offset) + VERT_ORIGIN(mw);
        *column = xbaeXtoCol(mw, *x + HORIZ_ORIGIN(mw));

        /*
         * Sanity check the result
         */
        if (*row >= TRAILING_VERT_ORIGIN(mw) ||
            *column >= TRAILING_HORIZ_ORIGIN(mw) ||
            *row < 0 || *column < 0)
            return False;

        /*
         * Adjust x,y so they are relative to this cell's origin.
         */
        *x -= COLUMN_POSITION(mw, *column) - HORIZ_ORIGIN(mw);
        *y %= row_height;

        return True;

        /* NOTREACHED */
        break;

    default:
        *row = -1;
        *column = -1;
        return False;
    }
}

/*
 * Convert the coordinates in an event to be relative to the Clip
 * window or the Matrix window.  Set the cell to indicate which one.
 * Used by some actions.
 */
/* ARGSUSED */
Boolean
xbaeEventToXY(mw, event, x, y, cell)
XbaeMatrixWidget mw;
XEvent *event;
int *x, *y;
CellType *cell;
{
    switch (event->type)
    {
    case ButtonPress:
    case ButtonRelease:
        *x = event->xbutton.x;
        *y = event->xbutton.y;
        break;
    case KeyPress:
    case KeyRelease:
        *x = event->xkey.x;
        *y = event->xkey.y;
        break;
    case MotionNotify:
        *x = event->xmotion.x;
        *y = event->xmotion.y;
        break;
    default:
        return False;
    }

    if (event->xbutton.subwindow == XtWindow(ClipChild(mw)))
    {
        *cell = NonFixedCell;
        *x -= FIXED_COLUMN_LABEL_OFFSET(mw);
        *y -= FIXED_ROW_LABEL_OFFSET(mw);
    }
    else if (event->xbutton.window == XtWindow(mw))
        if (*x < (int)COLUMN_LABEL_OFFSET(mw) &&
            *x > (int)VERT_SB_OFFSET(mw))
            *cell = RowLabelCell;
        else if (*y < (int)ROW_LABEL_OFFSET(mw) &&
                 *y > (int)HORIZ_SB_OFFSET(mw))
            *cell = ColumnLabelCell;
        else
            *cell = FixedCell;
    else if (event->xbutton.window == XtWindow(ClipChild(mw)))
        *cell = NonFixedCell;
    else if (event->xbutton.window == XtWindow(TextChild(mw)))
    {
        Position tx, ty;

        if (mw->matrix.current_parent == ClipChild(mw))
            *cell = NonFixedCell;
        else if (mw->matrix.current_parent == (Widget)mw)
            *cell = FixedCell;
        else                        /* We're on one of the extra clips */
        {
            *cell = FixedCell;
            *x += mw->matrix.current_parent->core.x;
            *y += mw->matrix.current_parent->core.y;
        }

        XtVaGetValues(TextChild(mw),
                      XmNx, &tx,
                      XmNy, &ty,
                      NULL);
        *x += tx;
        *y += ty;
    }
    else
        return False;

    return True;
}

/*
 * Convert a pixel position to the column it is contained in.
 */
int
xbaeXtoCol(mw, x)
XbaeMatrixWidget mw;
int x;
{
    int i;

    for (i = 0; i < mw->matrix.columns; i++)
        if (COLUMN_POSITION(mw, i) > x)
            return i - 1;
    /*
     * I have seen cases where this function returned mw->matrix.columns
     * causing a crash as array bounds are read - AL
     */
    if (i > mw->matrix.columns)
        return mw->matrix.columns - 1;

    return i - 1;
}

/*
 * Convert a pixel position to the trailing
 * fixed column it is contained in
 */
int
xbaeXtoTrailingCol(mw, x)
XbaeMatrixWidget mw;
int x;
{
    int i;

    x += COLUMN_POSITION(mw, TRAILING_HORIZ_ORIGIN(mw));
    for (i = TRAILING_HORIZ_ORIGIN(mw); i < mw->matrix.columns; i++)
    {
        if (COLUMN_POSITION(mw, i) > x)
            return i - 1;
    }

    return i - 1;
}

/*
 * Convert a row/column cell position to the x/y of its upper left corner
 * wrt the window it will be drawn in (either the matrix window for
 * fixed cells, or the clip window for non-fixed).
 */
void
xbaeRowColToXY(mw, row, column, x, y)
XbaeMatrixWidget mw;
int row;
int column;
int *x;
int *y;
{
    /*
     * If we are in a fixed cell, calculate x/y relative to Matrixs
     * window (take into account labels etc)
     */
    if (IS_FIXED(mw, row, column))
    {
        /*
         * Ignore horiz_origin if we are in a fixed column
         */
        if (IS_LEADING_FIXED_COLUMN(mw, column))
        {
            if (IS_FIXED_ROW(mw, row))
                *x = COLUMN_LABEL_OFFSET(mw) + COLUMN_POSITION(mw, column);
            else
                *x = COLUMN_POSITION(mw, column);        /* LeftClip */
        }
        else if (IS_TRAILING_FIXED_COLUMN(mw, column))
        {
            int m;
            if (IS_FIXED_ROW(mw, row))
                *x = TRAILING_FIXED_COLUMN_LABEL_OFFSET(mw);
            else
                *x = 0;                                        /* RightClip */
            for (m = TRAILING_HORIZ_ORIGIN(mw); m < column; m++)
                *x += COLUMN_WIDTH(mw, m);
        }
        else if (IS_FIXED_ROW(mw, row))
            *x = (COLUMN_POSITION(mw, column) -
                  COLUMN_POSITION(mw, mw->matrix.fixed_columns)) -
                HORIZ_ORIGIN(mw);
        else
            *x = COLUMN_LABEL_OFFSET(mw) +
                COLUMN_POSITION(mw, column) - HORIZ_ORIGIN(mw);

        /*
         * Ignore vert_origin if we are in a fixed row
         */
        if (IS_LEADING_FIXED_ROW(mw, row))
        {
            if (IS_FIXED_COLUMN(mw, column))
                *y = ROW_LABEL_OFFSET(mw) + ROW_HEIGHT(mw) * row;
            else
                *y = ROW_HEIGHT(mw) * row;        /* TopClip */
        }
        else if (IS_TRAILING_FIXED_ROW(mw, row))
        {
            int m;
            if (IS_FIXED_COLUMN(mw, column))
                *y = TRAILING_FIXED_ROW_LABEL_OFFSET(mw);
            else
                *y = 0;                                /* BottomClip */
            for (m = TRAILING_VERT_ORIGIN(mw); m < row; m++)
                *y += ROW_HEIGHT(mw);
        }
        else if (IS_FIXED_COLUMN(mw, column))

            *y = ROW_HEIGHT(mw) * ((row - (int)mw->matrix.fixed_rows) -
                                   VERT_ORIGIN(mw));
        else
            *y = ROW_LABEL_OFFSET(mw) + ROW_HEIGHT(mw) *
                (row - VERT_ORIGIN(mw));
    }

    /*
     * If we are not fixed we must account for fixed rows/columns
     * and scrolling origins.
     */
    else
    {
        *x = (COLUMN_POSITION(mw, column) -
              COLUMN_POSITION(mw, mw->matrix.fixed_columns)) -
            HORIZ_ORIGIN(mw);
        *y = ROW_HEIGHT(mw) * ((row - (int) mw->matrix.fixed_rows) -
                               VERT_ORIGIN(mw));
        *y -= mw->matrix.first_row_offset;
    }
}

#define FIXED_NONE        0
#define FIXED_LEFT        1
#define FIXED_RIGHT        2
#define FIXED_TOP        4
#define FIXED_BOTTOM        8
#define FIXED_TOPLEFT        5
#define FIXED_TOPRIGHT        6
#define FIXED_BOTLEFT        9
#define FIXED_BOTRIGHT        10
/* Gotta love that numbering scheme! - AL */


/*
 * Returns the window on which a cell is displayed, i.e. the matrix, the clip,
 * or one of the extra clips which handle the fixed row/col cells.
 */
Window
xbaeGetCellWindow(mw, w, row, column)
XbaeMatrixWidget mw;
Widget *w;
int row, column;
{
    int posn;
    Window win;

    if (IS_LEADING_FIXED_ROW(mw, row))
        posn = FIXED_TOP;
    else if (IS_TRAILING_FIXED_ROW(mw, row))
        posn = FIXED_BOTTOM;
    else
        posn = FIXED_NONE;
    if (IS_LEADING_FIXED_COLUMN(mw, column))
        posn += FIXED_LEFT;
    else if (IS_TRAILING_FIXED_COLUMN(mw, column))
        posn += FIXED_RIGHT;
    else
        posn += FIXED_NONE;        /* add zero!? */

    switch(posn)
    {
    case FIXED_TOPLEFT:
    case FIXED_TOPRIGHT:
    case FIXED_BOTLEFT:
    case FIXED_BOTRIGHT:
        /* total fixed cell - on parent matrix window */
        *w = (Widget)mw;
        win = XtWindow(mw);
        break;
        
    case FIXED_NONE:
        /* not fixed at all - on clip child */
        *w = ClipChild(mw);
        win = XtWindow(ClipChild(mw));
        break;

    case FIXED_LEFT:
        /* fixed col only - on left clip */
        *w = LeftClip(mw);
        win = XtWindow(LeftClip(mw));
        break;
            
    case FIXED_RIGHT:
        /* fixed trailing col only - on right clip */
        win = XtWindow(RightClip(mw));
        *w = RightClip(mw);
        break;

    case FIXED_TOP:
        /* fixed row only - on top clip */
        win = XtWindow(TopClip(mw));
        *w = TopClip(mw);
        break;

    case FIXED_BOTTOM:
        /* fixed trailing row only - on bottom clip */
        win = XtWindow(BottomClip(mw));
        *w = BottomClip(mw);
        break;
    default:        /* bogus */
        win = (Window)NULL;
        *w = (Widget)NULL;
    }
    return win;
}

void
xbaeCalcVertFill(mw, win, x, y, row, column, ax, ay, width, height)
XbaeMatrixWidget mw;
Window win;
int x;
int y;
int row;
int column;
int *ax;
int *ay;
int *width;
int *height;
{
    *ax = x;
    *width = COLUMN_WIDTH(mw, column);

    if (win == XtWindow(LeftClip(mw)))
    {
        *ax += COLUMN_LABEL_OFFSET(mw);
        *ay = LeftClip(mw)->core.y + LeftClip(mw)->core.height;
    }
    else if (win == XtWindow(RightClip(mw)))
    {
        *ax += TRAILING_FIXED_COLUMN_LABEL_OFFSET(mw);
        *ay = RightClip(mw)->core.y + RightClip(mw)->core.height;
    }
    else if (win == XtWindow(BottomClip(mw)))
    {
        *ax += BottomClip(mw)->core.x;
        *ay = BottomClip(mw)->core.y + BottomClip(mw)->core.height; 
    }
    else if (win == XtWindow(ClipChild(mw)))
    {
        if (XtIsManaged(LeftClip(mw)))
            *ax += FIXED_COLUMN_LABEL_OFFSET(mw);
        else
            *ax += COLUMN_LABEL_OFFSET(mw);
        *ay = ClipChild(mw)->core.y + ClipChild(mw)->core.height;
        if (*ax < (int)COLUMN_LABEL_OFFSET(mw))
        {
            *width += *ax - COLUMN_LABEL_OFFSET(mw);
            *ax = COLUMN_LABEL_OFFSET(mw);
        }
    }
    else                        /* must be in a corner */
        *ay = y;

    *height = MATRIX_VERT_VISIBLE_SPACE(mw) + ROW_LABEL_OFFSET(mw) +
        HORIZ_SB_OFFSET(mw) - *ay;
    /*
     * Unfortunately, on the filled area, we don't have the luxury
     * of the clip widgets to help us out with the edges of the area.
     * Check our width isn't going to draw outside the left or right clip
     */

    if (! IS_FIXED_COLUMN(mw, column))
    {
        if (XtIsManaged(LeftClip(mw)) &&
            *ax < (int)FIXED_COLUMN_LABEL_OFFSET(mw))
        {
            *width -= (FIXED_COLUMN_LABEL_OFFSET(mw) - *ax);
            *ax = FIXED_COLUMN_LABEL_OFFSET(mw);
        }

        if (XtIsManaged(RightClip(mw)) &&
            ((*ax + *width) > (int)RightClip(mw)->core.x))
            *width = RightClip(mw)->core.x - *ax;

        if (win == XtWindow(BottomClip(mw)))
        {
            if ((*ax + *width) >
                 (int)(BottomClip(mw)->core.x + BottomClip(mw)->core.width))
                *width = BottomClip(mw)->core.width +
                    BottomClip(mw)->core.x - *ax;

            if (*ax < (int)COLUMN_LABEL_OFFSET(mw))
            {
                *width += *ax - COLUMN_LABEL_OFFSET(mw);
                *ax = COLUMN_LABEL_OFFSET(mw);
            }
        }

        if ((win == XtWindow(ClipChild(mw))) &&
            ((*ax + *width) >
              (int)(ClipChild(mw)->core.x + ClipChild(mw)->core.width)))
            *width = ClipChild(mw)->core.width +
                ClipChild(mw)->core.x - *ax;
    }
}
    
void
xbaeCalcHorizFill(mw, win, x, y, row, column, ax, ay, width, height)
XbaeMatrixWidget mw;
Window win;
int x;
int y;
int row;
int column;
int *ax;
int *ay;
int *width;
int *height;
{
    *ay = y;
    *height = ROW_HEIGHT(mw);

    if (win == XtWindow(TopClip(mw)))
    {
        *ax = TopClip(mw)->core.x + TopClip(mw)->core.width;
        *ay += ROW_LABEL_OFFSET(mw);
    }
    else if (win == XtWindow(BottomClip(mw)))
    {
        *ax = BottomClip(mw)->core.x + BottomClip(mw)->core.width;
        *ay += TRAILING_FIXED_ROW_LABEL_OFFSET(mw);
    }
    else if (win == XtWindow(RightClip(mw)))
    {
        *ax = RightClip(mw)->core.x + RightClip(mw)->core.width; 
        *ay += RightClip(mw)->core.y;
    }
    else if (win == XtWindow(ClipChild(mw)))
    {
        if (XtIsManaged(TopClip(mw)))
            *ay += FIXED_ROW_LABEL_OFFSET(mw);
        else
            *ay += ROW_LABEL_OFFSET(mw);
        *ax = ClipChild(mw)->core.x + ClipChild(mw)->core.width;
        if (*ay < (int)ROW_LABEL_OFFSET(mw))
            *ay = ROW_LABEL_OFFSET(mw);
    }
    else                        /* must be in a corner */
        *ax = x;

    *width = MATRIX_HORIZ_VISIBLE_SPACE(mw) + COLUMN_LABEL_OFFSET(mw) +
        VERT_SB_OFFSET(mw) - *ax;

    /*
     * Unfortunately, on the filled area, we don't have the luxury
     * of the clip widgets to help us out with the edges of the area.
     * Check our width isn't going to draw outside the left or right clip,
     * or out past our matrix region.
     */

    if (! IS_FIXED_ROW(mw, row))
    {
        if (XtIsManaged(LeftClip(mw)) &&
            (*ay < (int)FIXED_ROW_LABEL_OFFSET(mw)))
        {
            *height -= (FIXED_ROW_LABEL_OFFSET(mw) - *ay);
            *ay = FIXED_ROW_LABEL_OFFSET(mw);
        }

        if (XtIsManaged(RightClip(mw)) &&
            ((*ay + *height) >
              (int)(RightClip(mw)->core.y + RightClip(mw)->core.height)))
            *height = RightClip(mw)->core.height +
                RightClip(mw)->core.y - *ay;

        if ((win == XtWindow(ClipChild(mw))) &&
            ((*ay + *height) >
              (int)(ClipChild(mw)->core.y + ClipChild(mw)->core.height)))
            *height = ClipChild(mw)->core.height +
                ClipChild(mw)->core.y - *ay;
    }
}