diff options
Diffstat (limited to 'ext/tk/lib/tk/textmark.rb')
-rw-r--r-- | ext/tk/lib/tk/textmark.rb | 102 |
1 files changed, 70 insertions, 32 deletions
diff --git a/ext/tk/lib/tk/textmark.rb b/ext/tk/lib/tk/textmark.rb index 650d95af70..72c1ce1ab4 100644 --- a/ext/tk/lib/tk/textmark.rb +++ b/ext/tk/lib/tk/textmark.rb @@ -5,38 +5,54 @@ require 'tk' require 'tk/text' class TkTextMark<TkObject - include TkText::IndexModMethods + include Tk::Text::IndexModMethods TMarkID_TBL = TkCore::INTERP.create_table - Tk_TextMark_ID = ['mark'.freeze, '00000'.taint].freeze - TkCore::INTERP.init_ip_env{ TMarkID_TBL.clear } + (Tk_TextMark_ID = ['mark'.freeze, '00000'.taint]).instance_eval{ + @mutex = Mutex.new + def mutex; @mutex; end + freeze + } + + TkCore::INTERP.init_ip_env{ + TMarkID_TBL.mutex.synchronize{ TMarkID_TBL.clear } + } def TkTextMark.id2obj(text, id) tpath = text.path - return id unless TMarkID_TBL[tpath] - TMarkID_TBL[tpath][id]? TMarkID_TBL[tpath][id]: id + TMarkID_TBL.mutex.synchronize{ + if TMarkID_TBL[tpath] + TMarkID_TBL[tpath][id]? TMarkID_TBL[tpath][id]: id + else + id + end + } end def initialize(parent, index) - #unless parent.kind_of?(TkText) - # fail ArgumentError, "expect TkText for 1st argument" + #unless parent.kind_of?(Tk::Text) + # fail ArgumentError, "expect Tk::Text for 1st argument" #end @parent = @t = parent @tpath = parent.path - # @path = @id = Tk_TextMark_ID.join('') - @path = @id = Tk_TextMark_ID.join(TkCore::INTERP._ip_id_).freeze - TMarkID_TBL[@id] = self - TMarkID_TBL[@tpath] = {} unless TMarkID_TBL[@tpath] - TMarkID_TBL[@tpath][@id] = self - Tk_TextMark_ID[1].succ! + Tk_TextMark_ID.mutex.synchronize{ + # @path = @id = Tk_TextMark_ID.join('') + @path = @id = Tk_TextMark_ID.join(TkCore::INTERP._ip_id_).freeze + Tk_TextMark_ID[1].succ! + } + TMarkID_TBL.mutex.synchronize{ + TMarkID_TBL[@id] = self + TMarkID_TBL[@tpath] = {} unless TMarkID_TBL[@tpath] + TMarkID_TBL[@tpath][@id] = self + } tk_call_without_enc(@t.path, 'mark', 'set', @id, _get_eval_enc_str(index)) @t._addtag id, self end def id - TkText::IndexString.new(@id) + Tk::Text::IndexString.new(@id) end def exist? @@ -49,15 +65,15 @@ class TkTextMark<TkObject end =begin - # move to TkText::IndexModMethods module + # move to Tk::Text::IndexModMethods module def +(mod) return chars(mod) if mod.kind_of?(Numeric) mod = mod.to_s if mod =~ /^\s*[+-]?\d/ - TkText::IndexString.new(@id + ' + ' + mod) + Tk::Text::IndexString.new(@id + ' + ' + mod) else - TkText::IndexString.new(@id + ' ' + mod) + Tk::Text::IndexString.new(@id + ' ' + mod) end end @@ -66,11 +82,11 @@ class TkTextMark<TkObject mod = mod.to_s if mod =~ /^\s*[+-]?\d/ - TkText::IndexString.new(@id + ' - ' + mod) + Tk::Text::IndexString.new(@id + ' - ' + mod) elsif mod =~ /^\s*[-]\s+(\d.*)$/ - TkText::IndexString.new(@id + ' - -' + $1) + Tk::Text::IndexString.new(@id + ' - -' + $1) else - TkText::IndexString.new(@id + ' ' + mod) + Tk::Text::IndexString.new(@id + ' ' + mod) end end =end @@ -121,46 +137,68 @@ class TkTextMark<TkObject end end end +TktMark = TkTextMark class TkTextNamedMark<TkTextMark - def self.new(parent, name, *args) - if TMarkID_TBL[parent.path] && TMarkID_TBL[parent.path][name] - return TMarkID_TBL[parent.path][name] - else - super(parent, name, *args) - end + def self.new(parent, name, index=nil) + TMarkID_TBL.mutex.synchronize{ + if TMarkID_TBL[parent.path] && TMarkID_TBL[parent.path][name] + obj = TMarkID_TBL[parent.path][name] + else + # super(parent, name, *args) + (obj = self.allocate).instance_eval{ + @parent = @t = parent + @tpath = parent.path + @path = @id = name + TMarkID_TBL[@id] = self + TMarkID_TBL[@tpath] = {} unless TMarkID_TBL[@tpath] + TMarkID_TBL[@tpath][@id] = self unless TMarkID_TBL[@tpath][@id] + @t._addtag @id, self + } + obj + end + + if obj && index + tk_call_without_enc(parent.path, 'mark', 'set', name, + _get_eval_enc_str(index)) + end + obj + } end def initialize(parent, name, index=nil) - #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 - TMarkID_TBL[@id] = self - TMarkID_TBL[@tpath] = {} unless TMarkID_TBL[@tpath] - TMarkID_TBL[@tpath][@id] = self unless TMarkID_TBL[@tpath][@id] tk_call_without_enc(@t.path, 'mark', 'set', @id, _get_eval_enc_str(index)) if index - @t._addtag id, self + @t._addtag @id, self end end +TktNamedMark = TkTextNamedMark class TkTextMarkInsert<TkTextNamedMark def self.new(parent,*args) super(parent, 'insert', *args) end end +TktMarkInsert = TkTextMarkInsert class TkTextMarkCurrent<TkTextNamedMark def self.new(parent,*args) super(parent, 'current', *args) end end +TktMarkCurrent = TkTextMarkCurrent class TkTextMarkAnchor<TkTextNamedMark def self.new(parent,*args) super(parent, 'anchor', *args) end end +TktMarkAnchor = TkTextMarkAnchor |