Tk Source Code

Artifact [f9338ac2]
Login

Artifact f9338ac2c6a784f86f2b9251031d294b735b6796:

Attachment "tk-selectionutf8-v2.patch" to ticket [2443069f] added by andygoth 2011-04-18 08:38:09.
--- a/doc/CrtSelHdlr.3	2008-12-17 16:55:30.000000000 -0600
+++ b/doc/CrtSelHdlr.3	2008-12-17 17:13:26.000000000 -0600
@@ -111,8 +111,19 @@
 handler for \fIselection\fR and \fItarget\fR on \fItkwin\fR, then the
 existing handler is replaced with a new one.
 .PP
+When \fBTk_CreateSelHandler\fR is called with a \fItarget\fR argument
+of STRING, Tk automatically creates a second handler with \fItarget\fR
+and \fIformat\fR set to UTF8_STRING.  If a UTF8_STRING handler already
+exists for \fIselection\fR on \fItkwin\fR, it is replaced, unless an
+existing STRING handler is also being replaced that has a different
+\fIproc\fR than the existing UTF8_STRING handler.
+.PP
 \fBTk_DeleteSelHandler\fR removes the handler given by \fItkwin\fR,
 \fIselection\fR, and \fItarget\fR, if such a handler exists.
-If there is no such handler then it has no effect.
+If there is no such handler then it has no effect.  If
+\fBTk_DeleteSelHandler\fR successfully removes a handler with a
+\fItarget\fR of STRING, it also attempts to remove a corresponding
+handler with \fItarget\fR and \fIformat\fR equal to UTF8_STRING and
+the same values of \fItkwin\fR and \fIproc\fR as the STRING handler.
 .SH KEYWORDS
 format, handler, selection, target
--- a/generic/tkSelect.c	2008-12-17 15:58:52.000000000 -0600
+++ b/generic/tkSelect.c	2008-12-17 16:47:15.000000000 -0600
@@ -131,6 +131,7 @@
 {
     register TkSelHandler *selPtr;
     TkWindow *winPtr = (TkWindow *) tkwin;
+    Tk_SelectionProc *oldProc;
 
     if (winPtr->dispPtr->multipleAtom == None) {
 	TkSelInit(tkwin);
@@ -146,10 +147,24 @@
 	    selPtr = (TkSelHandler *) ckalloc(sizeof(TkSelHandler));
 	    selPtr->nextPtr = winPtr->selHandlerList;
 	    winPtr->selHandlerList = selPtr;
+
+	    /*
+	     * When creating a new handler, we set the old proc to zero which
+	     * compares unequal to any real Tk selection handler entry.
+	     */
+
+	    oldProc = NULL;
 	    break;
 	}
 	if ((selPtr->selection == selection) && (selPtr->target == target)) {
 	    /*
+	     * When replacing an existing handler, we remember the previous
+	     * proc for later comparison with the UTF8_STRING handler.
+	     */
+
+	    oldProc = selPtr->proc;
+
+	    /*
 	     * Special case: when replacing handler created by "selection
 	     * handle", free up memory. Should there be a callback to allow
 	     * other clients to do this too?
@@ -176,42 +191,51 @@
 	/*
 	 * If the user asked for a STRING handler and we understand
 	 * UTF8_STRING, we implicitly create a UTF8_STRING handler for them.
+	 * First we check if a UTF8_STRING handler already exists for the same
+	 * widget and selection.  If one doesn't exist, one must be created.
+	 * If one does exist and it has the same proc as the STRING handler
+	 * that we just replaced, it must also be replaced.  If one does exist
+	 * but it has a different proc, or if we didn't just replace an
+	 * existing STRING handler, we must leave it alone, since we didn't
+	 * automatically create it in the first place.
 	 */
 
+	TkSelHandler *utf8selPtr;
+
 	target = winPtr->dispPtr->utf8Atom;
-	for (selPtr = winPtr->selHandlerList; ; selPtr = selPtr->nextPtr) {
-	    if (selPtr == NULL) {
-		selPtr = (TkSelHandler *) ckalloc(sizeof(TkSelHandler));
-		selPtr->nextPtr = winPtr->selHandlerList;
-		winPtr->selHandlerList = selPtr;
-		selPtr->selection = selection;
-		selPtr->target = target;
-		selPtr->format = target; /* We want UTF8_STRING format */
-		selPtr->proc = proc;
-		if (selPtr->proc == HandleTclCommand) {
-		    /*
-		     * The clientData is selection controlled memory, so we
-		     * should make a copy for this selPtr.
-		     */
-
-		    unsigned cmdInfoLen = sizeof(CommandInfo) +
-			    ((CommandInfo*)clientData)->cmdLength - 3;
-
-		    selPtr->clientData = (ClientData) ckalloc(cmdInfoLen);
-		    memcpy(selPtr->clientData, clientData, cmdInfoLen);
-		} else {
-		    selPtr->clientData = clientData;
-		}
-		selPtr->size = 8;
+	for (utf8selPtr = winPtr->selHandlerList; utf8selPtr != NULL;
+		utf8selPtr = utf8selPtr->nextPtr) {
+	    if ((utf8selPtr->selection == selection)
+		    && (utf8selPtr->target == target)) {
 		break;
 	    }
-	    if (selPtr->selection==selection && selPtr->target==target) {
+	}
+	if (utf8selPtr == NULL || ((utf8selPtr->format == target)
+		&& (utf8selPtr->proc == oldProc))) {
+	    ClientData newClientData;
+
+	    if (proc == HandleTclCommand) {
 		/*
-		 * Looks like we had a utf-8 target already. Leave it alone.
+		 * The clientData is selection controlled memory, so we should
+		 * make a copy for this new selection handler.
 		 */
 
-		break;
+		unsigned cmdInfoLen = sizeof(CommandInfo) +
+			((CommandInfo*)clientData)->cmdLength - 3;
+		
+		newClientData = (ClientData) ckalloc(cmdInfoLen);
+		memcpy(newClientData, clientData, cmdInfoLen);
+	    } else {
+		newClientData = clientData;
 	    }
+
+	    /*
+	     * This recursive call is OK, because we've changed the value
+	     * of 'target'.
+	     */
+
+	    Tk_CreateSelHandler(tkwin, selection, target, proc, newClientData,
+		    target);
 	}
     }
 }