File: TextStrSo.c

package info (click to toggle)
motif 2.3.8-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,432 kB
  • sloc: ansic: 452,643; sh: 4,613; makefile: 2,030; yacc: 1,604; lex: 352; cpp: 348
file content (1740 lines) | stat: -rw-r--r-- 52,214 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
/* 
 * Motif
 *
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
 *
 * These libraries and programs are free software; you can
 * redistribute them and/or modify them under the terms of the GNU
 * Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * These libraries and programs are distributed in the hope that
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with these librararies and programs; if not, write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301 USA
*/ 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#ifdef REV_INFO
#ifndef lint
static char rcsid[] = "$XConsortium: TextStrSo.c /main/14 1996/10/23 16:05:21 cde-osf $"
#endif
#endif

#define FIX_1320

#include <ctype.h>
#include <limits.h>
#include <X11/Xatom.h>
#include <X11/Xmd.h>
#include <Xm/AtomMgr.h>
#include <Xm/TextSelP.h>
#include <Xm/TextStrSoP.h>
#include <Xm/XmosP.h>
#include "XmI.h"		/* for _XmValidTimestamp() */
#include "TextI.h"
#include "TextStrSoI.h"

/********    Static Function Declarations    ********/

static void AddWidget(XmTextSource source,
		      XmTextWidget tw);
static char * _XmStringSourceGetChar(XmSourceData data,
				     XmTextPosition position);
static int CountLines(XmTextSource source,
		      XmTextPosition start,
		      unsigned long length);
static void RemoveWidget(XmTextSource source,
			 XmTextWidget tw);
static void _XmStringSourceReadString(XmTextSource source,
				      int start,
				      XmTextBlock block);
static XmTextPosition ReadSource(XmTextSource source,
				 XmTextPosition position,
				 XmTextPosition last_position,
				 XmTextBlock block);
static XmTextStatus Replace(XmTextWidget initiator,
			    XEvent *event,
			    XmTextPosition *start,
			    XmTextPosition *end,
			    XmTextBlock block,
#if NeedWidePrototypes
			    int call_callbacks);
#else
                            Boolean call_callbacks);
#endif /* NeedsWidePrototypes */
static void ScanParagraph(XmSourceData data,
			  XmTextPosition *new_position,
			  XmTextScanDirection dir,
			  int ddir,
			  XmTextPosition *last_char);
static XmTextPosition Scan(XmTextSource source,
			   XmTextPosition pos,
			   XmTextScanType sType,
			   XmTextScanDirection dir,
			   int count,
#if NeedWidePrototypes
			   int include);
#else
                           Boolean include);
#endif /* NeedWidePrototypes */
static Boolean GetSelection(XmTextSource source,
			    XmTextPosition *left,
			    XmTextPosition *right);
static void SetSelection(XmTextSource source,
			 XmTextPosition left,
			 XmTextPosition right,
			 Time set_time);

/********    End Static Function Declarations    ********/

#define TEXT_INCREMENT 1024
#define TEXT_INITIAL_INCREM 64

/* Convert a stream of bytes into a char*, BITS16*, or wchar_t* array. 
 * Return number of characters created.
 *
 * If num_chars == 1, don't add a null terminator, else if a null terminator
 * is present on the byte stream, convert it and add it to the character
 * array;  Count returned does not include NULL terminator (just like strlen).
 *
 * This routine assumes that a BITS16 is two-bytes;
 * the routine must be modified if these assumptions are incorrect.
 */

/* ARGSUSED */
int
_XmTextBytesToCharacters(char * characters, 
			 char * bytes,
			 int num_chars, 
#if NeedWidePrototypes
			 int add_null_terminator,
#else
			 Boolean add_null_terminator,
#endif /* NeedWidePrototypes */
			 int max_char_size)
{
  unsigned char * tmp_bytes;
  int num_bytes; 
  int count=0;
  BITS16 *bits16_ptr, temp_bits16;
  wchar_t *wchar_t_ptr;
  
  
  /* If 0 characters requested, or a null pointer passed, dont do
   * anything... just return 0 characters converted.
   */
  
  if (num_chars == 0 || bytes == NULL) return 0;
  
  switch (max_char_size) {
  case 1: {
    (void) memcpy((void*)characters, (void*)bytes, num_chars);
    count = num_chars;
    break;
  } /* end case 1 */
  case 2: {
    bits16_ptr = (BITS16 *) characters;
    tmp_bytes = (unsigned char*) bytes;
    for (
#ifndef NO_MULTIBYTE
	 num_bytes = mblen((char*)tmp_bytes, max_char_size), 
#else
	 num_bytes = *tmp_bytes ? 1 : 0,
#endif
	 temp_bits16 = 0; 
	 num_chars > 0 && num_bytes > 0;
	 num_chars--,
#ifndef NO_MULTIBYTE
	 num_bytes = mblen((char*)tmp_bytes, max_char_size), 
#else
	 num_bytes = *tmp_bytes ? 1 : 0,
#endif
	 temp_bits16 = 0, bits16_ptr ++) {
      if (num_bytes == 1) {
	temp_bits16 = (BITS16) *tmp_bytes++;
      } else {
	temp_bits16 = (BITS16) *tmp_bytes++;
	temp_bits16 = temp_bits16 << 8;
	temp_bits16 |= (BITS16) *tmp_bytes++;
      }
      *bits16_ptr = temp_bits16;
      count++;
    }
    /* if bytes is NULL terminated, characters should be too */
    if (add_null_terminator == True)
      *bits16_ptr = (BITS16) 0;  
    break;
  } /* end case 2 */
  default: {
    wchar_t_ptr = (wchar_t *)characters;
    count = mbstowcs(wchar_t_ptr, bytes, num_chars);
    if (add_null_terminator == True && count >= 0)
      wchar_t_ptr[count] = (wchar_t)0;
    break;
  } /* end default */
  } /* end switch */
  
  return count;
}

/* Convert an array of char*, BITS16*, or wchar_t* into a stream of bytes.
 * Return the number of bytes placed into 'bytes'
 *
 * Null terminate the byte stream - caller better have alloc'ed enough space!
 */

/* ARGSUSED */
int
_XmTextCharactersToBytes(char * bytes,
			 char * characters,
			 int num_chars,
			 int max_char_size)
{
  unsigned char *temp_char;
  unsigned char *byte_ptr;
  int count = 0;
  int i, j;
  BITS16 *bits16_ptr, temp_bits16;
  wchar_t *wchars;

  if (num_chars == 0 || characters == 0) {
    *bytes = '\0';
    return 0;
  }
  
  switch (max_char_size) {
  case 1: {
    (void) memcpy((void*)bytes, (void*)characters, num_chars);
    count = num_chars;
    break;
  } /* end case 1 */
  case 2: {
    bits16_ptr = (BITS16 *) characters;
    byte_ptr = (unsigned char*) bytes;
    temp_char = (unsigned char*) XtMalloc (max_char_size);
    for (i = 0; i < num_chars && *bits16_ptr != 0; i++, bits16_ptr++) {
      temp_bits16 = *bits16_ptr;
      /* create an array of chars; char[max_char_size - 1] contains the 
       * low order byte */
      for (j = max_char_size - 1; j >= 0; j--) {
	temp_char[j] = (unsigned char)(temp_bits16 & 0377);
	temp_bits16 = temp_bits16 >> 8;
      }
      /* start with high order byte.  If any byte is 0, skip it. */
      for (j = 0; j < max_char_size; j++) { 
	if (temp_char[j] > 0) {
	  *byte_ptr = temp_char[j];
	  byte_ptr++; count++;
	}
      }
    }
    XtFree ((char*)temp_char);
    if (count < num_chars) *byte_ptr = '\0';
    break;
  } /* end case 2 */
  default: {
    int nbytes;

    wchars = (wchar_t *)characters;
    for (i = 0; i < num_chars && *wchars != 0L; i++, wchars++) {
      nbytes = wctomb(bytes, *wchars);
      if (nbytes < 0)
	break; /* illegal char */
      count += nbytes;
      bytes += nbytes;
    }
    if (count >= 0)
      bytes[count] = '\0';
    break;
  } /* end default */
  } /* end switch */
  return (count);    /* return the number of bytes placed in bptr */
}

