File: canvas-features.pl

package info (click to toggle)
libgnome2-canvas-perl 1.002-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 320 kB
  • ctags: 76
  • sloc: perl: 1,601; makefile: 21; ansic: 13
file content (130 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download | duplicates (4)
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
package CanvasFeatures;

use strict;
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;

use constant GNOME_PAD_SMALL => 4;

#
# Event handler for the item to be reparented.  When the user clicks on the
# item, it will be reparented to another group.
#

sub item_event {
	my ($item, $event) = @_;

	return FALSE
		if ($event->type ne 'button-press') || ($event->button != 1);

	my $parent1 = $item->{parent1};
	my $parent2 = $item->{parent2};

	$item->reparent ($item->parent == $parent1 ? $parent2 : $parent1);

	return TRUE;
}

sub create {
	my $vbox = Gtk2::VBox->new (FALSE, GNOME_PAD_SMALL);
	$vbox->set_border_width (GNOME_PAD_SMALL);
	$vbox->show;

	# Instructions

	my $w = Gtk2::Label->new ("Reparent test:  click on the items to switch them between parents");
	$vbox->pack_start ($w, FALSE, FALSE, 0);
	$w->show;

	# Frame and canvas

	my $alignment = Gtk2::Alignment->new (0.5, 0.5, 0.0, 0.0);
	$vbox->pack_start ($alignment, FALSE, FALSE, 0);
	$alignment->show;

	my $frame = Gtk2::Frame->new;
	$frame->set_shadow_type ('in');
	$alignment->add ($frame);
	$frame->show;

	my $canvas = Gnome2::Canvas->new;
	$canvas->set_size_request (400, 200);
	$canvas->set_scroll_region (0, 0, 400, 200);
	$frame->add ($canvas);
	$canvas->show;

	# First parent and box

	my $parent1 = Gnome2::Canvas::Item->new ($canvas->root,
						 'Gnome2::Canvas::Group',
						 x => 0.0,
						 y => 0.0);

	Gnome2::Canvas::Item->new ($parent1, 'Gnome2::Canvas::Rect',
				   x1 => 0.0,
				   y1 => 0.0,
				   x2 => 200.0,
				   y2 => 200.0,
				   fill_color => 'tan');

	# Second parent and box

	my $parent2 = Gnome2::Canvas::Item->new ($canvas->root,
	                                         'Gnome2::Canvas::Group',
						 x => 200.0,
						 y => 0.0);

	Gnome2::Canvas::Item->new ($parent2, 'Gnome2::Canvas::Rect',
				   x1 => 0.0,
				   y1 => 0.0,
				   x2 => 200.0,
				   y2 => 200.0,
				   fill_color => "#204060");

	# Big circle to be reparented

	my $item = Gnome2::Canvas::Item->new ($parent1,
					      'Gnome2::Canvas::Ellipse',
					      x1 => 10.0,
					      y1 => 10.0,
					      x2 => 190.0,
					      y2 => 190.0,
					      outline_color => 'black',
					      fill_color => 'mediumseagreen',
					      width_units => 3.0);
	$item->{parent1} = $parent1;
	$item->{parent2} = $parent2;
	$item->signal_connect (event => \&item_event);

	# A group to be reparented

	my $group =
		Gnome2::Canvas::Item->new ($parent2, 'Gnome2::Canvas::Group',
					    x => 100.0,
					    y => 100.0);

	Gnome2::Canvas::Item->new ($group, 'Gnome2::Canvas::Ellipse',
				   x1 => -50.0,
				   y1 => -50.0,
				   x2 => 50.0,
				   y2 => 50.0,
				   outline_color => 'black',
				   fill_color => 'wheat',
				   width_units => 3.0);
	Gnome2::Canvas::Item->new ($group, 'Gnome2::Canvas::Ellipse',
				   x1 => -25.0,
				   y1 => -25.0,
				   x2 => 25.0,
				   y2 => 25.0,
				   fill_color => 'steelblue');

	$group->{parent1} = $parent1;
	$group->{parent2} = $parent2;
	$group->signal_connect (event => \&item_event);

	# Done

	return $vbox;
}

1;