Debian Codegraph @

pTk/mTk/generic/tkSelect.c

raw symbols SCIP
1 /*
2 * tkSelect.c --
3 *
4 * This file manages the selection for the Tk toolkit,
5 * translating between the standard X ICCCM conventions
6 * and Tcl commands.
7 *
8 * Copyright (c) 1990-1993 The Regents of the University of California.
9 * Copyright (c) 1994-1997 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 * RCS: @(#) $Id: tkSelect.c,v 1.13 2003/01/14 19:24:56 jenglish Exp $
15 */
16
17 #include "tkInt.h"
18 #include "tkSelect.h"
19
20 /*
21 * When a selection handler is set up by invoking "selection handle",
22 * one of the following data structures is set up to hold information
23 * about the command to invoke and its interpreter.
24 */
25
26 typedef struct {
27 Tcl_Interp *interp; /* Interpreter in which to invoke command. */
28 int cmdLength; /* # of non-NULL bytes in command. */
29 int charOffset; /* The offset of the next char to retrieve. */
30 int byteOffset; /* The expected byte offset of the next
31 * chunk. */
32 char buffer[TCL_UTF_MAX]; /* A buffer to hold part of a UTF character
33 * that is split across chunks.*/
34 LangCallback *command; /* Command to invoke. Actual space is
35 * allocated as large as necessary. This
36 * must be the last entry in the structure. */
37 } CommandInfo;
38
39 /*
40 * When selection ownership is claimed with the "selection own" Tcl command,
41 * one of the following structures is created to record the Tcl command
42 * to be executed when the selection is lost again.
43 */
44
45 typedef struct LostCommand {
46 Tcl_Interp *interp; /* Interpreter in which to invoke command. */
47 LangCallback *command; /* Command to invoke. Actual space is
48 * allocated as large as necessary. This
49 * must be the last entry in the structure. */
50 } LostCommand;
51
52 /*
53 * The structure below is used to keep each thread's pending list
54 * separate.
55 */
56
57 typedef struct ThreadSpecificData {
58 TkSelInProgress *pendingPtr;
59 /* Topmost search in progress, or
60 * NULL if none. */
61 } ThreadSpecificData;
62 static Tcl_ThreadDataKey dataKey;
63
64 /*
65 * Forward declarations for procedures defined in this file:
66 */
67
68 static int HandleTclCommand _ANSI_ARGS_((ClientData clientData,
69 int offset, char *buffer, int maxBytes));
70 static void LostSelection _ANSI_ARGS_((ClientData clientData));
71 static int SelGetProc _ANSI_ARGS_((ClientData clientData,
72 Tcl_Interp *interp, char *portion));
73
74 static void SelRcvIncrProc _ANSI_ARGS_((ClientData clientData,
75 XEvent *eventPtr));
76 static void SelTimeoutProc _ANSI_ARGS_((ClientData clientData));
77 static void FreeHandler _ANSI_ARGS_((ClientData clientData));
78 static int HandleCompat _ANSI_ARGS_((ClientData clientData,
79 int offset, long *buffer, int maxBytes,
80 Atom type, Tk_Window tkwin));
81 static int CompatXSelProc _ANSI_ARGS_((ClientData clientData,
82 Tcl_Interp *interp, long *portion,
83 int numItems, int format,
84 Atom type, Tk_Window tkwin));
85 static void FreeLost _ANSI_ARGS_((ClientData clientData));
86
87 typedef struct CompatHandler
88 {
89 Tk_SelectionProc *proc; /* Procedure to invoke to convert
90 * selection to type "target". */
91 ClientData clientData; /* Value to pass to proc. */
92 } CompatHandler;
93
94 /*
95 *--------------------------------------------------------------
96 *
97 * Tk_CreateSelHandler --
98 *
99 * This procedure is called to register a procedure
100 * as the handler for selection requests of a particular
101 * target type on a particular window for a particular
102 * selection.
103 *
104 * Results:
105 * None.
106 *
107 * Side effects:
108 * In the future, whenever the selection is in tkwin's
109 * window and someone requests the selection in the
110 * form given by target, proc will be invoked to provide
111 * part or all of the selection in the given form. If
112 * there was already a handler declared for the given
113 * window, target and selection type, then it is replaced.
114 * Proc should have the following form:
115 *
116 * int
117 * proc(clientData, offset, buffer, maxBytes)
118 * ClientData clientData;
119 * int offset;
120 * char *buffer;
121 * int maxBytes;
122 * {
123 * }
124 *
125 * The clientData argument to proc will be the same as
126 * the clientData argument to this procedure. The offset
127 * argument indicates which portion of the selection to
128 * return: skip the first offset bytes. Buffer is a
129 * pointer to an area in which to place the converted
130 * selection, and maxBytes gives the number of bytes
131 * available at buffer. Proc should place the selection
132 * in buffer as a string, and return a count of the number
133 * of bytes of selection actually placed in buffer (not
134 * including the terminating NULL character). If the
135 * return value equals maxBytes, this is a sign that there
136 * is probably still more selection information available.
137 *
138 *--------------------------------------------------------------
139 */
140
141 void
142 Tk_CreateXSelHandler(tkwin, selection, target, proc, clientData, format)
143 Tk_Window tkwin; /* Token for window. */
144 Atom selection; /* Selection to be handled. */
145 Atom target; /* The kind of selection conversions
146 * that can be handled by proc,
147 * e.g. TARGETS or STRING. */
148 Tk_XSelectionProc *proc; /* Procedure to invoke to convert
149 * selection to type "target". */
150 ClientData clientData; /* Value to pass to proc. */
151 Atom format; /* Format in which the selection
152 * information should be returned to
153 * the requestor. XA_STRING is best by
154 * far, but anything listed in the ICCCM
155 * will be tolerated (blech). */
156 {
157 register TkSelHandler *selPtr;
158 TkWindow *winPtr = (TkWindow *) tkwin;
159 TkDisplay *dispPtr = winPtr->dispPtr;
160
161 if (winPtr->dispPtr->multipleAtom == None) {
162 TkSelInit(tkwin);
163 }
164
165 /*
166 * See if there's already a handler for this target and selection on
167 * this window. If so, re-use it. If not, create a new one.
168 */
169
170 for (selPtr = winPtr->selHandlerList; ; selPtr = selPtr->nextPtr) {
171 if (selPtr == NULL) {
172 selPtr = (TkSelHandler *) ckalloc(sizeof(TkSelHandler));
173 selPtr->nextPtr = winPtr->selHandlerList;
174 winPtr->selHandlerList = selPtr;
175 break;
176 }
177 if ((selPtr->selection == selection) && (selPtr->target == target)) {
178
179 /*
180 * Special case: when replacing handler created by
181 * "selection handle", free up memory. Should there be a
182 * callback to allow other clients to do this too?
183 */
184
185 if (selPtr->proc == HandleCompat) {
186 FreeHandler(selPtr->clientData);
187 }
188 break;
189 }
190 }
191 selPtr->selection = selection;
192 selPtr->target = target;
193 selPtr->format = format;
194 selPtr->proc = proc;
195 selPtr->clientData = clientData;
196 if ((target == XA_STRING)
197 || (target==dispPtr->utf8Atom)
198 || (target==dispPtr->textAtom)
199 || (target==dispPtr->compoundTextAtom)) {
200 selPtr->size = 8;
201 } else {
202 selPtr->size = 32;
203 }
204
205 if ((target == XA_STRING) && (winPtr->dispPtr->utf8Atom != (Atom) NULL)) {
206 /*
207 * If the user asked for a STRING handler and we understand
208 * UTF8_STRING, we implicitly create a UTF8_STRING handler for them.
209 */
210
211 target = winPtr->dispPtr->utf8Atom;
212 for (selPtr = winPtr->selHandlerList; ;
213 selPtr = selPtr->nextPtr) {
214 if (selPtr == NULL) {
215 selPtr = (TkSelHandler *) ckalloc(sizeof(TkSelHandler));
216 selPtr->nextPtr = winPtr->selHandlerList;
217 winPtr->selHandlerList = selPtr;
218 selPtr->selection = selection;
219 selPtr->target = target;
220 selPtr->format = target; /* We want UTF8_STRING format */
221 selPtr->proc = proc;
222 #ifndef _LANG
223 if (selPtr->proc == HandleTclCommand) {
224 /*
225 * The clientData is selection controlled memory, so
226 * we should make a copy for this selPtr.
227 */
228 unsigned cmdInfoLen = sizeof(CommandInfo) +
229 ((CommandInfo*)clientData)->cmdLength - 3;
230 selPtr->clientData = (ClientData)ckalloc(cmdInfoLen);
231 memcpy(selPtr->clientData, clientData, cmdInfoLen);
232 }
233 #else
234 if (selPtr->proc == HandleCompat) {
235 CompatHandler *cd = (CompatHandler *) ckalloc(sizeof(CompatHandler));
236 *cd = *((CompatHandler *) clientData);
237 if (cd->proc == HandleTclCommand) {
238 CommandInfo *src = (CommandInfo *) cd->clientData;
239 CommandInfo *dst = (CommandInfo *) ckalloc(sizeof(*src));
240 *dst = *src;
241 cd->clientData = (ClientData) dst;
242 dst->command = LangCopyCallback(src->command);
243 }
244 selPtr->clientData = (ClientData) cd;
245 }
246 #endif
247 else {
248 selPtr->clientData = clientData;
249 }
250 selPtr->size = 8;
251 break;
252 }
253 if ((selPtr->selection == selection)
254 && (selPtr->target == target)) {
255 /*
256 * Looks like we had a utf-8 target already. Leave it alone.
257 */
258
259 break;
260 }
261 }
262 }
263 }
264
265
266 static int
267 HandleCompat(clientData, offset, Xbuffer, maxBytes, type, tkwin)
268 ClientData clientData;
269 int offset;
270 long *Xbuffer;
271 int maxBytes;
272 Atom type;
273 Tk_Window tkwin;
274 {
275 CompatHandler *cd = (CompatHandler *) clientData;
276
277 if (type == XA_STRING) {
278 return (*cd->proc)(cd->clientData, offset, (char *) Xbuffer, maxBytes);
279 }
280 else {
281 TkWindow *winPtr = (TkWindow *) tkwin;
282 if (winPtr && winPtr->dispPtr->utf8Atom != None && type == winPtr->dispPtr->utf8Atom) {
283 return (*cd->proc)(cd->clientData, offset, (char *) Xbuffer, maxBytes);
284 }
285 else {
286 #ifdef WIN32
287 return -1;
288 #else
289 char buffer[TK_SEL_BYTES_AT_ONCE];
290 int count = (*cd->proc)(cd->clientData, offset, buffer, maxBytes);
291 buffer[count] = '\0';
292 return TkSelCvtToX(Xbuffer, buffer, type, tkwin, maxBytes);
293 #endif
294 }
295 }
296 }
297
298
299 void
300 Tk_CreateSelHandler(tkwin, selection, target, proc, clientData, format)
301 Tk_Window tkwin; /* Token for window. */
302 Atom selection; /* Selection to be handled. */
303 Atom target; /* The kind of selection conversions
304 * that can be handled by proc,
305 * e.g. TARGETS or STRING. */
306 Tk_SelectionProc *proc; /* Procedure to invoke to convert
307 * selection to type "target". */
308 ClientData clientData; /* Value to pass to proc. */
309 Atom format; /* Format in which the selection
310 * information should be returned to
311 * the requestor. XA_STRING is best by
312 * far, but anything listed in the ICCCM
313 * will be tolerated (blech). */
314 {
315 CompatHandler *cd = (CompatHandler *) ckalloc(sizeof(CompatHandler));
316 cd->clientData = clientData;
317 cd->proc = proc;
318 Tk_CreateXSelHandler(tkwin, selection, target, HandleCompat,
319 (ClientData) cd, format);
320 }
321
322 /*
323 *----------------------------------------------------------------------
324 *
325 * Tk_DeleteSelHandler --
326 *
327 * Remove the selection handler for a given window, target, and
328 * selection, if it exists.
329 *
330 * Results:
331 * None.
332 *
333 * Side effects:
334 * The selection handler for tkwin and target is removed. If there
335 * is no such handler then nothing happens.
336 *
337 *----------------------------------------------------------------------
338 */
339
340 void
341 Tk_DeleteSelHandler(tkwin, selection, target)
342 Tk_Window tkwin; /* Token for window. */
343 Atom selection; /* The selection whose handler
344 * is to be removed. */
345 Atom target; /* The target whose selection
346 * handler is to be removed. */
347 {
348 TkWindow *winPtr = (TkWindow *) tkwin;
349 register TkSelHandler *selPtr, *prevPtr;
350 register TkSelInProgress *ipPtr;
351 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
352 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
353
354 /*
355 * Find the selection handler to be deleted, or return if it doesn't
356 * exist.
357 */
358
359 for (selPtr = winPtr->selHandlerList, prevPtr = NULL; ;
360 prevPtr = selPtr, selPtr = selPtr->nextPtr) {
361 if (selPtr == NULL) {
362 return;
363 }
364 if ((selPtr->selection == selection) && (selPtr->target == target)) {
365 break;
366 }
367 }
368
369 /*
370 * If ConvertSelection is processing this handler, tell it that the
371 * handler is dead.
372 */
373
374 for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL;
375 ipPtr = ipPtr->nextPtr) {
376 if (ipPtr->selPtr == selPtr) {
377 ipPtr->selPtr = NULL;
378 }
379 }
380
381 /*
382 * Free resources associated with the handler.
383 */
384
385 if (prevPtr == NULL) {
386 winPtr->selHandlerList = selPtr->nextPtr;
387 } else {
388 prevPtr->nextPtr = selPtr->nextPtr;
389 }
390
391 if ((target == XA_STRING) && (winPtr->dispPtr->utf8Atom != None)) {
392 /*
393 * If the user asked for a STRING handler and we understand
394 * UTF8_STRING, we may have implicitly created a UTF8_STRING handler
395 * for them. Look for it and delete it as necessary.
396 */
397 TkSelHandler *utf8selPtr;
398
399 target = winPtr->dispPtr->utf8Atom;
400 for (utf8selPtr = winPtr->selHandlerList; utf8selPtr != NULL;
401 utf8selPtr = utf8selPtr->nextPtr) {
402 if ((utf8selPtr->selection == selection)
403 && (utf8selPtr->target == target)) {
404 break;
405 }
406 }
407 if (utf8selPtr != NULL) {
408 if ((utf8selPtr->format == target)
409 && (utf8selPtr->proc == selPtr->proc)
410 && (utf8selPtr->size == selPtr->size)) {
411 /*
412 * This recursive call is OK, because we've
413 * changed the value of 'target'
414 */
415 Tk_DeleteSelHandler(tkwin, selection, target);
416 }
417 }
418 }
419
420 #ifndef _LANG
421 if (selPtr->proc == HandleTclCommand) {
422 /*
423 * Mark the CommandInfo as deleted and free it if we can.
424 */
425
426 ((CommandInfo*)selPtr->clientData)->interp = NULL;
427 Tcl_EventuallyFree(selPtr->clientData, TCL_DYNAMIC);
428 }
429 #else
430 if (selPtr->proc == HandleCompat) {
431 FreeHandler(selPtr->clientData);
432 }
433 #endif
434 ckfree((char *) selPtr);
435 }
436
437 /*
438 *--------------------------------------------------------------
439 *
440 * Tk_OwnSelection --
441 *
442 * Arrange for tkwin to become the owner of a selection.
443 *
444 * Results:
445 * None.
446 *
447 * Side effects:
448 * From now on, requests for the selection will be directed
449 * to procedures associated with tkwin (they must have been
450 * declared with calls to Tk_CreateSelHandler). When the
451 * selection is lost by this window, proc will be invoked
452 * (see the manual entry for details). This procedure may
453 * invoke callbacks, including Tcl scripts, so any calling
454 * function should be reentrant at the point where
455 * Tk_OwnSelection is invoked.
456 *
457 *--------------------------------------------------------------
458 */
459
460 void
461 Tk_OwnSelection(tkwin, selection, proc, clientData)
462 Tk_Window tkwin; /* Window to become new selection
463 * owner. */
464 Atom selection; /* Selection that window should own. */
465 Tk_LostSelProc *proc; /* Procedure to call when selection
466 * is taken away from tkwin. */
467 ClientData clientData; /* Arbitrary one-word argument to
468 * pass to proc. */
469 {
470 register TkWindow *winPtr = (TkWindow *) tkwin;
471 TkDisplay *dispPtr = winPtr->dispPtr;
472 TkSelectionInfo *infoPtr;
473 Tk_LostSelProc *clearProc = NULL;
474 ClientData clearData = NULL; /* Initialization needed only to
475 * prevent compiler warning. */
476
477
478 if (dispPtr->multipleAtom == None) {
479 TkSelInit(tkwin);
480 }
481 Tk_MakeWindowExist(tkwin);
482
483 /*
484 * This code is somewhat tricky. First, we find the specified selection
485 * on the selection list. If the previous owner is in this process, and
486 * is a different window, then we need to invoke the clearProc. However,
487 * it's dangerous to call the clearProc right now, because it could
488 * invoke a Tcl script that wrecks the current state (e.g. it could
489 * delete the window). To be safe, defer the call until the end of the
490 * procedure when we no longer care about the state.
491 */
492
493 for (infoPtr = dispPtr->selectionInfoPtr; infoPtr != NULL;
494 infoPtr = infoPtr->nextPtr) {
495 if (infoPtr->selection == selection) {
496 break;
497 }
498 }
499 if (infoPtr == NULL) {
500 infoPtr = (TkSelectionInfo*) ckalloc(sizeof(TkSelectionInfo));
501 infoPtr->selection = selection;
502 infoPtr->nextPtr = dispPtr->selectionInfoPtr;
503 dispPtr->selectionInfoPtr = infoPtr;
504 } else if (infoPtr->clearProc != NULL) {
505 if (infoPtr->owner != tkwin) {
506 clearProc = infoPtr->clearProc;
507 clearData = infoPtr->clearData;
508 } else if (infoPtr->clearProc == LostSelection) {
509 /*
510 * If the selection handler is one created by "selection own",
511 * be sure to free the record for it; otherwise there will be
512 * a memory leak.
513 */
514
515 FreeLost(infoPtr->clearData);
516 }
517 }
518
519 infoPtr->owner = tkwin;
520 infoPtr->serial = NextRequest(winPtr->display);
521 infoPtr->clearProc = proc;
522 infoPtr->clearData = clientData;
523
524 /*
525 * Tcl/Tk says:
526 * Note that we are using CurrentTime, even though ICCCM recommends against
527 * this practice (the problem is that we don't necessarily have a valid
528 * time to use). We will not be able to retrieve a useful timestamp for
529 * the TIMESTAMP target later.
530 *
531 * NI-S Believes that X ICCCM rules say we should use an event time
532 * if we have one, if we don't have a valid time then TkCurrentTime()
533 * will return CurrentTime anyway.
534 */
535
536 infoPtr->time = TkCurrentTime(dispPtr,True);
537
538 /*
539 * Note that we are not checking to see if the selection claim succeeded.
540 * If the ownership does not change, then the clearProc may never be
541 * invoked, and we will return incorrect information when queried for the
542 * current selection owner.
543 */
544
545 XSetSelectionOwner(winPtr->display, infoPtr->selection, winPtr->window,
546 infoPtr->time);
547
548 /*
549 * Now that we are done, we can invoke clearProc without running into
550 * reentrancy problems.
551 */
552
553 if (clearProc != NULL) {
554 (*clearProc)(clearData);
555 }
556 }
557
558 /*
559 *----------------------------------------------------------------------
560 *
561 * Tk_ClearSelection --
562 *
563 * Eliminate the specified selection on tkwin's display, if there is one.
564 *
565 * Results:
566 * None.
567 *
568 * Side effects:
569 * The specified selection is cleared, so that future requests to retrieve
570 * it will fail until some application owns it again. This procedure
571 * invokes callbacks, possibly including Tcl scripts, so any calling
572 * function should be reentrant at the point Tk_ClearSelection is invoked.
573 *
574 *----------------------------------------------------------------------
575 */
576
577 void
578 Tk_ClearSelection(tkwin, selection)
579 Tk_Window tkwin; /* Window that selects a display. */
580 Atom selection; /* Selection to be cancelled. */
581 {
582 register TkWindow *winPtr = (TkWindow *) tkwin;
583 TkDisplay *dispPtr = winPtr->dispPtr;
584 TkSelectionInfo *infoPtr;
585 TkSelectionInfo *prevPtr;
586 TkSelectionInfo *nextPtr;
587 Tk_LostSelProc *clearProc = NULL;
588 ClientData clearData = NULL; /* Initialization needed only to
589 * prevent compiler warning. */
590
591 if (dispPtr->multipleAtom == None) {
592 TkSelInit(tkwin);
593 }
594
595 for (infoPtr = dispPtr->selectionInfoPtr, prevPtr = NULL;
596 infoPtr != NULL; infoPtr = nextPtr) {
597 nextPtr = infoPtr->nextPtr;
598 if (infoPtr->selection == selection) {
599 if (prevPtr == NULL) {
600 dispPtr->selectionInfoPtr = nextPtr;
601 } else {
602 prevPtr->nextPtr = nextPtr;
603 }
604 break;
605 }
606 prevPtr = infoPtr;
607 }
608
609 if (infoPtr != NULL) {
610 clearProc = infoPtr->clearProc;
611 clearData = infoPtr->clearData;
612 ckfree((char *) infoPtr);
613 }
614 XSetSelectionOwner(winPtr->display, selection, None,
615 /* Fallback to CurrentTime */
616 TkCurrentTime(dispPtr,True));
617
618 if (clearProc != NULL) {
619 (*clearProc)(clearData);
620 }
621 }
622
623 /*
624 *--------------------------------------------------------------
625 *
626 * Tk_GetSelection --
627 *
628 * Retrieve the value of a selection and pass it off (in
629 * pieces, possibly) to a given procedure.
630 *
631 * Results:
632 * The return value is a standard Tcl return value.
633 * If an error occurs (such as no selection exists)
634 * then an error message is left in the interp's result.
635 *
636 * Side effects:
637 * The standard X11 protocols are used to retrieve the
638 * selection. When it arrives, it is passed to proc. If
639 * the selection is very large, it will be passed to proc
640 * in several pieces. Proc should have the following
641 * structure:
642 *
643 * int
644 * proc(clientData, interp, portion)
645 * ClientData clientData;
646 * Tcl_Interp *interp;
647 * char *portion;
648 * {
649 * }
650 *
651 * The interp and clientData arguments to proc will be the
652 * same as the corresponding arguments to Tk_GetSelection.
653 * The portion argument points to a character string
654 * containing part of the selection, and numBytes indicates
655 * the length of the portion, not including the terminating
656 * NULL character. If the selection arrives in several pieces,
657 * the "portion" arguments in separate calls will contain
658 * successive parts of the selection. Proc should normally
659 * return TCL_OK. If it detects an error then it should return
660 * TCL_ERROR and leave an error message in the interp's result; the
661 * remainder of the selection retrieval will be aborted.
662 *
663 *--------------------------------------------------------------
664 */
665
666 int
667 Tk_GetXSelection(interp, tkwin, selection, target, proc, clientData)
668 Tcl_Interp *interp; /* Interpreter to use for reporting
669 * errors. */
670 Tk_Window tkwin; /* Window on whose behalf to retrieve
671 * the selection (determines display
672 * from which to retrieve). */
673 Atom selection; /* Selection to retrieve. */
674 Atom target; /* Desired form in which selection
675 * is to be returned. */
676 Tk_GetXSelProc *proc; /* Procedure to call to process the
677 * selection, once it has been retrieved. */
678 ClientData clientData; /* Arbitrary value to pass to proc. */
679 {
680 TkWindow *winPtr = (TkWindow *) tkwin;
681 TkDisplay *dispPtr = winPtr->dispPtr;
682 TkSelectionInfo *infoPtr;
683 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
684 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
685
686 if (dispPtr->multipleAtom == None) {
687 TkSelInit(tkwin);
688 }
689
690 /*
691 * If the selection is owned by a window managed by this
692 * process, then call the retrieval procedure directly,
693 * rather than going through the X server (it's dangerous
694 * to go through the X server in this case because it could
695 * result in deadlock if an INCR-style selection results).
696 */
697
698 for (infoPtr = dispPtr->selectionInfoPtr; infoPtr != NULL;
699 infoPtr = infoPtr->nextPtr) {
700 if (infoPtr->selection == selection)
701 break;
702 }
703 if (infoPtr != NULL) {
704 register TkSelHandler *selPtr;
705 int offset, result, count;
706 long buffer[TK_SEL_WORDS_AT_ONCE+1];
707 TkSelInProgress ip;
708
709 for (selPtr = ((TkWindow *) infoPtr->owner)->selHandlerList;
710 selPtr != NULL; selPtr = selPtr->nextPtr) {
711 if ((selPtr->target == target)
712 && (selPtr->selection == selection)) {
713 break;
714 }
715 }
716 if (selPtr == NULL) {
717 Atom type = XA_STRING;
718 int format = 8;
719
720 count = TkSelDefaultSelection(infoPtr, target, buffer,
721 TK_SEL_BYTES_AT_ONCE, &type, &format);
722 if (count > TK_SEL_BYTES_AT_ONCE) {
723 panic("selection handler returned too many bytes");
724 }
725 if (count < 0) {
726 goto cantget;
727 }
728 result = (*proc)(clientData, interp, buffer, count, format, type, tkwin);
729 } else {
730 Atom type = selPtr->format;
731 int format = 32;
732 if ((type == XA_STRING)
733 || (type==dispPtr->utf8Atom)
734 || (type==dispPtr->textAtom)
735 || (type==dispPtr->compoundTextAtom)) {
736 format = 8;
737 }
738 offset = 0;
739 result = TCL_OK;
740 ip.selPtr = selPtr;
741 ip.nextPtr = tsdPtr->pendingPtr;
742 tsdPtr->pendingPtr = &ip;
743 while (1) {
744 count = (selPtr->proc)(selPtr->clientData, offset, buffer,
745 TK_SEL_BYTES_AT_ONCE, type, tkwin);
746 if ((count < 0) || (ip.selPtr == NULL)) {
747 tsdPtr->pendingPtr = ip.nextPtr;
748 goto cantget;
749 }
750 if (count > TK_SEL_BYTES_AT_ONCE) {
751 panic("selection handler returned too many bytes");
752 }
753 ((char *) buffer)[count] = '\0';
754 result = (*proc)(clientData, interp, buffer, count, format, type, tkwin);
755 if ((result != TCL_OK) || (count < TK_SEL_BYTES_AT_ONCE)
756 || (ip.selPtr == NULL)) {
757 break;
758 }
759 offset += count;
760 }
761 tsdPtr->pendingPtr = ip.nextPtr;
762 }
763 return result;
764 }
765
766 /*
767 * The selection is owned by some other process.
768 */
769
770 return TkSelGetSelection(interp, tkwin, selection, target, proc,
771 clientData);
772
773 cantget:
774 Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection),
775 " selection doesn't exist or form \"", Tk_GetAtomName(tkwin, target),
776 "\" not defined", (char *) NULL);
777 return TCL_ERROR;
778
779 }
780
781
782 typedef struct CompatInfo
783 {
784 Tk_GetSelProc *proc; /* Procedure to call to process the
785 * selection, once it has been retrieved. */
786 ClientData clientData; /* Arbitrary value to pass to proc. */
787 } CompatInfo;
788
789 static int
790 CompatXSelProc(clientData,interp,portion,numItems,format,type,tkwin)
791 ClientData clientData;
792 Tcl_Interp *interp;
793 long *portion;
794 int numItems;
795 int format;
796 Atom type;
797 Tk_Window tkwin;
798 {CompatInfo *info = (CompatInfo *) clientData;
799 TkWindow *winPtr = (TkWindow *) tkwin;
800 TkDisplay *dispPtr = winPtr->dispPtr;
801 if ((type == XA_STRING)
802 || (type==dispPtr->utf8Atom)
803 || (type==dispPtr->textAtom)
804 || (type==dispPtr->compoundTextAtom)) {
805 if (format != 8) {
806 sprintf(interp->result,
807 "bad format for string selection: wanted \"8\", got \"%d\"",
808 format);
809 return TCL_ERROR;
810 }
811 portion[numItems] = '\0';
812 return (*info->proc)(info->clientData, interp, (char *) portion);
813 } else {
814 #ifdef WIN32
815 return TCL_ERROR;
816 #else
817 char *string;
818 int result;
819 if (format != 32) {
820 sprintf(interp->result,
821 "bad format for selection: wanted \"32\", got \"%d\"",
822 format);
823 return TCL_ERROR;
824 }
825 string = TkSelCvtFromX(portion, (int) numItems, type, tkwin);
826 result = (*info->proc)(info->clientData, interp, string);
827 ckfree(string);
828 return result;
829 #endif
830 }
831 }
832
833 int
834 Tk_GetSelection(interp, tkwin, selection, target, proc, clientData)
835 Tcl_Interp *interp; /* Interpreter to use for reporting
836 * errors. */
837 Tk_Window tkwin; /* Window on whose behalf to retrieve
838 * the selection (determines display
839 * from which to retrieve). */
840 Atom selection; /* Selection to retrieve. */
841 Atom target; /* Desired form in which selection
842 * is to be returned. */
843 Tk_GetSelProc *proc; /* Procedure to call to process the
844 * selection, once it has been retrieved. */
845 ClientData clientData; /* Arbitrary value to pass to proc. */
846 {
847 CompatInfo cd;
848 cd.clientData = clientData;
849 cd.proc = proc;
850 return Tk_GetXSelection(interp, tkwin, selection, target, CompatXSelProc, (ClientData) &cd);
851 }
852
853 static void
854 FreeHandler(clientData)
855 ClientData clientData;
856 {
857 CompatHandler *cd = (CompatHandler *) clientData;
858 if (cd->proc == HandleTclCommand)
859 {
860 CommandInfo *cmdInfoPtr = (CommandInfo *) cd->clientData;
861 cmdInfoPtr->interp = NULL;
862 LangFreeCallback(cmdInfoPtr->command);
863 ckfree((char *) cmdInfoPtr);
864 }
865 ckfree((char *) cd);
866 }
867
868 /*
869 *--------------------------------------------------------------
870 *
871 * Tk_SelectionObjCmd --
872 *
873 * This procedure is invoked to process the "selection" Tcl
874 * command. See the user documentation for details on what
875 * it does.
876 *
877 * Results:
878 * A standard Tcl result.
879 *
880 * Side effects:
881 * See the user documentation.
882 *
883 *--------------------------------------------------------------
884 */
885
886 int
887 Tk_SelectionObjCmd(clientData, interp, objc, objv)
888 ClientData clientData; /* Main window associated with
889 * interpreter. */
890 Tcl_Interp *interp; /* Current interpreter. */
891 int objc; /* Number of arguments. */
892 Tcl_Obj *CONST objv[]; /* Argument objects. */
893 {
894 Tk_Window tkwin = (Tk_Window) clientData;
895 char *path = NULL;
896 Atom selection;
897 char *selName = NULL, *string;
898 int count, index;
899 Tcl_Obj **objs;
900 static CONST char *optionStrings[] = {
901 "clear", "exists", "get", "handle", "own", (char *) NULL
902 };
903 enum options { SELECTION_CLEAR, SELECTION_EXISTS, SELECTION_GET, SELECTION_HANDLE,
904 SELECTION_OWN };
905
906 if (objc < 2) {
907 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
908 return TCL_ERROR;
909 }
910
911 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
912 &index) != TCL_OK) {
913 return TCL_ERROR;
914 }
915
916 switch ((enum options) index) {
917 case SELECTION_CLEAR: {
918 static CONST char *clearOptionStrings[] = {
919 "-displayof", "-selection", (char *) NULL
920 };
921 enum clearOptions { CLEAR_DISPLAYOF, CLEAR_SELECTION };
922 int clearIndex;
923
924 for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
925 count-=2, objs+=2) {
926 string = Tcl_GetString(objs[0]);
927 if (string[0] != '-') {
928 break;
929 }
930 if (count < 2) {
931 Tcl_AppendResult(interp, "value for \"", string,
932 "\" missing", (char *) NULL);
933 return TCL_ERROR;
934 }
935
936 if (Tcl_GetIndexFromObj(interp, objs[0], clearOptionStrings,
937 "option", 0, &clearIndex) != TCL_OK) {
938 return TCL_ERROR;
939 }
940 switch ((enum clearOptions) clearIndex) {
941 case CLEAR_DISPLAYOF:
942 path = Tcl_GetString(objs[1]);
943 break;
944 case CLEAR_SELECTION:
945 selName = Tcl_GetString(objs[1]);
946 break;
947 }
948 }
949 if (count == 1) {
950 path = Tcl_GetString(objs[0]);
951 } else if (count > 1) {
952 Tcl_WrongNumArgs(interp, 2, objv, "?options?");
953 return TCL_ERROR;
954 }
955 if (path != NULL) {
956 tkwin = Tk_NameToWindow(interp, path, tkwin);
957 }
958 if (tkwin == NULL) {
959 return TCL_ERROR;
960 }
961 if (selName != NULL) {
962 selection = Tk_InternAtom(tkwin, selName);
963 } else {
964 selection = XA_PRIMARY;
965 }
966
967 Tk_ClearSelection(tkwin, selection);
968 break;
969 }
970
971 case SELECTION_EXISTS: {
972 Tcl_DString selBytes;
973 int result;
974 Window win;
975 static CONST char *getOptionStrings[] = {
976 "-displayof", "-selection", (char *) NULL
977 };
978 enum getOptions { GET_DISPLAYOF, GET_SELECTION };
979 int getIndex;
980
981 for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count>0;
982 count-=2, objs+=2) {
983 string = Tcl_GetString(objs[0]);
984 if (string[0] != '-') {
985 break;
986 }
987 if (count < 2) {
988 Tcl_AppendResult(interp, "value for \"", string,
989 "\" missing", (char *) NULL);
990 return TCL_ERROR;
991 }
992
993 if (Tcl_GetIndexFromObj(interp, objs[0], getOptionStrings,
994 "option", 0, &getIndex) != TCL_OK) {
995 return TCL_ERROR;
996 }
997
998 switch ((enum getOptions) getIndex) {
999 case GET_DISPLAYOF:
1000 path = Tcl_GetString(objs[1]);
1001 break;
1002 case GET_SELECTION:
1003 selName = Tcl_GetString(objs[1]);
1004 break;
1005 }
1006 }
1007 if (path != NULL) {
1008 tkwin = Tk_NameToWindow(interp, path, tkwin);
1009 }
1010 if (tkwin == NULL) {
1011 return TCL_ERROR;
1012 }
1013 if (selName != NULL) {
1014 selection = Tk_InternAtom(tkwin, selName);
1015 } else {
1016 selection = XA_PRIMARY;
1017 }
1018 if (count > 0) {
1019 Tcl_WrongNumArgs(interp, 2, objv, "?options?");
1020 return TCL_ERROR;
1021 }
1022
1023 win = XGetSelectionOwner(Tk_Display(tkwin), selection);
1024 if (win != None) {
1025 TkWindow *winPtr = (TkWindow *) tkwin;
1026 tkwin = Tk_IdToWindow(Tk_Display(tkwin), win);
1027 if (tkwin != NULL && tkwin != winPtr->dispPtr->clipWindow) {
1028 Tcl_SetObjResult(interp,LangWidgetObj(interp,tkwin));
1029 } else {
1030 Tcl_SetObjResult(interp,Tcl_NewIntObj(win));
1031 }
1032 }
1033 return TCL_OK;
1034 }
1035
1036 case SELECTION_GET: {
1037 Atom target;
1038 char *targetName = NULL;
1039 Tcl_DString selBytes;
1040 int result;
1041 static CONST char *getOptionStrings[] = {
1042 "-displayof", "-selection", "-type", (char *) NULL
1043 };
1044 enum getOptions { GET_DISPLAYOF, GET_SELECTION, GET_TYPE };
1045 int getIndex;
1046
1047 for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count>0;
1048 count-=2, objs+=2) {
1049 string = Tcl_GetString(objs[0]);
1050 if (string[0] != '-') {
1051 break;
1052 }
1053 if (count < 2) {
1054 Tcl_AppendResult(interp, "value for \"", string,
1055 "\" missing", (char *) NULL);
1056 return TCL_ERROR;
1057 }
1058
1059 if (Tcl_GetIndexFromObj(interp, objs[0], getOptionStrings,
1060 "option", 0, &getIndex) != TCL_OK) {
1061 return TCL_ERROR;
1062 }
1063
1064 switch ((enum getOptions) getIndex) {
1065 case GET_DISPLAYOF:
1066 path = Tcl_GetString(objs[1]);
1067 break;
1068 case GET_SELECTION:
1069 selName = Tcl_GetString(objs[1]);
1070 break;
1071 case GET_TYPE:
1072 targetName = Tcl_GetString(objs[1]);
1073 break;
1074 }
1075 }
1076 if (path != NULL) {
1077 tkwin = Tk_NameToWindow(interp, path, tkwin);
1078 }
1079 if (tkwin == NULL) {
1080 return TCL_ERROR;
1081 }
1082 if (selName != NULL) {
1083 selection = Tk_InternAtom(tkwin, selName);
1084 } else {
1085 selection = XA_PRIMARY;
1086 }
1087 if (count > 1) {
1088 Tcl_WrongNumArgs(interp, 2, objv, "?options?");
1089 return TCL_ERROR;
1090 } else if (count == 1) {
1091 target = Tk_InternAtom(tkwin, Tcl_GetString(objs[0]));
1092 } else if (targetName != NULL) {
1093 target = Tk_InternAtom(tkwin, targetName);
1094 } else {
1095 target = XA_STRING;
1096 }
1097
1098 Tcl_DStringInit(&selBytes);
1099 result = Tk_GetSelection(interp, tkwin, selection, target,
1100 SelGetProc, (ClientData) &selBytes);
1101 if (result == TCL_OK) {
1102 Tcl_DStringResult(interp, &selBytes);
1103 } else {
1104 Tcl_DStringFree(&selBytes);
1105 }
1106 return result;
1107 }
1108
1109 case SELECTION_HANDLE: {
1110 Atom target, format;
1111 char *targetName = NULL;
1112 char *formatName = NULL;
1113 register CommandInfo *cmdInfoPtr;
1114 int cmdLength;
1115 static CONST char *handleOptionStrings[] = {
1116 "-format", "-selection", "-type", (char *) NULL
1117 };
1118 enum handleOptions { HANDLE_FORMAT, HANDLE_SELECTION,
1119 HANDLE_TYPE };
1120 int handleIndex;
1121
1122 for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
1123 count-=2, objs+=2) {
1124 string = Tcl_GetString(objs[0]);
1125 if (string[0] != '-') {
1126 break;
1127 }
1128 if (count < 2) {
1129 Tcl_AppendResult(interp, "value for \"", string,
1130 "\" missing", (char *) NULL);
1131 return TCL_ERROR;
1132 }
1133
1134 if (Tcl_GetIndexFromObj(interp, objs[0],handleOptionStrings,
1135 "option", 0, &handleIndex) != TCL_OK) {
1136 return TCL_ERROR;
1137 }
1138
1139 switch ((enum handleOptions) handleIndex) {
1140 case HANDLE_FORMAT:
1141 formatName = Tcl_GetString(objs[1]);
1142 break;
1143 case HANDLE_SELECTION:
1144 selName = Tcl_GetString(objs[1]);
1145 break;
1146 case HANDLE_TYPE:
1147 targetName = Tcl_GetString(objs[1]);
1148 break;
1149 }
1150 }
1151
1152 if ((count < 2) || (count > 4)) {
1153 Tcl_WrongNumArgs(interp, 2, objv, "?options? window command");
1154 return TCL_ERROR;
1155 }
1156 tkwin = Tk_NameToWindow(interp, Tcl_GetString(objs[0]), tkwin);
1157 if (tkwin == NULL) {
1158 return TCL_ERROR;
1159 }
1160 if (selName != NULL) {
1161 selection = Tk_InternAtom(tkwin, selName);
1162 } else {
1163 selection = XA_PRIMARY;
1164 }
1165
1166 if (count > 2) {
1167 target = Tk_InternAtom(tkwin, Tcl_GetString(objs[2]));
1168 } else if (targetName != NULL) {
1169 target = Tk_InternAtom(tkwin, targetName);
1170 } else {
1171 target = XA_STRING;
1172 }
1173 if (count > 3) {
1174 format = Tk_InternAtom(tkwin, Tcl_GetString(objs[3]));
1175 } else if (formatName != NULL) {
1176 format = Tk_InternAtom(tkwin, formatName);
1177 } else {
1178 format = XA_STRING;
1179 }
1180 string = Tcl_GetStringFromObj(objs[1], &cmdLength);
1181 if (cmdLength == 0) {
1182 Tk_DeleteSelHandler(tkwin, selection, target);
1183 } else {
1184 cmdInfoPtr = (CommandInfo *) ckalloc((unsigned) (
1185 sizeof(CommandInfo) - 3 + cmdLength));
1186 cmdInfoPtr->interp = interp;
1187 cmdInfoPtr->charOffset = 0;
1188 cmdInfoPtr->byteOffset = 0;
1189 cmdInfoPtr->buffer[0] = '\0';
1190 cmdInfoPtr->cmdLength = cmdLength;
1191 cmdInfoPtr->command = LangMakeCallback(objs[1]);
1192 Tk_CreateSelHandler(tkwin, selection, target, HandleTclCommand,
1193 (ClientData) cmdInfoPtr, format);
1194 }
1195 return TCL_OK;
1196 }
1197
1198 case SELECTION_OWN: {
1199 register LostCommand *lostPtr;
1200 Tcl_Obj *script = NULL;
1201 int cmdLength;
1202 static CONST char *ownOptionStrings[] = {
1203 "-command", "-displayof", "-selection", (char *) NULL
1204 };
1205 enum ownOptions { OWN_COMMAND, OWN_DISPLAYOF, OWN_SELECTION };
1206 int ownIndex;
1207
1208 for (count = objc-2, objs = ((Tcl_Obj **)objv)+2; count > 0;
1209 count-=2, objs+=2) {
1210 string = Tcl_GetString(objs[0]);
1211 if (string[0] != '-') {
1212 break;
1213 }
1214 if (count < 2) {
1215 Tcl_AppendResult(interp, "value for \"", string,
1216 "\" missing", (char *) NULL);
1217 return TCL_ERROR;
1218 }
1219
1220 if (Tcl_GetIndexFromObj(interp, objs[0], ownOptionStrings,
1221 "option", 0, &ownIndex) != TCL_OK) {
1222 return TCL_ERROR;
1223 }
1224
1225 switch ((enum ownOptions) ownIndex) {
1226 case OWN_COMMAND:
1227 script = objs[1];
1228 break;
1229 case OWN_DISPLAYOF:
1230 path = Tcl_GetString(objs[1]);
1231 break;
1232 case OWN_SELECTION:
1233 selName = Tcl_GetString(objs[1]);
1234 break;
1235 }
1236 }
1237
1238 if (count > 2) {
1239 Tcl_WrongNumArgs(interp, 2, objv, "?options? ?window?");
1240 return TCL_ERROR;
1241 }
1242 if (selName != NULL) {
1243 selection = Tk_InternAtom(tkwin, selName);
1244 } else {
1245 selection = XA_PRIMARY;
1246 }
1247 if (count == 0) {
1248 TkSelectionInfo *infoPtr;
1249 TkWindow *winPtr;
1250 if (path != NULL) {
1251 tkwin = Tk_NameToWindow(interp, path, tkwin);
1252 }
1253 if (tkwin == NULL) {
1254 return TCL_ERROR;
1255 }
1256 winPtr = (TkWindow *)tkwin;
1257 for (infoPtr = winPtr->dispPtr->selectionInfoPtr;
1258 infoPtr != NULL; infoPtr = infoPtr->nextPtr) {
1259 if (infoPtr->selection == selection)
1260 break;
1261 }
1262
1263 /*
1264 * Ignore the internal clipboard window.
1265 */
1266
1267 if ((infoPtr != NULL)
1268 && (infoPtr->owner != winPtr->dispPtr->clipWindow)) {
1269 Tcl_SetResult(interp, Tk_PathName(infoPtr->owner),
1270 TCL_STATIC);
1271 }
1272 return TCL_OK;
1273 }
1274 tkwin = Tk_NameToWindow(interp, Tcl_GetString(objs[0]), tkwin);
1275 if (tkwin == NULL) {
1276 return TCL_ERROR;
1277 }
1278 if (count == 2) {
1279 if (!LangNull(objs[1])) {
1280 script = objs[1];
1281 }
1282 }
1283 if (script == NULL) {
1284 Tk_OwnSelection(tkwin, selection, (Tk_LostSelProc *) NULL,
1285 (ClientData) NULL);
1286 return TCL_OK;
1287 }
1288 #ifndef _LANG
1289 cmdLength = strlen(Tcl_GetString(script));
1290 lostPtr = (LostCommand *) ckalloc((unsigned) (sizeof(LostCommand)
1291 -3 + cmdLength));
1292 strcpy(lostPtr->command, Tcl_GetString(script));
1293 #else
1294 lostPtr = (LostCommand *) ckalloc((unsigned) (sizeof(LostCommand)));
1295 lostPtr->command = LangMakeCallback(script);
1296 #endif
1297 lostPtr->interp = interp;
1298 Tk_OwnSelection(tkwin, selection, LostSelection,
1299 (ClientData) lostPtr);
1300 return TCL_OK;
1301 }
1302 }
1303 return TCL_OK;
1304 }
1305
1306 /*
1307 *----------------------------------------------------------------------
1308 *
1309 * TkSelGetInProgress --
1310 *
1311 * This procedure returns a pointer to the thread-local
1312 * list of pending searches.
1313 *
1314 * Results:
1315 * The return value is a pointer to the first search in progress,
1316 * or NULL if there are none.
1317 *
1318 * Side effects:
1319 * None.
1320 *
1321 *----------------------------------------------------------------------
1322 */
1323
1324 TkSelInProgress *
1325 TkSelGetInProgress _ANSI_ARGS_((void))
1326 {
1327 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1328 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1329
1330 return tsdPtr->pendingPtr;
1331 }
1332
1333 /*
1334 *----------------------------------------------------------------------
1335 *
1336 * TkSelSetInProgress --
1337 *
1338 * This procedure is used to set the thread-local list of pending
1339 * searches. It is required because the pending list is kept
1340 * in thread local storage.
1341 *
1342 * Results:
1343 * None.
1344 *
1345 * Side effects:
1346 * None.
1347 *
1348 *----------------------------------------------------------------------
1349 */
1350 void
1351 TkSelSetInProgress(pendingPtr)
1352 TkSelInProgress *pendingPtr;
1353 {
1354 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1355 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1356
1357 tsdPtr->pendingPtr = pendingPtr;
1358 }
1359
1360 /*
1361 *----------------------------------------------------------------------
1362 *
1363 * TkSelDeadWindow --
1364 *
1365 * This procedure is invoked just before a TkWindow is deleted.
1366 * It performs selection-related cleanup.
1367 *
1368 * Results:
1369 * None.
1370 *
1371 * Side effects:
1372 * Frees up memory associated with the selection.
1373 *
1374 *----------------------------------------------------------------------
1375 */
1376
1377 void
1378 TkSelDeadWindow(winPtr)
1379 register TkWindow *winPtr; /* Window that's being deleted. */
1380 {
1381 register TkSelHandler *selPtr;
1382 register TkSelInProgress *ipPtr;
1383 TkSelectionInfo *infoPtr, *prevPtr, *nextPtr;
1384 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1385 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1386
1387 /*
1388 * While deleting all the handlers, be careful to check whether
1389 * ConvertSelection or TkSelPropProc are about to process one of the
1390 * deleted handlers.
1391 */
1392
1393 while (winPtr->selHandlerList != NULL) {
1394 selPtr = winPtr->selHandlerList;
1395 winPtr->selHandlerList = selPtr->nextPtr;
1396 for (ipPtr = tsdPtr->pendingPtr; ipPtr != NULL;
1397 ipPtr = ipPtr->nextPtr) {
1398 if (ipPtr->selPtr == selPtr) {
1399 ipPtr->selPtr = NULL;
1400 }
1401 }
1402 if (selPtr->proc == HandleCompat) {
1403 FreeHandler(selPtr->clientData);
1404 }
1405 ckfree((char *) selPtr);
1406 }
1407
1408 /*
1409 * Remove selections owned by window being deleted.
1410 */
1411
1412 for (infoPtr = winPtr->dispPtr->selectionInfoPtr, prevPtr = NULL;
1413 infoPtr != NULL; infoPtr = nextPtr) {
1414 nextPtr = infoPtr->nextPtr;
1415 if (infoPtr->owner == (Tk_Window) winPtr) {
1416 if (infoPtr->clearProc == LostSelection) {
1417 FreeLost(infoPtr->clearData);
1418 }
1419 ckfree((char *) infoPtr);
1420 infoPtr = prevPtr;
1421 if (prevPtr == NULL) {
1422 winPtr->dispPtr->selectionInfoPtr = nextPtr;
1423 } else {
1424 prevPtr->nextPtr = nextPtr;
1425 }
1426 }
1427 prevPtr = infoPtr;
1428 }
1429 }
1430
1431 /*
1432 *----------------------------------------------------------------------
1433 *
1434 * TkSelInit --
1435 *
1436 * Initialize selection-related information for a display.
1437 *
1438 * Results:
1439 * None.
1440 *
1441 * Side effects:
1442 * Selection-related information is initialized.
1443 *
1444 *----------------------------------------------------------------------
1445 */
1446
1447 void
1448 TkSelInit(tkwin)
1449 Tk_Window tkwin; /* Window token (used to find
1450 * display to initialize). */
1451 {
1452 register TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
1453
1454 /*
1455 * Fetch commonly-used atoms.
1456 */
1457
1458 dispPtr->multipleAtom = Tk_InternAtom(tkwin, "MULTIPLE");
1459 dispPtr->incrAtom = Tk_InternAtom(tkwin, "INCR");
1460 dispPtr->targetsAtom = Tk_InternAtom(tkwin, "TARGETS");
1461 dispPtr->timestampAtom = Tk_InternAtom(tkwin, "TIMESTAMP");
1462 dispPtr->textAtom = Tk_InternAtom(tkwin, "TEXT");
1463 dispPtr->compoundTextAtom = Tk_InternAtom(tkwin, "COMPOUND_TEXT");
1464 dispPtr->applicationAtom = Tk_InternAtom(tkwin, "TK_APPLICATION");
1465 dispPtr->windowAtom = Tk_InternAtom(tkwin, "TK_WINDOW");
1466 dispPtr->clipboardAtom = Tk_InternAtom(tkwin, "CLIPBOARD");
1467
1468 /*
1469 * Using UTF8_STRING instead of the XA_UTF8_STRING macro allows us
1470 * to support older X servers that didn't have UTF8_STRING yet.
1471 * This is necessary on Unix systems.
1472 * For more information, see:
1473 * http://www.cl.cam.ac.uk/~mgk25/unicode.html#x11
1474 */
1475
1476 #if !(defined(__WIN32__) || defined(MAC_TCL) || defined(MAC_OSX_TK))
1477 dispPtr->utf8Atom = Tk_InternAtom(tkwin, "UTF8_STRING");
1478 #else
1479 dispPtr->utf8Atom = (Atom) NULL;
1480 #endif
1481 }
1482
1483 /*
1484 *----------------------------------------------------------------------
1485 *
1486 * TkSelClearSelection --
1487 *
1488 * This procedure is invoked to process a SelectionClear event.
1489 *
1490 * Results:
1491 * None.
1492 *
1493 * Side effects:
1494 * Invokes the clear procedure for the window which lost the
1495 * selection.
1496 *
1497 *----------------------------------------------------------------------
1498 */
1499
1500 void
1501 TkSelClearSelection(tkwin, eventPtr)
1502 Tk_Window tkwin; /* Window for which event was targeted. */
1503 register XEvent *eventPtr; /* X SelectionClear event. */
1504 {
1505 register TkWindow *winPtr = (TkWindow *) tkwin;
1506 TkDisplay *dispPtr = winPtr->dispPtr;
1507 TkSelectionInfo *infoPtr;
1508 TkSelectionInfo *prevPtr;
1509
1510 /*
1511 * Invoke clear procedure for window that just lost the selection. This
1512 * code is a bit tricky, because any callbacks due to selection changes
1513 * between windows managed by the process have already been made. Thus,
1514 * ignore the event unless it refers to the window that's currently the
1515 * selection owner and the event was generated after the server saw the
1516 * SetSelectionOwner request.
1517 */
1518
1519 for (infoPtr = dispPtr->selectionInfoPtr, prevPtr = NULL;
1520 infoPtr != NULL; infoPtr = infoPtr->nextPtr) {
1521 if (infoPtr->selection == eventPtr->xselectionclear.selection) {
1522 break;
1523 }
1524 prevPtr = infoPtr;
1525 }
1526
1527 if (infoPtr != NULL && (infoPtr->owner == tkwin)
1528 && (eventPtr->xselectionclear.serial >= (unsigned) infoPtr->serial)) {
1529 if (prevPtr == NULL) {
1530 dispPtr->selectionInfoPtr = infoPtr->nextPtr;
1531 } else {
1532 prevPtr->nextPtr = infoPtr->nextPtr;
1533 }
1534
1535 /*
1536 * Because of reentrancy problems, calling clearProc must be done
1537 * after the infoPtr has been removed from the selectionInfoPtr
1538 * list (clearProc could modify the list, e.g. by creating
1539 * a new selection).
1540 */
1541
1542 if (infoPtr->clearProc != NULL) {
1543 (*infoPtr->clearProc)(infoPtr->clearData);
1544 }
1545 ckfree((char *) infoPtr);
1546 }
1547 }
1548
1549 /*
1550 *--------------------------------------------------------------
1551 *
1552 * SelGetProc --
1553 *
1554 * This procedure is invoked to process pieces of the selection
1555 * as they arrive during "selection get" commands.
1556 *
1557 * Results:
1558 * Always returns TCL_OK.
1559 *
1560 * Side effects:
1561 * Bytes get appended to the dynamic string pointed to by the
1562 * clientData argument.
1563 *
1564 *--------------------------------------------------------------
1565 */
1566
1567 /* ARGSUSED */
1568 static int
1569 SelGetProc(clientData, interp, portion)
1570 ClientData clientData; /* Dynamic string holding partially
1571 * assembled selection. */
1572 Tcl_Interp *interp; /* Interpreter used for error
1573 * reporting (not used). */
1574 char *portion; /* New information to be appended. */
1575 {
1576 Tcl_DStringAppend((Tcl_DString *) clientData, portion, -1);
1577 return TCL_OK;
1578 }
1579
1580 /*
1581 * HandleTclCommand --
1582 *
1583 * This procedure acts as selection handler for handlers created
1584 * by the "selection handle" command. It invokes a Tcl command to
1585 * retrieve the selection.
1586 *
1587 * Results:
1588 * The return value is a count of the number of bytes actually
1589 * stored at buffer, or -1 if an error occurs while executing
1590 * the Tcl command to retrieve the selection.
1591 *
1592 * Side effects:
1593 * None except for things done by the Tcl command.
1594 *
1595 *----------------------------------------------------------------------
1596 */
1597
1598 static int
1599 HandleTclCommand(clientData, offset, buffer, maxBytes)
1600 ClientData clientData; /* Information about command to execute. */
1601 int offset; /* Return selection bytes starting at this
1602 * offset. */
1603 char *buffer; /* Place to store converted selection. */
1604 int maxBytes; /* Maximum # of bytes to store at buffer. */
1605 {
1606 CommandInfo *cmdInfoPtr = (CommandInfo *) clientData;
1607 int spaceNeeded, length;
1608 #define MAX_STATIC_SIZE 100
1609 char staticSpace[MAX_STATIC_SIZE];
1610 char *command, *string;
1611 Tcl_Interp *interp = cmdInfoPtr->interp;
1612 Tcl_Obj *savePtr;
1613 Tcl_Obj *objPtr;
1614 int extraBytes, charOffset, count, numChars;
1615 CONST char *p;
1616
1617 /*
1618 * We must also protect the interpreter and the command from being
1619 * deleted too soon.
1620 */
1621
1622 Tcl_Preserve(clientData);
1623 Tcl_Preserve((ClientData) interp);
1624
1625 /*
1626 * Compute the proper byte offset in the case where the last chunk
1627 * split a character.
1628 */
1629
1630 if (offset == cmdInfoPtr->byteOffset) {
1631 charOffset = cmdInfoPtr->charOffset;
1632 extraBytes = strlen(cmdInfoPtr->buffer);
1633 if (extraBytes > 0) {
1634 strcpy(buffer, cmdInfoPtr->buffer);
1635 maxBytes -= extraBytes;
1636 buffer += extraBytes;
1637 }
1638 } else {
1639 cmdInfoPtr->byteOffset = 0;
1640 cmdInfoPtr->charOffset = 0;
1641 extraBytes = 0;
1642 charOffset = 0;
1643 }
1644
1645 savePtr = Tcl_GetObjResult(interp);
1646 Tcl_IncrRefCount(savePtr);
1647 Tcl_ResetResult(interp);
1648
1649 #ifndef _LANG
1650 /*
1651 * First, generate a command by taking the command string
1652 * and appending the offset and maximum # of bytes.
1653 */
1654
1655 spaceNeeded = cmdInfoPtr->cmdLength + 30;
1656 if (spaceNeeded < MAX_STATIC_SIZE) {
1657 command = staticSpace;
1658 } else {
1659 command = (char *) ckalloc((unsigned) spaceNeeded);
1660 }
1661 sprintf(command, "%s %d %d", cmdInfoPtr->command, charOffset, maxBytes);
1662
1663 /*
1664 * Execute the command. Be sure to restore the state of the
1665 * interpreter after executing the command.
1666 */
1667
1668 Tcl_DStringInit(&oldResult);
1669 Tcl_DStringGetResult(interp, &oldResult);
1670 if (TkCopyAndGlobalEval(interp, command) == TCL_OK) {
1671 #else
1672 if (LangDoCallback(interp, cmdInfoPtr->command,1,2,"%d %d", charOffset, maxBytes) == TCL_OK) {
1673 #endif
1674 objPtr = Tcl_GetObjResult(interp);
1675 string = Tcl_GetStringFromObj(objPtr, &length);
1676 count = (length > maxBytes) ? maxBytes : length;
1677 memcpy((VOID *) buffer, (VOID *) string, (size_t) count);
1678 buffer[count] = '\0';
1679
1680 /*
1681 * Update the partial character information for the next
1682 * retrieval if the command has not been deleted.
1683 */
1684
1685 if (cmdInfoPtr->interp != NULL) {
1686 if (length <= maxBytes) {
1687 cmdInfoPtr->charOffset += Tcl_NumUtfChars(string, -1);
1688 cmdInfoPtr->buffer[0] = '\0';
1689 } else {
1690 p = string;
1691 string += count;
1692 numChars = 0;
1693 while (p < string) {
1694 p = Tcl_UtfNext(p);
1695 numChars++;
1696 }
1697 cmdInfoPtr->charOffset += numChars;
1698 length = p - string;
1699 if (length > 0) {
1700 strncpy(cmdInfoPtr->buffer, string, (size_t) length);
1701 }
1702 cmdInfoPtr->buffer[length] = '\0';
1703 }
1704 cmdInfoPtr->byteOffset += count + extraBytes;
1705 }
1706 count += extraBytes;
1707 } else {
1708 count = -1;
1709 }
1710 Tcl_SetObjResult(interp,savePtr);
1711
1712 #ifndef _LANG
1713 if (command != staticSpace) {
1714 ckfree(command);
1715 }
1716 #endif
1717
1718
1719 Tcl_Release(clientData);
1720 Tcl_Release((ClientData) interp);
1721 return count;
1722 }
1723
1724 /*
1725 *----------------------------------------------------------------------
1726 *
1727 * TkSelDefaultSelection --
1728 *
1729 * This procedure is called to generate selection information
1730 * for a few standard targets such as TIMESTAMP and TARGETS.
1731 * It is invoked only if no handler has been declared by the
1732 * application.
1733 *
1734 * Results:
1735 * If "target" is a standard target understood by this procedure,
1736 * the selection is converted to that form and stored as a
1737 * character string in buffer. The type of the selection (e.g.
1738 * STRING or ATOM) is stored in *typePtr, and the return value is
1739 * a count of the # of non-NULL bytes at buffer. If the target
1740 * wasn't understood, or if there isn't enough space at buffer
1741 * to hold the entire selection (no INCR-mode transfers for this
1742 * stuff!), then -1 is returned.
1743 *
1744 * Side effects:
1745 * None.
1746 *
1747 *----------------------------------------------------------------------
1748 */
1749
1750 int
1751 TkSelDefaultSelection(infoPtr, target, lbuffer, maxBytes, typePtr, formatPtr)
1752 TkSelectionInfo *infoPtr; /* Info about selection being retrieved. */
1753 Atom target; /* Desired form of selection. */
1754 long *lbuffer; /* Place to put selection characters. */
1755 int maxBytes; /* Maximum # of bytes to store at buffer. */
1756 Atom *typePtr; /* Store here the type of the selection,
1757 * for use in converting to proper X format. */
1758 int *formatPtr;
1759 {
1760 register TkWindow *winPtr = (TkWindow *) infoPtr->owner;
1761 TkDisplay *dispPtr = winPtr->dispPtr;
1762 char *buffer = (char *) lbuffer;
1763
1764 if (target == dispPtr->timestampAtom) {
1765 if (maxBytes < 20) {
1766 return -1;
1767 }
1768 *((long *) buffer) = (long) infoPtr->time;
1769 *typePtr = XA_INTEGER;
1770 *formatPtr = 32;
1771 return 1;
1772 }
1773
1774 if (target == dispPtr->targetsAtom) {
1775 register TkSelHandler *selPtr;
1776 Atom *ap = (Atom *) buffer;
1777
1778 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1779 return -1;
1780 *ap++ = Tk_InternAtom((Tk_Window) winPtr,"MULTIPLE");
1781 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1782 return -1;
1783 *ap++ = Tk_InternAtom((Tk_Window) winPtr,"TARGETS");
1784 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1785 return -1;
1786 *ap++ = Tk_InternAtom((Tk_Window) winPtr,"TIMESTAMP");
1787 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1788 return -1;
1789 *ap++ = Tk_InternAtom((Tk_Window) winPtr,"TK_APPLICATION");
1790 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1791 return -1;
1792 *ap++ = Tk_InternAtom((Tk_Window) winPtr,"TK_WINDOW");
1793
1794 for (selPtr = winPtr->selHandlerList; selPtr != NULL;
1795 selPtr = selPtr->nextPtr) {
1796 if (selPtr->selection == infoPtr->selection) {
1797 if ((char *)ap >= buffer + maxBytes - sizeof(Atom))
1798 return -1;
1799 *ap++ = selPtr->target;
1800 }
1801 }
1802 *typePtr = XA_ATOM;
1803 *formatPtr = 32;
1804 return (ap - ((Atom *) buffer));
1805 }
1806
1807 if (target == dispPtr->applicationAtom) {
1808 int length;
1809 Tk_Uid name = winPtr->mainPtr->winPtr->nameUid;
1810
1811 length = strlen(name);
1812 if (maxBytes <= length) {
1813 return -1;
1814 }
1815 strcpy(buffer, name);
1816 *typePtr = XA_STRING;
1817 *formatPtr = 8;
1818 return length;
1819 }
1820
1821 if (target == dispPtr->windowAtom) {
1822 int length;
1823 char *name = winPtr->pathName;
1824
1825 length = strlen(name);
1826 if (maxBytes <= length) {
1827 return -1;
1828 }
1829 strcpy(buffer, name);
1830 *typePtr = XA_STRING;
1831 *formatPtr = 8;
1832 return length;
1833 }
1834
1835 return -1;
1836 }
1837
1838 /*
1839 *----------------------------------------------------------------------
1840 *
1841 * LostSelection --
1842 *
1843 * This procedure is invoked when a window has lost ownership of
1844 * the selection and the ownership was claimed with the command
1845 * "selection own".
1846 *
1847 * Results:
1848 * None.
1849 *
1850 * Side effects:
1851 * A Tcl script is executed; it can do almost anything.
1852 *
1853 *----------------------------------------------------------------------
1854 */
1855
1856 static void
1857 LostSelection(clientData)
1858 ClientData clientData; /* Pointer to LostCommand structure. */
1859 {
1860 LostCommand *lostPtr = (LostCommand *) clientData;
1861 Tcl_Obj *objPtr;
1862 Tcl_Interp *interp;
1863
1864 interp = lostPtr->interp;
1865 Tcl_Preserve((ClientData) interp);
1866
1867 /*
1868 * Execute the command. Save the interpreter's result, if any, and
1869 * restore it after executing the command.
1870 */
1871
1872 objPtr = Tcl_GetObjResult(interp);
1873 Tcl_IncrRefCount(objPtr);
1874 Tcl_ResetResult(interp);
1875
1876 #ifndef _LANG
1877 if (TkCopyAndGlobalEval(interp, lostPtr->command) != TCL_OK) {
1878 #else
1879 if (LangDoCallback(interp, lostPtr->command,0,0) != TCL_OK) {
1880 #endif
1881 Tcl_BackgroundError(interp);
1882 }
1883
1884 Tcl_SetObjResult(interp, objPtr);
1885
1886 Tcl_Release((ClientData) interp);
1887
1888 /*
1889 * Free the storage for the command, since we're done with it now.
1890 */
1891 FreeLost(clientData);
1892 }
1893
1894 static void
1895 FreeLost(clientData)
1896 ClientData clientData;
1897 {
1898 LostCommand *lostPtr = (LostCommand *) clientData;
1899 LangFreeCallback(lostPtr->command);
1900 ckfree((char *) lostPtr);
1901 }
1902
1903
1904
1905
1906