1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nagai 2003-06-25 05:49:10 +00:00
parent f3d3cfb30d
commit 9a47a04607
3 changed files with 128 additions and 40 deletions

View file

@ -3088,7 +3088,9 @@ class TkObject<TkKernel
begin begin
cget name cget name
rescue 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 end
else else
fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at
@ -4773,6 +4775,15 @@ module TkClipboard
end end
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 :TkCanvas, 'tkcanvas'
autoload :TkImage, 'tkcanvas' autoload :TkImage, 'tkcanvas'
autoload :TkBitmapImage, 'tkcanvas' autoload :TkBitmapImage, 'tkcanvas'
@ -4789,7 +4800,6 @@ autoload :TkAfter, 'tkafter'
autoload :TkTimer, 'tkafter' autoload :TkTimer, 'tkafter'
autoload :TkPalette, 'tkpalette' autoload :TkPalette, 'tkpalette'
autoload :TkFont, 'tkfont' autoload :TkFont, 'tkfont'
autoload :TkVirtualEvent, 'tkvirtevent'
autoload :TkBgError, 'tkbgerror' autoload :TkBgError, 'tkbgerror'
autoload :TkManageFocus, 'tkmngfocus' autoload :TkManageFocus, 'tkmngfocus'
autoload :TkPalette, 'tkpalette' autoload :TkPalette, 'tkpalette'

View file

@ -32,6 +32,10 @@ class TkCanvas<TkWindow
WidgetClassName = 'Canvas'.freeze WidgetClassName = 'Canvas'.freeze
WidgetClassNames[WidgetClassName] = self WidgetClassNames[WidgetClassName] = self
def __destroy_hook__
TkcItem::CItemID_TBL.delete(@path)
end
def create_self(keys) def create_self(keys)
if keys and keys != None if keys and keys != None
tk_call 'canvas', @path, *hash_kv(keys) tk_call 'canvas', @path, *hash_kv(keys)
@ -119,6 +123,11 @@ class TkCanvas<TkWindow
end end
def delete(*args) 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)} tk_send 'delete', *args.collect{|t| tagid(t)}
self self
end end

View file

@ -39,9 +39,15 @@ class TkText<TkTextWin
end end
def init_instance_variable def init_instance_variable
@cmdtbl = {}
@tags = {} @tags = {}
end end
def __destroy_hook__
TTagID_TBL.delete(@path)
TMarkID_TBL.delete(@path)
end
def create_self(keys) def create_self(keys)
if keys and keys != None if keys and keys != None
tk_call 'text', @path, *hash_kv(keys) tk_call 'text', @path, *hash_kv(keys)
@ -73,10 +79,10 @@ class TkText<TkTextWin
end end
def tagid2obj(tagid) def tagid2obj(tagid)
if not @tags[tagid] if @tags[tagid]
tagid
else
@tags[tagid] @tags[tagid]
else
tagid
end end
end end
@ -92,13 +98,36 @@ class TkText<TkTextWin
} }
end 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) def mark_next(index)
tagid2obj(tk_send('mark', 'next', index)) tagid2obj(tk_send('mark', 'next', index))
end end
alias next_mark mark_next
def mark_previous(index) def mark_previous(index)
tagid2obj(tk_send('mark', 'previous', index)) tagid2obj(tk_send('mark', 'previous', index))
end end
alias previous_mark mark_previous
def image_cget(index, slot) def image_cget(index, slot)
case slot.to_s case slot.to_s
@ -259,12 +288,23 @@ class TkText<TkTextWin
self self
end end
alias addtag tag_add alias addtag tag_add
alias add_tag tag_add
def tag_delete(*tags) def tag_delete(*tags)
tk_send '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 self
end end
alias deltag tag_delete alias deltag tag_delete
alias delete_tag tag_delete
def tag_bind(tag, seq, cmd=Proc.new, args=nil) def tag_bind(tag, seq, cmd=Proc.new, args=nil)
_bind([@path, 'tag', 'bind', tag], seq, cmd, args) _bind([@path, 'tag', 'bind', tag], seq, cmd, args)
@ -876,7 +916,7 @@ class TkTextTag<TkObject
def destroy def destroy
tk_call @t.path, 'tag', 'delete', @id 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 self
end end
end end
@ -884,7 +924,18 @@ end
class TkTextNamedTag<TkTextTag class TkTextNamedTag<TkTextTag
def self.new(parent, name, *args) def self.new(parent, name, *args)
if TTagID_TBL[parent.path] && TTagID_TBL[parent.path][name] 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 else
super(parent, name, *args) super(parent, name, *args)
end end
@ -894,11 +945,11 @@ class TkTextNamedTag<TkTextTag
if not parent.kind_of?(TkText) if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect) fail format("%s need to be TkText", parent.inspect)
end end
@t = parent @parent = @t = parent
@tpath = parent.path @tpath = parent.path
@path = @id = name @path = @id = name
TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath] TTagID_TBL[@tpath] = {} unless TTagID_TBL[@tpath]
TTagID_TBL[@tpath][@id] = self TTagID_TBL[@tpath][@id] = self unless TTagID_TBL[@tpath][@id]
#if mode #if mode
# tk_call @t.path, "addtag", @id, *args # tk_call @t.path, "addtag", @id, *args
#end #end
@ -916,27 +967,39 @@ class TkTextNamedTag<TkTextTag
end end
end end
class TkTextTagSel<TkTextTag class TkTextTagSel<TkTextNamedTag
def initialize(parent, keys=nil) def self.new(parent, *args)
if not parent.kind_of?(TkText) super(parent, 'sel', *args)
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
end end
end end
class TkTextMark<TkObject class TkTextMark<TkObject
TMarkID_TBL = {}
Tk_TextMark_ID = ['mark0000'] 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) def initialize(parent, index)
if not parent.kind_of?(TkText) if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect) fail format("%s need to be TkText", parent.inspect)
end end
@t = parent @parent = @t = parent
@tpath = parent.path
@path = @id = Tk_TextMark_ID[0] @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_TextMark_ID[0] = Tk_TextMark_ID[0].succ
tk_call @t.path, 'mark', 'set', @id, index tk_call @t.path, 'mark', 'set', @id, index
@t._addtag id, self @t._addtag id, self
@ -990,39 +1053,45 @@ class TkTextMark<TkObject
end end
end end
class TkTextMarkInsert<TkTextMark class TkTextNamedMark<TkTextMark
def initialize(parent, index=nil) 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) if not parent.kind_of?(TkText)
fail format("%s need to be TkText", parent.inspect) fail format("%s need to be TkText", parent.inspect)
end end
@t = parent @parent = @t = parent
@path = @id = 'insert' @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 tk_call @t.path, 'mark', 'set', @id, index if index
@t._addtag id, self @t._addtag id, self
end end
end end
class TkTextMarkInsert<TkTextNamedMark
def self.new(parent,*args)
super(parent, 'insert', *args)
end
end
class TkTextMarkCurrent<TkTextMark class TkTextMarkCurrent<TkTextMark
def initialize(parent,index=nil) def self.new(parent,*args)
if not parent.kind_of?(TkText) super(parent, 'current', *args)
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
end end
end end
class TkTextMarkAnchor<TkTextMark class TkTextMarkAnchor<TkTextMark
def initialize(parent,index=nil) def self.new(parent,*args)
if not parent.kind_of?(TkText) super(parent, 'anchor', *args)
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
end end
end end