| 1 | # Copyright 2008, 2009, 2010, 2011, 2013 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | |||
| 18 | package App::Chart::Gtk2::Ex::CellRendererTextBits; | ||
| 19 | use 5.008; | ||
| 20 | use Carp; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | # uncomment this to run the ### lines | ||
| 25 | #use Smart::Comments; | ||
| 26 | |||
| 27 | sub renderer_edited_set_value { | ||
| 28 | my ($renderer, $dest, $column_num) = @_; | ||
| 29 | defined $column_num or croak 'No column number supplied'; | ||
| 30 | my @userdata = ($dest, $column_num); | ||
| 31 | require Scalar::Util; | ||
| 32 | Scalar::Util::weaken ($userdata[0]); | ||
| 33 | $renderer->signal_connect (edited => \&_renderer_edited_set_value_handler, | ||
| 34 | \@userdata); | ||
| 35 | } | ||
| 36 | sub _renderer_edited_set_value_handler { | ||
| 37 | my ($renderer, $pathstr, $newtext, $userdata) = @_; | ||
| 38 | my ($dest, $column_num) = @$userdata; | ||
| 39 | |||
| 40 | if ($dest->can('get_tree_view')) { | ||
| 41 | # on Gtk2::TreeViewColumn go to the Gtk2::TreeView | ||
| 42 | $dest = $dest->get_tree_view || croak 'No viewer from get_tree_view'; | ||
| 43 | } | ||
| 44 | if (my $pspec = $dest->find_property('model')) { | ||
| 45 | if ($pspec->get_value_type eq 'Gtk2::TreeModel') { | ||
| 46 | # on Gtk2::TreeView, or Gtk2::CellView, etc, go to the Gtk2::TreeModel | ||
| 47 | $dest = $dest->get('model') | ||
| 48 | || croak 'No model from get_model'; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | my $path = Gtk2::TreePath->new_from_string ($pathstr); | ||
| 52 | my $iter = $dest->get_iter ($path) | ||
| 53 | || croak "Path $pathstr not found in model"; | ||
| 54 | ### renderer_edited_set_value() set_value "path=$pathstr col=$column_num" | ||
| 55 | $dest->set_value ($iter, $column_num, $newtext); | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | __END__ | ||
| 60 | |||
| 61 | =for stopwords Ryde Chart | ||
| 62 | |||
| 63 | =head1 NAME | ||
| 64 | |||
| 65 | App::Chart::Gtk2::Ex::CellRendererTextBits -- helpers for Gtk2::CellRendererText objects | ||
| 66 | |||
| 67 | =head1 SYNOPSIS | ||
| 68 | |||
| 69 | use App::Chart::Gtk2::Ex::CellRendererTextBits; | ||
| 70 | |||
| 71 | =head1 FUNCTIONS | ||
| 72 | |||
| 73 | =over 4 | ||
| 74 | |||
| 75 | =item C<< App::Chart::Gtk2::Ex::CellRendererTextBits::renderer_edited_set_value ($renderer, $view_or_model, $column_num) >> | ||
| 76 | |||
| 77 | Setup C<$renderer> so that when edited the new value is stored to the given | ||
| 78 | view or model C<$column_num> using a C<set_value()> such as ListStore and | ||
| 79 | TreeStore implement. | ||
| 80 | |||
| 81 | The underlying model doesn't have to be a ListStore or TreeStore, anything | ||
| 82 | with a C<set_value()> is fine. C<$view_or_model> can be any of | ||
| 83 | |||
| 84 | =over 4 | ||
| 85 | |||
| 86 | =item * | ||
| 87 | |||
| 88 | A viewer widget with a C<model> property, such as C<Gtk2::CellView>. The | ||
| 89 | model it's showing when edited is used. | ||
| 90 | |||
| 91 | =item * | ||
| 92 | |||
| 93 | A viewer object with a C<get_tree_view> method, such as | ||
| 94 | C<Gtk2::TreeViewColumn> of Gtk 2.12 and up, returning a C<Gtk2::TreeView> or | ||
| 95 | similar which in turn has a C<model> property. When packing a renderer in a | ||
| 96 | TreeViewColumn just pass that column and the TreeView and model it's showing | ||
| 97 | when edited is used. | ||
| 98 | |||
| 99 | =item * | ||
| 100 | |||
| 101 | A C<Gtk2::TreeModel> with a C<set_value> method, for direct use. It might | ||
| 102 | sometimes make sense to store into a different model than the one being | ||
| 103 | viewed, but the paths (Gtk2::TreePath coordinates) must be the same. | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | Usually the C<$column_num> to write back is the same column displayed by the | ||
| 108 | renderer, per C<add_attribute()>. But there's no way to automatically | ||
| 109 | extract that from the renderer/viewer setup (as of Gtk 2.20) so it must be | ||
| 110 | supplied here. | ||
| 111 | |||
| 112 | If you're using a single renderer in multiple viewers or columns then this | ||
| 113 | function is no good because it records a single destination viewer/model and | ||
| 114 | column within the renderer. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head1 SEE ALSO | ||
| 119 | |||
| 120 | L<Gtk2::CellRendererText>, L<Gtk2::Ex::WidgetBits> | ||
| 121 | |||
| 122 | =head1 HOME PAGE | ||
| 123 | |||
| 124 | L<http://user42.tuxfamily.org/chart/index.html> | ||
| 125 | |||
| 126 | =head1 LICENCE | ||
| 127 | |||
| 128 | Copyright 2008, 2009, 2010, 2011, 2013 Kevin Ryde | ||
| 129 | |||
| 130 | Chart is free software; you can redistribute it and/or modify it under the | ||
| 131 | terms of the GNU General Public License as published by the Free Software | ||
| 132 | Foundation; either version 3, or (at your option) any later version. | ||
| 133 | |||
| 134 | Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 135 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 136 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 137 | details. | ||
| 138 | |||
| 139 | You should have received a copy of the GNU General Public License along with | ||
| 140 | Chart; see the file F<COPYING>. Failing that, see | ||
| 141 | L<http://www.gnu.org/licenses/>. | ||
| 142 | |||
| 143 | =cut |