diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-06-18 07:47:15 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-06-18 07:47:15 +0000 |
commit | 992923053cfb51dc04c6a7ee07ca6fff1b61d1a5 (patch) | |
tree | 2b2857adc35295dcd45c57c338c440ec16f99eff /lib | |
parent | 16487ee284f17cfbf6fad9f6b2fedc57f0f37e0a (diff) |
1.1b9_26
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tk.rb | 177 | ||||
-rw-r--r-- | lib/tkpalette.rb | 46 | ||||
-rw-r--r-- | lib/tktext.rb | 268 |
3 files changed, 446 insertions, 45 deletions
@@ -54,6 +54,7 @@ module TkComm return tk_tcl2ruby(str) unless idx list = tk_tcl2ruby(str[0,idx]) + list = [] if list == "" str = str[idx+1..-1] i = -1 brace = 1 @@ -78,7 +79,6 @@ module TkComm if keys and keys != None for k, v in keys conf.push("-#{k}") - v = install_cmd(v) if v.kind_of? Proc conf.push(v) end end @@ -86,6 +86,20 @@ module TkComm end private :hash_kv + def array2tk_list(ary) + ary.collect{|e| + if e.kind_of? Array + "{#{array2tk_list(e)}}" + elsif e.kind_of? Hash + "{#{e.to_a.collect{|ee| array2tk_list(ee)}.join(' ')}}" + else + s = _get_eval_string(e) + (s.index(/\s/))? "{#{s}}": s + end + }.join(" ") + end + private :array2tk_list + def bool(val) case val when "1", 1, 'yes', 'true' @@ -221,8 +235,12 @@ module TkComm end def _bind(path, context, cmd, 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 - id = install_bind(cmd, args) tk_call 'bind', path, "<#{context}>", id rescue uninstall_cmd(id) @@ -271,7 +289,7 @@ module TkCore def TkCore.callback(arg) arg = Array(tk_split_list(arg)) - TkUtil.eval_cmd Tk_CMDTBL[arg.shift], *arg + _get_eval_string(TkUtil.eval_cmd(Tk_CMDTBL[arg.shift], *arg)) end def mainloop @@ -282,6 +300,10 @@ module TkCore return nil if str == None if str.kind_of?(Hash) str = hash_kv(str).join(" ") + elsif str.kind_of?(Array) + str = array2tk_list(str) + elsif str.kind_of?(Proc) + str = install_cmd(v) elsif str == nil str = "" elsif str == false @@ -325,6 +347,9 @@ module Tk include TkCore extend Tk + TCL_VERSION = INTERP._invoke("info", "tclversion") + TK_VERSION = INTERP._invoke("set", "tk_version") + def root TkRoot.new end @@ -448,8 +473,20 @@ class TkVariable def initialize(val="") @id = Tk_VARIABLE_ID[0] Tk_VARIABLE_ID[0] = Tk_VARIABLE_ID[0].succ - s = '"' + _get_eval_string(val).gsub(/[][$"]/, '\\\\\&') + '"' #' - INTERP._eval(format('global %s; set %s %s', @id, @id, s)) + if val == [] + INTERP._eval(format('global %s; set %s(0) 0; unset %s(0)', + @id, @id, @id)) + elsif val.kind_of?(Array) + s = '"' + array2tk_list(val).gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; array set %s %s', @id, @id, s)) + elsif val.kind_of?(Hash) + s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\ + ..gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; array set %s %s', @id, @id, s)) + else + s = '"' + _get_eval_string(val).gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; set %s %s', @id, @id, s)) + end end def id @@ -457,11 +494,50 @@ class TkVariable end def value - INTERP._eval(format('global %s; set %s', @id, @id)) + begin + INTERP._eval(format('global %s; set %s', @id, @id)) + rescue + if INTERP._eval(format('global %s; array exists %s', @id, @id)) != "1" + raise + else + INTERP._eval(format('global %s; array get %s', @id, @id)) + end + end end def value=(val) - INTERP._eval(format('global %s; set %s %s', @id, @id, _get_eval_string(val))) + begin + INTERP._eval(format('global %s; set %s %s', @id, @id, _get_eval_string(val))) + rescue + if INTERP._eval(format('global %s; array exists %s', @id, @id)) != "1" + raise + else + INTERP._eval(format('global %s; unset %s'), @id, @id) + if val == [] + INTERP._eval(format('global %s; set %s(0) 0; unset %s(0)', + @id, @id, @id)) + elsif val.kind_of?(Array) + s = '"' + array2tk_list(val).gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; array set %s %s', @id, @id, s)) + elsif val.kind_of?(Hash) + s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\ + .gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; array set %s %s', @id, @id, s)) + else + raise + end + end + end + end + + def [](index) + INTERP._eval(format('global %s; set %s(%s)', + @id, @id, _get_eval_string(index))) + end + + def []=(index,val) + INTERP._eval(format('global %s; set %s(%s) %s', @id, @id, + _get_eval_string(index), _get_eval_string(val))) end def to_i @@ -506,6 +582,16 @@ class TkVariable end end +class TkVarAccess<TkVariable + def initialize(varname, val=nil) + @id = varname + if val + s = '"' + _get_eval_string(val).gsub(/[][$"]/, '\\\\\&') + '"' #' + INTERP._eval(format('global %s; set %s %s', @id, @id, s)) + end + end +end + module TkSelection include Tk extend Tk @@ -577,49 +663,49 @@ module TkWinfo def TkWinfo.depth(window) number(tk_call('winfo', 'depth', window.path)) end - def winfo_depth(window) + def winfo_depth TkWinfo.depth self end def TkWinfo.exist?(window) bool(tk_call('winfo', 'exists', window.path)) end - def winfo_exist?(window) + def winfo_exist? TkWinfo.exist? self end def TkWinfo.fpixels(window, number) number(tk_call('winfo', 'fpixels', window.path, number)) end - def winfo_fpixels(window, number) + def winfo_fpixels(number) TkWinfo.fpixels self end def TkWinfo.geometry(window) list(tk_call('winfo', 'geometry', window.path)) end - def winfo_geometry(window) + def winfo_geometry TkWinfo.geometry self end def TkWinfo.height(window) number(tk_call('winfo', 'height', window.path)) end - def winfo_height(window) + def winfo_height TkWinfo.height self end def TkWinfo.id(window) number(tk_call('winfo', 'id', window.path)) end - def winfo_id(window) + def winfo_id TkWinfo.id self end def TkWinfo.mapped?(window) bool(tk_call('winfo', 'ismapped', window.path)) end - def winfo_mapped?(window) + def winfo_mapped? TkWinfo.mapped? self end def TkWinfo.parent(window) window(tk_call('winfo', 'parent', window.path)) end - def winfo_parent(window) + def winfo_parent TkWinfo.parent self end def TkWinfo.widget(id) @@ -631,139 +717,139 @@ module TkWinfo def TkWinfo.pixels(window, number) number(tk_call('winfo', 'pixels', window.path, number)) end - def winfo_pixels(window, number) + def winfo_pixels(number) TkWinfo.pixels self, number end def TkWinfo.reqheight(window) number(tk_call('winfo', 'reqheight', window.path)) end - def winfo_reqheight(window) + def winfo_reqheight TkWinfo.reqheight self end def TkWinfo.reqwidth(window) number(tk_call('winfo', 'reqwidth', window.path)) end - def winfo_reqwidth(window) + def winfo_reqwidth TkWinfo.reqwidth self end def TkWinfo.rgb(window, color) list(tk_call('winfo', 'rgb', window.path, color)) end - def winfo_rgb(window, color) + def winfo_rgb(color) TkWinfo.rgb self, color end def TkWinfo.rootx(window) number(tk_call('winfo', 'rootx', window.path)) end - def winfo_rootx(window) + def winfo_rootx TkWinfo.rootx self end def TkWinfo.rooty(window) number(tk_call('winfo', 'rooty', window.path)) end - def winfo_rooty(window) + def winfo_rooty TkWinfo.rooty self end def TkWinfo.screen(window) tk_call 'winfo', 'screen', window.path end - def winfo_screen(window) + def winfo_screen TkWinfo.screen self end def TkWinfo.screencells(window) number(tk_call('winfo', 'screencells', window.path)) end - def winfo_screencells(window) + def winfo_screencells TkWinfo.screencells self end def TkWinfo.screendepth(window) number(tk_call('winfo', 'screendepth', window.path)) end - def winfo_screendepth(window) + def winfo_screendepth TkWinfo.screendepth self end def TkWinfo.screenheight (window) number(tk_call('winfo', 'screenheight', window.path)) end - def winfo_screenheight(window) + def winfo_screenheight TkWinfo.screenheight self end def TkWinfo.screenmmheight(window) number(tk_call('winfo', 'screenmmheight', window.path)) end - def winfo_screenmmheight(window) + def winfo_screenmmheight TkWinfo.screenmmheight self end def TkWinfo.screenmmwidth(window) number(tk_call('winfo', 'screenmmwidth', window.path)) end - def winfo_screenmmwidth(window) + def winfo_screenmmwidth TkWinfo.screenmmwidth self end def TkWinfo.screenvisual(window) tk_call 'winfo', 'screenvisual', window.path end - def winfo_screenvisual(window) + def winfo_screenvisual TkWinfo.screenvisual self end def TkWinfo.screenwidth(window) number(tk_call('winfo', 'screenwidth', window.path)) end - def winfo_screenwidth(window) + def winfo_screenwidth TkWinfo.screenwidth self end def TkWinfo.toplevel(window) window(tk_call('winfo', 'toplevel', window.path)) end - def winfo_toplevel(window) + def winfo_toplevel TkWinfo.toplevel self end def TkWinfo.visual(window) tk_call 'winfo', 'visual', window.path end - def winfo_visual(window) + def winfo_visual TkWinfo.visual self end def TkWinfo.vrootheigh(window) number(tk_call('winfo', 'vrootheight', window.path)) end - def winfo_vrootheight(window) + def winfo_vrootheight TkWinfo.vrootheight self end def TkWinfo.vrootwidth(window) number(tk_call('winfo', 'vrootwidth', window.path)) end - def winfo_vrootwidth(window) + def winfo_vrootwidth TkWinfo.vrootwidth self end def TkWinfo.vrootx(window) number(tk_call('winfo', 'vrootx', window.path)) end - def winfo_vrootx(window) + def winfo_vrootx TkWinfo.vrootx self end def TkWinfo.vrooty(window) number(tk_call('winfo', 'vrooty', window.path)) end - def winfo_vrooty(window) + def winfo_vrooty TkWinfo.vrooty self end def TkWinfo.width(window) number(tk_call('winfo', 'width', window.path)) end - def winfo_width(window) + def winfo_width TkWinfo.width self end def TkWinfo.x(window) number(tk_call('winfo', 'x', window.path)) end - def winfo_x(window) + def winfo_x TkWinfo.x self end def TkWinfo.y(window) number(tk_call('winfo', 'y', window.path)) end - def winfo_y(window) + def winfo_y TkWinfo.y self end end @@ -804,7 +890,7 @@ module TkGrid if args[-1].kind_of?(Hash) keys = args.pop end - wins = [widget.path] + wins = [widget.epath] for i in args wins.push i.epath end @@ -928,6 +1014,19 @@ class TkObject<TkKernel configure slot, install_cmd(value) end + def configinfo(slot = nil) + if slot + conf = tk_split_list(tk_send('configure', "-#{slot}") ) + conf[0] = conf[0][1..-1] + conf + else + tk_split_list(tk_send('configure') ).collect{|conf| + conf[0] = conf[0][1..-1] + conf + } + end + end + def bind(context, cmd=Proc.new, args=nil) _bind path, context, cmd, args end diff --git a/lib/tkpalette.rb b/lib/tkpalette.rb new file mode 100644 index 0000000000..b317330937 --- /dev/null +++ b/lib/tkpalette.rb @@ -0,0 +1,46 @@ +# +# tkpalette.rb : methods for Tcl/Tk standard library 'palette.tcl' +# 1998/06/18 by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp> +# +module TkPalette + include Tk + extend Tk + + def TkPalette.set(*args) + args = args.to_a.flatten if args.kind_of? Hash + tk_call 'tk_setPalette', *args + end + def TkPalette.setPalette(*args) + TkPalette.set(*args) + end + + def TkPalette.bisque + tk_call 'tk_bisque' + end + + def TkPalette.darken(color, percent) + tk_call 'tkDarken', color, percent + end + + def TkPalette.recolorTree(window, colors) + if not colors.kind_of?(Hash) + fail "2nd arg need to be Hash" + end + + colors.each{|key, value| + begin + if window.cget(key) == tk_call('set', "tkPalette(#{key})") + window[key] = colors[key] + end + rescue + # ignore + end + } + + TkWinfo.children(window).each{|w| TkPalette.recolorTree(w, colors)} + end + + def recolorTree(colors) + TkPalette.recolorTree(self, colors) + end +end diff --git a/lib/tktext.rb b/lib/tktext.rb index a940c31a1b..8e6772c470 100644 --- a/lib/tktext.rb +++ b/lib/tktext.rb @@ -45,6 +45,26 @@ class TkText<TkTextWin end } end + def image_names + tk_send('image', 'names').collect{|elt| + if not @tags[elt] + elt + else + @tags[elt] + end + } + end + + def set_insert(index) + tk_send 'mark', 'set', 'insert', index + end + def set_current(index) + tk_send 'mark', 'set', 'current', index + end + + def insert(index, chars, *tags) + super index, chars, tags.collect{|x|_get_eval_string(x)}.join(' ') + end def destroy @tags.each_value do |t| @@ -68,6 +88,15 @@ class TkText<TkTextWin tk_send 'debug', boolean end + def bbox(index) + inf = tk_send('bbox', index) + (inf == "")? [0,0,0,0]: inf + end + def dlineinfo(index) + inf = tk_send('dlineinfo', index) + (inf == "")? [0,0,0,0,0]: inf + end + def yview(*what) tk_send 'yview', *what end @@ -107,18 +136,69 @@ class TkTextTag<TkObject tk_call @t.path, 'tag', 'remove', @id, *index end + def ranges + l = tk_split_list(tk_call(@t.path, 'tag', 'ranges', @id)) + r = [] + while key=l.shift + r.push [key, l.shift] + end + r + end + + def nextrange(first, last=nil) + l = tk_split_list(tk_call(@t.path, 'tag', 'nextrange', @id, first, last)) + r = [] + while key=l.shift + r.push [key, l.shift] + end + r + end + + def prevrange(first, last=nil) + l = tk_split_list(tk_call(@t.path, 'tag', 'prevrange', @id, first, last)) + r = [] + while key=l.shift + r.push [key, l.shift] + end + r + end + + def [](key) + cget key + end + + def []=(key,val) + configure key, val + end + + def cget(key) + tk_call @t.path, 'tag', 'cget', @id, "-#{key}" + end + def configure(keys) tk_call @t.path, 'tag', 'configure', @id, *hash_kv(keys) end +# 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 bind(seq, cmd=Proc.new, args=nil) id = install_bind(cmd, args) - tk_call @t, 'tag', 'bind', @id, "<#{seq}>", id + tk_call @t.path, 'tag', 'bind', @id, "<#{seq}>", id @t._addcmd cmd end + def raise(above=None) + tk_call @t.path, 'tag', 'raise', @id, above + end + def lower(below=None) - tk_call @t.path, 'tag', 'lower', below + tk_call @t.path, 'tag', 'lower', @id, below end def destroy @@ -126,6 +206,18 @@ class TkTextTag<TkObject 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) + @t._addtag id, self + end +end + class TkTextMark<TkObject $tk_text_mark = 'mark0000' def initialize(parent, index) @@ -150,19 +242,183 @@ class TkTextMark<TkObject tk_call @t.path, 'mark', 'unset', @id end alias destroy unset + + def gravity + tk_call @t.path, 'mark', 'gravity', @id + end + + def gravity=(direction) + tk_call @t.path, 'mark', 'gravity', @id, direction + end +end + +class TkTextMarkInsert<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 = 'insert' + tk_call @t.path, 'mark', 'set', @id, index if index + @t._addtag id, self + 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 + end end class TkTextWindow<TkObject - def initialize(parent, index, *args) + def initialize(parent, index, keys) if not parent.kind_of?(TkText) fail format("%s need to be TkText", parent.inspect) end @t = parent - @path = @index = index - tk_call @t.path, 'window', 'create', index, *args + if index == 'end' + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', 'end - 1 chars')) + elsif index.kind_of? TkTextMark + if tk_call(@t.path,'index',index.path) == tk_call(@t.path,'index','end') + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', 'end - 1 chars')) + else + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', index.path)) + end + else + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', index)) + end + @path.gravity = 'left' + @index = @path.path + @id = keys['window'] + if keys['create'] + @p_create = keys['create'] + if @p_create.kind_of? Proc + keys['create'] = install_cmd(proc{@id = @p_create.call; @id.path}) + end + end + tk_call @t.path, 'window', 'create', @index, *hash_kv(keys) + end + + def [](slot) + cget(slot) + end + def []=(slot, value) + configure(slot, value) + end + + def cget(slot) + tk_call @t.path, 'window', 'cget', @index, "-#{slot}" end def configure(slot, value) - 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 + + def window + @id + end + + def window=(value) + tk_call @t.path, 'window', 'configure', @index, '-window', value + @id = value + end + + def create + @p_create + end + + def create=(value) + @p_create = value + if @p_create.kind_of? Proc + value = install_cmd(proc{@id = @p_create.call}) + end + tk_call @t.path, 'window', 'configure', @index, '-create', value + end + + def configinfo(slot = nil) + if slot + conf = tk_split_list(tk_call @t.path, 'window', 'configure', + @index, "-#{slot}") + conf[0] = conf[0][1..-1] + conf + else + tk_split_list(tk_call @t.path, 'window', 'configure', + @index).collect{|conf| + conf[0] = conf[0][1..-1] + conf + } + end + end +end + +class TkTextImage<TkObject + def initialize(parent, index, keys) + if not parent.kind_of?(TkText) + fail format("%s need to be TkText", parent.inspect) + end + @t = parent + if index == 'end' + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', 'end - 1 chars')) + elsif index.kind_of? TkTextMark + if tk_call(@t.path,'index',index.path) == tk_call(@t.path,'index','end') + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', 'end - 1 chars')) + else + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', index.path)) + end + else + @path = TkTextMark.new(@t, tk_call(@t.path, 'index', index)) + end + @path.gravity = 'left' + @index = @path.path + @id = tk_call(@t.path, 'image', 'create', @index, *hash_kv(keys)) + end + + def [](slot) + cget(slot) + end + def []=(slot, value) + configure(slot, value) + end + + def cget(slot) + tk_call @t.path, 'image', 'cget', @index, "-#{slot}" + 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' + end + + def image=(value) + tk_call @t.path, 'image', 'configure', @index, '-image', value + end + + def configinfo(slot = nil) + if slot + conf = tk_split_list(tk_call @t.path, 'image', 'configure', + @index, "-#{slot}") + conf[0] = conf[0][1..-1] + conf + else + tk_split_list(tk_call @t.path, 'image', 'configure', + @index).collect{|conf| + conf[0] = conf[0][1..-1] + conf + } + end end end |