Blocking signals



Because of a need for drag and drop, I have to keep a SimpleList manually sorted. However, I am having 
problems blocking the "row-changed" signal to prevent recursion.

What have I got wrong here?

#!/usr/bin/perl -w

use strict;
use Gtk2 -init;
use Gtk2::SimpleList;

my $win = Gtk2::Window->new;
$win->signal_connect (delete_event => sub { Gtk2->main_quit; });

my $vbox = Gtk2::VBox->new;
$win->add ($vbox);

my $button = Gtk2::Button->new('Add row');
$vbox->pack_start ($button, 1, 1, 0);

my $slist = Gtk2::SimpleList->new (
                        'Int Field'     => 'int',
                        'Text Field'    => 'text',
        );

$slist -> {signalid} = $slist -> get_model -> 
 signal_connect('row-changed' => sub {
  manual_sort_by_column ($slist, 0);
 });

$vbox->add ($slist);
$button->signal_connect (clicked => sub {
 $slist->signal_handler_block($slist -> {signalid});
 push @{$slist -> {data}},
  [$#{$slist -> {data}}+2, "test".($#{$slist -> {data}}+2)];
 $slist->signal_handler_unblock($slist -> {signalid});
});

$vbox->pack_start ($button, 1, 1, 0);

$win->show_all;
Gtk2->main;


sub compare_numeric_col { $_[0] <=> $_[1] }
sub compare_text_col { $_[0] cmp $_[1] }


sub manual_sort_by_column {
        my ($slist, $sortcol) = @_;

        my %sortfuncs = (
                'Glib::Scalar' => \&compare_text_col,
                'Glib::String' => \&compare_text_col,
                'Glib::Int' => \&compare_numeric_col,
                'Glib::Double' => \&compare_numeric_col,
        );

        my $sortfunc = $sortfuncs{$slist->get_model-> get_column_type($sortcol)};

        my @data = map { [ @$_ ] } @{ $slist->{data} };
        @data = sort { $sortfunc->($a->[$sortcol], $b->[$sortcol]) } @data;
        @{$slist->{data}} = @data;
}



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