| 1 | # Copyright 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it | ||
| 6 | # and/or modify it under the terms of the GNU General Public License as | ||
| 7 | # published by the Free Software Foundation; either version 3, or (at your | ||
| 8 | # option) any later version. | ||
| 9 | # | ||
| 10 | # Chart is distributed in the hope that it will be | ||
| 11 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 13 | # Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Chart. If not, see | ||
| 17 | # <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | |||
| 20 | package App::Chart::Gtk2::Ex::CellView::TreeDND; | ||
| 21 | use 5.008; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use Carp; | ||
| 25 | |||
| 26 | # uncomment this to run the ### lines | ||
| 27 | #use Smart::Comments; | ||
| 28 | |||
| 29 | sub drag_dest_append_target_list { | ||
| 30 | my $widget = shift; | ||
| 31 | $widget->set_target_list ($widget->get_target_list, @_); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub drag_dest_init { | ||
| 35 | my ($self) = @_; | ||
| 36 | ### CellView-TreeDND drag_dest_init() | ||
| 37 | |||
| 38 | $self->drag_dest_set(['highlight'], ['copy','move'], | ||
| 39 | { target => "text/plain", | ||
| 40 | flags => [] }, | ||
| 41 | { target => 'GTK_TREE_MODEL_ROW', | ||
| 42 | flags => ['same-app'] }); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub drag_motion { | ||
| 46 | my ($self, $context, $x, $y, $time) = @_; | ||
| 47 | ### CellView-TreeDND drag_motion() | ||
| 48 | |||
| 49 | my $path = $self->get_displayed_row || do { | ||
| 50 | ### no path set | ||
| 51 | return 0; | ||
| 52 | }; | ||
| 53 | |||
| 54 | my $target = $self->drag_dest_find_target | ||
| 55 | ($context, $self->drag_dest_get_target_list) || do { | ||
| 56 | ### no matching target | ||
| 57 | return 0; | ||
| 58 | }; | ||
| 59 | |||
| 60 | if ($target->name eq 'GTK_TREE_MODEL_ROW') { | ||
| 61 | ### target model row, request data | ||
| 62 | $context->{'for_motion'} = 1; | ||
| 63 | $self->drag_get_data ($context, $target, $time); | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | delete $context->{'for_motion'}; | ||
| 68 | $context->status ($context->suggested_action, $time); | ||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub drag_drop { | ||
| 73 | my ($self, $context, $x, $y, $time) = @_; | ||
| 74 | ### CellView-TreeDND drag_drop() | ||
| 75 | delete $context->{'for_motion'}; | ||
| 76 | |||
| 77 | my $model = $self->get_model || do { | ||
| 78 | ### no model to drop on | ||
| 79 | return 0; | ||
| 80 | }; | ||
| 81 | $model->isa('Gtk2::TreeDragDest') || do { | ||
| 82 | ### model is not a TreeDragDest | ||
| 83 | return 0; | ||
| 84 | }; | ||
| 85 | my $path = $self->get_displayed_row || do { | ||
| 86 | ### no row displayed | ||
| 87 | return 0; | ||
| 88 | }; | ||
| 89 | |||
| 90 | my $target = $self->drag_dest_find_target | ||
| 91 | ($context, $self->drag_dest_get_target_list); | ||
| 92 | $self->drag_get_data ($context, $target, $time); | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub drag_data_received { | ||
| 97 | my ($self, $context, $x, $y, $sel, $info, $time) = @_; | ||
| 98 | ### CellView-TreeDND drag_data_received(): $context->{'for_motion'} | ||
| 99 | |||
| 100 | my ($model, $path); | ||
| 101 | my $good = (($model = $self->get_model) | ||
| 102 | && $model->isa('Gtk2::TreeDragDest') | ||
| 103 | && ($path = $self->get_displayed_row)); | ||
| 104 | |||
| 105 | if ($context->{'for_motion'}) { | ||
| 106 | $good &&= $model->row_drop_possible ($path, $sel); | ||
| 107 | ### for motion good: $good | ||
| 108 | $context->status (($good ? $context->suggested_action : 'default'), $time); | ||
| 109 | } else { | ||
| 110 | $good &&= $model->drag_data_received ($path, $sel); | ||
| 111 | ### received good: $good | ||
| 112 | $context->finish ($good, $good && ($context->action eq 'move'), $time); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ |