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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
package CanvasArrowhead;
use strict;
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;
use constant LEFT => 50.0;
use constant RIGHT => 350.0;
use constant MIDDLE => 150.0;
use constant DEFAULT_WIDTH => 2;
use constant DEFAULT_SHAPE_A => 8;
use constant DEFAULT_SHAPE_B => 10;
use constant DEFAULT_SHAPE_C => 3;
sub set_dimension {
my ($canvas, $arrow_name, $text_name,
$x1, $y1, $x2, $y2, $tx, $ty, $dim) = @_;
$canvas->{$arrow_name}->set (points => [$x1, $y1, $x2, $y2]);
$canvas->{$text_name}->set (text => $dim,
x => $tx,
y => $ty);
}
sub move_drag_box {
my ($item, $x, $y) = @_;
$item->set (x1 => $x - 5.0,
y1 => $y - 5.0,
x2 => $x + 5.0,
y2 => $y + 5.0);
}
sub set_arrow_shape {
my $canvas = shift;
my $width = $canvas->{width};
my $shape_a = $canvas->{shape_a};
my $shape_b = $canvas->{shape_b};
my $shape_c = $canvas->{shape_c};
# Big arrow
$canvas->{big_arrow}->set (width_pixels => 10 * $width,
arrow_shape_a => $shape_a * 10,
arrow_shape_b => $shape_b * 10,
arrow_shape_c => $shape_c * 10);
# Outline
my @coords = ();
$coords[0] = RIGHT - 10 * $shape_a;
$coords[1] = MIDDLE;
$coords[2] = RIGHT - 10 * $shape_b;
$coords[3] = MIDDLE - 10 * ($shape_c + $width / 2.0);
$coords[4] = RIGHT;
$coords[5] = MIDDLE;
$coords[6] = $coords[2];
$coords[7] = MIDDLE + 10 * ($shape_c + $width / 2.0);
$coords[8] = $coords[0];
$coords[9] = $coords[1];
$canvas->{outline}->set (points => \@coords);
# Drag boxes
move_drag_box ($canvas->{width_drag_box},
LEFT,
MIDDLE - 10 * $width / 2.0);
move_drag_box ($canvas->{shape_a_drag_box},
RIGHT - 10 * $shape_a,
MIDDLE);
move_drag_box ($canvas->{shape_b_c_drag_box},
RIGHT - 10 * $shape_b,
MIDDLE - 10 * ($shape_c + $width / 2.0));
# Dimensions
set_dimension ($canvas, "width_arrow", "width_text",
LEFT - 10,
MIDDLE - 10 * $width / 2.0,
LEFT - 10,
MIDDLE + 10 * $width / 2.0,
LEFT - 15,
MIDDLE,
$width);
set_dimension ($canvas, "shape_a_arrow", "shape_a_text",
RIGHT - 10 * $shape_a,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 10,
RIGHT,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 10,
RIGHT - 10 * $shape_a / 2.0,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 15,
$shape_a);
set_dimension ($canvas, "shape_b_arrow", "shape_b_text",
RIGHT - 10 * $shape_b,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 35,
RIGHT,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 35,
RIGHT - 10 * $shape_b / 2.0,
MIDDLE + 10 * ($width / 2.0 + $shape_c) + 40,
$shape_b);
set_dimension ($canvas, "shape_c_arrow", "shape_c_text",
RIGHT + 10,
MIDDLE - 10 * $width / 2.0,
RIGHT + 10,
MIDDLE - 10 * ($width / 2.0 + $shape_c),
RIGHT + 15,
MIDDLE - 10 * ($width / 2.0 + $shape_c / 2.0),
$shape_c);
# Info
$canvas->{width_info}->set (text => "width: $width");
$canvas->{shape_a_info}->set (text => "arrow_shape_a: $shape_a");
$canvas->{shape_b_info}->set (text => "arrow_shape_b: $shape_b");
$canvas->{shape_c_info}->set (text => "arrow_shape_c: $shape_c");
# Sample arrows
$canvas->{sample_1}->set (width_pixels => $width,
arrow_shape_a => $shape_a,
arrow_shape_b => $shape_b,
arrow_shape_c => $shape_c);
$canvas->{sample_2}->set (width_pixels => $width,
arrow_shape_a => $shape_a,
arrow_shape_b => $shape_b,
arrow_shape_c => $shape_c);
$canvas->{sample_3}->set (width_pixels => $width,
arrow_shape_a => $shape_a,
arrow_shape_b => $shape_b,
arrow_shape_c => $shape_c);
}
sub highlight_box {
my ($item, $event) = @_;
if ($event->type eq 'enter-notify') {
$item->set (fill_color => 'red');
} elsif ($event->type eq 'leave-notify') {
$item->set (fill_color => undef)
unless $event->state & 'button1-mask';
} elsif ($event->type eq 'button-press') {
$item->grab ([qw/pointer-motion-mask button-release-mask/],
Gtk2::Gdk::Cursor->new ('fleur'),
$event->time);
} elsif ($event->type eq 'button-release') {
$item->ungrab ($event->time);
}
return FALSE;
}
sub create_drag_box {
my ($root, $box_name, $callback) = @_;
my $box = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Rect',
fill_color => undef,
outline_color => 'black',
width_pixels => 0);
$box->signal_connect (event => \&highlight_box);
$box->signal_connect (event => $callback);
$root->canvas->{$box_name} = $box;
}
sub width_event {
my ($item, $event) = @_;
return FALSE
if (($event->type ne 'motion-notify') ||
!($event->state >= 'button1-mask'));
my $width = (MIDDLE - $event->y) / 5;
return FALSE
if $width < 0;
$item->canvas->{width} = $width;
set_arrow_shape ($item->canvas);
return FALSE;
}
sub shape_a_event {
my ($item, $event) = @_;
return FALSE
if (($event->type ne 'motion-notify') ||
!($event->state >= 'button1-mask'));
my $shape_a = (RIGHT - $event->x) / 10;
return FALSE if (($shape_a < 0) || ($shape_a > 30));
$item->canvas->{shape_a} = $shape_a;
set_arrow_shape ($item->canvas);
return FALSE;
}
sub shape_b_c_event {
my ($item, $event) = @_;
return FALSE
if (($event->type ne 'motion-notify') ||
!($event->state >= 'button1-mask'));
my $change = FALSE;
my $shape_b = (RIGHT - $event->x) / 10;
if (($shape_b >= 0) && ($shape_b <= 30)) {
$item->canvas->{shape_b} = $shape_b;
$change = TRUE;
}
my $width = $item->canvas->{width};
my $shape_c = ((MIDDLE - 5 * $width) - $event->y) / 10;
if ($shape_c >= 0) {
$item->canvas->{shape_c} = $shape_c;
$change = TRUE;
}
set_arrow_shape ($item->canvas)
if $change;
return FALSE;
}
sub create_dimension {
my ($root, $arrow_name, $text_name, $anchor) = @_;
my $item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
fill_color => 'black',
first_arrowhead => TRUE,
last_arrowhead => TRUE,
arrow_shape_a => 5.0,
arrow_shape_b => 5.0,
arrow_shape_c => 3.0);
$root->canvas->{$arrow_name} = $item;
$item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Text',
fill_color => 'black',
font => 'Sans 12',
anchor => $anchor);
$root->canvas->{$text_name} = $item;
}
sub create_info {
my ($root, $info_name, $x, $y) = @_;
my $item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Text',
x => $x,
y => $y,
fill_color => 'black',
font => 'Sans 14',
anchor => 'GTK_ANCHOR_NW');
$root->canvas->{$info_name} = $item;
}
sub create_sample_arrow {
my ($root, $sample_name, $x1, $y1, $x2, $y2) = @_;
my $item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
points => [$x1, $y1, $x2, $y2],
fill_color => 'black',
first_arrowhead => TRUE,
last_arrowhead => TRUE);
$root->canvas->{$sample_name} = $item;
}
sub create {
my $vbox = Gtk2::VBox->new (FALSE, 4);
$vbox->set_border_width (4);
$vbox->show;
my $w = Gtk2::Label->new ("This demo allows you to edit arrowhead shapes. Drag the little boxes\n"
. "to change the shape of the line and its arrowhead. You can see the\n"
. "arrows at their normal scale on the right hand side of the window.");
$vbox->pack_start ($w, FALSE, FALSE, 0);
$w->show;
$w = Gtk2::Alignment->new (0.5, 0.5, 0.0, 0.0);
$vbox->pack_start ($w, TRUE, TRUE, 0);
$w->show;
my $frame = Gtk2::Frame->new;
$frame->set_shadow_type ('in');
$w->add ($frame);
$frame->show;
my $canvas = Gnome2::Canvas->new;
$canvas->set_size_request (500, 350);
$canvas->set_scroll_region (0, 0, 500, 350);
$frame->add ($canvas);
$canvas->show;
my $root = $canvas->root;
$canvas->{width} = DEFAULT_WIDTH;
$canvas->{shape_a} = DEFAULT_SHAPE_A;
$canvas->{shape_b} = DEFAULT_SHAPE_B;
$canvas->{shape_c} = DEFAULT_SHAPE_C;
# Big arrow
my $item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
points => [LEFT, MIDDLE,
RIGHT, MIDDLE],
fill_color => 'mediumseagreen',
width_pixels => DEFAULT_WIDTH * 10,
last_arrowhead => TRUE);
$canvas->{big_arrow} = $item;
# Arrow outline
$item = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
fill_color => 'black',
width_pixels => 2,
cap_style => 'round',
join_style => 'round');
$canvas->{outline} = $item;
# Drag boxes
create_drag_box ($root, "width_drag_box", \&width_event);
create_drag_box ($root, "shape_a_drag_box", \&shape_a_event);
create_drag_box ($root, "shape_b_c_drag_box", \&shape_b_c_event);
# Dimensions
create_dimension ($root, "width_arrow", "width_text", 'e');
create_dimension ($root, "shape_a_arrow", "shape_a_text", 'n');
create_dimension ($root, "shape_b_arrow", "shape_b_text", 'n');
create_dimension ($root, "shape_c_arrow", "shape_c_text", 'w');
# Info
create_info ($root, "width_info", LEFT, 260);
create_info ($root, "shape_a_info", LEFT, 280);
create_info ($root, "shape_b_info", LEFT, 300);
create_info ($root, "shape_c_info", LEFT, 320);
# Division line
Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Line',
points => [RIGHT + 50, 0,
RIGHT + 50, 1000],
fill_color => 'black',
width_pixels => 2);
# Sample arrows
create_sample_arrow ($root, "sample_1", RIGHT + 100, 30, RIGHT + 100, MIDDLE - 30);
create_sample_arrow ($root, "sample_2", RIGHT + 70, MIDDLE, RIGHT + 130, MIDDLE);
create_sample_arrow ($root, "sample_3", RIGHT + 70, MIDDLE + 30, RIGHT + 130, MIDDLE + 120);
# Done!
set_arrow_shape ($canvas);
return $vbox;
}
1;
|