1

I'm working on a list of items using Gtk2::SimpleList for the different fields. One of the fields should be a name, I would like to use Gtk2::EntryCompletion as the entry for the fields.

How can I add a new column type of Gtk2::EntryCompletion to a Gtk2::SimpleList?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
Kamil.Khoury
  • 210
  • 2
  • 9
  • You should be able to use `Gtk2::SimpleList->add_column_type()` and add a custom column type with a custom Gtk2::CellRenderer class, see [Gtk.Entry in Gtk.TreeView (CellRenderer)](https://stackoverflow.com/q/13756787/2173773) – Håkon Hægland Jul 15 '21 at 15:26

1 Answers1

0

Here is a basic example that seems to work for me. For more information, have a look at the example programs

package MyEntry;
use feature qw(say);
use strict;
use warnings;
use Glib qw/TRUE FALSE/;
use Gtk2;
use base qw(Gtk2::Entry);

use Glib::Object::Subclass Gtk2::Entry::, interfaces => [ Gtk2::CellEditable:: ];

sub set_text {
    my ( $self, $text) = @_;
    $self->get_buffer->set_text ($text);
}

sub START_EDITING { }
sub EDITING_DONE { }
sub REMOVE_WIDGET { }

package CellRendererEntry;
use feature qw(say);
use strict;
use warnings;

use Glib qw/TRUE FALSE/;
use Gtk2;
use Gtk2::Gdk::Keysyms;

use Glib::Object::Subclass
    "Gtk2::CellRendererText",
;

# Creates a tree model containing the completions
sub create_completion_model {
    my $store = Gtk2::ListStore->new (Glib::String::);
    my @words = qw(perl python java javascript c c++ ruby go haskell lisp php);
    for my $word (@words) {
        $store->set ($store->append, 0, $word);
    }
    return $store;
}

sub START_EDITING {
    my ($cell, $event, $view, $path, $background_area, $cell_area, $flags) = @_;

    my $entry = MyEntry->new;
    $entry->set_text ($cell->get('text'));
    $entry->grab_focus;
    my $completion = Gtk2::EntryCompletion->new;
    $entry->set_completion ($completion);
    $completion->set_model (create_completion_model ());
    $completion->set_text_column (0);
    $entry->signal_connect (key_press_event => sub {
          my ($widget, $event) = @_;
          if ($event->keyval == $Gtk2::Gdk::Keysyms{Return}) {
              $cell->signal_emit (edited => $path, $entry->get_text);
              $entry->destroy;
              return TRUE;
          }
          return FALSE;
     });
    # Make sure the entry has the correct height.  On some versions of
    # gtk+, the entry would otherwise be just a few pixels tall.
    $entry->set (height_request => $cell_area->height);
    $entry->show;
    return $entry;
}

package main;
use feature qw(say);
use strict;
use warnings;

use Data::Printer;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';
use Gtk2::SimpleList;

my $window = Gtk2::Window->new('toplevel');
$window->set_title('SimpleList test');
$window->set_position('center');
$window->set_default_size(450, 350);
$window->signal_connect(
    'delete-event' =>
    sub {
        Gtk2->main_quit();
        exit;
    });

my $vb = Gtk2::VBox->new(0, 6);
$window->add($vb);

my $sw = Gtk2::ScrolledWindow->new;
$sw->set_policy (qw/automatic automatic/);
$vb->pack_start($sw, 1, 1, 0);

Gtk2::SimpleList->add_column_type(
    'entry_completion',
    type     => 'Glib::String',
    renderer => 'CellRendererEntry',
    attr     => 'text',
);

my $list = Gtk2::SimpleList->new(
    'Text Field'    => 'entry_completion',
    'Int Field'     => 'int',
    'Double Field'  => 'double',
    'Bool Field'    => 'bool',
);
@{$list->{data}} = (
        [ 'text', 1, 1.1,  TRUE ],
        [ 'text', 2, 2.2, FALSE ],
    );
$list->set_column_editable (0, TRUE);
$sw->add($list);
my $quitbtn = Gtk2::Button->new_from_stock('gtk-quit');
$quitbtn->signal_connect( clicked => sub { Gtk2->main_quit; 1 } );
$vb->pack_start($quitbtn, 0, 0, 0);
$window->show_all();
Gtk2->main();
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174