char * 
_XmStringSourceGetString(XmTextWidget tw,
			 XmTextPosition from,
			 XmTextPosition to,
#if NeedWidePrototypes
			 int want_wchar)
#else
                         Boolean want_wchar)
#endif /* NeedWidePrototypes */
{
  char *buf;
  wchar_t *wc_buf;
  XmTextBlockRec block;
  int destpos;
  XmTextPosition pos, ret_pos;
  int return_val = 0;

  destpos = 0;
  if (!want_wchar) {
    /* NOTE: to - from could result in a truncated long. */
    buf = XtMalloc(((int)(to - from) + 1) * (int)tw->text.char_size);
    for (pos = from; pos < to; ) {
      pos = ReadSource(tw->text.source, pos, to, &block);
      if (block.length == 0)
	break;
      
      (void)memcpy((void*)&buf[destpos], (void*)block.ptr, block.length);
      destpos += block.length;
    }
    buf[destpos] = 0;
    return buf;
  } else { /* want buffer of wchar_t * data */
    /* NOTE: to - from could result in a truncated long. */
    buf = XtMalloc(((int)(to - from) + 1) * sizeof(wchar_t));
    wc_buf = (wchar_t *)buf;
    for (pos = from; pos < to; ) {
      ret_pos = ReadSource(tw->text.source, pos, to, &block);
      if (block.length == 0)
	break;
      
      /* NOTE: ret_pos - pos could result in a truncated long. */
      return_val =  mbstowcs(&wc_buf[destpos], block.ptr,
			     (unsigned int) (ret_pos - pos));
      if (return_val > 0) destpos += return_val;
      pos = ret_pos;
    }
    wc_buf[destpos] = (wchar_t)0L;
    return ((char*)wc_buf);
  }
}


static void 
AddWidget(XmTextSource source,
	  XmTextWidget tw)
{
  XmSourceData data = source->data;
  data->numwidgets++;
  data->widgets = (XmTextWidget *)
    XtRealloc((char *) data->widgets,
	      (unsigned) (sizeof(XmTextWidget) * data->numwidgets));
  data->widgets[data->numwidgets - 1] = tw;
  
  if (data->numwidgets == 1)
    _XmTextSetHighlight((Widget) tw, 0, tw->text.last_position, 
		       XmHIGHLIGHT_NORMAL);
  else {
    tw->text.highlight.list = (_XmHighlightRec *)
      XtRealloc((char *) tw->text.highlight.list, 
		data->widgets[0]->text.highlight.maximum *
		sizeof(_XmHighlightRec));
    tw->text.highlight.maximum = data->widgets[0]->text.highlight.maximum;
    tw->text.highlight.number = data->widgets[0]->text.highlight.number;
    memmove((void *) tw->text.highlight.list, 
	    (void *) data->widgets[0]->text.highlight.list,
	    (size_t) data->widgets[0]->text.highlight.number *
	    sizeof(_XmHighlightRec));
  }
  
  
  if (data->hasselection && data->numwidgets == 1) {
    Time select_time = XtLastTimestampProcessed(XtDisplay((Widget)tw));
    if (!select_time) select_time = _XmValidTimestamp((Widget)tw);
    if (!XmePrimarySource((Widget) data->widgets[0], select_time)) {
      (*source->SetSelection)(source, 1, 0, select_time);
    } else {
      XmAnyCallbackStruct cb;
      
      data->prim_time = select_time;
      cb.reason = XmCR_GAIN_PRIMARY;
      cb.event = NULL;
      XtCallCallbackList ((Widget) data->widgets[0],
			  data->widgets[0]->text.gain_primary_callback,
			  (XtPointer) &cb);
    }
  }
}

/********************************<->***********************************/
static char * 
_XmStringSourceGetChar(XmSourceData data,
		       XmTextPosition position)       /* starting position */
{
  /* gap_size is the number of character in the gap, not number of bytes */
  register int gap_size;
  register XmTextPosition char_pos;
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  int char_size;
  
  if (tw->text.char_size > 1) {
    if (tw->text.char_size == 2)
      char_size = 2;
    else
      char_size = sizeof(wchar_t);
    char_pos = position * char_size;
    
    /* regardless of what it contains, data->ptr is treated as a char * ptr */
    if (data->ptr + char_pos < data->gap_start)
      return (&data->ptr[char_pos]);
    
    gap_size = (data->gap_end - data->gap_start) / char_size;
    if (position + gap_size >= data->maxlength)
      return ("");
    return (&data->ptr[(position + gap_size) * char_size]);
  } else {
    char_pos = position;
    /* regardless of what it contains, data->ptr is treated as a char * ptr */
    if (data->ptr + char_pos < data->gap_start)
      return (&data->ptr[char_pos]);
    
    gap_size = (data->gap_end - data->gap_start);
    if (char_pos + gap_size >= data->maxlength)
      return ("");
    return (&data->ptr[(char_pos + gap_size)]);
  }
}


