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
|
/*
* tkSelect.c --
*
* This file manages the selection for the Tk toolkit, translating
* between the standard X ICCCM conventions and Tcl commands.
*
* Copyright (c) 1990-1993 The Regents of the University of California.
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkSelect.h"
/*
* When a selection handler is set up by invoking "selection handle", one of
* the following data structures is set up to hold information about the
* command to invoke and its interpreter.
*/
typedef struct {
Tcl_Interp *interp; /* Interpreter in which to invoke command. */
int cmdLength; /* # of non-NULL bytes in command. */
int charOffset; /* The offset of the next char to retrieve. */
int byteOffset; /* The expected byte offset of the next
* chunk. */
char buffer[TCL_UTF_MAX]; /* A buffer to hold part of a UTF character
* that is split across chunks. */
char command[1]; /* Command to invoke. Actual space is
* allocated as large as necessary. This must
* be the last entry in the structure. */
} CommandInfo;
/*
* When selection ownership is claimed with the "selection own" Tcl command,
* one of the following structures is created to record the Tcl command to be
* executed when the selection is lost again.
*/
typedef struct LostCommand {
Tcl_Interp *interp; /* Interpreter in which to invoke command. */
Tcl_Obj *cmdObj; /* Reference to command to invoke. */
} LostCommand;
/*
* The structure below is used to keep each thread's pending list separate.
*/
typedef struct ThreadSpecificData {
TkSelInProgress *pendingPtr;
/* Topmost search in progress, or NULL if
* none. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* Forward declarations for functions defined in this file:
*/
static int HandleTclCommand(ClientData clientData,
int offset, char *buffer, int maxBytes);
static void LostSelection(ClientData clientData);
static int SelGetProc(ClientData clientData,
Tcl_Interp *interp, const char *portion);
/*
*--------------------------------------------------------------
*
* Tk_CreateSelHandler --
*
* This function is called to register a function as the handler for
* selection requests of a particular target type on a particular window
* for a particular selection.
*
* Results:
* None.
*
* Side effects:
* In the future, whenever the selection is in tkwin's window and someone
* requests the selection in the form given by target, proc will be
* invoked to provide part or all of the selection in the given form. If
* there was already a handler declared for the given window, target and
* selection type, then it is replaced. Proc should have the following
* form:
*
* int
* proc(
* ClientData clientData,
* int offset,
* char *buffer,
* int maxBytes)
* {
* }
*
* The clientData argument to proc will be the same as the clientData
* argument to this function. The offset argument indicates which portion
* of the selection to return: skip the first offset bytes. Buffer is a
* pointer to an area in which to place the converted selection, and
* maxBytes gives the number of bytes available at buffer. Proc should
* place the selection in buffer as a string, and return a count of the
* number of bytes of selection actually placed in buffer (not including
* the terminating NULL character). If the return value equals maxBytes,
* this is a sign that there is probably still more selection information
* available.
*
*--------------------------------------------------------------
*/
void
Tk_CreateSelHandler(
Tk_Window tkwin, /* Token for window. */
Atom selection, /* Selection to be handled. */
Atom target, /* The kind of selection conversions that can
* be handled by proc, e.g. TARGETS or
* STRING. */
Tk_SelectionProc *proc, /* Function to invoke to convert selection to
* type "target". */
ClientData clientData, /* Value to pass to proc. */
Atom format) /* Format in which the selection information
* should be returned to the requestor.
* XA_STRING is best by far, but anything
* listed in the ICCCM will be tolerated
* (blech). */
{
register TkSelHandler *selPtr;
TkWindow *winPtr = (TkWindow *) tkwin;
Tk_SelectionProc *oldProc;
if (winPtr->dispPtr->multipleAtom == None) {
TkSelInit(tkwin);
}
/*
* See if there's already a handler for this target and selection on this
* window. If so, re-use it. If not, create a new one.
*/
for (selPtr = winPtr->selHandlerList; ; selPtr = selPtr->nextPtr) {
if (selPtr == NULL) {
selPtr = ckalloc(sizeof(TkSelHandler));
selPtr->nextPtr = winPtr->selHandlerList;
winPtr->selHandlerList = selPtr;
/*
* When creating a new handler, we set the old proc to zero which
* compares unequal to any real Tk selection handler entry.
*/
oldProc = NULL;
break;
}
if ((selPtr->selection == selection) && (selPtr->target == target)) {
/*
* When replacing an existing handler, we remember the previous
* proc for later comparison with the UTF8_STRING handler.
*/
oldProc = selPtr->proc;
/*
* Special case: when replacing handler created by "selection
* handle", free up memory. Should there be a callback to allow
* other clients to do this too?
*/
if (selPtr->proc == HandleTclCommand) {
ckfree(selPtr->clientData);
}
break;
}
}
selPtr->selection = selection;
selPtr->target = target;
selPtr->format = format;
selPtr->proc = proc;
selPtr->clientData = clientData;
if (format == XA_STRING) {
selPtr->size = 8;
} else {
selPtr->size = 32;
}
if ((target == XA_STRING) && (winPtr->dispPtr->utf8Atom != (Atom) 0)) {
/*
* If the user asked for a STRING handler and we understand
* UTF8_STRING, we implicitly create a UTF8_STRING handler for them.
* First we check if a UTF8_STRING handler already exists for the same
* widget and selection. If one doesn't exist, one must be created.
* If one does exist and it has the same proc as the STRING handler
* that we just replaced, it must also be replaced. If one does exist
* but it has a different proc, or if we didn't just replace an
* existing STRING handler, we must leave it alone, since we didn't
* automatically create it in the first place.
*/
TkSelHandler *utf8selPtr;
target = winPtr->dispPtr->utf8Atom;
for (utf8selPtr = winPtr->selHandlerList; utf8selPtr != NULL;
utf8selPtr = utf8selPtr->nextPtr) {
if ((utf8selPtr->selection == selection)
&& (utf8selPtr->target == target)) {
break;
}
}
if (utf8selPtr == NULL || ((utf8selPtr->format == target)
&& (utf8selPtr->proc == oldProc))) {
ClientData newClientData;
if (proc == HandleTclCommand) {
/*
* The clientData is selection controlled memory, so we should
* make a copy for this new selection handler.
*/
unsigned cmdInfoLen = sizeof(CommandInfo) +
((CommandInfo*)clientData)->cmdLength - 3;
newClientData = (ClientData) ckalloc(cmdInfoLen);
memcpy(newClientData, clientData, cmdInfoLen);
} else {
newClientData = clientData;
}
/*
* This recursive call is OK, because we've changed the value
* of 'target'.
*/
Tk_CreateSelHandler(tkwin, selection, target, proc, newClientData,
target);
}
}
}
/*
*----------------------------------------------------------------------
*
* Tk_DeleteSelHandler --
*
* Remove the selection handler for a given window, target, and
* selection, if it exists.
*
* Results:
* None.
*
* Side effects:
* The selection handler for tkwin and target is removed. If there is no
* such handler then nothing happens.
*
*----------------------------------------------------------------------
*/
void
Tk_DeleteSelHandler(
Tk_Window tkwin, /* Token for window. */
Atom selection, /* The selection whose handler is to be
* removed. */
Atom target) /* The target whose selection handler is to be
* removed. */
{
TkWindow *winPtr = (TkWindow *) tkwin;
register TkSelHandler *selPtr, *prevPtr;
register TkSelInProgress *ipPtr;
ThreadSpecificData *tsdPtr =
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* Find the selection handler to be deleted, or return if it doesn't
* exist.
*/
for (selPtr = winPtr->selHandlerList, prevPtr = NULL; ;
prevPtr = selPtr, selPtr = selPtr->nextPtr) {
if (selPtr == NULL) {
return;
}
if ((selPtr->selection == selection) && (selPtr->target == target)) {
break;
}
}
/*
* If ConvertSelection is processing this handler, tell it that the
* handler is dead.
*/
for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL;
ipPtr = ipPtr->nextPtr) {
if (ipPtr->selPtr == selPtr) {
ipPtr->selPtr = NULL;
}
}
/*
* Free resources associated with the handler.
*/
if (prevPtr == NULL) {
winPtr->selHandlerList = selPtr->nextPtr;
} else {
prevPtr->nextPtr = selPtr->nextPtr;
}
if ((target == XA_STRING) && (winPtr->dispPtr->utf8Atom != (Atom) 0)) {
/*
* If the user asked for a STRING handler and we understand
* UTF8_STRING, we may have implicitly created a UTF8_STRING handler
* for them. Look for it and delete it as necessary.
*/
TkSelHandler *utf8selPtr;
target = winPtr->dispPtr->utf8Atom;
for (utf8selPtr = winPtr->selHandlerList; utf8selPtr != NULL;
utf8selPtr = utf8selPtr->nextPtr) {
if ((utf8selPtr->selection == selection)
&& (utf8selPtr->target == target)) {
break;
}
}
if (utf8selPtr != NULL) {
if ((utf8selPtr->format == target)
&& (utf8selPtr->proc == selPtr->proc)
&& (utf8selPtr->size == selPtr->size)) {
/*
* This recursive call is OK, because we've changed the value
* of 'target'.
*/
Tk_DeleteSelHandler(tkwin, selection, target);
}
}
}
if (selPtr->proc == HandleTclCommand) {
/*
* Mark the CommandInfo as deleted and free it if we can.
*/
((CommandInfo *) selPtr->clientData)->interp = NULL;
Tcl_EventuallyFree(selPtr->clientData, TCL_DYNAMIC);
}
ckfree(selPtr);
}
/*
*--------------------------------------------------------------
*
* Tk_OwnSelection --
*
* Arrange for tkwin to become the owner of a selection.
*
* Results:
* None.
*
* Side effects:
* From now on, requests for the selection will be directed to functions
* associated with tkwin (they must have been declared with calls to
* Tk_CreateSelHandler). When the selection is lost by this window, proc
* will be invoked (see the manual entry for details). This function may
* invoke callbacks, including Tcl scripts, so any calling function
* should be reentrant at the point where Tk_OwnSelection is invoked.
*
*--------------------------------------------------------------
*/
void
Tk_OwnSelection(
Tk_Window tkwin, /* Window to become new selection owner. */
Atom selection, /* Selection that window should own. */
Tk_LostSelProc *proc, /* Function to call when selection is taken
* away from tkwin. */
ClientData clientData) /* Arbitrary one-word argument to pass to
* proc. */
{
register TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkSelectionInfo *infoPtr;
Tk_LostSelProc *clearProc = NULL;
ClientData clearData = NULL;/* Initialization needed only to prevent
* compiler warning. */
if (dispPtr->multipleAtom == None) {
TkSelInit(tkwin);
}
Tk_MakeWindowExist(tkwin);
/*
* This code is somewhat tricky. First, we find the specified selection on
* the selection list. If the previous owner is in this process, and is a
* different window, then we need to invoke the clearProc. However, it's
* dangerous to call the clearProc right now, because it could invoke a
* Tcl script that wrecks the current state (e.g. it could delete the
* window). To be safe, defer the call until the end of the function when
* we no longer care about the state.
*/
for (infoPtr = dispPtr->selectionInfoPtr; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->selection == selection) {
break;
}
}
if (infoPtr == NULL) {
infoPtr = ckalloc(sizeof(TkSelectionInfo));
infoPtr->selection = selection;
infoPtr->nextPtr = dispPtr->selectionInfoPtr;
dispPtr->selectionInfoPtr = infoPtr;
} else if (infoPtr->clearProc != NULL) {
if (infoPtr->owner != tkwin) {
clearProc = infoPtr->clearProc;
clearData = infoPtr->clearData;
} else if (infoPtr->clearProc == LostSelection) {
/*
* If the selection handler is one created by "selection own", be
* sure to free the record for it; otherwise there will be a
* memory leak.
*/
ckfree(infoPtr->clearData);
}
}
infoPtr->owner = tkwin;
infoPtr->serial = NextRequest(winPtr->display);
infoPtr->clearProc = proc;
infoPtr->clearData = clientData;
/*
* Note that we are using CurrentTime, even though ICCCM recommends
* against this practice (the problem is that we don't necessarily have a
* valid time to use). We will not be able to retrieve a useful timestamp
* for the TIMESTAMP target later.
*/
infoPtr->time = CurrentTime;
/*
* Note that we are not checking to see if the selection claim succeeded.
* If the ownership does not change, then the clearProc may never be
* invoked, and we will return incorrect information when queried for the
* current selection owner.
*/
XSetSelectionOwner(winPtr->display, infoPtr->selection, winPtr->window,
infoPtr->time);
/*
* Now that we are done, we can invoke clearProc without running into
* reentrancy problems.
*/
if (clearProc != NULL) {
clearProc(clearData);
}
}
/*
*----------------------------------------------------------------------
*
* Tk_ClearSelection --
*
* Eliminate the specified selection on tkwin's display, if there is one.
*
* Results:
* None.
*
* Side effects:
* The specified selection is cleared, so that future requests to
* retrieve it will fail until some application owns it again. This
* function invokes callbacks, possibly including Tcl scripts, so any
* calling function should be reentrant at the point Tk_ClearSelection is
* invoked.
*
*----------------------------------------------------------------------
*/
void
Tk_ClearSelection(
Tk_Window tkwin, /* Window that selects a display. */
Atom selection) /* Selection to be cancelled. */
{
register TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkSelectionInfo *infoPtr;
TkSelectionInfo *prevPtr;
TkSelectionInfo *nextPtr;
Tk_LostSelProc *clearProc = NULL;
ClientData clearData = NULL;/* Initialization needed only to prevent
* compiler warning. */
if (dispPtr->multipleAtom == None) {
TkSelInit(tkwin);
}
for (infoPtr = dispPtr->selectionInfoPtr, prevPtr = NULL;
infoPtr != NULL; infoPtr = nextPtr) {
nextPtr = infoPtr->nextPtr;
if (infoPtr->selection == selection) {
if (prevPtr == NULL) {
dispPtr->selectionInfoPtr = nextPtr;
} else {
prevPtr->nextPtr = nextPtr;
}
break;
}
prevPtr = infoPtr;
}
if (infoPtr != NULL) {
clearProc = infoPtr->clearProc;
clearData = infoPtr->clearData;
ckfree(infoPtr);
}
XSetSelectionOwner(winPtr->display, selection, None, CurrentTime);
if (clearProc != NULL) {
clearProc(clearData);
}
}
/*
*--------------------------------------------------------------
*
* Tk_GetSelection --
*
* Retrieve the value of a selection and pass it off (in pieces,
* possibly) to a given function.
*
* Results:
* The return value is a standard Tcl return value. If an error occurs
* (such as no selection exists) then an error message is left in the
* interp's result.
*
* Side effects:
* The standard X11 protocols are used to retrieve the selection. When it
* arrives, it is passed to proc. If the selection is very large, it will
* be passed to proc in several pieces. Proc should have the following
* structure:
*
* int
* proc(
* ClientData clientData,
* Tcl_Interp *interp,
* char *portion)
* {
* }
*
* The interp and clientData arguments to proc will be the same as the
* corresponding arguments to Tk_GetSelection. The portion argument
* points to a character string containing part of the selection, and
* numBytes indicates the length of the portion, not including the
* terminating NULL character. If the selection arrives in several
* pieces, the "portion" arguments in separate calls will contain
* successive parts of the selection. Proc should normally return TCL_OK.
* If it detects an error then it should return TCL_ERROR and leave an
* error message in the interp's result; the remainder of the selection
* retrieval will be aborted.
*
*--------------------------------------------------------------
*/
int
Tk_GetSelection(
Tcl_Interp *interp, /* Interpreter to use for reporting errors. */
Tk_Window tkwin, /* Window on whose behalf to retrieve the
* selection (determines display from which to
* retrieve). */
Atom selection, /* Selection to retrieve. */
Atom target, /* Desired form in which selection is to be
* returned. */
Tk_GetSelProc *proc, /* Function to call to process the selection,
* once it has been retrieved. */
ClientData clientData) /* Arbitrary value to pass to proc. */
{
TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkSelectionInfo *infoPtr;
ThreadSpecificData *tsdPtr =
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
if (dispPtr->multipleAtom == None) {
TkSelInit(tkwin);
}
/*
* If the selection is owned by a window managed by this process, then
* call the retrieval function directly, rather than going through the X
* server (it's dangerous to go through the X server in this case because
* it could result in deadlock if an INCR-style selection results).
*/
for (infoPtr = dispPtr->selectionInfoPtr; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->selection == selection) {
break;
}
}
if (infoPtr != NULL) {
register TkSelHandler *selPtr;
int offset, result, count;
char buffer[TK_SEL_BYTES_AT_ONCE+1];
TkSelInProgress ip;
for (selPtr = ((TkWindow *) infoPtr->owner)->selHandlerList;
selPtr != NULL; selPtr = selPtr->nextPtr) {
if (selPtr->target==target && selPtr->selection==selection) {
break;
}
}
if (selPtr == NULL) {
Atom type;
count = TkSelDefaultSelection(infoPtr, target, buffer,
TK_SEL_BYTES_AT_ONCE, &type);
if (count > TK_SEL_BYTES_AT_ONCE) {
Tcl_Panic("selection handler returned too many bytes");
}
if (count < 0) {
goto cantget;
}
buffer[count] = 0;
result = proc(clientData, interp, buffer);
} else {
offset = 0;
result = TCL_OK;
ip.selPtr = selPtr;
ip.nextPtr = tsdPtr->pendingPtr;
tsdPtr->pendingPtr = &ip;
while (1) {
count = selPtr->proc(selPtr->clientData, offset, buffer,
TK_SEL_BYTES_AT_ONCE);
if ((count < 0) || (ip.selPtr == NULL)) {
tsdPtr->pendingPtr = ip.nextPtr;
goto cantget;
}
if (count > TK_SEL_BYTES_AT_ONCE) {
Tcl_Panic("selection handler returned too many bytes");
}
buffer[count] = '\0';
result = proc(clientData, interp, buffer);
if ((result != TCL_OK) || (count < TK_SEL_BYTES_AT_ONCE)
|| (ip.selPtr == NULL)) {
break;
}
offset += count;
}
tsdPtr->pendingPtr = ip.nextPtr;
}
return result;
}
/*
* The selection is owned by some other process.
*/
return TkSelGetSelection(interp, tkwin, selection, target, proc,
clientData);
cantget:
Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection),
" selection doesn't exist or form \"",
Tk_GetAtomName(tkwin, target), "\" not defined", NULL);
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* Tk_SelectionObjCmd --
*
* This function is invoked to process the "selection" Tcl command. See
* the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Tk_SelectionObjCmd(
ClientData clientData, /* Main window associated with
* interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = clientData;
const char *path = NULL;
Atom selection;
const char *selName = NULL;
const char *string;
int count, index;
Tcl_Obj **objs;
static const char *const optionStrings[] = {
"clear", "get", "handle", "own", NULL
};
enum options {
SELECTION_CLEAR, SELECTION_GET, SELECTION_HANDLE, SELECTION_OWN
};
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
&index) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum options) index) {
case SELECTION_CLEAR: {
static const char *const clearOptionStrings[] = {
"-displayof", "-selection", NULL
};
enum clearOptions { CLEAR_DISPLAYOF, CLEAR_SELECTION };
int clearIndex;
for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
count-=2, objs+=2) {
string = Tcl_GetString(objs[0]);
if (string[0] != '-') {
break;
}
if (count < 2) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objs[0], clearOptionStrings,
"option", 0, &clearIndex) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum clearOptions) clearIndex) {
case CLEAR_DISPLAYOF:
path = Tcl_GetString(objs[1]);
break;
case CLEAR_SELECTION:
selName = Tcl_GetString(objs[1]);
break;
}
}
if (count == 1) {
path = Tcl_GetString(objs[0]);
} else if (count > 1) {
Tcl_WrongNumArgs(interp, 2, objv, "?-option value ...?");
return TCL_ERROR;
}
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
if (selName != NULL) {
selection = Tk_InternAtom(tkwin, selName);
} else {
selection = XA_PRIMARY;
}
Tk_ClearSelection(tkwin, selection);
break;
}
case SELECTION_GET: {
Atom target;
const char *targetName = NULL;
Tcl_DString selBytes;
int result;
static const char *const getOptionStrings[] = {
"-displayof", "-selection", "-type", NULL
};
enum getOptions { GET_DISPLAYOF, GET_SELECTION, GET_TYPE };
int getIndex;
for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count>0;
count-=2, objs+=2) {
string = Tcl_GetString(objs[0]);
if (string[0] != '-') {
break;
}
if (count < 2) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objs[0], getOptionStrings,
"option", 0, &getIndex) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum getOptions) getIndex) {
case GET_DISPLAYOF:
path = Tcl_GetString(objs[1]);
break;
case GET_SELECTION:
selName = Tcl_GetString(objs[1]);
break;
case GET_TYPE:
targetName = Tcl_GetString(objs[1]);
break;
}
}
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
if (selName != NULL) {
selection = Tk_InternAtom(tkwin, selName);
} else {
selection = XA_PRIMARY;
}
if (count > 1) {
Tcl_WrongNumArgs(interp, 2, objv, "?-option value ...?");
return TCL_ERROR;
} else if (count == 1) {
target = Tk_InternAtom(tkwin, Tcl_GetString(objs[0]));
} else if (targetName != NULL) {
target = Tk_InternAtom(tkwin, targetName);
} else {
target = XA_STRING;
}
Tcl_DStringInit(&selBytes);
result = Tk_GetSelection(interp, tkwin, selection, target,
SelGetProc, &selBytes);
if (result == TCL_OK) {
Tcl_DStringResult(interp, &selBytes);
} else {
Tcl_DStringFree(&selBytes);
}
return result;
}
case SELECTION_HANDLE: {
Atom target, format;
const char *targetName = NULL;
const char *formatName = NULL;
register CommandInfo *cmdInfoPtr;
int cmdLength;
static const char *const handleOptionStrings[] = {
"-format", "-selection", "-type", NULL
};
enum handleOptions {
HANDLE_FORMAT, HANDLE_SELECTION, HANDLE_TYPE
};
int handleIndex;
for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
count-=2, objs+=2) {
string = Tcl_GetString(objs[0]);
if (string[0] != '-') {
break;
}
if (count < 2) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objs[0],handleOptionStrings,
"option", 0, &handleIndex) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum handleOptions) handleIndex) {
case HANDLE_FORMAT:
formatName = Tcl_GetString(objs[1]);
break;
case HANDLE_SELECTION:
selName = Tcl_GetString(objs[1]);
break;
case HANDLE_TYPE:
targetName = Tcl_GetString(objs[1]);
break;
}
}
if ((count < 2) || (count > 4)) {
Tcl_WrongNumArgs(interp, 2, objv, "?-option value ...? window command");
return TCL_ERROR;
}
tkwin = Tk_NameToWindow(interp, Tcl_GetString(objs[0]), tkwin);
if (tkwin == NULL) {
return TCL_ERROR;
}
if (selName != NULL) {
selection = Tk_InternAtom(tkwin, selName);
} else {
selection = XA_PRIMARY;
}
if (count > 2) {
target = Tk_InternAtom(tkwin, Tcl_GetString(objs[2]));
} else if (targetName != NULL) {
target = Tk_InternAtom(tkwin, targetName);
} else {
target = XA_STRING;
}
if (count > 3) {
format = Tk_InternAtom(tkwin, Tcl_GetString(objs[3]));
} else if (formatName != NULL) {
format = Tk_InternAtom(tkwin, formatName);
} else {
format = XA_STRING;
}
string = Tcl_GetStringFromObj(objs[1], &cmdLength);
if (cmdLength == 0) {
Tk_DeleteSelHandler(tkwin, selection, target);
} else {
cmdInfoPtr = ckalloc(Tk_Offset(CommandInfo, command)
+ 1 + cmdLength);
cmdInfoPtr->interp = interp;
cmdInfoPtr->charOffset = 0;
cmdInfoPtr->byteOffset = 0;
cmdInfoPtr->buffer[0] = '\0';
cmdInfoPtr->cmdLength = cmdLength;
memcpy(cmdInfoPtr->command, string, cmdLength + 1);
Tk_CreateSelHandler(tkwin, selection, target, HandleTclCommand,
cmdInfoPtr, format);
}
return TCL_OK;
}
case SELECTION_OWN: {
register LostCommand *lostPtr;
Tcl_Obj *commandObj = NULL;
static const char *const ownOptionStrings[] = {
"-command", "-displayof", "-selection", NULL
};
enum ownOptions { OWN_COMMAND, OWN_DISPLAYOF, OWN_SELECTION };
int ownIndex;
for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
count-=2, objs+=2) {
string = Tcl_GetString(objs[0]);
if (string[0] != '-') {
break;
}
if (count < 2) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objs[0], ownOptionStrings,
"option", 0, &ownIndex) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum ownOptions) ownIndex) {
case OWN_COMMAND:
commandObj = objs[1];
break;
case OWN_DISPLAYOF:
path = Tcl_GetString(objs[1]);
break;
case OWN_SELECTION:
selName = Tcl_GetString(objs[1]);
break;
}
}
if (count > 2) {
Tcl_WrongNumArgs(interp, 2, objv, "?-option value ...? ?window?");
return TCL_ERROR;
}
if (selName != NULL) {
selection = Tk_InternAtom(tkwin, selName);
} else {
selection = XA_PRIMARY;
}
if (count == 0) {
TkSelectionInfo *infoPtr;
TkWindow *winPtr;
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
winPtr = (TkWindow *)tkwin;
for (infoPtr = winPtr->dispPtr->selectionInfoPtr;
infoPtr != NULL; infoPtr = infoPtr->nextPtr) {
if (infoPtr->selection == selection) {
break;
}
}
/*
* Ignore the internal clipboard window.
*/
if ((infoPtr != NULL)
&& (infoPtr->owner != winPtr->dispPtr->clipWindow)) {
Tcl_SetResult(interp, Tk_PathName(infoPtr->owner), TCL_STATIC);
}
return TCL_OK;
}
tkwin = Tk_NameToWindow(interp, Tcl_GetString(objs[0]), tkwin);
if (tkwin == NULL) {
return TCL_ERROR;
}
if (count == 2) {
commandObj = objs[1];
}
if (commandObj == NULL) {
Tk_OwnSelection(tkwin, selection, NULL, NULL);
return TCL_OK;
}
lostPtr = ckalloc(sizeof(LostCommand));
lostPtr->interp = interp;
lostPtr->cmdObj = commandObj;
Tcl_IncrRefCount(commandObj);
Tk_OwnSelection(tkwin, selection, LostSelection, lostPtr);
return TCL_OK;
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkSelGetInProgress --
*
* This function returns a pointer to the thread-local list of pending
* searches.
*
* Results:
* The return value is a pointer to the first search in progress, or NULL
* if there are none.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
TkSelInProgress *
TkSelGetInProgress(void)
{
ThreadSpecificData *tsdPtr =
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
return tsdPtr->pendingPtr;
}
/*
*----------------------------------------------------------------------
*
* TkSelSetInProgress --
*
* This function is used to set the thread-local list of pending
* searches. It is required because the pending list is kept in thread
* local storage.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TkSelSetInProgress(
TkSelInProgress *pendingPtr)
{
ThreadSpecificData *tsdPtr =
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
tsdPtr->pendingPtr = pendingPtr;
}
/*
*----------------------------------------------------------------------
*
* TkSelDeadWindow --
*
* This function is invoked just before a TkWindow is deleted. It
* performs selection-related cleanup.
*
* Results:
* None.
*
* Side effects:
* Frees up memory associated with the selection.
*
*----------------------------------------------------------------------
*/
void
TkSelDeadWindow(
register TkWindow *winPtr) /* Window that's being deleted. */
{
register TkSelHandler *selPtr;
register TkSelInProgress *ipPtr;
TkSelectionInfo *infoPtr, *prevPtr, *nextPtr;
ThreadSpecificData *tsdPtr =
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* While deleting all the handlers, be careful to check whether
* ConvertSelection or TkSelPropProc are about to process one of the
* deleted handlers.
*/
while (winPtr->selHandlerList != NULL) {
selPtr = winPtr->selHandlerList;
winPtr->selHandlerList = selPtr->nextPtr;
for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL;
ipPtr = ipPtr->nextPtr) {
if (ipPtr->selPtr == selPtr) {
ipPtr->selPtr = NULL;
}
}
if (selPtr->proc == HandleTclCommand) {
/*
* Mark the CommandInfo as deleted and free it when we can.
*/
((CommandInfo *) selPtr->clientData)->interp = NULL;
Tcl_EventuallyFree(selPtr->clientData, TCL_DYNAMIC);
}
ckfree(selPtr);
}
/*
* Remove selections owned by window being deleted.
*/
for (infoPtr = winPtr->dispPtr->selectionInfoPtr, prevPtr = NULL;
infoPtr != NULL; infoPtr = nextPtr) {
nextPtr = infoPtr->nextPtr;
if (infoPtr->owner == (Tk_Window) winPtr) {
if (infoPtr->clearProc == LostSelection) {
ckfree(infoPtr->clearData);
}
ckfree(infoPtr);
infoPtr = prevPtr;
if (prevPtr == NULL) {
winPtr->dispPtr->selectionInfoPtr = nextPtr;
} else {
prevPtr->nextPtr = nextPtr;
}
}
prevPtr = infoPtr;
}
}
/*
*----------------------------------------------------------------------
*
* TkSelInit --
*
* Initialize selection-related information for a display.
*
* Results:
* None.
*
* Side effects:
* Selection-related information is initialized.
*
*----------------------------------------------------------------------
*/
void
TkSelInit(
Tk_Window tkwin) /* Window token (used to find display to
* initialize). */
{
register TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
/*
* Fetch commonly-used atoms.
*/
dispPtr->multipleAtom = Tk_InternAtom(tkwin, "MULTIPLE");
dispPtr->incrAtom = Tk_InternAtom(tkwin, "INCR");
dispPtr->targetsAtom = Tk_InternAtom(tkwin, "TARGETS");
dispPtr->timestampAtom = Tk_InternAtom(tkwin, "TIMESTAMP");
dispPtr->textAtom = Tk_InternAtom(tkwin, "TEXT");
dispPtr->compoundTextAtom = Tk_InternAtom(tkwin, "COMPOUND_TEXT");
dispPtr->applicationAtom = Tk_InternAtom(tkwin, "TK_APPLICATION");
dispPtr->windowAtom = Tk_InternAtom(tkwin, "TK_WINDOW");
dispPtr->clipboardAtom = Tk_InternAtom(tkwin, "CLIPBOARD");
/*
* Using UTF8_STRING instead of the XA_UTF8_STRING macro allows us to
* support older X servers that didn't have UTF8_STRING yet. This is
* necessary on Unix systems. For more information, see:
* http://www.cl.cam.ac.uk/~mgk25/unicode.html#x11
*/
#if !defined(__WIN32__)
dispPtr->utf8Atom = Tk_InternAtom(tkwin, "UTF8_STRING");
#else
dispPtr->utf8Atom = (Atom) 0;
#endif
}
/*
*----------------------------------------------------------------------
*
* TkSelClearSelection --
*
* This function is invoked to process a SelectionClear event.
*
* Results:
* None.
*
* Side effects:
* Invokes the clear function for the window which lost the
* selection.
*
*----------------------------------------------------------------------
*/
void
TkSelClearSelection(
Tk_Window tkwin, /* Window for which event was targeted. */
register XEvent *eventPtr) /* X SelectionClear event. */
{
register TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkSelectionInfo *infoPtr;
TkSelectionInfo *prevPtr;
/*
* Invoke clear function for window that just lost the selection. This
* code is a bit tricky, because any callbacks due to selection changes
* between windows managed by the process have already been made. Thus,
* ignore the event unless it refers to the window that's currently the
* selection owner and the event was generated after the server saw the
* SetSelectionOwner request.
*/
for (infoPtr = dispPtr->selectionInfoPtr, prevPtr = NULL;
infoPtr != NULL; infoPtr = infoPtr->nextPtr) {
if (infoPtr->selection == eventPtr->xselectionclear.selection) {
break;
}
prevPtr = infoPtr;
}
if (infoPtr != NULL && (infoPtr->owner == tkwin) &&
(eventPtr->xselectionclear.serial >= (unsigned) infoPtr->serial)) {
if (prevPtr == NULL) {
dispPtr->selectionInfoPtr = infoPtr->nextPtr;
} else {
prevPtr->nextPtr = infoPtr->nextPtr;
}
/*
* Because of reentrancy problems, calling clearProc must be done
* after the infoPtr has been removed from the selectionInfoPtr list
* (clearProc could modify the list, e.g. by creating a new
* selection).
*/
if (infoPtr->clearProc != NULL) {
infoPtr->clearProc(infoPtr->clearData);
}
ckfree(infoPtr);
}
}
/*
*--------------------------------------------------------------
*
* SelGetProc --
*
* This function is invoked to process pieces of the selection as they
* arrive during "selection get" commands.
*
* Results:
* Always returns TCL_OK.
*
* Side effects:
* Bytes get appended to the dynamic string pointed to by the clientData
* argument.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
SelGetProc(
ClientData clientData, /* Dynamic string holding partially assembled
* selection. */
Tcl_Interp *interp, /* Interpreter used for error reporting (not
* used). */
const char *portion) /* New information to be appended. */
{
Tcl_DStringAppend(clientData, portion, -1);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* HandleTclCommand --
*
* This function acts as selection handler for handlers created by the
* "selection handle" command. It invokes a Tcl command to retrieve the
* selection.
*
* Results:
* The return value is a count of the number of bytes actually stored at
* buffer, or -1 if an error occurs while executing the Tcl command to
* retrieve the selection.
*
* Side effects:
* None except for things done by the Tcl command.
*
*----------------------------------------------------------------------
*/
static int
HandleTclCommand(
ClientData clientData, /* Information about command to execute. */
int offset, /* Return selection bytes starting at this
* offset. */
char *buffer, /* Place to store converted selection. */
int maxBytes) /* Maximum # of bytes to store at buffer. */
{
CommandInfo *cmdInfoPtr = clientData;
int spaceNeeded, length;
#define MAX_STATIC_SIZE 100
char staticSpace[MAX_STATIC_SIZE];
char *command;
const char *string;
Tcl_Interp *interp = cmdInfoPtr->interp;
Tcl_DString oldResult;
int extraBytes, charOffset, count, numChars, code;
const char *p;
/*
* We must also protect the interpreter and the command from being deleted
* too soon.
*/
Tcl_Preserve(clientData);
Tcl_Preserve(interp);
/*
* Compute the proper byte offset in the case where the last chunk split a
* character.
*/
if (offset == cmdInfoPtr->byteOffset) {
charOffset = cmdInfoPtr->charOffset;
extraBytes = strlen(cmdInfoPtr->buffer);
if (extraBytes > 0) {
strcpy(buffer, cmdInfoPtr->buffer);
maxBytes -= extraBytes;
buffer += extraBytes;
}
} else {
cmdInfoPtr->byteOffset = 0;
cmdInfoPtr->charOffset = 0;
extraBytes = 0;
charOffset = 0;
}
/*
* First, generate a command by taking the command string and appending
* the offset and maximum # of bytes.
*/
spaceNeeded = cmdInfoPtr->cmdLength + 30;
if (spaceNeeded < MAX_STATIC_SIZE) {
command = staticSpace;
} else {
command = ckalloc(spaceNeeded);
}
sprintf(command, "%s %d %d", cmdInfoPtr->command, charOffset, maxBytes);
/*
* Execute the command. Be sure to restore the state of the interpreter
* after executing the command.
*/
Tcl_DStringInit(&oldResult);
Tcl_DStringGetResult(interp, &oldResult);
code = Tcl_EvalEx(interp, command, -1, TCL_EVAL_GLOBAL);
if (code == TCL_OK) {
string = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &length);
count = (length > maxBytes) ? maxBytes : length;
memcpy(buffer, string, (size_t) count);
buffer[count] = '\0';
/*
* Update the partial character information for the next retrieval if
* the command has not been deleted.
*/
if (cmdInfoPtr->interp != NULL) {
if (length <= maxBytes) {
cmdInfoPtr->charOffset += Tcl_NumUtfChars(string, -1);
cmdInfoPtr->buffer[0] = '\0';
} else {
p = string;
string += count;
numChars = 0;
while (p < string) {
p = Tcl_UtfNext(p);
numChars++;
}
cmdInfoPtr->charOffset += numChars;
length = p - string;
if (length > 0) {
strncpy(cmdInfoPtr->buffer, string, (size_t) length);
}
cmdInfoPtr->buffer[length] = '\0';
}
cmdInfoPtr->byteOffset += count + extraBytes;
}
count += extraBytes;
} else {
/*
* Something went wrong. Log errors as background errors, and silently
* drop everything else.
*/
if (code == TCL_ERROR) {
Tcl_AddErrorInfo(interp, "\n (command handling selection)");
Tcl_BackgroundException(interp, code);
}
count = -1;
}
Tcl_DStringResult(interp, &oldResult);
if (command != staticSpace) {
ckfree(command);
}
Tcl_Release(clientData);
Tcl_Release(interp);
return count;
}
/*
*----------------------------------------------------------------------
*
* TkSelDefaultSelection --
*
* This function is called to generate selection information for a few
* standard targets such as TIMESTAMP and TARGETS. It is invoked only if
* no handler has been declared by the application.
*
* Results:
* If "target" is a standard target understood by this function, the
* selection is converted to that form and stored as a character string
* in buffer. The type of the selection (e.g. STRING or ATOM) is stored
* in *typePtr, and the return value is a count of the # of non-NULL
* bytes at buffer. If the target wasn't understood, or if there isn't
* enough space at buffer to hold the entire selection (no INCR-mode
* transfers for this stuff!), then -1 is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TkSelDefaultSelection(
TkSelectionInfo *infoPtr, /* Info about selection being retrieved. */
Atom target, /* Desired form of selection. */
char *buffer, /* Place to put selection characters. */
int maxBytes, /* Maximum # of bytes to store at buffer. */
Atom *typePtr) /* Store here the type of the selection, for
* use in converting to proper X format. */
{
register TkWindow *winPtr = (TkWindow *) infoPtr->owner;
TkDisplay *dispPtr = winPtr->dispPtr;
if (target == dispPtr->timestampAtom) {
if (maxBytes < 20) {
return -1;
}
sprintf(buffer, "0x%x", (unsigned int) infoPtr->time);
*typePtr = XA_INTEGER;
return strlen(buffer);
}
if (target == dispPtr->targetsAtom) {
register TkSelHandler *selPtr;
int length;
Tcl_DString ds;
if (maxBytes < 50) {
return -1;
}
Tcl_DStringInit(&ds);
Tcl_DStringAppend(&ds,
"MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW", -1);
for (selPtr = winPtr->selHandlerList; selPtr != NULL;
selPtr = selPtr->nextPtr) {
if ((selPtr->selection == infoPtr->selection)
&& (selPtr->target != dispPtr->applicationAtom)
&& (selPtr->target != dispPtr->windowAtom)) {
const char *atomString = Tk_GetAtomName((Tk_Window) winPtr,
selPtr->target);
Tcl_DStringAppendElement(&ds, atomString);
}
}
length = Tcl_DStringLength(&ds);
if (length >= maxBytes) {
Tcl_DStringFree(&ds);
return -1;
}
memcpy(buffer, Tcl_DStringValue(&ds), (unsigned) (1+length));
Tcl_DStringFree(&ds);
*typePtr = XA_ATOM;
return length;
}
if (target == dispPtr->applicationAtom) {
int length;
Tk_Uid name = winPtr->mainPtr->winPtr->nameUid;
length = strlen(name);
if (maxBytes <= length) {
return -1;
}
strcpy(buffer, name);
*typePtr = XA_STRING;
return length;
}
if (target == dispPtr->windowAtom) {
int length;
char *name = winPtr->pathName;
length = strlen(name);
if (maxBytes <= length) {
return -1;
}
strcpy(buffer, name);
*typePtr = XA_STRING;
return length;
}
return -1;
}
/*
*----------------------------------------------------------------------
*
* LostSelection --
*
* This function is invoked when a window has lost ownership of the
* selection and the ownership was claimed with the command "selection
* own".
*
* Results:
* None.
*
* Side effects:
* A Tcl script is executed; it can do almost anything.
*
*----------------------------------------------------------------------
*/
static void
LostSelection(
ClientData clientData) /* Pointer to LostCommand structure. */
{
LostCommand *lostPtr = clientData;
Tcl_Obj *objPtr;
Tcl_Interp *interp;
int code;
interp = lostPtr->interp;
Tcl_Preserve(interp);
/*
* Execute the command. Save the interpreter's result, if any, and restore
* it after executing the command.
*/
objPtr = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(objPtr);
Tcl_ResetResult(interp);
code = Tcl_EvalObjEx(interp, lostPtr->cmdObj, TCL_EVAL_GLOBAL);
if (code != TCL_OK) {
Tcl_BackgroundException(interp, code);
}
Tcl_SetObjResult(interp, objPtr);
Tcl_DecrRefCount(objPtr);
Tcl_Release(interp);
/*
* Free the storage for the command, since we're done with it now.
*/
Tcl_DecrRefCount(lostPtr->cmdObj);
ckfree(lostPtr);
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|