File: ScrollMgr.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 (1654 lines) | stat: -rw-r--r-- 55,510 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
/*
 * 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.
 *
 * $Id: ScrollMgr.c,v 1.1 2001-07-18 11:06:00 root Exp $
 */

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


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

#include <Xm/Xm.h>
#include "MatrixP.h"
#include "Draw.h"
#include "Shadow.h"
#include "ScrollMgr.h"
#include "Utils.h"
#include <Xm/ScrollBar.h>

/*
 * ScrollMgr implementation.
 * When we scroll using XCopyArea, occluding windows will cause GraphicsExpose
 * events to be generated, if there are no occluding windows then NoExpose
 * events will be generated. The removal of occluding windows will cause Expose
 * events.  If a number of scrolls (XCopyAreas) occur in quick succession,
 * the events will contain obsolete x/y information since our internal
 * coordinates have been scrolled to a new location.  The ScrollMgr
 * keeps track of scrolls and offsets required to relocate the events to the
 * current coordinate system.
 *
 * The Matrix widget has two ScrollMgrs, one for the Matrix's window
 * and one for the Clip widget's window.
 *
 * Each widgets compress_exposures field should be XtExposeCompressSeries
 * or XtExposeNoCompress.
 *
 * The idea behind this code is based on the PanHandler posted by Chuck Ocheret
 * (chuck@fid.morgan.com)
 */


/*
 * Create and initialize a ScrollMgr
 */
SmScrollMgr
xbaeSmCreateScrollMgr()
{
    SmScrollMgr scrollMgr = XtNew(SmScrollMgrRec);

    scrollMgr->offset_x = 0;
    scrollMgr->offset_y = 0;
    scrollMgr->scroll_count = 0;
    scrollMgr->scroll_queue = NULL;
    scrollMgr->scrolling = False;

    return scrollMgr;
}

/*
 * Destroy a ScrollMgr, including any queued scrolls
 */
void
xbaeSmDestroyScrollMgr(scrollMgr)
SmScrollMgr scrollMgr;
{
    if (scrollMgr->scroll_queue)
    {
        SmScrollNode node = scrollMgr->scroll_queue->next;

        while (node != scrollMgr->scroll_queue)
        {
            SmScrollNode d = node;

            node = node->next;
            XtFree((XtPointer) d);
        }
        XtFree((XtPointer) node);
    }

    XtFree((XtPointer) scrollMgr);
}

/*
 * Record a new scroll request in the ScrollMgr
 */
void
xbaeSmAddScroll(scrollMgr, delta_x, delta_y)
SmScrollMgr scrollMgr;
int delta_x;
int delta_y;
{
    SmScrollNode node = XtNew(SmScrollNodeRec);

    node->x = delta_x;
    node->y = delta_y;

    scrollMgr->offset_x += delta_x;
    scrollMgr->offset_y += delta_y;
    scrollMgr->scroll_count++;

    /*
     * Insert the node at the end of the queue
     */
    if (!scrollMgr->scroll_queue)
    {
        scrollMgr->scroll_queue = node;
        node->next = node;
        node->prev = node;
    }
    else
    {
        SmScrollNode last = scrollMgr->scroll_queue->prev;

        last->next = node;
        node->next = scrollMgr->scroll_queue;
        node->prev = last;
        scrollMgr->scroll_queue->prev = node;
    }
}

/*
 * Remove a scroll from the ScrollMgr queue
 */
void
xbaeSmRemoveScroll(scrollMgr)
SmScrollMgr scrollMgr;
{
    if (scrollMgr->scroll_count)
    {
        SmScrollNode node = scrollMgr->scroll_queue;

        scrollMgr->offset_x -= node->x;
        scrollMgr->offset_y -= node->y;

        /*
         * Remove node from head of queue
         */
        if (node->next == node)
            scrollMgr->scroll_queue = NULL;
        else
        {
            scrollMgr->scroll_queue = node->next;
            node->next->prev = node->prev;
            node->prev->next = node->next;
        }
        XtFree((XtPointer) node);

        scrollMgr->scroll_count--;
    }
}

/*
 * Handle an expose event
 */
void
xbaeSmScrollEvent(scrollMgr, event)
SmScrollMgr scrollMgr;
XEvent *event;
{
    switch (event->type)
    {

    case Expose:

        /*
         * Normal Expose event, translate it into our scrolled
         * coordinate system.
         */
        event->xexpose.x += scrollMgr->offset_x;
        event->xexpose.y += scrollMgr->offset_y;
        break;

    case GraphicsExpose:

        /*
         * If we are not scrolling, then this must be the first
         * GraphicsExpose event.  Remove the corresponding scroll from the
         * queue, and if we have more GraphicsExposes to come, set scrolling
         * to True.
         */
        if (scrollMgr->scrolling == False)
        {
            xbaeSmRemoveScroll(scrollMgr);
            if (event->xgraphicsexpose.count != 0)
                scrollMgr->scrolling = True;
        }

        /*
         * This is the last GraphicsExpose so set scrolling to False.
         */
        else if (event->xgraphicsexpose.count == 0)
            scrollMgr->scrolling = False;

        /*
         * Translate the event into our scrolled coordinate system.
         */
        event->xgraphicsexpose.x += scrollMgr->offset_x;
        event->xgraphicsexpose.y += scrollMgr->offset_y;
        break;

    case NoExpose:

        /*
         * A NoExpose event means we won't be getting any GraphicsExpose
         * events, so remove the scroll from the queue and set scrolling
         * to False.
         */
        xbaeSmRemoveScroll(scrollMgr);
        scrollMgr->scrolling = False;
        break;

    default:
        break;
    }
}

/*
 * Callback for vertical scrollbar
 */