/*DELTA: length IS NOW TREATED AS NUMBER OF CHARACTERS - CALLERS MUST CHANGE*/
static int 
CountLines(XmTextSource source,
	   XmTextPosition start,
	   unsigned long length)
{
  XmSourceData data = source->data;
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  int num_lines = 0;
  unsigned long seg_length;
  char *ptr;
  BITS16 *bits16_ptr, *bits16_gap_start, *bits16_gap_end;
  wchar_t *wchar_t_ptr, *wchar_t_gap_start, *wchar_t_gap_end;
  
  /* verify that the 'start' and 'length' parameters are reasonable */
  
  if (start + length > data->length)
    length = data->length - start;
  if (length == 0) return num_lines;
  
  seg_length = (data->gap_start - data->ptr) / (tw->text.char_size < 3 ?
						(int)tw->text.char_size :
						sizeof(wchar_t));
  
  /* make sure the segment length is not greater than the length desired */
  if (length < seg_length) seg_length = length;
  
  switch ((int)tw->text.char_size) {
  case 1: {
    /* setup the variables for the search of new lines before the gap */
    ptr = data->ptr + start;
    
    /* search up to gap */
    while (seg_length--) {
      if (*ptr++ == *(data->PSWC_NWLN)) ++num_lines;
    }
    
    /* check to see if we need more data after the gap */
    if ((int)length > data->gap_start - (data->ptr + start)) {
      if (data->gap_start - (data->ptr + start) > 0) /* if we searched
						      * before gap,
						      * adjust length */
	length -= data->gap_start - (data->ptr + start);
      ptr = data->gap_end;
      
      /* continue search till length is completed */
      while (length--) {
	if (*ptr++ == *(data->PSWC_NWLN)) ++num_lines;
      }
    }
    break;
  } /* end case 1 */
  case 2: {
    /* setup the variables for the search of new lines before the gap */
    bits16_ptr = (BITS16 *) data->ptr;
    bits16_gap_start = (BITS16 *) data->gap_start;
    bits16_gap_end = (BITS16 *) data->gap_end;
    bits16_ptr += start;
    
    /* search up to gap */
    while (seg_length--) {
      if (*bits16_ptr++ == *(BITS16 *)(data->PSWC_NWLN)) ++num_lines;
    }
    
    /* check to see if we need more data after the gap */
    if ((int)length > bits16_gap_start - ((BITS16 *)data->ptr + start)) {
      /* if we searched before gap, adjust length */
      if (bits16_gap_start - ((BITS16 *)data->ptr + start) > 0)
	length -= bits16_gap_start - ((BITS16 *)data->ptr + start);
      bits16_ptr = bits16_gap_end;
      
      /* continue search till length is completed */
      while (length--) {
	if (*bits16_ptr++ == *(BITS16 *)(data->PSWC_NWLN)) ++num_lines;
      }
    }
    break;
  } /* end case 2 */
  default: {
    /* setup the variables for the search of new lines before the gap */
    wchar_t_ptr = (wchar_t *) data->ptr;
    wchar_t_gap_start = (wchar_t *) data->gap_start;
    wchar_t_gap_end = (wchar_t *) data->gap_end;
    wchar_t_ptr += start;
    
    /* search up to gap */
    while (seg_length--) {
      if (*wchar_t_ptr++ == *(wchar_t *)(data->PSWC_NWLN)) ++num_lines;
    }
    
    /* check to see if we need more data after the gap */
    if ((int)length > wchar_t_gap_start - ((wchar_t *)data->ptr + start)) {
      /* if we searched before gap, adjust length */
      if (wchar_t_gap_start - ((wchar_t *)data->ptr + start) > 0)
	length -= wchar_t_gap_start - ((wchar_t *)data->ptr + start);
      wchar_t_ptr = wchar_t_gap_end;
      
      /* continue search till length is completed */
      while (length--) {
	if (*wchar_t_ptr++ == *(wchar_t *)(data->PSWC_NWLN)) ++num_lines;
      }
    }
    break;
  } /* end default */
  } /* end switch */
  return num_lines;
}

static void 
RemoveWidget(XmTextSource source,
	     XmTextWidget tw)
{
  XmSourceData data = source->data;
  int i;
  for (i=0; i<data->numwidgets; i++) {
    if (data->widgets[i] == tw) {
      XmTextPosition left, right;
      Boolean had_selection = False;
      Time select_time =
	XtLastTimestampProcessed(XtDisplay((Widget)tw));
      
      if (data->hasselection) {
	(*source->GetSelection)(source, &left, &right);
	(*source->SetSelection)(source, 1, -999, select_time);
	had_selection = True;
      }
      data->numwidgets--;
      data->widgets[i] = data->widgets[data->numwidgets];
      if (i == 0 && data->numwidgets > 0 && had_selection)
	(*source->SetSelection)(source, left, right, select_time);
      if (data->numwidgets == 0) _XmStringSourceDestroy(source);
      return;
    }
  }
}

Boolean *
_XmStringSourceGetPending(XmTextWidget tw)
{
  Boolean *pending;
  XmSourceData data = tw->text.source->data;
  int i;
  
  pending = (Boolean *)XtMalloc(data->numwidgets*sizeof(Boolean));
  for (i=0; i<data->numwidgets; i++) 
    pending[i] = ((XmTextWidget)data->widgets[i])->text.pendingoff;

  return pending;
}

void
_XmStringSourceSetPending(XmTextWidget tw,
			  Boolean *pending)
{
  XmSourceData data = tw->text.source->data;
  int i;
  
  if ((long)pending > 1)
    for (i=0; i<data->numwidgets; i++) 
      ((XmTextWidget)data->widgets[i])->text.pendingoff = pending[i];
  else
    for (i=0; i<data->numwidgets; i++) 
      ((XmTextWidget)data->widgets[i])->text.pendingoff = 
	(Boolean)(long)pending;
}

/*
 * Determines where to move the gap and calls memmove to move the gap.
 */
/********************************<->***********************************/
void 
_XmStringSourceSetGappedBuffer(XmSourceData data,
			       XmTextPosition position) /* starting position */
{
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  int count, char_size = (tw->text.char_size < 3 ? 
			  (int)tw->text.char_size :
			  sizeof(wchar_t));
  
  /* if no change in gap placement, return */
  if (data->ptr + (position * char_size) == data->gap_start)
    return;
  
  if (data->ptr + (position * char_size) < data->gap_start) {
    /* move gap to the left */
    count = data->gap_start - 
      (data->ptr + (position * char_size));
    memmove(data->gap_end - count, data->ptr + (position*char_size), count);
    data->gap_start -= count; /* ie, data->gap_start = position; */
    data->gap_end -= count;   /* ie, data->gap_end = position + gap_size; */
  } else {
    /* move gap to the right */
    count = (data->ptr + 
	     (position * char_size)) - data->gap_start;
    memmove(data->gap_start, data->gap_end, count);
    data->gap_start += count; /* ie, data->gap_start = position; */
    data->gap_end += count;   /* ie, data->gap_end = position + gap_size; */
  }
}

/********************************<->***********************************/
/* The only caller of this routine expects to get char* in block */
static void 
_XmStringSourceReadString(XmTextSource source,
			  int start,
			  XmTextBlock block)
{
  XmSourceData data = source->data;
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  int gap_size = data->gap_end - data->gap_start;
  int byte_start = start * (tw->text.char_size < 3 ?
			    (int)tw->text.char_size :
			    sizeof(wchar_t));
  
  if (data->ptr + byte_start + block->length <= data->gap_start)
    block->ptr = data->ptr + byte_start;
  else if (data->ptr + byte_start + gap_size >= data->gap_end)
    block->ptr = data->ptr + byte_start + gap_size;
  else {
    block->ptr = data->ptr + byte_start;
    block->length = data->gap_start - (data->ptr + byte_start);
  }
}

/* Caller wants block to contain char*; _XmStringSourceReadString provides
 * char*, BITS16* or wchar_t*; so we need to modify what it gives us.
 */
static XmTextPosition 
ReadSource(XmTextSource source,
	   XmTextPosition position,       /* starting position */
	   XmTextPosition last_position,  /* The last position we're interested
					     in.  Don't return info about any
					     later positions. */
	   XmTextBlock block)            /* RETURN: text read in */
{
  XmTextPosition return_pos;
  int num_bytes;
  XmSourceData data = source->data;
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  int char_size = (tw->text.char_size < 3 ?
		   (int)tw->text.char_size :
		   sizeof(wchar_t));
  
  if (last_position > data->length) last_position = data->length;
  /* NOTE: the length calculation could result in a truncated long */
  block->length = (int)((last_position - position) * char_size);
  if (block->length < 0 ) block->length = 0;
  block->format = XmFMT_8_BIT;
  _XmStringSourceReadString(source, (int)position, block);
  
  if (block->length > 0) {
    if (data->old_length == 0) {
      data->value = (char *) 
	XtMalloc((unsigned)(block->length + 1) * (int)tw->text.char_size);
      data->old_length = block->length;
    } else if (block->length > data->old_length) {
      data->value = XtRealloc(data->value, (unsigned)
			      ((block->length + 1) * (int)tw->text.char_size));
      data->old_length = block->length;
    }
    
    if ((int)tw->text.char_size == 1) {
      return_pos = position + block->length;
    } else {
      return_pos = position + (block->length / char_size);
      num_bytes = _XmTextCharactersToBytes(data->value, block->ptr, 
					   block->length / char_size,
					   (int)tw->text.char_size);
      block->length = num_bytes;
      block->ptr = data->value;
    }
    return return_pos;
  } else
    return 0;
}

