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
|
/*
* tkClipboard.c --
*
* This file manages the clipboard for the Tk toolkit, maintaining a
* collection of data buffers that will be supplied on demand to
* requesting applications.
*
* Copyright (c) 1994 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.
*
* RCS: @(#) $Id: tkClipboard.c,v 1.15.2.2 2007/11/21 16:46:26 dgp Exp $
*/
#include "tkInt.h"
#include "tkSelect.h"
/*
* Prototypes for functions used only in this file:
*/
static int ClipboardAppHandler(ClientData clientData,
int offset, char *buffer, int maxBytes);
static int ClipboardHandler(ClientData clientData,
int offset, char *buffer, int maxBytes);
static int ClipboardWindowHandler(ClientData clientData,
int offset, char *buffer, int maxBytes);
static void ClipboardLostSel(ClientData clientData);
static int ClipboardGetProc(ClientData clientData,
Tcl_Interp *interp, char *portion);
/*
*----------------------------------------------------------------------
*
* ClipboardHandler --
*
* This function acts as selection handler for the clipboard manager. It
* extracts the required chunk of data from the buffer chain for a given
* selection target.
*
* Results:
* The return value is a count of the number of bytes actually stored at
* buffer.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ClipboardHandler(
ClientData clientData, /* Information about data to fetch. */
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. */
{
TkClipboardTarget *targetPtr = (TkClipboardTarget*) clientData;
TkClipboardBuffer *cbPtr;
char *srcPtr, *destPtr;
size_t count = 0;
int scanned = 0;
size_t length, freeCount;
/*
* Skip to buffer containing offset byte
*/
for (cbPtr = targetPtr->firstBufferPtr; ; cbPtr = cbPtr->nextPtr) {
if (cbPtr == NULL) {
return 0;
}
if (scanned + cbPtr->length > offset) {
break;
}
scanned += cbPtr->length;
}
/*
* Copy up to maxBytes or end of list, switching buffers as needed.
*/
freeCount = maxBytes;
srcPtr = cbPtr->buffer + (offset - scanned);
destPtr = buffer;
length = cbPtr->length - (offset - scanned);
while (1) {
if (length > freeCount) {
strncpy(destPtr, srcPtr, freeCount);
return maxBytes;
} else {
strncpy(destPtr, srcPtr, length);
destPtr += length;
count += length;
freeCount -= length;
}
cbPtr = cbPtr->nextPtr;
if (cbPtr == NULL) {
break;
}
srcPtr = cbPtr->buffer;
length = cbPtr->length;
}
return (int)count;
}
/*
*----------------------------------------------------------------------
*
* ClipboardAppHandler --
*
* This function acts as selection handler for retrievals of type
* TK_APPLICATION. It returns the name of the application that owns the
* clipboard. Note: we can't use the default Tk selection handler for
* this selection type, because the clipboard window isn't a "real"
* window and doesn't have the necessary information.
*
* Results:
* The return value is a count of the number of bytes actually stored at
* buffer.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ClipboardAppHandler(
ClientData clientData, /* Pointer to TkDisplay structure. */
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. */
{
TkDisplay *dispPtr = (TkDisplay *) clientData;
size_t length;
CONST char *p;
p = dispPtr->clipboardAppPtr->winPtr->nameUid;
length = strlen(p);
length -= offset;
if (length <= 0) {
return 0;
}
if (length > (size_t) maxBytes) {
length = maxBytes;
}
strncpy(buffer, p, length);
return (int)length;
}
/*
*----------------------------------------------------------------------
*
* ClipboardWindowHandler --
*
* This function acts as selection handler for retrievals of type
* TK_WINDOW. Since the clipboard doesn't correspond to any particular
* window, we just return ".". We can't use Tk's default handler for this
* selection type, because the clipboard window isn't a valid window.
*
* Results:
* The return value is 1, the number of non-null bytes stored at buffer.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ClipboardWindowHandler(
ClientData clientData, /* Not used. */
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. */
{
buffer[0] = '.';
buffer[1] = 0;
return 1;
}
/*
*----------------------------------------------------------------------
*
* ClipboardLostSel --
*
* This function is invoked whenever clipboard ownership is claimed by
* another window. It just sets a flag so that we know the clipboard was
* taken away.
*
* Results:
* None.
*
* Side effects:
* The clipboard is marked as inactive.
*
*----------------------------------------------------------------------
*/
static void
ClipboardLostSel(
ClientData clientData) /* Pointer to TkDisplay structure. */
{
TkDisplay *dispPtr = (TkDisplay*) clientData;
dispPtr->clipboardActive = 0;
}
/*
*----------------------------------------------------------------------
*
* Tk_ClipboardClear --
*
* Take control of the clipboard and clear out the previous contents.
* This function must be invoked before any calls to Tk_ClipboardAppend.
*
* Results:
* A standard Tcl result. If an error occurs, an error message is left in
* the interp's result.
*
* Side effects:
* From now on, requests for the CLIPBOARD selection will be directed to
* the clipboard manager routines associated with clipWindow for the
* display of tkwin. In order to guarantee atomicity, no event handling
* should occur between Tk_ClipboardClear and the following
* Tk_ClipboardAppend calls. This function may cause a user-defined
* LostSel command to be invoked when the CLIPBOARD is claimed, so any
* calling function should be reentrant at the point Tk_ClipboardClear is
* invoked.
*
*----------------------------------------------------------------------
*/
int
Tk_ClipboardClear(
Tcl_Interp *interp, /* Interpreter to use for error reporting. */
Tk_Window tkwin) /* Window in application that is clearing
* clipboard; identifies application and
* display. */
{
TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkClipboardTarget *targetPtr, *nextTargetPtr;
TkClipboardBuffer *cbPtr, *nextCbPtr;
if (dispPtr->clipWindow == NULL) {
int result;
result = TkClipInit(interp, dispPtr);
if (result != TCL_OK) {
return result;
}
}
/*
* Discard any existing clipboard data and delete the selection handler(s)
* associated with that data.
*/
for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
targetPtr = nextTargetPtr) {
for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
cbPtr = nextCbPtr) {
ckfree(cbPtr->buffer);
nextCbPtr = cbPtr->nextPtr;
ckfree((char *) cbPtr);
}
nextTargetPtr = targetPtr->nextPtr;
Tk_DeleteSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
targetPtr->type);
ckfree((char *) targetPtr);
}
dispPtr->clipTargetPtr = NULL;
/*
* Reclaim the clipboard selection if we lost it.
*/
if (!dispPtr->clipboardActive) {
Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom,
ClipboardLostSel, (ClientData) dispPtr);
dispPtr->clipboardActive = 1;
}
dispPtr->clipboardAppPtr = winPtr->mainPtr;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tk_ClipboardAppend --
*
* Append a buffer of data to the clipboard. The first buffer of a given
* type determines the format for that type. Any successive appends to
* that type must have the same format or an error will be returned.
* Tk_ClipboardClear must be called before a sequence of
* Tk_ClipboardAppend calls can be issued. In order to guarantee
* atomicity, no event handling should occur between Tk_ClipboardClear
* and the following Tk_ClipboardAppend calls.
*
* Results:
* A standard Tcl result. If an error is returned, an error message is
* left in the interp's result.
*
* Side effects:
* The specified buffer will be copied onto the end of the clipboard.
* The clipboard maintains a list of buffers which will be used to supply
* the data for a selection get request. The first time a given type is
* appended, Tk_ClipboardAppend will register a selection handler of the
* appropriate type.
*
*----------------------------------------------------------------------
*/
int
Tk_ClipboardAppend(
Tcl_Interp *interp, /* Used for error reporting. */
Tk_Window tkwin, /* Window that selects a display. */
Atom type, /* The desired conversion type for this
* clipboard item, e.g. STRING or LENGTH. */
Atom format, /* Format in which the selection information
* should be returned to the requestor. */
char* buffer) /* NULL terminated string containing the data
* to be added to the clipboard. */
{
TkWindow *winPtr = (TkWindow *) tkwin;
TkDisplay *dispPtr = winPtr->dispPtr;
TkClipboardTarget *targetPtr;
TkClipboardBuffer *cbPtr;
/*
* If this application doesn't already own the clipboard, clear the
* clipboard. If we don't own the clipboard selection, claim it.
*/
if (dispPtr->clipboardAppPtr != winPtr->mainPtr) {
Tk_ClipboardClear(interp, tkwin);
} else if (!dispPtr->clipboardActive) {
Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom,
ClipboardLostSel, (ClientData) dispPtr);
dispPtr->clipboardActive = 1;
}
/*
* Check to see if the specified target is already present on the
* clipboard. If it isn't, we need to create a new target; otherwise, we
* just append the new buffer to the clipboard list.
*/
for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
targetPtr = targetPtr->nextPtr) {
if (targetPtr->type == type) {
break;
}
}
if (targetPtr == NULL) {
targetPtr = (TkClipboardTarget*) ckalloc(sizeof(TkClipboardTarget));
targetPtr->type = type;
targetPtr->format = format;
targetPtr->firstBufferPtr = targetPtr->lastBufferPtr = NULL;
targetPtr->nextPtr = dispPtr->clipTargetPtr;
dispPtr->clipTargetPtr = targetPtr;
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
type, ClipboardHandler, (ClientData) targetPtr, format);
} else if (targetPtr->format != format) {
Tcl_AppendResult(interp, "format \"", Tk_GetAtomName(tkwin, format),
"\" does not match current format \"",
Tk_GetAtomName(tkwin, targetPtr->format),"\" for ",
Tk_GetAtomName(tkwin, type), NULL);
return TCL_ERROR;
}
/*
* Append a new buffer to the buffer chain.
*/
cbPtr = (TkClipboardBuffer*) ckalloc(sizeof(TkClipboardBuffer));
cbPtr->nextPtr = NULL;
if (targetPtr->lastBufferPtr != NULL) {
targetPtr->lastBufferPtr->nextPtr = cbPtr;
} else {
targetPtr->firstBufferPtr = cbPtr;
}
targetPtr->lastBufferPtr = cbPtr;
cbPtr->length = strlen(buffer);
cbPtr->buffer = (char *) ckalloc((unsigned) (cbPtr->length + 1));
strcpy(cbPtr->buffer, buffer);
TkSelUpdateClipboard((TkWindow*)(dispPtr->clipWindow), targetPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tk_ClipboardObjCmd --
*
* This function is invoked to process the "clipboard" 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_ClipboardObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *CONST objv[]) /* Argument strings. */
{
Tk_Window tkwin = (Tk_Window) clientData;
char *path = NULL;
Atom selection;
static CONST char *optionStrings[] = { "append", "clear", "get", NULL };
enum options { CLIPBOARD_APPEND, CLIPBOARD_CLEAR, CLIPBOARD_GET };
int index, i;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
&index) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum options) index) {
case CLIPBOARD_APPEND: {
Atom target, format;
char *targetName = NULL;
char *formatName = NULL;
char *string;
static CONST char *appendOptionStrings[] = {
"-displayof", "-format", "-type", NULL
};
enum appendOptions { APPEND_DISPLAYOF, APPEND_FORMAT, APPEND_TYPE };
int subIndex, length;
for (i = 2; i < objc - 1; i++) {
string = Tcl_GetStringFromObj(objv[i], &length);
if (string[0] != '-') {
break;
}
/*
* If the argument is "--", it signifies the end of arguments.
*/
if (string[1] == '-' && length == 2) {
i++;
break;
}
if (Tcl_GetIndexFromObj(interp, objv[i], appendOptionStrings,
"option", 0, &subIndex) != TCL_OK) {
return TCL_ERROR;
}
/*
* Increment i so that it points to the value for the flag instead
* of the flag itself.
*/
i++;
if (i >= objc) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
switch ((enum appendOptions) subIndex) {
case APPEND_DISPLAYOF:
path = Tcl_GetString(objv[i]);
break;
case APPEND_FORMAT:
formatName = Tcl_GetString(objv[i]);
break;
case APPEND_TYPE:
targetName = Tcl_GetString(objv[i]);
break;
}
}
if (objc - i != 1) {
Tcl_WrongNumArgs(interp, 2, objv, "?options? data");
return TCL_ERROR;
}
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
if (targetName != NULL) {
target = Tk_InternAtom(tkwin, targetName);
} else {
target = XA_STRING;
}
if (formatName != NULL) {
format = Tk_InternAtom(tkwin, formatName);
} else {
format = XA_STRING;
}
return Tk_ClipboardAppend(interp, tkwin, target, format,
Tcl_GetString(objv[i]));
}
case CLIPBOARD_CLEAR: {
static CONST char *clearOptionStrings[] = { "-displayof", NULL };
enum clearOptions { CLEAR_DISPLAYOF };
int subIndex;
if (objc != 2 && objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv, "?-displayof window?");
return TCL_ERROR;
}
if (objc == 4) {
if (Tcl_GetIndexFromObj(interp, objv[2], clearOptionStrings,
"option", 0, &subIndex) != TCL_OK) {
return TCL_ERROR;
}
if ((enum clearOptions) subIndex == CLEAR_DISPLAYOF) {
path = Tcl_GetString(objv[3]);
}
}
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
return Tk_ClipboardClear(interp, tkwin);
}
case CLIPBOARD_GET: {
Atom target;
char *targetName = NULL;
Tcl_DString selBytes;
int result;
char *string;
static CONST char *getOptionStrings[] = {
"-displayof", "-type", NULL
};
enum getOptions { APPEND_DISPLAYOF, APPEND_TYPE };
int subIndex;
for (i = 2; i < objc; i++) {
string = Tcl_GetString(objv[i]);
if (string[0] != '-') {
break;
}
if (Tcl_GetIndexFromObj(interp, objv[i], getOptionStrings,
"option", 0, &subIndex) != TCL_OK) {
return TCL_ERROR;
}
i++;
if (i >= objc) {
Tcl_AppendResult(interp, "value for \"", string,
"\" missing", NULL);
return TCL_ERROR;
}
switch ((enum getOptions) subIndex) {
case APPEND_DISPLAYOF:
path = Tcl_GetString(objv[i]);
break;
case APPEND_TYPE:
targetName = Tcl_GetString(objv[i]);
break;
}
}
if (path != NULL) {
tkwin = Tk_NameToWindow(interp, path, tkwin);
}
if (tkwin == NULL) {
return TCL_ERROR;
}
selection = Tk_InternAtom(tkwin, "CLIPBOARD");
if (objc - i > 1) {
Tcl_WrongNumArgs(interp, 2, objv, "?options?");
return TCL_ERROR;
} else if (objc - i == 1) {
target = Tk_InternAtom(tkwin, Tcl_GetString(objv[i]));
} else if (targetName != NULL) {
target = Tk_InternAtom(tkwin, targetName);
} else {
target = XA_STRING;
}
Tcl_DStringInit(&selBytes);
result = Tk_GetSelection(interp, tkwin, selection, target,
ClipboardGetProc, (ClientData) &selBytes);
if (result == TCL_OK) {
Tcl_DStringResult(interp, &selBytes);
} else {
Tcl_DStringFree(&selBytes);
}
return result;
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkClipInit --
*
* This function is called to initialize the window for claiming
* clipboard ownership and for receiving selection get results. This
* function is called from tkSelect.c as well as tkClipboard.c.
*
* Results:
* The result is a standard Tcl return value, which is normally TCL_OK.
* If an error occurs then an error message is left in the interp's
* result and TCL_ERROR is returned.
*
* Side effects:
* Sets up the clipWindow and related data structures.
*
*----------------------------------------------------------------------
*/
int
TkClipInit(
Tcl_Interp *interp, /* Interpreter to use for error reporting. */
register TkDisplay *dispPtr)/* Display to initialize. */
{
XSetWindowAttributes atts;
dispPtr->clipTargetPtr = NULL;
dispPtr->clipboardActive = 0;
dispPtr->clipboardAppPtr = NULL;
/*
* Create the window used for clipboard ownership and selection retrieval,
* and set up an event handler for it.
*/
dispPtr->clipWindow = Tk_CreateWindow(interp, (Tk_Window) NULL,
"_clip", DisplayString(dispPtr->display));
if (dispPtr->clipWindow == NULL) {
return TCL_ERROR;
}
Tcl_Preserve((ClientData) dispPtr->clipWindow);
atts.override_redirect = True;
Tk_ChangeWindowAttributes(dispPtr->clipWindow, CWOverrideRedirect, &atts);
Tk_MakeWindowExist(dispPtr->clipWindow);
if (dispPtr->multipleAtom == None) {
/*
* Need to invoke selection initialization to make sure that atoms we
* depend on below are defined.
*/
TkSelInit(dispPtr->clipWindow);
}
/*
* Create selection handlers for types TK_APPLICATION and TK_WINDOW on
* this window. Can't use the default handlers for these types because
* this isn't a full-fledged window.
*/
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
dispPtr->applicationAtom, ClipboardAppHandler,
(ClientData) dispPtr, XA_STRING);
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
dispPtr->windowAtom, ClipboardWindowHandler,
(ClientData) dispPtr, XA_STRING);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ClipboardGetProc --
*
* This function is invoked to process pieces of the selection as they
* arrive during "clipboard 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
ClipboardGetProc(
ClientData clientData, /* Dynamic string holding partially assembled
* selection. */
Tcl_Interp *interp, /* Interpreter used for error reporting (not
* used). */
char *portion) /* New information to be appended. */
{
Tcl_DStringAppend((Tcl_DString *) clientData, portion, -1);
return TCL_OK;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|