| 1 | # really should have a way to say what kinds of models are compatible | ||
| 2 | # - same num columns | ||
| 3 | # - same class, or subclass | ||
| 4 | # drop text to one column ... | ||
| 5 | |||
| 6 | |||
| 7 | # Copyright 2008, 2009, 2010 Kevin Ryde | ||
| 8 | |||
| 9 | # This file is part of Chart. | ||
| 10 | # | ||
| 11 | # Chart is free software; you can redistribute it and/or modify | ||
| 12 | # it under the terms of the GNU General Public License as published by the | ||
| 13 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 14 | # version. | ||
| 15 | # | ||
| 16 | # Chart is distributed in the hope that it will be useful, but | ||
| 17 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 18 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 19 | # for more details. | ||
| 20 | # | ||
| 21 | # You should have received a copy of the GNU General Public License along | ||
| 22 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 23 | |||
| 24 | package App::Chart::Gtk2::Ex::ListStore::DragByCopy; | ||
| 25 | use 5.008; | ||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | # uncomment this to run the ### lines | ||
| 30 | #use Smart::Comments; | ||
| 31 | |||
| 32 | |||
| 33 | #------------------------------------------------------------------------------ | ||
| 34 | # drag source | ||
| 35 | |||
| 36 | # gtk_tree_drag_source_row_draggable ($self, $src_path) | ||
| 37 | sub ROW_DRAGGABLE { | ||
| 38 | my ($self, $src_path) = @_; | ||
| 39 | ### DragByCopy ROW_DRAGGABLE | ||
| 40 | ### path: $src_path->to_string | ||
| 41 | ### result per get_iter(): $self->get_iter($src_path) | ||
| 42 | |||
| 43 | return $self->get_iter ($src_path); | ||
| 44 | } | ||
| 45 | |||
| 46 | # gtk_tree_drag_source_drag_data_get ($self, $src_path, $sel) | ||
| 47 | sub DRAG_DATA_GET { | ||
| 48 | my ($self, $src_path, $sel) = @_; | ||
| 49 | ### DragByCopy DRAG_DATA_GET | ||
| 50 | ### src_path: $src_path->to_string | ||
| 51 | ### sel type: $sel->type->name | ||
| 52 | |||
| 53 | my $ret = $sel->set_row_drag_data ($self, $src_path); | ||
| 54 | ### result: $ret | ||
| 55 | return $ret; | ||
| 56 | } | ||
| 57 | |||
| 58 | # gtk_tree_drag_source_drag_data_delete ($self, $src_path) | ||
| 59 | sub DRAG_DATA_DELETE { | ||
| 60 | my ($self, $src_path) = @_; | ||
| 61 | ### DragByCopy DRAG_DATA_DELETE | ||
| 62 | ### path: $src_path->to_string | ||
| 63 | |||
| 64 | my $iter = $self->get_iter ($src_path) || do { | ||
| 65 | ### no, get_iter() returns undef | ||
| 66 | return 0; | ||
| 67 | }; | ||
| 68 | # supposed to be rugged against dodginess, so protect here with an eval | ||
| 69 | eval { $self->remove ($iter); 1 } || do { | ||
| 70 | ### no, error from source remove(): $@ | ||
| 71 | return 0; | ||
| 72 | }; | ||
| 73 | ### yes | ||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | #------------------------------------------------------------------------------ | ||
| 79 | # drag dest | ||
| 80 | |||
| 81 | # gtk_tree_drag_dest_row_drop_possible | ||
| 82 | # | ||
| 83 | sub ROW_DROP_POSSIBLE { | ||
| 84 | my ($self, $dst_path, $sel) = @_; | ||
| 85 | ### DragByCopy ROW_DROP_POSSIBLE | ||
| 86 | ### to path: $dst_path->to_string | ||
| 87 | ### type: $sel->type->name | ||
| 88 | |||
| 89 | if ($dst_path->get_depth != 1) { | ||
| 90 | ### no, dest path depth: $dst_path->get_depth | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | my ($src_model, $src_path) = $sel->get_row_drag_data | ||
| 94 | or do { | ||
| 95 | ### no, source data not a row | ||
| 96 | return 0; | ||
| 97 | }; | ||
| 98 | |||
| 99 | my $n_columns = $self->get_n_columns; | ||
| 100 | if ($src_model->get_n_columns != $n_columns) { | ||
| 101 | ### no, different number of columns | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | ### yes | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | # gtk_tree_drag_dest_drag_data_received | ||
| 110 | # | ||
| 111 | sub DRAG_DATA_RECEIVED { | ||
| 112 | my ($self, $dst_path, $sel) = @_; | ||
| 113 | ### DragByCopy DRAG_DATA_RECEIVED | ||
| 114 | ### to path: $dst_path->to_string | ||
| 115 | ### type: $sel->type->name | ||
| 116 | ### src model: ($sel->get_row_drag_data)[0] | ||
| 117 | ### src path : (($sel->type->name eq 'GTK_TREE_MODEL_ROW') && ($sel->get_row_drag_data)[1]->to_string) | ||
| 118 | |||
| 119 | if ($dst_path->get_depth != 1) { | ||
| 120 | ### no, dest path depth: $dst_path->get_depth | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | my ($dst_index) = $dst_path->get_indices; | ||
| 124 | |||
| 125 | my ($src_model, $src_path) = $sel->get_row_drag_data | ||
| 126 | or do { | ||
| 127 | ### no, source data not a row | ||
| 128 | return 0; | ||
| 129 | }; | ||
| 130 | my $src_iter = $src_model->get_iter ($src_path) || do { | ||
| 131 | ### no, source get_iter() undef | ||
| 132 | return 0; | ||
| 133 | }; | ||
| 134 | |||
| 135 | # interleaved list (0, 'value0', 1, 'value1', ...) of columns and values | ||
| 136 | my @row = map {; ($_, $src_model->get_value($src_iter,$_)) } | ||
| 137 | 0 .. $src_model->get_n_columns - 1; | ||
| 138 | |||
| 139 | # drag is supposed to be rugged against dodgy stuff, so protect here with | ||
| 140 | # an eval | ||
| 141 | eval { $self->insert_with_values ($dst_index, @row); 1 } or do { | ||
| 142 | ### no, error from insert_with_values(): $@ | ||
| 143 | return 0; | ||
| 144 | }; | ||
| 145 | ### yes | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | __END__ |