void
_XmTextValidate(XmTextPosition * start,
		XmTextPosition * end,
		int maxsize)
{
  if (*start < 0) *start = 0;
  if (*start > maxsize) {
    *start = maxsize;
  }
  if (*end < 0) *end = 0;
  if (*end > maxsize) {
    *end = maxsize;
  }
  
  if (*start > *end) { 
    XmTextPosition tmp; /* tmp variable for swapping positions */
    tmp = *end;    
    *end = *start;
    *start = tmp;
  }
}

Boolean 
_XmTextModifyVerify(XmTextWidget initiator,
		    XEvent *event,
		    XmTextPosition *start,
		    XmTextPosition *end,
		    XmTextPosition *cursorPos,	/* RETURN, may be NULL if not */
		    XmTextBlock block,
		    XmTextBlock newblock,	/* RETURN */
		    Boolean *freeBlock)
{
  register XmSourceData data = initiator->text.source->data;
  register long delta;
  register int block_num_chars;  /* number of characters in the block */
  XmTextPosition newInsert = initiator->text.cursor_position;
  XmTextVerifyCallbackStruct tvcb;
  XmTextVerifyCallbackStructWcs wcs_tvcb;
  XmTextBlockRecWcs wcs_newblock;
  
  *freeBlock = False;
  
  if (*start == *end && block->length == 0) return False;
  
  _XmTextValidate(start, end, data->length);
  
    newblock->length = block->length; 	/* RETURNed values */
    newblock->format = block->format; 
    newblock->ptr = block->ptr; 
    
    if (!initiator->text.modify_verify_callback &&
        !initiator->text.wcs_modify_verify_callback) 
    {
        /* we have neither callback, so do expensive 
        ** computation only if cursorPos is needed 
        */
        if (cursorPos)
  	{
    	block_num_chars = _XmTextCountCharacters(block->ptr, block->length);
    	*cursorPos = *start + block_num_chars;
  	}
      return True;
    }
  
    /* cursorPos may be needed to be returned. If not, and if the text is
    ** not editable, can drop out now
    */
    if (!cursorPos && !data->editable)
      return False;
  
    /* there is at least one callback, so block_num_chars is needed; 
    ** this is the potentially-expensive operation that we're trying to avoid 
    */
    block_num_chars = _XmTextCountCharacters(block->ptr, block->length);
    if (cursorPos) *cursorPos = *start + block_num_chars;
    
    if (!data->editable)	/* if cursorPos was needed, it's set; can now drop out */
      return False;
  
    /* we have at least one callback on editable text, and cursorPos may or may
    ** not need to be set. block_num_chars has been set, so perform some other 
    ** quick evaluations on whether or not the modify is reasonable. Then
    ** call one or both of the callbacks, and if necessary reset cursorPos.
    */
  
    delta = block_num_chars - (*end - *start);
    if (delta > 0 && (data->length + delta > data->maxallowed))
      return False;
  
  /* If both modify_verify and modify_verify_wcs are registered:
   *    - first call the char* callback, then
   *    - pass the modified data from the char* callback to the
   *      wchar_t callback.
   * If programmers set both callback lists, they get's what they asked for.
   */
  
  wcs_newblock.wcsptr = (wchar_t *)NULL;
  wcs_newblock.length = 0;
  
  /* If there are char* callbacks registered, call them. */
  if (initiator->text.modify_verify_callback) {
    /* Fill in the block to pass to the callback. */
    if (block->length) {
      newblock->ptr = (char *) XtMalloc(block->length + 1);
      *freeBlock = True;
      (void) memcpy((void*) newblock->ptr, (void*) block->ptr,
		    block->length);
      newblock->ptr[block->length] = '\0';
    }
    
    /* Call Verification Callback to indicate that text is being modified */
    tvcb.reason = XmCR_MODIFYING_TEXT_VALUE;
    tvcb.event = event;
    tvcb.currInsert = (XmTextPosition) (initiator->text.cursor_position);
    tvcb.newInsert = (XmTextPosition) (initiator->text.cursor_position);
    tvcb.startPos = *start;
    tvcb.endPos = *end;
    tvcb.doit = True;
    tvcb.text = newblock;
    XtCallCallbackList ((Widget) initiator,
			initiator->text.modify_verify_callback,
			(XtPointer) &tvcb);
    /* If doit flag is false, application wants to negate the action,
     * so free allocate space and return False.
     */
    if (!tvcb.doit) {
      if (newblock->ptr && newblock->ptr != block->ptr) 
	XtFree(newblock->ptr);
      *freeBlock = False;
      return False;
    } else {
      *start = tvcb.startPos;
      *end = tvcb.endPos;
      newInsert = tvcb.newInsert;
      _XmTextValidate (start, end, data->length);
      if (tvcb.text != newblock || tvcb.text->ptr != newblock->ptr) {
	newblock->length = tvcb.text->length;
	if (newblock->ptr && newblock->ptr != block->ptr) 
	  XtFree(newblock->ptr);
	*freeBlock = False;
	if (newblock->length) {
	  newblock->ptr = XtMalloc(newblock->length + 1);
	  *freeBlock = True;
	  (void)memcpy((void*)newblock->ptr, (void*)tvcb.text->ptr, 
		       tvcb.text->length);
	} else newblock->ptr = NULL;
      }
      newblock->format = tvcb.text->format;
      block_num_chars = _XmTextCountCharacters(newblock->ptr,
					       newblock->length);
      
      delta = block_num_chars - (*end - *start);
      
      if (delta > 0 && data->length + delta > data->maxallowed &&
	  (!UnderVerifyPreedit(initiator))) {
	if (newblock->ptr && newblock->ptr != block->ptr) 
	  XtFree(newblock->ptr);
	*freeBlock = False;
	return False;
      }
    }
  }  /* end if there are char* modify verify callbacks */
  
  if (initiator->text.wcs_modify_verify_callback) {
    wcs_newblock.wcsptr = (wchar_t *)XtMalloc((unsigned)sizeof(wchar_t) *
					      (newblock->length + 1));
    wcs_newblock.length = mbstowcs(wcs_newblock.wcsptr, newblock->ptr,
				   block_num_chars);
    if (wcs_newblock.length < 0) wcs_newblock.length = 0;
    wcs_tvcb.reason = XmCR_MODIFYING_TEXT_VALUE;
    wcs_tvcb.event = event;
    wcs_tvcb.currInsert = initiator->text.cursor_position;
    wcs_tvcb.newInsert = initiator->text.cursor_position;
    wcs_tvcb.startPos = *start;
    wcs_tvcb.endPos = *end;
    wcs_tvcb.doit = True;
    wcs_tvcb.text = &wcs_newblock;
    
    XtCallCallbackList((Widget) initiator,
		       initiator->text.wcs_modify_verify_callback,
		       (XtPointer) &wcs_tvcb);
    if (!wcs_tvcb.doit) {
      if (newblock->ptr && newblock->ptr != block->ptr)
	XtFree(newblock->ptr);
      *freeBlock = False;
      if (wcs_newblock.wcsptr) XtFree((char*)wcs_newblock.wcsptr);
      return False;
    } else {
      *start = wcs_tvcb.startPos;
      *end = wcs_tvcb.endPos;
      newInsert = wcs_tvcb.newInsert;
      _XmTextValidate (start, end, data->length);
      /* use newblock as a temporary holder and put the char*
       * data there */
      if (newblock->ptr && newblock->ptr != block->ptr) {
	XtFree(newblock->ptr);
	newblock->ptr = NULL;
      }
      *freeBlock = False;
      if (wcs_tvcb.text->length) {
	newblock->ptr = (char*) XtMalloc((unsigned)
					 (1 + wcs_tvcb.text->length) *
					 (int)initiator->text.char_size);
	*freeBlock = True;
	wcs_tvcb.text->wcsptr[wcs_tvcb.text->length] = (wchar_t) 0L;
	/* NOTE: wcstombs returns a long which could be truncated */
	newblock->length = (int) wcstombs(newblock->ptr, 
					  wcs_tvcb.text->wcsptr,
					  (wcs_tvcb.text->length + 1) *
					  (int)initiator->text.char_size);
	if (newblock->length < 0) newblock->length = 0;
      } else {
	newblock->ptr = NULL;
	newblock->length = 0;
      }
      
      block_num_chars = wcs_tvcb.text->length;
      delta = block_num_chars - (*end - *start);
      
      /* if the wcstombs found bad data, newblock->length is negative */
      if ((delta > 0 && data->length + delta > data->maxallowed &&
	  (!UnderVerifyPreedit(initiator))) ||
	  newblock->length < 0) {
	
	if (newblock->ptr && newblock->ptr != block->ptr)
	  XtFree(newblock->ptr);
	*freeBlock = False;
	if (wcs_newblock.wcsptr) XtFree((char*)wcs_newblock.wcsptr);
	return False;
      }
    }
    
    /* If we alloced space for the wcs_newblock, we need to clean it up */
    if (wcs_newblock.wcsptr) XtFree((char*)wcs_newblock.wcsptr);
    
  }  /* end if there are wide char modify verify callbacks */
  
  if (cursorPos)
  {
	  if (initiator->text.cursor_position != newInsert)	/* true only if we have callbacks */
	  {
	    if (newInsert > data->length + delta) {
	      *cursorPos = data->length + delta;
	    } else if (newInsert < 0) {
	      *cursorPos = 0;
	    } else {
	      *cursorPos = newInsert;
	    }
	  }
	  else
	    *cursorPos = *start + block_num_chars;
  }
  
  return True;
}

