~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/lib/tkextlib/blt/tabset.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#  tkextlib/blt/tabset.rb
 
3
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
 
4
#
 
5
 
 
6
require 'tk'
 
7
require 'tkextlib/blt.rb'
 
8
 
 
9
module Tk::BLT
 
10
  class Tabset < TkWindow
 
11
    class Tab < TkObject
 
12
      include TkTreatItemFont
 
13
 
 
14
      TabID_TBL = TkCore::INTERP.create_table
 
15
      TabsetTab_ID = ['blt_tabset_tab'.freeze, '00000'.taint].freeze
 
16
 
 
17
      TkCore::INTERP.init_ip_env{ TabID_TBL.clear }
 
18
 
 
19
      def self.id2obj(tabset, id)
 
20
        tpath = tabset.path
 
21
        return id unless TabID_TBL[tpath]
 
22
        TabID_TBL[tpath][id]? TabID_TBL[tpath]: id
 
23
      end
 
24
 
 
25
      def self.new(parent, pos=nil, name=nil, keys={})
 
26
        if pos.kind_of?(Hash)
 
27
          keys = pos
 
28
          name = nil
 
29
          pos  = nil
 
30
        end
 
31
        if name.kind_of?(Hash)
 
32
          keys = name
 
33
          name = nil
 
34
        end
 
35
 
 
36
        if name && TabID_TBL[parent.path] && TabID_TBL[parent.path][name]
 
37
          TabID_TBL[parent.path][name]
 
38
        else
 
39
          super(parent, pos, name, keys)
 
40
        end
 
41
      end
 
42
 
 
43
      def initialize(parent, pos, name, keys)
 
44
        @t = parent
 
45
        @tpath = parent.path
 
46
        if name
 
47
          @path = @id = name
 
48
          TabID_TBL[@tpath] = {} unless TabID_TBL[@tpath]
 
49
          TabID_TBL[@tpath][@id] = self
 
50
 
 
51
          unless (list(tk_call(@tpath, 'tab', 'names', @id)).empty?)
 
52
            if pos
 
53
              idx = tk_call(@tpath, 'index', '-name', @id)
 
54
              if pos.to_s == 'end'
 
55
                tk_call(@tpath, idx, 'moveto', 'after', 'end')
 
56
              else
 
57
                tk_call(@tpath, idx, 'moveto', 'before', pos)
 
58
              end
 
59
            end
 
60
            tk_call(@tpath, 'tab', 'configure', @id, keys)
 
61
            return
 
62
          end
 
63
 
 
64
        else
 
65
          @path = @id = TabsetTab_ID.join(TkCore::INTERP._ip_id_)
 
66
          TabID_TBL[@tpath] = {} unless TabID_TBL[@tpath]
 
67
          TabID_TBL[@tpath][@id] = self
 
68
          TabsetTab_ID[1].succ!
 
69
        end
 
70
 
 
71
        pos = 'end' unless pos
 
72
        tk_call(@tpath, 'insert', pos, @id, keys)
 
73
      end
 
74
 
 
75
      #def bind(context, cmd=Proc.new, *args)
 
76
      #  @t.tab_bind(@id, context, cmd, *args)
 
77
      #  self
 
78
      #end
 
79
      def bind(context, *args)
 
80
        # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
 
81
        if TkComm._callback_entry?(args[0]) || !block_given?
 
82
          cmd = args.shift
 
83
        else
 
84
          cmd = Proc.new
 
85
        end
 
86
        @t.tab_bind(@id, context, cmd, *args)
 
87
        self
 
88
      end
 
89
      #def bind_append(context, cmd=Proc.new, *args)
 
90
      #  @t.tab_bind_append(@id, context, cmd, *args)
 
91
      #  self
 
92
      #end
 
93
      def bind_append(context, *args)
 
94
        # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
 
95
        if TkComm._callback_entry?(args[0]) || !block_given?
 
96
          cmd = args.shift
 
97
        else
 
98
          cmd = Proc.new
 
99
        end
 
100
        @t.tab_bind_append(@id, context, cmd, *args)
 
101
        self
 
102
      end
 
103
      def bind_remove(context)
 
104
        @t.tab_bind_remove(@id, context)
 
105
        self
 
106
      end
 
107
      def bindinfo(context=nil)
 
108
        @t.tab_bindinfo(@id, context)
 
109
      end
 
110
 
 
111
      def cget(*args)
 
112
        @t.tab_cget(@id, *args)
 
113
      end
 
114
      def configure(*args)
 
115
        @t.tab_configure(@id, *args)
 
116
      end
 
117
      def configinfo(*args)
 
118
        @t.tab_configinfo(@id, *args)
 
119
      end
 
120
      def current_configinfo(*args)
 
121
        @t.current_tab_configinfo(@id, *args)
 
122
      end
 
