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

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

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

/*

Copyright 1987, 1988, 1994, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/

#define INTRINSIC_C

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "IntrinsicI.h"
#include "VarargsI.h"           /* for geoTattler */
#ifndef NO_IDENTIFY_WINDOWS
#include <X11/Xatom.h>
#endif
#ifndef VMS
#include <sys/stat.h>
#endif                          /* VMS */
#ifdef WIN32
#include <direct.h>             /* for _getdrives() */
#endif

#include <stdio.h>
#include <stdlib.h>

String XtCXtToolkitError = "XtToolkitError";

Boolean
XtIsSubclass(Widget widget, WidgetClass myWidgetClass)
{
    register WidgetClass w;
    Boolean retval = FALSE;

    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    LOCK_PROCESS;
    for (w = widget->core.widget_class; w != NULL; w = w->core_class.superclass)
        if (w == myWidgetClass) {
            retval = TRUE;
            break;
        }
    UNLOCK_PROCESS;
    UNLOCK_APP(app);
    return retval;
}                               /* XtIsSubclass */

Boolean
_XtCheckSubclassFlag(Widget object, _XtXtEnum flag)
{
    Boolean retval;

    LOCK_PROCESS;
    if (object->core.widget_class->core_class.class_inited & flag)
        retval = TRUE;
    else
        retval = FALSE;
    UNLOCK_PROCESS;
    return retval;
} /*_XtVerifySubclass */

Boolean
_XtIsSubclassOf(Widget object,
                WidgetClass myWidgetClass,
                WidgetClass superClass,
                _XtXtEnum flag)
{
    LOCK_PROCESS;
    if (!(object->core.widget_class->core_class.class_inited & flag)) {
        UNLOCK_PROCESS;
        return False;
    }
    else {
        register WidgetClass c = object->core.widget_class;

        while (c != superClass) {
            if (c == myWidgetClass) {
                UNLOCK_PROCESS;
                return True;
            }
            c = c->core_class.superclass;
        }
        UNLOCK_PROCESS;
        return False;
    }
} /*_XtIsSubclassOf */

XtPointer
XtGetClassExtension(WidgetClass object_class,
                    Cardinal byte_offset,
                    XrmQuark type, long version, Cardinal record_size)
{
    ObjectClassExtension ext;

    LOCK_PROCESS;

    ext = *(ObjectClassExtension *) ((char *) object_class + byte_offset);
    while (ext && (ext->record_type != type || ext->version < version
                   || ext->record_size < record_size)) {
        ext = (ObjectClassExtension) ext->next_extension;
    }

    UNLOCK_PROCESS;
    return (XtPointer) ext;
}

static void
ComputeWindowAttributes(Widget widget,
                        XtValueMask *value_mask,
                        XSetWindowAttributes *values)
{
    XtExposeProc expose;

    *value_mask = CWEventMask | CWColormap;
    (*values).event_mask = (long) XtBuildEventMask(widget);
    (*values).colormap = widget->core.colormap;
    if (widget->core.background_pixmap != XtUnspecifiedPixmap) {
        *value_mask |= CWBackPixmap;
        (*values).background_pixmap = widget->core.background_pixmap;
    }
    else {
        *value_mask |= CWBackPixel;
        (*values).background_pixel = widget->core.background_pixel;
    }
    if (widget->core.border_pixmap != XtUnspecifiedPixmap) {
        *value_mask |= CWBorderPixmap;
        (*values).border_pixmap = widget->core.border_pixmap;
    }
    else {
        *value_mask |= CWBorderPixel;
        (*values).border_pixel = widget->core.border_pixel;
    }
    LOCK_PROCESS;
    expose = widget->core.widget_class->core_class.expose;
    UNLOCK_PROCESS;
    if (expose == (XtExposeProc) NULL) {
        /* Try to avoid redisplay upon resize by making bit_gravity the same
           as the default win_gravity */
        *value_mask |= CWBitGravity;
        (*values).bit_gravity = NorthWestGravity;
    }
}                               /* ComputeWindowAttributes */

static void
CallChangeManaged(register Widget widget)
{
    register Cardinal i;
    XtWidgetProc change_managed;
    register WidgetList children;
    int managed_children = 0;

    register CompositePtr cpPtr;
    register CompositePartPtr clPtr;

    if (XtIsComposite(widget)) {
        cpPtr = (CompositePtr) &((CompositeWidget) widget)->composite;
        clPtr = (CompositePartPtr) &((CompositeWidgetClass)
                                      widget->core.
                                      widget_class)->composite_class;
    }
    else
        return;

    children = cpPtr->children;
    LOCK_PROCESS;
    change_managed = clPtr->change_managed;
    UNLOCK_PROCESS;

    /* CallChangeManaged for all children */
    for (i = cpPtr->num_children; i != 0; --i) {
        CallChangeManaged(children[i - 1]);
        if (XtIsManaged(children[i - 1]))
            managed_children++;
    }

    if (change_managed != NULL && managed_children != 0) {
        CALLGEOTAT(_XtGeoTrace(widget, "Call \"%s\"[%d,%d]'s changemanaged\n",
                               XtName(widget),
                               widget->core.width, widget->core.height));
        (*change_managed) (widget);
    }
}                               /* CallChangeManaged */

static void
MapChildren(CompositePart *cwp)
{
    Cardinal i;
    WidgetList children;

    children = cwp->children;
    for (i = 0; i < cwp->num_children; i++) {
        Widget child = children[i];

        if (XtIsWidget(child)) {
            if (child->core.managed && child->core.mapped_when_managed) {
                XtMapWidget(children[i]);
            }
        }
    }
}                               /* MapChildren */

