| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Graphics::Simple::GnomeCanvas -- implement Graphics::Simple using Gnome Canvas | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Graphics::Simple; | ||
| 8 | # use those operations | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | The module C<Graphics::Simple::GnomeCanvas> is an implementation | ||
| 13 | of the C<Graphics::Simple> API. | ||
| 14 | |||
| 15 | =head1 DEVICE-DEPENDENT OPERATIONS | ||
| 16 | |||
| 17 | =head2 stop | ||
| 18 | |||
| 19 | Waiting is implemented by waiting for a button click in any of the windows | ||
| 20 | managed by this module. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | print "GSGTK\n"; | ||
| 26 | |||
| 27 | package Graphics::Simple::GnomeCanvas; | ||
| 28 | use strict; | ||
| 29 | |||
| 30 | use base 'Graphics::Simple::Window'; | ||
| 31 | |||
| 32 | use Gtk; | ||
| 33 | use Gnome; | ||
| 34 | init Gnome "simplegraph"; | ||
| 35 | |||
| 36 | use vars qw/$BP/; | ||
| 37 | $BP = 0; # A global variable, button pressed to indicate continuing. | ||
| 38 | |||
| 39 | sub _construct { | ||
| 40 | my($type, $x, $y) = @_; | ||
| 41 | my $t = Gtk::Window->new('-toplevel'); | ||
| 42 | my $c = Gnome::Canvas->new(); | ||
| 43 | $c->set_usize($x,$y); | ||
| 44 | $c->set_scroll_region(0,0,$x,$y); | ||
| 45 | $t->add($c); | ||
| 46 | $t->show_all; | ||
| 47 | my $this = bless { | ||
| 48 | C => $c, | ||
| 49 | }, $type; | ||
| 50 | $c->signal_connect("event", sub { | ||
| 51 | my($w, $e) = @_; | ||
| 52 | # print "WE: $w, $e: $e->{type}\n"; | ||
| 53 | if($e->{type} eq "button_press") { | ||
| 54 | $BP ++; | ||
| 55 | } | ||
| 56 | }); | ||
| 57 | return $this; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _line { | ||
| 61 | my $this = shift; | ||
| 62 | my $name = shift; | ||
| 63 | # print "POI: @_\n"; | ||
| 64 | my $g = $this->{C}->root; | ||
| 65 | my $l = $g->new($g, "Gnome::CanvasLine", | ||
| 66 | points => [@_], | ||
| 67 | fill_color => $this->{Current_Color}, | ||
| 68 | width_units => $this->{Current_LineWidth}, | ||
| 69 | ); | ||
| 70 | $this->{I}{$name} = $l; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _ellipse { | ||
| 74 | my($this, $name, $x1, $y1, $x2, $y2) = @_; | ||
| 75 | my $g = $this->{C}->root; | ||
| 76 | my $c = $g->new($g, "Gnome::CanvasEllipse", | ||
| 77 | x1 => $x1, y1 => $y1, | ||
| 78 | x2 => $x2, y2 => $y2, | ||
| 79 | outline_color => $this->{Current_Color}, | ||
| 80 | width_units => $this->{Current_LineWidth}, | ||
| 81 | ); | ||
| 82 | $this->{I}{$name} = $c; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _text { | ||
| 86 | my($this, $name, $x, $y, $text) = @_; | ||
| 87 | # print "T: $x $y $text\n"; | ||
| 88 | my $g = $this->{C}->root; | ||
| 89 | my $c = $g->new($g, "Gnome::CanvasText", | ||
| 90 | x => $x, y => $y, | ||
| 91 | text => $text, | ||
| 92 | fill_color => $this->{Current_Color}, | ||
| 93 | ); | ||
| 94 | $this->{I}{$name} = $c; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _remove { | ||
| 98 | my($this, $n) = @_; | ||
| 99 | (delete $this->{I}{$n})->destroy; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _image { | ||
| 103 | my($this, $n, $x, $y, $w, $h, $d, $str) = @_; | ||
| 104 | my $pixmap = Gtk::Gdk::Pixmap->new( | ||
| 105 | $this->{C}->window, | ||
| 106 | $w, | ||
| 107 | $h, | ||
| 108 | -1 | ||
| 109 | ); | ||
| 110 | my $gc = $this->{C}->style->fg_gc; | ||
| 111 | if($depth == 1) { | ||
| 112 | $pixmap->draw_gray_image( | ||
| 113 | $gc, | ||
| 114 | 0,0, | ||
| 115 | $w, $h, | ||
| 116 | 'NONE', | ||
| 117 | $w | ||
| 118 | ); | ||
| 119 | } elsif($depth == 3) { | ||
| 120 | $pixmap->draw_rgb_image( | ||
| 121 | $gc, | ||
| 122 | 0,0, | ||
| 123 | $w, $h, | ||
| 124 | 'NONE', | ||
| 125 | $w*3 | ||
| 126 | ); | ||
| 127 | } else { | ||
| 128 | die("Depth must be 3"); | ||
| 129 | } | ||
| 130 | my $wid = Gtk::Pixmap->new( | ||
| 131 | $pm, undef | ||
| 132 | ); | ||
| 133 | my $g = $this->{C}->root; | ||
| 134 | my $c = $g->new($g, "Gnome::CanvasWidget", | ||
| 135 | widget => $wid, | ||
| 136 | x => $x, | ||
| 137 | y => $y, | ||
| 138 | # XXX width + height | ||
| 139 | ); | ||
| 140 | $this->{I}{$name} = $c; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _clear { | ||
| 144 | my($this) = @_; | ||
| 145 | for(values %{$this->{I}}) { | ||
| 146 | print "DEST $_\n"; | ||
| 147 | $_->destroy; | ||
| 148 | } | ||
| 149 | delete $this->{I}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _finish { | ||
| 153 | my($this) = @_; | ||
| 154 | $this->_wait(); | ||
| 155 | } | ||
| 156 | |||
| 157 | # For now, just press button 1 | ||
| 158 | sub _wait { | ||
| 159 | my($this) = @_; | ||
| 160 | $BP = 0; | ||
| 161 | print "Waiting... Click a button in the window\n"; | ||
| 162 | Gtk->main_iteration while !$BP; | ||
| 163 | print "Continuing...\n"; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Copyright(C) Tuomas J. Lukka 1999. All rights reserved. | ||
| 172 | This software may be distributed under the same conditions as Perl itself. | ||
| 173 | |||
| 174 | =cut |