/*ARGSUSED*/
static XmTextStatus 
Replace(XmTextWidget initiator,
        XEvent * event,		/* unused */
        XmTextPosition *start,
        XmTextPosition *end,
        XmTextBlock block,
#if NeedWidePrototypes
        int call_callbacks)	/* unused */
#else
        Boolean call_callbacks)	/* unused */
#endif
{
  register XmSourceData data = initiator->text.source->data;
  register int i;
  register long delta;
  register int block_num_chars;  /* number of characters in the block */
  int gap_size;
  int old_maxlength;
  int char_size = (initiator->text.char_size < 3 ?
		   (int)initiator->text.char_size :
		   sizeof(wchar_t));
  
  if (*start == *end && block->length == 0) return EditReject;
  
  _XmTextValidate(start, end, data->length);
  
  block_num_chars = _XmTextCountCharacters(block->ptr, block->length);
  delta = block_num_chars - (*end - *start);
  
  if (!data->editable ||
      (delta > 0 && data->length + delta > data->maxallowed &&
	(!UnderVerifyPreedit(initiator))))
    return EditError;
  
  /**********************************************************************/
  
  initiator->text.output->DrawInsertionPoint(initiator,
					     initiator->text.cursor_position,
					     off);
  
  /* Move the gap to the editing position (*start). */
  _XmStringSourceSetGappedBuffer(data, *start);
  
  for (i=0; i<data->numwidgets; i++) {
    _XmTextDisableRedisplay(data->widgets[i], TRUE);
    if (data->hasselection)
      _XmTextSetHighlight((Widget)data->widgets[i], data->left, 
			 data->right, XmHIGHLIGHT_NORMAL);
  }
  
  old_maxlength = data->maxlength;
  if (data->length + delta >= data->maxlength) {
    int gap_start_offset, gap_end_offset;
    
    while (data->length + delta >= data->maxlength) {
      if (data->maxlength < TEXT_INCREMENT)
	data->maxlength *= 2;
      else
	data->maxlength += TEXT_INCREMENT;
    }
    
    gap_start_offset = data->gap_start - data->ptr;
    gap_end_offset = data->gap_end - data->ptr;
    data->ptr = XtRealloc(data->ptr, (unsigned) 
			  ((data->maxlength) * char_size));
    data->gap_start = data->ptr + gap_start_offset;
    data->gap_end = data->ptr + gap_end_offset +
      (char_size * (data->maxlength - old_maxlength));
    if (gap_end_offset != (old_maxlength * char_size))
      memmove(data->gap_end, data->ptr + gap_end_offset, 
	      (char_size * old_maxlength) - gap_end_offset);
    /* Do something to move the allocated space into the buffer */
  } 
  
  /* NOTE: the value of delta could be truncated by cast to int. */
  data->length += (int) delta;
  
  if (data->hasselection && *start < data->right && *end > data->left) {
    if (*start <= data->left) {
      if (*end < data->right) {
	data->left = *end; /* delete encompasses left half of the
			      selection so move left endpoint */
      } else {
	data->right = data->left; /* delete encompasses the selection
				     so set selection to NULL */
      }
    } else {
      if (*end >= data->right) {
	data->right = *start; /* delete encompasses the right half of the
				 selection so move right endpoint */
      } else {
	data->right -= (*end - *start); /* delete is completely within the
					   selection so shrink the selection */
      }
    }
  }
  
  /* delete data */
  gap_size = data->gap_end - data->gap_start;
  /* expand the end of the gap to the right */
  if ((data->ptr + gap_size + (*end * char_size)) > data->gap_end)
    data->gap_end += ((*end - *start) * char_size);
  
  /* add data */
  /* copy the data into the gap_start and increment the gap start pointer */
  /* convert data from char* to characters and copy into the gapped buffer */
  if ((int)initiator->text.char_size == 1) {
    for (i=0; i < block->length; i++) {
      /* if (data->gap_start == data->gap_end) break; */
      *data->gap_start++ = block->ptr[i];
    }
    
  } else {
    data->gap_start += char_size * 
      _XmTextBytesToCharacters(data->gap_start,
			       &block->ptr[0], 
			       block_num_chars, False,
			       (int)initiator->text.char_size);
  }
  
  if (data->hasselection && data->left != data->right) {
    if (*end <= data->left) {
      data->left += delta;
      data->right += delta;
    }
    if (data->left > data->right)
      data->right = data->left;
  }
  
  for (i=0; i<data->numwidgets; i++) {
    _XmTextInvalidate(data->widgets[i], *start, *end, delta);
    _XmTextUpdateLineTable((Widget) data->widgets[i], *start,
			   *end, block, True);
    if (data->hasselection)
      _XmTextSetHighlight((Widget)data->widgets[i], data->left, 
			 data->right, XmHIGHLIGHT_SELECTED);
    
    _XmTextEnableRedisplay(data->widgets[i]);
  }
  initiator->text.output->DrawInsertionPoint(initiator,
					     initiator->text.cursor_position, 
					     on);
  
  if (data->maxlength != TEXT_INITIAL_INCREM &&
      ((data->maxlength > TEXT_INCREMENT &&
	data->length <= data->maxlength - TEXT_INCREMENT) ||
       data->length <= data->maxlength >> 1)) {
    /* Move the gap to the last position. */
    _XmStringSourceSetGappedBuffer(data, data->length);
    
    data->maxlength = TEXT_INITIAL_INCREM;
    
    while (data->length >= data->maxlength) {
      if (data->maxlength < TEXT_INCREMENT)
	data->maxlength *= 2;
      else
	data->maxlength += TEXT_INCREMENT;
    }
    
    data->ptr = XtRealloc(data->ptr, (unsigned) 
			  ((data->maxlength) * char_size));
    data->gap_start = data->ptr + (data->length * char_size);
    data->gap_end = data->ptr + ((data->maxlength - 1) * char_size);
  }
  
  return EditDone;
}

