Non-editable textview paste

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Non-editable textview paste

Thomas Christensen
Hi,

I was wondering if anybody could give me a pointer to how to catch a
paste (non-clipboard primarily), in a textview.

I have tried the 'clipboard-paste' signal but is doesn't get triggered
on a plain (middle-mouse) paste, and it's not applicable anyways since
my textview is editable => 0.

Any hints would be well received.

Thomas Christensen

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Non-editable textview paste

muppet-6

Thomas Christensen said:
> I was wondering if anybody could give me a pointer to how to catch a
> paste (non-clipboard primarily), in a textview.

I'm guessing that you want to catch a paste and redirect it to some other
destination, which will eventually modify the textbuffer on its own.  But the
"non-clipboard primarily" makes me wonder if perhaps you mean "i just want to
know whenever the buffer is modified"; if that's the case, you can just listen
to the "changed" or "insert-text" signals on the TextBuffer.


> I have tried the 'clipboard-paste' signal but is doesn't get triggered
> on a plain (middle-mouse) paste, and it's not applicable anyways since
> my textview is editable => 0.

The selections are different; middle button paste is the PRIMARY, while Ctrl+V
is CLIPBOARD.  Inside GtkTextView, the 'paste-clipboard' signal handles the
CLIPBOARD selection, and the 'button-press-event' handler fetches and inserts
the PRIMARY selection when button==2.

Both of these eventually wind up down in gtk_text_buffer_insert_interactive()
with default_editable==FALSE (since you've set your treeview to non-editable)
and thus do nothing.


> Any hints would be well received.

The paste-clipboard and button-press-event signals fire regardless of whether
the view is editable, so you can still hook up to them and do stuff.


--
muppet <scott at asofyet dot org>

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Non-editable textview paste

Thomas Christensen
"muppet" <[hidden email]> writes:

> Thomas Christensen said:
>> I was wondering if anybody could give me a pointer to how to catch
>> a paste (non-clipboard primarily), in a textview.
>
> I'm guessing that you want to catch a paste and redirect it to some
> other destination, which will eventually modify the textbuffer on
> its own.  But the

I want to fetch text pasted onto the (read-only) GtkTextView and
insert it into a GtkEntry.  It's kind of a TELNET program, with a
output window (GtkTextView) and a input window (GtkEntry).

> "non-clipboard primarily" makes me wonder if perhaps you mean "i
> just want to know whenever the buffer is modified"; if that's the
> case, you can just listen to the "changed" or "insert-text" signals
> on the TextBuffer.

Nope, it will not get modified by the paste.

>> I have tried the 'clipboard-paste' signal but is doesn't get
>> triggered on a plain (middle-mouse) paste, and it's not applicable
>> anyways since my textview is editable => 0.
>
> The selections are different; middle button paste is the PRIMARY,
> while Ctrl+V is CLIPBOARD.  Inside GtkTextView, the
> 'paste-clipboard' signal handles the CLIPBOARD selection, and the
> 'button-press-event' handler fetches and inserts the PRIMARY
> selection when button==2.

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

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.

> Both of these eventually wind up down in
> gtk_text_buffer_insert_interactive() with default_editable==FALSE
> (since you've set your treeview to non-editable) and thus do
> nothing.

Can I somehow hook into this code?

>> Any hints would be well received.
>
> The paste-clipboard and button-press-event signals fire regardless
> of whether the view is editable, so you can still hook up to them
> and do stuff.

Yes, I now hook into 'button-press-event', but need to fetch the
pasted string.

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

Thomas

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Non-editable textview paste

muppet-6

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

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list