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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#!/usr/bin/perl -w
#
# Stock Item and Icon Browser
#
# This source code for this demo doesn't demonstrate anything
# particularly useful in applications. The purpose of the "demo" is
# just to provide a handy place to browse the available stock icons
# and stock items.
#
package stock_browser;
use Glib qw(TRUE FALSE);
use Gtk2;
my $window = undef;
#
# The C version of this sample defines a structure called StockItemInfo
# to hold information about the stock items; it makes StockItemInfo into
# a new boxed type so that, when used with the GtkListStore, all the
# memory management happens properly.
#
# in perl we don't do such things. the StockItemInfo structure becomes
# just a perl hash, and we store it in the Gtk2::ListStore in a column
# with the Glib::Scalar type. this way all the memory management happens
# properly and you don't have to think up elaborate schemes to do so
# (because i already did it for you).
#
sub id_to_macro {
my $id = shift;
my $macro = uc($id);
$macro =~ s/-/_/g;
$macro =~ s/^GTK_/GTK_STOCK_/;
return $macro;
}
sub create_model {
#store = gtk_list_store_new (2, STOCK_ITEM_INFO_TYPE, G_TYPE_STRING);
#my $store = Gtk2::ListStore->new ('scalar', 'Glib::String');
my $store = Gtk2::ListStore->new ('Glib::Scalar', 'Glib::String');
foreach my $id (sort Gtk2::Stock->list_ids) {
my $item = Gtk2::Stock->lookup ($id);
my %info = (
id => $id,
item => $item ? $item : {}
);
# only show icons for stock IDs that have default icons
my $icon_set = Gtk2::IconFactory->lookup_default ($info{id});
if ($icon_set) {
# See at which sizes this stock icon really exists
my @sizes = $icon_set->get_sizes;
# Use menu size if it exists, otherwise first size found
my $size = grep (@sizes, 'menu')
? 'menu'
: $sizes[0];
$info{small_icon} = $window->render_icon ($info{id}, $size);
if ($size ne 'menu') {
# Make the result the proper size for our thumbnail
$info{small_icon} =
Gtk2::Gdk::Pixbuf->scale_simple ($info{small_icon},
Gtk2::Icon->size_lookup ('menu'),
'bilinear');
}
}
$info{accel_str} = $info{item}{keyval}
? Gtk2::Accelerator->name ($info{item}{keyval},
$info{item}{modifier})
: '';
$info{macro} = id_to_macro ($info{id});
# something about redhat9's gnome setup registers a whole slew of
# stock items which don't return icons in this context. i don't
# know if it's because we don't do Gnome2::Program->init or what,
# but it's really annoying, because it messes with lots of things,
# and since you can't pass undef/null for an object property, we
# can't tell the cell renderers to use NULL for the pixbufs.
# so, we only add an entry for stock items which have icons.
if ($info{small_icon}) {
my $iter = $store->append;
$store->set ($iter, 0, \%info, 1, $id);
}
}
return $store;
}
#
# Finds the largest size at which the given image stock id is
# available. This would not be useful for a normal application
#
sub get_largest_size {
my $id = shift;
my $best_size = 'invalid';
my $best_pixels = 0;
my $set = Gtk2::IconFactory->lookup_default ($id);
foreach my $size ($set->get_sizes) {
my ($width, $height) = Gtk2::IconSize->lookup ($size);
next unless defined $height;
if ($width * $height > $best_pixels) {
$best_size = $size;
$best_pixels = $width * $height;
}
}
return $best_size;
}
sub selection_changed {
my $selection = shift;
# warn "\n\nselection_changed $selection";
my $treeview = $selection->get_tree_view;
my $display = $treeview->{stock_display};
my ($model, $iter) = $selection->get_selected;
if ($iter) {
my ($info) = $model->get ($iter, 0);
if ($info->{small_icon} && $info->{item}{label}) {
$display->{type_label}->set_text ("Icon and Item");
} elsif ($info->{small_icon}) {
$display->{type_label}->set_text ("Icon Only");
} elsif ($info->{item}{label}) {
$display->{type_label}->set_text ("Item Only");
} else {
$display->{type_label}->set_text ("???????");
}
$display->{macro_label}->set_text ($info->{macro});
$display->{id_label}->set_text ($info->{id});
if ($info->{item}{label}) {
$display->{label_accel_label}->set_text_with_mnemonic (sprintf '%s %s', $info->{item}{label}, $info->{accel_str});
} else {
$display->{label_accel_label}->set_text ("");
}
if ($info->{small_icon}) {
$display->{icon_image}->set_from_stock ($info->{id},
get_largest_size ($info->{id}));
} else {
$display->{icon_image}->set_from_pixbuf (undef);
}
} else {
$display->{type_label}->set_text ("No selected item");
$display->{macro_label}->set_text ("");
$display->{id_label}->set_text ("");
$display->{label_accel_label}->set_text ("");
$display->{icon_image}->set_from_pixbuf (undef);
}
}
sub macro_set_func_text {
my ($tree_column, $cell, $model, $iter) = @_;
my ($info) = $model->get ($iter, 0);
$cell->set (text => $info->{macro});
}
sub macro_set_func_pixbuf {
my ($tree_column, $cell, $model, $iter) = @_;
my ($info) = $model->get ($iter, 0);
$cell->set (pixbuf => $info->{small_icon});
}
sub id_set_func {
my ($tree_column, $cell, $model, $iter) = @_;
my ($info) = $model->get ($iter, 0);
$cell->set (text => $info->{id});
}
sub accel_set_func {
my ($tree_column, $cell, $model, $iter) = @_;
my ($info) = $model->get ($iter, 0);
$cell->set (text => $info->{accel_str});
}
sub label_set_func {
my ($tree_column, $cell, $model, $iter) = @_;
my ($info) = $model->get ($iter, 0);
# items aren't required to have labels
$cell->set (text => $info->{item}{label} || '');
}
sub do {
if (!$window) {
$window = Gtk2::Window->new;
$window->set_title ("Stock Icons and Items");
$window->set_default_size (-1, 500);
$window->signal_connect (destroy => sub {$window = undef; 1});
$window->set_border_width (8);
my $hbox = Gtk2::HBox->new (FALSE, 8);
$window->add ($hbox);
my $sw = Gtk2::ScrolledWindow->new;
$sw->set_policy ('never', 'automatic');
$hbox->pack_start ($sw, FALSE, FALSE, 0);
my $model = create_model ();
my $treeview = Gtk2::TreeView->new_with_model ($model);
$sw->add ($treeview);
my $column = Gtk2::TreeViewColumn->new;
$column->set_title ("Macro");
my $cell_renderer = Gtk2::CellRendererPixbuf->new;
$column->pack_start ($cell_renderer, FALSE);
### this doesn't work for 2.0.x, because stock_id isn't an attribute for the
### renderer until 2.2.x
### $column->set_attributes ($cell_renderer, stock_id => 1);
$column->set_cell_data_func ($cell_renderer, \¯o_set_func_pixbuf);
$cell_renderer = Gtk2::CellRendererText->new;
$column->pack_start ($cell_renderer, TRUE);
$column->set_cell_data_func ($cell_renderer, \¯o_set_func_text);
$treeview->append_column ($column);
$cell_renderer = Gtk2::CellRendererText->new;
$treeview->insert_column_with_data_func (-1,
"Label",
$cell_renderer,
\&label_set_func);
$cell_renderer = Gtk2::CellRendererText->new;
$treeview->insert_column_with_data_func (-1,
"Accel",
$cell_renderer,
\&accel_set_func);
$cell_renderer = Gtk2::CellRendererText->new;
$treeview->insert_column_with_data_func (-1,
"ID",
$cell_renderer,
\&id_set_func);
my $align = Gtk2::Alignment->new (0.5, 0.0, 0.0, 0.0);
$hbox->pack_end ($align, FALSE, FALSE, 0);
my $frame = Gtk2::Frame->new ("Selected Item");
$align->add ($frame);
my $vbox = Gtk2::VBox->new (FALSE, 8);
$vbox->set_border_width (4);
$frame->add ($vbox);
my $display = {
type_label => Gtk2::Label->new,
macro_label => Gtk2::Label->new,
id_label => Gtk2::Label->new,
label_accel_label => Gtk2::Label->new,
icon_image => Gtk2::Image->new_from_pixbuf (undef), # empty image
};
$treeview->{stock_display} = $display;
$vbox->pack_start ($display->{type_label}, FALSE, FALSE, 0);
$vbox->pack_start ($display->{icon_image}, FALSE, FALSE, 0);
$vbox->pack_start ($display->{label_accel_label}, FALSE, FALSE, 0);
$vbox->pack_start ($display->{macro_label}, FALSE, FALSE, 0);
$vbox->pack_start ($display->{id_label}, FALSE, FALSE, 0);
my $selection = $treeview->get_selection;
$selection->set_mode ('single');
$selection->signal_connect (changed => \&selection_changed);
}
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.
|