brilant_blue has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

I wrote following code (filename is clipboard.pl):

#!/usr/bin/env perl use strict; use warnings; use diagnostics; use feature qw(say); use utf8; use Gtk3; use Readonly; Readonly my @LINKS => ( { url => 'http://www.seznam.cz/', title => 'Seznam.cz', }, { url => 'http://www.google.cz/', title => 'Google', }, { url => 'http://www.next-url.cz/', title => 'Next URL', }, ); # Links to display Readonly my %CONST => ( WINDOW_TITLE => 'Clipboard', WINDOW_BORDER_WIDTH => 10, WINDOW_WIDTH => 500, WINDOW_HEIGHT => 300, SPACING => 10, # All these numbers are in pixels ); # Some constants for better understanding Gtk3->init; # Initialize Gtk3 my $window = Gtk3::Window->new('toplevel'); my $scrolledwin = Gtk3::ScrolledWindow->new(undef, undef); my $pressed_ctrl = 0; my $store = Gtk3::ListStore->new('Glib::String', 'Glib::String' +); my $view = Gtk3::TreeView->new; my $renderer = Gtk3::CellRendererText->new; my $vbox_main = Gtk3::Box->new('vertical', $CONST{SPACING}); my $hbox_bla = Gtk3::Box->new('horizontal', $CONST{SPACING}); my $button_copy = Gtk3::Button->new; my $column; my $iter; for my $item (@LINKS) { $iter = $store->append; $store->set($iter, 0 => $item->{title}, 1 => $item->{url}); } $view->set_model($store); $column = Gtk3::TreeViewColumn->new_with_attributes( 'Title of page', $renderer, text => 0 ); $view->append_column($column); $column = Gtk3::TreeViewColumn->new_with_attributes( 'URL of page', $renderer, text => 1 ); $view->append_column($column); $scrolledwin->add($view); $button_copy->set_label('Copy URL to clipboard'); $button_copy->signal_connect(clicked => sub { my $selection = $view->get_selection; my @selpaths = $selection->get_selected_rows; for my $tpath ($selpaths[0]) { say '$tpath --> ', $tpath; for my $tp (@$tpath) { say '$tp --> ', $tp; my $iter = $store->get_iter($tp); my $col0content = $store->get($iter, 0); my $col1content = $store->get($iter, 1); say '$col0content --> ', $col0content; say '$col1content --> ', $col1content; my $clipboard = Gtk3::Clipboard->get; # XXX $clipboard->set_text($col1content); } } return; }); $vbox_main->pack_start($scrolledwin, 1, 1, 0); $vbox_main->pack_start($hbox_bla, 0, 0, 0); $hbox_bla->pack_start($button_copy, 0, 0, 0); $window->set_title($CONST{WINDOW_TITLE}); $window->set_border_width($CONST{WINDOW_BORDER_WIDTH}); $window->set_default_size($CONST{WINDOW_WIDTH}, $CONST{WINDOW_HEIGHT}) +; $window->signal_connect(destroy => sub { Gtk3->main_quit; return; }); $window->signal_connect(key_press_event => sub { my $self = shift; my $event = shift; $event = $event->keyval; if ($event == Gtk3::Gdk::KEY_Control_L or $event == Gtk3::Gdk::KEY_Control_R ) { $pressed_ctrl = 1; return; } if ($event == Gtk3::Gdk::KEY_w or $event == Gtk3::Gdk::KEY_q ) { if ($pressed_ctrl) { Gtk3->main_quit; return; } return; } return; }); $window->signal_connect(key_release_event => sub { my $self = shift; my $event = shift; $event = $event->keyval; if ($event == Gtk3::Gdk::KEY_Control_L or $event == Gtk3::Gdk::KEY_Control_R ) { $pressed_ctrl = 0; return; } return; }); $window->add($vbox_main); $window->show_all; Gtk3->main; # Gtk3 main loop

Output is:

$ perl clipboard.pl $tpath --> ARRAY(0x9f46000) $tp --> Gtk3::TreePath=SCALAR(0x9f45ff0) $col0content --> Seznam.cz $col1content --> http://www.seznam.cz/ *** unhandled exception in callback: *** Cannot convert scalar a4a8bf0 to an object of type Gtk3::Gdk::At +om at clipboard.pl line 86. *** ignoring at /usr/share/perl5/vendor_perl/Gtk3.pm line 321.

Problem, as you can see in output, is at line 86, marked by '# XXX' at the end of the line.

What can I do if I want to copy a text in $col1content to clipboard?