#define Increment(data, position, direction)\
{\
  if (direction == XmsdLeft) {\
    if (position > 0) \
      position--;\
  } else {\
    if (position < data->length)\
      position++;\
  }\
}

#define Look(data, position, dir) \
    ((dir == XmsdLeft) \
      ? ((position) ? _XmStringSourceGetChar(data, position - 1) \
	            : NULL) \
      : ((position == data->length) ? NULL \
	                            : _XmStringSourceGetChar(data, position)))
   
static void 
ScanParagraph(XmSourceData data,
	      XmTextPosition *new_position,
	      XmTextScanDirection dir,
	      int ddir,
	      XmTextPosition *last_char)
{
  Boolean found = False;
  XmTextPosition position = *new_position;
  char mb_char[1 + MB_LEN_MAX];
  char * c;
  
  while (position >= 0 && position <= data->length) {
    /* DELTA: Look now returns a pointer */
    /* DELTA: EFFECIENCY: LEAVE AS SHORT*, INT*, ... COMPARE TO PSWC_NWLN */
    c = Look(data, position, dir);
    (void) _XmTextCharactersToBytes(mb_char, c, 1, 
				    (int)data->widgets[0]->text.char_size);
    if (*mb_char == '\n') {
      /* DELTA: Look now returns a pointer */
      c = Look(data, position + ddir, dir);
      (void) _XmTextCharactersToBytes(mb_char, c, 1,
				      (int)data->widgets[0]->text.char_size);
      while (isspace((unsigned char)*mb_char)) {
	if (*mb_char == '\n') {
	  found = True;
	  while (isspace((unsigned char)*mb_char)) {
	    /* DELTA: Look now returns a pointer */
	    c = Look(data, position + ddir, dir);
	    (void) _XmTextCharactersToBytes(mb_char, c, 1,
					(int)data->widgets[0]->text.char_size);
	    Increment(data, position, dir);
	  }
	  break;
	}
	/* DELTA: Look now returns a pointer */
	c = Look(data, position + ddir, dir);
	(void) _XmTextCharactersToBytes(mb_char, c, 1,
					(int)data->widgets[0]->text.char_size);
	Increment(data, position, dir);
	/* BEGIN 3145 fix -- Do not bypass a nonspace character */
	if (!isspace((unsigned char)*c))
	  *last_char = (position) + ddir;
	/* END 3145 */
      }
      if (found) break;
    } else if (!isspace((unsigned char)*mb_char)) {
      *last_char = (position) + ddir;
    }
    
    if(((dir == XmsdRight) && (position == data->length)) ||
       ((dir == XmsdLeft) && (position == 0)))
      break;
    Increment(data, position, dir);
  }
  
  *new_position = position;
}

static XmTextPosition 
Scan(XmTextSource source,
     XmTextPosition pos,
     XmTextScanType sType,
     XmTextScanDirection dir,
     int count,
#if NeedWidePrototypes
     int include)
#else
     Boolean include)
#endif /* NeedWidePrototypes */
{
  register long whiteSpace = -1;
  register XmTextPosition position = pos;
  register int i;
  XmTextPosition temp;
  XmSourceData data = source->data;
  XmTextWidget tw = (XmTextWidget)data->widgets[0];
  char * c;
  BITS16 * bits16_ptr;
  wchar_t * wchar_t_ptr;
  char mb_char[1 + MB_LEN_MAX];
  Boolean start_is_mb, cur_is_mb;  /* False == 1-byte char, else multi-byte */
  int num_bytes = 0;
  int ddir = (dir == XmsdRight) ? 1 : -1;
  
  switch (sType) {
  case XmSELECT_POSITION: 
    if (!include && count > 0)
      count -= 1;
    for (i = 0; i < count; i++) {
      Increment(data, position, dir);
    }
    break;
  case XmSELECT_WHITESPACE: 
  case XmSELECT_WORD:
    if (tw->text.char_size == 1) {
      char * c;
      for (i = 0; i < count; i++) {
	whiteSpace = -1;
	while (position >= 0 && position <= data->length) {
	  c = Look(data, position, dir);
	  if (c && isspace((unsigned char)*c)) {
	    if (whiteSpace < 0) whiteSpace = position;
#ifdef FIX_1320
	    if (*c == '\n') break;
#endif
	  } else if (whiteSpace >= 0)
	    break;
	  position += ddir;
	}
      }
    } else {
      for (i = 0; i < count; i++) {
	whiteSpace = -1;
	num_bytes = _XmTextCharactersToBytes(mb_char,
					     Look(data, position, dir),
					     1,(int)tw->text.char_size);
	start_is_mb = (num_bytes < 2 ? False : True);
	while (position >= 0 && position <= data->length) {
	  num_bytes = _XmTextCharactersToBytes(mb_char,
					       Look(data, position, dir),
					       1, (int)tw->text.char_size);
	  cur_is_mb = (num_bytes < 2 ? False : True);
	  if (!cur_is_mb && 
	      isspace((unsigned char)*mb_char)) {
	    if (whiteSpace < 0) whiteSpace = position;
	  } else if ((sType == XmSELECT_WORD) &&
		     (start_is_mb ^ cur_is_mb)) {
	    if (whiteSpace < 0) whiteSpace = position;
	    break;
	  } else if (whiteSpace >= 0)
	    break;
	  position += ddir;
	}
      }
    }
    if (!include) {
      if(whiteSpace < 0 && dir == XmsdRight)
	whiteSpace = data->length;
      position = whiteSpace;
    }
    break;
  case XmSELECT_LINE: 
    for (i = 0; i < count; i++) {
      while (position >= 0 && position <= data->length) {
	/* DELTA: Look now returns a pointer */
	if ((int)tw->text.char_size == 1) {
	  c = Look(data, position, dir);
	  if ((c == '\0') || (*c == *data->PSWC_NWLN))
	    break;
	}
	else if ((int)tw->text.char_size == 2) {
	  bits16_ptr = (BITS16 *) Look(data, position, dir);
	  if ((bits16_ptr == NULL) ||
	      (*bits16_ptr == *(BITS16 *)data->PSWC_NWLN))
	    break;
	}
	else { /* MB_CUR_MAX == 3 or 4 or more */
	  wchar_t_ptr = (wchar_t *) Look(data, position, dir);
	  if ((wchar_t_ptr == NULL) ||
	      (*wchar_t_ptr == *(wchar_t *)data->PSWC_NWLN))
	    break;
	}
	
	if(((dir == XmsdRight) && (position == data->length)) || 
	   ((dir == XmsdLeft) && (position == 0)))
	  break;
	Increment(data, position, dir);
      }
      if (i + 1 != count)
	Increment(data, position, dir);
    }
    if (include) {
      /* later!!!check for last char in file # eol */
      Increment(data, position, dir);
    }
    break;
  case XmSELECT_PARAGRAPH: 
    /* Muliple paragraph scanning is not guarenteed to work. */
    for (i = 0; i < count; i++) {
      XmTextPosition start_position = position; 
      XmTextPosition last_char = position; 
      
      /* if scanning forward, check for between paragraphs condition */
      if (dir == XmsdRight) {
	/* DELTA: Look now returns a pointer */
	c = Look(data, position, dir);
	(void) _XmTextCharactersToBytes(mb_char, 
					Look(data, position, dir), 
					1, (int)tw->text.char_size);
	/* if is space, go back to first non-space */
	while (isspace((unsigned char)*mb_char)) {
	  if (position > 0)
	    position--;
	  else if (position == 0)
	    break;
	  (void) _XmTextCharactersToBytes(mb_char, 
					  Look(data, position, dir),
					  1, (int)tw->text.char_size);
	}
      }
      
      temp = position;
      ScanParagraph(data, &temp, dir, ddir, &last_char);
      position = temp;
      
      /*
       * If we are at the beginning of the paragraph and we are
       * scanning left, we need to rescan to find the character
       * at the beginning of the next paragraph.
       */  
      if (dir == XmsdLeft) {
	/* If we started at the beginning of the paragraph, rescan */
	if (last_char == start_position) {
	  temp = position;
	  ScanParagraph(data, &temp, dir, ddir, &last_char);
	}
	/*
	 * Set position to the last non-space 
	 * character that was scanned.
	 */
	position = last_char;
      }
      
      if (i + 1 != count)
	Increment(data, position, dir);
      
    }
    if (include) {
      Increment(data, position, dir);
    }
    break;
  case XmSELECT_ALL:
  default: 
    if (dir == XmsdLeft)
      position = 0;
    else
      position = data->length;
  }
  if (position < 0) position = 0;
  if (position > data->length) position = data->length;
  return(position);
}

