2 # tkextlib/tcllib/tkpiechart.rb
3 # by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
5 # * Part of tcllib extension
6 # * Create 2D or 3D pies with labels in Tcl canvases
11 require 'tkextlib/tcllib.rb'
13 # TkPackage.require('tkpiechart', '6.6')
14 TkPackage.require('tkpiechart')
23 module Tk::Tcllib::Tkpiechart
24 PACKAGE_NAME = 'tkpiechart'.freeze
29 def self.package_version
31 TkPackage.require('tkpiechart')
38 include TkConfigMethod
41 self.path + ';' + self.tag
46 ['::switched::cget', self.tag]
50 ['::switched::configure', self.tag]
54 def __configinfo_struct
55 {:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil,
56 :default_value=>1, :current_value=>2}
58 private :__configinfo_struct
61 super() << 'select' << 'autoupdate' << 'selectable'
63 private :__boolval_optkeys
66 super() << 'bordercolor' << 'textbackground' <<
67 'widestvaluetext' << 'title'
69 private :__strval_optkeys
74 private :__listval_optkeys
77 ####################################
78 class PieChartObj < TkcItem
84 private :__font_optkeys
87 ####################################
91 def create_self(x, y, width, height, keys=None)
92 if keys and keys != None
93 @tag_key = tk_call_without_enc('::stooop::new', 'pie',
94 @c, x, y, *hash_kv(keys, true))
96 @tag_key = tk_call_without_enc('::stooop::new', 'pie', @c, x, y)
101 id = "pie(#{@tag_key})"
103 @tag = @tag_pie = TkcNamedTag(@c, id)
104 @tag_slices = TkcNamedTag(@c, "pieSlices(#{@tag_key})")
119 def _entry_slice(slice)
120 @slice_tbl[slice.to_eval] = slice
122 def _delete_slice(slice)
123 @slice_tbl.delete(slice.to_eval)
127 tk_call_without_enc('::stooop::delete', @tag_key)
128 CItemID_TBL.mutex.synchronize{
129 CItemID_TBL[@path].delete(@id) if CItemID_TBL[@path]
134 def new_slice(text=None)
135 Slice.new(self, text)
138 def delete_slice(slice)
139 unless slice.kind_of?(Slice)
140 unless (slice = @slice_tbl[slice])
141 return tk_call_without_enc('pie::deleteSlice', @tag_key, slice)
144 unless slice.kind_of?(Slice) && slice.pie == self
145 fail ArgumentError, "argument is not a slice of self"
151 tk_split_simplelist(tk_call_without_enc('pie::selectedSlices',
152 @tag_key)).collect{|slice|
153 @slice_tbl[slice] || Slice.new(:no_create, self, slice)
158 ####################################
159 class Slice < TkcItem
163 ['::switched::configure', self.tag]
165 private :__config_cmd
167 #------------------------
169 def initialize(pie, *args)
170 unless pie.kind_of?(Pie) && pie != :no_create
171 fail ArgumentError, "expects TkPiechart::Pie for 1st argument"
175 @pie, @tag_key = args
177 text = args[0] || None
179 @tag_key = tk_call_without_enc('pie::newSlice', @pie.tag_key, text)
181 @parent = @c = @pie.canvas
184 @pie._entry_slice(self)
186 @id = "slices(#{@tag_key})"
187 @tag = TkcNamedTag.new(@pie.canvas, @id)
189 CItemID_TBL.mutex.synchronize{
190 CItemID_TBL[@path] = {} unless CItemID_TBL[@path]
191 CItemID_TBL[@path][@id] = self
206 tk_call_without_enc('pie::deleteSlice', @pie.tag_key, @tag_key)
207 CItemID_TBL.mutex.synchronize{
208 CItemID_TBL[@path].delete(@id) if CItemID_TBL[@path]
210 @pie._delete_slice(self)
214 def size(share, disp=None)
215 tk_call_without_enc('pie::sizeSlice',
216 @pie.tag_key, @tag_key, share, disp)
221 tk_call_without_enc('pie::labelSlice', @pie.tag_key, @tag_key, text)
226 ####################################
227 class BoxLabeler < TkcItem
231 ['::switched::configure', self.tag]
233 private :__config_cmd
235 #------------------------
237 def create_self(keys=None)
238 if keys and keys != None
239 @tag_key = tk_call_without_enc('::stooop::new', 'pieBoxLabeler',
240 *hash_kv(keys, true))
242 @tag_key = tk_call_without_enc('::stooop::new', 'pieBoxLabeler')
245 id = "pieBoxLabeler(#{@tag_key})"
246 @tag = TkcNamedTag(@c, id)
253 ####################################
254 class PeripheralLabeler < TkcItem
258 ['font', 'smallfont']
260 private :__font_optkeys
263 ['::switched::configure', self.tag]
265 private :__config_cmd
267 #------------------------
269 def create_self(keys=None)
270 if keys and keys != None
271 @tag_key = tk_call_without_enc('::stooop::new',
272 'piePeripheralLabeler',
273 *hash_kv(keys, true))
275 @tag_key = tk_call_without_enc('::stooop::new', 'piePeripheralLabeler')
278 id = "piePeripheralLabeler(#{@tag_key})"
279 @tag = TkcNamedTag(@c, id)
286 ####################################
287 class Label < TkcItem
291 ['::switched::configure', self.tag]
293 private :__config_cmd
295 #------------------------
297 def create_self(x, y, keys=None)
298 if keys and keys != None
299 @tag_key = tk_call_without_enc('::stooop::new', 'canvasLabel',
300 @c, x, y, width, height,
301 *hash_kv(keys, true))
303 @tag_key = tk_call_without_enc('::stooop::new', 'canvasLabel',
304 @c, x, y, width, height)
307 id = "canvasLabel(#{@tag_key})"
308 @tag = TkcNamedTag(@c, id)