static Boolean
ShouldMapAllChildren(CompositePart *cwp)
{
    Cardinal i;
    WidgetList children;

    children = cwp->children;
    for (i = 0; i < cwp->num_children; i++) {
        Widget child = children[i];

        if (XtIsWidget(child)) {
            if (XtIsRealized(child) && (!(child->core.managed
                                          && child->core.
                                          mapped_when_managed))) {
                return False;
            }
        }
    }

    return True;
}                               /* ShouldMapAllChildren */

static void
RealizeWidget(Widget widget)
{
    XtValueMask value_mask;
    XSetWindowAttributes values;
    XtRealizeProc realize;
    Window window;
    Display *display;
    String class_name;
    Widget hookobj;

    if (!XtIsWidget(widget) || XtIsRealized(widget))
        return;
    display = XtDisplay(widget);

    _XtInstallTranslations(widget);

    ComputeWindowAttributes(widget, &value_mask, &values);
    LOCK_PROCESS;
    realize = widget->core.widget_class->core_class.realize;
    class_name = widget->core.widget_class->core_class.class_name;
    UNLOCK_PROCESS;
    if (realize == NULL)
        XtAppErrorMsg(XtWidgetToApplicationContext(widget),
                      "invalidProcedure", "realizeProc", XtCXtToolkitError,
                      "No realize class procedure defined", NULL, NULL);
    else {
        CALLGEOTAT(_XtGeoTrace(widget, "Call \"%s\"[%d,%d]'s realize proc\n",
                               XtName(widget),
                               widget->core.width, widget->core.height));
        (*realize) (widget, &value_mask, &values);
    }
    window = XtWindow(widget);
    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
        XtChangeHookDataRec call_data;

        call_data.type = XtHrealizeWidget;
        call_data.widget = widget;
        XtCallCallbackList(hookobj,
                           ((HookObject) hookobj)->hooks.changehook_callbacks,
                           (XtPointer) &call_data);
    }
#ifndef NO_IDENTIFY_WINDOWS
    if (_XtGetPerDisplay(display)->appContext->identify_windows) {
        int len_nm, len_cl;
        char *s;

        len_nm = widget->core.name ? (int) strlen(widget->core.name) : 0;
        len_cl = (int) strlen(class_name);
        s = __XtMalloc((unsigned) (len_nm + len_cl + 2));
        s[0] = '\0';
        if (len_nm)
            strcpy(s, widget->core.name);
        strcpy(s + len_nm + 1, class_name);
        XChangeProperty(display, window,
                        XInternAtom(display, "_MIT_OBJ_CLASS",
                                    False),
                        XA_STRING, 8, PropModeReplace, (unsigned char *) s,
                        len_nm + len_cl + 2);
        XtFree(s);
    }
#endif
#ifdef notdef
    _XtRegisterAsyncHandlers(widget);
#endif
    /* (re)register any grabs extant in the translations */
    _XtRegisterGrabs(widget);
    /* reregister any grabs added with XtGrab{Button,Key} */
    _XtRegisterPassiveGrabs(widget);
    XtRegisterDrawable(display, window, widget);

    _XtExtensionSelect(widget);

    if (XtIsComposite(widget)) {
        Cardinal i;
        CompositePart *cwp = &(((CompositeWidget) widget)->composite);
        WidgetList children = cwp->children;

        /* Realize all children */
        for (i = cwp->num_children; i != 0; --i) {
            RealizeWidget(children[i - 1]);
        }
        /* Map children that are managed and mapped_when_managed */

        if (cwp->num_children != 0) {
            if (ShouldMapAllChildren(cwp)) {
                XMapSubwindows(display, window);
            }
            else {
                MapChildren(cwp);
            }
        }
    }

    /* If this is the application's popup shell, map it */
    if (widget->core.parent == NULL && widget->core.mapped_when_managed) {
        XtMapWidget(widget);
    }
}                               /* RealizeWidget */

void
XtRealizeWidget(Widget widget)
{
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    if (XtIsRealized(widget)) {
        UNLOCK_APP(app);
        return;
    }
    CallChangeManaged(widget);
    RealizeWidget(widget);
    UNLOCK_APP(app);
}                               /* XtRealizeWidget */

static void
UnrealizeWidget(Widget widget)
{
    CompositeWidget cw;

    if (!XtIsWidget(widget) || !XtIsRealized(widget))
        return;

    /* If this is the application's popup shell, unmap it? */
    /* no, the window is being destroyed */

    /* Recurse on children */
    if (XtIsComposite(widget)) {
        Cardinal i;
        WidgetList children;

        cw = (CompositeWidget) widget;
        children = cw->composite.children;
        /* Unrealize all children */
        for (i = cw->composite.num_children; i != 0; --i) {
            UnrealizeWidget(children[i - 1]);
        }
        /* Unmap children that are managed and mapped_when_managed? */
        /* No, it's ok to be managed and unrealized as long as your parent */
        /* is unrealized. XtUnrealize widget makes sure the "top" widget */
        /* is unmanaged, we can ignore all descendents */
    }

    if (XtHasCallbacks(widget, XtNunrealizeCallback) == XtCallbackHasSome)
        XtCallCallbacks(widget, XtNunrealizeCallback, NULL);

    /* Unregister window */
    XtUnregisterDrawable(XtDisplay(widget), XtWindow(widget));

    /* Remove Event Handlers */
    /* remove grabs. Happens automatically when window is destroyed. */

    /* Destroy X Window, done at outer level with one request */
    widget->core.window = None;

    /* Removing the event handler here saves having to keep track if
     * the translation table is changed while the widget is unrealized.
     */
    _XtRemoveTranslations(widget);
}                               /* UnrealizeWidget */

