Class: TkTimer

Inherits:
Object show all
Extended by:
TkCore
Includes:
TkCore
Defined in:
lib/tk/timer.rb

Direct Known Subclasses

TkRTTimer

Constant Summary collapse

TkCommandNames =
['after'.freeze].freeze
Tk_CBTBL =
TkUtil.untrust({})
DEFAULT_IGNORE_EXCEPTIONS =
[ NameError, RuntimeError ].freeze

Constants included from TkCore

TkCore::EventFlag, TkCore::INTERP, TkCore::INTERP_MUTEX, TkCore::INTERP_ROOT_CHECK, TkCore::INTERP_THREAD, TkCore::INTERP_THREAD_STATUS, TkCore::RUN_EVENTLOOP_ON_MAIN_THREAD, TkCore::WIDGET_DESTROY_HOOK, TkCore::WITH_ENCODING, TkCore::WITH_RUBY_VM

Constants included from TkComm

TkComm::GET_CONFIGINFO_AS_ARRAY, TkComm::GET_CONFIGINFOwoRES_AS_ARRAY, TkComm::TkExtlibAutoloadModule, TkComm::Tk_CMDTBL, TkComm::Tk_IDs, TkComm::Tk_WINDOWS, TkComm::USE_TCLs_LIST_FUNCTIONS, TkComm::WidgetClassNames

Constants included from TkUtil

TkUtil::None, TkUtil::RELEASE_DATE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TkCore

_tk_call_to_list_core, after, after_cancel, after_idle, appname, appsend, appsend_deny, appsend_displayof, callback_break, callback_continue, callback_return, chooseColor, chooseDirectory, do_one_event, event_generate, getMultipleOpenFile, getMultipleSaveFile, getOpenFile, getSaveFile, get_eventloop_tick, get_eventloop_weight, get_no_event_wait, inactive, inactive_displayof, ip_eval, ip_eval_with_enc, ip_eval_without_enc, ip_invoke, ip_invoke_with_enc, ip_invoke_without_enc, is_mainloop?, load_cmd_on_ip, mainloop, mainloop_exist?, mainloop_thread?, mainloop_watchdog, messageBox, rb_appsend, rb_appsend_displayof, reset_inactive, reset_inactive_displayof, scaling, scaling_displayof, set_eventloop_tick, set_eventloop_weight, set_no_event_wait, tk_call, tk_call_to_list, tk_call_to_list_with_enc, tk_call_to_list_without_enc, tk_call_to_simplelist, tk_call_to_simplelist_with_enc, tk_call_to_simplelist_without_enc, tk_call_with_enc, tk_call_without_enc, windowingsystem

Methods included from TkComm

_at, _callback_entry?, _callback_entry_class?, _curr_cmd_id, _fromUTF8, _genobj_for_tkwidget, _next_cmd_id, _toUTF8, array2tk_list, #bind, #bind_all, #bind_append, #bind_append_all, #bind_remove, #bind_remove_all, #bindinfo, #bindinfo_all, bool, image_obj, #install_cmd, install_cmd, list, num_or_nil, num_or_str, number, procedure, simplelist, slice_ary, string, #subst, tk_tcl2ruby, uninstall_cmd, #uninstall_cmd, window

Methods included from TkEvent

#install_bind, #install_bind_for_event_class

Methods included from TkUtil

_conv_args, #_conv_args, #_fromUTF8, #_get_eval_enc_str, _get_eval_enc_str, #_get_eval_string, _get_eval_string, _symbolkey2str, #_symbolkey2str, #_toUTF8, bool, #bool, eval_cmd, #hash_kv, hash_kv, install_cmd, num_or_nil, #num_or_nil, num_or_str, #num_or_str, number, #number, string, #string, uninstall_cmd, untrust

Constructor Details

#initialize(*args, &b) ⇒ TkTimer

Returns a new instance of TkTimer.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/tk/timer.rb', line 148

def initialize(*args, &b)
  Tk_CBID.mutex.synchronize{
    # @id = Tk_CBID.join('')
    @id = Tk_CBID.join(TkCore::INTERP._ip_id_)
    Tk_CBID[1].succ!
  }

  @wait_var = TkVariable.new(0)

  @at_end_proc = nil

  @cb_cmd = TkCore::INTERP.get_cb_entry(self.method(:do_callback))

  @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 = DEFAULT_IGNORE_EXCEPTIONS
  # Unless @cancel_on_exception, Ruby/Tk shows an error dialog box when
  # an excepsion is raised on TkTimer callback procedure.
  # If @cancel_on_exception is an array of exception classes and the raised
  # exception is included in the array, Ruby/Tk cancels executing 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
  @in_callback = false
