Tk Source Code

Artifact [550b3a07]
Login

Artifact 550b3a075caf7c014a9c9d6cc87c923e5bea7046:

Attachment "primary-selection-problem2.tcl" to ticket [de156e9e] added by kjnash 2017-07-01 01:42:21.
#! /usr/bin/env wish

set script {
    pack [text  .text -width 40 -height 17 -bg white] -pady 5
    if {[interp issafe]} {
        .text insert end "A safe interpreter is \"safe\" to run\nuntrusted\
                code.  So there's nothing to\nsee\
                here! Move along now!\n\nThe PRIMARY selection will be\
                replaced\nwith \"PAYLOAD\".\n\nOn Linux the text above will\
                be\ndeselected when this happens. Try\n<<PasteSelection>> in a\
                different\nwindow.\n\nWindows allows multiple selections\
                so\nthe \"deselection\" cue is absent; however\nthe PRIMARY\
                selection can only be copied\nto a window in an unsafe\
                interpreter\nwith the same (root) master."
        .text tag add sel 1.23 1.27
    } else {
        .text insert end "An unsafe interpreter's script can\nwrite\
                to the PRIMARY selection at any\ntime.\
                You must trust the author of any\nscript\
                that you run in an unsafe\ninterpreter\
                not to abuse this privilege\n(and\
                many others)."
        .text tag add sel 2.13 2.20
    }

    # Use the selection in an unmapped text widget to
    # manipulate the PRIMARY selection.
    # When this happens, the mapped text
    # widget loses its selection.
    text .t2
    .t2 insert end PAYLOAD
    proc TryAgain {} {
        .t2 tag remove sel 1.0 end-1c
        after 400 .t2 tag add sel 1.0 end-1c
        after 5000 TryAgain
        return
    }
    after 5000 TryAgain
}

if 0 {
    # Run in the master interpreter.
    eval $script
} else {
    # To run in a slave interpreter.
    set int2 slave2
    ::safe::interpCreate $int2
    ::safe::loadTk $int2
    $int2 eval $script
}

# Unsafe slave to test PasteSelection
set int1 slave1
interp create -- $int1
$int1 eval {
    package require Tk
    wm title . {Unsafe Slave}
    pack [text  .text -width 40 -height 17 -bg white] -pady 5
    .text insert end "Try <<PasteSelection>> here.\n\nOn Linux, you can\
            also\n<<PasteSelection>> to windows of other\nprocesses.\n\nOn\
            Windows, you can only\n<<PasteSelection>> here."
}