void
XtUnrealizeWidget(Widget widget)
{
    Window window;
    Widget hookobj;

    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    window = XtWindow(widget);
    if (!XtIsRealized(widget)) {
        UNLOCK_APP(app);
        return;
    }
    if (widget->core.managed && widget->core.parent != NULL)
        XtUnmanageChild(widget);
    UnrealizeWidget(widget);
    if (window != None)
        XDestroyWindow(XtDisplay(widget), window);
    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
        XtChangeHookDataRec call_data;

        call_data.type = XtHunrealizeWidget;
        call_data.widget = widget;
        XtCallCallbackList(hookobj,
                           ((HookObject) hookobj)->hooks.changehook_callbacks,
                           (XtPointer) &call_data);
    }
    UNLOCK_APP(app);
}                               /* XtUnrealizeWidget */

void
XtCreateWindow(Widget widget,
               unsigned int window_class,
               Visual *visual,
               XtValueMask value_mask,
               XSetWindowAttributes *attributes)
{
    XtAppContext app = XtWidgetToApplicationContext(widget);

    LOCK_APP(app);
    if (widget->core.window == None) {
        if (widget->core.width == 0 || widget->core.height == 0) {
            Cardinal count = 1;

            XtAppErrorMsg(app,
                          "invalidDimension", "xtCreateWindow",
                          XtCXtToolkitError,
                          "Widget %s has zero width and/or height",
                          &widget->core.name, &count);
        }
        widget->core.window =
            XCreateWindow(XtDisplay(widget),
                          (widget->core.parent ?
                           widget->core.parent->core.window :
                           widget->core.screen->root),
                          (int) widget->core.x, (int) widget->core.y,
                          (unsigned) widget->core.width,
                          (unsigned) widget->core.height,
                          (unsigned) widget->core.border_width,
                          (int) widget->core.depth, window_class, visual,
                          value_mask, attributes);
    }
    UNLOCK_APP(app);
}                               /* XtCreateWindow */

/* ---------------- XtNameToWidget ----------------- */

static Widget NameListToWidget(Widget root,
                               XrmNameList names,
                               XrmBindingList bindings,
                               int in_depth, int *out_depth, int *found_depth);

typedef Widget(*NameMatchProc) (XrmNameList,
                                XrmBindingList,
                                WidgetList, Cardinal, int, int *, int *);

static Widget
MatchExactChildren(XrmNameList names,
                   XrmBindingList bindings,
                   register WidgetList children,
                   register Cardinal num,
                   int in_depth,
                   int *out_depth,
                   int *found_depth)
{
    register Cardinal i;
    register XrmName name = *names;
    Widget w, result = NULL;
    int d, min = 10000;

    for (i = 0; i < num; i++) {
        if (name == children[i]->core.xrm_name) {
            w = NameListToWidget(children[i], &names[1], &bindings[1],
                                 in_depth + 1, &d, found_depth);
            if (w != NULL && d < min) {
                result = w;
                min = d;
            }
        }
    }
    *out_depth = min;
    return result;
}

static Widget
MatchWildChildren(XrmNameList names,
                  XrmBindingList bindings,
                  register WidgetList children,
                  register Cardinal num,
                  int in_depth,
                  int *out_depth,
                  int *found_depth)
{
    register Cardinal i;
    Widget w, result = NULL;
    int d, min = 10000;

    for (i = 0; i < num; i++) {
        w = NameListToWidget(children[i], names, bindings,
                             in_depth + 1, &d, found_depth);
        if (w != NULL && d < min) {
            result = w;
            min = d;
        }
    }
    *out_depth = min;
    return result;
}

static Widget
SearchChildren(Widget root,
               XrmNameList names,
               XrmBindingList bindings,
               NameMatchProc matchproc,
               int in_depth,
               int *out_depth,
               int *found_depth)
{
    Widget w1 = NULL, w2;
    int d1, d2;

    if (XtIsComposite(root)) {
        w1 = (*matchproc) (names, bindings,
                           ((CompositeWidget) root)->composite.children,
                           ((CompositeWidget) root)->composite.num_children,
                           in_depth, &d1, found_depth);
    }
    else
        d1 = 10000;
    w2 = (*matchproc) (names, bindings, root->core.popup_list,
                       root->core.num_popups, in_depth, &d2, found_depth);
    *out_depth = (d1 < d2 ? d1 : d2);
    return (d1 < d2 ? w1 : w2);
}

static Widget
NameListToWidget(register Widget root,
                 XrmNameList names,
                 XrmBindingList bindings,
                 int in_depth,
                 int *out_depth,
                 int *found_depth)
{
    int d1, d2;

    if (in_depth >= *found_depth) {
        *out_depth = 10000;
        return NULL;
    }

    if (names[0] == NULLQUARK) {
        *out_depth = *found_depth = in_depth;
        return root;
    }

    if (!XtIsWidget(root)) {
        *out_depth = 10000;
        return NULL;
    }

    if (*bindings == XrmBindTightly) {
        return SearchChildren(root, names, bindings, MatchExactChildren,
                              in_depth, out_depth, found_depth);

    }
    else {                      /* XrmBindLoosely */
        Widget w1, w2;

        w1 = SearchChildren(root, names, bindings, MatchExactChildren,
                            in_depth, &d1, found_depth);
        w2 = SearchChildren(root, names, bindings, MatchWildChildren,
                            in_depth, &d2, found_depth);
        *out_depth = (d1 < d2 ? d1 : d2);
        return (d1 < d2 ? w1 : w2);
    }
}                               /* NameListToWidget */

