Re: How to implement custom interfaces in derived TreeModelSort ?



On 29.05.20 12:16, Gaëtan Frenoy wrote:
Inspired by what is described in Glib::Object::Subclass[1], I tried the
following:

---------------------------------------------------------------------------------------

package MyTreeModelSort;

use strict;
use Gtk3 -init;

use Glib::Object::Subclass
     'Gtk3::TreeModelSort',
     interfaces => [ 'Gtk3::TreeDragDest' ];

sub DRAG_DATA_RECEIVED {
     my ($dest_path, $selection_data) = @_;
     print("drag data received : $dest_path ; $selection_data ;\n");
     return 1;
}

sub ROW_DROP_POSSIBLE {
     my ($dest_path, $selection_data) = @_;
     print("row drop possible : $dest_path ; $selection_data ;\n");
     return 1;
}
---------------------------------------------------------------------------------------


But my custom methods are never called so there's something I do not do
right ; for sure.  Any tip or suggestion will be warmly welcome.

Yes, that's the right approach.  Maybe you forgot to call
enable_model_drag_source or enable_model_drag_dest?  The following code,
adapting from an old thread on this list*, works for me:

---
#!/usr/bin/perl

use strict;
use warnings;
use Gtk3 -init;

my $window = Gtk3::Window->new('toplevel');

{
    package MyTreeModelSort;
    use Glib::Object::Subclass
        Gtk3::TreeModelSort::,
        interfaces => [ Gtk3::TreeDragDest:: ],
        ;

    sub new
    {
        my ($proto, $childModel) = @_;
        my $class = ref($proto) || $proto;
        return Glib::Object::new($class, model => $childModel);
    }
    sub DRAG_DATA_RECEIVED
    {
        print "DRAG_DATA_RECEIVED\n";
    }
    sub ROW_DROP_POSSIBLE
    {
        print "ROW_DROP_POSSIBLE\n";
    }
}

my $model = Gtk3::TreeStore->new('Glib::String');
my $sorter = MyTreeModelSort->new($model);
my $list = Gtk3::TreeView->new_with_model($sorter);
my $column = Gtk3::TreeViewColumn->new_with_attributes(
  'Title', Gtk3::CellRendererText->new, 'text' => 0);
$list->append_column($column);

foreach ('a'..'z')
{
    my $iter = $model->append(undef);
    $model->set($iter, 0 => 'line'.$_);
}

my $targets = [Gtk3::TargetEntry->new('GTK_TREE_MODEL_ROW',
['same-widget'], 0)];
$list->enable_model_drag_source('button1-mask', $targets, 'move');
$list->enable_model_drag_dest($targets, 'move');

$window->add($list);
$window->signal_connect(destroy => sub { Gtk3->main_quit; });
$window->show_all;
Gtk3->main;
---

-Torsten

* <https://mail.gnome.org/archives/gtk-perl-list/2007-March/msg00011.html>



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