goocanvas 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of
4
+ # goocanvas/demo/demo-events.c. It is licensed
5
+ # under the terms of the GNU Library General Public License, version
6
+ # 2 or (at your option) later.
7
+ #
8
+ # Copyright (C) 2013 Ruby-GNOME2 Project Team
9
+ #
10
+ # This library is free software; you can redistribute it and/or
11
+ # modify it under the terms of the GNU Lesser General Public
12
+ # License as published by the Free Software Foundation; either
13
+ # version 2.1 of the License, or (at your option) any later version.
14
+ #
15
+ # This library is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ # Lesser General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU Lesser General Public
21
+ # License along with this library; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
+
24
+ class CanvasSampleEvents < Gtk::Box
25
+ def initialize
26
+ super(:vertical, 4)
27
+ border_width = 4
28
+ show
29
+ create_events_page(self)
30
+ end
31
+
32
+ private
33
+ def create_events_page(vbox)
34
+ # Instructions
35
+
36
+ label = Gtk::Label.new(<<-EOT)
37
+ Move the mouse over the items to check they receive the right motion events.
38
+ The first 2 items in each group are 1) invisible and 2) visible but unpainted.
39
+ EOT
40
+ vbox.pack_start(label, :expand => false,
41
+ :fill => false,
42
+ :padding => 0)
43
+ label.show
44
+
45
+ # Frame and canvas
46
+
47
+ alignment = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
48
+ vbox.pack_start(alignment, :expand => false,
49
+ :fill => false,
50
+ :padding => 0)
51
+ alignment.show
52
+
53
+ frame = Gtk::Frame.new
54
+ frame.shadow_type = :in
55
+ alignment.add(frame)
56
+ frame.show
57
+
58
+ canvas = Goo::Canvas.new
59
+
60
+ canvas.set_size_request(600, 450)
61
+ canvas.set_bounds(0, 0, 600, 450)
62
+ frame.add(canvas)
63
+ canvas.show
64
+
65
+ create_events_area(canvas, 0, :none, "none")
66
+ create_events_area(canvas, 1, :visible_painted, "visible-painted")
67
+ create_events_area(canvas, 2, :visible_fill, "visible-fill")
68
+ create_events_area(canvas, 3, :visible_stroke, "visible-stroke")
69
+ create_events_area(canvas, 4, :visible, "visible")
70
+ create_events_area(canvas, 5, :painted, "painted")
71
+ create_events_area(canvas, 6, :fill, "fill")
72
+ create_events_area(canvas, 7, :stroke, "stroke")
73
+ create_events_area(canvas, 8, :all, "all")
74
+ end
75
+
76
+ def create_events_area(canvas, area_num, pointer_events, label)
77
+ row = area_num / 3
78
+ col = area_num % 3
79
+ x = col * 200
80
+ y = row * 150
81
+
82
+ root = canvas.root_item
83
+
84
+ dash = Goo::CanvasLineDash.new(5.0, 5.0)
85
+
86
+ # Create invisible item.
87
+ rect = Goo::CanvasRect.new(:parent => root,
88
+ :x => x + 45,
89
+ :y => y + 35,
90
+ :width => 30,
91
+ :height => 30,
92
+ :fill_color => "red",
93
+ :visibility => :invisible,
94
+ :line_width => 5.0,
95
+ :pointer_events => pointer_events)
96
+ view_id = sprintf("%s invisible", label)
97
+ setup_item_signals(rect, view_id)
98
+
99
+ # Display a thin rect around it to indicate it is there.
100
+ rect = Goo::CanvasRect.new(:parent => root,
101
+ :x => x + 42.5,
102
+ :y => y + 32.5,
103
+ :width => 36,
104
+ :height => 36,
105
+ # TODO: [BUG] Segmentation fault
106
+ #:line_dash => dash,
107
+ :line_width => 1.0,
108
+ :stroke_color => "gray")
109
+
110
+ # Create unpainted item.
111
+ rect = Goo::CanvasRect.new(:parent => root,
112
+ :x => x + 85,
113
+ :y => y + 35,
114
+ :width => 30,
115
+ :height => 30,
116
+ :stroke_pattern => nil,
117
+ :line_width => 5.0,
118
+ :pointer_events => pointer_events)
119
+ view_id = sprintf("%s unpainted", label)
120
+ setup_item_signals(rect, view_id)
121
+
122
+ # Display a thin rect around it to indicate it is there.
123
+ rect = Goo::CanvasRect.new(:parent => root,
124
+ :x => x + 82.5,
125
+ :y => y + 32.5,
126
+ :width => 36,
127
+ :height => 36,
128
+ # TODO: [BUG] Segmentation fault
129
+ #:line_dash => dash,
130
+ :line_width => 1.0,
131
+ :stroke_color => "gray")
132
+
133
+ # Create stroked item.
134
+ rect = Goo::CanvasRect.new(:parent => root,
135
+ :x => x + 125,
136
+ :y => y + 35,
137
+ :width => 30,
138
+ :height => 30,
139
+ :line_width => 5.0,
140
+ :pointer_events => pointer_events)
141
+ view_id = sprintf("%s stroked", label)
142
+ setup_item_signals(rect, view_id)
143
+
144
+ # Create filled item.
145
+ rect = Goo::CanvasRect.new(:parent => root,
146
+ :x => x + 60,
147
+ :y => y + 75,
148
+ :width => 30,
149
+ :height => 30,
150
+ :fill_color => "red",
151
+ :stroke_pattern => nil,
152
+ :line_width => 5.0,
153
+ :pointer_events => pointer_events)
154
+ view_id = sprintf("%s filled", label)
155
+ setup_item_signals(rect, view_id)
156
+
157
+ # Create stroked & filled item.
158
+ rect = Goo::CanvasRect.new(:parent => root,
159
+ :x => x + 100,
160
+ :y => y + 75,
161
+ :width => 30,
162
+ :height => 30,
163
+ :fill_color => "red",
164
+ :line_width => 5.0,
165
+ :pointer_events => pointer_events)
166
+ view_id = sprintf("%s stroked & filled", label)
167
+ setup_item_signals(rect, view_id)
168
+
169
+ Goo::CanvasText.new(:parent => root,
170
+ :text => label,
171
+ :x => x + 100,
172
+ :y => y + 130,
173
+ :width => -1,
174
+ :anchor => :center,
175
+ :font => "Sans 12",
176
+ :fill_color => "blue")
177
+ end
178
+
179
+ def setup_item_signals(item, item_id)
180
+ item.signal_connect("motion_notify_event") do |widget, event|
181
+ if item_id
182
+ printf("%s item received 'motion-notify' signal\n", item_id)
183
+ end
184
+ false
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of
4
+ # goocanvas/demo/demo-features.c. It is licensed
5
+ # under the terms of the GNU Library General Public License, version
6
+ # 2 or (at your option) later.
7
+ #
8
+ # Copyright (C) 2013 Ruby-GNOME2 Project Team
9
+ #
10
+ # This library is free software; you can redistribute it and/or
11
+ # modify it under the terms of the GNU Lesser General Public
12
+ # License as published by the Free Software Foundation; either
13
+ # version 2.1 of the License, or (at your option) any later version.
14
+ #
15
+ # This library is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ # Lesser General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU Lesser General Public
21
+ # License along with this library; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
+
24
+ class CanvasSampleFeatures < Gtk::Box
25
+ def initialize
26
+ super(:vertical, 4)
27
+ set_border_width(4)
28
+ show
29
+ create_canvas_features(self)
30
+ end
31
+
32
+ private
33
+ def create_canvas_features(box)
34
+ instructions(box)
35
+ root = frame_and_canvas(box)
36
+ parent1 = first_parent_and_box(root)
37
+ parent2 = second_parent_and_box(root)
38
+ big_circle_to_be_reparented(parent1, parent2)
39
+ a_group_to_be_reparented(parent1, parent2)
40
+ end
41
+
42
+ def instructions(box)
43
+ label = Gtk::Label.new(<<-EOT)
44
+ Reparent test: click on the items to switch them between parents
45
+ EOT
46
+ box.pack_start(label, :expand => false,
47
+ :fill => false,
48
+ :padding => 0)
49
+ label.show
50
+ end
51
+
52
+ def frame_and_canvas(box)
53
+ alignment = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
54
+ box.pack_start(alignment, :expand => false,
55
+ :fill => false,
56
+ :padding => 0)
57
+ alignment.show
58
+
59
+ frame = Gtk::Frame.new
60
+ frame.shadow_type = :in
61
+ alignment.add(frame)
62
+ frame.show
63
+
64
+ canvas = Goo::Canvas.new
65
+ root = canvas.root_item
66
+
67
+ canvas.set_size_request(400, 200)
68
+ canvas.set_bounds(0, 0, 400, 200)
69
+ frame.add(canvas)
70
+ canvas.show
71
+
72
+ root
73
+ end
74
+
75
+ def first_parent_and_box(root)
76
+ parent1 = Goo::CanvasGroup.new(:parent => root)
77
+ Goo::CanvasRect.new(:parent => parent1,
78
+ :x => 0,
79
+ :y => 0,
80
+ :width => 200,
81
+ :height => 200,
82
+ :fill_color => "tan")
83
+ parent1
84
+ end
85
+
86
+ def second_parent_and_box(root)
87
+ parent2 = Goo::CanvasGroup.new(:parent => root)
88
+ parent2.translate(200, 0)
89
+ Goo::CanvasRect.new(:parent => parent2,
90
+ :x => 0,
91
+ :y => 0,
92
+ :width => 200,
93
+ :height => 200,
94
+ :fill_color => "#204060")
95
+ parent2
96
+ end
97
+
98
+ def big_circle_to_be_reparented(parent1, parent2)
99
+ item = Goo::CanvasEllipse.new(:parent => parent1,
100
+ :center_x => 100,
101
+ :center_y => 100,
102
+ :radius_x => 90,
103
+ :radius_y => 90,
104
+ :stroke_color => "black",
105
+ :fill_color => "mediumseagreen",
106
+ :line_width => 3.0)
107
+
108
+ item.signal_connect("button-press-event") do |widget, event|
109
+ on_button_press(widget, event, parent1, parent2)
110
+ end
111
+ end
112
+
113
+ def a_group_to_be_reparented(parent1, parent2)
114
+ group = Goo::CanvasGroup.new(:parent => parent2)
115
+ group.translate(100, 100)
116
+
117
+ Goo::CanvasEllipse.new(:parent => group,
118
+ :center_x => 0,
119
+ :center_y => 0,
120
+ :radius_x => 50,
121
+ :radius_y => 50,
122
+ :stroke_color => "black",
123
+ :fill_color => "wheat",
124
+ :line_width => 3.0)
125
+
126
+ Goo::CanvasEllipse.new(:parent => group,
127
+ :center_x => 0,
128
+ :center_y => 0,
129
+ :radius_x => 25,
130
+ :radius_y => 25,
131
+ :fill_color => "steelblue")
132
+
133
+ group.signal_connect("button-press-event") do |widget, event|
134
+ on_button_press(widget, event, parent1, parent2)
135
+ end
136
+ end
137
+
138
+ def on_button_press(item, event, parent1, parent2)
139
+ puts "In on_button_press"
140
+ parent = item.parent
141
+ item.remove
142
+ if parent == parent1
143
+ parent2.add_child(item, -1)
144
+ else
145
+ parent1.add_child(item, -1)
146
+ end
147
+ end
148
+ end
@@ -21,28 +21,27 @@ Original Copyright:
21
21
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
22
  GNU General Public License for more details.