Widget
XtNameToWidget(Widget root, _Xconst char *name)
{
    XrmName *names;
    XrmBinding *bindings;
    int len, depth, found = 10000;
    Widget result;

    WIDGET_TO_APPCON(root);

    len = (int) strlen(name);
    if (len == 0)
        return NULL;

    LOCK_APP(app);
    names = (XrmName *) ALLOCATE_LOCAL((unsigned) (len + 1) * sizeof(XrmName));
    bindings = (XrmBinding *)
        ALLOCATE_LOCAL((unsigned) (len + 1) * sizeof(XrmBinding));
    if (names == NULL || bindings == NULL)
        _XtAllocError(NULL);

    XrmStringToBindingQuarkList(name, bindings, names);
    if (names[0] == NULLQUARK) {
        DEALLOCATE_LOCAL((char *) bindings);
        DEALLOCATE_LOCAL((char *) names);
        UNLOCK_APP(app);
        return NULL;
    }

    result = NameListToWidget(root, names, bindings, 0, &depth, &found);

    DEALLOCATE_LOCAL((char *) bindings);
    DEALLOCATE_LOCAL((char *) names);
    UNLOCK_APP(app);
    return result;
}                               /* XtNameToWidget */

/* Define user versions of intrinsics macros */

#undef XtDisplayOfObject
Display *
XtDisplayOfObject(Widget object)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    if (XtIsSubclass(object, hookObjectClass))
        return DisplayOfScreen(((HookObject) object)->hooks.screen);
    return XtDisplay(XtIsWidget(object) ? object : _XtWindowedAncestor(object));
}

#undef XtDisplay
Display *
XtDisplay(Widget widget)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    return DisplayOfScreen(widget->core.screen);
}

#undef XtScreenOfObject
Screen *
XtScreenOfObject(Widget object)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    if (XtIsSubclass(object, hookObjectClass))
        return ((HookObject) object)->hooks.screen;
    return XtScreen(XtIsWidget(object) ? object : _XtWindowedAncestor(object));
}

#undef XtScreen
Screen *
XtScreen(Widget widget)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    return widget->core.screen;
}

#undef XtWindowOfObject
Window
XtWindowOfObject(Widget object)
{
    return XtWindow(XtIsWidget(object) ? object : _XtWindowedAncestor(object));
}

#undef XtWindow
Window
XtWindow(Widget widget)
{
    return widget->core.window;
}

#undef XtSuperclass
WidgetClass
XtSuperclass(Widget widget)
{
    WidgetClass retval;

    LOCK_PROCESS;
    retval = XtClass(widget)->core_class.superclass;
    UNLOCK_PROCESS;
    return retval;
}

#undef XtClass
WidgetClass
XtClass(Widget widget)
{
    WidgetClass retval;

    LOCK_PROCESS;
    retval = widget->core.widget_class;
    UNLOCK_PROCESS;
    return retval;
}

#undef XtIsManaged
Boolean
XtIsManaged(Widget object)
{
    Boolean retval;

    WIDGET_TO_APPCON(object);

    LOCK_APP(app);
    if (XtIsRectObj(object))
        retval = object->core.managed;
    else
        retval = False;
    UNLOCK_APP(app);
    return retval;
}

#undef XtIsRealized
Boolean
XtIsRealized(Widget object)
{
    Boolean retval;

    WIDGET_TO_APPCON(object);

    LOCK_APP(app);
    retval = XtWindowOfObject(object) != None;
    UNLOCK_APP(app);
    return retval;
}                               /* XtIsRealized */

#undef XtIsSensitive
Boolean
XtIsSensitive(Widget object)
{
    Boolean retval;

    WIDGET_TO_APPCON(object);

    LOCK_APP(app);
    if (XtIsRectObj(object))
        retval = object->core.sensitive && object->core.ancestor_sensitive;
    else
        retval = False;
    UNLOCK_APP(app);
    return retval;
}

/*
 * Internal routine; must be called only after XtIsWidget returns false
 */
Widget
_XtWindowedAncestor(register Widget object)
{
    Widget obj = object;

    for (object = XtParent(object); object && !XtIsWidget(object);)
        object = XtParent(object);

    if (object == NULL) {
        String params = XtName(obj);
        Cardinal num_params = 1;

        XtErrorMsg("noWidgetAncestor", "windowedAncestor", XtCXtToolkitError,
                   "Object \"%s\" does not have windowed ancestor",
                   &params, &num_params);
    }

    return object;
}

#undef XtParent
Widget
XtParent(Widget widget)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    return widget->core.parent;
}

#undef XtName
String
XtName(Widget object)
{
    /* Attempts to LockApp() here will generate endless recursive loops */
    return XrmQuarkToString(object->core.xrm_name);
}

