Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkClipboard.c
1810 views
1
/*
2
* tkClipboard.c --
3
*
4
* This file manages the clipboard for the Tk toolkit,
5
* maintaining a collection of data buffers that will be
6
* supplied on demand to requesting applications.
7
*
8
* Copyright (c) 1994 The Regents of the University of California.
9
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
10
*
11
* See the file "license.terms" for information on usage and redistribution
12
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
*
14
* SCCS: @(#) tkClipboard.c 1.14 96/02/15 18:52:37
15
*/
16
17
#include "tkInt.h"
18
#include "tkSelect.h"
19
20
/*
21
* Prototypes for procedures used only in this file:
22
*/
23
24
static int ClipboardAppHandler _ANSI_ARGS_((ClientData clientData,
25
int offset, char *buffer, int maxBytes));
26
static int ClipboardHandler _ANSI_ARGS_((ClientData clientData,
27
int offset, char *buffer, int maxBytes));
28
static int ClipboardWindowHandler _ANSI_ARGS_((
29
ClientData clientData, int offset, char *buffer,
30
int maxBytes));
31
static void ClipboardLostSel _ANSI_ARGS_((ClientData clientData));
32
33
/*
34
*----------------------------------------------------------------------
35
*
36
* ClipboardHandler --
37
*
38
* This procedure acts as selection handler for the
39
* clipboard manager. It extracts the required chunk of
40
* data from the buffer chain for a given selection target.
41
*
42
* Results:
43
* The return value is a count of the number of bytes
44
* actually stored at buffer.
45
*
46
* Side effects:
47
* None.
48
*
49
*----------------------------------------------------------------------
50
*/
51
52
static int
53
ClipboardHandler(clientData, offset, buffer, maxBytes)
54
ClientData clientData; /* Information about data to fetch. */
55
int offset; /* Return selection bytes starting at this
56
* offset. */
57
char *buffer; /* Place to store converted selection. */
58
int maxBytes; /* Maximum # of bytes to store at buffer. */
59
{
60
TkClipboardTarget *targetPtr = (TkClipboardTarget*) clientData;
61
TkClipboardBuffer *cbPtr;
62
char *srcPtr, *destPtr;
63
int count = 0;
64
int scanned = 0;
65
size_t length, freeCount;
66
67
/*
68
* Skip to buffer containing offset byte
69
*/
70
71
for (cbPtr = targetPtr->firstBufferPtr; ; cbPtr = cbPtr->nextPtr) {
72
if (cbPtr == NULL) {
73
return 0;
74
}
75
if (scanned + cbPtr->length > offset) {
76
break;
77
}
78
scanned += cbPtr->length;
79
}
80
81
/*
82
* Copy up to maxBytes or end of list, switching buffers as needed.
83
*/
84
85
freeCount = maxBytes;
86
srcPtr = cbPtr->buffer + (offset - scanned);
87
destPtr = buffer;
88
length = cbPtr->length - (offset - scanned);
89
while (1) {
90
if (length > freeCount) {
91
strncpy(destPtr, srcPtr, freeCount);
92
return maxBytes;
93
} else {
94
strncpy(destPtr, srcPtr, length);
95
destPtr += length;
96
count += length;
97
freeCount -= length;
98
}
99
cbPtr = cbPtr->nextPtr;
100
if (cbPtr == NULL) {
101
break;
102
}
103
srcPtr = cbPtr->buffer;
104
length = cbPtr->length;
105
}
106
return count;
107
}
108
109
/*
110
*----------------------------------------------------------------------
111
*
112
* ClipboardAppHandler --
113
*
114
* This procedure acts as selection handler for retrievals of type
115
* TK_APPLICATION. It returns the name of the application that
116
* owns the clipboard. Note: we can't use the default Tk
117
* selection handler for this selection type, because the clipboard
118
* window isn't a "real" window and doesn't have the necessary
119
* information.
120
*
121
* Results:
122
* The return value is a count of the number of bytes
123
* actually stored at buffer.
124
*
125
* Side effects:
126
* None.
127
*
128
*----------------------------------------------------------------------
129
*/
130
131
static int
132
ClipboardAppHandler(clientData, offset, buffer, maxBytes)
133
ClientData clientData; /* Pointer to TkDisplay structure. */
134
int offset; /* Return selection bytes starting at this
135
* offset. */
136
char *buffer; /* Place to store converted selection. */
137
int maxBytes; /* Maximum # of bytes to store at buffer. */
138
{
139
TkDisplay *dispPtr = (TkDisplay *) clientData;
140
size_t length;
141
char *p;
142
143
p = dispPtr->clipboardAppPtr->winPtr->nameUid;
144
length = strlen(p);
145
length -= offset;
146
if (length <= 0) {
147
return 0;
148
}
149
if (length > maxBytes) {
150
length = maxBytes;
151
}
152
strncpy(buffer, p, length);
153
return length;
154
}
155
156
/*
157
*----------------------------------------------------------------------
158
*
159
* ClipboardWindowHandler --
160
*
161
* This procedure acts as selection handler for retrievals of
162
* type TK_WINDOW. Since the clipboard doesn't correspond to
163
* any particular window, we just return ".". We can't use Tk's
164
* default handler for this selection type, because the clipboard
165
* window isn't a valid window.
166
*
167
* Results:
168
* The return value is 1, the number of non-null bytes stored
169
* at buffer.
170
*
171
* Side effects:
172
* None.
173
*
174
*----------------------------------------------------------------------
175
*/
176
177
static int
178
ClipboardWindowHandler(clientData, offset, buffer, maxBytes)
179
ClientData clientData; /* Not used. */
180
int offset; /* Return selection bytes starting at this
181
* offset. */
182
char *buffer; /* Place to store converted selection. */
183
int maxBytes; /* Maximum # of bytes to store at buffer. */
184
{
185
buffer[0] = '.';
186
buffer[1] = 0;
187
return 1;
188
}
189
190
/*
191
*----------------------------------------------------------------------
192
*
193
* ClipboardLostSel --
194
*
195
* This procedure is invoked whenever clipboard ownership is
196
* claimed by another window. It just sets a flag so that we
197
* know the clipboard was taken away.
198
*
199
* Results:
200
* None.
201
*
202
* Side effects:
203
* The clipboard is marked as inactive.
204
*
205
*----------------------------------------------------------------------
206
*/
207
208
static void
209
ClipboardLostSel(clientData)
210
ClientData clientData; /* Pointer to TkDisplay structure. */
211
{
212
TkDisplay *dispPtr = (TkDisplay*) clientData;
213
214
dispPtr->clipboardActive = 0;
215
}
216
217
/*
218
*----------------------------------------------------------------------
219
*
220
* Tk_ClipboardClear --
221
*
222
* Take control of the clipboard and clear out the previous
223
* contents. This procedure must be invoked before any
224
* calls to Tk_AppendToClipboard.
225
*
226
* Results:
227
* A standard Tcl result. If an error occurs, an error message is
228
* left in interp->result.
229
*
230
* Side effects:
231
* From now on, requests for the CLIPBOARD selection will be
232
* directed to the clipboard manager routines associated with
233
* clipWindow for the display of tkwin. In order to guarantee
234
* atomicity, no event handling should occur between
235
* Tk_ClipboardClear and the following Tk_AppendToClipboard
236
* calls. This procedure may cause a user-defined LostSel command
237
* to be invoked when the CLIPBOARD is claimed, so any calling
238
* function should be reentrant at the point Tk_ClipboardClear is
239
* invoked.
240
*
241
*----------------------------------------------------------------------
242
*/
243
244
int
245
Tk_ClipboardClear(interp, tkwin)
246
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
247
Tk_Window tkwin; /* Window in application that is clearing
248
* clipboard; identifies application and
249
* display. */
250
{
251
TkWindow *winPtr = (TkWindow *) tkwin;
252
TkDisplay *dispPtr = winPtr->dispPtr;
253
TkClipboardTarget *targetPtr, *nextTargetPtr;
254
TkClipboardBuffer *cbPtr, *nextCbPtr;
255
256
if (dispPtr->clipWindow == NULL) {
257
int result;
258
259
result = TkClipInit(interp, dispPtr);
260
if (result != TCL_OK) {
261
return result;
262
}
263
}
264
265
/*
266
* Discard any existing clipboard data and delete the selection
267
* handler(s) associated with that data.
268
*/
269
270
for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
271
targetPtr = nextTargetPtr) {
272
for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
273
cbPtr = nextCbPtr) {
274
ckfree(cbPtr->buffer);
275
nextCbPtr = cbPtr->nextPtr;
276
ckfree((char *) cbPtr);
277
}
278
nextTargetPtr = targetPtr->nextPtr;
279
Tk_DeleteSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
280
targetPtr->type);
281
ckfree((char *) targetPtr);
282
}
283
dispPtr->clipTargetPtr = NULL;
284
285
/*
286
* Reclaim the clipboard selection if we lost it.
287
*/
288
289
if (!dispPtr->clipboardActive) {
290
Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom,
291
ClipboardLostSel, (ClientData) dispPtr);
292
dispPtr->clipboardActive = 1;
293
}
294
dispPtr->clipboardAppPtr = winPtr->mainPtr;
295
return TCL_OK;
296
}
297
298
/*
299
*----------------------------------------------------------------------
300
*
301
* Tk_ClipboardAppend --
302
*
303
* Append a buffer of data to the clipboard. The first buffer of
304
* a given type determines the format for that type. Any successive
305
* appends to that type must have the same format or an error will
306
* be returned. Tk_ClipboardClear must be called before a sequence
307
* of Tk_ClipboardAppend calls can be issued. In order to guarantee
308
* atomicity, no event handling should occur between Tk_ClipboardClear
309
* and the following Tk_AppendToClipboard calls.
310
*
311
* Results:
312
* A standard Tcl result. If an error is returned, an error message
313
* is left in interp->result.
314
*
315
* Side effects:
316
* The specified buffer will be copied onto the end of the clipboard.
317
* The clipboard maintains a list of buffers which will be used to
318
* supply the data for a selection get request. The first time a given
319
* type is appended, Tk_ClipboardAppend will register a selection
320
* handler of the appropriate type.
321
*
322
*----------------------------------------------------------------------
323
*/
324
325
int
326
Tk_ClipboardAppend(interp, tkwin, type, format, buffer)
327
Tcl_Interp *interp; /* Used for error reporting. */
328
Tk_Window tkwin; /* Window that selects a display. */
329
Atom type; /* The desired conversion type for this
330
* clipboard item, e.g. STRING or LENGTH. */
331
Atom format; /* Format in which the selection
332
* information should be returned to
333
* the requestor. */
334
char* buffer; /* NULL terminated string containing the data
335
* to be added to the clipboard. */
336
{
337
TkWindow *winPtr = (TkWindow *) tkwin;
338
TkDisplay *dispPtr = winPtr->dispPtr;
339
TkClipboardTarget *targetPtr;
340
TkClipboardBuffer *cbPtr;
341
342
/*
343
* If this application doesn't already own the clipboard, clear
344
* the clipboard. If we don't own the clipboard selection, claim it.
345
*/
346
347
if (dispPtr->clipboardAppPtr != winPtr->mainPtr) {
348
Tk_ClipboardClear(interp, tkwin);
349
} else if (!dispPtr->clipboardActive) {
350
Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom,
351
ClipboardLostSel, (ClientData) dispPtr);
352
dispPtr->clipboardActive = 1;
353
}
354
355
/*
356
* Check to see if the specified target is already present on the
357
* clipboard. If it isn't, we need to create a new target; otherwise,
358
* we just append the new buffer to the clipboard list.
359
*/
360
361
for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
362
targetPtr = targetPtr->nextPtr) {
363
if (targetPtr->type == type)
364
break;
365
}
366
if (targetPtr == NULL) {
367
targetPtr = (TkClipboardTarget*) ckalloc(sizeof(TkClipboardTarget));
368
targetPtr->type = type;
369
targetPtr->format = format;
370
targetPtr->firstBufferPtr = targetPtr->lastBufferPtr = NULL;
371
targetPtr->nextPtr = dispPtr->clipTargetPtr;
372
dispPtr->clipTargetPtr = targetPtr;
373
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
374
type, ClipboardHandler, (ClientData) targetPtr, format);
375
} else if (targetPtr->format != format) {
376
Tcl_AppendResult(interp, "format \"", Tk_GetAtomName(tkwin, format),
377
"\" does not match current format \"",
378
Tk_GetAtomName(tkwin, targetPtr->format),"\" for ",
379
Tk_GetAtomName(tkwin, type), (char *) NULL);
380
return TCL_ERROR;
381
}
382
383
/*
384
* Append a new buffer to the buffer chain.
385
*/
386
387
cbPtr = (TkClipboardBuffer*) ckalloc(sizeof(TkClipboardBuffer));
388
cbPtr->nextPtr = NULL;
389
if (targetPtr->lastBufferPtr != NULL) {
390
targetPtr->lastBufferPtr->nextPtr = cbPtr;
391
} else {
392
targetPtr->firstBufferPtr = cbPtr;
393
}
394
targetPtr->lastBufferPtr = cbPtr;
395
396
cbPtr->length = strlen(buffer);
397
cbPtr->buffer = (char *) ckalloc((unsigned) (cbPtr->length + 1));
398
strcpy(cbPtr->buffer, buffer);
399
400
TkSelUpdateClipboard((TkWindow*)(dispPtr->clipWindow), targetPtr);
401
402
return TCL_OK;
403
}
404
405
/*
406
*----------------------------------------------------------------------
407
*
408
* Tk_ClipboardCmd --
409
*
410
* This procedure is invoked to process the "clipboard" Tcl
411
* command. See the user documentation for details on what
412
* it does.
413
*
414
* Results:
415
* A standard Tcl result.
416
*
417
* Side effects:
418
* See the user documentation.
419
*
420
*----------------------------------------------------------------------
421
*/
422
423
int
424
Tk_ClipboardCmd(clientData, interp, argc, argv)
425
ClientData clientData; /* Main window associated with
426
* interpreter. */
427
Tcl_Interp *interp; /* Current interpreter. */
428
int argc; /* Number of arguments. */
429
char **argv; /* Argument strings. */
430
{
431
Tk_Window tkwin = (Tk_Window) clientData;
432
char *path = NULL;
433
size_t length;
434
int count;
435
char c;
436
char **args;
437
438
if (argc < 2) {
439
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
440
" option ?arg arg ...?\"", (char *) NULL);
441
return TCL_ERROR;
442
}
443
c = argv[1][0];
444
length = strlen(argv[1]);
445
if ((c == 'a') && (strncmp(argv[1], "append", length) == 0)) {
446
Atom target, format;
447
char *targetName = NULL;
448
char *formatName = NULL;
449
450
for (count = argc-2, args = argv+2; count > 1; count -= 2, args += 2) {
451
if (args[0][0] != '-') {
452
break;
453
}
454
c = args[0][1];
455
length = strlen(args[0]);
456
if ((c == '-') && (length == 2)) {
457
args++;
458
count--;
459
break;
460
}
461
if ((c == 'd') && (strncmp(args[0], "-displayof", length) == 0)) {
462
path = args[1];
463
} else if ((c == 'f')
464
&& (strncmp(args[0], "-format", length) == 0)) {
465
formatName = args[1];
466
} else if ((c == 't')
467
&& (strncmp(args[0], "-type", length) == 0)) {
468
targetName = args[1];
469
} else {
470
Tcl_AppendResult(interp, "unknown option \"", args[0],
471
"\"", (char *) NULL);
472
return TCL_ERROR;
473
}
474
}
475
if (count != 1) {
476
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
477
" append ?options? data\"", (char *) NULL);
478
return TCL_ERROR;
479
}
480
if (path != NULL) {
481
tkwin = Tk_NameToWindow(interp, path, tkwin);
482
}
483
if (tkwin == NULL) {
484
return TCL_ERROR;
485
}
486
if (targetName != NULL) {
487
target = Tk_InternAtom(tkwin, targetName);
488
} else {
489
target = XA_STRING;
490
}
491
if (formatName != NULL) {
492
format = Tk_InternAtom(tkwin, formatName);
493
} else {
494
format = XA_STRING;
495
}
496
return Tk_ClipboardAppend(interp, tkwin, target, format, args[0]);
497
} else if ((c == 'c') && (strncmp(argv[1], "clear", length) == 0)) {
498
for (count = argc-2, args = argv+2; count > 0; count -= 2, args += 2) {
499
if (args[0][0] != '-') {
500
break;
501
}
502
if (count < 2) {
503
Tcl_AppendResult(interp, "value for \"", *args,
504
"\" missing", (char *) NULL);
505
return TCL_ERROR;
506
}
507
c = args[0][1];
508
length = strlen(args[0]);
509
if ((c == 'd') && (strncmp(args[0], "-displayof", length) == 0)) {
510
path = args[1];
511
} else {
512
Tcl_AppendResult(interp, "unknown option \"", args[0],
513
"\"", (char *) NULL);
514
return TCL_ERROR;
515
}
516
}
517
if (count > 0) {
518
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
519
" clear ?options?\"", (char *) NULL);
520
return TCL_ERROR;
521
}
522
if (path != NULL) {
523
tkwin = Tk_NameToWindow(interp, path, tkwin);
524
}
525
if (tkwin == NULL) {
526
return TCL_ERROR;
527
}
528
return Tk_ClipboardClear(interp, tkwin);
529
} else {
530
sprintf(interp->result,
531
"bad option \"%.50s\": must be clear or append",
532
argv[1]);
533
return TCL_ERROR;
534
}
535
}
536
537
/*
538
*----------------------------------------------------------------------
539
*
540
* TkClipInit --
541
*
542
* This procedure is called to initialize the window for claiming
543
* clipboard ownership and for receiving selection get results. This
544
* function is called from tkSelect.c as well as tkClipboard.c.
545
*
546
* Results:
547
* The result is a standard Tcl return value, which is normally TCL_OK.
548
* If an error occurs then an error message is left in interp->result
549
* and TCL_ERROR is returned.
550
*
551
* Side effects:
552
* Sets up the clipWindow and related data structures.
553
*
554
*----------------------------------------------------------------------
555
*/
556
557
int
558
TkClipInit(interp, dispPtr)
559
Tcl_Interp *interp; /* Interpreter to use for error
560
* reporting. */
561
register TkDisplay *dispPtr;/* Display to initialize. */
562
{
563
XSetWindowAttributes atts;
564
565
dispPtr->clipTargetPtr = NULL;
566
dispPtr->clipboardActive = 0;
567
dispPtr->clipboardAppPtr = NULL;
568
569
/*
570
* Create the window used for clipboard ownership and selection retrieval,
571
* and set up an event handler for it.
572
*/
573
574
dispPtr->clipWindow = Tk_CreateWindow(interp, (Tk_Window) NULL,
575
"_clip", DisplayString(dispPtr->display));
576
if (dispPtr->clipWindow == NULL) {
577
return TCL_ERROR;
578
}
579
atts.override_redirect = True;
580
Tk_ChangeWindowAttributes(dispPtr->clipWindow, CWOverrideRedirect, &atts);
581
Tk_MakeWindowExist(dispPtr->clipWindow);
582
583
if (dispPtr->multipleAtom == None) {
584
/*
585
* Need to invoke selection initialization to make sure that
586
* atoms we depend on below are defined.
587
*/
588
589
TkSelInit(dispPtr->clipWindow);
590
}
591
592
/*
593
* Create selection handlers for types TK_APPLICATION and TK_WINDOW
594
* on this window. Can't use the default handlers for these types
595
* because this isn't a full-fledged window.
596
*/
597
598
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
599
dispPtr->applicationAtom, ClipboardAppHandler,
600
(ClientData) dispPtr, XA_STRING);
601
Tk_CreateSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
602
dispPtr->windowAtom, ClipboardWindowHandler,
603
(ClientData) dispPtr, XA_STRING);
604
return TCL_OK;
605
}
606
607