static Boolean 
GetSelection(XmTextSource source,
	     XmTextPosition *left,
	     XmTextPosition *right)
{
  XmSourceData data = source->data;
  
  if (data->hasselection && data->left < data->right && data->left >= 0) {
    *left = data->left;
    *right = data->right;
    return True;
  } else {
    *left = *right = 0;
    data->hasselection = False;
    data->take_selection = True;
  }
  return False;
}

static void 
SetSelection(XmTextSource source,
	     XmTextPosition left,
	     XmTextPosition right, /* if right == -999, then  we're in 
				      LoseSelection, so don't call 
				      XtDisownSelection.*/
	     Time set_time)
{
  XmSourceData data = source->data;
  XmTextWidget tw;
  int i;
  int oldleft, oldright;
  
  if (!XtIsRealized((Widget)data->widgets[0]) ||
      (left > right && !data->hasselection)) return;
  
  if (left < 0) left = right = 0;
  
  for (i=0; i<data->numwidgets; i++) {
    tw = (XmTextWidget)(data->widgets[i]);
    (*tw->text.output->DrawInsertionPoint)(tw, tw->text.cursor_position,
					   off);
    _XmTextDisableRedisplay(data->widgets[i], FALSE);
    if (data->hasselection)
      _XmTextSetHighlight((Widget)data->widgets[i], data->left, 
			 data->right, XmHIGHLIGHT_NORMAL);
    
    data->widgets[i]->text.output->data->refresh_ibeam_off = True;
  }
  
  oldleft = data->left;
  oldright = data->right;
  data->left = left;
  data->right = right;
  if (data->numwidgets > 0) {
    Widget widget = (Widget) data->widgets[0];
    tw = (XmTextWidget)widget;
    if (!set_time) set_time = _XmValidTimestamp(widget);
    
    if (left <= right) {
      if (data->take_selection || (oldleft == oldright && left != right)) {
	if (!XmePrimarySource(widget, set_time)) {
	  (*source->SetSelection)(source, 1, 0, set_time);
	} else {
	  XmAnyCallbackStruct cb;
	  
	  data->prim_time = set_time;
	  data->hasselection = True;
	  data->take_selection = False;
	  
	  cb.reason = XmCR_GAIN_PRIMARY;
	  cb.event = NULL;
	  XtCallCallbackList ((Widget) data->widgets[0], 
			      data->widgets[0]->text.gain_primary_callback,
			      (XtPointer) &cb);
	}
      }
      if (data->hasselection && data->left < data->right) {
	for (i=0; i<data->numwidgets; i++) {
	  _XmTextSetHighlight((Widget) data->widgets[i], data->left,
			     data->right, XmHIGHLIGHT_SELECTED);
	}
      }
      if (left == right) tw->text.add_mode = False;
    } else {
      if (right != -999)
	XtDisownSelection(widget, XA_PRIMARY, set_time);
      data->hasselection = False;
      data->take_selection = True;
      tw->text.add_mode = False;
    }
  }
  for (i=0; i<data->numwidgets; i++) {
    tw = (XmTextWidget)(data->widgets[i]);
    _XmTextEnableRedisplay(data->widgets[i]);
    (*tw->text.output->DrawInsertionPoint)(tw, tw->text.cursor_position,
					   on);
  }
}

/* Public routines. */

void
_XmTextValueChanged(XmTextWidget initiator,
		    XEvent *event)
{
  XmAnyCallbackStruct cb;
  
  cb.reason = XmCR_VALUE_CHANGED;
  cb.event = event;
  if (initiator->text.value_changed_callback)
    XtCallCallbackList((Widget)initiator,
		       initiator->text.value_changed_callback, (XtPointer)&cb);
}

XmTextSource 
_XmStringSourceCreate(char *value,
#if NeedWidePrototypes
		      int is_wchar)
#else
                      Boolean is_wchar)