Boolean
XtIsObject(Widget object)
{
    WidgetClass wc;
    String class_name;

    /* perform basic sanity checks */
    if (object->core.self != object || object->core.xrm_name == NULLQUARK)
        return False;

    LOCK_PROCESS;
    wc = object->core.widget_class;
    if (wc->core_class.class_name == NULL ||
        wc->core_class.xrm_class == NULLQUARK ||
        (class_name = XrmClassToString(wc->core_class.xrm_class)) == NULL ||
        strcmp(wc->core_class.class_name, class_name) != 0) {
        UNLOCK_PROCESS;
        return False;
    }
    UNLOCK_PROCESS;

    if (XtIsWidget(object)) {
        if (object->core.name == NULL ||
            (class_name = XrmNameToString(object->core.xrm_name)) == NULL ||
            strcmp(object->core.name, class_name) != 0)
            return False;
    }
    return True;
}

#if defined(WIN32)
static int
access_file(char *path, char *pathbuf, int len_pathbuf, char **pathret)
{
    if (access(path, F_OK) == 0) {
        if (strlen(path) < len_pathbuf)
            *pathret = pathbuf;
        else
            *pathret = XtMalloc(strlen(path));
        if (*pathret) {
            strcpy(*pathret, path);
            return 1;
        }
    }
    return 0;
}

static int
AccessFile(char *path, char *pathbuf, int len_pathbuf, char **pathret)
{
    unsigned long drives;
    int i, len;
    char *drive;
    char buf[MAX_PATH];
    char *bufp;

    /* just try the "raw" name first and see if it works */
    if (access_file(path, pathbuf, len_pathbuf, pathret))
        return 1;

#if defined(WIN32) && defined(__MINGW32__)
    /* don't try others */
    return 0;
#endif

    /* try the places set in the environment */
    drive = getenv("_XBASEDRIVE");
    if (!drive)
        drive = "C:";
    len = strlen(drive) + strlen(path);
    bufp = XtStackAlloc(len + 1, buf);
    strcpy(bufp, drive);
    strcat(bufp, path);
    if (access_file(bufp, pathbuf, len_pathbuf, pathret)) {
        XtStackFree(bufp, buf);
        return 1;
    }

    /* one last place to look */
    drive = getenv("HOMEDRIVE");
    if (drive) {
        len = strlen(drive) + strlen(path);
        bufp = XtStackAlloc(len + 1, buf);
        strcpy(bufp, drive);
        strcat(bufp, path);
        if (access_file(bufp, pathbuf, len_pathbuf, pathret)) {
            XtStackFree(bufp, buf);
            return 1;
        }
    }

    /* does OS/2 (with or with gcc-emx) have getdrives()? */
    /* tried everywhere else, go fishing */
    drives = _getdrives();
#define C_DRIVE ('C' - 'A')
#define Z_DRIVE ('Z' - 'A')
    for (i = C_DRIVE; i <= Z_DRIVE; i++) {      /* don't check on A: or B: */
        if ((1 << i) & drives) {
            len = 2 + strlen(path);
            bufp = XtStackAlloc(len + 1, buf);
            *bufp = 'A' + i;
            *(bufp + 1) = ':';
            *(bufp + 2) = '\0';
            strcat(bufp, path);
            if (access_file(bufp, pathbuf, len_pathbuf, pathret)) {
                XtStackFree(bufp, buf);
                return 1;
            }
        }
    }
    return 0;
}
#endif

static Boolean
TestFile(String path)
{
#ifndef VMS
    int ret = 0;
    struct stat status;

#if defined(WIN32)
    char buf[MAX_PATH];
    char *bufp;
    int len;
    UINT olderror = SetErrorMode(SEM_FAILCRITICALERRORS);

    if (AccessFile(path, buf, MAX_PATH, &bufp))
        path = bufp;

    (void) SetErrorMode(olderror);
#endif
    ret = (access(path, R_OK) == 0 &&   /* exists and is readable */
           stat(path, &status) == 0 &&  /* get the status */
#ifndef X_NOT_POSIX
           S_ISDIR(status.st_mode) == 0);       /* not a directory */
#else
           (status.st_mode & S_IFMT) != S_IFDIR);       /* not a directory */
#endif                          /* X_NOT_POSIX else */
    return (Boolean) ret;
#else                           /* VMS */
    return TRUE;                /* Who knows what to do here? */
#endif                          /* VMS */
}

/* return of TRUE = resolved string fit, FALSE = didn't fit.  Not
   null-terminated and not collapsed if it didn't fit */

static Boolean Resolve(register _Xconst char *source,   /* The source string */
                       register int len,        /* The length in bytes of *source */
                       Substitution sub,        /* Array of string values to substitute */
                       Cardinal num,    /* Number of substitution entries */
                       char *buf,       /* Where to put the resolved string; */
                       char collapse) { /* Character to collapse */
    register int bytesLeft = PATH_MAX;
    register char *bp = buf;

#ifndef DONT_COLLAPSE
    Boolean atBeginning = TRUE;
    Boolean prevIsCollapse = FALSE;

#define PUT(ch) \
    { \
        if (--bytesLeft == 0) return FALSE; \
        if (prevIsCollapse) \
            if ((*bp = ch) != collapse) { \
                prevIsCollapse = FALSE; \
                bp++; \
            } \
            else bytesLeft++; \
        else if ((*bp++ = ch) == collapse && !atBeginning) \
            prevIsCollapse = TRUE; \
    }
#else                           /* DONT_COLLAPSE */

#define PUT(ch) \
    { \
        if (--bytesLeft == 0) return FALSE; \
        *bp++ = ch; \
    }
#endif                          /* DONT_COLLAPSE */
#define escape '%'

    while (len--) {
#ifndef DONT_COLLAPSE
        if (*source == collapse) {
            PUT(*source);
            source++;
            continue;
        }
        else
#endif                          /* DONT_COLLAPSE */
        if (*source != escape) {
            PUT(*source);
        }
        else {
            source++;
            if (len-- == 0) {
                PUT(escape);
                break;
            }

            if (*source == ':' || *source == escape) {
                PUT(*source);
            }
            else {
                /* Match the character against the match array */
                register Cardinal j;

                for (j = 0; j < num && sub[j].match != *source; j++) {
                }

                /* Substitute the substitution string */

                if (j >= num) {
                    PUT(*source);
                }
                else if (sub[j].substitution != NULL) {
                    char *sp = sub[j].substitution;

                    while (*sp) {
                        PUT(*sp);
                        sp++;
                    }
                }
            }
        }
        source++;
#ifndef DONT_COLLAPSE
        atBeginning = FALSE;
#endif                          /* DONT_COLLAPSE */
    }
    PUT('\0');

    return TRUE;
#undef PUT
#undef escape
}

