summaryrefslogtreecommitdiffstats
path: root/ext/tk/lib/tkextlib/tile/treeview.rb
blob: 46e3499f21c26fff7a3b1e089fad3f1b2e1fc618 (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
#
#  treeview widget
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'
require 'tkextlib/tile.rb'

module Tk
  module Tile
    class Treeview < TkWindow
    end

    module TreeviewConfig
      include TkItemConfigMethod

      def __item_cget_cmd(id)
        [self.path, id[0], id[1]]
      end
      private :__item_cget_cmd

      def __item_config_cmd(id)
        [self.path, id[0], id[1]]
      end
      private :__item_config_cmd

      def __item_numstrval_optkeys(id)
        case id[0]
        when :item, 'item'
          ['width']
        when :column, 'column'
          super(id[1])
        when :heading, 'heading'
          super(id[1])
        end
      end
      private :__item_numstrval_optkeys

      def __item_strval_optkeys(id)
        case id[0]
        when :item, 'item'
          super(id) + ['id']
        when :column, 'column'
          super(id[1])
        when :heading, 'heading'
          super(id[1])
        end
      end
      private :__item_strval_optkeys

      def __item_boolval_optkeys(id)
        case id[0]
        when :item, 'item'
          ['open']
        when :column, 'column'
          super(id[1])
        when :heading, 'heading'
          super(id[1])
        end
      end
      private :__item_boolval_optkeys

      def __item_listval_optkeys(id)
        case id[0]
        when :item, 'item'
          ['values']
        when :column, 'column'
          []
        when :heading, 'heading'
          []
        end
      end
      private :__item_listval_optkeys

      alias __itemcget itemcget
      alias __itemconfigure itemconfigure
      alias __itemconfiginfo itemconfiginfo
      alias __current_itemconfiginfo current_itemconfiginfo

      private :__itemcget, :__itemconfigure
      private :__itemconfiginfo, :__current_itemconfiginfo

      # Treeview Item
      def itemcget(tagOrId, option)
        __itemcget([:item, tagOrId], option)
      end
      def itemconfigure(tagOrId, slot, value=None)
        __itemconfigure([:item, tagOrId], slot, value)
      end
      def itemconfiginfo(tagOrId, slot=nil)
        __itemconfiginfo([:item, tagOrId], slot)
      end
      def current_itemconfiginfo(tagOrId, slot=nil)
        __current_itemconfiginfo([:item, tagOrId], slot)
      end

      # Treeview Column
      def columncget(tagOrId, option)
        __itemcget([:column, tagOrId], option)
      end
      def columnconfigure(tagOrId, slot, value=None)
        __itemconfigure([:column, tagOrId], slot, value)
      end
      def columnconfiginfo(tagOrId, slot=nil)
        __itemconfiginfo([:column, tagOrId], slot)
      end
      def current_columnconfiginfo(tagOrId, slot=nil)
        __current_itemconfiginfo([:column, tagOrId], slot)
      end
      alias column_cget columncget
      alias column_configure columnconfigure
      alias column_configinfo columnconfiginfo
      alias current_column_configinfo current_columnconfiginfo

      # Treeview Heading
      def headingcget(tagOrId, option)
        __itemcget([:heading, tagOrId], option)
      end
      def headingconfigure(tagOrId, slot, value=None)
        __itemconfigure([:heading, tagOrId], slot, value)
      end
      def headingconfiginfo(tagOrId, slot=nil)
        __itemconfiginfo([:heading, tagOrId], slot)
      end
      def current_headingconfiginfo(tagOrId, slot=nil)
        __current_itemconfiginfo([:heading, tagOrId], slot)
      end
      alias heading_cget headingcget
      alias heading_configure headingconfigure
      alias heading_configinfo headingconfiginfo
      alias current_heading_configinfo current_headingconfiginfo
    end
  end
end

class Tk::Tile::Treeview < TkWindow
  include Tk::Tile::TileWidget
  include Scrollable

  include Tk::Tile::TreeviewConfig

  if Tk::Tile::USE_TTK_NAMESPACE
    TkCommandNames = ['::ttk::treeview'.freeze].freeze
  else
    TkCommandNames = ['::treeview'.freeze].freeze
  end
  WidgetClassName = 'Treeview'.freeze
  WidgetClassNames[WidgetClassName] = self

  def self.style(*args)
    [self::WidgetClassName, *(args.map!{|a| _get_eval_string(a)})].join('.')
  end

  def tagid(id)
    if id.kind_of?(Array)
      [id[0], _get_eval_string(id[1])]
    else
      _get_eval_string(id)
    end
  end

  def children(item)
    simplelist(tk_send_without_enc('children', item))
  end
  def set_children(item, *items)
    tk_send_without_enc('children', item, 
                        array2tk_list(items.flatten, true))
    self
  end

  def delete(*items)
    tk_send_without_enc('delete', array2tk_list(items.flatten, true))
    self
  end

  def detach(*items)
    tk_send_without_enc('detach', array2tk_list(items.flatten, true))
    self
  end

  def exist?(item)
    bool(tk_send_without_enc('exists', _get_eval_enc_str(item)))
  end

  def focus_item(item = None)
    tk_send('focus', item)
  end

  def identify(x, y)
    ret = simplelist(tk_send('identify', x, y))
    case ret[0]
    when 'heading', 'separator', 'cell'
      ret[-1] = num_or_str(ret[-1])
    end
  end

  def index(item)
    number(tk_send('index', item))
  end

  def insert(parent, idx, keys={})
    keys = _symbolkey2str(keys)
    id = keys.delete('id')
    if id
      tk_send('insert', parent, idx, '-id', id, *hash_kv(keys))
    else
      tk_send('insert', parent, idx, *hash_kv(keys))
    end
    self
  end

  def instate(spec, cmd=Proc.new)
    tk_send('instate', spec, cmd)
  end
  def state(spec=None)
    tk_send('state', spec)
  end

  def move(item, parent, idx)
    tk_send('move', item, parent, idx)
    self
  end

  def next(item)
    tk_send('next', item)
  end

  def parent(item)
    tk_send('parent', item)
  end

  def prev(item)
    tk_send('prev', item)
  end

  def see(item)
    tk_send('see', item)
    self
  end

  def selection
    simplelist(tk_send('selection'))
  end
  alias selection_get selection

  def selection_add(*items)
    tk_send('selection', 'add', array2tk_list(items.flatten, true))
    self
  end
  def selection_remove(*items)
    tk_send('selection', 'remove', array2tk_list(items.flatten, true))
    self
  end
  def selection_set(*items)
    tk_send('selection', 'set', array2tk_list(items.flatten, true))
    self
  end
  def selection_toggle(*items)
    tk_send('selection', 'toggle', array2tk_list(items.flatten, true))
    self
  end

  def get(item, col)
    tk_send('set', item, col)
  end
  def set(item, col, value)
    tk_send('set', item, col, value)
    self
  end
end