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
|
#!/usr/bin/perl -w
# Note: this is a generic launcher script, designed to work with all Gtk versions.
# If you are interested in the Perl port of the Gtk widget demo, please look at
# Gtk/samples/test.pl.
use Gtk;
if (!init_check Gtk) {
print STDERR "You have to run make test under X\n";
exit(0);
}
# Build list of available sample scripts
foreach $file (<*/samples/*.pl>) {
open(F, "<$file");
$file =~ m!^(.*/)!;
$directory = $1;
$file = $';
$title = undef;
$requires = undef;
while (<F>) {
chop;
if (/TITLE:\s+(.*?)\s*$/) {
$title = $1;
}
if (/REQUIRES:\s+(.*?)\s*$/) {
$requires = $1;
}
}
close(F);
if (defined $title or defined $requires) {
push @samples, {file => $file, directory => $directory, title => $title, requires => $requires};
}
}
# Build execution environment
push @exec, $^X, "-w";
foreach (@INC) {
if (m!^/!) {
push @exec, "-I$_";
} else {
push @exec, "-I../../$_";
}
}
sub run {
my($script) = @_;
my(@blib) = ();
foreach (split(/\s+/, $script->{requires})) {
next if /^Gtk$/;
push(@blib, "-Mblib=../../$_") if -d "$_/blib";
push(@blib, "-Mblib=../../GdkImlib")
if (/^Gnome$/ && -d "GdkImlib/blib");
}
print "\nExecuting (in $script->{directory}: ", join(' ', @exec, @blib, $script->{file}), "\n";
if (!fork) {
chdir($script->{directory});
exec @exec, @blib, $script->{file};
}
}
# Build UI
$main_window = new Gtk::Window -toplevel;
$main_window->set_title("Samples for Perl/Gtk+");
$main_window->set_border_width(5);
$main_window->set_usize(600, 500);
$hbox = new Gtk::HBox 0, 0;
show $hbox;
$main_window->add($hbox);
$list_scroller = new Gtk::ScrolledWindow(undef, undef);
$list_scroller->set_policy(-automatic, -automatic);
$list_scroller->border_width(5);
show $list_scroller;
$sample_list = new Gtk::List;
$sample_list->set_selection_mode(-multiple);
$sample_list->signal_connect( "select_child" => sub {
show_sample($_[1]->{sample});
} );
$list_scroller->add_with_viewport($sample_list);
$list_scroller->set_usize(150, 100);
$hbox->pack_start($list_scroller, 0, 1, 5);
show $sample_list;
$vbox = new Gtk::VBox 0, 0;
show $vbox;
$title_label = new Gtk::Label "";
show $title_label;
$vbox->pack_start($title_label, 0, 1, 0);
$file_label = new Gtk::Label "";
show $file_label;
$vbox->pack_start($file_label, 0, 1, 0);
$requires_label = new Gtk::Label "";
show $requires_label;
$vbox->pack_start($requires_label, 0, 1, 0);
$source_hbox = new Gtk::HBox 0, 0;
$source = new Gtk::Text undef, undef;
show $source;
sub show_sample {
my($sample) = @_;
open (F, "<" . $sample->{directory} . $sample->{file});
$source->freeze;
$source->realize;
$source->delete_text(0, $source->get_length);
while (<F>) {
$source->insert_text($_, $source->get_length);
}
$source->thaw;
close(F);
$current_sample = $sample;
$run->set_sensitive(1);
$title_label->set("Title: " . $sample->{title});
$file_label->set("File: " . $sample->{directory}.$sample->{file});
$requires_label->set("Requires: " . $sample->{requires});
}
$source_hbox->pack_start($source, 1, 1, 0);
show $source_hbox;
$source_vscroll = new Gtk::VScrollbar($source->vadj);
$source_vscroll->show;
$source_hbox->pack_end($source_vscroll, 0, 0, 0);
$vbox->pack_start($source_hbox, 1, 1, 0);
$hbbox = new Gtk::HButtonBox;
$run = new Gtk::Button "Quit";
$run->signal_connect("clicked" => sub { Gtk->exit(0);} );
show $run;
$hbbox->add($run);
$run = new Gtk::Button "Run";
$run->signal_connect("clicked" => sub { run $current_sample if defined $current_sample;} );
show $run;
$run->can_default(1);
$run->set_sensitive(0);
$hbbox->add($run);
show $hbbox;
$vbox->pack_end($hbbox, 0, 1, 5);
$hbox->pack_start($vbox, 1, 1, 5);
foreach (sort {$a->{title} cmp $b->{title}} @samples) {
$list_item = new Gtk::ListItem $_->{title};
$list_item->{sample} = $_;
$sample_list->add($list_item);
$list_item->show;
}
sub idle {
$sample_list->set_selection_mode(-browse);
$sample_list->select_item(0);
return 0;
}
Gtk->idle_add(\&idle);
$main_window->signal_connect("destroy" => sub { destroy $main_window; exit } );
$run->grab_default();
show $main_window;
main Gtk;
|