summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/lib/tkextlib/tcllib/tkpiechart.rb
blob: 2f6e4b44fc62deeda8abf341327d544ef559cd96 (plain)
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
#
#  tkextlib/tcllib/tkpiechart.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
#   * Part of tcllib extension
#   * Create 2D or 3D pies with labels in Tcl canvases
#

require 'tk'
require 'tk/canvas'
require 'tkextlib/tcllib.rb'

# TkPackage.require('tkpiechart', '6.6')
TkPackage.require('tkpiechart')

module Tk
  module Tcllib
    module Tkpiechart
    end
  end
end

module Tk::Tcllib::Tkpiechart
  PACKAGE_NAME = 'tkpiechart'.freeze
  def self.package_name
    PACKAGE_NAME
  end

  def self.package_version
    begin
      TkPackage.require('tkpiechart')
    rescue
      ''
    end
  end

  module ConfigMethod
    include TkConfigMethod

    def __pathname
      self.path + ';' + self.tag
    end
    private :__pathname

    def __cget_cmd
      ['::switched::cget', self.tag]
    end

    def __config_cmd
      ['::switched::configure', self.tag]
    end
    private :__config_cmd

    def __configinfo_struct
      {:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil,
        :default_value=>1, :current_value=>2}
    end
    private :__configinfo_struct

    def __boolval_optkeys
      super() << 'select' << 'autoupdate' << 'selectable'
    end
    private :__boolval_optkeys

    def __strval_optkeys
      super() << 'bordercolor' << 'textbackground' <<
        'widestvaluetext' << 'title'
    end
    private :__strval_optkeys

    def __listval_optkeys
      super() << 'colors'
    end
    private :__listval_optkeys
  end

  ####################################
  class PieChartObj < TkcItem
    include ConfigMethod

    def __font_optkeys
      ['titlefont']
    end
    private :__font_optkeys
  end

  ####################################
  class Pie < TkcItem
    include ConfigMethod

    def create_self(x, y, width, height, keys=None)
      if keys and keys != None
        @tag_key = tk_call_without_enc('::stooop::new', 'pie',
                                       @c, x, y, *hash_kv(keys, true))
      else
        @tag_key = tk_call_without_enc('::stooop::new', 'pie', @c, x, y)
      end

      @slice_tbl = {}

      id = "pie(#{@tag_key})"

      @tag = @tag_pie = TkcNamedTag(@c, id)
      @tag_slices = TkcNamedTag(@c, "pieSlices(#{@tag_key})")

      id
    end
    private :create_self

    def tag_key
      @tag_key
    end
    def tag
      @tag
    end
    def canvas
      @c
    end
    def _entry_slice(slice)
      @slice_tbl[slice.to_eval] = slice
    end
    def _delete_slice(slice)
      @slice_tbl.delete(slice.to_eval)
    end

    def delete
      tk_call_without_enc('::stooop::delete', @tag_key)
      CItemID_TBL.mutex.synchronize{
        CItemID_TBL[@path].delete(@id) if CItemID_TBL[@path]
      }
      self
    end

    def new_slice(text=None)
      Slice.new(self, text)
    end

    def delete_slice(slice)
      unless slice.kind_of?(Slice)
        unless (slice = @slice_tbl[slice])
          return tk_call_without_enc('pie::deleteSlice', @tag_key, slice)
        end
      end
      unless slice.kind_of?(Slice) && slice.pie == self
        fail ArgumentError, "argument is not a slice of self"
      end
      slice.delete
    end

    def selected_slices
      tk_split_simplelist(tk_call_without_enc('pie::selectedSlices',
                                              @tag_key)).collect{|slice|
        @slice_tbl[slice] || Slice.new(:no_create, self, slice)
      }
    end
  end

  ####################################
  class Slice < TkcItem
    include ConfigMethod

    def __config_cmd
      ['::switched::configure', self.tag]
    end
    private :__config_cmd

    #------------------------

    def initialize(pie, *args)
      unless pie.kind_of?(Pie) && pie != :no_create
        fail ArgumentError, "expects TkPiechart::Pie for 1st argument"
      end

      if pie == :no_create
        @pie, @tag_key = args
      else
        text = args[0] || None
        @pie = pie
        @tag_key = tk_call_without_enc('pie::newSlice', @pie.tag_key, text)
      end
      @parent = @c = @pie.canvas
      @path = @parent.path

      @pie._entry_slice(self)

      @id = "slices(#{@tag_key})"
      @tag = TkcNamedTag.new(@pie.canvas, @id)

      CItemID_TBL.mutex.synchronize{
        CItemID_TBL[@path] = {} unless CItemID_TBL[@path]
        CItemID_TBL[@path][@id] = self
      }
    end

    def tag_key
      @tag_key
    end
    def tag
      @tag
    end
    def pie
      @pie
    end

    def delete
      tk_call_without_enc('pie::deleteSlice', @pie.tag_key, @tag_key)
      CItemID_TBL.mutex.synchronize{
        CItemID_TBL[@path].delete(@id) if CItemID_TBL[@path]
      }
      @pie._delete_slice(self)
      self
    end

    def size(share, disp=None)
      tk_call_without_enc('pie::sizeSlice',
                          @pie.tag_key, @tag_key, share, disp)
      self
    end

    def label(text)
      tk_call_without_enc('pie::labelSlice', @pie.tag_key, @tag_key, text)
      self
    end
  end

  ####################################
  class BoxLabeler < TkcItem
    include ConfigMethod

    def __config_cmd
      ['::switched::configure', self.tag]
    end
    private :__config_cmd

    #------------------------

    def create_self(keys=None)
      if keys and keys != None
        @tag_key = tk_call_without_enc('::stooop::new', 'pieBoxLabeler',
                                       *hash_kv(keys, true))
      else
        @tag_key = tk_call_without_enc('::stooop::new', 'pieBoxLabeler')
      end

      id = "pieBoxLabeler(#{@tag_key})"
      @tag = TkcNamedTag(@c, id)

      id
    end
    private :create_self
  end

  ####################################
  class PeripheralLabeler < TkcItem
    include ConfigMethod

    def __font_optkeys
      ['font', 'smallfont']
    end
    private :__font_optkeys

    def __config_cmd
      ['::switched::configure', self.tag]
    end
    private :__config_cmd

    #------------------------

    def create_self(keys=None)
      if keys and keys != None
        @tag_key = tk_call_without_enc('::stooop::new',
                                       'piePeripheralLabeler',
                                       *hash_kv(keys, true))
      else
        @tag_key = tk_call_without_enc('::stooop::new', 'piePeripheralLabeler')
      end

      id = "piePeripheralLabeler(#{@tag_key})"
      @tag = TkcNamedTag(@c, id)

      id
    end
    private :create_self
  end

  ####################################
  class Label < TkcItem
    include ConfigMethod

    def __config_cmd
      ['::switched::configure', self.tag]
    end
    private :__config_cmd

    #------------------------

    def create_self(x, y, keys=None)
      if keys and keys != None
        @tag_key = tk_call_without_enc('::stooop::new', 'canvasLabel',
                                       @c, x, y, width, height,
                                       *hash_kv(keys, true))
      else
        @tag_key = tk_call_without_enc('::stooop::new', 'canvasLabel',
                                       @c, x, y, width, height)
      end

      id = "canvasLabel(#{@tag_key})"
      @tag = TkcNamedTag(@c, id)

      id
    end
    private :create_self
  end
end