Tk Source Code

View Ticket
Login
Ticket UUID: 3c8a969ac152fa7e2e367f2bc00b1acda61482ba
Title: [selection handle] doesn't replace a previously installed utf-8 handler
Type: Bug Version: 8.6.2
Submitter: anonymous Created on: 2014-09-22 13:36:07
Subsystem: 53. [selection] Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2014-09-22 15:35:39
Resolution: None Closed By: nobody
    Closed on:
Description:
Setting up a selection handler for type STRING also triggers the setting up of the relative UTF-8 handler. When replacing a handler by a subsequent call to [selection handle], the UTF-8 handler is not modified. This results in the old handler to be called even after a new one was installed.

The following simple script exhibits this behavior:

===================================================================================================

package require Tk

set i 0
proc setup {} {
    global i

    switch $i {
        0 { selection handle . h1; incr i }
        1 { selection handle . h2; incr i }
        default { exit }
    }

    selection own .
    puts "Ready..."
}

proc h1 {off max} {
    after 0 setup
    return "h1"
}

proc h2 {off max} {
    after 0 setup
    return "h2"
}

setup

===================================================================================================

This script will copy "h1" twice in the PRIMARY selection.

The following patch Works For Me (tm), but please double check it before committing.

===================================================================================================

--- generic/tkSelect.c
+++ generic/tkSelect.c
@@ -182,35 +182,32 @@
 		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 = Tk_Offset(CommandInfo, command) +
-			    ((CommandInfo *)clientData)->cmdLength + 1;
-
-		    selPtr->clientData = ckalloc(cmdInfoLen);
-		    memcpy(selPtr->clientData, clientData, cmdInfoLen);
-		} else {
-		    selPtr->clientData = clientData;
-		}
-		selPtr->size = 8;
 		break;
 	    }
 	    if (selPtr->selection==selection && selPtr->target==target) {
-		/*
-		 * Looks like we had a utf-8 target already. Leave it alone.
-		 */
+                break;
+            }
+	}
 
-		break;
-	    }
+	if (selPtr->proc == HandleTclCommand) {
+	    /*
+	     * The clientData is selection controlled memory, so we
+	     * should make a copy for this selPtr.
+	     */
+
+	    unsigned cmdInfoLen = Tk_Offset(CommandInfo, command) +
+		((CommandInfo *)clientData)->cmdLength + 1;
+
+	    selPtr->clientData = ckalloc(cmdInfoLen);
+	    memcpy(selPtr->clientData, clientData, cmdInfoLen);
+	} else {
+	    selPtr->clientData = clientData;
 	}
+	selPtr->size = 8;
     }
 }
 
 /*
  *----------------------------------------------------------------------

===================================================================================================
User Comments: anonymous (claiming to be [email protected]) added on 2014-09-22 15:35:39:
Yes, sorry, I missed that. Would be great if this could finally be merged. Do you want me to try out the patch attached to [2443069]?

jan.nijtmans added on 2014-09-22 14:24:09:

Is this a duplicate of [2443069] ????