Replies are listed 'Best First'.
Re: Gtk3::Clipboard problem
by Corion (Patriarch) on Sep 01, 2014 at 10:20 UTC

    At least from my vague reading of the documentation in its original Klingon, it seems that ->clipboard_get() is supposed to be called on an existing object and not on a class method.

    I haven't done enough Gtk3 programming to know what the customary object would be on which one would call the method though.

    Update: Grepping the distribution for "clipboard", I find t/zz-GtkTextBuffer.t, which might or might not have relevant usage:

    SKIP: { skip 'clipboard stuff; missing annotations', 0 unless Gtk3::CHECK_VERSION (3, 2, 0); my $clipboard = Gtk3::Clipboard::get(Gtk3::Gdk::Atom::intern('clipbo +ard', Glib::FALSE)); $buffer -> paste_clipboard($clipboard, $buffer -> get_end_iter(), TR +UE); $buffer -> paste_clipboard($clipboard, undef, TRUE); $buffer -> copy_clipboard($clipboard); $buffer -> cut_clipboard($clipboard, TRUE); $buffer -> add_selection_clipboard($clipboard); $buffer -> remove_selection_clipboard($clipboard); }
Re: Gtk3::Clipboard problem
by Anonymous Monk on Sep 01, 2014 at 10:20 UTC
Re: Gtk3::Clipboard problem
by brilant_blue (Beadle) on Sep 01, 2014 at 11:06 UTC

    Ok, I replaced line marked by '# XXX' with this:

    my $clipboard = Gtk3::Clipboard::get(Gtk3::Gdk::Atom::intern('clipboard', 0));

    And the next line with this:

    $clipboard->set_text($col1content, length $col1content);

    Now I don't get any error but the clipboard is still not working.

      Now I don't get any error but the clipboard is still not working.

      What does that mean "not working"?

      What happens when you run the test t/zz-GtkTextBuffer.t (does it pass, is the clipboard section skipped)?

      gtk-text-buffer-set-text shows text/length just like gtk-clipboard-set-text but zz-GtkTextBuffer.t shows set_text without a length argument, so

      What happens if you use  set_text($col1content);?

        I click the button 'Copy URL to clipboard' and then I try to paste copied text in text editor--nothing happen.

        This is output of test:

        1..44 ok 1 - An object of class 'Gtk3::TextBuffer' isa 'Gtk3::TextBuffer' ok 2 ok 3 - An object of class 'Gtk3::TextBuffer' isa 'Gtk3::TextBuffer' ok 4 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 5 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 6 ok 7 ok 8 ok 9 ok 10 ok 11 ok 12 ok 13 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 14 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 15 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 16 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 17 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 18 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 19 ok 20 ok 21 ok 22 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 23 - An object of class 'Gtk3::TextChildAnchor' isa 'Gtk3::TextChil +dAnchor' ok 24 - An object of class 'Gtk3::TextMark' isa 'Gtk3::TextMark' ok 25 ok 26 - An object of class 'Gtk3::TextIter' isa 'Gtk3::TextIter' ok 27 - An object of class 'Gtk3::TextMark' isa 'Gtk3::TextMark' ok 28 - An object of class 'Gtk3::TextMark' isa 'Gtk3::TextMark' ok 29 ok 30 ok 31 - An object of class 'Gtk3::TextTag' isa 'Gtk3::TextTag' ok 32 ok 33 ok 34 - An object of class 'Gtk3::TextTag' isa 'Gtk3::TextTag' ok 35 - An object of class 'Gtk3::TextTag' isa 'Gtk3::TextTag' ok 36 ok 37 - An object of class 'Gtk3::TargetList' isa 'Gtk3::TargetList' ok 38 - An object of class 'Gtk3::TargetList' isa 'Gtk3::TargetList' ok 39 - An object of class 'Gtk3::TargetList' isa 'Gtk3::TargetList' ok 40 - An object of class 'Gtk3::TargetList' isa 'Gtk3::TargetList' ok 41 ok 42 ok 43 ok 44

        If I replace line $clipboard->set_text($col1content, length $col1content); with $clipboard->set_text($col1content); I get an error message:

        $ perl clipboard.pl $tpath --> ARRAY(0x9e5e740) $tp --> Gtk3::TreePath=SCALAR(0x9e5e730) $col0content --> Google $col1content --> http://www.google.cz/ $clipboard --> Gtk3::Clipboard=HASH(0xa517230) *** unhandled exception in callback: *** Gtk3::Clipboard::set_text: passed too few parameters (expected 3 +, got 2) at clipboard.pl line 91. *** ignoring at /usr/share/perl5/vendor_perl/Gtk3.pm line 321. $