diff options
Diffstat (limited to 'ext/tk/lib/tk/texttag.rb')
-rw-r--r-- | ext/tk/lib/tk/texttag.rb | 110 |
1 files changed, 73 insertions, 37 deletions
diff --git a/ext/tk/lib/tk/texttag.rb b/ext/tk/lib/tk/texttag.rb index cc2c56210f..792d544fe7 100644 --- a/ext/tk/lib/tk/texttag.rb +++ b/ext/tk/lib/tk/texttag.rb @@ -7,17 +7,29 @@ require 'tk/tagfont' class TkTextTag<TkObject include TkTreatTagFont - include TkText::IndexModMethods + include Tk::Text::IndexModMethods TTagID_TBL = TkCore::INTERP.create_table - Tk_TextTag_ID = ['tag'.freeze, '00000'.taint].freeze - TkCore::INTERP.init_ip_env{ TTagID_TBL.clear } + (Tk_TextTag_ID = ['tag'.freeze, '00000'.taint]).instance_eval{ + @mutex = Mutex.new + def mutex; @mutex; end + freeze + } + + TkCore::INTERP.init_ip_env{ + TTagID_TBL.mutex.synchronize{ TTagID_TBL.clear } + } def TkTextTag.id2obj(text, id) tpath = text.path - return id unless TTagID_TBL[tpath] - TTagID_TBL[tpath][id]? TTagID_TBL[tpath][id]: id + TTagID_TBL.mutex.synchronize{ + if TTagID_TBL[tpath] + TTagID_TBL[tpath][id]? TTagID_TBL[tpath][id]: id + else + id + end + } end def initialize(parent, *args) @@ -26,12 +38,16 @@ class TkTextTag<TkObject #end @parent = @t = parent @tpath = parent.path - # @path = @id = Tk_TextTag_ID.join('') - @path = @id = Tk_TextTag_ID.join(TkCore::INTERP._ip_id_).freeze - # TTagID_TBL[@id] = self - TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath] - TTagID_TBL[@tpath][@id] = self - Tk_TextTag_ID[1].succ! + Tk_TextTag_ID.mutex.synchronize{ + # @path = @id = Tk_TextTag_ID.join('') + @path = @id = Tk_TextTag_ID.join(TkCore::INTERP._ip_id_).freeze + Tk_TextTag_ID[1].succ! + } + TTagID_TBL.mutex.synchronize{ + TTagID_TBL[@id] = self + TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath] + TTagID_TBL[@tpath][@id] = self + } #tk_call @t.path, "tag", "configure", @id, *hash_kv(keys) if args != [] keys = args.pop @@ -47,7 +63,7 @@ class TkTextTag<TkObject end def id - TkText::IndexString.new(@id) + Tk::Text::IndexString.new(@id) end def exist? @@ -60,11 +76,11 @@ class TkTextTag<TkObject end def first - TkText::IndexString.new(@id + '.first') + Tk::Text::IndexString.new(@id + '.first') end def last - TkText::IndexString.new(@id + '.last') + Tk::Text::IndexString.new(@id + '.last') end def add(*indices) @@ -83,7 +99,7 @@ class TkTextTag<TkObject l = tk_split_simplelist(tk_call_without_enc(@t.path, 'tag', 'ranges', @id)) r = [] while key=l.shift - r.push [TkText::IndexString.new(key), TkText::IndexString.new(l.shift)] + r.push [Tk::Text::IndexString.new(key), Tk::Text::IndexString.new(l.shift)] end r end @@ -92,7 +108,7 @@ class TkTextTag<TkObject simplelist(tk_call_without_enc(@t.path, 'tag', 'nextrange', @id, _get_eval_enc_str(first), _get_eval_enc_str(last))).collect{|idx| - TkText::IndexString.new(idx) + Tk::Text::IndexString.new(idx) } end @@ -100,7 +116,7 @@ class TkTextTag<TkObject simplelist(tk_call_without_enc(@t.path, 'tag', 'prevrange', @id, _get_eval_enc_str(first), _get_eval_enc_str(last))).collect{|idx| - TkText::IndexString.new(idx) + Tk::Text::IndexString.new(idx) } end @@ -221,40 +237,58 @@ class TkTextTag<TkObject def destroy tk_call_without_enc(@t.path, 'tag', 'delete', @id) - TTagID_TBL[@tpath].delete(@id) if TTagID_TBL[@tpath] + TTagID_TBL.mutex.synchronize{ + TTagID_TBL[@tpath].delete(@id) if TTagID_TBL[@tpath] + } self end end +TktTag = TkTextTag class TkTextNamedTag<TkTextTag def self.new(parent, name, *args) - if TTagID_TBL[parent.path] && TTagID_TBL[parent.path][name] - tagobj = TTagID_TBL[parent.path][name] - if args != [] - keys = args.pop - if keys.kind_of?(Hash) - tagobj.add(*args) if args != [] - tagobj.configure(keys) - else - args.push keys - tagobj.add(*args) - end + tagobj = nil + TTagID_TBL.mutex.synchronize{ + if TTagID_TBL[parent.path] && TTagID_TBL[parent.path][name] + tagobj = TTagID_TBL[parent.path][name] + else + # super(parent, name, *args) + (tagobj = self.allocate).instance_eval{ + @parent = @t = parent + @tpath = parent.path + @path = @id = name + TTagID_TBL[@id] = self + TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath] + TTagID_TBL[@tpath][@id] = self unless TTagID_TBL[@tpath][@id] + @t._addtag @id, self + } + end + } + + if args != [] + keys = args.pop + if keys.kind_of?(Hash) + tagobj.add(*args) if args != [] + tagobj.configure(keys) + else + args.push keys + tagobj.add(*args) end - return tagobj - else - super(parent, name, *args) end + + tagobj end def initialize(parent, name, *args) - #unless parent.kind_of?(TkText) - # fail ArgumentError, "expect TkText for 1st argument" + # dummy:: not called by 'new' method + + #unless parent.kind_of?(Tk::Text) + # fail ArgumentError, "expect Tk::Text for 1st argument" #end @parent = @t = parent @tpath = parent.path @path = @id = name - TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath] - TTagID_TBL[@tpath][@id] = self unless TTagID_TBL[@tpath][@id] + #if mode # tk_call @t.path, "addtag", @id, *args #end @@ -268,12 +302,14 @@ class TkTextNamedTag<TkTextTag add(*args) end end - @t._addtag id, self + @t._addtag @id, self end end +TktNamedTag = TkTextNamedTag class TkTextTagSel<TkTextNamedTag def self.new(parent, *args) super(parent, 'sel', *args) end end +TktTagSel = TkTextTagSel |