use strict;
use Glib::Object::Introspection;
use Gtk3 -init;

# Window
my $window = Gtk3::Window->new();
$window->set_size_request(300, 200);
$window->signal_connect('delete_event' => sub {
    Gtk3->main_quit();
});

# TreeView
my $tree = Gtk3::TreeView->new();
$window->add($tree);

# Standard TreeStore
my $model = Gtk3::TreeStore->new(['Glib::String']);
for (my $i=0; $i < 5; $i++) {
    $model->insert_with_values(undef, -1, 0 => "Item $i");
}
# Sorted TreeStore
my $model_sort = Gtk3::TreeModelSort->new_with_model($model);

## Use store
# If using the standard TreeStore, drag_data_received and drag_data_get are correctly raised
# $tree->set_model($model);

# If using the sorted TreeStore, drag_data events are *NOT* raised
$tree->set_model($model_sort);

# Build columns
my $colA = Gtk3::TreeViewColumn->new_with_attributes('ColA', Gtk3::CellRendererText->new(), 'markup', 0);
$tree->append_column($colA);

# Setup DnD
my $target_entry = Gtk3::TargetEntry->new('text/plain', ${Gtk3::TargetFlags->new(qw/same-widget/)}, 0);
$tree->enable_model_drag_source('button1-mask', [ $target_entry ], [ 'default', 'move' ]);
$tree->enable_model_drag_dest([ $target_entry ], [ 'default', 'move' ]);

# Handle DnD events
$tree->signal_connect('drag_begin' => sub {
    my ($tree, $context) = @_;
    print("Drap begin\n");
    return $context;
});
$tree->signal_connect('drag_data_received' => sub {
    my ($tree, $context, $x, $y, $data) = @_;
    print("Drag data received : $tree ; $context ; $x ; $y ; $data\n");
    return 1;
});

$tree->signal_connect('drag_data_get' => sub {
    my ($tree, $context, $data) = @_;
    print("Drag data get : $tree ; $context ; $data\n");
    return 1;
});

$window->show_all();
Gtk3->main();
