File: editable_cells.pl

package info (click to toggle)
libgtk2-perl 1%3A1.140-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,808 kB
  • ctags: 609
  • sloc: perl: 14,245; ansic: 118; makefile: 70
file content (211 lines) | stat: -rw-r--r-- 5,718 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Tree View/Editable Cells
#
# This demo demonstrates the use of editable cells in a GtkTreeView. If
# you're new to the GtkTreeView widgets and associates, look into
# the GtkListStore example first.
#
#

package editable_cells;

use Glib qw(TRUE FALSE);
use Gtk2;

my $window = undef;

#typedef struct
#{
#  gint     number;
#  gchar   *product;
#  gboolean editable;
#}
#Item;

use constant COLUMN_NUMBER   => 0;
use constant COLUMN_PRODUCT  => 1;
use constant COLUMN_EDITABLE => 2;
use constant NUM_COLUMNS     => 3;

my @articles = ();

sub add_items {
  push @articles,
    { number => 3, product => "bottles of coke", editable => TRUE, },
    { number => 5, product => "packages of noodles", editable => TRUE, },
    { number => 2, product => "packages of chocolate chip cookies", editable => TRUE, },
    { number => 1, product => "can vanilla ice cream", editable => TRUE, },
    { number => 6, product => "eggs", editable => TRUE, },
  ;
}

sub create_model {
  # create array
  add_items ();

  # create list store
  my $model = Gtk2::ListStore->new (qw/Glib::Int Glib::String Glib::Boolean/);

  # add items
  foreach my $a (@articles) {
      my $iter = $model->append;

      $model->set ($iter,
                   COLUMN_NUMBER, $a->{number},
                   COLUMN_PRODUCT, $a->{product},
                   COLUMN_EDITABLE, $a->{editable});
  }

  return $model;
}

sub add_item {
  my ($button, $model) = @_;

  push @articles, {
	number => 0,
	product => "Description here",
	editable => TRUE,
  };

  my $iter = $model->append;
  $model->set ($iter,
               COLUMN_NUMBER, $articles[-1]{number},
               COLUMN_PRODUCT, $articles[-1]{product},
               COLUMN_EDITABLE, $articles[-1]{editable});
}

sub remove_item {
  my ($widget, $treeview) = @_;
  my $model = $treeview->get_model;
  my $selection = $treeview->get_selection;

  my $iter = $selection->get_selected;
  if ($iter) {
      my $path = $model->get_path ($iter);
      my $i = ($path->get_indices)[0];
      $model->remove ($iter);

      splice @articles, $i;
  }
}

sub cell_edited {
  my ($cell, $path_string, $new_text, $model) = @_;
  my $path = Gtk2::TreePath->new_from_string ($path_string);

  my $column = $cell->get_data ("column");

  my $iter = $model->get_iter ($path);

  if ($column == COLUMN_NUMBER) {
	my $i = ($path->get_indices)[0];
	$articles[$i]{number} = $new_text;

	$model->set ($iter, $column, $articles[$i]{number});

  } elsif ($column == COLUMN_PRODUCT) {
	my $i = ($path->get_indices)[0];
	$articles[$i]{product} = $new_text;

	$model->set ($iter, $column, $articles[$i]{product});
  }
}

sub add_columns {
  my $treeview = shift;
  my $model = $treeview->get_model;

  # number column
  my $renderer = Gtk2::CellRendererText->new;
  $renderer->signal_connect (edited => \&cell_edited, $model);
  $renderer->set_data (column => COLUMN_NUMBER);

  $treeview->insert_column_with_attributes (-1, "Number", $renderer,
					    text => COLUMN_NUMBER,
					    editable => COLUMN_EDITABLE);

  # product column
  $renderer = Gtk2::CellRendererText->new;
  $renderer->signal_connect (edited => \&cell_edited, $model);
  $renderer->set_data (column => COLUMN_PRODUCT);

  $treeview->insert_column_with_attributes (-1, "Product", $renderer,
					    text => COLUMN_PRODUCT,
					    editable => COLUMN_EDITABLE);
}

sub do {
  if (!$window) {
      # create window, etc
      $window = Gtk2::Window->new;
      $window->set_title ("Shopping list");
      $window->set_border_width (5);
      $window->signal_connect (destroy => sub { $window = undef; 1 });

      my $vbox = Gtk2::VBox->new (FALSE, 5);
      $window->add ($vbox);

      $vbox->pack_start (Gtk2::Label->new ("Shopping list (you can edit the cells!)"),
			 FALSE, FALSE, 0);

      my $sw = Gtk2::ScrolledWindow->new;
      $sw->set_shadow_type ('etched-in');
      $sw->set_policy ('automatic', 'automatic');
      $vbox->pack_start ($sw, TRUE, TRUE, 0);

      # create model
      my $model = create_model ();

      # create tree view
      my $treeview = Gtk2::TreeView->new_with_model ($model);
##      g_object_unref (model);
      $treeview->set_rules_hint (TRUE);
      $treeview->get_selection->set_mode ('single');

      add_columns ($treeview);

      $sw->add ($treeview);

      # some buttons
      my $hbox = Gtk2::HBox->new (TRUE, 4);
      $vbox->pack_start ($hbox, FALSE, FALSE, 0);

      my $button = Gtk2::Button->new ("Add item");
      $button->signal_connect (clicked => \&add_item, $model);
      $hbox->pack_start ($button, TRUE, TRUE, 0);

      $button = Gtk2::Button->new ("Remove item");
      $button->signal_connect (clicked => \&remove_item, $treeview);
      $hbox->pack_start ($button, TRUE, TRUE, 0);

      $window->set_default_size (320, 200);
  }

  if (!$window->visible) {
      $window->show_all;
  } else {	 
      $window->destroy;
      $window = undef;
  }

  return $window;
}

1;
__END__
Copyright (C) 2003 by the gtk2-perl team (see the file AUTHORS for the
full list)

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Library General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Library General Public License for more
details.

You should have received a copy of the GNU Library General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA  02111-1307  USA.