/* ARGSUSED */
void
xbaeScrollVertCB(w, client_data, call_data)
Widget w;
XtPointer client_data;
XmScrollBarCallbackStruct *call_data;
{
    XbaeMatrixWidget mw = (XbaeMatrixWidget) XtParent(w);
    Rectangle fixed, nonfixed;
    int src_y, dest_y, height;
    int vert_sb_offset = VERT_SB_OFFSET(mw);
    int row_height = ROW_HEIGHT(mw);
    int trailing_fixed_row_label_offset = TRAILING_FIXED_ROW_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 row_label_width = ROW_LABEL_WIDTH(mw);
    int fixed_row_label_offset = FIXED_ROW_LABEL_OFFSET(mw);
    
    /*
     * Not managed yet
     */
    if (!XtIsRealized((Widget)mw))
        return;

    /*
     * Didn't scroll
     */
    if (call_data->value == VERT_ORIGIN(mw))
        return;

    /*
     * Scrolled forward. We want to copy a chunk starting at src_y up
     * to the top (dest_y=0)
     */
    else if (call_data->value > VERT_ORIGIN(mw))
    {
        dest_y = 0;
        src_y = (call_data->value - VERT_ORIGIN(mw)) * row_height;
        height = ClipChild(mw)->core.height - src_y;
    }

    /*
     * Scrolled backward. We want to copy a chunk starting at the top
     * (src_y=0) down to dest_y.
     */
    else
    {
        dest_y = (VERT_ORIGIN(mw) - call_data->value) * row_height;
        src_y = 0;
        height = ClipChild(mw)->core.height - dest_y;
    }

    /*
     * The textField needs to scroll along with the cells.
     */
    if (XtIsManaged(TextChild(mw)) &&
                mw->matrix.current_row >= (int)mw->matrix.fixed_rows &&
                mw->matrix.current_row < TRAILING_VERT_ORIGIN(mw))
        XtMoveWidget(TextChild(mw),
                     TextChild(mw)->core.x, TextChild(mw)->core.y +
                     (VERT_ORIGIN(mw) - call_data->value) * row_height);

    /*
     * Now we can adjust our vertical origin
     */
    VERT_ORIGIN(mw) = call_data->value;

    /*
     * If we scrolled more than a screenful, just clear and
     * redraw the whole thing
     */
    if (height <= 0)
    {
        /*
         * Clear the whole clip window.
         */
        XClearArea(XtDisplay(mw), XtWindow(ClipChild(mw)),
                   0, 0,
                   0 /*Full Width*/, 0 /*Full Height*/,
                   False);

        /*
         * Clear the whole Left and Right Clips
         */
        if (XtIsManaged(LeftClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(LeftClip(mw)),
                       0, 0,
                       0 /*Full Width*/, 0 /*Full Height*/,
                       False);
        if (XtIsManaged(RightClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(RightClip(mw)),
                       0, 0,
                       0 /*Full Width*/, 0 /*Full Height*/,
                       False);
        /*
         * Redraw all the non-fixed cells in the clip window
         */
        SETRECT(nonfixed,
                0, 0,
                ClipChild(mw)->core.width - 1,
                ClipChild(mw)->core.height - 1);

        /*
         * Clear non-fixed row labels, if necessary.
         * If we don't have a row label width, then
         * it would clear the entire width.
         */
        if (row_label_width)
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       vert_sb_offset, fixed_row_label_offset,
                       row_label_width - 1,
                       VISIBLE_HEIGHT(mw),
                       False);

        /*
         * Clear the trailing filled rows, if necessary
         */
        if (IN_GRID_ROW_MODE(mw) && NEED_HORIZ_FILL(mw))
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       trailing_fixed_column_label_offset +
                       trailing_fixed_column_width,
                       fixed_row_label_offset,
                       FILL_HORIZ_WIDTH(mw),
                       ClipChild(mw)->core.height,
                       False);

        /*
         * Redraw non-fixed row labels and cells in fixed columns
         */
        SETRECT(fixed,
                vert_sb_offset, fixed_row_label_offset,
                trailing_fixed_column_label_offset +
                trailing_fixed_column_width - 1,
                trailing_fixed_row_label_offset - 1);
    }

    /*
     * If we scrolled less than a screenful, we want to copy as many
     * pixels as we can and then clear and redraw the newly scrolled data.
     */
    else
    {
        int y_clear = src_y > dest_y ? height : 0;

        /*
         * Queue this scroll with the ScrollMgr
         */
        xbaeSmAddScroll(mw->matrix.clip_scroll_mgr, 0, dest_y - src_y);

        /*
         * Copy the non-fixed cells in the clip widget
         */
        XCopyArea(XtDisplay(mw),
                  XtWindow(ClipChild(mw)), XtWindow(ClipChild(mw)),
                  mw->matrix.draw_gc,
                  0, src_y,
                  ClipChild(mw)->core.width, height,
                  0, dest_y);

        /*
         * Clear the newly scrolled chunk of the clip widget
         */
        XClearArea(XtDisplay(mw), XtWindow(ClipChild(mw)),
                   0, y_clear,
                   0 /*Full Width*/, ClipChild(mw)->core.height - height,
                   False);

        /*
         * Redraw the non-fixed cells into the new chunk
         */
        SETRECT(nonfixed,
                0, y_clear,
                ClipChild(mw)->core.width - 1,
                (y_clear + (ClipChild(mw)->core.height - height)) - 1);

        /*
         * Queue this scroll with the ScrollMgr
         */
        xbaeSmAddScroll(mw->matrix.matrix_scroll_mgr, 0, dest_y - src_y);

        /*
         * Copy cells in the fixed columns on LeftClip
         */
        if (XtIsManaged(LeftClip(mw)))
            XCopyArea(XtDisplay(mw),
                      XtWindow(LeftClip(mw)), XtWindow(LeftClip(mw)),
                      mw->matrix.draw_gc,
                      0, src_y,
                      FIXED_COLUMN_WIDTH(mw), height,
                      0, dest_y);

        /*
         * Copy cells in the trailing fixed columns on RightClip
         */
        if (XtIsManaged(RightClip(mw)))
            XCopyArea(XtDisplay(mw),
                      XtWindow(RightClip(mw)), XtWindow(RightClip(mw)),
                      mw->matrix.draw_gc,
                      0, src_y,
                      trailing_fixed_column_width, height,
                      0, dest_y);

        /*
         * Translate coordinates for row labels (on the parent matrix window).
         */
        src_y += fixed_row_label_offset;
        dest_y += fixed_row_label_offset;

        /*
         * Copy the row labels
         */
        if (row_label_width)
            XCopyArea(XtDisplay(mw),
                      XtWindow(mw), XtWindow(mw),
                      mw->matrix.draw_gc,
                      vert_sb_offset, src_y,
                      row_label_width, height,
                      vert_sb_offset, dest_y);
        /*
         * Copy trailing filled portion of the rows if necessary
         */
        if (IN_GRID_ROW_MODE(mw) && NEED_HORIZ_FILL(mw))
        {
            XCopyArea(XtDisplay(mw), XtWindow(mw), XtWindow(mw),
                      mw->matrix.draw_gc,
                      trailing_fixed_column_label_offset +
                      trailing_fixed_column_width, src_y,
                      FILL_HORIZ_WIDTH(mw), height,
                      trailing_fixed_column_label_offset +
                      trailing_fixed_column_width, dest_y);
        }

        /*
         * Clear newly scrolled chunk of fixed columns on LeftClip
         */
        if (XtIsManaged(LeftClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(LeftClip(mw)),
                       0, y_clear, 0 /* Full Width */,
                       ClipChild(mw)->core.height - height, False);
        /*
         * Clear newly scrolled chunk of trailing fixed columns on RightClip
         */
        if (XtIsManaged(RightClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(RightClip(mw)),
                       0, y_clear, 0 /* Full Width */,
                       ClipChild(mw)->core.height - height, False);
        /*
         * Translate coordinates for row labels on Matrix
         */
        y_clear += fixed_row_label_offset;

        /*
         * Clear the newly scrolled chunk of row labels
         */
        if (row_label_width)
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       vert_sb_offset, y_clear,
                       row_label_width,
                       CLIP_VERT_VISIBLE_SPACE(mw) +
                       mw->manager.shadow_thickness,
                       False);

        /*
         * Clear the trailing filled rows, if necessary
         */
        if ((src_y > dest_y) && IN_GRID_ROW_MODE(mw) && NEED_HORIZ_FILL(mw))
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       trailing_fixed_column_label_offset +
                       trailing_fixed_column_width, y_clear, 
                       FILL_HORIZ_WIDTH(mw),
                       ClipChild(mw)->core.height - height,
                       False);
         
        /*
         * Redraw the new chunk of fixed columns and row labels
         */
        SETRECT(fixed,
                vert_sb_offset, y_clear,
                trailing_fixed_column_label_offset +
                trailing_fixed_column_width - 1,
                y_clear + CLIP_VERT_VISIBLE_SPACE(mw));
    }

    /*
     * Perform the actual redraw. The call to draw the grid shadows
     * must be done after any XClearAreas() to ensure we don't redraw
     * more than we have to; that is, we could put the call in the
     * redraw cells routine, but we would end up occasionally redrawing
     * more than once.
     */
    xbaeRedrawLabelsAndFixed(mw, &fixed);
    xbaeRedrawCells(mw, &nonfixed);
}

