Tk Library Source Code

Documentation
Login


[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]

NAME

persistentSelection - Enhanced PRIMARY selection

Table Of Contents

SYNOPSIS

package require Tcl 8.5
package require Tk 8.5
package require persistentSelection ?1.0?

persistentSelection::fixText
persistentSelection::getClassicPrimarySel ?displayOf? ?withOthers?
persistentSelection::getPrimarySel ?displayOf?
persistentSelection::report type pathName

DESCRIPTION

Consider a Tk widget that sets its -exportselection option to boolean true, thereby exporting its selection. When the selection in the widget is canceled, by default Tk also cancels the PRIMARY selection. In this situation, an attempt to read the PRIMARY selection, e.g. by a <> event, will fail.

The persistentSelection package offers more convenient behavior. It will sustain as the PRIMARY selection the last non-empty value set by any Tk widget that it monitors, as long as the PRIMARY selection is not claimed by another process, or by a widget that it does not monitor.

The persistentSelection package works by binding to the <> event on the widgets of interest, and using this binding to monitor any change of the selection in the widget. Its commands are confined to the namespace ::persistentSelection.

COMMANDS

USAGE

Script requirements

For each widget that will be monitored by persistentSelection, the command persistentSelection::report must be bound to event <> in one of the widget's binding tags.

For example, the developer may wish to use persistentSelection with every _text_ and _entryPlus_ widget in an application: this can be achieved by adding the <> binding to the Text and EntryPlus bindtags. See EXAMPLES.

If persistentSelection is not required for all widgets of a particular class, the binding can be added to per-widget bindtags. See EXAMPLES.

Widget requirements

To be capable of being monitored by persistentSelection, a widget must satisfy three conditions:

The Tk and Ttk widgets that can export their selection are _text_, _entry_, _listbox_, _spinbox_, _ttk::entry_, _ttk::spinbox_, and _ttk::combobox_.

Text widgets

In versions of Tk older than 8.6.9, the _text_ widget does not generate <> events in a few "corner cases" in which its selection changes. These omissions can be corrected by changes to the Text bindings, in order to satisfy the second condition of Widget requirements.

In addition, versions of Tk older than 8.6.6 process selection events slightly differently from newer versions, and in combination with the Text bindings this confuses persistentStore. If an upgrade to the current version of Tcl/Tk is not feasible, this problem can be resolved by making a slight modification to the widget bindings, in order to satisfy the third condition of Widget requirements.

Either the script should call the command persistentSelection::fixText to adjust the Text bindings and the commands that they call; or the widget can use the Ntext binding tag (for ntext version 1.0 or above) instead of the default Text bindtag.

In either case, the argument type supplied to command persistentSelection::report should have the value text.

Entry, spinbox, ttk::entry, ttk::spinbox, and ttk::combobox widgets

The _entry_, _spinbox_, _ttk::entry_, _ttk::spinbox_, and _ttk::combobox_ widgets do not generate a <> event when their selection changes, and therefore require modification.

The package widgetPlus provides snit wrappers _widgetPlus::entryPlus_, _widgetPlus::spinboxPlus_, _widgetPlus::ttkEntryPlus_, _widgetPlus::ttkSpinboxPlus_, and _widgetPlus::ttkComboboxPlus_ respectively. Each widgetPlus widget generates the required <> events.

In all these cases, the argument type supplied to command persistentSelection::report should have the value entry. This argument determines how persistentSelection will inspect the widget's selection, and the commands that do so are the same for all these widgets.

Listbox widgets

A similar wrapper has not been created for the _listbox_ widget, which has the complication of permitting multiple selections.

TO DO

BUGS

This version of persistentSelection is intended to be compatible with all releases of Tk 8.5 and 8.6, and with the branches core-8-5-branch, core-8-6-branch, revised_text, and trunk in the source code repository for Tk. Any incompatibility with any of these versions, for any Tk windowing system, should be reported as a bug. Please report such in the category persistentSelection of the Tklib Trackers.

EXAMPLES

Each example uses persistentSelection to retain the last non-empty value of the selection in certain widgets. Each example also includes the creation of sample widgets.

Monitor all _entryPlus_ widgets.

package require widgetPlus
widgetPlus::entryPlus .ep
pack .ep

package require persistentSelection
bind EntryPlus <<Selection>> {::persistentSelection::report entry %W}

Monitor all _text_ widgets that use the default Text bindings.

text .t
pack .t

package require persistentSelection
::persistentSelection::fixText
bind Text <<Selection>> {::persistentSelection::report text %W}

Monitor all _text_ widgets that use the default Text bindings, and all _entryPlus_ widgets.

text .t
pack .t

package require widgetPlus
widgetPlus::entryPlus .ep
pack .ep

package require persistentSelection
::persistentSelection::fixText
bind Text <<Selection>> {::persistentSelection::report text %W}
bind EntryPlus <<Selection>> {::persistentSelection::report entry %W}

Monitor all _text_ widgets that use Ntext bindings.

text .t
pack .t

package require ntext
bindtags .t {.t Ntext . all}

package require persistentSelection
bind Ntext <<Selection>> {::persistentSelection::report text %W}

Monitor a single _entryPlus_ widget .ep

package require widgetPlus
widgetPlus::entryPlus .ep
pack .ep

package require persistentSelection
bind .ep <<Selection>> {::persistentSelection::report entry %W}

Monitor a single _text_ widget .t

text .t
pack .t

package require persistentSelection
bind .t <<Selection>> {::persistentSelection::report text %W}

SEE ALSO

bindtags, ntext, text, widgetPlus