Tk Source Code

Artifact [51b40b79]
Login

Artifact 51b40b791214b60e2b5cfb60a86ab9539bb7140e:

Attachment "tkClipboard.c.diffs_for_clipboard_command" to ticket [1954968f] added by venksi 2008-05-01 00:42:10.
--- tkClipboard.c~	2004-05-03 15:23:08.000000000 -0700
+++ tkClipboard.c	2008-04-30 10:13:52.000000000 -0700
@@ -33,6 +33,25 @@
 static int		ClipboardGetProc _ANSI_ARGS_((ClientData clientData,
 			    Tcl_Interp *interp, char *portion));
 
+
+/*
+ * When clipboard ownership is claimed with the "selection own" or
+ * "clipboard" Tcl command, one of the following structures is created
+ * to record the Tcl command to be executed when the clipboard is lost
+ * again.
+ */
+
+typedef struct LostClipCommand {
+    Tcl_Interp *interp;		/* Interpreter in which to invoke command. */
+    TkDisplay *dispPtr;		/* Display on which we're working */
+    char command[4];		/* Command to invoke.  Actual space is
+				 * allocated as large as necessary.  This
+				 * must be the last entry in the structure. */
+} LostClipCommand;
+
+
+
+
 /*
  *----------------------------------------------------------------------
  *
@@ -212,9 +231,42 @@
 ClipboardLostSel(clientData)
     ClientData clientData;		/* Pointer to TkDisplay structure. */
 {
-    TkDisplay *dispPtr = (TkDisplay*) clientData;
+    LostClipCommand *lostPtr = (LostClipCommand *) clientData;
+    Tcl_Obj *objPtr;
+    Tcl_Interp *interp;
+
+    Tcl_Preserve((ClientData) interp);
+
+    lostPtr->dispPtr->clipboardActive = 0;
+    interp = lostPtr->interp;
+    if (interp == NULL) { /* we do not have any script to execute */
+	ckfree((char *) lostPtr);
+	return;
+    }
 
-    dispPtr->clipboardActive = 0;
+    /*
+     * Execute the command.  Save the interpreter's result, if any, and
+     * restore it after executing the command.
+     */
+
+    objPtr = Tcl_GetObjResult(interp);
+    Tcl_IncrRefCount(objPtr);
+    Tcl_ResetResult(interp);
+
+    if (TkCopyAndGlobalEval(interp, lostPtr->command) != TCL_OK) {
+	Tcl_BackgroundError(interp);
+    }
+
+    Tcl_SetObjResult(interp, objPtr);
+    Tcl_DecrRefCount(objPtr);
+
+    Tcl_Release((ClientData) interp);
+    
+    /*
+     * Free the storage for the command, since we're done with it now.
+     */
+
+    ckfree((char *) lostPtr);
 }
 
 /*
@@ -326,7 +378,7 @@
  */
 
 int
-Tk_ClipboardAppend(interp, tkwin, type, format, buffer)
+Tk_ClipboardAppend(interp, tkwin, type, format, script, buffer)
     Tcl_Interp *interp;		/* Used for error reporting. */
     Tk_Window tkwin;		/* Window that selects a display. */
     Atom type;			/* The desired conversion type for this
@@ -334,6 +386,7 @@
     Atom format;		/* Format in which the selection
 				 * information should be returned to
 				 * the requestor. */
+    char* script;		/* script to be executed when we lose clipboard */
     char* buffer;		/* NULL terminated string containing the data
 				 * to be added to the clipboard. */
 {
@@ -350,8 +403,18 @@
     if (dispPtr->clipboardAppPtr != winPtr->mainPtr) {
 	Tk_ClipboardClear(interp, tkwin);
     } else if (!dispPtr->clipboardActive) {
+	cmdLength = script == NULL ?  0 : strlen(script);
+	lostPtr = (LostClipCommand *) ckalloc ((unsigned) (sizeof(LostClipCommand)
+							   - 3 + cmdLength));
+	if (script == NULL) {
+	    lostPtr->interp = NULL;
+	} else {
+	    lostPtr->interp = interp;
+	    strcpy(lostPtr->command,script);
+	}
+							  
 	Tk_OwnSelection(dispPtr->clipWindow, dispPtr->clipboardAtom,
-		ClipboardLostSel, (ClientData) dispPtr);
+		ClipboardLostSel, (ClientData) lostPtr);
 	dispPtr->clipboardActive = 1;
     }
 
@@ -453,12 +516,13 @@
 	    Atom target, format;
 	    char *targetName = NULL;
 	    char *formatName = NULL;
+	    char *script = NULL;
 	    char *string;
 	    static CONST char *appendOptionStrings[] = {
-		"-displayof", "-format", "-type", NULL
+		"-displayof", "-format", "-type", "-command", NULL
 	    };
 	    enum appendOptions { APPEND_DISPLAYOF, APPEND_FORMAT,
-				     APPEND_TYPE };
+				 APPEND_TYPE, APPEND_COMMAND };
 	    int subIndex, length;
 	    
 	    for (i = 2; i < objc - 1; i++) {
@@ -500,6 +564,9 @@
 		    case APPEND_TYPE:
 			targetName = Tcl_GetString(objv[i]);
 			break;
+		    case APPEND_COMMAND:
+			script = Tcl_GetString(objv[1]);
+			break
 		}
 	    }
 	    if (objc - i != 1) {
@@ -522,7 +589,7 @@
 	    } else {
 		format = XA_STRING;
 	    }
-	    return Tk_ClipboardAppend(interp, tkwin, target, format,
+	    return Tk_ClipboardAppend(interp, tkwin, target, format, script, 
 		    Tcl_GetString(objv[i]));
 	}
 	case CLIPBOARD_CLEAR: {