123
 
 
124
      def delete()
 
125
        @t.delete(@id)
 
126
        TabID_TBL[@tpath].delete(@id)
 
127
        self
 
128
      end
 
129
 
 
130
      def get_name()
 
131
        @id.dup
 
132
      end
 
133
 
 
134
      def focus()
 
135
        @t.focus(self.index)
 
136
      end
 
137
 
 
138
      def index()
 
139
        @t.index_name(@id)
 
140
      end
 
141
 
 
142
      def invoke()
 
143
        @t.invoke(self.index)
 
144
      end
 
145
 
 
146
      def move_before(idx)
 
147
        @t.move_before(self.index, idx)
 
148
      end
 
149
      def move_after(idx)
 
150
        @t.move_after(self.index, idx)
 
151
      end
 
152
 
 
153
      def perforation_highlight(mode)
 
154
        @t.perforation.highlight(self.index, mode)
 
155
      end
 
156
      def perforation_invoke()
 
157
        @t.perforation.invoke(self.index)
 
158
      end
 
159
 
 
160
      def see()
 
161
        @t.see(self.index)
 
162
      end
 
163
 
 
164
      def tearoff(name=None)
 
165
        @t.tab_tearoff(self.index, *args)
 
166
      end
 
167
    end
 
168
 
 
169
    ########################################
 
170
 
 
171
    class NamedTab < Tab
 
172
      def self.new(parent, name)
 
173
        super(parent, nil, name, {})
 
174
      end
 
175
    end
 
176
 
 
177
    ########################################
 
178
 
 
179
    include X_Scrollable
 
180
    include TkItemConfigMethod
 
181
 
 
182
    TkCommandNames = ['::blt::tabset'.freeze].freeze
 
183
    WidgetClassName = 'Tabset'.freeze
 
184
    WidgetClassNames[WidgetClassName] = self
 
185
 
 
186
    def __destroy_hook__
 
187
      Tk::BLT::Tabset::Tab::TabID_TBL.delete(@path)
 
188
    end
 
189
 
 
190
    ########################################
 
191
 
 
192
    def __boolval_optkeys
 
193
      super() << 'samewidth' << 'tearoff'
 
194
    end
 
195
    private :__strval_optkeys
 
196
 
 
197
    def __strval_optkeys
 
198
      super() << 'tabbackground' << 'tabforeground'
 
199
    end
 
200
    private :__strval_optkeys
 
201
 
 
202
    def __item_cget_cmd(id)
 
203
      [self.path, 'tab', 'cget', id]
 
204
    end
 
205
    private :__item_cget_cmd
 
206
 
 
207
    def __item_config_cmd(id)
 
208
      [self.path, 'tab', 'configure', id]
 
209
    end
 
210
    private :__item_config_cmd
 
211
 
 
212
    def __item_pathname(tagOrId)
 
213
      if tagOrId.kind_of?(Tk::BLT::Tabset::Tab)
 
214
        self.path + ';' + tagOrId.id.to_s
 
215
      else
 
216
        self.path + ';' + tagOrId.to_s
 
217
      end
 
218
    end
 
219
    private :__item_pathname
 
220
 
 
221
    alias tab_cget itemcget
 
222
    alias tab_configure itemconfigure
 
223
    alias tab_configinfo itemconfiginfo
 
224
    alias current_tab_configinfo current_itemconfiginfo
 
225
 
 
226
    def __item_strval_optkeys(id)
 
227
      super(id) << 'shadow'
 
228
    end
 
229
    private :__item_strval_optkeys
 
230
 
 
231
    def tagid(tab)
 
232
      if tab.kind_of?(Tk::BLT::Tabset::Tab)
 
233
        tab.id
 
234
      else
 
235
        tab
 
236
      end
 
237
    end
 
238
 
 
239
    def tagindex(tab)
 
240
      if tab.kind_of?(Tk::BLT::Tabset::Tab)
 
241
        tab.index
 
242
      else
 
243
        tab
 
244
      end
 
245
    end
 
246
 
 
247
    ########################################
 
248
 
 
249
    def activate(index)
 
250
      tk_send('activate', tagindex(index))
 
251
      self
 
252
    end
 
253
    alias highlight activate
 
254
 
 
255
    #def tabbind(tag, context, cmd=Proc.new, *args)
 
256
    #  _bind([path, "bind", tagid(tag)], context, cmd, *args)
 
257
    #  self
 
258
    #end
 
259
    def tabbind(tag, context, *args)
 
260
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
 
261
      if TkComm._callback_entry?(args[0]) || !block_given?
 
262
        cmd = args.shift
 
263
      else
 
264
        cmd = Proc.new
 
265
      end
 
266
      _bind([path, "bind", tagid(tag)], context, cmd, *args)
 
267
      self
 
268
    end
 
