Re: Non-editable textview paste




On Jun 30, 2005, at 7:57 PM, Thomas Christensen wrote:

Ok, I can react to the "button 2" signal, but is there a more generic "paste signal"?

From what i can tell, just "paste-clipboard" and "button-press-event".


Also, I have done '#perldoc Gtk2::Selection' and googled a little, but
I can't quite find how to fetch the selection.  I have seen mentions
of gtk_selection_convert at the GTK+ site's selections tutorial, which
I am not sure I fully understand; but Gtk2::Selection doesn't seem
have this method.

There's higher-level stuff in Gtk2::Clipboard. The easiest one is $text = $clipboard->wait_for_text, which wraps up the async request and wait in a synchronous call. See the goofy example, below.


Thanks for the pointers, and tell me if this is not the right place to ask, since it is mostly GTK+ oriented.

It's all good. Although we do seem to be a little light on the Buffy and pie around here.



#!/usr/bin/perl -w

use strict;
use Gtk2 -init;
use Glib qw(FALSE TRUE);

my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub { Gtk2->main_quit });

my $textview = Gtk2::TextView->new;
$textview->set_editable (FALSE);
$textview->get_buffer->create_tag ('tt', family => 'Monospace');
$textview->signal_connect (paste_clipboard => sub {
    my ($view) = @_;
    # the paste-clipboard signal is emitted by the keybindings for
    # pasting from the clipboard selection.
    do_paste ($view, Gtk2::Gdk->SELECTION_CLIPBOARD);
});
$textview->signal_connect (button_press_event => sub {
    my ($view, $event) = @_;
    if ($event->button == 2) {
    # middle mouse buttons paste from the primary selection.
    do_paste ($view, Gtk2::Gdk->SELECTION_PRIMARY);
    }
    return FALSE;
});

my $scroller = Gtk2::ScrolledWindow->new;
$scroller->add ($textview);

$window->add ($scroller);

$window->show_all;
Gtk2->main;

sub do_paste {
    my ($view, $atom) = @_;
    my $clipboard = $view->get_clipboard ($atom);

    # if you wanted to insert the text, you could do it like this...
    #$view->get_buffer->paste_clipboard ($clipboard, undef, TRUE);

    # or, we'll do something else.

    # wait_for_text requests the contents of the clipboard as a string,
    # and runs a main loop to wait for the server to return it.
    # that means stuff can happen before this call finishes.
    my $text = $clipboard->wait_for_text;

    # and now we get fancy just because it's fun.
    my $buffer = $view->get_buffer;
    $buffer->delete ($buffer->get_bounds);
    $buffer->insert ($buffer->get_end_iter, "User wants to paste\n");
$buffer->insert_with_tags_by_name ($buffer->get_end_iter, $text, 'tt');
    $buffer->insert ($buffer->get_end_iter,
"\nBut we're read-only, so we ain't gonna let him.");
}




--
That's it! It's one thing for a ghost to scare my children, but it's another to play my theremin!
  - Homer Simpson




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]