summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-25 05:49:10 +0000
committernagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-25 05:49:10 +0000
commit9a47a0460792a1c7447cff0dfe1059877da87be2 (patch)
treee1049c37508f683d974133eddfc03e09eb2558cb /ext
parentf3d3cfb30dc3f8ea018ef23ae4295b10d8bdb963 (diff)
tkcanvas.rb :
* Although requiring manual control of GC, memory eating problem of TkCanvas Items is fixed. Probably, a time when GC should run is only after removing many canvas items. GC's cost is large and the man who knows proper timing to start GC is the man who create the script. So, Ruby/Tk doesn't start GC automatically. tktext.rb : * add some methods and bug fix tk.rb : * add widget destroy hook binding to TkBindTag::ALL git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/tk/lib/tk.rb14
-rw-r--r--ext/tk/lib/tkcanvas.rb9
-rw-r--r--ext/tk/lib/tktext.rb145
3 files changed, 128 insertions, 40 deletions
diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb
index d7531b2f8f..6d8618a58a 100644
--- a/ext/tk/lib/tk.rb
+++ b/ext/tk/lib/tk.rb
@@ -3088,7 +3088,9 @@ class TkObject<TkKernel
begin
cget name
rescue
- fail NameError, "undefined local variable or method `#{name}' for #{self.to_s}", error_at
+ fail NameError,
+ "undefined local variable or method `#{name}' for #{self.to_s}",
+ error_at
end
else
fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at
@@ -4773,6 +4775,15 @@ module TkClipboard
end
end
+# widget_destroy_hook
+require 'tkvirtevent'
+TkBindTag::ALL.bind(TkVirtualEvent.new('Destroy'), proc{|widget|
+ if widget.respond_to? :__destroy_hook__
+ widget.__destroy_hook__
+ end
+ }, '%W')
+
+# autoload
autoload :TkCanvas, 'tkcanvas'
autoload :TkImage, 'tkcanvas'
autoload :TkBitmapImage, 'tkcanvas'
@@ -4789,7 +4800,6 @@ autoload :TkAfter, 'tkafter'
autoload :TkTimer, 'tkafter'
autoload :TkPalette, 'tkpalette'
autoload :TkFont, 'tkfont'
-autoload :TkVirtualEvent, 'tkvirtevent'
autoload :TkBgError, 'tkbgerror'
autoload :TkManageFocus, 'tkmngfocus'
autoload :TkPalette, 'tkpalette'
diff --git a/ext/tk/lib/tkcanvas.rb b/ext/tk/lib/tkcanvas.rb
index 8c5b610f18..a98d770ccd 100644
--- a/ext/tk/lib/tkcanvas.rb
+++ b/ext/tk/lib/tkcanvas.rb
@@ -32,6 +32,10 @@ class TkCanvas<TkWindow
WidgetClassName = 'Canvas'.freeze
WidgetClassNames[WidgetClassName] = self
+ def __destroy_hook__
+ TkcItem::CItemID_TBL.delete(@path)
+ end
+
def create_self(keys)
if keys and keys != None
tk_call 'canvas', @path, *hash_kv(keys)
@@ -119,6 +123,11 @@ class TkCanvas<TkWindow
end
def delete(*args)
+ if TkcItem::CItemID_TBL[self.path]
+ find('withtag', *args).each{|item|
+ TkcItem::CItemID_TBL[self.path].delete(item.id)
+ }
+ end
tk_send 'delete', *args.collect{|t| tagid(t)}
self
end
diff --git a/ext/tk/lib/tktext.rb b/ext/tk/lib/tktext.rb
index 638be472f3..f23744af22 100644
--- a/ext/tk/lib/tktext.rb
+++ b/ext/tk/lib/tktext.rb
@@ -39,9 +39,15 @@ class TkText<TkTextWin
end
def init_instance_variable
+ @cmdtbl = {}
@tags = {}
end
+ def __destroy_hook__
+ TTagID_TBL.delete(@path)
+ TMarkID_TBL.delete(@path)
+ end
+
def create_self(keys)
if keys and keys != None
tk_call 'text', @path, *hash_kv(keys)
@@ -73,10 +79,10 @@ class TkText<TkTextWin
end
def tagid2obj(tagid)
- if not @tags[tagid]
- tagid
- else
+ if @tags[tagid]
@tags[tagid]
+ else
+ tagid
end
end
@@ -92,13 +98,36 @@ class TkText<TkTextWin
}
end
+ def mark_gravity(mark, direction=nil)
+ if direction
+ tk_send 'mark', 'gravity', mark, direction
+ self
+ else
+ tk_send 'mark', 'gravity', mark
+ end
+ end
+
+ def mark_set(mark, index)
+ tk_send 'mark', 'set', mark, index
+ self
+ end
+ alias set_mark mark_set
+
+ def mark_unset(*marks)
+ tk_send 'mark', 'unset', *marks
+ self
+ end
+ alias unset_mark mark_unset
+
def mark_next(index)
tagid2obj(tk_send('mark', 'next', index))
end
+ alias next_mark mark_next
def mark_previous(index)
tagid2obj(tk_send('mark', 'previous', index))
end
+ alias previous_mark mark_previous
def image_cget(index, slot)
case slot.to_s
@@ -259,12 +288,23 @@ class TkText<TkTextWin
self
end
alias addtag tag_add
+ alias add_tag tag_add
def tag_delete(*tags)
tk_send 'tag', 'delete', *tags
+ if TkTextTag::TTagID_TBL[@path]
+ tags.each{|tag|
+ if tag.kind_of? TkTextTag
+ TTagID_TBL[@path].delete(tag.id)
+ else
+ TTagID_TBL[@path].delete(tag)
+ end
+ }
+ end
self
end
alias deltag tag_delete
+ alias delete_tag tag_delete
def tag_bind(tag, seq, cmd=Proc.new, args=nil)
_bind([@path, 'tag', 'bind', tag], seq, cmd, args)
@@ -876,7 +916,7 @@ class TkTextTag<TkObject
def destroy
tk_call @t.path, 'tag', 'delete', @id
- TTagID_TBL[@tpath].delete(@id) if CTagID_TBL[@tpath]
+ TTagID_TBL[@tpath].delete(@id) if TTagID_TBL[@tpath]
self
end
end
@@ -884,7 +924,18 @@ end
class TkTextNamedTag<TkTextTag
def self.new(parent, name, *args)
if TTagID_TBL[parent.path] && TTagID_TBL[parent.path][name]
- return TTagID_TBL[parent.path][name]
+ tagobj = TTagID_TBL[parent.path][name]
+ if args != [] then
+ keys = args.pop
+ if keys.kind_of? Hash then
+ tagobj.add(*args) if args != []
+ tagobj.configure(keys)
+ else
+ args.push keys
+ tagobj.add(*args)
+ end
+ end
+ return tagobj
else
super(parent, name, *args)
end
@@ -894,11 +945,11 @@ class TkTextNamedTag<TkTextTag
if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect)
end
- @t = parent
+ @parent = @t = parent
@tpath = parent.path
@path = @id = name
TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath]
- TTagID_TBL[@tpath][@id] = self
+ TTagID_TBL[@tpath][@id] = self unless TTagID_TBL[@tpath][@id]
#if mode
# tk_call @t.path, "addtag", @id, *args
#end
@@ -916,27 +967,39 @@ class TkTextNamedTag<TkTextTag
end
end
-class TkTextTagSel<TkTextTag
- def initialize(parent, keys=nil)
- if not parent.kind_of?(TkText)
- fail format("%s need to be TkText", parent.inspect)
- end
- @t = parent
- @path = @id = 'sel'
- #tk_call @t.path, "tag", "configure", @id, *hash_kv(keys)
- configure(keys) if keys
- @t._addtag id, self
+class TkTextTagSel<TkTextNamedTag
+ def self.new(parent, *args)
+ super(parent, 'sel', *args)
end
end
class TkTextMark<TkObject
+ TMarkID_TBL = {}
Tk_TextMark_ID = ['mark0000']
+
+ TkComm.__add_target_for_init__(self)
+
+ def self.__init_tables__
+ TMarkID_TBL.clear
+ Tk_TextMark_ID[0] = 'mark0000'
+ end
+
+ def TkTextMark.id2obj(text, id)
+ tpath = text.path
+ return id unless TMarkID_TBL[tpath]
+ TMarkID_TBL[tpath][id]? TMarkID_TBL[tpath][id]: id
+ end
+
def initialize(parent, index)
if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect)
end
- @t = parent
+ @parent = @t = parent
+ @tpath = parent.path
@path = @id = Tk_TextMark_ID[0]
+ TMarkID_TBL[@id] = self
+ TMarkID_TBL[@tpath] = {} unless TMarkID_TBL[@tpath]
+ TMarkID_TBL[@tpath][@id] = self
Tk_TextMark_ID[0] = Tk_TextMark_ID[0].succ
tk_call @t.path, 'mark', 'set', @id, index
@t._addtag id, self
@@ -990,39 +1053,45 @@ class TkTextMark<TkObject
end
end
-class TkTextMarkInsert<TkTextMark
- def initialize(parent, index=nil)
+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
+ end
+
+ def initialize(parent, name, index=nil)
if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect)
end
- @t = parent
- @path = @id = 'insert'
+ @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 @t.path, 'mark', 'set', @id, index if index
@t._addtag id, self
end
end
+class TkTextMarkInsert<TkTextNamedMark
+ def self.new(parent,*args)
+ super(parent, 'insert', *args)
+ end
+end
+
class TkTextMarkCurrent<TkTextMark
- def initialize(parent,index=nil)
- if not parent.kind_of?(TkText)
- fail format("%s need to be TkText", parent.inspect)
- end
- @t = parent
- @path = @id = 'current'
- tk_call @t.path, 'mark', 'set', @id, index if index
- @t._addtag id, self
+ def self.new(parent,*args)
+ super(parent, 'current', *args)
end
end
class TkTextMarkAnchor<TkTextMark
- def initialize(parent,index=nil)
- if not parent.kind_of?(TkText)
- fail format("%s need to be TkText", parent.inspect)
- end
- @t = parent
- @path = @id = 'anchor'
- tk_call @t.path, 'mark', 'set', @id, index if index
- @t._addtag id, self
+ def self.new(parent,*args)
+ super(parent, 'anchor', *args)
end
end