summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/lib/tkextlib/blt/tabset.rb
blob: c4716c7304fbf31601663f800157f1269f4aed50 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#
#  tkextlib/blt/tabset.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#

require 'tk'
require 'tkextlib/blt.rb'

module Tk::BLT
  class Tabset < TkWindow
    class Tab < TkObject
      include TkTreatItemFont

      TabID_TBL = TkCore::INTERP.create_table

      (TabsetTab_ID = ['blt_tabset_tab'.freeze, TkUtil.untrust('00000')]).instance_eval{
        @mutex = Mutex.new
        def mutex; @mutex; end
        freeze
      }

      TkCore::INTERP.init_ip_env{
        TabID_TBL.mutex.synchronize{ TabID_TBL.clear }
      }

      def self.id2obj(tabset, id)
        tpath = tabset.path
        TabID_TBL.mutex.synchronize{
          if TabID_TBL[tpath]
            TabID_TBL[tpath][id]? TabID_TBL[tpath][id]: id
          else
            id
          end
        }
      end

      def self.new(parent, pos=nil, name=nil, keys={})
        if pos.kind_of?(Hash)
          keys = pos
          name = nil
          pos  = nil
        end
        if name.kind_of?(Hash)
          keys = name
          name = nil
        end
        obj = nil
        TabID_TBL.mutex.synchronize{
          if name && TabID_TBL[parent.path] && TabID_TBL[parent.path][name]
            obj = TabID_TBL[parent.path][name]
            if pos
              if pos.to_s == 'end'
                obj.move_after('end')
              else
                obj.move_before(pos)
              end
            end
            obj.configure if keys && ! keys.empty?
          else
            (obj = self.allocate).instance_eval{
              initialize(parent, pos, name, keys)
              TabID_TBL[@tpath] = {} unless TabID_TBL[@tpath]
              TabID_TBL[@tpath][@id] = self
            }
          end
        }
        obj
      end

      def initialize(parent, pos, name, keys)
        @t = parent
        @tpath = parent.path
        if name
          @path = @id = name
          unless (list(tk_call(@tpath, 'tab', 'names', @id)).empty?)
            if pos
              idx = tk_call(@tpath, 'index', '-name', @id)
              if pos.to_s == 'end'
                tk_call(@tpath, 'move', idx, 'after', 'end')
              else
                tk_call(@tpath, 'move', idx, 'before', pos)
              end
            end
            tk_call(@tpath, 'tab', 'configure', @id, keys)
          else
            pos = 'end' unless pos
            tk_call(@tpath, 'insert', pos, @id, keys)
          end
        else
          pos = 'end' unless pos
          TabsetTab_ID.mutex.synchronize{
            @path = @id = TabsetTab_ID.join(TkCore::INTERP._ip_id_)
            TabsetTab_ID[1].succ!
          }
          tk_call(@tpath, 'insert', pos, @id, keys)
        end
      end

      #def bind(context, cmd=Proc.new, *args)
      #  @t.tab_bind(@id, context, cmd, *args)
      #  self
      #end
      def bind(context, *args)
        # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
        if TkComm._callback_entry?(args[0]) || !block_given?
          cmd = args.shift
        else
          cmd = Proc.new
        end
        @t.tab_bind(@id, context, cmd, *args)
        self
      end
      #def bind_append(context, cmd=Proc.new, *args)
      #  @t.tab_bind_append(@id, context, cmd, *args)
      #  self
      #end
      def bind_append(context, *args)
        # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
        if TkComm._callback_entry?(args[0]) || !block_given?
          cmd = args.shift
        else
          cmd = Proc.new
        end
        @t.tab_bind_append(@id, context, cmd, *args)
        self
      end
      def bind_remove(context)
        @t.tab_bind_remove(@id, context)
        self
      end
      def bindinfo(context=nil)
        @t.tab_bindinfo(@id, context)
      end

      def cget_tkstring(*args)
        @t.tab_cget_tkstring(@id, *args)
      end
      def cget(*args)
        @t.tab_cget(@id, *args)
      end
      def cget_strict(*args)
        @t.tab_cget_strict(@id, *args)
      end
      def configure(*args)
        @t.tab_configure(@id, *args)
      end
      def configinfo(*args)
        @t.tab_configinfo(@id, *args)
      end
      def current_configinfo(*args)
        @t.current_tab_configinfo(@id, *args)
      end

      def delete()
        @t.delete(@id)
        TabID_TBL.mutex.synchronize{
          TabID_TBL[@tpath].delete(@id)
        }
        self
      end

      def get_name()
        @id.dup
      end

      def focus()
        @t.focus(self.index)
      end

      def index()
        @t.index_name(@id)
      end

      def invoke()
        @t.invoke(self.index)
      end

      def move_before(idx)
        @t.move_before(self.index, idx)
      end
      def move_after(idx)
        @t.move_after(self.index, idx)
      end

      def perforation_highlight(mode)
        @t.perforation_highlight(self.index, mode)
      end
      def perforation_invoke()
        @t.perforation_invoke(self.index)
      end

      def see()
        @t.see(self.index)
      end

      def tearoff(name=None)
        @t.tab_tearoff(self.index, *args)
      end
    end

    ########################################

    class NamedTab < Tab
      def self.new(parent, name)
        super(parent, nil, name, {})
      end
    end

    ########################################

    include X_Scrollable
    include TkItemConfigMethod

    TkCommandNames = ['::blt::tabset'.freeze].freeze
    WidgetClassName = 'Tabset'.freeze
    WidgetClassNames[WidgetClassName] ||= self

    def __destroy_hook__
      Tk::BLT::Tabset::Tab::TabID_TBL.mutex.synchronize{
        Tk::BLT::Tabset::Tab::TabID_TBL.delete(@path)
      }
    end

    ########################################

    def __boolval_optkeys
      super() << 'samewidth' << 'tearoff'
    end
    private :__strval_optkeys

    def __strval_optkeys
      super() << 'tabbackground' << 'tabforeground'
    end
    private :__strval_optkeys

    def __item_cget_cmd(id)
      [self.path, 'tab', 'cget', id]
    end
    private :__item_cget_cmd

    def __item_config_cmd(id)
      [self.path, 'tab', 'configure', id]
    end
    private :__item_config_cmd

    def __item_pathname(tagOrId)
      if tagOrId.kind_of?(Tk::BLT::Tabset::Tab)
        self.path + ';' + tagOrId.id.to_s
      else
        self.path + ';' + tagOrId.to_s
      end
    end
    private :__item_pathname

    alias tab_cget_tkstring itemcget_tkstring
    alias tab_cget itemcget
    alias tab_cget_strict itemcget_strict
    alias tab_configure itemconfigure
    alias tab_configinfo itemconfiginfo
    alias current_tab_configinfo current_itemconfiginfo

    def __item_strval_optkeys(id)
      super(id) << 'shadow'
    end
    private :__item_strval_optkeys

    def tagid(tab)
      if tab.kind_of?(Tk::BLT::Tabset::Tab)
        tab.id
      else
        tab
      end
    end

    def tagindex(tab)
      if tab.kind_of?(Tk::BLT::Tabset::Tab)
        tab.index
      else
        tab
      end
    end

    ########################################

    def activate(index)
      tk_send('activate', tagindex(index))
      self
    end
    alias highlight activate

    #def tabbind(tag, context, cmd=Proc.new, *args)
    #  _bind([path, "bind", tagid(tag)], context, cmd, *args)
    #  self
    #end
    def tabbind(tag, context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind([path, "bind", tagid(tag)], context, cmd, *args)
      self
    end
    #def tabbind_append(tag, context, cmd=Proc.new, *args)
    #  _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
    #  self
    #end
    def tabbind_append(tag, context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
      self
    end
    def tabbind_remove(tag, context)
      _bind_remove([path, "bind", tagid(tag)], context)
      self
    end
    def tabbindinfo(tag, context=nil)
      _bindinfo([path, "bind", tagid(tag)], context)
    end

    def delete(first, last=None)
      tk_send('delete', tagindex(first), tagindex(last))
      if first.kind_of?(Tk::BLT::Tabset::Tab)
        TabID_TBL.mutex.synchronize{
          TabID_TBL[@path].delete(first.id)
        }
      end
      # middle tabs of the range are unknown
      if last.kind_of?(Tk::BLT::Tabset::Tab)
        TabID_TBL.mutex.synchronize{
          TabID_TBL[@path].delete(last.id)
        }
      end
      self
    end

    def focus(index)
      tk_send('focus', tagindex(index))
      self
    end

    def get_tab(index)
      if (idx = tk_send_without_enc('get', tagindex(index))).empty?
        nil
      else
        Tk::BLT::Tabset::Tab.id2obj(self, idx)
      end
    end
    def get_tabobj(index)
      if (idx = tk_send_without_enc('get', tagindex(index))).empty?
        nil
      else
       Tk::BLT::Tabset::Tab.new(self, nil, name, {})
      end
    end

    def index(str)
      num_or_str(tk_send('index', str))
    end
    def index_name(tab)
      num_or_str(tk_send('index', '-name', tagid(tab)))
    end

    def insert(pos, tab, keys={})
      pos = 'end' if pos.nil?
      Tk::BLT::Tabset::Tab.new(self, tagindex(pos), tagid(tab), keys)
    end
    def insert_tabs(pos, *tabs)
      pos = 'end' if pos.nil?
      if tabs[-1].kind_of?(Hash)
        keys = tabs.pop
      else
        keys = {}
      end
      fail ArgumentError, 'no tabs is given' if tabs.empty?
      tabs.map!{|tab| tagid(tab)}
      tk_send('insert', tagindex(pos), *(tabs + [keys]))
      tabs.collect{|tab| Tk::BLT::Tabset::Tab.new(self, nil, tagid(tab))}
    end

    def invoke(index)
      tk_send('invoke', tagindex(index))
    end

    def move_before(index, base_idx)
      tk_send('move', tagindex(index), 'before', tagindex(base_idx))
      self
    end
    def move_after(index, base_idx)
      tk_send('move', tagindex(index), 'after', tagindex(base_idx))
      self
    end

    def nearest(x, y)
      Tk::BLT::Tabset::Tab.id2obj(self, num_or_str(tk_send_without_enc('nearest', x, y)))
    end

    def perforation_activate(mode)
      tk_send('perforation', 'activate', mode)
      self
    end
    def perforation_highlight(index, *args)
      if args.empty?
        # index --> mode
        tk_send('perforation', 'highlight', index)
      elsif args.size == 1
        # args[0] --> mode
        tk_send('perforation', 'highlight', tagindex(index), args[0])
      else # Error: call to get Tcl's error message
        tk_send('perforation', 'highlight', tagindex(index), *args)
      end
      self
    end
    def perforation_invoke(index=nil)
      if index
        tk_send('perforation', 'invoke', tagindex(index))
      else
        tk_send('perforation', 'invoke')
      end
    end

    def scan_mark(x, y)
      tk_send_without_enc('scan', 'mark', x, y)
      self
    end
    def scan_dragto(x, y)
      tk_send_without_enc('scan', 'dragto', x, y)
      self
    end

    def see(index)
      tk_send('see', tagindex(index))
      self
    end

    def size()
      number(tk_send_without_enc('size'))
    end

    def select(index)
      tk_send('select', tagindex(index))
      self
    end

    def tab_dockall
      tk_send('tab', 'dockall')
      self
    end

    def tab_names(pat=None)
      simplelist(tk_send('tab', 'names', pat)).collect{|name|
        Tk::BLT::Tabset::Tab.id2obj(self, name)
      }
    end

    def tab_objs(pat=None)
      simplelist(tk_send('tab', 'names', pat)).collect{|name|
        Tk::BLT::Tabset::Tab.new(self, nil, name, {})
      }
    end

    def tab_ids(pat=None)
      simplelist(tk_send('tab', 'names', pat))
    end

    def tab_pageheight
      number(tk_send('tab', 'pageheight'))
    end

    def tab_pagewidth
      number(tk_send('tab', 'pagewidth'))
    end

    def tab_tearoff(index, parent=None)
      window(tk_send('tab', 'tearoff', tagindex(index), parent))
    end

    def xscrollcommand(cmd=Proc.new)
      configure_cmd 'scrollcommand', cmd
      self
    end
    alias scrollcommand xscrollcommand

    def xview(*index)
      if index.empty?
        list(tk_send_without_enc('view'))
      else
        tk_send_without_enc('view', *index)
        self
      end
    end
    alias view xview
    alias view_moveto xview_moveto
    alias view_scroll xview_scroll

    alias scrollbar xscrollbar
  end
end