269
    #def tabbind_append(tag, context, cmd=Proc.new, *args)
 
270
    #  _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
 
271
    #  self
 
272
    #end
 
273
    def tabbind_append(tag, context, *args)
 
274
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
 
275
      if TkComm._callback_entry?(args[0]) || !block_given?
 
276
        cmd = args.shift
 
277
      else
 
278
        cmd = Proc.new
 
279
      end
 
280
      _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
 
281
      self
 
282
    end
 
283
    def tabbind_remove(tag, context)
 
284
      _bind_remove([path, "bind", tagid(tag)], context)
 
285
      self
 
286
    end
 
287
    def tabbindinfo(tag, context=nil)
 
288
      _bindinfo([path, "bind", tagid(tag)], context)
 
289
    end
 
290
 
 
291
    def delete(first, last=None)
 
292
      tk_send('delete', tagindex(first), tagindex(last))
 
293
      if first.kind_of?(Tk::BLT::Tabset::Tab)
 
294
        TabID_TBL[@path].delete(first.id)
 
295
      end
 
296
      # middle tabs of the range are unknown
 
297
      if last.kind_of?(Tk::BLT::Tabset::Tab)
 
298
        TabID_TBL[@path].delete(last.id)
 
299
      end
 
300
      self
 
301
    end
 
302
 
 
303
    def focus(index)
 
304
      tk_send('focus', tagindex(index))
 
305
      self
 
306
    end
 
307
 
 
308
    def get_tab(index)
 
309
      Tk::BLT::Tabset::Tab.id2obj(tk_send_without_enc('get', tagindex(index)))
 
310
    end
 
311
 
 
312
    def index(str)
 
313
      num_or_str(tk_send('index', str))
 
314
    end
 
315
    def index_name(tab)
 
316
      num_or_str(tk_send('index', '-mame', tagid(tab)))
 
317
    end
 
318
 
 
319
    def insert(pos, tab, keys={})
 
320
      Tk::BLT::Tabset::Tab.new(self, tagindex(pos), tagid(tab), keys)
 
321
    end
 
322
 
 
323
    def invoke(index)
 
324
      tk_send('invoke', tagindex(index))
 
325
    end
 
326
 
 
327
    def move_before(index, base_idx)
 
328
      tk_send('move', tagindex(index), 'before', tagindex(base_idx))
 
329
      self
 
330
    end
 
331
    def move_after(index, base_idx)
 
332
      tk_send('move', tagindex(index), 'after', tagindex(base_idx))
 
333
      self
 
334
    end
 
335
 
 
336
    def nearest(x, y)
 
337
      Tk::BLT::Tabset::Tab.id2obj(num_or_str(tk_send_without_enc('nearest', x, y)))
 
338
    end
 
339
 
 
340
    def perforation_highlight(index, mode)
 
341
      tk_send('perforation', 'highlight', tagindex(index), mode)
 
342
      self
 
343
    end
 
344
    def perforation_invoke(index)
 
345
      tk_send('perforation', 'invoke', tagindex(index))
 
346
    end
 
347
 
 
348
    def scan_mark(x, y)
 
349
      tk_send_without_enc('scan', 'mark', x, y)
 
350
      self
 
351
    end
 
352
    def scan_dragto(x, y)
 
353
      tk_send_without_enc('scan', 'dragto', x, y)
 
354
      self
 
355
    end
 
356
 
 
357
    def see(index)
 
358
      tk_send('see', tagindex(index))
 
359
      self
 
360
    end
 
361
 
 
362
    def size()
 
363
      number(tk_send_without_enc('size'))
 
364
    end
 
365
 
 
366
    def select(index)
 
367
      tk_send('select', tagindex(index))
 
368
      self
 
369
    end
 
370
 
 
371
    def tab_names(pat=None)
 
372
      simplelist(tk_send('tab', 'names', pat)).collect{|name|
 
373
        Tk::BLT::Tabset::Tab.id2obj(name)
 
374
      }
 
375
    end
 
376
 
 
377
    def tab_tearoff(index, name=None)
 
378
      window(tk_send('tab', 'tearoff', tagindex(index), name))
 
379
    end
 
380
 
 
381
    def xscrollcommand(cmd=Proc.new)
 
382
      configure_cmd 'scrollcommand', cmd
 
383
      self
 
384
    end
 
385
    alias scrollcommand xscrollcommand
 
386
 
 
387
    def xview(*index)
 
388
      if index.empty?
 
389
        list(tk_send_without_enc('view'))
 
390
      else
 
391
        tk_send_without_enc('view', *index)
 
392
        self
 
393
      end
 
394
    end
 
395
    alias view xview
 
396
    alias view_moveto xview_moveto
 
397
    alias view_scroll xview_scroll
 
398
 
 
399
    alias scrollbar xscrollbar
 
400
  end
 
401
end