package PhD::CellRendererTextCompletion ;

use strict ;
use diagnostics ;
use constant DEBUG => 0 ;

use Data::Dumper ;

use File::Spec ;

use Glib qw/TRUE FALSE/;
use Gtk2 ;
use Gtk2::GladeXML;
use Gtk2::Ex::Simple::List ;


use Glib::Object::Subclass
	Gtk2::CellRendererText::,
	properties => [
		Glib::ParamSpec->boxed ('list', 'List',
		                        'List of possible values',
					'Glib::Scalar',
				        [qw/readable writable/]),
	],
	;

sub set_completion_model
{
    my ($cell,$completion_model) =@_ ;
    $cell->{completion_model} = $completion_model ;
}

sub get_completion_model
{
    my ($cell) =@_ ;
    return $cell->{completion_model} ;
}

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

    my $editable = $cell->Gtk2::CellRendererText::START_EDITING
	($event, $view, $path, $background_area, $cell_area, $flags) ;

#    $editable->set_width_chars(15) ;

    my $tra_module = PhD::TBL->new('names','tra_module.tbl') ;
    warn "Cannot load tra_module.tbl\n" if (!defined($tra_module)) ;

    my $completion = Gtk2::EntryCompletion->new() ;
    $completion->set_model($cell->get_completion_model()) ;
    $completion->set_text_column(0) ;
    $completion->set_match_func(\&match_name) ;
    $editable->set_completion($completion) ;

    return $editable ;
}

sub match_name
{
    # For  each  line in  the  pulldown  menu,  look which  names
    # matches the regexp typed by the user in the filter
    my ($ent_comp,$key,$iter) = @_ ;
    my $ret = FALSE ;
    my $model = $ent_comp->get_model() ;
    my $name = $model->get($iter) ;
    $ret = TRUE if ($name eq '') ;
    $ret = TRUE if ($name =~ m/$key/i) ;
    warn "name [$name] matches key [$key] : ret [$ret]\n" if (DEBUG>= 4) ;
    return $ret ;
}

Gtk2::Ex::Simple::List->add_column_type
    ('completion' =>
     (type=>'Glib::String',
      renderer=>'PhD::CellRendererTextCompletion',
      attr=> 'text')) ;

1 ;