end

Instance Attribute Details

#after_idObject (readonly)

Returns the value of attribute after_id



208
209
210
# File 'lib/tk/timer.rb', line 208

def after_id
  @after_id
end

#after_scriptObject (readonly)

Returns the value of attribute after_script



209
210
211
# File 'lib/tk/timer.rb', line 209

def after_script
  @after_script
end

#current_argsObject (readonly)

Returns the value of attribute current_args



211
212
213
# File 'lib/tk/timer.rb', line 211

def current_args
  @current_args
end

#current_procObject (readonly)

Returns the value of attribute current_proc



210
211
212
# File 'lib/tk/timer.rb', line 210

def current_proc
  @current_proc
end

#current_sleepObject (readonly) Also known as: current_interval

Returns the value of attribute current_sleep



212
213
214
# File 'lib/tk/timer.rb', line 212

def current_sleep
  @current_sleep
end

#loop_execObject

Returns the value of attribute loop_exec



216
217
218
# File 'lib/tk/timer.rb', line 216

def loop_exec
  @loop_exec
end

#return_valueObject (readonly)

Returns the value of attribute return_value



214
215
216
# File 'lib/tk/timer.rb', line 214

def return_value
  @return_value
end

Class Method Details

.callback(obj_id) ⇒ Object



40
41
42
43
44
# File 'lib/tk/timer.rb', line 40

def self.callback(obj_id)
  ex_obj = Tk_CBTBL[obj_id]
  return "" if ex_obj == nil; # canceled
  ex_obj.cb_call
end