23
23
 
24
- You should have received a copy of the GNU General Public License
25
- along with this program; if not, write to the Free Software
26
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27
- 02111-1307, USA.
24
+ You should have received a copy of the GNU General Public License along
25
+ with this program; if not, write to the Free Software Foundation, Inc.,
26
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
27
 
29
28
  =end
30
29
 
31
- class CanvasSampleFifteen < Gtk::VBox
30
+ class CanvasSampleFifteen < Gtk::Box
32
31
  PIECE_SIZE = 50
33
32
  SCRAMBLE_MOVES = 32
34
33
 
35
34
  def initialize
36
- super(false, 4)
35
+ super(:vertical, 4)
37
36
  border_width = 4
38
37
  show()
39
38
 
40
39
  alignment = Gtk::Alignment.new(0.5, 0.5, 0.0, 0.0)
41
- pack_start(alignment, true, true, 0)
40
+ pack_start(alignment, :expand => true, :fill => true, :padding => 0)
42
41
  alignment.show()
43
42
 
44
43
  frame = Gtk::Frame.new
45
- frame.set_shadow_type(Gtk::SHADOW_IN);
44
+ frame.set_shadow_type(:in);
46
45
  alignment.add(frame)
47
46
  frame.show()
48
47
 
@@ -57,14 +56,14 @@ class CanvasSampleFifteen < Gtk::VBox
57
56
  @board = Array.new(16)
