1#
2#  tkextlib/blt/tabnotebook.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tkextlib/blt.rb'
8require 'tkextlib/blt/tabset.rb'
9
10module Tk::BLT
11  class Tabnotebook < Tabset
12    TkCommandNames = ['::blt::tabnotebook'.freeze].freeze
13    WidgetClassName = 'Tabnotebook'.freeze
14    WidgetClassNames[WidgetClassName] ||= self
15
16    class Tab < Tk::BLT::Tabset::Tab
17      def self.new(parent, pos=nil, name=nil, keys={})
18        if pos.kind_of?(Hash)
19          keys = pos
20          name = nil
21          pos  = nil
22        end
23        if name.kind_of?(Hash)
24          keys = name
25          name = nil
26        end
27        obj = nil
28        TabID_TBL.mutex.synchronize{
29          if name && TabID_TBL[parent.path] && TabID_TBL[parent.path][name]
30            obj = TabID_TBL[parent.path][name]
31            if pos
32              if pos.to_s == 'end'
33                obj.move_after('end')
34              else
35                obj.move_before(pos)
36              end
37            end
38            obj.configure if keys && ! keys.empty?
39          else
40            (obj = self.allocate).instance_eval{
41              initialize(parent, pos, name, keys)
42              TabID_TBL[@tpath] = {} unless TabID_TBL[@tpath]
43              TabID_TBL[@tpath][@id] = self
44            }
45          end
46        }
47        obj
48      end
49
50      def initialize(parent, pos, name, keys)
51        @t = parent
52        @tpath = parent.path
53        if name
54          @path = @id = name
55          unless (list(tk_call(@tpath, 'tab', 'names', @id)).empty?)
56            if pos
57              idx = tk_call(@tpath, 'index', @id)
58              if pos.to_s == 'end'
59                tk_call(@tpath, 'move', idx, 'after', 'end')
60              else
61                tk_call(@tpath, 'move', idx, 'before', pos)
62              end
63            end
64            tk_call(@tpath, 'tab', 'configure', @id, keys)
65          else
66            fail ArgumentError, "can't find tab \"#{@id}\" in #{@t}"
67          end
68        else
69          pos = 'end' unless pos
70          @path = @id = tk_call(@tpath, 'insert', pos, keys)
71        end
72      end
73    end
74
75    #######################################
76
77    def get_tab(index)
78      if (idx = tk_send_without_enc('id', tagindex(index))).empty?
79        nil
80      else
81        Tk::BLT::Tabset::Tab.id2obj(self, idx)
82      end
83    end
84    alias get_id get_tab
85
86    def get_tabobj(index)
87      if (idx = tk_send_without_enc('id', tagindex(index))).empty?
88        nil
89      else
90        Tk::BLT::Tabnotebook::Tab.new(self, nil, idx)
91      end
92    end
93
94    alias index_name index
95
96    def insert(pos=nil, keys={})
97      if pos.kind_of?(Hash)
98        keys = pos
99        pos = nil
100      end
101      pos = 'end' if pos.nil?
102      Tk::BLT::Tabnotebook::Tab.new(self, nil,
103                                    tk_send('insert', tagindex(pos), keys))
104
105    end
106    undef :insert_tabs
107
108    undef :tab_pageheight, :tab_pagewidth
109  end
110end
111