File: EntryCompletion.pm

package info (click to toggle)
gscan2pdf 2.13.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,700 kB
  • sloc: perl: 22,713; xml: 81; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 2,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Gscan2pdf::EntryCompletion;

use strict;
use warnings;
use Gtk3;
use Glib qw(TRUE FALSE);    # To get TRUE and FALSE

BEGIN {
    use Exporter ();
    our ( $VERSION, @EXPORT_OK, %EXPORT_TAGS );

    $VERSION = '2.13.5';

    use base qw(Exporter Gtk3::Entry);
    %EXPORT_TAGS = ();    # eg: TAG => [ qw!name1 name2! ],

    # your exported package globals go here,
    # as well as any optionally exported functions
    @EXPORT_OK = qw();
}

sub new {
    my ( $class, $text, $suggestions ) = @_;
    my $self       = Gtk3::Entry->new;
    my $completion = Gtk3::EntryCompletion->new;
    $completion->set_inline_completion(TRUE);
    $completion->set_text_column(0);
    $self->set_completion($completion);
    my $model = Gtk3::ListStore->new('Glib::String');
    $completion->set_model($model);
    $self->set_activates_default(TRUE);
    bless $self, $class;
    if ( defined $text )        { $self->set_text($text) }
    if ( defined $suggestions ) { $self->add_to_suggestions($suggestions) }
    return $self;
}

sub get_suggestions {
    my ($self) = @_;
    my $completion = $self->get_completion;
    my @suggestions;
    $completion->get_model->foreach(
        sub {
            my ( $model, $path, $iter ) = @_;
            my $suggestion = $model->get( $iter, 0 );
            push @suggestions, $suggestion;
            return FALSE;    # FALSE=continue
        }
    );
    return \@suggestions;
}

sub add_to_suggestions {
    my ( $self, $suggestions ) = @_;
    my $completion = $self->get_completion;
    my $model      = $completion->get_model;
    for my $text ( @{$suggestions} ) {
        my $flag = FALSE;
        $model->foreach(
            sub {
                ( $model, my $path, my $iter ) = @_;
                my $suggestion = $model->get( $iter, 0 );
                if ( $suggestion eq $text ) { $flag = TRUE }
                return $flag;    # FALSE=continue
            }
        );
        if ( not $flag ) {
            $model->set( $model->append, 0, $text );
        }
    }
    return;
}

1;

__END__