_XtString
XtFindFile(_Xconst _XtString path,
           Substitution substitutions,
           Cardinal num_substitutions,
           XtFilePredicate predicate)
{
    char *buf, *buf1, *buf2;
    _Xconst _XtString colon;
    int len;
    Boolean firstTime = TRUE;

    buf1 = __XtMalloc((unsigned) PATH_MAX);
    buf2 = __XtMalloc((unsigned) PATH_MAX);
    buf = buf1;

    if (predicate == NULL)
        predicate = TestFile;

    while (1) {
        colon = path;
        /* skip leading colons */
        while (*colon) {
            if (*colon != ':')
                break;
            colon++;
            path++;
        }
        /* now look for an un-escaped colon */
        for (; *colon; colon++) {
            if (*colon == '%' && *(path + 1)) {
                colon++;        /* bump it an extra time to skip %. */
                continue;
            }
            if (*colon == ':')
                break;
        }
        len = (int) (colon - path);
        if (Resolve(path, len, substitutions, num_substitutions, buf, '/')) {
            if (firstTime || strcmp(buf1, buf2) != 0) {
#ifdef XNL_DEBUG
                printf("Testing file %s\n", buf);
#endif                          /* XNL_DEBUG */
                /* Check out the file */
                if ((*predicate) (buf)) {
                    /* We've found it, return it */
#ifdef XNL_DEBUG
                    printf("File found.\n");
#endif                          /* XNL_DEBUG */
                    if (buf == buf1) {
                        XtFree(buf2);
                        return buf1;
                    }
                    XtFree(buf1);
                    return buf2;
                }
                if (buf == buf1)
                    buf = buf2;
                else
                    buf = buf1;
                firstTime = FALSE;
            }
        }

        /* Nope...any more paths? */

        if (*colon == '\0')
            break;
        path = colon + 1;
    }

    /* No file found */

    XtFree(buf1);
    XtFree(buf2);
    return NULL;
}

/* The implementation of this routine is operating system dependent */
/* Should match the code in Xlib _XlcMapOSLocaleName */

static String
ExtractLocaleName(String lang)
{

#if defined(hpux) || defined(CSRG_BASED) || defined(sun) || defined(SVR4) || defined(sgi) || defined(__osf__) || defined(AIXV3) || defined(ultrix) || defined(WIN32) || defined (linux)
#ifdef hpux
/*
 * We need to discriminated between HPUX 9 and HPUX 10. The equivalent
 * code in Xlib in SetLocale.c does include locale.h via X11/Xlocale.h.
 */
#include <locale.h>
#ifndef _LastCategory
    /* HPUX 9 and earlier */
#define SKIPCOUNT 2
#define STARTCHAR ':'
#define ENDCHAR ';'
#else
    /* HPUX 10 */
#define ENDCHAR ' '
#endif
#else
#ifdef ultrix
#define SKIPCOUNT 2
#define STARTCHAR '\001'
#define ENDCHAR '\001'
#else
#ifdef WIN32
#define SKIPCOUNT 1
#define STARTCHAR '='
#define ENDCHAR ';'
#define WHITEFILL
#else
#if defined(__osf__) || (defined(AIXV3) && !defined(AIXV4))
#define STARTCHAR ' '
#define ENDCHAR ' '
#else
#if defined(linux)
#define STARTSTR "LC_CTYPE="
#define ENDCHAR ';'
#else
#if !defined(sun) || defined(SVR4)
#define STARTCHAR '/'
#define ENDCHAR '/'
#endif
#endif
#endif
#endif
#endif
#endif

    String start;
    String end;
    int len;

#ifdef SKIPCOUNT
    int n;
#endif
    static char *buf = NULL;

#ifdef WHITEFILL
    char *temp;
#endif

    start = lang;
#ifdef SKIPCOUNT
    for (n = SKIPCOUNT;
         --n >= 0 && start && (start = strchr(start, STARTCHAR)); start++);
    if (!start)
        start = lang;
#endif
#ifdef STARTCHAR
    if (start && (start = strchr(start, STARTCHAR)))
#elif  defined (STARTSTR)
    if (start && (start = strstr(start, STARTSTR)))
#endif
    {
#ifdef STARTCHAR
        start++;
#elif defined (STARTSTR)
        start += strlen(STARTSTR);
#endif

        if ((end = strchr(start, ENDCHAR))) {
            len = (int) (end - start);
            XtFree(buf);
            buf = XtMalloc((Cardinal) (len + 1));
            if (buf == NULL)
                return NULL;
            strncpy(buf, start, (size_t) len);
            *(buf + len) = '\0';
#ifdef WHITEFILL
            for (temp = buf; (temp = strchr(temp, ' ')) != NULL;)
                *temp++ = '-';
#endif
            return buf;
        }
        else                    /* if no ENDCHAR is found we are at the end of the line */
            return start;
    }
#ifdef WHITEFILL
    if (strchr(lang, ' ')) {
        XtFree(buf);
        buf = strdup(lang);
        if (buf == NULL)
            return NULL;
        for (temp = buf; (temp = strchr(temp, ' ')) != NULL;)
            *temp++ = '-';
        return buf;
    }
#endif
#undef STARTCHAR
#undef ENDCHAR
#undef WHITEFILL
#endif

    return lang;
}

