| 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__ | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | App::Chart::Gtk2::Ex::ListStore::DragByCopy -- drag and drop copying between ListStores | ||
| 155 | |||
| 156 | =head1 SYNOPSIS | ||
| 157 | |||
| 158 | package MyStore; | ||
| 159 | use Gtk2; | ||
| 160 | use base 'App::Chart::Gtk2::Ex::ListStore::DragByCopy'; | ||
| 161 | use Glib::Object::Subclass | ||
| 162 | 'Gtk2::ListStore', | ||
| 163 | interfaces => [ 'Gtk2::TreeDragSource', | ||
| 164 | 'Gtk2::TreeDragDest' ]; | ||
| 165 | |||
| 166 | =head1 DESCRIPTION | ||
| 167 | |||
| 168 | C<App::Chart::Gtk2::Ex::ListStore::DragByCopy> is designed as a | ||
| 169 | multi-inheritance mix-in for Perl sub-classes of C<Gtk2::ListStore>. It | ||
| 170 | provides versions of the following methods which allow drag-and-drop between | ||
| 171 | different models. | ||
| 172 | |||
| 173 | ROW_DRAGGABLE | ||
| 174 | DRAG_DATA_GET | ||
| 175 | DRAG_DATA_DELETE | ||
| 176 | |||
| 177 | ROW_DROP_POSSIBLE | ||
| 178 | DRAG_DATA_RECEIVED | ||
| 179 | |||
| 180 | Normally ListStore restricts drag and drop to re-ordering rows within a | ||
| 181 | single model. With this mix-in rows can be copied to or from any compatible | ||
| 182 | model, though still only within the same running program. | ||
| 183 | |||
| 184 | =head1 FUNCTIONS | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item C<< $bool = ROW_DRAGGABLE ($liststore, $src_path) >> | ||
| 189 | |||
| 190 | =item C<< $bool = DRAG_DATA_GET ($liststore, $src_path, $selection) >> | ||
| 191 | |||
| 192 | =item C<< $bool = DRAG_DATA_DELETE ($liststore, $src_path) >> | ||
| 193 | |||
| 194 | The drag methods offer a row as data and delete it with C<remove> when | ||
| 195 | dragged. | ||
| 196 | |||
| 197 | If you want to impose extra conditions on dragging you can write your own | ||
| 198 | versions of these functions and chain up. For example if only the first | ||
| 199 | three rows of the model are draggable then | ||
| 200 | |||
| 201 | sub ROW_DRAGGABLE { | ||
| 202 | my ($self, $path) = @_; | ||
| 203 | my ($index) = $path->get_indices; | ||
| 204 | if ($index >= 3) { return 0; } # not draggable | ||
| 205 | return $self->SUPER::ROW_DRAGGABLE ($path); | ||
| 206 | } | ||
| 207 | |||
| 208 | =item C<< $bool = ROW_DROP_POSSIBLE ($liststore, $dst_path, $selection) >> | ||
| 209 | |||
| 210 | =item C<< $bool = DRAG_DATA_RECEIVED ($self, $dst_path, $selection) >> | ||
| 211 | |||
| 212 | The drop methods accept a row from any TreeModel. They get the row data | ||
| 213 | with C<< $src->get >> and store it with C<< $dst->insert_with_values >>. | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =head1 SEE ALSO | ||
| 218 | |||
| 219 | L<Gtk2::ListStore> | ||
| 220 | |||
| 221 | =cut |