Re: paste the clipboard over gtktreeview



Thanks! Is exactly that I wanted :)



On Tue, May 27, 2008 at 3:43 AM, muppet <scott asofyet org> wrote:

On May 25, 2008, at 2:40 PM, anguila wrote:

How can I paste the clipboard (with Control + V) over a gtktreeview widget (list) ?
Any idea?

So far as i know, you create a key binding (e.g. with a menu item), and then in the handler for that command, pull data from the clipboard and do the appropriate list insertions.

Instead of spamming the message with lots of boilerplate code to set up the accelerators and all that, here's a bunch of GtkBuilder XML.  The interesting part is the on_paste() callback, near the end, which just grabs the clipboard contents as plain text, splits by whitespace, and inserts each item as its own line.  In a real app, you'd do more appropriate parsing and marshaling and whatnot.


#!/usr/bin/env perl

use strict;
use warnings;
use Glib ':constants';
use Gtk2 -init;
use Gtk2::SimpleList;

#
# Gtk2::Builder is cool.  Now we can express the shortcuts and menu items
# and all that as data instead of as tedious code.
#
my $interface = '<interface>
 <object class="GtkUIManager" id="uimanager">
   <child>
     <object class="GtkActionGroup" id="actions">
       <child>
         <object class="GtkAction" id="file-menu">
           <property name="label">_File</property>
         </object>
       </child>
       <child>
         <object class="GtkAction" id="quit">
           <property name="stock-id">gtk-quit</property>
           <signal name="activate" handler="on_quit" />
         </object>
       </child>
       <child>
         <object class="GtkAction" id="paste">
           <property name="stock-id">gtk-paste</property>
           <signal name="activate" handler="on_paste" />
         </object>
       </child>
       <child>
         <object class="GtkAction" id="clear">
           <property name="stock-id">gtk-delete</property>
           <signal name="activate" handler="on_clear" />
         </object>
       </child>
     </object>
   </child>
   <ui>
     <menubar name="menubar">
       <menu action="">          <menuitem action="">          <menuitem action="">          <menuitem action="">        </menu>
     </menubar>
   </ui>
 </object>
 <object class="GtkWindow" id="window">
   <property name="default-width">250</property>
   <property name="default-height">500</property>
   <signal name="destroy" handler="on_window_destroy" />
   <child>
     <object class="GtkVBox" id="vbox">
       <child>
         <object class="GtkMenu" constructor="uimanager" id="menubar" />
         <packing>
           <property name="expand">false</property>
         </packing>
       </child>
       <child>
         <object class="GtkScrolledWindow" id="scroller">
           <property name="hscrollbar-policy">automatic</property>
           <property name="vscrollbar-policy">automatic</property>
           <child>
             <object class="GtkTreeView" id="treeview">
               <property name="headers-visible">false</property>
             </object>
           </child>
         </object>
       </child>
     </object>
   </child>
 </object>
</interface>';


my $builder = Gtk2::Builder->new ();
$builder->add_from_string ($interface);
$builder->connect_signals ();

my $slist = Gtk2::SimpleList->new_from_treeview (
       $builder->get_object ('treeview'),
       Words => 'text',
);


$builder->get_object ('window')->show_all;

Gtk2->main;


sub on_window_destroy { Gtk2->main_quit }
sub on_quit { $builder->get_object ('window')->destroy; }

#
# Here's the interesting part.
#
sub on_paste {
   my $clipboard = Gtk2::Clipboard->get_for_display
               ($builder->get_object ('window')->get_display (),
                Gtk2::Gdk->SELECTION_PRIMARY);

   my $text = $clipboard->wait_for_text ();

   foreach (split /\s+/, $text) {
       push @{ $slist->{data} }, $_ if length;
   }
}

sub on_clear {
   @{ $slist->{data} } = ();
}

__END__


--
Zella, carrying a kite in a long plastic box:  It's like book!
Yvonne, carrying the wooden kite string spool:  It's like a wood!




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