summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-07-03 07:06:51 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-07-03 07:06:51 +0000
commitab801dbdb7ff8a99b5e0976516b879b27bcf3e1b (patch)
tree2657a1ca78c166beda5dfb609f9c53c5bae6f85c /lib
parent1a2003d1f176001f4c691d14a080e722bb12fc7b (diff)
1.1b9_29
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@260 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/mkmf.rb2
-rw-r--r--lib/readbytes.rb36
-rw-r--r--lib/tk.rb82
-rw-r--r--lib/tkafter.rb76
-rw-r--r--lib/tkcanvas.rb560
-rw-r--r--lib/tkentry.rb24
-rw-r--r--lib/tktext.rb242
7 files changed, 816 insertions, 206 deletions
diff --git a/lib/mkmf.rb b/lib/mkmf.rb
index 306f23905b..c650b497df 100644
--- a/lib/mkmf.rb
+++ b/lib/mkmf.rb
@@ -263,7 +263,7 @@ hdrdir = #{$hdrdir}
CC = #{CONFIG["CC"]}
prefix = #{CONFIG["prefix"]}
-CFLAGS = #{CONFIG["CCDLFLAGS"]} -I#{$hdrdir} -I#{CONFIG["includedir"]} #{CFLAGS} #{$CFLAGS} #{$defs.join(" ")}
+CFLAGS = #{CONFIG["CCDLFLAGS"]} -I$(hdrdir) -I#{CONFIG["includedir"]} #{CFLAGS} #{$CFLAGS} #{$defs.join(" ")}
DLDFLAGS = #{$DLDFLAGS} #{$LDFLAGS}
LDSHARED = #{CONFIG["LDSHARED"]}
diff --git a/lib/readbytes.rb b/lib/readbytes.rb
new file mode 100644
index 0000000000..d6a3b10afe
--- /dev/null
+++ b/lib/readbytes.rb
@@ -0,0 +1,36 @@
+# readbytes.rb
+#
+# add IO#readbytes, which reads fixed sized data.
+# it guarantees read data size.
+
+class TruncatedDataError<IOError
+ def initialize(mesg, data)
+ @data = data
+ super(mesg)
+ end
+ attr_reader :data
+end
+
+class IO
+ def readbytes(n)
+ str = read(n)
+ if str == nil
+ raise EOFError, "End of file reached"
+ end
+ if str.size < n
+ raise TruncatedDataError.new("data truncated", str)
+ end
+ str
+ end
+end
+
+if __FILE__ == $0
+ begin
+ loop do
+ print STDIN.readbytes(6)
+ end
+ rescue TruncatedDataError
+ p $!.data
+ raise
+ end
+end
diff --git a/lib/tk.rb b/lib/tk.rb
index 204e22ac37..287bda62af 100644
--- a/lib/tk.rb
+++ b/lib/tk.rb
@@ -284,29 +284,6 @@ module TkComm
TkGrid.configure *args
end
- def after(ms, cmd=Proc.new)
- myid = _curr_cmd_id
- INTERP._eval('after '+ms+' '+_get_eval_string(install_cmd(proc{
- TkUtil.eval_cmd cmd
- uninstall_cmd myid
- })))
- return
- if false #defined? Thread
- Thread.start do
- ms = Float(ms)/1000
- ms = 10 if ms == 0
- sleep ms/1000
- cmd.call
- end
- else
- myid = _curr_cmd_id
- INTERP._eval('after '+ms+' '+_get_eval_string(install_cmd(proc{
- TkUtil.eval_cmd cmd
- uninstall_cmd myid
- })))
- end
- end
-
def update(idle=nil)
if idle
tk_call 'update', 'idletasks'
@@ -324,6 +301,24 @@ module TkCore
INTERP = TclTkIp.new
INTERP._invoke("proc", "rb_out", "args", "ruby [format \"TkCore.callback %%Q!%s!\" $args]")
+ def after(ms, cmd=Proc.new)
+ myid = _curr_cmd_id
+ cmdid = install_cmd(cmd)
+ tk_call("after",ms,cmdid)
+ return
+ if false #defined? Thread
+ Thread.start do
+ ms = Float(ms)/1000
+ ms = 10 if ms == 0
+ sleep ms/1000
+ cmd.call
+ end
+ else
+ cmdid = install_cmd(cmd)
+ tk_call("after",ms,cmdid)
+ end
+ end
+
def TkCore.callback(arg)
arg = Array(tk_split_list(arg))
_get_eval_string(TkUtil.eval_cmd(Tk_CMDTBL[arg.shift], *arg))
@@ -1029,13 +1024,12 @@ class TkObject<TkKernel
tk_tcl2ruby tk_call path, 'cget', "-#{slot}"
end
- def configure(slot, value)
- if value == FALSE
- value = "0"
- elsif value.kind_of? Proc
- value = install_cmd(value)
- end
- tk_call path, 'configure', "-#{slot}", value
+ def configure(slot, value=None)
+ if slot.kind_of? Hash
+ tk_call path, 'configure', *hash_kv(slot)
+ else
+ tk_call path, 'configure', "-#{slot}", value
+ end
end
def configure_cmd(slot, value)
@@ -1500,19 +1494,23 @@ module TkComposite
end
end
- def configure(slot, value)
- if @delegates and @delegates[slot]
- for i in @delegates[slot]
- if not i
- i = @delegates['DEFALUT']
- redo
- else
- last = i.configure(slot, value)
+ def configure(slot, value=None)
+ if slot.kind_of? Hash
+ slot.each{|slot,value| configure slot, value}
+ else
+ if @delegates and @delegates[slot]
+ for i in @delegates[slot]
+ if not i
+ i = @delegates['DEFALUT']
+ redo
+ else
+ last = i.configure(slot, value)
+ end
end
+ last
+ else
+ super
end
- last
- else
- super
end
end
end
@@ -1550,3 +1548,5 @@ autoload :TkEntry, 'tkentry'
autoload :TkText, 'tktext'
autoload :TkDialog, 'tkdialog'
autoload :TkMenubar, 'tkmenubar'
+autoload :TkAfter, 'tkafter'
+autoload :TkPalette, 'tkpalette'
diff --git a/lib/tkafter.rb b/lib/tkafter.rb
index 708d051002..23fc87dedb 100644
--- a/lib/tkafter.rb
+++ b/lib/tkafter.rb
@@ -1,6 +1,6 @@
#
# tkafter.rb : methods for Tcl/Tk after command
-# 1998/06/23 by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
+# 1998/07/02 by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
#
require 'tk'
@@ -20,10 +20,9 @@ class TkAfter
@after_id = nil
arg = Array(tk_split_list(arg))
obj_id = arg.shift
- return nil if Tk_CBTBL[obj_id] == nil; # canceled
- ret = _get_eval_string(Tk_CBTBL[obj_id].do_callback(*arg))
- Tk_CBTBL[obj_id].set_next_callback(*arg)
- ret
+ ex_obj = Tk_CBTBL[obj_id]
+ return nil if ex_obj == nil; # canceled
+ _get_eval_string(ex_obj.do_callback(*arg))
end
def TkAfter.info
@@ -37,7 +36,15 @@ class TkAfter
# instance methods
###############################
def do_callback(*args)
- @current_proc.call(*args)
+ @in_callback = true
+ ret = @current_proc.call(*args)
+ if @set_next
+ set_next_callback(*args)
+ else
+ @set_next = true
+ end
+ @in_callback = false
+ ret
end
def set_callback(sleep, args=nil)
@@ -49,6 +56,7 @@ class TkAfter
def set_next_callback(*args)
if @running == false || @proc_max == 0 || @do_loop == 0
Tk_CBTBL[@id] = nil ;# for GC
+ @running = false
return
end
if @current_pos >= @proc_max
@@ -56,6 +64,7 @@ class TkAfter
@current_pos = 0
else
Tk_CBTBL[@id] = nil ;# for GC
+ @running = false
return
end
end
@@ -88,9 +97,11 @@ class TkAfter
@id = format("a%.4d", Tk_CBID[0])
Tk_CBID[0] += 1
- @init_sleep=0
- @init_proc=nil
- @init_args=[]
+ @set_next = true
+
+ @init_sleep = 0
+ @init_proc = nil
+ @init_args = []
@current_script = []
@current_proc = nil
@@ -188,37 +199,56 @@ class TkAfter
self
end
- def start(sleep=0, init_proc=nil, *init_args)
+ def set_start_proc(sleep, init_proc, *init_args)
+ if !sleep == 'idle' && !sleep.kind_of?(Integer)
+ fail format("%s need to be Integer", sleep.inspect)
+ end
+ @init_proc = init_proc
+ @init_args = init_args
+ self
+ end
+
+ def start(*init_args)
return nil if @running
Tk_CBTBL[@id] = self
@do_loop = @loop_exec
@current_pos = 0
- if !sleep == 'idle' && !sleep.kind_of?(Integer)
- fail format("%s need to be Integer", sleep.inspect)
+ argc = init_args.size
+ if argc > 0
+ sleep = init_args.shift
+ if !sleep == 'idle' && !sleep.kind_of?(Integer)
+ fail format("%s need to be Integer", sleep.inspect)
+ end
+ @init_sleep = sleep
end
+ @init_proc = init_args.shift if argc > 1
+ @init_args = init_args if argc > 0
- @init_proc = init_proc
- @init_args = init_args
- @current_sleep = @init_sleep = sleep
+ @current_sleep = @init_sleep
@running = true
- if init_proc
- if not init_proc.kind_of? Proc
- fail format("%s need to be Proc", init_proc.inspect)
+ if @init_proc
+ if not @init_proc.kind_of? Proc
+ fail format("%s need to be Proc", @init_proc.inspect)
end
- @current_proc = init_proc
- set_callback(sleep, init_args)
+ @current_proc = @init_proc
+ set_callback(sleep, @init_args)
+ @set_next = false if @in_callback
else
- set_next_callback(*init_args)
+ set_next_callback(*@init_args)
end
self
end
- def restart
+ def restart(*restart_args)
cancel if @running
- start(@init_sleep, @init_proc, @init_args)
+ if restart_args == []
+ start(@init_sleep, @init_proc, *@init_args)
+ else
+ start(*restart_args)
+ end
end
def cancel
diff --git a/lib/tkcanvas.rb b/lib/tkcanvas.rb
index a02db097fd..1d4c4c583a 100644
--- a/lib/tkcanvas.rb
+++ b/lib/tkcanvas.rb
@@ -2,6 +2,8 @@
# tkcanvas.rb - Tk canvas classes
# $Date$
# by Yukihiro Matsumoto <matz@caelum.co.jp>
+# $Date$
+# by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
require "tk"
@@ -9,107 +11,224 @@ class TkCanvas<TkWindow
def create_self
tk_call 'canvas', path
end
+
def tagid(tag)
- if tag.kind_of?(TkcItem)
+ if tag.kind_of?(TkcItem) || tag.kind_of?(TkcTag)
tag.id
else
tag
end
end
private :tagid
- def addtag(tag, *args)
- tk_send 'addtag', tagid(tag), *args
+
+ def addtag(tag, mode, *args)
+ tk_send 'addtag', tagid(tag), mode, *args
end
- def addtag_above(tagOrId)
- addtag('above', tagOrId)
+ def addtag_above(tagOrId, target)
+ addtag(tagOrId, 'above', tagid(target))
end
- def addtag_all
- addtag('all')
+ def addtag_all(tagOrId)
+ addtag(tagOrId, 'all')
end
- def addtag_below(tagOrId)
- addtag('below', tagOrId)
+ def addtag_below(tagOrId, target)
+ addtag(tagOrId, 'below', tagid(target))
end
- def addtag_closest(x, y, halo=None, start=None)
- addtag('closest', x, y, halo, start)
+ def addtag_closest(tagOrId, x, y, halo=None, start=None)
+ addtag(tagOrId, 'closest', x, y, halo, start)
end
- def addtag_enclosed(x1, y1, x2, y2)
- addtag('enclosed', x1, y1, x2, y2)
+ def addtag_enclosed(tagOrId, x1, y1, x2, y2)
+ addtag(tagOrId, 'enclosed', x1, y1, x2, y2)
end
- def addtag_overlapping(x1, y1, x2, y2)
- addtag('overlapping', x1, y1, x2, y2)
+ def addtag_overlapping(tagOrId, x1, y1, x2, y2)
+ addtag(tagOrId, 'overlapping', x1, y1, x2, y2)
end
- def addtag_withtag(tagOrId)
- addtag('withtag', tagOrId)
+ def addtag_withtag(tagOrId, tag)
+ addtag(tagOrId, 'withtag', tagid(tag))
end
- def bbox(tag)
- list(tk_send('bbox', tagid(tag)))
+
+ def bbox(tagOrId, *tags)
+ list(tk_send('bbox', tagid(tagOrId), *tags))
end
- def itembind(tag, seq, cmd=Proc.new)
- id = install_cmd(cmd)
- tk_send 'bind', tagid(tag), "<#{seq}>", id
- @cmdtbl.push id
+
+ def itembind(tag, context, cmd=Proc.new, args=nil)
+ context = context.join("><") if context.kind_of? Array
+ if /,/ =~ context
+ context = context.split(/\s*,\s*/).join("><")
+ end
+ id = install_bind(cmd, args)
+ begin
+ tk_send 'bind', tagid(tag), "<#{context}>", id
+ rescue
+ uninstall_cmd(cmd)
+ fail
+ end
+ # @cmdtbl.push id
end
+
def canvasx(x, *args)
- tk_send 'canvasx', x, *args
+ tk_tcl2ruby(tk_send 'canvasx', x, *args)
end
def canvasy(y, *args)
- tk_send 'canvasy', y, *args
+ tk_tcl2ruby(tk_send 'canvasy', y, *args)
end
+
def coords(tag, *args)
- tk_send 'coords', tagid(tag), *args
+ if args == []
+ tk_split_list(tk_send('coords', tagid(tag)))
+ else
+ tk_send('coords', tagid(tag), *args)
+ end
end
+
def dchars(tag, first, last=None)
tk_send 'dchars', tagid(tag), first, last
end
+
def delete(*args)
tk_send 'delete', *args
end
alias remove delete
+
def dtag(tag, tag_to_del=None)
tk_send 'dtag', tagid(tag), tag_to_del
end
- def find(*args)
- tk_send 'find', *args
+
+ def find(mode, *args)
+ list(tk_send 'find', mode, *args).filter{|id|
+ TkcItem.id2obj(id)
+ }
+ end
+ def find_above(target)
+ find('above', tagid(target))
+ end
+ def find_all
+ find('all')
+ end
+ def find_below(target)
+ find('below', tagid(target))
+ end
+ def find_closest(x, y, halo=None, start=None)
+ find('closest', x, y, halo, start)
+ end
+ def find_enclosed(x1, y1, x2, y2)
+ find('enclosed', x1, y1, x2, y2)
+ end
+ def find_overlapping(x1, y1, x2, y2)
+ find('overlapping', x1, y1, x2, y2)
+ end
+ def find_withtag(tag)
+ find('withtag', tag)
+ end
+
+ def itemfocus(tagOrId=nil)
+ if tagOrId
+ tk_send 'focus', tagid(tagOrId)
+ else
+ ret = tk_send('focus')
+ if ret == ""
+ nil
+ else
+ TkcItem.id2obj(ret)
+ end
+ end
+ end
+
+ def gettags(tagOrId)
+ list(tk_send('gettags', tagid(tagOrId))).collect{|tag|
+ TkcTag.id2obj(tag)
+ }
+ end
+
+ def icursor(tagOrId, index)
+ tk_send 'icursor', tagid(tagOrId), index
+ end
+
+ def index(tagOrId, index)
+ tk_send 'index', tagid(tagOrId), index
end
- def itemfocus(tag)
- tk_send 'find', tagid(tag)
+
+ def insert(tagOrId, index, string)
+ tk_send 'insert', tagid(tagOrId), index, string
end
- def gettags(tag)
- tk_send 'gettags', tagid(tag)
+
+ def itemcget(tagOrId, option)
+ tk_send 'itemcget', tagid(tagOrId), option
end
- def icursor(tag, index)
- tk_send 'icursor', tagid(tag), index
+
+ def itemconfigure(tagOrId, key, value=None)
+ if key.kind_of? Hash
+ tk_send 'itemconfigure', tagid(tagOrId), *hash_kv(key)
+ else
+ tk_send 'itemconfigure', tagid(tagOrId), "-#{key}", value
+ end
end
- def index(tag)
- tk_send 'index', tagid(tag), index
+# def itemconfigure(tagOrId, keys)
+# tk_send 'itemconfigure', tagid(tagOrId), *hash_kv(keys)
+# end
+
+ def itemconfiginfo(tagOrId, key=nil)
+ if key
+ conf = tk_split_list(tk_send 'itemconfigure', tagid(tagOrId), "-#{key}")
+ conf[0] = conf[0][1..-1]
+ conf
+ else
+ tk_split_list(tk_send 'itemconfigure', tagid(tagOrId)).collect{|conf|
+ conf[0] = conf[0][1..-1]
+ conf
+ }
+ end
end
- def lower(tag, below=None)
+
+ def lower(tag, below=None)
tk_send 'lower', tagid(tag), below
end
+
def move(tag, x, y)
tk_send 'move', tagid(tag), x, y
end
- def itemtype(tag)
- tk_send 'type', tagid(tag)
- end
+
def postscript(keys)
tk_send "postscript", *hash_kv(keys)
end
+
def raise(tag, above=None)
tk_send 'raise', tagid(tag), above
end
+
def scale(tag, x, y, xs, ys)
tk_send 'scale', tagid(tag), x, y, xs, ys
end
+
def scan_mark(x, y)
tk_send 'scan', 'mark', x, y
end
def scan_dragto(x, y)
tk_send 'scan', 'dragto', x, y
end
- def select(*args)
- tk_send 'select', *args
+
+ def select(mode, *args)
+ tk_send 'select', mode, *args
+ end
+ def select_adjust(tagOrId, index)
+ select('adjust', tagid(tagOrId), index)
+ end
+ def select_clear
+ select('clear')
+ end
+ def select_from(tagOrId, index)
+ select('from', tagid(tagOrId), index)
+ end
+ def select_item
+ select('item')
end
+ def select_to(tagOrId, index)
+ select('to', tagid(tagOrId), index)
+ end
+
+ def itemtype(tag)
+ TkcItem.type2class(tk_send 'type', tagid(tag))
+ end
+
def xview(*index)
tk_send 'xview', *index
end
@@ -118,168 +237,337 @@ class TkCanvas<TkWindow
end
end
-class TkcItem<TkObject
- def initialize(parent, *args)
- if not parent.kind_of?(TkCanvas)
- fail format("%s need to be TkCanvas", parent.inspect)
- end
- @c = parent
- @path = parent.path
- if args[-1].kind_of? Hash
- keys = args.pop
- end
- @id = create_self(*args)
- if keys
- tk_call @path, 'itemconfigure', @id, *hash_kv(keys)
- end
+module TkcTagAccess
+ include TkComm
+
+ def addtag(tag)
+ @c.addtag(tag, 'with', @id)
end
- def create_self(*args) end
- private :create_self
- def id
- return @id
+
+ def bbox
+ @c.bbox(@id)
end
- def configure(slot, value)
- tk_call path, 'itemconfigure', @id, "-#{slot}", value
+ def bind(seq, cmd=Proc.new, args=nil)
+ @c.itembind @id, seq, cmd, args
end
- def addtag(tag)
- @c.addtag(tag, 'withtag', @id)
+ def cget(option)
+ @c.itemcget @id, option
end
- def bbox
- @c.bbox(@id)
+
+ def configure(key, value=None)
+ @c.itemconfigure @id, key, value
end
- def bind(seq, cmd=Proc.new)
- @c.itembind @id, seq, cmd
+# def configure(keys)
+# @c.itemconfigure @id, keys
+# end
+
+ def configinfo
+ @c.itemconfigure @id
end
+
def coords(*args)
@c.coords @id, *args
end
+
def dchars(first, last=None)
@c.dchars @id, first, last
end
- def dtag(ttd)
- @c.dtag @id, ttd
+
+ def dtag(tag_to_del=None)
+ @c.dtag @id, tag_to_del
+ end
+
+ def find
+ @c.find 'withtag', @id
end
+ alias list find
+
def focus
- @c.focus @id
+ @c.itemfocus @id
end
+
def gettags
@c.gettags @id
end
- def icursor
- @c.icursor @id
+
+ def icursor(index)
+ @c.icursor @id, index
end
- def index
- @c.index @id
+
+ def index(index)
+ @c.index @id, index
end
+
def insert(beforethis, string)
@c.insert @id, beforethis, string
end
+
def lower(belowthis=None)
@c.lower @id, belowthis
end
+
def move(xamount, yamount)
@c.move @id, xamount, yamount
end
+
def raise(abovethis=None)
@c.raise @id, abovethis
end
+
def scale(xorigin, yorigin, xscale, yscale)
@c.scale @id, xorigin, yorigin, xscale, yscale
end
+
+ def select_adjust(index)
+ @c.select('adjust', @id, index)
+ end
+ def select_from(index)
+ @c.select('from', @id, index)
+ end
+ def select_to(index)
+ @c.select('to', @id, index)
+ end
+
def itemtype
@c.itemtype @id
end
- def destroy
- tk_call path, 'delete', @id
+end
+
+class TkcTag<TkObject
+ include TkcTagAccess
+
+ CTagID_TBL = {}
+
+ def TkcTag.id2obj(id)
+ CTagID_TBL[id]? CTagID_TBL[id]: id
+ end
+
+ $tk_canvas_tag = 'ctag0000'
+ def initialize(parent, mode=nil, *args)
+ if not parent.kind_of?(TkCanvas)
+ fail format("%s need to be TkCanvas", parent.inspect)
+ end
+ @c = parent
+ @path = @id = $tk_canvas_tag
+ CTagID_TBL[@id] = self
+ $tk_canvas_tag = $tk_canvas_tag.succ
+ if mode
+ tk_call @c.path, "addtag", @id, mode, *args
+ end
end
+ def id
+ return @id
+ end
+
+ def delete
+ @c.delete @id
+ CTagID_TBL[@id] = nil
+ end
+ alias remove delete
+ alias destroy delete
+
+ def set_to_above(target)
+ @c.addtag_above(@id, target)
+ end
+ alias above set_to_above
+
+ def set_to_all
+ @c.addtag_all(@id)
+ end
+ alias all set_to_all
+
+ def set_to_below(target)
+ @c.addtag_below(@id, target)
+ end
+ alias below set_to_below
+
+ def set_to_closest(x, y, halo=None, start=None)
+ @c.addtag_closest(@id, x, y, halo, start)
+ end
+ alias closest set_to_closest
+
+ def set_to_enclosed(x1, y1, x2, y2)
+ @c.addtag_enclosest(@id, x1, y1, x2, y2)
+ end
+ alias enclosed set_to_enclosed
+
+ def set_to_overlapping(x1, y1, x2, y2)
+ @c.addtag_overlapping(@id, x1, y1, x2, y2)
+ end
+ alias overlapping set_to_overlapping
+
+ def set_to_withtag(target)
+ @c.addtag_withtag(@id, target)
+ end
+ alias withtag set_to_withtag
+end
+
+class TkcTagAll<TkcTag
+ def initialize(parent)
+ if not parent.kind_of?(TkCanvas)
+ fail format("%s need to be TkCanvas", parent.inspect)
+ end
+ @c = parent
+ @path = @id = 'all'
+ CTagID_TBL[@id] = self
+ end
+end
+
+class TkcTagCurrent<TkcTag
+ def initialize(parent)
+ if not parent.kind_of?(TkCanvas)
+ fail format("%s need to be TkCanvas", parent.inspect)
+ end
+ @c = parent
+ @path = @id = 'current'
+ CTagID_TBL[@id] = self
+ end
+end
+
+class TkcGroup<TkcTag
+ $tk_group_id = 'tkg00000'
+ def create_self(parent, *args)
+ if not parent.kind_of?(TkCanvas)
+ fail format("%s need to be TkCanvas", parent.inspect)
+ end
+ @c = parent
+ @path = @id = $tk_group_id
+ CTagID_TBL[@id] = self
+ $tk_group_id = $tk_group_id.succ
+ add(*args) if args != []
+ end
+
+ def include(*tags)
+ for i in tags
+ i.addtag @id
+ end
+ end
+
+ def exclude(*tags)
+ for i in tags
+ i.delete @id
+ end
+ end
+end
+
+
+class TkcItem<TkObject
+ include TkcTagAccess
+
+ CItemTypeToClass = {}
+ CItemID_TBL = {}
+
+ def TkcItem.type2class(type)
+ CItemTypeToClass[type]
+ end
+
+ def TkcItem.id2obj(id)
+ CItemID_TBL[id]? CItemID_TBL[id]: id
+ end
+
+ def initialize(parent, *args)
+ if not parent.kind_of?(TkCanvas)
+ fail format("%s need to be TkCanvas", parent.inspect)
+ end
+ @c = parent
+ @path = parent.path
+ if args[-1].kind_of? Hash
+ keys = args.pop
+ end
+ @id = create_self(*args).to_i ;# 'canvas item id' is integer number
+ CItemID_TBL[@id] = self
+ if keys
+ tk_call @path, 'itemconfigure', @id, *hash_kv(keys)
+ end
+ end
+ def create_self(*args); end
+ private :create_self
+ def id
+ return @id
+ end
+
+ def delete
+ @c.delete @id
+ CItemID_TBL[@id] = nil
+ end
+ alias remove delete
+ alias destroy delete
end
class TkcArc<TkcItem
+ CItemTypeToClass['arc'] = self
def create_self(*args)
tk_call(@path, 'create', 'arc', *args)
end
end
class TkcBitmap<TkcItem
+ CItemTypeToClass['bitmap'] = self
def create_self(*args)
tk_call(@path, 'create', 'bitmap', *args)
end
end
class TkcImage<TkcItem
+ CItemTypeToClass['image'] = self
def create_self(*args)
tk_call(@path, 'create', 'image', *args)
end
end
class TkcLine<TkcItem
+ CItemTypeToClass['line'] = self
def create_self(*args)
tk_call(@path, 'create', 'line', *args)
end
end
class TkcOval<TkcItem
+ CItemTypeToClass['oval'] = self
def create_self(*args)
tk_call(@path, 'create', 'oval', *args)
end
end
class TkcPolygon<TkcItem
+ CItemTypeToClass['polygon'] = self
def create_self(*args)
tk_call(@path, 'create', 'polygon', *args)
end
end
class TkcRectangle<TkcItem
+ CItemTypeToClass['rectangle'] = self
def create_self(*args)
tk_call(@path, 'create', 'rectangle', *args)
end
end
class TkcText<TkcItem
+ CItemTypeToClass['text'] = self
def create_self(*args)
tk_call(@path, 'create', 'text', *args)
end
end
class TkcWindow<TkcItem
+ CItemTypeToClass['window'] = self
def create_self(*args)
tk_call(@path, 'create', 'window', *args)
end
end
-class TkcGroup<TkcItem
- $tk_group_id = 'tkg00000'
- def create_self(*args)
- @id = $tk_group_id
- $tk_group_id = $tk_group_id.succ
- end
-
- def add(*tags)
- for i in tags
- i.addtag @id
- end
- end
- def add(*tags)
- for i in tags
- i.addtag @id
- end
- end
- def delete(*tags)
- for i in tags
- i.delete @id
- end
- end
- def list
- @c.find 'withtag', @id
- end
- alias remove delete
-end
-
class TkImage<TkObject
include Tk
+ Tk_IMGTBL = {}
+
$tk_image_id = 'i00000'
def initialize(keys=nil)
@path = $tk_image_id
$tk_image_id = $tk_image_id.succ
tk_call 'image', 'create', @type, @path, *hash_kv(keys)
+ Tk_IMGTBL[@path] = self
end
+ def delete
+ Tk_IMGTBL[@id] = nil if @id
+ tk_call('image', 'delete', @path)
+ end
def height
number(tk_call('image', 'height', @path))
end
@@ -291,10 +579,13 @@ class TkImage<TkObject
end
def TkImage.names
- tk_call('image', 'names', @path).split
+ Tk.tk_call('image', 'names').split.filter{|id|
+ (Tk_IMGTBL[id])? Tk_IMGTBL[id] : id
+ }
end
+
def TkImage.types
- tk_call('image', 'types', @path).split
+ Tk.tk_call('image', 'types').split
end
end
@@ -314,13 +605,60 @@ class TkPhotoImage<TkImage
def blank
tk_send 'blank'
end
- def cget
- tk_send 'cget'
+
+ def cget(option)
+ tk_send 'cget', option
end
+
+ def copy(source, *opts)
+ args = opts.collect{|term|
+ if term.kind_of?(String) && term.include?(?\s)
+ term.split
+ else
+ term
+ end
+ }.flatten
+
+ tk_send 'copy', source, *args
+ end
+
def get(x, y)
tk_send 'get', x, y
end
- def put(data, to=None)
- tk_send 'put', data, to
+
+ def put(data, *to)
+ if to == []
+ tk_send 'put', data
+ else
+ tk_send 'put', data, '-to', *to
+ end
+ end
+
+ def read(file, *opts)
+ args = opts.collect{|term|
+ if term.kind_of?(String) && term.include?(?\s)
+ term.split
+ else
+ term
+ end
+ }.flatten
+
+ tk_send 'read', file, *args
+ end
+
+ def redither
+ tk_send 'redither'
+ end
+
+ def write(file, *opts)
+ args = opts.collect{|term|
+ if term.kind_of?(String) && term.include?(?\s)
+ term.split
+ else
+ term
+ end
+ }.flatten
+
+ tk_send 'write', file, *args
end
end
diff --git a/lib/tkentry.rb b/lib/tkentry.rb
index bcf092a15c..645fc997b1 100644
--- a/lib/tkentry.rb
+++ b/lib/tkentry.rb
@@ -35,23 +35,23 @@ class TkEntry<TkLabel
def dragto(pos)
tk_send 'scan', 'dragto', pos
end
- def select_adjust(index)
- tk_send 'select', 'adjust', index
+ def selection_adjust(index)
+ tk_send 'selection', 'adjust', index
end
- def select_clear
- tk_send 'select', 'clear', 'end'
+ def selection_clear
+ tk_send 'selection', 'clear', 'end'
end
- def select_from(index)
- tk_send 'select', 'from', index
+ def selection_from(index)
+ tk_send 'selection', 'from', index
end
- def select_present()
- tk_send('select', 'present') == 1
+ def selection_present()
+ tk_send('selection', 'present') == 1
end
- def select_range(s, e)
- tk_send 'select', 'range', s, e
+ def selection_range(s, e)
+ tk_send 'selection', 'range', s, e
end
- def select_to(index)
- tk_send 'select', 'to', index
+ def selection_to(index)
+ tk_send 'selection', 'to', index
end
def xview(*index)
tk_send 'xview', *index
diff --git a/lib/tktext.rb b/lib/tktext.rb
index 146944dde7..74fec8c953 100644
--- a/lib/tktext.rb
+++ b/lib/tktext.rb
@@ -15,19 +15,19 @@ class TkText<TkTextWin
tk_send 'index', index
end
def value
- tk_send 'get', "1.0", "end"
+ tk_send 'get', "1.0", "end - 1 char"
end
def value= (val)
tk_send 'delete', "1.0", 'end'
tk_send 'insert', "1.0", val
end
def _addcmd(cmd)
- @cmdtbl.push id
+ @cmdtbl.push cmd
end
def _addtag(name, obj)
@tags[name] = obj
end
- def tag_names(index=nil)
+ def tag_names(index=None)
tk_split_list(tk_send('tag', 'names', index)).collect{|elt|
if not @tags[elt]
elt
@@ -110,6 +110,168 @@ class TkText<TkTextWin
def xview_pickplace(*what)
tk_send 'xview', '-pickplace', *what
end
+
+ def tag_add(tag,index1,index2=None)
+ tk_send 'tag', 'add', tag, index1, index2
+ end
+
+ def tag_bind(tag, seq, cmd=Proc.new, args=nil)
+ seq = context.join("><") if seq.kind_of? Array
+ if /,/ =~ seq
+ seq = seq.split(/\s*,\s*/).join("><")
+ end
+ id = install_bind(cmd, args)
+ tk_send 'tag', 'bind', tag, "<#{seq}>", id
+ # _addcmd cmd
+ end
+
+ def tag_bindinfo(tag)
+ tk_split_list(tk_send('tag', 'bind', tag)).filter{|seq|
+ seq.tr('<>',' ').strip.gsub(/\s+/,',')
+ }
+ end
+
+ def tag_cget(tag, key)
+ tk_call @t.path, 'tag', 'cget', tag, "-#{key}"
+ end
+
+ def tag_configure(tag, key, val=None)
+ if key.kind_of? Hash
+ tk_send 'tag', 'configure', tag, *hash_kv(key)
+ else
+ tk_send 'tag', 'configure', tag, "-#{key}", val
+ end
+ end
+
+ def configinfo(tag, key=nil)
+ if key
+ conf = tk_split_list(tk_send('tag','configure',tag,"-#{key}"))
+ conf[0] = conf[0][1..-1]
+ conf
+ else
+ tk_split_list(tk_send('tag', 'configure', tag)).collect{|conf|
+ conf[0] = conf[0][1..-1]
+ conf
+ }
+ end
+ end
+
+ def tag_raise(tag, above=None)
+ tk_send 'tag', 'raise', tag, above
+ end
+
+ def tag_lower(tag, below=None)
+ tk_send 'tag', 'lower', tag, below
+ end
+
+ def tag_remove(tag, *index)
+ tk_send 'tag', 'remove', tag, *index
+ end
+
+ def tag_ranges(tag)
+ l = tk_split_list(tk_send('tag', 'ranges', tag))
+ r = []
+ while key=l.shift
+ r.push [key, l.shift]
+ end
+ r
+ end
+
+ def tag_nextrange(tag, first, last=None)
+ tk_split_list(tk_send('tag', 'nextrange', tag, first, last))
+ end
+
+ def tag_prevrange(tag, first, last=None)
+ tk_split_list(tk_send('tag', 'prevrange', tag, first, last))
+ end
+
+ def search_with_length(pat,start,stop=None)
+ pat = pat.char if pat.kind_of? Integer
+ if stop != None
+ return ["", 0] if compare(start,'>=',stop)
+ txt = get(start,stop)
+ if (pos = txt.index(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index(start + " + #{pos} chars"), pat.split('').length]
+ else
+ return [index(start + " + #{pos} chars"), $&.split('').length]
+ end
+ else
+ return ["", 0]
+ end
+ else
+ txt = get(start,'end - 1 char')
+ if (pos = txt.index(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index(start + " + #{pos} chars"), pat.split('').length]
+ else
+ return [index(start + " + #{pos} chars"), $&.split('').length]
+ end
+ else
+ txt = get('1.0','end - 1 char')
+ if (pos = txt.index(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index("1.0 + #{pos} chars"), pat.split('').length]
+ else
+ return [index("1.0 + #{pos} chars"), $&.split('').length]
+ end
+ else
+ return ["", 0]
+ end
+ end
+ end
+ end
+
+ def search(pat,start,stop=None)
+ search_with_length(pat,start,stop)[0]
+ end
+
+ def rsearch_with_length(pat,start,stop=None)
+ pat = pat.char if pat.kind_of? Integer
+ if stop != None
+ return ["", 0] if compare(start,'<=',stop)
+ txt = get(stop,start)
+ if (pos = txt.rindex(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index(stop + " + #{pos} chars"), pat.split('').length]
+ else
+ return [index(stop + " + #{pos} chars"), $&.split('').length]
+ end
+ else
+ return ["", 0]
+ end
+ else
+ txt = get('1.0',start)
+ if (pos = txt.rindex(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index("1.0 + #{pos} chars"), pat.split('').length]
+ else
+ return [index("1.0 + #{pos} chars"), $&.split('').length]
+ end
+ else
+ txt = get('1.0','end - 1 char')
+ if (pos = txt.rindex(pat))
+ pos = txt[0..(pos-1)].split('').length if pos > 0
+ if pat.kind_of? String
+ return [index("1.0 + #{pos} chars"), pat.split('').length]
+ else
+ return [index("1.0 + #{pos} chars"), $&.split('').length]
+ end
+ else
+ return ["", 0]
+ end
+ end
+ end
+ end
+
+ def rsearch(pat,start,stop=None)
+ rsearch_with_length(pat,start,stop)[0]
+ end
end
class TkTextTag<TkObject
@@ -145,13 +307,12 @@ class TkTextTag<TkObject
r
end
- def nextrange(first, last=nil)
+ def nextrange(first, last=None)
tk_split_list(tk_call(@t.path, 'tag', 'nextrange', @id, first, last))
end
- def prevrange(first, last=nil)
+ def prevrange(first, last=None)
tk_split_list(tk_call(@t.path, 'tag', 'prevrange', @id, first, last))
- l = tk_split_list(tk_call(@t.path, 'tag', 'prevrange', @id, first, last))
end
def [](key)
@@ -166,22 +327,49 @@ class TkTextTag<TkObject
tk_call @t.path, 'tag', 'cget', @id, "-#{key}"
end
- def configure(key, val=nil)
+ def configure(key, val=None)
if key.kind_of? Hash
tk_call @t.path, 'tag', 'configure', @id, *hash_kv(key)
else
tk_call @t.path, 'tag', 'configure', @id, "-#{key}", val
end
end
-
- def configinfo
- tk_split_list(tk_call(@t.path, 'tag', 'configure', @id))
+# def configure(key, value)
+# if value == FALSE
+# value = "0"
+# elsif value.kind_of? Proc
+# value = install_cmd(value)
+# end
+# tk_call @t.path, 'tag', 'configure', @id, "-#{key}", value
+# end
+
+ def configinfo(key=nil)
+ if key
+ conf = tk_split_list(tk_call(@t.path, 'tag','configure',@id,"-#{key}"))
+ conf[0] = conf[0][1..-1]
+ conf
+ else
+ tk_split_list(tk_call(@t.path, 'tag', 'configure', @id)).collect{|conf|
+ conf[0] = conf[0][1..-1]
+ conf
+ }
+ end
end
def bind(seq, cmd=Proc.new, args=nil)
+ seq = context.join("><") if seq.kind_of? Array
+ if /,/ =~ seq
+ seq = seq.split(/\s*,\s*/).join("><")
+ end
id = install_bind(cmd, args)
tk_call @t.path, 'tag', 'bind', @id, "<#{seq}>", id
- @t._addcmd cmd
+ # @t._addcmd cmd
+ end
+
+ def bindinfo
+ tk_split_list(tk_call(@t.path, 'tag', 'bind', @id)).filter{|seq|
+ seq.tr('<>',' ').strip.gsub(/\s+/,',')
+ }
end
def raise(above=None)
@@ -307,12 +495,23 @@ class TkTextWindow<TkObject
tk_call @t.path, 'window', 'cget', @index, "-#{slot}"
end
- def configure(slot, value)
- @id = value if slot == 'window'
- if slot == 'create'
- self.create=value
+ def configure(slot, value=None)
+ if slot.kind_of? Hash
+ @id = slot['window'] if slot['window']
+ if slot['create']
+ self.create=value
+ slot['create']=nil
+ end
+ if slot.size > 0
+ tk_call @t.path, 'window', 'configure', @index, *hash_kv(slot)
+ end
else
- tk_call @t.path, 'window', 'configure', @index, "-#{slot}", value
+ @id = value if slot == 'window'
+ if slot == 'create'
+ self.create=value
+ else
+ tk_call @t.path, 'window', 'configure', @index, "-#{slot}", value
+ end
end
end
@@ -386,9 +585,16 @@ class TkTextImage<TkObject
tk_call @t.path, 'image', 'cget', @index, "-#{slot}"
end
- def configure(slot, value)
- tk_call @t.path, 'image', 'configure', @index, "-#{slot}", value
+ def configure(slot, value=None)
+ if slot.kind_of? Hash
+ tk_call @t.path, 'image', 'configure', @index, *hash_kv(slot)
+ else
+ tk_call @t.path, 'image', 'configure', @index, "-#{slot}", value
+ end
end
+# def configure(slot, value)
+# tk_call @t.path, 'image', 'configure', @index, "-#{slot}", value
+# end
def image
tk_call @t.path, 'image', 'configure', @index, '-image'