mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 05:25:34 +02:00

* tkafter.rb: Add self to 1st argument of interval- and loop-proc TkAfter#current_interval returns an interval (sleep) time value TkAfter#current_args returns an array of arguments TkAfter#return_value returns a return value of last loop-proc e.g. TkAfter.new( proc{|obj| 500 - obj.current_interval}, 10, [proc{|obj| p obj.current_args}, 'proc', 1], proc{|obj| p obj.current_args; ['return', 2]}, [proc{|obj| p obj.return_value p ['proc', obj.current_args[0].call(obj.return_value[1], obj.current_args[1])]}, proc{|*args| args[0] + args[1]}, 1], proc{p ['proc', 4]} ).start(100) * tk*.rb: Allow to use Symbols for parameters. Allow new notation of constructor (also allow old notation). e.g. TkFrame.new('classname'=>'User'){|base| pack f = TkFrame.new(base, :classname=>'ButtonFrame').pack TkButton.new( :parent => f, :text => 'Quit', :command => proc{exit} ).pack( :fill => :x, :pady => 2 ) } * tkcanvas.rb: (TkcItem) Add 'coords' parameter to the canvas item constructor (for new notation of constructor). e.g. c = TkCanvas.new.pack l = TkcLine.new(c, :coords=>[[0,0], [100,100]]) * tcltklib.c: New 'mainloop' and 'mainloop_watchdog'. The priority of their event-loop can be controlled. They accept an optional argument. If it false, they don't exit although the root widget is destroyed. This function is sometimes useful, if it is used with 'restart'. 'mainloop' can't treat Thread#join/value in a callback routine. (e.g. TkButton.new(:command=>proc{p Thread.new{button.invoke}.value}) ) 'mainloop_watchdog' can treat them, but watchdog thread is always running (so, a little heavier than 'mainloop'). If the purpose of using Thread#join/value is to do something under some safe-level, please use Proc object. (e.g. :command=>proc{$SAFE=1;proc{$SAFE=2;button.invoke}.call;p $SAFE}) * tk.rb: Support functions of new 'mainloop' and 'mainloop_watchdog'. * tk.rb: (Tk.restart) Add 'app-name' paramater and 'use' parameter. 'app-name' specifies the name and the resource class of the application. If 'app-name' is specified to 'xxx', the application class on the resource database is set to 'Xxx' and the application name is changed by the same rule of Tk.appname method. 'use' specifies the main window for embedding the root widget instead of generating a new window. * tk.rb: Add new parameter 'widgetname' to the widget constructor to support effective use of Resource Database. For example, the resource 'Xxx*quit.text: QUIT' can set the text of the button generated by the following code. e.g. Tk.restart('Xxx') TkButton.new(nil, 'widgetname'=>'quit', 'command'=>proc{exit}).pack Tk.mainloop * tk.rb: TkOption::get always returns a tainted string. Add TkOption::new_proc_class. It generates a class to import procedures defined on the resource database. For example, there is a following resource file. ----< resource-test >------------ *CMD.foo: {|*args| p [$SAFE, :foo, args]} *CMD.XXX.bar: {|*args| p [$SAFE, :bar, args]} *Button.command: ruby {p self; p $SAFE; TkOption::CMD::XXX.bar(1,2,3)} --------------------------------- The following code is a sample of use of the resource file. e.g. require 'tk' TkOption.readfile 'resource-test' p TkOption.new_proc_class(:CMD, [:foo], 1) p TkOption.new_proc_class(:XXX, [:bar], 2, false, TkOption::CMD) TkButton.new(:text=>'test').pack Tk.mainloop git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2515 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
312 lines
6.2 KiB
Ruby
312 lines
6.2 KiB
Ruby
#
|
|
# tkafter.rb : methods for Tcl/Tk after command
|
|
#
|
|
# $Id$
|
|
#
|
|
require 'tk'
|
|
|
|
class TkAfter
|
|
include TkCore
|
|
extend TkCore
|
|
|
|
Tk_CBID = [0]
|
|
Tk_CBTBL = {}
|
|
|
|
INTERP._invoke("proc", "rb_after", "id", "ruby [format \"TkAfter.callback %%Q!%s!\" $id]")
|
|
|
|
###############################
|
|
# class methods
|
|
###############################
|
|
def TkAfter.callback(obj_id)
|
|
@after_id = nil
|
|
ex_obj = Tk_CBTBL[obj_id]
|
|
return nil if ex_obj == nil; # canceled
|
|
_get_eval_string(ex_obj.do_callback)
|
|
end
|
|
|
|
def TkAfter.info
|
|
tk_call('after', 'info').split(' ').collect!{|id|
|
|
ret = Tk_CBTBL.find{|key,val| val.after_id == id}
|
|
(ret == nil)? id: ret[1]
|
|
}
|
|
end
|
|
|
|
###############################
|
|
# instance methods
|
|
###############################
|
|
def do_callback
|
|
@in_callback = true
|
|
begin
|
|
@return_value = @current_proc.call(self)
|
|
rescue StandardError, NameError
|
|
if @cancel_on_exception
|
|
cancel
|
|
return nil
|
|
else
|
|
fail $!
|
|
end
|
|
end
|
|
if @set_next
|
|
set_next_callback(@current_args)
|
|
else
|
|
@set_next = true
|
|
end
|
|
@in_callback = false
|
|
@return_value
|
|
end
|
|
|
|
def set_callback(sleep, args=nil)
|
|
@after_script = "rb_after #{@id}"
|
|
@after_id = tk_call('after', sleep, @after_script)
|
|
@current_args = args
|
|
@current_script = [sleep, @after_script]
|
|
end
|
|
|
|
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
|
|
if @do_loop < 0 || (@do_loop -= 1) > 0
|
|
@current_pos = 0
|
|
else
|
|
Tk_CBTBL[@id] = nil ;# for GC
|
|
@running = false
|
|
return
|
|
end
|
|
end
|
|
|
|
@current_args = args
|
|
|
|
if @sleep_time.kind_of? Proc
|
|
sleep = @sleep_time.call(self)
|
|
else
|
|
sleep = @sleep_time
|
|
end
|
|
@current_sleep = sleep
|
|
|
|
cmd, *cmd_args = @loop_proc[@current_pos]
|
|
@current_pos += 1
|
|
@current_proc = cmd
|
|
|
|
set_callback(sleep, cmd_args)
|
|
end
|
|
|
|
def initialize(*args)
|
|
@id = format("a%.4d", Tk_CBID[0])
|
|
Tk_CBID[0] += 1
|
|
|
|
@set_next = true
|
|
|
|
@init_sleep = 0
|
|
@init_proc = nil
|
|
@init_args = []
|
|
|
|
@current_script = []
|
|
@current_proc = nil
|
|
@current_args = nil
|
|
@return_value = nil
|
|
|
|
@sleep_time = 0
|
|
@current_sleep = 0
|
|
@loop_exec = 0
|
|
@do_loop = 0
|
|
@loop_proc = []
|
|
@proc_max = 0
|
|
@current_pos = 0
|
|
|
|
@after_id = nil
|
|
@after_script = nil
|
|
|
|
@cancel_on_exception = true
|
|
|
|
set_procs(*args) if args != []
|
|
|
|
@running = false
|
|
end
|
|
|
|
attr :after_id
|
|
attr :after_script
|
|
attr :current_proc
|
|
attr :current_args
|
|
attr :current_sleep
|
|
alias :current_interval :current_sleep
|
|
attr :return_value
|
|
|
|
attr_accessor :loop_exec
|
|
|
|
def get_procs
|
|
[@init_sleep, @init_proc, @init_args, @sleep_time, @loop_exec, @loop_proc]
|
|
end
|
|
|
|
def current_status
|
|
[@running, @current_sleep, @current_proc, @current_args,
|
|
@do_loop, @cancel_on_exception]
|
|
end
|
|
|
|
def cancel_on_exception?
|
|
@cancel_on_exception
|
|
end
|
|
|
|
def cancel_on_exception=(mode)
|
|
@cancel_on_exception = mode
|
|
end
|
|
|
|
def running?
|
|
@running
|
|
end
|
|
|
|
def loop_rest
|
|
@do_loop
|
|
end
|
|
|
|
def loop_rest=(rest)
|
|
@do_loop = rest
|
|
end
|
|
|
|
def set_procs(interval, loop_exec, *procs)
|
|
if !interval == 'idle' \
|
|
&& !interval.kind_of?(Integer) && !interval.kind_of?(Proc)
|
|
fail format("%s need to be Integer or Proc", interval.inspect)
|
|
end
|
|
@sleep_time = interval
|
|
|
|
@loop_proc = []
|
|
procs.each{|e|
|
|
if e.kind_of? Proc
|
|
@loop_proc.push([e])
|
|
else
|
|
@loop_proc.push(e)
|
|
end
|
|
}
|
|
@proc_max = @loop_proc.size
|
|
@current_pos = 0
|
|
|
|
@do_loop = 0
|
|
if loop_exec
|
|
if loop_exec.kind_of?(Integer) && loop_exec < 0
|
|
@loop_exec = -1
|
|
elsif loop_exec == nil || loop_exec == false || loop_exec == 0
|
|
@loop_exec = 1
|
|
else
|
|
if not loop_exec.kind_of?(Integer)
|
|
fail format("%s need to be Integer", loop_exec.inspect)
|
|
end
|
|
@loop_exec = loop_exec
|
|
end
|
|
@do_loop = @loop_exec
|
|
end
|
|
|
|
self
|
|
end
|
|
|
|
def add_procs(*procs)
|
|
procs.each{|e|
|
|
if e.kind_of? Proc
|
|
@loop_proc.push([e])
|
|
else
|
|
@loop_proc.push(e)
|
|
end
|
|
}
|
|
@proc_max = @loop_proc.size
|
|
|
|
self
|
|
end
|
|
|
|
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_sleep = sleep
|
|
@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
|
|
|
|
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
|
|
|
|
@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)
|
|
end
|
|
@current_proc = @init_proc
|
|
set_callback(sleep, @init_args)
|
|
@set_next = false if @in_callback
|
|
else
|
|
set_next_callback(@init_args)
|
|
end
|
|
|
|
self
|
|
end
|
|
|
|
def restart(*restart_args)
|
|
cancel if @running
|
|
if restart_args == []
|
|
start(@init_sleep, @init_proc, *@init_args)
|
|
else
|
|
start(*restart_args)
|
|
end
|
|
end
|
|
|
|
def cancel
|
|
@running = false
|
|
tk_call 'after', 'cancel', @after_id if @after_id
|
|
@after_id = nil
|
|
Tk_CBTBL[@id] = nil ;# for GC
|
|
self
|
|
end
|
|
alias stop cancel
|
|
|
|
def continue(wait=nil)
|
|
sleep, cmd = @current_script
|
|
return nil if cmd == nil || @running == true
|
|
if wait
|
|
if not wait.kind_of? Integer
|
|
fail format("%s need to be Integer", wait.inspect)
|
|
end
|
|
sleep = wait
|
|
end
|
|
Tk_CBTBL[@id] = self
|
|
@running = true
|
|
@after_id = tk_call('after', sleep, cmd)
|
|
self
|
|
end
|
|
|
|
def skip
|
|
return nil if @running == false
|
|
cancel
|
|
Tk_CBTBL[@id] = self
|
|
@running = true
|
|
set_next_callback(@current_args)
|
|
self
|
|
end
|
|
|
|
def info
|
|
if @after_id
|
|
inf = tk_split_list(tk_call('after', 'info', @after_id))
|
|
[Tk_CBTBL[inf[0][1]], inf[1]]
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
end
|