static void
FillInLangSubs(Substitution subs, XtPerDisplay pd)
{
    int len;
    String string;
    char *p1, *p2, *p3;
    char **rest;
    char *ch;

    if (pd->language == NULL || pd->language[0] == '\0') {
        subs[0].substitution = subs[1].substitution =
            subs[2].substitution = subs[3].substitution = NULL;
        return;
    }

    string = ExtractLocaleName(pd->language);

    if (string == NULL || string[0] == '\0') {
        subs[0].substitution = subs[1].substitution =
            subs[2].substitution = subs[3].substitution = NULL;
        return;
    }

    len = (int) strlen(string) + 1;
    subs[0].substitution = (_XtString) string;
    p1 = subs[1].substitution = XtMallocArray(3, (Cardinal) len);
    p2 = subs[2].substitution = subs[1].substitution + len;
    p3 = subs[3].substitution = subs[2].substitution + len;

    /* Everything up to the first "_" goes into p1.  From "_" to "." in
       p2.  The rest in p3.  If no delimiters, all goes into p1.  We
       assume p1, p2, and p3 are large enough. */

    *p1 = *p2 = *p3 = '\0';

    ch = strchr(string, '_');
    if (ch != NULL) {
        len = (int) (ch - string);
        (void) strncpy(p1, string, (size_t) len);
        p1[len] = '\0';
        string = ch + 1;
        rest = &p2;
    }
    else
        rest = &p1;

    /* Rest points to where we put the first part */

    ch = strchr(string, '.');
    if (ch != NULL) {
        len = (int) (ch - string);
        strncpy(*rest, string, (size_t) len);
        (*rest)[len] = '\0';
        (void) strcpy(p3, ch + 1);
    }
    else
        (void) strcpy(*rest, string);
}

/*
 * default path used if environment variable XFILESEARCHPATH
 * is not defined.  Also substituted for %D.
 * The exact value should be documented in the implementation
 * notes for any Xt implementation.
 */
static const char *
implementation_default_path(void)
{
#if defined(WIN32)
    static char xfilesearchpath[] = "";

    return xfilesearchpath;
#else
    return XFILESEARCHPATHDEFAULT;
#endif
}


/* *INDENT-OFF* */
static SubstitutionRec defaultSubs[] = {
    {'N', NULL},
    {'T', NULL},
    {'S', NULL},
    {'C', NULL},
    {'L', NULL},
    {'l', NULL},
    {'t', NULL},
    {'c', NULL}
};
/* *INDENT-ON* */

