From bb4e34110bcd2b50a256877c3c26d1765baf74e6 Mon Sep 17 00:00:00 2001 From: nagai Date: Fri, 15 Oct 2004 09:06:52 +0000 Subject: * ext/tk/lib/tk/timer.rb: TkTimer.new(interval, loop){ ... } is acceptable. Add TkTimer.start ( == new + start ). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/tk/lib/tk/timer.rb | 51 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'ext/tk') diff --git a/ext/tk/lib/tk/timer.rb b/ext/tk/lib/tk/timer.rb index a6d7efa100..27c40e57e4 100644 --- a/ext/tk/lib/tk/timer.rb +++ b/ext/tk/lib/tk/timer.rb @@ -27,6 +27,10 @@ class TkTimer ############################### # class methods ############################### + def self.start(*args, &b) + self.new(*args, &b).start + end + def self.callback(obj_id) ex_obj = Tk_CBTBL[obj_id] return "" if ex_obj == nil; # canceled @@ -53,6 +57,7 @@ class TkTimer end end + ############################### # instance methods ############################### @@ -131,7 +136,7 @@ class TkTimer set_callback(sleep, cmd_args) end - def initialize(*args) + def initialize(*args, &b) # @id = Tk_CBID.join('') @id = Tk_CBID.join(TkCore::INTERP._ip_id_) Tk_CBID[1].succ! @@ -170,6 +175,17 @@ class TkTimer # callback procedures silently (TkTimer#cancel is called and no dialog is # shown). + if b + case args.size + when 0 + add_procs(b) + when 1 + args << -1 << b + else + args << b + end + end + set_procs(*args) if args != [] @running = false @@ -227,8 +243,16 @@ class TkTimer #self end + def set_interval(interval) + if interval != 'idle' \ + && !interval.kind_of?(Integer) && !interval.kind_of?(Proc) + fail ArguemntError, "expect Integer or Proc" + end + @sleep_time = interval + end + def set_procs(interval, loop_exec, *procs) - if !interval == 'idle' \ + if interval != 'idle' \ && !interval.kind_of?(Integer) && !interval.kind_of?(Proc) fail ArguemntError, "expect Integer or Proc for 1st argument" end @@ -245,20 +269,19 @@ class TkTimer @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 ArguemntError, "expect Integer for 2nd argument" - end - @loop_exec = loop_exec + if loop_exec.kind_of?(Integer) && loop_exec < 0 + @loop_exec = -1 + elsif loop_exec == true + @loop_exec = -1 + elsif loop_exec == nil || loop_exec == false || loop_exec == 0 + @loop_exec = 0 + else + if not loop_exec.kind_of?(Integer) + fail ArguemntError, "expect Integer for 2nd argument" end - @do_loop = @loop_exec + @loop_exec = loop_exec end + @do_loop = @loop_exec self end -- cgit v1.2.3