/*
 * Callback for horizontal scrollbar
 */
/* ARGSUSED */
void
xbaeScrollHorizCB(w, client_data, call_data)
Widget w;
XtPointer client_data;
XmScrollBarCallbackStruct *call_data;
{
    XbaeMatrixWidget mw = (XbaeMatrixWidget) XtParent(w);
    Rectangle fixed, nonfixed;
    int src_x, dest_x, width;
    int horiz_sb_offset = HORIZ_SB_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 trailing_fixed_column_label_offset =
        TRAILING_FIXED_COLUMN_LABEL_OFFSET(mw);*/
    int column_label_height = COLUMN_LABEL_HEIGHT(mw);
    int fixed_column_label_offset = FIXED_COLUMN_LABEL_OFFSET(mw);
    int fixed_column_width = FIXED_COLUMN_WIDTH(mw);
    int vert_dead_space_height = VERT_DEAD_SPACE_HEIGHT(mw);
    Boolean need_vert_fill = NEED_VERT_FILL(mw);
    Boolean has_attached_trailing_rows = HAS_ATTACHED_TRAILING_ROWS(mw);
    Boolean need_vert_dead_space_fill = NEED_VERT_DEAD_SPACE_FILL(mw);
    
    /*printf("%d\n", HORIZ_ORIGIN(mw));*/
    /*
     * Didn't scroll
     */
    if (call_data->value == HORIZ_ORIGIN(mw))
        return;

    /*
     * Scrolled right. We want to copy a chunk starting at src_x over to
     * the left (dest_x=0)
     */
    else if (call_data->value > HORIZ_ORIGIN(mw))
    {
        dest_x = 0;
        src_x = call_data->value - HORIZ_ORIGIN(mw);
        width = ClipChild(mw)->core.width - src_x;
    }

    /*
     * Scrolled left. We want to copy a chunk starting at the left (src_x=0)
     * over to the right to dest_x
     */
    else
    {
        dest_x = HORIZ_ORIGIN(mw) - call_data->value;
        src_x = 0;
        width = ClipChild(mw)->core.width - dest_x;
    }

    /*
     * The textField needs to scroll along with the cells.
     */
    if (XtIsManaged(TextChild(mw)) &&
                mw->matrix.current_column >= (int)mw->matrix.fixed_columns &&
                mw->matrix.current_column < TRAILING_HORIZ_ORIGIN(mw))
    {
        XtMoveWidget(TextChild(mw),
                     TextChild(mw)->core.x + (HORIZ_ORIGIN(mw) -
                                              call_data->value),
                     TextChild(mw)->core.y);
    }

    /*
     * Now we can adjust our horizontal origin
     */
    HORIZ_ORIGIN(mw) = call_data->value;
    mw->matrix.left_column = xbaeXtoCol(mw, fixed_column_width +
                                        HORIZ_ORIGIN(mw)) -
        mw->matrix.fixed_columns;

    if (!XtIsRealized((Widget)mw))
        return;

    /*
     * If we scrolled more than a screenful, just clear and
     * redraw the whole thing
     */
    if (width <= 0)
    {
        /*
         * Clear the whole clip window
         */
        XClearArea(XtDisplay(mw), XtWindow(ClipChild(mw)),
                   0, 0,
                   0 /* Full Width */, 0 /* Full Height */,
                   False);

        /*
         * Clear the whole Top and Bottom Clips
         */
        if (XtIsManaged(TopClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(TopClip(mw)),
                       0, 0,
                       0 /*Full Width*/, 0 /*Full Height*/,
                       False);
        if (XtIsManaged(BottomClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(BottomClip(mw)),
                       0, 0,
                       0 /*Full Width*/, 0 /*Full Height*/,
                       False);
        /*
         * Redraw all the non-fixed cells in the clip window
         */
        SETRECT(nonfixed,
                0, 0,
                ClipChild(mw)->core.width - 1,
                ClipChild(mw)->core.height - 1);

        /*
         * Clear the non-fixed column labels
         */
        if (column_label_height)
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       fixed_column_label_offset, horiz_sb_offset,
                       VISIBLE_WIDTH(mw),
                       column_label_height - 1, False);
        /*
         * Clear the trailing fixed column
         */
        if (IN_GRID_COLUMN_MODE(mw))
        {
            if (need_vert_fill && (! has_attached_trailing_rows))
                XClearArea(XtDisplay(mw), XtWindow(mw),
                           fixed_column_label_offset,
                           trailing_fixed_row_label_offset +
                           trailing_fixed_row_height,
                           MATRIX_HORIZ_VISIBLE_SPACE(mw), 0, False);
            
            if (need_vert_dead_space_fill)
                XClearArea(XtDisplay(mw), XtWindow(mw),
                           fixed_column_label_offset,
                           UNATTACHED_TRAILING_ROWS_OFFSET(mw),
                           MATRIX_HORIZ_VISIBLE_SPACE(mw), 0, False);
        }

        /*
         * Redraw non-fixed column labels and cells in fixed rows
         */
        SETRECT(fixed,
                fixed_column_label_offset, horiz_sb_offset,
                 fixed_column_label_offset + CLIP_HORIZ_VISIBLE_SPACE(mw),
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);
    }

    /*
     * If we scrolled less than a screenful, we want to copy as many
     * pixels as we can and then clear and redraw the newly scrolled data.
     */
    else
    {
        int x_clear = src_x > dest_x ? width : 0;
        int unattached_trailing_rows_offset =
            UNATTACHED_TRAILING_ROWS_OFFSET(mw);

        /*
         * Queue this scroll with the ScrollMgr
         */
        xbaeSmAddScroll(mw->matrix.clip_scroll_mgr, dest_x - src_x, 0);

        /*
         * Copy the non-fixed cells in the clip widget
         */
        XCopyArea(XtDisplay(mw),
                  XtWindow(ClipChild(mw)), XtWindow(ClipChild(mw)),
                  mw->matrix.draw_gc, src_x, 0, width,
                  ClipChild(mw)->core.height, dest_x, 0);

        /*
         * Clear the newly scrolled chunk of the clip widget
         */
        XClearArea(XtDisplay(mw), XtWindow(ClipChild(mw)),
                   x_clear, 0,
                   ClipChild(mw)->core.width - width, 0 /*Full Height*/,
                   False);

        /*
         * Redraw the non-fixed cells into the new chunk
         */
        SETRECT(nonfixed,
                x_clear, horiz_sb_offset,
                (x_clear + (ClipChild(mw)->core.width - width)) - 1,
                ClipChild(mw)->core.height - 1);

        /*
         * Queue this scroll with the ScrollMgr
         */
        xbaeSmAddScroll(mw->matrix.matrix_scroll_mgr, dest_x - src_x, 0);

        /*
         * Copy cells across in fixed rows on TopClip
         */
        if (XtIsManaged(TopClip(mw)))
            XCopyArea(XtDisplay(mw),
                      XtWindow(TopClip(mw)), XtWindow(TopClip(mw)),
                      mw->matrix.draw_gc, src_x, 0,
                      width, FIXED_ROW_HEIGHT(mw), dest_x, 0);

        /*
         * Copy cells across in trailing fixed rows on BottomClip
         */
        if (XtIsManaged(BottomClip(mw)))
            XCopyArea(XtDisplay(mw),
                      XtWindow(BottomClip(mw)), XtWindow(BottomClip(mw)),
                      mw->matrix.draw_gc, src_x, horiz_sb_offset, width,
                      trailing_fixed_row_height, dest_x, 0);

        /*
         * Translate coordinates for column labels on the Matrix.
         */
        src_x += fixed_column_label_offset;
        dest_x += fixed_column_label_offset;

        /*
         * Copy the column labels
         */
        if (column_label_height)
            XCopyArea(XtDisplay(mw),
                      XtWindow(mw), XtWindow(mw),
                      mw->matrix.draw_gc,
                      src_x, horiz_sb_offset,
                      width, column_label_height,
                      dest_x, horiz_sb_offset);
        
        /*
         * Copy trailing filled portion of the columns if necessary
         */
        if (IN_GRID_COLUMN_MODE(mw))
        {
            if (need_vert_fill && (! has_attached_trailing_rows))
                XCopyArea(XtDisplay(mw), XtWindow(mw), XtWindow(mw),
                          mw->matrix.draw_gc,
                          src_x, trailing_fixed_row_label_offset +
                          trailing_fixed_row_height,
                          width, FILL_VERT_HEIGHT(mw),
                          dest_x, trailing_fixed_row_label_offset +
                          trailing_fixed_row_height);
            
            if (need_vert_dead_space_fill)
                XCopyArea(XtDisplay(mw), XtWindow(mw), XtWindow(mw),
                          mw->matrix.draw_gc,
                          src_x, unattached_trailing_rows_offset,
                          width, vert_dead_space_height,
                          dest_x, unattached_trailing_rows_offset);
        }
        
        /*
         * Clear newly scrolled chunk of fixed rows on TopClip
         */
        if (XtIsManaged(TopClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(TopClip(mw)),
                       x_clear, 0,
                       ClipChild(mw)->core.width - width, 0 /* Full Height */,
                       False);

        /*
         * Clear newly scrolled chunk of trailing fixed rows on BottomClip
         */
        if (XtIsManaged(BottomClip(mw)))
            XClearArea(XtDisplay(mw), XtWindow(BottomClip(mw)),
                       x_clear, 0,
                       ClipChild(mw)->core.width - width, 0 /* Full Height */,
                       False);

        /*
         * Translate coordinates for row labels on Matrix
         */
        x_clear += fixed_column_label_offset;

        /*
         * Clear the newly scrolled chunk of column labels
         */
        if (column_label_height)
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       x_clear, horiz_sb_offset,
                       ClipChild(mw)->core.width - width,
                       column_label_height,
                       False);

        /*
         * Clear the dead space if necessary
         */
        if (IN_GRID_COLUMN_MODE(mw) && need_vert_dead_space_fill)
            XClearArea(XtDisplay(mw), XtWindow(mw),
                       x_clear,unattached_trailing_rows_offset,
                       ClipChild(mw)->core.width - width,
                       vert_dead_space_height, False);

        /*
         * Redraw the new chunk of fixed rows and column labels
         */
        SETRECT(fixed,
                x_clear, horiz_sb_offset,
                x_clear + CLIP_HORIZ_VISIBLE_SPACE(mw),
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);
    }

    /*
     * Perform the actual redraw. The call to draw the grid shadows
     * must be done after any XClearAreas() to ensure we don't redraw
     * more than we have to; that is, we could put the call in the
     * redraw cells routine, but we would end up occasionally redrawing
     * more than once.
     */
    xbaeRedrawLabelsAndFixed(mw, &fixed);
    xbaeRedrawCells(mw, &nonfixed);
}

/*
 * Redraw all cells in the clip widget damaged by the passed Rectangle.
 * The Rectangle must be within the bounds of the cells. These are the
 * non-fixed cells.
 */
void
xbaeRedrawCells(mw, expose)
XbaeMatrixWidget mw;
Rectangle *expose;
{
    int startCol, endCol, startRow, endRow, i, j;
    Rectangle rect;
    Boolean set_mask = False;

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

    /*
     * Translate the 'expose' Rectangle to take into account the
     * fixed rows or columns.
     */
    SETRECT(rect,
            expose->x1 + FIXED_COLUMN_WIDTH(mw),
            expose->y1 + FIXED_ROW_HEIGHT(mw),
            expose->x2 + FIXED_COLUMN_WIDTH(mw),
            expose->y2 + FIXED_ROW_HEIGHT(mw));

    /*
     * Calculate the starting and ending rows/columns of the cells
     * which must be redrawn.
     */
    startCol = xbaeXtoCol(mw, rect.x1 + HORIZ_ORIGIN(mw));
    endCol = xbaeXtoCol(mw, rect.x2 + HORIZ_ORIGIN(mw));
    startRow = YtoRow(mw, rect.y1 + mw->matrix.first_row_offset) +
        VERT_ORIGIN(mw);
    endRow = YtoRow(mw, rect.y2 + mw->matrix.first_row_offset) +
        VERT_ORIGIN(mw);

    SANITY_CHECK_ROW(mw, startRow);
    SANITY_CHECK_ROW(mw, endRow);
    SANITY_CHECK_COLUMN(mw, startCol);
    SANITY_CHECK_COLUMN(mw, endCol);

    /*
     * Redraw all cells which were exposed.
     */
    for (i = startRow; i <= endRow; i++)
    {
        /*
         * If we need to clip the vertical fill
         */
        if ((!set_mask) && IN_GRID_COLUMN_MODE(mw) &&
            ((mw->matrix.rows - 1) == i) && NEED_VERT_FILL(mw))
        {
            set_mask = True;
            xbaeSetClipMask(mw, CLIP_TRAILING_FIXED_ROWS);
        }

        for (j = startCol; j <= endCol; j++)
            xbaeDrawCell(mw, i, j);
    }

    if (set_mask)
        xbaeSetClipMask(mw, CLIP_NONE);
}

/*
 * Redraw the row and column labels and the cells in fixed rows/columns
 * that are overlapped by the Rectangle argument.
 */
void
xbaeRedrawLabelsAndFixed(mw, expose)
XbaeMatrixWidget mw;
Rectangle *expose;
{
    /*
     * Set up some local variables to avoid calling too many macros :p
     */
    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 fixed_column_width = FIXED_COLUMN_WIDTH(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);
    Boolean need_vert_fill = NEED_VERT_FILL(mw);
    Boolean has_attached_trailing_rows = HAS_ATTACHED_TRAILING_ROWS(mw);

    if (mw->matrix.disable_redisplay)
        return;

    /*
     * Handle the row labels that are in fixed rows
     */
    if (mw->matrix.rows && mw->matrix.fixed_rows && mw->matrix.row_labels)
    {
        Rectangle rect;
        
        /*
         * Get the Rectangle enclosing the fixed row labels
         */
        SETRECT(rect, vert_sb_offset, row_label_offset,
                vert_sb_offset + row_label_width - 1,
                fixed_row_label_offset - 1);
        
        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endRow, i;

            /*
             * Intersect the fixed-row-labels Rectangle with the expose
             * Rectangle along the Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            Y_INTERSECT(rect, *expose, intersect);

            /*
             * Redraw each label that was intersected
             */
            endRow = YtoRow(mw, intersect.y2 + mw->matrix.first_row_offset);
            SANITY_CHECK_ROW(mw, endRow);
            for (i = YtoRow(mw, intersect.y1 + mw->matrix.first_row_offset),
                     SANITY_CHECK_ROW(mw, i);
                 i <= endRow; i++)
                xbaeDrawRowLabel(mw, i, False);
        }
    }

    /*
     * Handle the row labels that are in trailing fixed rows
     */
    if (mw->matrix.rows && mw->matrix.trailing_fixed_rows &&
        mw->matrix.row_labels)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the fixed row labels
         */
        SETRECT(rect, vert_sb_offset, trailing_fixed_row_label_offset,
                vert_sb_offset + row_label_width - 1,
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);

        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endRow, i;

            /*
             * Intersect the fixed-row-labels Rectangle with the expose
             * Rectangle along the Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            Y_INTERSECT(rect, *expose, intersect);

            /*
             * Redraw each label that was intersected
             */
            endRow = YtoRow(mw, intersect.y2) + TRAILING_VERT_ORIGIN(mw);
            SANITY_CHECK_ROW(mw, endRow);
            for (i = YtoRow(mw, intersect.y1) + TRAILING_VERT_ORIGIN(mw),
                 SANITY_CHECK_ROW(mw, i); i <= endRow; i++)
                xbaeDrawRowLabel(mw, i, False);
        }
    }

    /*
     * Handle row labels that aren't in fixed rows
     */
    if (mw->matrix.row_labels && mw->matrix.rows)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the non-fixed row labels
         */
        SETRECT(rect, vert_sb_offset, fixed_row_label_offset,
                vert_sb_offset + row_label_width - 1,
                fixed_row_label_offset + VISIBLE_HEIGHT(mw) - 1);

        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endRow, i;

            /*
             * Intersect the fixed-row-labels Rectangle with the expose
             * Rectangle along the Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            Y_INTERSECT(rect, *expose, intersect);

            /*
             * Translate 'intersect' to take into account any fixed rows.
             * This gets it back into the coord system expected by YtoRow().
             */
            intersect.y1 += FIXED_ROW_HEIGHT(mw);
            intersect.y2 += FIXED_ROW_HEIGHT(mw);

            /*
             * Redraw each label that was intersected
             */
            endRow = YtoRow(mw, intersect.y2) + VERT_ORIGIN(mw);
            SANITY_CHECK_ROW(mw, endRow);
            for (i = YtoRow(mw, intersect.y1) + VERT_ORIGIN(mw),
                 SANITY_CHECK_ROW(mw, i); i <= endRow;         i++)
                xbaeDrawRowLabel(mw, i, False);
        }
    }

    /*
     * Handle the column labels that are in fixed columns
     */
    if (mw->matrix.columns && mw->matrix.fixed_columns &&
        mw->matrix.column_labels)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the portion of the column labels
         * that are in fixed columns
         */
        SETRECT(rect,
                column_label_offset, horiz_sb_offset,
                fixed_column_label_offset - 1,
                horiz_sb_offset + column_label_height - 1);

        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endCol, i;

            /*
             * Intersect the fixed-column-labels Rectangle with the expose
             * Rectangle along the X axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            X_INTERSECT(rect, *expose, intersect);

            /*
             * Redraw each label that was intersected
             */
            endCol = xbaeXtoCol(mw, intersect.x2);
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = xbaeXtoCol(mw, intersect.x1), SANITY_CHECK_COLUMN(mw, i);
                 i <= endCol; i++)
                xbaeDrawColumnLabel(mw, i, False);
        }
    }

    /*
     * Handle the column labels that are in trailing fixed columns
     */
    if (mw->matrix.columns && mw->matrix.trailing_fixed_columns &&
        mw->matrix.column_labels)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the portion of the column labels
         * that are in fixed columns
         */
        SETRECT(rect,
                trailing_fixed_column_label_offset, horiz_sb_offset,
                trailing_fixed_column_label_offset +
                trailing_fixed_column_width,
                horiz_sb_offset + column_label_height);

        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endCol, i;

            /*
             * Intersect the fixed-column-labels Rectangle with the expose
             * Rectangle along the X axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            X_INTERSECT(rect, *expose, intersect);

            /*
             * Redraw each label that was intersected
             */
            endCol = xbaeXtoTrailingCol(mw, intersect.x2);
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = xbaeXtoTrailingCol(mw, intersect.x1),
                 SANITY_CHECK_COLUMN(mw, i); i <= endCol; i++)
                xbaeDrawColumnLabel(mw, i, False);
        }
    }

    /*
     * Handle column labels that aren't in fixed columns
     */
    if (mw->matrix.column_labels && mw->matrix.columns)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the non-fixed column labels
         */
        SETRECT(rect,
                fixed_column_label_offset, horiz_sb_offset,
                fixed_column_label_offset + VISIBLE_WIDTH(mw) - 1,
                horiz_sb_offset + column_label_height - 1);

        /*
         * If the expose Rectangle overlaps, then some labels must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int endCol, i;

            /*
             * Intersect the non-fixed-column-labels Rectangle with the expose
             * Rectangle along the X axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            X_INTERSECT(rect, *expose, intersect);

            /*
             * Translate 'intersect' to take into account any fixed columns.
             * This gets it back into the coord system expected by XtoCol().
             */
            intersect.x1 += fixed_column_width;
            intersect.x2 += fixed_column_width;

            /*
             * Redraw each label that was intersected
             */
            endCol = xbaeXtoCol(mw, intersect.x2 + HORIZ_ORIGIN(mw));
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = xbaeXtoCol(mw, intersect.x1 + HORIZ_ORIGIN(mw)),
                 SANITY_CHECK_COLUMN(mw, i); i <= endCol; i++)
                xbaeDrawColumnLabel(mw, i, False);
        }
    }

    /*
     * Handle cells in fixed rows except those also in fixed columns
     */
    if (mw->matrix.rows && mw->matrix.columns && mw->matrix.fixed_rows)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the cells in fixed rows
         */
        SETRECT(rect,
                fixed_column_label_offset, row_label_offset,
                fixed_column_label_offset + VISIBLE_WIDTH(mw) - 1,
                fixed_row_label_offset - 1);

        /*
         * If the expose Rectangle overlaps, then some cells must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int startCol, endCol, startRow, endRow, i, j;

            /*
             * Intersect the fixed-cells Rectangle with the expose
             * Rectangle along the X and Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            INTERSECT(rect, *expose, intersect);

            intersect.x1 += fixed_column_width;
            intersect.x2 += fixed_column_width;

            /*
             * Get starting and ending rows/columns. Always take into
             * account the scrolling origins for the columns; for rows
             * only if we are fixed in that dimension.
             */
            startCol = xbaeXtoCol(mw, intersect.x1 + HORIZ_ORIGIN(mw));
            endCol = xbaeXtoCol(mw, intersect.x2 + HORIZ_ORIGIN(mw));

            startRow = YtoRow(mw, intersect.y1) +
                (mw->matrix.fixed_rows
                 ? 0
                 : VERT_ORIGIN(mw));
            endRow = YtoRow(mw, intersect.y2) +
                (mw->matrix.fixed_rows
                 ? 0
                 : VERT_ORIGIN(mw));

            /*
             * Redraw each cell that was intersected
             */
            SANITY_CHECK_ROW(mw, startRow);
            SANITY_CHECK_ROW(mw, endRow);
            SANITY_CHECK_COLUMN(mw, startCol);
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = startRow; i <= endRow; i++)
                for (j = startCol; j <= endCol; j++)
                    xbaeDrawCell(mw, i, j);
        }
    }

    /*
     * Handle cells in trailing fixed rows except those also in fixed columns
     */
    if (mw->matrix.rows && mw->matrix.columns &&
        mw->matrix.trailing_fixed_rows)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the cells in trailing fixed rows
         */
        SETRECT(rect, fixed_column_label_offset,
                trailing_fixed_row_label_offset,
                fixed_column_label_offset + VISIBLE_WIDTH(mw) - 1,
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);

        /*
         * If the expose Rectangle overlaps, then some cells must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int startCol, endCol, startRow, endRow, i, j;

            /*
             * Intersect the fixed-cells Rectangle with the expose
             * Rectangle along the X and Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            INTERSECT(rect, *expose, intersect);

            intersect.x1 += fixed_column_width;
            intersect.x2 += fixed_column_width;

            /*
             * Get starting and ending rows/columns. Always take into
             * account the scrolling origins for the columns and never
             * for the rows.
             */
            startCol = xbaeXtoCol(mw, intersect.x1 + HORIZ_ORIGIN(mw));
            endCol = xbaeXtoCol(mw, intersect.x2 + HORIZ_ORIGIN(mw));

            startRow = YtoRow(mw, intersect.y1) + TRAILING_VERT_ORIGIN(mw);
            endRow = YtoRow(mw, intersect.y2) + TRAILING_VERT_ORIGIN(mw);

            /*
             * Redraw each cell that was intersected
             */
            SANITY_CHECK_ROW(mw, startRow);
            SANITY_CHECK_ROW(mw, endRow);
            SANITY_CHECK_COLUMN(mw, startCol);
            SANITY_CHECK_COLUMN(mw, endCol);
            xbaeSetClipMask(mw, CLIP_TRAILING_FIXED_ROWS);
            for (i = startRow; i <= endRow; i++)
                for (j = startCol; j <= endCol; j++)
                    xbaeDrawCell(mw, i, j);
            xbaeSetClipMask(mw, CLIP_NONE);
        }
    }

    /*
     * Handle cells in fixed columns
     */
    if (mw->matrix.rows && mw->matrix.columns && mw->matrix.fixed_columns)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the cells in fixed columns
         */
        SETRECT(rect,
                column_label_offset, row_label_offset,
                fixed_column_label_offset - 1,
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);

        /*
         * If the expose Rectangle overlaps, then some cells must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int startCol, endCol, startRow, endRow, skipRow = -1, i, j;
            Boolean redrawFixedRows, redrawTrailingFixedRows;
            unsigned int clip_reason = CLIP_NONE;

            /*
             * Intersect the fixed-cells Rectangle with the expose
             * Rectangle along the X and Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            INTERSECT(rect, *expose, intersect);

            /*
             * If we have any fixed rows, we might need to redraw the cells
             * located in the intersection of the fixed rows and columns.
             * These cells may force use to be different than our current
             * VERT_ORIGIN.
             */
            redrawFixedRows = redrawTrailingFixedRows = False;
            if (mw->matrix.fixed_rows)
            {
                clip_reason = CLIP_FIXED_COLUMNS;

                SETRECT(rect,
                        column_label_offset, row_label_offset,
                        fixed_column_label_offset - 1,
                        fixed_row_label_offset - 1);

                if (OVERLAP(*expose, rect))
                    redrawFixedRows = True;
            }

            if (mw->matrix.trailing_fixed_rows)
            {
                clip_reason = CLIP_FIXED_COLUMNS;

                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 (OVERLAP(*expose, rect))
                    redrawTrailingFixedRows = True;
            }

            if (CLIP_NONE != clip_reason)
                xbaeSetClipMask(mw, clip_reason);

            /*
             * Get starting and ending rows/columns. Never take into
             * account the scrolling origins for the rows; for columns
             * only if we are fixed in that dimension.
             */
            startCol = xbaeXtoCol(mw, intersect.x1 +
                              (mw->matrix.fixed_columns
                               ? 0
                               : HORIZ_ORIGIN(mw)));
            endCol = xbaeXtoCol(mw, intersect.x2 +
                            (mw->matrix.fixed_columns
                             ? 0
                             : HORIZ_ORIGIN(mw)));

            startRow = redrawFixedRows ? 0 : YtoRow(mw, intersect.y1) +
                VERT_ORIGIN(mw);
            if (redrawTrailingFixedRows)
            {
                skipRow = YtoRow(mw, intersect.y2) + VERT_ORIGIN(mw) -
                    mw->matrix.trailing_fixed_rows + 1;
                endRow = mw->matrix.rows - 1;
            }
            else
                endRow = YtoRow(mw, intersect.y2) + VERT_ORIGIN(mw);

            /*
             * Redraw each cell that was intersected
             */
            SANITY_CHECK_ROW(mw, startRow);
            SANITY_CHECK_ROW(mw, endRow);
            SANITY_CHECK_COLUMN(mw, startCol);
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = startRow; i <= endRow; i++)
                for (j = startCol; j <= endCol; j++)
                {
                    /*
                     * If we had to redraw cells located in both fixed rows
                     * and columns, when we are done redrawing those cells,
                     * we need to skip to the correct non-fixed row to draw,
                     * or alternatively, jump to the trailing fixed row
                     * to draw.
                     */
                    if (redrawFixedRows && i == mw->matrix.fixed_rows)
                        i += VERT_ORIGIN(mw);

                    if (redrawTrailingFixedRows && i == skipRow &&
                        endRow > skipRow && i < TRAILING_VERT_ORIGIN(mw))
                        i = TRAILING_VERT_ORIGIN(mw);

                    /*
                     * If we need to clip the vertical fill
                     */
                    if (!(clip_reason & CLIP_TRAILING_FIXED_ROWS) &&
                        IN_GRID_COLUMN_MODE(mw) &&
                        ((mw->matrix.rows - 1) == i) && 
                        (need_vert_fill && (! has_attached_trailing_rows)))
                    {
                        clip_reason |= CLIP_FIXED_COLUMNS |
                            CLIP_TRAILING_FIXED_ROWS;
                        xbaeSetClipMask(mw, clip_reason);
                    }

                    xbaeDrawCell(mw, i, j);
                }

            if (CLIP_NONE != clip_reason)
                xbaeSetClipMask(mw, CLIP_NONE);
        }
    }

    /*
     * Handle cells in trailing fixed columns
     */
    if (mw->matrix.rows && mw->matrix.columns &&
        mw->matrix.trailing_fixed_columns)
    {
        Rectangle rect;

        /*
         * Get the Rectangle enclosing the cells in trailing fixed columns
         */
        SETRECT(rect,
                trailing_fixed_column_label_offset, row_label_offset,
                trailing_fixed_column_label_offset +
                trailing_fixed_column_width - 1,
                trailing_fixed_row_label_offset +
                trailing_fixed_row_height - 1);

        /*
         * If the expose Rectangle overlaps, then some cells must be redrawn
         */
        if (OVERLAP(*expose, rect))
        {
            Rectangle intersect;
            int startCol, endCol, startRow, endRow, skipRow = -1, i, j;
            Boolean redrawFixedRows, redrawTrailingFixedRows;
            unsigned int clip_reason = CLIP_NONE;

            /*
             * Intersect the fixed-cells Rectangle with the expose
             * Rectangle along the X and Y axis.  The resulting Rectangle will
             * be in 'rect's coordinate system.
             */
            INTERSECT(rect, *expose, intersect);

            /*
             * If we have any fixed rows, we might need to redraw the cells
             * located in the intersection of the fixed rows and columns.
             * These cells may force us to be different than our current
             * VERT_ORIGIN.
             */
            redrawFixedRows = redrawTrailingFixedRows = False;
            if (mw->matrix.fixed_rows)
            {
                clip_reason = CLIP_FIXED_COLUMNS;

                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 (OVERLAP(*expose, rect))
                    redrawFixedRows = True;
            }

            if (mw->matrix.trailing_fixed_rows)
            {
                clip_reason = CLIP_FIXED_COLUMNS;

                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 (OVERLAP(*expose, rect))
                    redrawTrailingFixedRows = True;
            }

            if (CLIP_NONE != clip_reason)
                xbaeSetClipMask(mw, clip_reason);

            /*
             * Get starting and ending rows/columns. Never take into
             * account the scrolling origins for the rows; for columns
             * only if we are fixed in that dimension.
             */
            startCol = xbaeXtoTrailingCol(mw, intersect.x1);
            endCol = xbaeXtoTrailingCol(mw, intersect.x2);

            startRow = redrawFixedRows ? 0 : YtoRow(mw, intersect.y1) +
                VERT_ORIGIN(mw);

            if (redrawTrailingFixedRows)
            {
                skipRow = YtoRow(mw, intersect.y2) + VERT_ORIGIN(mw) -
                    mw->matrix.trailing_fixed_rows + 1;
                endRow = mw->matrix.rows - 1;
            }
            else
                endRow = YtoRow(mw, intersect.y2) + VERT_ORIGIN(mw);

            /*
             * Redraw each cell that was intersected
             */
            SANITY_CHECK_ROW(mw, startRow);
            SANITY_CHECK_ROW(mw, endRow);
            SANITY_CHECK_COLUMN(mw, startCol);
            SANITY_CHECK_COLUMN(mw, endCol);
            for (i = startRow; i <= endRow; i++)
                for (j = startCol; j <= endCol; j++)
                {
                    /*
                     * If we had to redraw cells located in both fixed rows
                     * and columns, when we are done redrawing those cells,
                     * we need to skip to the correct non-fixed row to draw
                     */                    
                    if (redrawFixedRows && (i == mw->matrix.fixed_rows))
                        i += VERT_ORIGIN(mw);

                    if (redrawTrailingFixedRows && (i == skipRow) &&
                        (endRow > skipRow))
                        i = TRAILING_VERT_ORIGIN(mw);
                    
                    /*
                     * If we need to clip the vertical fill
                     */
                    if (!(clip_reason & CLIP_TRAILING_FIXED_ROWS) &&
                        IN_GRID_COLUMN_MODE(mw) &&
                        ((mw->matrix.rows - 1) == i) && 
                        (need_vert_fill && (! has_attached_trailing_rows)))
                    {
                        clip_reason |= CLIP_TRAILING_FIXED_COLUMNS |
                            CLIP_TRAILING_FIXED_ROWS;
                        xbaeSetClipMask(mw, clip_reason);
                    }

                    xbaeDrawCell(mw, i, j);
                }

            if (CLIP_NONE != clip_reason)
                xbaeSetClipMask(mw, CLIP_NONE);
        }
    }

    /*
     * Draw a shadow just inside row/column labels and around outer edge
     * of clip widget.        We can't use height of clip widget because it is
     * truncated to nearest row.  We use cell_visible_height instead.
     */
    if (mw->manager.shadow_thickness)
    {
        Dimension width, height;

        if (! mw->matrix.fill)
        {
            width = ClipChild(mw)->core.width + fixed_column_width +
                trailing_fixed_column_width +
                2 * mw->manager.shadow_thickness;
            height = mw->matrix.cell_visible_height + FIXED_ROW_HEIGHT(mw) +
                trailing_fixed_row_height +
                2 * mw->manager.shadow_thickness;
        }
        else
        {
            width = mw->core.width - row_label_width -
                VERT_SB_SPACE(mw);
            
            height = mw->core.height - column_label_height -
                HORIZ_SB_SPACE(mw) ;
        }
        
        DRAW_SHADOW(XtDisplay(mw), XtWindow(mw),
                    mw->manager.top_shadow_GC,
                    mw->manager.bottom_shadow_GC,
                    mw->manager.shadow_thickness,
                    row_label_width + vert_sb_offset,
                    column_label_height + horiz_sb_offset,
                    width, height, mw->matrix.shadow_type);
    }
}