58
57
 
59
58
  0.upto(14) do |i|
60
- @board[i] = Piece.new(@canvas.root_item)
59
+ @board[i] = Piece.new(:parent => @canvas.root_item)
61
60
  @board[i].setup(self, i)
62
61
  end
63
62
  @board[15] = nil;
64
63
 
65
64
  # Scramble button
66
- button = Gtk::Button.new("Scramble")
67
- pack_start(button, false, false, 0)
65
+ button = Gtk::Button.new(:label => "Scramble")
66
+ pack_start(button, :expand => false, :fill => false, :padding => 0)
68
67
  button.signal_connect("clicked") do |button|
69
68
  scramble()
70
69
  end
@@ -77,13 +76,14 @@ class CanvasSampleFifteen < Gtk::VBox
77
76
  return
78
77
  end
79
78
  end
80
- dialog = Gtk::MessageDialog.new(parent_window,
81
- Gtk::Dialog::DESTROY_WITH_PARENT,
82
- Gtk::MessageDialog::INFO,
83
- Gtk::MessageDialog::BUTTONS_OK,
84
- "You stud, you win!")
79
+ dialog = Gtk::MessageDialog.new(:parent => parent.parent,
80
+ :flags => :destroy_with_parent,
81
+ :type => :info,
82
+ :buttons_type => :ok,
83
+ :message => "You stud, you win!")
85
84
  dialog.set_modal(true)
