diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/e2mmap1_0.rb | 71 | ||||
-rw-r--r-- | lib/tk.rb | 1254 | ||||
-rw-r--r-- | lib/tkcanvas.rb | 326 | ||||
-rw-r--r-- | lib/tkclass.rb | 38 | ||||
-rw-r--r-- | lib/tkcore.rb | 528 | ||||
-rw-r--r-- | lib/tkdialog.rb | 62 | ||||
-rw-r--r-- | lib/tkentry.rb | 67 | ||||
-rw-r--r-- | lib/tkscrollbox.rb | 27 | ||||
-rw-r--r-- | lib/tktext.rb | 164 | ||||
-rw-r--r-- | lib/tkthcore.rb | 550 |
10 files changed, 0 insertions, 3087 deletions
diff --git a/lib/e2mmap1_0.rb b/lib/e2mmap1_0.rb deleted file mode 100644 index d245dec975..0000000000 --- a/lib/e2mmap1_0.rb +++ /dev/null @@ -1,71 +0,0 @@ -# -# e2mmap.rb - -# $Release Version: 1.0$ -# $Revision$ -# $Date$ -# by Keiju ISHITSUKA -# -# -- -# -# - -module Exception2MessageMapper - RCS_ID='-$Header$-' - E2MM = Exception2MessageMapper - - def E2MM.extend_to(b) - c = eval("self", b) - c.extend(self) - c.bind(b) - end - - def bind(b) - eval " - @binding = binding - E2MM_ErrorMSG = Hash.new - - # fail(err, *rest) - # err: 例外 - # rest: メッセージに渡すパラメータ - # - def fail!(*rest) - super - end - - def fail(err, *rest) - $! = err.new(sprintf(E2MM_ErrorMSG[err], *rest)) - super() - end - - public :fail - # def_exception(c, m) - # c: exception - # m: message_form - # 例外cのメッセージをmとする. - # - def def_e2message(c, m) - E2MM_ErrorMSG[c] = m - end - - # def_exception(c, m) - # c: exception_name - # m: message_form - # s: 例外スーパークラス(デフォルト: Exception) - # 例外名``c''をもつ例外を定義し, そのメッセージをmとする. - # - def def_exception(c, m) - - c = c.id2name if c.kind_of?(Fixnum) - eval \"class \#{c} < Exception - end - E2MM_ErrorMSG[\#{c}] = '\#{m}' - \", @binding - end -", b - - end - - E2MM.extend_to(binding) - def_exception("ErrNotClassOrModule", "Not Class or Module") -end - diff --git a/lib/tk.rb b/lib/tk.rb deleted file mode 100644 index 2cbbec02a8..0000000000 --- a/lib/tk.rb +++ /dev/null @@ -1,1254 +0,0 @@ -# -# tk.rb - Tk interface modue using tcltklib -# $Date$ -# by Yukihiro Matsumoto <matz@netlab.co.jp> - -# use Shigehiro's tcltklib -require "tcltklib" -require "tkutil" - -module TkComm - None = Object.new - def None.to_s - 'None' - end - - Tk_CMDTBL = {} - Tk_WINDOWS = {} - - def error_at - frames = caller(1) - frames.delete_if do |c| - c =~ %r!/tk(|core|thcore|canvas|text|entry|scrollbox)\.rb:\d+! - end - frames - end - private :error_at - - def tk_tcl2ruby(val) - case val - when /^-?\d+$/ - val.to_i - when /^\./ - Tk_WINDOWS[val] - when /^rb_out (c\d+)/ - Tk_CMDTBL[$1] - when / / - val.split.collect{|elt| - tk_tcl2ruby(elt) - } - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - - def tk_split_list(str) - idx = str.index('{') - return tk_tcl2ruby(str) if not idx - - list = tk_tcl2ruby(str[0,idx]) - str = str[idx+1..-1] - i = -1 - brace = 1 - str.each_byte {|c| - i += 1 - brace += 1 if c == ?{ - brace -= 1 if c == ?} - break if brace == 0 - } - if str[0, i] == ' ' - list.push ' ' - else - list.push tk_split_list(str[0, i]) - end - list += tk_split_list(str[i+1..-1]) - list - end - private :tk_tcl2ruby, :tk_split_list - - def hash_kv(keys) - conf = [] - 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 - conf - end - private :hash_kv - - def bool(val) - case val - when "1", 1, 'yes', 'true' - TRUE - else - FALSE - end - end - def number(val) - case val - when /^-?\d+$/ - val.to_i - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - def string(val) - if val == "{}" - '' - elsif val[0] == ?{ - val[1..-2] - else - val - end - end - def list(val) - tk_split_list(val) - end - def window(val) - Tk_WINDOWS[val] - end - def procedure(val) - if val =~ /^rb_out (c\d+)/ - Tk_CMDTBL[$1] - else - nil - end - end - private :bool, :number, :string, :list, :window, :procedure - - Tk_IDs = [0] # [0]-cmdid, [1]-winid - def _curr_cmd_id - id = format("c%.4d", Tk_IDs[0]) - end - def _next_cmd_id - id = _curr_cmd_id - Tk_IDs[0] += 1 - end - def install_cmd(cmd) - return '' if cmd == '' - id = _next_cmd_id - Tk_CMDTBL[id] = cmd - @cmdtbl = [] if not @cmdtbl - @cmdtbl.push id - return format("rb_out %s", id); - end - def uninstall_cmd(id) - Tk_CMDTBL[id] = nil - end - private :install_cmd, :uninstall_cmd - - def install_win(ppath) - id = format("w%.4d", Tk_IDs[0]) - Tk_IDs[0] += 1 - if !ppath or ppath == "." - @path = format(".%s", id); - else - @path = format("%s.%s", ppath, id) - end - Tk_WINDOWS[@path] = self - end - - def uninstall_win() - Tk_WINDOWS[@path] = nil - end - - class Event - def initialize(seq,b,f,h,k,s,t,w,x,y,aa,ee,kk,nn,ww,tt,xx,yy) - @serial = seq - @num = b - @focus = (f == 1) - @height = h - @keycode = k - @state = s - @time = t - @width = w - @x = x - @y = y - @char = aa - @send_event = (ee == 1) - @keysym = kk - @keysym_num = nn - @type = tt - @widget = ww - @x_root = xx - @y_root = yy - end - attr :serial - attr :num - attr :focus - attr :height - attr :keycode - attr :state - attr :time - attr :width - attr :x - attr :y - attr :char - attr :send_event - attr :keysym - attr :keysym_num - attr :type - attr :widget - attr :x_root - attr :y_root - end - - def install_bind(cmd, args=nil) - if args - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, *arg - }) - id + " " + args - else - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, Event.new(*arg) - }) - id + ' %# %b %f %h %k %s %t %w %x %y %A %E %K %N %W %T %X %Y' - end - end - - def _bind(path, context, cmd, args=nil) - begin - id = install_bind(cmd, args) - tk_call 'bind', path, "<#{context}>", id - rescue - uninstall_cmd(id) - fail - end - end - private :install_bind, :_bind - - def bind_all(context, cmd=Proc.new, args=nil) - _bind 'all', context, cmd, args - end - - def pack(*args) - TkPack.configure *args - end - - def after(ms, cmd=Proc.new) - myid = _curr_cmd_id - tk_call 'after', ms, - install_cmd(proc{ - TkUtil.eval_cmd cmd - uninstall_cmd myid - }) - end -end - -module TkCore - include TkComm - extend TkComm - - INTERP = TclTkIp.new - INTERP._eval("proc rb_out {args} { ruby [format \"TkCore.callback %%Q!%s!\" $args] }") - - def TkCore.callback(arg) - arg = Array(tk_split_list(arg)) - TkUtil.eval_cmd Tk_CMDTBL[arg.shift], *arg - end - - def mainloop - TclTkLib.mainloop - end - - def _get_eval_string(*args) - argstr = "" - args.each{|arg| - next if arg == None - if arg.kind_of?(Hash) - str = hash_kv(arg).join(" ") - elsif arg == nil - str = "" - elsif arg == false - str = "0" - elsif arg == true - str = "1" - elsif (arg.respond_to?(:to_eval)) - str = arg.to_eval() - else - str = arg.to_s() - end - argstr += " " if argstr != "" - argstr += '"' + str.gsub(/[][$"]/, '\\\\\&') + '"' - } - return argstr - end - - def tk_call(*args) - argstr = _get_eval_string(*args) - - res = INTERP._eval(argstr) - if INTERP._return_value() != 0 - fail RuntimeError, res, error_at - end - return res - end -end - -module Tk - include TkCore - extend Tk - - def root - TkRoot.new - end - - def bell - tk_call 'bell' - end - - def mainloop - TclTkLib.mainloop - end - - module Scrollable - def xscrollcommand(cmd=Proc.new) - configure_cmd 'xscrollcommand', cmd - end - def yscrollcommand(cmd=Proc.new) - configure_cmd 'yscrollcommand', cmd - end - end - - module Wm - def aspect(*args) - w = window(tk_call('wm', 'grid', path, *args)) - w.split.collect{|s|s.to_i} if args.length == 0 - end - def client(name=None) - tk_call 'wm', 'client', path, name - end - def colormapwindows(*args) - list(tk_call('wm', 'colormapwindows', path, *args)) - end - def wm_command(value=None) - string(tk_call('wm', 'command', path, value)) - end - def deiconify - tk_call 'wm', 'deiconify', path - end - def focusmodel(*args) - tk_call 'wm', 'focusmodel', path, *args - end - def frame - tk_call 'wm', 'frame', path - end - def geometry(*args) - list(tk_call('wm', 'geometry', path, *args)) - end - def grid(*args) - w = tk_call('wm', 'grid', path, *args) - list(w) if args.size == 0 - end - def group(*args) - tk_call 'wm', 'path', path, *args - end - def iconbitmap(*args) - tk_call 'wm', 'bitmap', path, *args - end - def iconify - tk_call 'wm', 'iconify' - end - def iconmask(*args) - tk_call 'wm', 'iconmask', path, *args - end - def iconname(*args) - tk_call 'wm', 'iconname', path, *args - end - def iconposition(*args) - w = tk_call('wm', 'iconposition', path, *args) - list(w) if args.size == 0 - end - def iconwindow(*args) - tk_call 'wm', 'iconwindow', path, *args - end - def maxsize(*args) - w = tk_call('wm', 'maxsize', path, *args) - list(w) if not args.size == 0 - end - def minsize(*args) - w = tk_call('wm', 'minsize', path, *args) - list(w) if args.size == 0 - end - def overrideredirect(bool=None) - if bool == None - bool(tk_call('wm', 'overrideredirect', path)) - else - tk_call 'wm', 'overrideredirect', path, bool - end - end - def positionfrom(*args) - tk_call 'wm', 'positionfrom', path, *args - end - def protocol(name, func=None) - func = install_cmd(func) if not func == None - tk_call 'wm', 'command', path, name, func - end - def resizable(*args) - w = tk_call('wm', 'resizable', path, *args) - if args.length == 0 - list(w).collect{|e| bool(e)} - end - end - def sizefrom(*args) - list(tk_call('wm', 'sizefrom', path, *args)) - end - def state - tk_call 'wm', 'state', path - end - def title(*args) - tk_call 'wm', 'title', path, *args - end - def transient(*args) - tk_call 'wm', 'transient', path, *args - end - def withdraw - tk_call 'wm', 'withdraw', path - end - end -end - -class TkVariable - include Tk - - Tk_VARIABLE_ID = ["v00000"] - def initialize(val="") - @id = Tk_VARIABLE_ID[0] - Tk_VARIABLE_ID[0] = Tk_VARIABLE_ID[0].succ - INTERP._eval(format('global %s; set %s %s', @id, @id, _get_eval_string(val))) - end - - def id - @id - end - - def value - INTERP._eval(format('global %s; set %s', @id, @id)) - end - - def value=(val) - INTERP._eval(format('global %s; set %s %s', @id, @id, _get_eval_string(val))) - end - - def to_i - Integer(number(value)) - end - - def to_f - Float(number(value)) - end - - def to_s - String(string(value)) - end - - def inspect - format "<TkVariable: %s>", @id - end - - def ==(other) - case other - when TkVariable - self.equal(self) - when String - self.to_s == other - when Integer - self.to_i == other - when Float - self.to_f == other - when Array - self.to_a == other - else - false - end - end - - def to_a - list(value) - end - - def to_eval - @id - end -end - -module TkSelection - include Tk - extend Tk - def clear(win=Tk.root) - tk_call 'selection', 'clear', win.path - end - def get(type=None) - tk_call 'selection', 'get', type - end - def TkSelection.handle(win, func, type=None, format=None) - id = install_cmd(func) - tk_call 'selection', 'handle', win.path, id, type, format - end - def handle(func, type=None, format=None) - TkSelection.handle self, func, type, format - end - def TkSelection.own(win, func=None) - id = install_cmd(func) - tk_call 'selection', 'own', win.path, id - end - def own(func=None) - TkSelection.own self, func - end - - module_function :clear, :get -end - -module TkWinfo - include Tk - extend Tk - def TkWinfo.atom(name) - tk_call 'winfo', name - end - def winfo_atom(name) - TkWinfo.atom name - end - def TkWinfo.atomname(id) - tk_call 'winfo', id - end - def winfo_atomname(id) - TkWinfo.atomname id - end - def TkWinfo.cells(window) - number(tk_call('winfo', window.path)) - end - def winfo_cells - TkWinfo.cells self - end - def TkWinfo.children(window) - c = tk_call('winfo', 'children', window.path) - list(c) - end - def winfo_children - TkWinfo.children self - end - def TkWinfo.classname(window) - tk_call 'winfo', 'class', window.path - end - def winfo_classname - TkWinfo.classname self - end - def TkWinfo.containing(rootX, rootY) - path = tk_call('winfo', 'class', window.path) - window(path) - end - def winfo_containing(x, y) - TkWinfo.containing x, y - end - def TkWinfo.depth(window) - number(tk_call('winfo', 'depth', window.path)) - end - def winfo_depth(window) - TkWinfo.depth self - end - def TkWinfo.exist?(window) - bool(tk_call('winfo', 'exists', window.path)) - end - def winfo_exist?(window) - TkWinfo.exist? self - end - def TkWinfo.fpixels(window, number) - number(tk_call('winfo', 'fpixels', window.path, number)) - end - def winfo_fpixels(window, number) - TkWinfo.fpixels self - end - def TkWinfo.geometry(window) - list(tk_call('winfo', 'geometry', window.path)) - end - def winfo_geometry(window) - TkWinfo.geometry self - end - def TkWinfo.height(window) - number(tk_call('winfo', 'height', window.path)) - end - def winfo_height(window) - TkWinfo.height self - end - def TkWinfo.id(window) - number(tk_call('winfo', 'id', window.path)) - end - def winfo_id(window) - TkWinfo.id self - end - def TkWinfo.mapped?(window) - bool(tk_call('winfo', 'ismapped', window.path)) - end - def winfo_mapped?(window) - TkWinfo.mapped? self - end - def TkWinfo.parent(window) - window(tk_call('winfo', 'parent', window.path)) - end - def winfo_parent(window) - TkWinfo.parent self - end - def TkWinfo.widget(id) - window(tk_call('winfo', 'pathname', id)) - end - def winfo_widget(id) - TkWinfo.widget id - end - def TkWinfo.pixels(window, number) - number(tk_call('winfo', 'pixels', window.path, number)) - end - def winfo_pixels(window, number) - TkWinfo.pixels self, number - end - def TkWinfo.reqheight(window) - number(tk_call('winfo', 'reqheight', window.path)) - end - def winfo_reqheight(window) - TkWinfo.reqheight self - end - def TkWinfo.reqwidth(window) - number(tk_call('winfo', 'reqwidth', window.path)) - end - def winfo_reqwidth(window) - TkWinfo.reqwidth self - end - def TkWinfo.rgb(window, color) - list(tk_call('winfo', 'rgb', window.path, color)) - end - def winfo_rgb(window, color) - TkWinfo.rgb self, color - end - def TkWinfo.rootx(window) - number(tk_call('winfo', 'rootx', window.path)) - end - def winfo_rootx(window) - TkWinfo.rootx self - end - def TkWinfo.rooty(window) - number(tk_call('winfo', 'rooty', window.path)) - end - def winfo_rooty(window) - TkWinfo.rooty self - end - def TkWinfo.screen(window) - tk_call 'winfo', 'screen', window.path - end - def winfo_screen(window) - TkWinfo.screen self - end - def TkWinfo.screencells(window) - number(tk_call('winfo', 'screencells', window.path)) - end - def winfo_screencells(window) - TkWinfo.screencells self - end - def TkWinfo.screendepth(window) - number(tk_call('winfo', 'screendepth', window.path)) - end - def winfo_screendepth(window) - TkWinfo.screendepth self - end - def TkWinfo.screenheight (window) - number(tk_call('winfo', 'screenheight', window.path)) - end - def winfo_screenheight(window) - TkWinfo.screenheight self - end - def TkWinfo.screenmmheight(window) - number(tk_call('winfo', 'screenmmheight', window.path)) - end - def winfo_screenmmheight(window) - TkWinfo.screenmmheight self - end - def TkWinfo.screenmmwidth(window) - number(tk_call('winfo', 'screenmmwidth', window.path)) - end - def winfo_screenmmwidth(window) - TkWinfo.screenmmwidth self - end - def TkWinfo.screenvisual(window) - tk_call 'winfo', 'screenvisual', window.path - end - def winfo_screenvisual(window) - TkWinfo.screenvisual self - end - def TkWinfo.screenwidth(window) - number(tk_call('winfo', 'screenwidth', window.path)) - end - def winfo_screenwidth(window) - TkWinfo.screenwidth self - end - def TkWinfo.toplevel(window) - window(tk_call('winfo', 'toplevel', window.path)) - end - def winfo_toplevel(window) - TkWinfo.toplevel self - end - def TkWinfo.visual(window) - tk_call 'winfo', 'visual', window.path - end - def winfo_visual(window) - TkWinfo.visual self - end - def TkWinfo.vrootheigh(window) - number(tk_call('winfo', 'vrootheight', window.path)) - end - def winfo_vrootheight(window) - TkWinfo.vrootheight self - end - def TkWinfo.vrootwidth(window) - number(tk_call('winfo', 'vrootwidth', window.path)) - end - def winfo_vrootwidth(window) - TkWinfo.vrootwidth self - end - def TkWinfo.vrootx(window) - number(tk_call('winfo', 'vrootx', window.path)) - end - def winfo_vrootx(window) - TkWinfo.vrootx self - end - def TkWinfo.vrooty(window) - number(tk_call('winfo', 'vrooty', window.path)) - end - def winfo_vrooty(window) - TkWinfo.vrooty self - end - def TkWinfo.width(window) - number(tk_call('winfo', 'width', window.path)) - end - def winfo_width(window) - TkWinfo.width self - end - def TkWinfo.x(window) - number(tk_call('winfo', 'x', window.path)) - end - def winfo_x(window) - TkWinfo.x self - end - def TkWinfo.y(window) - number(tk_call('winfo', 'y', window.path)) - end - def winfo_y(window) - TkWinfo.y self - end -end - -module TkPack - include Tk - extend Tk - def configure(win, *args) - if args[-1].kind_of?(Hash) - keys = args.pop - end - wins = [win.epath] - for i in args - wins.push i.epath - end - tk_call "pack", 'configure', *(wins+hash_kv(keys)) - end - - def forget(*args) - tk_call 'pack', 'forget' *args - end - - def propagate(master, bool=None) - bool(tk_call('pack', 'propagate', master.epath, bool)) - end - module_function :configure, :forget, :propagate -end - -module TkOption - include Tk - extend Tk - def add pat, value, pri=None - tk_call 'option', 'add', pat, value, pri - end - def clear - tk_call 'option', 'clear' - end - def get win, classname, name - tk_call 'option', 'get', classname, name - end - def readfile file, pri=None - tk_call 'option', 'readfile', file, pri - end - module_function :add, :clear, :get, :readfile -end - -class TkObject<TkKernel - include Tk - - def path - return @path - end - - def epath - return @path - end - - def to_eval - @path - end - - def tk_send(cmd, *rest) - tk_call path, cmd, *rest - end - private :tk_send - - def method_missing(id, *args) - if (args.length == 1) - configure id.id2name, args[0] - else - $@ = error_at - super - end - end - - def []=(id, val) - configure id, val - 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 - end - - def configure_cmd(slot, value) - configure slot, install_cmd(value) - end - - def bind(context, cmd=Proc.new, args=nil) - _bind path, context, cmd, args - end - - def tk_trace_variable(v) - unless v.kind_of?(TkVariable) - fail ArgumentError, format("requires TkVariable given %s", v.type) - end - v - end - private :tk_trace_variable - - def destroy - tk_call 'trace', 'vdelete', @tk_vn, 'w', @var_id if @var_id - end -end - -class TkWindow<TkObject - - def initialize(parent=nil, keys=nil) - install_win(if parent then parent.path end) - create_self - if keys - tk_call @path, 'configure', *hash_kv(keys) - end - end - - def create_self - end - private :create_self - - def pack(keys = nil) - tk_call 'pack', epath, *hash_kv(keys) - self - end - - def unpack(keys = nil) - tk_call 'pack', 'forget', epath - self - end - - def place(keys = nil) - tk_call 'place', epath, *hash_kv(keys) - self - end - - def unplace(keys = nil) - tk_call 'place', 'forget', epath, *hash_kv(keys) - self - end - alias place_forget unplace - - def place_config(keys) - tk_call "place", 'configure', epath, *hash_kv(keys) - end - - def place_info() - ilist = list(tk_call('place', 'info', epath)) - info = {} - while key = ilist.shift - info[key[1,-1]] = ilist.shift - end - return info - end - - def place_slaves() - list(tk_call('place', 'slaves', epath)).collect { |w| - window(w) - } - end - - def focus - tk_call 'focus', path - self - end - - def grab(*args) - if !args or args.length == 0 - tk_call 'grab', 'set', path - elsif args.length == 1 - case args[0] - when 'global' - tk_call 'grab', 'set', '-global', path - else - val = tk_call('grab', arg[0], path) - end - case args[0] - when 'current' - return window(val) - when 'status' - return val - end - else - fail ArgumentError, 'wrong # of args' - end - end - - def lower(below=None) - tk_call 'lower', path, below - self - end - def raise(above=None) - tk_call 'raise', path, above - self - end - - def command(cmd=Proc.new) - configure_cmd 'command', cmd - end - - def colormodel model=None - tk_call 'tk', 'colormodel', path, model - self - end - - def destroy - tk_call 'destroy', path - if @cmdtbl - for id in @cmdtbl - uninstall_cmd id - end - end - uninstall_win - end -end - -class TkRoot<TkWindow - include Wm - ROOT = [] - def TkRoot.new - return ROOT[0] if ROOT[0] - new = super - ROOT[0] = new - end - def create_self - @path = '.' - end - def path - "." - end -end - -class TkToplevel<TkWindow - include Wm - def initialize(parent=nil, screen=nil, classname=nil, keys=nil) - @screen = screen if screen - @classname = classname if classname - super(parent, keys) - end - - def create_self - s = [] - s.push "-screen #@screen" if @screen - s.push "-class #@classname" if @classname - tk_call 'toplevel', path, *s - end -end - -class TkFrame<TkWindow - def create_self - tk_call 'frame', @path - end -end - -class TkLabel<TkWindow - def create_self - tk_call 'label', @path - end - def textvariable(v) - configure 'textvariable', tk_trace_variable(v) - end -end - -class TkButton<TkLabel - def create_self - tk_call 'button', @path - end - def invoke - tk_send 'invoke' - end - def flash - tk_send 'flash' - end -end - -class TkRadioButton<TkButton - def create_self - tk_call 'radiobutton', @path - end - def deselect - tk_send 'deselect' - end - def select - tk_send 'select' - end - def variable(v) - configure 'variable', tk_trace_variable(v) - end -end - -class TkCheckButton<TkRadioButton - def create_self - tk_call 'checkbutton', @path - end - def toggle - tk_send 'toggle' - end -end - -class TkMessage<TkLabel - def create_self - tk_call 'message', @path - end -end - -class TkScale<TkWindow - def create_self - tk_call 'scale', path - end - - def get - number(tk_send('get')) - end - - def set(val) - tk_send "set", val - end - - def value - get - end - - def value= (val) - set val - end -end - -class TkScrollbar<TkWindow - def create_self - tk_call 'scrollbar', path - end - - def delta(deltax=None, deltay=None) - number(tk_send('delta', deltax, deltay)) - end - - def fraction(x=None, y=None) - number(tk_send('fraction', x, y)) - end - - def identify(x=None, y=None) - tk_send('fraction', x, y) - end - - def get - ary1 = tk_send('get', path).split - ary2 = [] - for i in ary1 - ary2.push number(i) - end - ary2 - end - - def set(first, last) - tk_send "set", first, last - end -end - -class TkTextWin<TkWindow - def bbox(index) - tk_send 'bbox', index - end - def delete(first, last=None) - tk_send 'delete', first, last - end - def get(*index) - tk_send 'get', *index - end - def index(index) - tk_send 'index', index - end - def insert(index, chars, *args) - tk_send 'insert', index, chars, *args - 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 see(index) - tk_send 'see', index - end -end - -class TkListbox<TkTextWin - def create_self - tk_call 'listbox', path - end - - def curselection - tk_send 'curselection' - end - def nearest(y) - tk_send 'nearest', y - end - def selection_anchor(index) - tk_send 'selection', 'anchor', index - end - def selection_clear(first, last=None) - tk_send 'selection', 'clear', first, last - end - def selection_includes - bool(tk_send('selection', 'includes')) - end - def selection_set(first, last=None) - tk_send 'selection', 'set', first, last - end - def xview(cmd, index, *more) - tk_send 'xview', cmd, index, *more - end - def yview(cmd, index, *more) - tk_send 'yview', cmd, index, *more - end -end - -class TkMenu<TkWindow - def create_self - tk_call 'menu', path - end - def activate(index) - tk_send 'activate', index - end - def add(type, keys=nil) - tk_send 'add', type, *hash_kv(keys) - end - def index(index) - tk_send 'index', index - end - def invoke - tk_send 'invoke' - end - def insert(index, type, *keys) - tk_send 'add', index, type, *hash_kv(keys) - end - def post(x, y) - tk_send 'post', x, y - end - def postcascade(index) - tk_send 'postcascade', index - end - def postcommand(cmd=Proc.new) - configure_cmd 'postcommand', cmd - end - def menutype(index) - tk_send 'type', index - end - def unpost - tk_send 'unpost' - end - def yposition(index) - number(tk_send('yposition', index)) - end -end - -class TkMenubutton<TkLabel - def create_self - tk_call 'menubutton', path - end -end - -module TkComposite - def initialize(parent=nil, *args) - @frame = TkFrame.new(parent) - @path = @epath = @frame.path - initialize_composite(*args) - end - - def epath - @epath - end - - def initialize_composite(*args) end - private :initialize_composite - - def delegate(option, *wins) - unless @delegates - @delegates = {} - @delegates['DEFAULT'] = @frame - end - if option.kind_of?(String) - @delegates[option] = wins - else - for i in option - @delegates[i] = wins - end - 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) - end - end - last - else - super - end - end -end - -autoload :TkCanvas, 'tkcanvas' -autoload :TkImage, 'tkcanvas' -autoload :TkBitmapImage, 'tkcanvas' -autoload :TkPhotoImage, 'tkcanvas' -autoload :TkEntry, 'tkentry' -autoload :TkText, 'tktext' diff --git a/lib/tkcanvas.rb b/lib/tkcanvas.rb deleted file mode 100644 index a02db097fd..0000000000 --- a/lib/tkcanvas.rb +++ /dev/null @@ -1,326 +0,0 @@ -# -# tkcanvas.rb - Tk canvas classes -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require "tk" - -class TkCanvas<TkWindow - def create_self - tk_call 'canvas', path - end - def tagid(tag) - if tag.kind_of?(TkcItem) - tag.id - else - tag - end - end - private :tagid - def addtag(tag, *args) - tk_send 'addtag', tagid(tag), *args - end - def addtag_above(tagOrId) - addtag('above', tagOrId) - end - def addtag_all - addtag('all') - end - def addtag_below(tagOrId) - addtag('below', tagOrId) - end - def addtag_closest(x, y, halo=None, start=None) - addtag('closest', x, y, halo, start) - end - def addtag_enclosed(x1, y1, x2, y2) - addtag('enclosed', x1, y1, x2, y2) - end - def addtag_overlapping(x1, y1, x2, y2) - addtag('overlapping', x1, y1, x2, y2) - end - def addtag_withtag(tagOrId) - addtag('withtag', tagOrId) - end - def bbox(tag) - list(tk_send('bbox', tagid(tag))) - end - def itembind(tag, seq, cmd=Proc.new) - id = install_cmd(cmd) - tk_send 'bind', tagid(tag), "<#{seq}>", id - @cmdtbl.push id - end - def canvasx(x, *args) - tk_send 'canvasx', x, *args - end - def canvasy(y, *args) - tk_send 'canvasy', y, *args - end - def coords(tag, *args) - tk_send 'coords', tagid(tag), *args - 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 - end - def itemfocus(tag) - tk_send 'find', tagid(tag) - end - def gettags(tag) - tk_send 'gettags', tagid(tag) - end - def icursor(tag, index) - tk_send 'icursor', tagid(tag), index - end - def index(tag) - tk_send 'index', tagid(tag), index - end - 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 - end - def xview(*index) - tk_send 'xview', *index - end - def yview(*index) - tk_send 'yview', *index - 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 - end - def create_self(*args) end - private :create_self - def id - return @id - end - - def configure(slot, value) - tk_call path, 'itemconfigure', @id, "-#{slot}", value - end - - def addtag(tag) - @c.addtag(tag, 'withtag', @id) - end - def bbox - @c.bbox(@id) - end - def bind(seq, cmd=Proc.new) - @c.itembind @id, seq, cmd - 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 - end - def focus - @c.focus @id - end - def gettags - @c.gettags @id - end - def icursor - @c.icursor @id - end - def index - @c.index @id - 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 itemtype - @c.itemtype @id - end - def destroy - tk_call path, 'delete', @id - end -end - -class TkcArc<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'arc', *args) - end -end -class TkcBitmap<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'bitmap', *args) - end -end -class TkcImage<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'image', *args) - end -end -class TkcLine<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'line', *args) - end -end -class TkcOval<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'oval', *args) - end -end -class TkcPolygon<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'polygon', *args) - end -end -class TkcRectangle<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'rectangle', *args) - end -end -class TkcText<TkcItem - def create_self(*args) - tk_call(@path, 'create', 'text', *args) - end -end -class TkcWindow<TkcItem - 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_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) - end - - def height - number(tk_call('image', 'height', @path)) - end - def itemtype - tk_call('image', 'type', @path) - end - def width - number(tk_call('image', 'height', @path)) - end - - def TkImage.names - tk_call('image', 'names', @path).split - end - def TkImage.types - tk_call('image', 'types', @path).split - end -end - -class TkBitmapImage<TkImage - def initialize(*args) - @type = 'bitmap' - super - end -end - -class TkPhotoImage<TkImage - def initialize(*args) - @type = 'photo' - super - end - - def blank - tk_send 'blank' - end - def cget - tk_send 'cget' - end - def get(x, y) - tk_send 'get', x, y - end - def put(data, to=None) - tk_send 'put', data, to - end -end diff --git a/lib/tkclass.rb b/lib/tkclass.rb deleted file mode 100644 index 0b33d4ec8b..0000000000 --- a/lib/tkclass.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# tkclass.rb - Tk classes -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require "tk" - -TopLevel = TkToplevel -Frame = TkFrame -Label = TkLabel -Button = TkButton -Radiobutton = TkRadioButton -Checkbutton = TkCheckButton -Message = TkMessage -Entry = TkEntry -Text = TkText -Scale = TkScale -Scrollbar = TkScrollbar -Listbox = TkListbox -Menu = TkMenu -Menubutton = TkMenubutton -Canvas = TkCanvas -Arc = TkcArc -Bitmap = TkcBitmap -Line = TkcLine -Oval = TkcOval -Polygon = TkcPolygon -Rectangle = TkcRectangle -TextItem = TkcText -WindowItem = TkcWindow -Selection = TkSelection -Winfo = TkWinfo -Pack = TkPack -Variable = TkVariable - -def Mainloop - Tk.mainloop -end diff --git a/lib/tkcore.rb b/lib/tkcore.rb deleted file mode 100644 index c151b0af9e..0000000000 --- a/lib/tkcore.rb +++ /dev/null @@ -1,528 +0,0 @@ -# -# tkcore.rb - Tk interface modue without thread -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require "tkutil" -if defined? Thread - require "thread" -end - -module Tk - include TkUtil - extend Tk - - wish_path = nil - ENV['PATH'].split(":").each {|path| - for wish in ['wish4.2', 'wish4.1', 'wish4.0', 'wish'] - if File.exist? path+'/'+wish - wish_path = path+'/'+wish - break - end - break if wish_path - end - } - fail 'can\'t find wish' if not wish_path #' - - def Tk.tk_exit - if not PORT.closed? - PORT.print "exit\n" - PORT.close - end - end - -# PORT = open(format("|%s -n %s", wish_path, File.basename($0)), "w+"); - PORT = open(format("|%s", wish_path), "w+"); - trap "EXIT", proc{Tk.tk_exit} - trap "PIPE", "" - - def tk_write(*args) - printf PORT, *args; - PORT.print "\n" - PORT.flush - end - tk_write '\ -wm withdraw . -proc rb_out args { - puts [format %%s $args] - flush stdout -} -proc rb_ans arg { - if [catch $arg var] {puts "!$var"} {puts "=$var@@"} - flush stdout -} -proc tkerror args { exit } -proc keepalive {} { rb_out alive; after 120000 keepalive} -after 120000 keepalive' - - READABLE = [] - READ_CMD = {} - - def file_readable(port, cmd) - if cmd == nil - READABLE.delete port - else - READABLE.push port - end - READ_CMD[port] = cmd - end - - WRITABLE = [] - WRITE_CMD = {} - def file_writable(port, cmd) - if cmd == nil - WRITABLE.delete port - else - WRITABLE.push port - end - WRITE_CMD[port] = cmd - end - module_function :file_readable, :file_writable - - file_readable PORT, proc { - line = PORT.gets - exit if not line - Tk.dispatch(line.chop!) - } - - def error_at - frames = caller(1) - frames.delete_if do |c| - c =~ %r!/tk(|core|thcore|canvas|text|entry|scrollbox)\.rb:\d+! - end - frames - end - - def tk_tcl2ruby(val) - case val - when /^-?\d+$/ - val.to_i - when /^\./ - $tk_window_list[val] - when /^rb_out (c\d+)/ - $tk_cmdtbl[$1] - when / / - val.split.collect{|elt| - tk_tcl2ruby(elt) - } - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - - def tk_split_list(str) - idx = str.index('{') - return tk_tcl2ruby(str) if not idx - - list = tk_tcl2ruby(str[0,idx]) - str = str[idx+1..-1] - i = -1 - brace = 1 - str.each_byte {|c| - i += 1 - brace += 1 if c == ?{ - brace -= 1 if c == ?} - break if brace == 0 - } - if str[0, i] == ' ' - list.push ' ' - else - list.push tk_split_list(str[0, i]) - end - list += tk_split_list(str[i+1..-1]) - list - end - private :tk_tcl2ruby, :tk_split_list - - def bool(val) - case bool - when "1", 1, 'yes', 'true' - TRUE - else - FALSE - end - end - def number(val) - case val - when /^-?\d+$/ - val.to_i - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - def string(val) - if val == "{}" - '' - elsif val[0] == ?{ - val[1..-2] - else - val - end - end - def list(val) - tk_split_list(val) - end - def window(val) - $tk_window_list[val] - end - def procedure(val) - if val =~ /^rb_out (c\d+)/ - $tk_cmdtbl[$1] - else - nil - end - end - private :bool, :number, :string, :list, :window, :procedure - - # mark for non-given arguments - None = Object.new - def None.to_s - 'None' - end - - $tk_event_queue = [] - def tk_call(str, *args) - args = args.collect{|s| - next if s == None - if s.kind_of?(Hash) - s = hash_kv(s).join(" ") - else - if not s - s = "0" - elsif s == TRUE - s = "1" - elsif s.kind_of?(TkObject) - s = s.path - elsif s.kind_of?(TkVariable) - s = s.id - else - s = s.to_s - s.gsub!(/["\\\$\[\]]/, '\\\\\0') #" - s.gsub!(/\{/, '\\\\173') - s.gsub!(/\}/, '\\\\175') - end - "\"#{s}\"" - end - } - str += " " - str += args.join(" ") - print str, "\n" if $DEBUG - tk_write 'rb_ans {%s}', str - while PORT.gets - print $_ if $DEBUG - $_.chop! - if /^=(.*)@@$/ - val = $1 - break - elsif /^=/ - val = $' + "\n" - while TRUE - PORT.readline - if ~/@@$/ - val += $' - return val - else - val += $_ - end - end - elsif /^!/ - $@ = error_at - msg = $' - if msg =~ /unknown option "-(.*)"/ - $! = NameError.new(format("undefined method `%s' for %s(%s)", - $1, self, self.type)) #`' - else - $! = RuntimeError.new(format("%s - %s", self.type, msg)) - end - fail - end - $tk_event_queue.push $_ - end - - while ev = $tk_event_queue.shift - Tk.dispatch ev - end - fail 'wish closed' if PORT.closed? -# tk_split_list(val) - val - end - - def hash_kv(keys) - conf = [] - if keys - for k, v in keys - conf.push("-#{k}") - v = install_cmd(v) if v.kind_of? Proc - conf.push(v) - end - end - conf - end - private :tk_call, :error_at, :hash_kv - - $tk_cmdid = 0 - def install_cmd(cmd) - return '' if cmd == '' # uninstall cmd - id = format("c%.4d", $tk_cmdid) - $tk_cmdid += 1 - $tk_cmdtbl[id] = cmd - @cmdtbl = [] if not @cmdtbl - @cmdtbl.push id - return format('rb_out %s', id) - end - def uninstall_cmd(id) - $tk_cmdtbl[id] = nil - end - private :install_cmd, :uninstall_cmd - - $tk_window_list = {} - class Event - def initialize(seq,b,f,h,k,s,t,w,x,y,aa,ee,kk,nn,ww,tt,xx,yy) - @serial = seq - @num = b - @focus = (f == 1) - @height = h - @keycode = k - @state = s - @time = t - @width = w - @x = x - @y = y - @char = aa - @send_event = (ee == 1) - @keysym = kk - @keysym_num = nn - @type = tt - @widget = ww - @x_root = xx - @y_root = yy - end - attr :serial - attr :num - attr :focus - attr :height - attr :keycode - attr :state - attr :time - attr :width - attr :x - attr :y - attr :char - attr :send_event - attr :keysym - attr :keysym_num - attr :type - attr :widget - attr :x_root - attr :y_root - end - - def install_bind(cmd, args=nil) - if args - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, *arg - }) - id + " " + args - else - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, Event.new(*arg) - }) - id + " %# %b %f %h %k %s %t %w %x %y %A %E %K %N %W %T %X %Y" - end - end - - def _bind(path, context, cmd, args=nil) - begin - id = install_bind(cmd, args) - tk_call 'bind', path, "<#{context}>", id - rescue - $tk_cmdtbl[id] = nil - fail - end - end - private :install_bind, :_bind - - def bind_all(context, cmd=Proc.new, args=nil) - _bind 'all', context, cmd, args - end - - def pack(*args) - TkPack.configure *args - end - - $tk_cmdtbl = {} - - def after(ms, cmd=Proc.new) - myid = format("c%.4d", $tk_cmdid) - tk_call 'after', ms, - install_cmd(proc{ - TkUtil.eval_cmd cmd - uninstall_cmd myid - }) - end - - def update(idle=nil) - if idle - tk_call 'update', 'idletasks' - else - tk_call 'update' - end - end - - def dispatch(line) - if line =~ /^c\d+/ - cmd = $& - fail "no command `#{cmd}'" if not $tk_cmdtbl[cmd] - args = tk_split_list($') - TkUtil.eval_cmd $tk_cmdtbl[cmd], *args - elsif line =~ /^alive$/ - # keep alive, do nothing - else - fail "malformed line <#{line}>" - end - end - - def mainloop - begin - tk_write 'after idle {wm deiconify .}' - while TRUE - rf, wf = select(READABLE, WRITABLE) - for f in rf - READ_CMD[f].call(f) if READ_CMD[f] - if f.closed? - READABLE.delete f - READ_CMD[f] = nil - end - end - for f in wf - WRITE_CMD[f].call(f) if WRITE_CMD[f] - if f.closed? - WRITABLE.delete f - WRITE_CMD[f] = nil - end - end - end - ensure - Tk.tk_exit - end - end - - def root - $tk_root - end - - def bell - tk_call 'bell' - end - module_function :after, :update, :dispatch, :mainloop, :root, :bell - - module Scrollable - def xscrollcommand(cmd=Proc.new) - configure_cmd 'xscrollcommand', cmd - end - def yscrollcommand(cmd=Proc.new) - configure_cmd 'yscrollcommand', cmd - end - end - - module Wm - def aspect(*args) - w = window(tk_call('wm', 'grid', path, *args)) - w.split.collect{|s|s.to_i} if args.length == 0 - end - def client(name=None) - tk_call 'wm', 'client', path, name - end - def colormapwindows(*args) - list(tk_call('wm', 'colormapwindows', path, *args)) - end - def wm_command(value=None) - string(tk_call('wm', 'command', path, value)) - end - def deiconify - tk_call 'wm', 'deiconify', path - end - def focusmodel(*args) - tk_call 'wm', 'focusmodel', path, *args - end - def frame - tk_call 'wm', 'frame', path - end - def geometry(*args) - list(tk_call('wm', 'geometry', path, *args)) - end - def grid(*args) - w = tk_call('wm', 'grid', path, *args) - list(w) if args.size == 0 - end - def group(*args) - tk_call 'wm', 'path', path, *args - end - def iconbitmap(*args) - tk_call 'wm', 'bitmap', path, *args - end - def iconify - tk_call 'wm', 'iconify' - end - def iconmask(*args) - tk_call 'wm', 'iconmask', path, *args - end - def iconname(*args) - tk_call 'wm', 'iconname', path, *args - end - def iconposition(*args) - w = tk_call('wm', 'iconposition', path, *args) - list(w) if args.size == 0 - end - def iconwindow(*args) - tk_call 'wm', 'iconwindow', path, *args - end - def maxsize(*args) - w = tk_call('wm', 'maxsize', path, *args) - list(w) if not args.size == 0 - end - def minsize(*args) - w = tk_call('wm', 'minsize', path, *args) - list(w) if args.size == 0 - end - def overrideredirect(bool=None) - if bool == None - bool(tk_call('wm', 'overrideredirect', path)) - else - tk_call 'wm', 'overrideredirect', path, bool - end - end - def positionfrom(*args) - tk_call 'wm', 'positionfrom', path, *args - end - def protocol(name, func=None) - func = install_cmd(func) if not func == None - tk_call 'wm', 'command', path, name, func - end - def resizable(*args) - w = tk_call('wm', 'resizable', path, *args) - if args.length == 0 - list(w).collect{|e| bool(e)} - end - end - def sizefrom(*args) - list(tk_call('wm', 'sizefrom', path, *args)) - end - def state - tk_call 'wm', 'state', path - end - def title(*args) - tk_call 'wm', 'title', path, *args - end - def transient(*args) - tk_call 'wm', 'transient', path, *args - end - def withdraw - tk_call 'wm', 'withdraw', path - end - end -end diff --git a/lib/tkdialog.rb b/lib/tkdialog.rb deleted file mode 100644 index e8f2142e07..0000000000 --- a/lib/tkdialog.rb +++ /dev/null @@ -1,62 +0,0 @@ -require "tk" - -class TkDialog < TkWindow - # initialize tk_dialog - def initialize - super - @var = TkVariable.new - id = @var.id - INTERP._eval('eval {global '+id+';'+ - 'set '+id+' [tk_dialog '+ - @path+" "+title+" \"#{message}\" "+bitmap+" "+ - default_button+" "+buttons+']}') - end - def value - return @var.value.to_i - end - ###################################################### - # # - # these methods must be overridden for each dialog # - # # - ###################################################### - def title - return "DIALOG" - end - def message - return "MESSAGE" - end - def bitmap - return "info" - end - def default_button - return 0 - end - def buttons - return "BUTTON1 BUTTON2" - end -end - -# -# dialog for warning -# -class TkWarning < TkDialog - def initialize(mes) - @mes = mes - super() - end - def message - return @mes - end - def title - return "WARNING"; - end - def bitmap - return "warning"; - end - def default_button - return 0; - end - def buttons - return "OK"; - end -end diff --git a/lib/tkentry.rb b/lib/tkentry.rb deleted file mode 100644 index bcf092a15c..0000000000 --- a/lib/tkentry.rb +++ /dev/null @@ -1,67 +0,0 @@ -# -# tkentry.rb - Tk entry classes -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require 'tk.rb' - -class TkEntry<TkLabel - def create_self - tk_call 'entry', @path - end - def scrollcommand(cmd) - configure 'scrollcommand', cmd - end - - def delete(s, e=None) - tk_send 'delete', s, e - end - - def cursor - tk_send 'index', 'insert' - end - def cursor=(index) - tk_send 'icursor', index - end - def index(index) - number(tk_send('index', index)) - end - def insert(pos,text) - tk_send 'insert', pos, text - end - def mark(pos) - tk_send 'scan', 'mark', pos - end - def dragto(pos) - tk_send 'scan', 'dragto', pos - end - def select_adjust(index) - tk_send 'select', 'adjust', index - end - def select_clear - tk_send 'select', 'clear', 'end' - end - def select_from(index) - tk_send 'select', 'from', index - end - def select_present() - tk_send('select', 'present') == 1 - end - def select_range(s, e) - tk_send 'select', 'range', s, e - end - def select_to(index) - tk_send 'select', 'to', index - end - def xview(*index) - tk_send 'xview', *index - end - - def value - tk_send 'get' - end - def value= (val) - tk_send 'delete', 0, 'end' - tk_send 'insert', 0, val - end -end diff --git a/lib/tkscrollbox.rb b/lib/tkscrollbox.rb deleted file mode 100644 index 8d129b2f4b..0000000000 --- a/lib/tkscrollbox.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# tkscrollbox.rb - Tk Listbox with Scrollbar -# as an example of Composite Widget -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require 'tk.rb' - -class TkScrollbox<TkListbox - include TkComposite - def initialize_composite - list = TkListbox.new(@frame) - scroll = TkScrollbar.new(@frame) - @path = list.path - - list.configure 'yscroll', scroll.path+" set" - list.pack 'side'=>'left','fill'=>'both','expand'=>'yes' - scroll.configure 'command', list.path+" yview" - scroll.pack 'side'=>'right','fill'=>'y' - - delegate('DEFAULT', list) - delegate('foreground', list) - delegate('background', list, scroll) - delegate('borderwidth', @frame) - delegate('relief', @frame) - end -end diff --git a/lib/tktext.rb b/lib/tktext.rb deleted file mode 100644 index 47e11f2c85..0000000000 --- a/lib/tktext.rb +++ /dev/null @@ -1,164 +0,0 @@ -# -# tktext.rb - Tk text classes -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require 'tk.rb' - -class TkText<TkTextWin - include Scrollable - def create_self - tk_call 'text', @path - @tags = {} - end - def index(index) - tk_send 'index', index - end - def value - tk_send 'get', "1.0", "end" - end - def value= (val) - tk_send 'delete', "1.0", 'end' - tk_send 'insert', "1.0", val - end - def _addcmd(cmd) - @cmdtbl.push id - end - def _addtag(name, obj) - @tags[name] = obj - end - def tag_names - tk_send('tag', 'names').collect{|elt| - if not @tags[elt] - elt - else - @tags[elt] - end - } - end - def window_names - tk_send('window', 'names').collect{|elt| - if not @tags[elt] - elt - else - @tags[elt] - end - } - end - - def destroy - for t in @tags - t.destroy - end - super - end - - def backspace - self.delete 'insert' - end - - def compare(idx1, op, idx2) - bool(tk_send('compare', idx1, op, idx2)) - end - - def debug - bool(tk_send('debug')) - end - def debug=(boolean) - tk_send 'debug', boolean - end - - def yview(*what) - tk_send 'yview', *what - end - def yview_pickplace(*what) - tk_send 'yview', '-pickplace', *what - end - - def xview(*what) - tk_send 'xview', *what - end - def xview_pickplace(*what) - tk_send 'xview', '-pickplace', *what - end -end - -class TkTextTag<TkObject - $tk_text_tag = 'tag0000' - 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 = $tk_text_tag - $tk_text_tag = $tk_text_tag.succ - tk_call @t.path, "tag", "configure", @id, *hash_kv(keys) - @t._addtag id, self - end - def id - return @id - end - - def add(*index) - tk_call @t.path, 'tag', 'add', @id, *index - end - - def configure(keys) - tk_call @t.path, 'tag', 'configure', @id, *hash_kv(keys) - end - - def bind(seq, cmd=Proc.new) - id = install_cmd(cmd) - tk_call @t, 'tag', 'bind', tag, "<#{seq}>", id - @t._addcmd cmd - end - - def lower(below=None) - tk_call @t.path, 'tag', 'lower', below - end - - def destroy - tk_call @t.path, 'tag', 'delete', @id - end -end - -class TkTextMark<TkObject - $tk_text_mark = 'mark0000' - def initialize(parent, index) - if not parent.kind_of?(TkText) - fail format("%s need to be TkText", parent.inspect) - end - @t = parent - @path = @id = $tk_text_mark - $tk_text_mark = $tk_text_mark.succ - tk_call @t.path, 'mark', 'set', @id, index - @t._addtag id, self - end - def id - return @id - end - - def set(where) - tk_call @t.path, 'mark', 'set', @id, where - end - - def unset - tk_call @t.path, 'mark', 'unset', @id - end - alias destroy unset -end - -class TkTextWindow<TkObject - def initialize(parent, index, *args) - 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 - end - - def configure(slot, value) - tk_call @t.path, 'window', 'configure', @index, "-#{slot}", value - end -end diff --git a/lib/tkthcore.rb b/lib/tkthcore.rb deleted file mode 100644 index a6648502bd..0000000000 --- a/lib/tkthcore.rb +++ /dev/null @@ -1,550 +0,0 @@ -# -# tkthcore.rb - Tk interface modue using thread -# $Date$ -# by Yukihiro Matsumoto <matz@caelum.co.jp> - -require "tkutil" -require "thread" - -module Tk - include TkUtil - extend Tk - - def Tk.tk_exit - if not PORT.closed? - tk_write "exit" - PORT.close - end - end - - trap "EXIT", proc{Tk.tk_exit} - trap "PIPE", '' - - wish_path = nil - ENV['PATH'].split(":").each {|path| - for wish in ['wish4.2', 'wish4.1', 'wish4.0', 'wish'] - if File.exist? path+'/'+wish - wish_path = path+'/'+wish - break - end - break if wish_path - end - } - fail 'can\'t find wish' if not wish_path #' - - # mark for non-given arguments - None = Object.new - def None.to_s - 'None' - end - - Qin = Queue.new - Qout = Queue.new - Qwish = Queue.new - Qcmd = Queue.new - PORT = open(format("|%s -n %s", wish_path, File.basename($0)), "w+"); - - $tk_not_init = TRUE - - def Tk.init - $tk_not_init = FALSE - Thread.start do - loop do - while line = PORT.gets - line.chop! - if line =~ /^[=!]/ - Qwish.push line - else - Qcmd.push line - end - end - exit - end - end - - Thread.start do - ary = [PORT] - loop do - str = Qin.pop - print "Qin: ", str, "\n" if $DEBUG - tk_write 'if [catch {%s} var] {puts "!$var"} {puts "=$var@@"};flush stdout', str - line = tk_recv - Qout.push(line) - end - end - end - - $tk_event_queue = [] - def tk_recv() - val = nil - $_ = Qwish.pop - loop do - if /^=(.*)@@$/ - val = $1 - break - elsif /^=/ - val = $' + "\n" - while TRUE - PORT.readline - if ~/@@$/ - val += $' - return val - else - val += $_ - end - end - elsif /^!/ - $@ = error_at - msg = $' - if msg =~ /unknown option "-(.*)"/ - fail NameError, format("undefined method `%s' for %s(%s)", $1, self, self.type) #`' - else - fail format("%s - %s", self.type, msg) - end - end - end - - fail 'wish closed' if PORT.closed? -# tk_split_list(val) - val - end - - def tk_call(str, *args) - Tk.init if $tk_not_init - args = args.collect{|s| - next if s == None - if s.kind_of?(Hash) - s = hash_kv(s).join(" ") - else - if not s - s = "0" - elsif s == TRUE - s = "1" - elsif s.kind_of?(TkObject) - s = s.path - elsif s.kind_of?(TkVariable) - s = s.id - else - s = s.to_s - s.gsub!(/["\\\$\[\]]/, '\\\\\0') #" - s.gsub!(/\{/, '\\\\173') - s.gsub!(/\}/, '\\\\175') - end - "\"#{s}\"" - end - } - str += " " - str += args.join(" ") - Qin.push str - return Qout.pop - end - - def tk_write(*args) - PORT.printf *args; PORT.print "\n" - PORT.flush - end - module_function :tk_write, :tk_recv - tk_write '\ -wm withdraw . -proc rb_out args { - puts [format %%s $args] - flush stdout -} -proc tkerror args { exit } -proc keepalive {} { rb_out alive; after 120000 keepalive} -after 120000 keepalive' - - READ_TH = {} - def file_readable(port, cmd) - if cmd == nil - if READ_TH[port].has_key? - READ_TH[port].exit - READ_TH[port] = nil - end - else - READ_TH[port] = Thread.start{ - loop do - TkUtil.eval_cmd cmd - end - } - end - end - - WRITE_TH = {} - def file_writable(port, cmd) - if cmd == nil - if WRITE_TH[port].has_key? - WRITE_TH[port].exit - end - else - WRITE_TH[port] = Thread.start{ - loop do - TkUtil.eval_cmd cmd - end - } - end - end - module_function :file_readable, :file_writable - - def tk_tcl2ruby(val) - case val - when /^-?\d+$/ - val.to_i - when /^\./ - $tk_window_list[val] - when /^rb_out (c\d+)/ - $tk_cmdtbl[$1] - when / / - val.split.collect{|elt| - tk_tcl2ruby(elt) - } - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - - def tk_split_list(str) - idx = str.index('{') - return tk_tcl2ruby(str) if not idx - - list = tk_tcl2ruby(str[0,idx]) - str = str[idx+1..-1] - i = -1 - brace = 1 - str.each_byte {|c| - i += 1 - brace += 1 if c == ?{ - brace -= 1 if c == ?} - break if brace == 0 - } - if str[0, i] == ' ' - list.push ' ' - else - list.push tk_split_list(str[0, i]) - end - list += tk_split_list(str[i+1..-1]) - list - end - private :tk_tcl2ruby, :tk_split_list - - def dispatch(line) - if line =~ /^c\d+/ - cmd = $& - fail "no command `#{cmd}'" if not $tk_cmdtbl[cmd] - args = tk_split_list($') - TkUtil.eval_cmd $tk_cmdtbl[cmd], *args - elsif line =~ /^alive$/ - # keep alive, do nothing - else - fail "malformed line <#{line}>" - end - end - module_function :dispatch - - def error_at - frames = caller(1) - frames.delete_if do |c| - c =~ %r!/tk(|core|thcore|canvas|text|entry|scrollbox)\.rb:\d+! - end - frames - end - - def bool(val) - case bool - when "1", 1, 'yes', 'true' - TRUE - else - FALSE - end - end - def number(val) - case val - when /^-?\d+$/ - val.to_i - when /^-?\d+\.\d*$/ - val.to_f - else - val - end - end - def string(val) - if val == "{}" - '' - elsif val[0] == ?{ - val[1..-2] - else - val - end - end - def list(val) - tk_split_list(val) - end - def window(val) - $tk_window_list[val] - end - def procedure(val) - if val =~ /^rb_out (c\d+)/ - $tk_cmdtbl[$1] - else - nil - end - end - private :bool, :number, :string, :list, :window, :procedure - - def hash_kv(keys) - conf = [] - if keys - for k, v in keys - conf.push("-#{k}") - v = install_cmd(v) if v.kind_of? Proc - conf.push(v) - end - end - conf - end - private :tk_call, :error_at, :hash_kv - - $tk_cmdid = 0 - def install_cmd(cmd) - return '' if cmd == '' # uninstall cmd - id = format("c%.4d", $tk_cmdid) - $tk_cmdid += 1 - $tk_cmdtbl[id] = cmd - @cmdtbl = [] if not @cmdtbl - @cmdtbl.push id - return format('rb_out %s', id) - end - def uninstall_cmd(id) - $tk_cmdtbl[id] = nil - end - private :install_cmd, :uninstall_cmd - - $tk_window_list = {} - class Event - def initialize(seq,b,f,h,k,s,t,w,x,y,aa,ee,kk,nn,ww,tt,xx,yy) - @serial = seq - @num = b - @focus = (f == 1) - @height = h - @keycode = k - @state = s - @time = t - @width = w - @x = x - @y = y - @char = aa - @send_event = (ee == 1) - @keysym = kk - @keysym_num = nn - @type = tt - @widget = ww - @x_root = xx - @y_root = yy - end - attr :serial - attr :num - attr :focus - attr :height - attr :keycode - attr :state - attr :time - attr :width - attr :x - attr :y - attr :char - attr :send_event - attr :keysym - attr :keysym_num - attr :type - attr :widget - attr :x_root - attr :y_root - end - - def install_bind(cmd, args=nil) - if args - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, *arg - }) - id + " " + args - else - id = install_cmd(proc{|arg| - TkUtil.eval_cmd cmd, Event.new(*arg) - }) - id + " %# %b %f %h %k %s %t %w %x %y %A %E %K %N %W %T %X %Y" - end - end - - def _bind(path, context, cmd, args=nil) - begin - id = install_bind(cmd, args) - tk_call 'bind', path, "<#{context}>", id - rescue - $tk_cmdtbl[id] = nil - fail - end - end - private :install_bind, :_bind - - def bind_all(context, cmd=Proc.new, args=nil) - _bind 'all', context, cmd, args - end - - def pack(*args) - TkPack.configure *args - end - - $tk_cmdtbl = {} - - Qafter = Queue.new - def after(ms, cmd=Proc.new) - unless $tk_after_thread - $tk_after_thread = Thread.start{ - loop do - cmd = Qafter.pop - TkUtil.eval_cmd cmd - end - } - end - Thread.start do - sleep Float(ms)/1000 - Qafter.push cmd - end - end - - def update(idle=nil) - if idle - tk_call 'update', 'idletasks' - else - tk_call 'update' - end - end - - def root - $tk_root - end - - def bell - tk_call 'bell' - end - - def mainloop - begin - tk_call 'after', 'idle', 'wm deiconify .' - loop do - dispatch Qcmd.pop - end - ensure - Tk.tk_exit - end - end - module_function :after, :update, :dispatch, :mainloop, :root, :bell - - module Scrollable - def xscrollcommand(cmd=Proc.new) - configure_cmd 'xscrollcommand', cmd - end - def yscrollcommand(cmd=Proc.new) - configure_cmd 'yscrollcommand', cmd - end - end - - module Wm - def aspect(*args) - w = window(tk_call('wm', 'grid', path, *args)) - w.split.collect{|s|s.to_i} if args.length == 0 - end - def client(name=None) - tk_call 'wm', 'client', path, name - end - def colormapwindows(*args) - list(tk_call('wm', 'colormapwindows', path, *args)) - end - def wm_command(value=None) - string(tk_call('wm', 'command', path, value)) - end - def deiconify - tk_call 'wm', 'deiconify', path - end - def focusmodel(*args) - tk_call 'wm', 'focusmodel', path, *args - end - def frame - tk_call 'wm', 'frame', path - end - def geometry(*args) - list(tk_call('wm', 'geometry', path, *args)) - end - def grid(*args) - w = tk_call('wm', 'grid', path, *args) - list(w) if args.size == 0 - end - def group(*args) - tk_call 'wm', 'path', path, *args - end - def iconbitmap(*args) - tk_call 'wm', 'bitmap', path, *args - end - def iconify - tk_call 'wm', 'iconify' - end - def iconmask(*args) - tk_call 'wm', 'iconmask', path, *args - end - def iconname(*args) - tk_call 'wm', 'iconname', path, *args - end - def iconposition(*args) - w = tk_call('wm', 'iconposition', path, *args) - list(w) if args.size == 0 - end - def iconwindow(*args) - tk_call 'wm', 'iconwindow', path, *args - end - def maxsize(*args) - w = tk_call('wm', 'maxsize', path, *args) - list(w) if not args.size == 0 - end - def minsize(*args) - w = tk_call('wm', 'minsize', path, *args) - list(w) if args.size == 0 - end - def overrideredirect(bool=None) - if bool == None - bool(tk_call('wm', 'overrideredirect', path)) - else - tk_call 'wm', 'overrideredirect', path, bool - end - end - def positionfrom(*args) - tk_call 'wm', 'positionfrom', path, *args - end - def protocol(name, func=None) - func = install_cmd(func) if not func == None - tk_call 'wm', 'command', path, name, func - end - def resizable(*args) - w = tk_call('wm', 'resizable', path, *args) - if args.length == 0 - list(w).collect{|e| bool(e)} - end - end - def sizefrom(*args) - list(tk_call('wm', 'sizefrom', path, *args)) - end - def state - tk_call 'wm', 'state', path - end - def title(*args) - tk_call 'wm', 'title', path, *args - end - def transient(*args) - tk_call 'wm', 'transient', path, *args - end - def withdraw - tk_call 'wm', 'withdraw', path - end - end -end |