#endif /* NeedWidePrototypes */
{
  XmTextSource source;
  XmSourceData data;
  int num_chars;
  char newline = '\n';
  wchar_t *wc_value;
  char * tmp_value;
  int char_size, max_char_size;
  int ret_value = 0;

  source = (XmTextSource) XtMalloc((unsigned) sizeof(XmTextSourceRec));
  data = source->data = (XmSourceData)
    XtMalloc((unsigned) sizeof(XmSourceDataRec));
  source->AddWidget = AddWidget;
  source->CountLines = CountLines;
  source->RemoveWidget = RemoveWidget;
  source->ReadSource = ReadSource;
  source->Replace = (ReplaceProc) Replace;
  source->Scan = Scan;
  source->GetSelection = GetSelection;
  source->SetSelection = SetSelection;
  
  data->source = source;
  switch (MB_CUR_MAX) {
  case 1: case 2: 
    max_char_size = char_size = MB_CUR_MAX;
    break;
  case 0: 
    max_char_size = char_size = 1;
    break;
  default: 
    max_char_size = MB_CUR_MAX;
    char_size = sizeof(wchar_t);
  }
  
  if (is_wchar) {
    for (num_chars = 0, wc_value = (wchar_t*)value; 
	 wc_value[num_chars] != 0L;
	 num_chars++)
      /*EMPTY*/;
    
    data->maxlength = TEXT_INITIAL_INCREM;
    
    while ((num_chars + 1) >= data->maxlength) {
      if (data->maxlength < TEXT_INCREMENT)
	data->maxlength *= 2;
      else
	data->maxlength += TEXT_INCREMENT;
    }
    
    data->old_length = 0;
    data->ptr = XtMalloc((unsigned)((data->maxlength) * char_size));
    tmp_value = XtMalloc((unsigned)((num_chars + 1) * max_char_size));
    ret_value = wcstombs(tmp_value, wc_value, (num_chars + 1) * max_char_size);
    data->value = NULL;  /* Scratch area for block->ptr conversions */
    /* Doesnt include NULL */
    if (ret_value < 0)
      data->length = 0;
    else 
      data->length = _XmTextBytesToCharacters(data->ptr, tmp_value, num_chars,
					      False, max_char_size);
    XtFree(tmp_value);
  } else {
    if (value)
      num_chars = _XmTextCountCharacters(value, strlen(value));
    else
      num_chars = 0;
    data->maxlength = TEXT_INITIAL_INCREM;
    
    while ((num_chars + 1) >= data->maxlength) {
      if (data->maxlength < TEXT_INCREMENT)
	data->maxlength *= 2;
      else
	data->maxlength += TEXT_INCREMENT;
    }
    
    data->old_length = 0;
    data->ptr = XtMalloc((unsigned)((data->maxlength) * char_size));
    
    data->value = NULL;  /* Scratch area for block->ptr conversions */
    data->length = _XmTextBytesToCharacters(data->ptr, value, num_chars,
					    False, max_char_size);
  }
  
  data->PSWC_NWLN = (char *) XtMalloc(char_size);
  _XmTextBytesToCharacters(data->PSWC_NWLN, &newline, 1, False, 
			   max_char_size);
  
  data->numwidgets = 0;
  data->widgets = (XmTextWidget *) XtMalloc((unsigned) sizeof(XmTextWidget));
  data->hasselection = False;
  data->take_selection = True;
  data->left = data->right = 0;
  data->editable = True;
  data->maxallowed = INT_MAX;
  data->gap_start = data->ptr + (data->length * char_size);
  data->gap_end = data->ptr + ((data->maxlength - 1) * char_size);
  data->prim_time = 0;
  return source;
}

void 
_XmStringSourceDestroy(XmTextSource source)
{
  XtFree((char *) source->data->ptr);
  XtFree((char *) source->data->value);
  XtFree((char *) source->data->widgets);
  XtFree((char *) source->data->PSWC_NWLN);
  XtFree((char *) source->data);
  XtFree((char *) source);
  source = NULL;
}

char *
_XmStringSourceGetValue(XmTextSource source,
#if NeedWidePrototypes
			int want_wchar)
#else
                        Boolean want_wchar)
#endif /* NeedWidePrototypes */
{
  XmSourceData data = source->data;
  XmTextWidget tw = (XmTextWidget) data->widgets[0];
  XmTextBlockRec block;
  int length = 0;
  XmTextPosition pos = 0;
  XmTextPosition ret_pos = 0;
  XmTextPosition last_pos = 0;
  char * temp;
  wchar_t * wc_temp;
  int return_val;

  if (!want_wchar) {
    if (data->length > 0)
      temp = (char *) XtMalloc((unsigned)
			       (data->length + 1) * (int)tw->text.char_size);
    else
      return(XtNewString(""));
    
    last_pos = (XmTextPosition) data->length;
    
    while (pos < last_pos) {
      ret_pos = ReadSource(source, pos, last_pos, &block);
      if (block.length == 0)
	break;
      
      (void)memcpy((void*)&temp[length], (void*)block.ptr, block.length);
      length += block.length;
      pos = ret_pos;
    }
    temp[length] = '\0';
    return (temp);
  } else {
    
    if (data->length > 0)
      wc_temp = (wchar_t*)XtMalloc((unsigned)
				   (data->length+1) * sizeof(wchar_t));
    else {
      wc_temp = (wchar_t*)XtMalloc((unsigned) sizeof(wchar_t));
      wc_temp[0] = (wchar_t)0L;
      return (char*) wc_temp;
    }
    
    last_pos = (XmTextPosition) data->length;
    
    while (pos < last_pos) {
      ret_pos = ReadSource(source, pos, last_pos, &block);
      if (block.length == 0)
	break;
      
      /* NOTE: ret_pos - pos could result in a truncated long. */
      return_val = mbstowcs(&wc_temp[length], block.ptr, (int)(ret_pos - pos));
      if (return_val > 0) length += return_val;
      pos = ret_pos;
    }
    wc_temp[length] = (wchar_t)0L;
    return ((char*)wc_temp);
  }
}

void 
_XmStringSourceSetValue(XmTextWidget tw,
			char *value)
{
  XmTextSource source = tw->text.source;
  XmSourceData data = source->data;
  Boolean editable, freeBlock;
  int maxallowed;
  XmTextBlockRec block, newblock;
  XmTextPosition fromPos = 0; 
  XmTextPosition toPos = data->length;
  
  (*source->SetSelection)(source, 1, 0,
			  XtLastTimestampProcessed(XtDisplay(tw)));
  block.format = XmFMT_8_BIT;
  block.length = strlen(value);
  block.ptr = value;
  editable = data->editable;
  maxallowed = data->maxallowed;
  data->editable = TRUE;
  data->maxallowed = INT_MAX;
  _XmTextSetHighlight((Widget)tw, 0, tw->text.last_position, 
		     XmHIGHLIGHT_NORMAL);
  
  if (_XmTextModifyVerify(tw, NULL, &fromPos, &toPos,
			  NULL, &block, &newblock, &freeBlock)) {
    (void)(source->Replace)(tw, NULL, &fromPos, &toPos, &newblock, False);
    if (freeBlock && newblock.ptr) XtFree(newblock.ptr);
    _XmTextValueChanged(tw, NULL);
  }
  
  data->editable = editable;
  data->maxallowed = maxallowed;
}


Boolean 
_XmStringSourceHasSelection(XmTextSource source)
{
  return source->data->hasselection;
}

Boolean 
_XmStringSourceGetEditable(XmTextSource source)
{
  return source->data->editable;
}

void 
_XmStringSourceSetEditable(XmTextSource source,
#if NeedWidePrototypes
			   int editable)
#else
                           Boolean editable)
#endif /* NeedWidePrototypes */
{
  source->data->editable = editable;
}

int 
_XmStringSourceGetMaxLength(XmTextSource source)
{
  return source->data->maxallowed;
}

void 
_XmStringSourceSetMaxLength(XmTextSource source,
			    int max)
{
  source->data->maxallowed = max;
}