.info(obj = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tk/timer.rb', line 46

def self.info(obj = nil)
  if obj
    if obj.kind_of?(TkTimer)
      if obj.after_id
        inf = tk_split_list(tk_call_without_enc('after','info',obj.after_id))
        [Tk_CBTBL[inf[0][1]], inf[1]]
      else
        nil
      end
    else
      fail ArgumentError, "TkTimer object is expected"
    end
  else
    tk_call_without_enc('after', 'info').split(' ').collect!{|id|
      ret = Tk_CBTBL.find{|key,val| val.after_id == id}
      (ret == nil)? id: ret[1]
    }
  end
end

.start(*args, &b) ⇒ Object

class methods



36
37
38
# File 'lib/tk/timer.rb', line 36

def self.start(*args, &b)
  self.new(*args, &b).start
end

Instance Method Details

#add_procs(*procs) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/tk/timer.rb', line 313

def add_procs(*procs)
  procs.each{|e|
    # if e.kind_of?(Proc)
    if TkComm._callback_entry?(e)
      @loop_proc.push([e])
    else
      @loop_proc.push(e)
    end
  }
  @proc_max = @loop_proc.size

  self
end

#at_end(*arg, &b) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/tk/timer.rb', line 486

def at_end(*arg, &b)
  if arg.empty?
    if b
      @at_end_proc = b
    else
      # no proc
      return @at_end_proc
    end
  else
    fail ArgumentError, "wrong number of arguments" if arg.length != 1 || b
    @at_end_proc = arg[0]
  end
  self
end

#cancelObject Also known as: stop



439
440
441
442
443
444
445
446
447
448
# File 'lib/tk/timer.rb', line 439

def cancel
  @running = false
  # @wait_var.value = 0
  __at_end__
  tk_call 'after', 'cancel', @after_id if @after_id
  @after_id = nil

  Tk_CBTBL.delete(@id) ;# for GC
  self
end

#cancel_on_exception=(mode) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/tk/timer.rb', line 241

def cancel_on_exception=(mode)
  if mode.kind_of?(Array)
    @cancel_on_exception = mode
  elsif mode
    @cancel_on_exception = DEFAULT_IGNORE_EXCEPTIONS
  else
    @cancel_on_exception = false
  end
  #self
end

#cancel_on_exception?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/tk/timer.rb', line 237

def cancel_on_exception?
  @cancel_on_exception
end

#cb_callObject



224
225
226
# File 'lib/tk/timer.rb', line 224

def cb_call
  @cb_cmd.call
end

#continue(wait = nil) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/tk/timer.rb', line 451

def continue(wait=nil)
  fail RuntimeError, "is already running" if @running
  return restart() if @current_script.empty?
  sleep, cmd = @current_script
  fail RuntimeError, "no procedure to continue" unless cmd
  if wait
    unless wait.kind_of?(Integer)
      fail ArgumentError, "expect Integer for 1st argument"
    end
    sleep = wait
  end
  Tk_CBTBL[@id] = self
  @running = true
  @after_id = tk_call_without_enc('after', sleep, cmd)
  self
end

#current_statusObject



232
233
234
235
# File 'lib/tk/timer.rb', line 232

def current_status
  [@running, @current_sleep, @current_proc, @current_args,
    @do_loop, @cancel_on_exception]
end

#delete_at(n) ⇒ Object



343
344
345
346
347
348
# File 'lib/tk/timer.rb', line 343

def delete_at(n)
  @loop_proc.delete_at(n)
  @proc_max = @loop_proc.size
  cancel if @proc_max == 0
  self
end

#delete_procs(*procs) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/tk/timer.rb', line 327

def delete_procs(*procs)
  procs.each{|e|
    # if e.kind_of?(Proc)
    if TkComm._callback_entry?(e)
      @loop_proc.delete([e])
    else
      @loop_proc.delete(e)
    end
  }
  @proc_max = @loop_proc.size

  cancel if @proc_max == 0

  self
end

#do_callbackObject

instance methods



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tk/timer.rb', line 70

def do_callback
  @in_callback = true
  @after_id = nil
  begin
    @return_value = @current_proc.call(self)
  rescue SystemExit
    exit(0)
  rescue Interrupt
    exit!(1)
  rescue Exception => e
    if @cancel_on_exception &&
        @cancel_on_exception.find{|exc| e.kind_of?(exc)}
      cancel
      @return_value = e
      @in_callback = false
      return e
    else
      fail e
    end
  end
  if @set_next
    set_next_callback(@current_args)
  else
    @set_next = true
  end
  @in_callback = false
  @return_value
end

#eventloop_tkwaitObject



526
527
528
# File 'lib/tk/timer.rb', line 526

def eventloop_tkwait
  wait(false, true)
end

#eventloop_wait(check_root = false) ⇒ Object



517
518
519
# File 'lib/tk/timer.rb', line 517

def eventloop_wait(check_root = false)
  wait(false, check_root)
end

#get_procsObject



228
229
230
# File 'lib/tk/timer.rb', line 228

def get_procs
  [@init_sleep, @init_proc, @init_args, @sleep_time, @loop_exec, @loop_proc]
end

#infoObject



477
478
479
480
481
482
483
484
# File 'lib/tk/timer.rb', line 477

def info
  if @after_id
    inf = tk_split_list(tk_call_without_enc('after', 'info', @after_id))
    [Tk_CBTBL[inf[0][1]], inf[1]]
  else
    nil
  end
end

#loop_restObject



256
257
258
# File 'lib/tk/timer.rb', line 256

def loop_rest
  @do_loop
end

#loop_rest=(rest) ⇒ Object



260
261
262
263
# File 'lib/tk/timer.rb', line 260

def loop_rest=(rest)
  @do_loop = rest
  #self
end

#reset(*reset_args) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/tk/timer.rb', line 412

def reset(*reset_args)
  restart() if @running

  if @init_proc
    @return_value = @init_proc.call(self)
  else
    @return_value = nil
  end

  @current_pos   = 0
  @current_args  = @init_args
  @current_script = []

  @set_next = false if @in_callback

  self
end

#restart(*restart_args, &b) ⇒ Object



430
431
432
433
434
435
436
437
# File 'lib/tk/timer.rb', line 430

def restart(*restart_args, &b)
  cancel if @running
  if restart_args.empty? && !b
    start(@init_sleep, @init_proc, *@init_args)
  else
    start(*restart_args, &b)
  end
end

#running?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/tk/timer.rb', line 252

def running?
  @running
end

#set_callback(sleep, args = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tk/timer.rb', line 99

def set_callback(sleep, args=nil)
  if TkCore::INTERP.deleted?
    self.cancel
    return self
  end
  @after_script = "rb_after #{@id}"
  @current_args = args
  @current_script = [sleep, @after_script]
  @after_id = tk_call_without_enc('after', sleep, @after_script)
  self
end

#set_interval(interval) ⇒ Object



265
266
267
268
269
270
271
272
273
# File 'lib/tk/timer.rb', line 265

def set_interval(interval)
  #if interval != 'idle' && interval != :idle \
  #  && !interval.kind_of?(Integer) && !interval.kind_of?(Proc)
  if interval != 'idle' && interval != :idle \
    && !interval.kind_of?(Integer) && !TkComm._callback_entry?(interval)
    fail ArgumentError, "expect Integer or Proc"
  end
  @sleep_time = interval
end

#set_next_callback(args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tk/timer.rb', line 111

def set_next_callback(args)
  if @running == false || @proc_max == 0 || @do_loop == 0
    Tk_CBTBL.delete(@id) ;# for GC
    @running = false
    # @wait_var.value = 0
    __at_end__
    return
  end
  if @current_pos >= @proc_max
    if @do_loop < 0 || (@do_loop -= 1) > 0
      @current_pos = 0
    else
      Tk_CBTBL.delete(@id) ;# for GC
      @running = false
      # @wait_var.value = 0
      __at_end__
      return
    end
  end

  @current_args = args

  # if @sleep_time.kind_of?(Proc)
  if TkComm._callback_entry?(@sleep_time)
    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

#set_procs(interval, loop_exec, *procs) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/tk/timer.rb', line 275

def set_procs(interval, loop_exec, *procs)
  #if interval != 'idle' && interval != :idle \
  #   && !interval.kind_of?(Integer) && !interval.kind_of?(Proc)
  if interval != 'idle' && interval != :idle \
    && !interval.kind_of?(Integer) && !TkComm._callback_entry?(interval)
    fail ArgumentError, "expect Integer or Proc for 1st argument"
  end
  @sleep_time = interval

  @loop_proc = []
  procs.each{|e|
    # if e.kind_of?(Proc)
    if TkComm._callback_entry?(e)
      @loop_proc.push([e])
    else
      @loop_proc.push(e)
    end
  }
  @proc_max = @loop_proc.size
  @current_pos = 0

  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 ArgumentError, "expect Integer for 2nd argument"
    end
    @loop_exec = loop_exec
  end
  @do_loop = @loop_exec

  self
end

#set_start_proc(sleep = nil, init_proc = nil, *init_args, &b) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/tk/timer.rb', line 350

def set_start_proc(sleep=nil, init_proc=nil, *init_args, &b)
  # set parameters for 'restart'
  sleep = @init_sleep unless sleep

  if sleep != 'idle' && sleep != :idle && !sleep.kind_of?(Integer)
    fail ArgumentError, "expect Integer or 'idle' for 1st argument"
  end

  @init_sleep = sleep
  @init_proc = init_proc
  @init_args = init_args

  @init_proc = b if !@init_proc && b
  @init_proc = proc{|*args| } if @init_sleep > 0 && !@init_proc

  self
end

#skipObject



468
469
470
471
472
473
474
475
# File 'lib/tk/timer.rb', line 468

def skip
  fail RuntimeError, "is not running now" unless @running
  cancel
  Tk_CBTBL[@id] = self
  @running = true
  set_next_callback(@current_args)
  self
end

#start(*init_args, &b) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/tk/timer.rb', line 368

def start(*init_args, &b)
  return nil if @running

  Tk_CBTBL[@id] = self
  @do_loop = @loop_exec
  @current_pos = 0
  @return_value = nil
  @after_id = nil

  @init_sleep = 0
  @init_proc  = nil
  @init_args  = nil

  argc = init_args.size
  if argc > 0
    sleep = init_args.shift
    if sleep != 'idle' && sleep != :idle && !sleep.kind_of?(Integer)
      fail ArgumentError, "expect Integer or 'idle' for 1st argument"
    end
    @init_sleep = sleep
  end
  @init_proc = init_args.shift if argc > 1
  @init_args = init_args if argc > 2

  @init_proc = b if !@init_proc && b
  @init_proc = proc{|*args| } if @init_sleep > 0 && !@init_proc

  @current_sleep = @init_sleep
  @running = true
  if @init_proc
    # if not @init_proc.kind_of?(Proc)
    if !TkComm._callback_entry?(@init_proc)
      fail ArgumentError, "Argument '#{@init_proc}' need to be Proc"
    end
    @current_proc = @init_proc
    set_callback(@init_sleep, @init_args)
    @set_next = false if @in_callback
  else
    set_next_callback(@init_args)
  end

  self
end

#thread_tkwaitObject



529
530
531
# File 'lib/tk/timer.rb', line 529

def thread_tkwait
  wait(true, true)
end

#thread_wait(check_root = false) ⇒ Object



520
521
522
# File 'lib/tk/timer.rb', line 520

def thread_wait(check_root = false)
  wait(true, check_root)
end

#tkwait(on_thread = true) ⇒ Object



523
524
525
# File 'lib/tk/timer.rb', line 523

def tkwait(on_thread = true)
  wait(on_thread, true)
end

#wait(on_thread = true, check_root = false) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/tk/timer.rb', line 501

def wait(on_thread = true, check_root = false)
  unless @running
    if @return_value.kind_of?(Exception)
      fail @return_value
    else
      return @return_value
    end
  end

  @wait_var.wait(on_thread, check_root)
  if @return_value.kind_of?(Exception)
    fail @return_value
  else
    @return_value
  end
end