_XtString
XtResolvePathname(Display *dpy,
                  _Xconst char *type,
                  _Xconst char *filename,
                  _Xconst char *suffix,
                  _Xconst char *path,
                  Substitution substitutions,
                  Cardinal num_substitutions,
                  XtFilePredicate predicate)
{
    XtPerDisplay pd;
    static const char *defaultPath = NULL;
    const char *impl_default = implementation_default_path();
    int idef_len = (int) strlen(impl_default);
    char *massagedPath;
    int bytesAllocd, bytesLeft;
    char *ch, *result;
    Substitution merged_substitutions;
    XrmRepresentation db_type;
    XrmValue value;
    XrmName name_list[3];
    XrmClass class_list[3];
    Boolean pathMallocd = False;

    LOCK_PROCESS;
    pd = _XtGetPerDisplay(dpy);
    if (path == NULL) {
#ifndef VMS
        if (defaultPath == NULL) {
            defaultPath = getenv("XFILESEARCHPATH");
            if (defaultPath == NULL)
                defaultPath = impl_default;
        }
        path = defaultPath;
#endif                          /* VMS */
    }

    if (path == NULL)
        path = "";              /* NULL would kill us later */

    if (filename == NULL) {
        filename = XrmClassToString(pd->class);
    }

    bytesAllocd = bytesLeft = 1000;
    massagedPath = ALLOCATE_LOCAL((size_t) bytesAllocd);
    if (massagedPath == NULL)
        _XtAllocError(NULL);

    if (path[0] == ':') {
        strcpy(massagedPath, "%N%S");
        ch = &massagedPath[4];
        bytesLeft -= 4;
    }
    else
        ch = massagedPath;

    /* Insert %N%S between adjacent colons
     * and default path for %D.
     * Default path should not have any adjacent colons of its own.
     */

    while (*path != '\0') {
        if (bytesLeft < idef_len) {
            int bytesUsed = bytesAllocd - bytesLeft;
            char *new;

            bytesAllocd += 1000;
            new = __XtMalloc((Cardinal) bytesAllocd);
            strncpy(new, massagedPath, (size_t) bytesUsed);
            ch = new + bytesUsed;
            if (pathMallocd)
                XtFree(massagedPath);
            else
                DEALLOCATE_LOCAL(massagedPath);
            pathMallocd = True;
            massagedPath = new;
            bytesLeft = bytesAllocd - bytesUsed;
        }
        if (*path == '%' && *(path + 1) == ':') {
            *ch++ = '%';
            *ch++ = ':';
            path += 2;
            bytesLeft -= 2;
            continue;
        }
        if (*path == ':' && *(path + 1) == ':') {
            strcpy(ch, ":%N%S:");
            ch += 6;
            bytesLeft -= 6;
            while (*path == ':')
                path++;
            continue;
        }
        if (*path == '%' && *(path + 1) == 'D') {
            strcpy(ch, impl_default);
            ch += idef_len;
            bytesLeft -= idef_len;
            path += 2;
            continue;
        }
        *ch++ = *path++;
        bytesLeft--;
    }
    *ch = '\0';
#ifdef XNL_DEBUG
    printf("Massaged path: %s\n", massagedPath);
#endif                          /* XNL_DEBUG */

    if (num_substitutions == 0)
        merged_substitutions = defaultSubs;
    else {
        int i = XtNumber(defaultSubs);
        Substitution sub, def;

        merged_substitutions = sub = (Substitution)
            ALLOCATE_LOCAL((unsigned) (num_substitutions + (Cardinal) i) *
                           sizeof(SubstitutionRec));
        if (sub == NULL)
            _XtAllocError(NULL);
        for (def = defaultSubs; i--; sub++, def++)
            sub->match = def->match;
        for (i = (int) num_substitutions; i--;)
            *sub++ = *substitutions++;
    }
    merged_substitutions[0].substitution = (_XtString) filename;
    merged_substitutions[1].substitution = (_XtString) type;
    merged_substitutions[2].substitution = (_XtString) suffix;
    name_list[0] = pd->name;
    name_list[1] = XrmPermStringToQuark("customization");
    name_list[2] = NULLQUARK;
    class_list[0] = pd->class;
    class_list[1] = XrmPermStringToQuark("Customization");
    class_list[2] = NULLQUARK;
    if (XrmQGetResource(XrmGetDatabase(dpy), name_list, class_list,
                        &db_type, &value) && db_type == _XtQString)
        merged_substitutions[3].substitution = (char *) value.addr;
    else
        merged_substitutions[3].substitution = NULL;
    FillInLangSubs(&merged_substitutions[4], pd);

    result = XtFindFile(massagedPath, merged_substitutions,
                        num_substitutions + XtNumber(defaultSubs), predicate);

    if (merged_substitutions[5].substitution != NULL)
        XtFree((XtPointer) merged_substitutions[5].substitution);

    if (merged_substitutions != defaultSubs)
        DEALLOCATE_LOCAL(merged_substitutions);

    if (pathMallocd)
        XtFree(massagedPath);
    else
        DEALLOCATE_LOCAL(massagedPath);

    UNLOCK_PROCESS;
    return result;
}

Boolean
XtCallAcceptFocus(Widget widget, Time *time)
{
    XtAcceptFocusProc ac;
    Boolean retval;

    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    LOCK_PROCESS;
    ac = XtClass(widget)->core_class.accept_focus;
    UNLOCK_PROCESS;

    if (ac != NULL)
        retval = (*ac) (widget, time);
    else
        retval = FALSE;
    UNLOCK_APP(app);
    return retval;
}

#ifdef XT_GEO_TATTLER
/**************************************************************************
 GeoTattler:  This is used to debug Geometry management in Xt.

  It uses a pseudo resource XtNgeotattler.

  E.G. if those lines are found in the resource database:

    myapp*draw.XmScale.geoTattler: ON
    *XmScrollBar.geoTattler:ON
    *XmRowColumn.exit_button.geoTattler:ON

   then:

    all the XmScale children of the widget named draw,
    all the XmScrollBars,
    the widget named exit_button in any XmRowColumn

   will return True to the function IsTattled(), and will generate
   outlined trace to stdout.

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

#define XtNgeoTattler "geoTattler"
#define XtCGeoTattler "GeoTattler"

typedef struct {
    Boolean geo_tattler;
} GeoDataRec;

/* *INDENT-OFF* */
static XtResource geo_resources[] = {
    { XtNgeoTattler, XtCGeoTattler, XtRBoolean, sizeof(Boolean),
      XtOffsetOf(GeoDataRec, geo_tattler),
      XtRImmediate, (XtPointer) False }
};
/* *INDENT-ON* */

/************************************************************************
  This function uses XtGetSubresources to find out if a widget
  needs to be geo-spied by the caller. */
static Boolean
IsTattled(Widget widget)
{
    GeoDataRec geo_data;

    XtGetSubresources(widget, (XtPointer) &geo_data,
                      (String) NULL, (String) NULL,
                      geo_resources, XtNumber(geo_resources), NULL, 0);

    return geo_data.geo_tattler;

}                               /* IsTattled */

static int n_tab = 0;           /* not MT for now */

void
_XtGeoTab(int direction)
{                               /* +1 or -1 */
    n_tab += direction;
}

void
_XtGeoTrace(Widget widget, const char *fmt, ...)
{
    if (IsTattled(widget)) {
        va_list args;
        int i;

        va_start(args, fmt);
        for (i = 0; i < n_tab; i++)
            printf("     ");
        (void) vprintf(fmt, args);
        va_end(args);
    }
}

#endif                          /* XT_GEO_TATTLER */