13.14. Clipboard and Selection MethodsPerl/Tk supports two kinds of selection. They are named after the atoms that identify them: CLIPBOARD and PRIMARY. Both selection buffers are capable of handling arbitrary data, but they default to simple ASCII text strings. Handling other data is beyond the scope of this book, thus we confine the discussion to strings. When making a selection, standard Perl/Tk widgets—such as Text and Entry—select PRIMARY and highlight the selection. The widgets copy the selection to CLIPBOARD as well. This means that pasting text in Tk works in either of two ways:
Although we don't talk about virtual events until Chapter 15, "Anatomy of the MainLoop", participating widgets (effectively) bind <<Copy>>, <<Cut>>, and <<Paste>> virtual events to class methods that manipulate the CLIPBOARD selection. The MainWindow generates virtual <<Copy>>, <<Cut>>, and <<Paste>> events when it sees the characters Control-c, Control-x, and Control-v, respectively. Applications differ as to which selection mechanism they use. The tendency these days is to use the CLIPBOARD and the characters c, x, and v. What differs is the lead-in character. It may be Control, Alt, or, on a Macintosh, the Apple key. Venerable Unix applications tend to use PRIMARY, where you copy with button 1 and paste with button 2. Your mileage may vary. 13.14.1. Clipboard MethodsThe following methods manipulate the internal Tk clipboard, under Unix or Win32. To clear out the clipboard, use clipboardClear: $widget->clipboardClear; Any data in the clipboard will be removed. $widget owns the clipboard. To add data to the clipboard, use the clipboardAppend method: $widget->clipboardAppend("data to add"); To find out what's in the clipboard, see the SelectionGet method in the following section. 13.14.2. Selection MethodsSome widgets allow the user to make a selection. For example, the user can make a selection by dragging the mouse over some characters in the Text, Entry, and Listbox widgets. You can manipulate the selection by using the following methods. 13.14.2.1. Clearing the selectionTo clear the current selection from any widget, use SelectionClear: $widget->SelectionClear; You can specify a -selection option, which takes either PRIMARY or CLIPBOARD. The default is PRIMARY. 13.14.2.2. Getting the selectionTo determine the current selection for the application, use SelectionGet: $selection = $widget->SelectionGet; You can specify the -selection option with the SelectionGet method: $clipboard = $widget->SelectionGet(-selection => 'CLIPBOARD'); The -selection option takes, again, either PRIMARY or CLIPBOARD. The default is PRIMARY. The SelectionGet command aborts if there is no selection, but you can catch errors using this idiom: Tk::catch { $sel = $mw->SelectionGet }; if ( $@ ) { warn $@; } else { print "selection = '$sel'\n"; } 13.14.2.3. Assigning a callbackYou can call SelectionHandle to assign a callback that's automatically invoked when the selection associated with $widget changes: $widget->SelectionHandle( \&callback); When $widget owns the selection and there's a request for its selection, the callback is invoked. The callback should then return the selection. It's very possible that the caller may have insufficient space for the entire selection. Please read the Tk::Selection manpage for further details. 13.14.2.4. Determining the ownerYou can find out which widget on the screen currently owns the selection by calling SelectionOwner(a widget owns the selection if something is selected in it): $widget = $widget->SelectionOwner; You can also specify the -selection option with either PRIMARY or CLIPBOARD as the value to determine who owns the selection or the current clipboard value, respectively. 13.14.2.5. Setting the ownerTo force a widget to own the selection, call SelectionOwn: $widget->SelectionOwn; You can also specify which type of selection to force by using the -selection option with PRIMARY or CLIPBOARD. Finally, you can specify a -command option with an associated callback that will be invoked when that widget's selection is taken away. 13.14.3. Exporting the Selection to the Outside WorldHere's a clever trick that inserts a string into both the PRIMARY and CLIPBOARD selections: my $sel = 'frogs lacking lipophores are blue'; # Put a string in the CLIPBOARD buffer. $mw->clipboardClear; $mw->clipboardAppend('--', $sel); # Put a string in the PRIMARY X buffer. $mw->SelectionClear; $mw->SelectionHandle( sub {$sel} ); $mw->SelectionOwn; Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|