86
- dialog.run_and_close()
85
+ dialog.run
86
+ dialog.hide
87
87
  end
88
88
 
89
89
  def piece_enter_notify(item)
@@ -156,7 +156,7 @@ class CanvasSampleFifteen < Gtk::VBox
156
156
  elsif (dir == 3) && ((pos % 4) != 3) # right
157
157
  x = 1
158
158
  else
159
- retry
159
+ redo
160
160
  end
161
161
 
162
162
  oldpos = pos + y * 4 + x;
@@ -165,7 +165,7 @@ class CanvasSampleFifteen < Gtk::VBox
165
165
  @board[pos].pos = pos
166
166
  @board[pos].translate(-x * PIECE_SIZE, -y * PIECE_SIZE)
167
167
  pos = oldpos
168
- end
168
+ end
169
169
  end
170
170
 
171
171
  class Piece < Goo::CanvasGroup
@@ -181,14 +181,22 @@ class CanvasSampleFifteen < Gtk::VBox
181
181
  x = i % 4
182
182
  translate( x * PIECE_SIZE,y * PIECE_SIZE)
183
183
 
184
- Goo::CanvasRect.new(self, 0, 0, PIECE_SIZE, PIECE_SIZE,
184
+ Goo::CanvasRect.new(:parent => self,
185
+ :x => 0,
186
+ :y => 0,
187
+ :width => PIECE_SIZE,
188
+ :height => PIECE_SIZE,
185
189
  :line_width => 1.0,
186
190
  :fill_color => get_piece_color(x, y),
187
191
  :stroke_color => "black")
188
192
 
189
193
 
190
- @text = Goo::CanvasText.new(self,i.to_s, PIECE_SIZE / 2.0,
191
- PIECE_SIZE / 2.0, -1, Gtk::ANCHOR_CENTER,
194
+ @text = Goo::CanvasText.new(:parent => self,
195
+ :text => i.to_s,
196
+ :x => PIECE_SIZE / 2.0,
197
+ :y => PIECE_SIZE / 2.0,
198
+ :width => -1,
199
+ :anchor => :center,
192
200
  :font=>"Sans bold 24",
193
201
  :fill_color => "black")
194
202
  @num = i