| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-History. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-History is free software; you can redistribute it and/or modify it | ||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Gtk2-Ex-History is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-History. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | # The TreeDragDest handlers here allow row dragging between the back, | ||
| 20 | # forward and current lists as displayed in Gtk2::Ex::History::Dialog. | ||
| 21 | # | ||
| 22 | # Cf DragByCopy in the drop handler. | ||
| 23 | |||
| 24 | package Gtk2::Ex::History::ListStore; | ||
| 25 | use 5.008; | ||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | use List::Util; | ||
| 29 | use Gtk2; | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | #use Smart::Comments; | ||
| 33 | |||
| 34 | our $VERSION = 8; | ||
| 35 | |||
| 36 | use Glib::Object::Subclass | ||
| 37 | 'Gtk2::ListStore', | ||
| 38 | interfaces => [ 'Gtk2::TreeDragDest' ]; | ||
| 39 | |||
| 40 | use constant { COL_PLACE => 0 }; | ||
| 41 | |||
| 42 | sub INIT_INSTANCE { | ||
| 43 | my ($self) = @_; | ||
| 44 | $self->set_column_types ('Glib::Scalar'); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub ROW_DROP_POSSIBLE { | ||
| 48 | my ($self, $dst_path, $sel) = @_; | ||
| 49 | ### History ListStore ROW_DROP_POSSIBLE | ||
| 50 | |||
| 51 | $dst_path->get_depth == 1 or do { | ||
| 52 | ### no, depth: $dst_path->get_depth | ||
| 53 | return 0; | ||
| 54 | }; | ||
| 55 | my ($src_model, $src_path) = $sel->get_row_drag_data | ||
| 56 | or do { | ||
| 57 | ### no, source data not a row | ||
| 58 | return 0; | ||
| 59 | }; | ||
| 60 | |||
| 61 | ### others: map {defined} $self->{'others'} | ||
| 62 | ### others: @{$self->{'others'}} | ||
| 63 | unless (List::Util::first { $src_model == ($_||0) } | ||
| 64 | $self, @{$self->{'others'}}) { | ||
| 65 | ### no, source not self or other | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | ### yes | ||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | # insert_with_values() always for code here and in Dialog | ||
| 74 | BEGIN { | ||
| 75 | if (! Gtk2::ListStore->can('insert_with_values')) { | ||
| 76 | eval "#line ".(__LINE__+1)." \"".__FILE__."\"\n" . <<'HERE' or die; | ||
| 77 | sub insert_with_values { | ||
| 78 | my $self = shift; | ||
| 79 | my $pos = shift; | ||
| 80 | $self->set ($self->insert($pos), @_); | ||
| 81 | } | ||
| 82 | 1; | ||
| 83 | HERE | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub DRAG_DATA_RECEIVED { | ||
| 88 | my ($self, $dst_path, $sel) = @_; | ||
| 89 | $self->ROW_DROP_POSSIBLE ($dst_path, $sel) | ||
| 90 | or return 0; | ||
| 91 | |||
| 92 | my ($src_model, $src_path) = $sel->get_row_drag_data | ||
| 93 | or do { | ||
| 94 | ### no, source data not a row | ||
| 95 | return 0; | ||
| 96 | }; | ||
| 97 | my $src_iter = $src_model->get_iter ($src_path) || do { | ||
| 98 | ### oops, source row gone | ||
| 99 | return 0; | ||
| 100 | }; | ||
| 101 | my $place = $src_model->get($src_iter, 0); | ||
| 102 | |||
| 103 | if ($self->{'current'}) { | ||
| 104 | ### yes, by goto drop on current | ||
| 105 | $self->{'history'}->goto ($place); | ||
| 106 | } else { | ||
| 107 | ### yes, by insert | ||
| 108 | my ($dst_index) = $dst_path->get_indices; | ||
| 109 | $self->insert_with_values ($dst_index, COL_PLACE, $place); | ||
| 110 | } | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | __END__ |