Skip to content

Commit

Permalink
* ext/tk/lib/tk/timer.rb (TkRTTimer): correct calculation of offset
Browse files Browse the repository at this point in the history
  value. get a little better accuracy.
* ext/tk/sample/demos-en/widget: use a binding with no local variables
  when eval a sample script.
* ext/tk/sample/demos-en/bind.rb: ditto.
* ext/tk/sample/demos-en/tcolor: ditto.
* ext/tk/sample/demos-jp/widget: ditto.
* ext/tk/sample/demos-jp/bind.rb: ditto.
* ext/tk/sample/demos-jp/tcolor: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8143 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nagai committed Mar 13, 2005
1 parent 28b1db0 commit cde3bdf
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 49 deletions.
18 changes: 18 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
Mon Mar 14 00:13:49 2005 Hidetoshi NAGAI <[email protected]>

* ext/tk/lib/tk/timer.rb (TkRTTimer): correct calculation of offset
value. get a little better accuracy.

* ext/tk/sample/demos-en/widget: use a binding with no local variables
when eval a sample script.

* ext/tk/sample/demos-en/bind.rb: ditto.

* ext/tk/sample/demos-en/tcolor: ditto.

* ext/tk/sample/demos-jp/widget: ditto.

* ext/tk/sample/demos-jp/bind.rb: ditto.

* ext/tk/sample/demos-jp/tcolor: ditto.

Sun Mar 13 22:19:17 2005 Nobuyoshi Nakada <[email protected]>

* eval.c (recursive_pop): raise TypeError instead of fatal error.
Expand Down
2 changes: 1 addition & 1 deletion ext/tk/lib/tk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4002,7 +4002,7 @@ def bindtags_unshift(tag)
#Tk.freeze

module Tk
RELEASE_DATE = '2005-03-10'.freeze
RELEASE_DATE = '2005-03-13'.freeze

autoload :AUTO_PATH, 'tk/variable'
autoload :TCL_PACKAGE_PATH, 'tk/variable'
Expand Down
56 changes: 42 additions & 14 deletions ext/tk/lib/tk/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ class TkRTTimer < TkTimer
def initialize(*args, &b)
super(*args, &b)

@offset_list = Array.new(DEFAULT_OFFSET_LIST_SIZE, 0.0)
@offset_list = Array.new(DEFAULT_OFFSET_LIST_SIZE){ [0, 0] }
@offset_s = 0
@offset_u = 0
@est_time = nil
end

Expand All @@ -533,10 +535,18 @@ def set_interval(interval)
end

def _offset_ave
size = @offset_list.size.to_f
s = 0.0
@offset_list.each{|n| s + n}
s / size
size = 0
d_sec = 0; d_usec = 0
@offset_list.each_with_index{|offset, idx|
# weight = 1
weight = idx + 1
size += weight
d_sec += offset[0] * weight
d_usec += offset[1] * weight
}
offset_s, mod = d_sec.divmod(size)
offset_u = ((mod * 1000000 + d_usec) / size.to_f).round
[offset_s, offset_u]
end
private :_offset_ave

Expand Down Expand Up @@ -564,33 +574,51 @@ def set_next_callback(args)
@current_pos += 1
@current_proc = cmd

@offset_s, @offset_u = _offset_ave

if TkComm._callback_entry?(@sleep_time)
sleep = @sleep_time.call(self)
else
sleep = @sleep_time
end

if @est_time
@est_time = Time.at(@est_time.to_f + sleep / 1000.0)
@est_time = Time.at(@est_time.to_i, @est_time.usec + sleep*1000)
else
@est_time = Time.at(@cb_start_time.to_f + sleep / 1000.0)
@est_time = Time.at(@cb_start_time.to_i,
@cb_start_time.usec + sleep*1000)
end

offset = _offset_ave

real_sleep = ((@est_time - Time.now)*1000.0 + offset).round
real_sleep = 0 if real_sleep < 0
now = Time.now
real_sleep = ((@est_time.to_i - now.to_i + @offset_s)*1000.0 +
(@est_time.usec - now.usec + @offset_u)/1000.0).round
if real_sleep <= 0
real_sleep = 0
@offset_s = now.to_i
@offset_u = now.usec
end
@current_sleep = real_sleep

set_callback(real_sleep, cmd_args)
end

def cb_call
@cb_start_time = Time.now

if @est_time
@offset_list.shift
@offset_list.push((@est_time - @cb_start_time) * 1000.0)

@cb_start_time = Time.now

if @current_sleep == 0
@offset_list.push([
@offset_s - @cb_start_time.to_i,
@offset_u - @cb_start_time.usec
])
else
@offset_list.push([
@offset_s + (@est_time.to_i - @cb_start_time.to_i),
@offset_u + (@est_time.usec - @cb_start_time.usec)
])
end
end

@cb_cmd.call
Expand Down
30 changes: 24 additions & 6 deletions ext/tk/sample/demos-en/bind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,35 @@ def tag_binding_for_bind_demo(tag, enter_style, leave_style)
tag_binding_for_bind_demo(tag, tagstyle_bold, tagstyle_normal)
}
d1.bind('1',
proc{eval `cat #{[$demo_dir,'items.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'items.rb'].join(File::Separator)}`,
_null_binding)
})
d2.bind('1',
proc{eval `cat #{[$demo_dir,'plot.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'plot.rb'].join(File::Separator)}`,
_null_binding)
})
d3.bind('1',
proc{eval `cat #{[$demo_dir,'ctext.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'ctext.rb'].join(File::Separator)}`,
_null_binding)
})
d4.bind('1',
proc{eval `cat #{[$demo_dir,'arrow.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'arrow.rb'].join(File::Separator)}`,
_null_binding)
})
d5.bind('1',
proc{eval `cat #{[$demo_dir,'ruler.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'ruler.rb'].join(File::Separator)}`,
_null_binding)
})
d6.bind('1',
proc{eval `cat #{[$demo_dir,'cscroll.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'cscroll.rb'].join(File::Separator)}`,
_null_binding)
})

TkTextMarkInsert.new(t, '0.0')
configure('state','disabled')
Expand Down
17 changes: 11 additions & 6 deletions ext/tk/sample/demos-en/tcolor
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,14 @@ def hsbToRgb(hue,sat,value)
end


def _null_binding
Module.new.instance_eval{binding}
end
private :_null_binding

def doUpdate
newCmd = $command.to_s.gsub("%%","\"#{$color}\"")
eval(newCmd)
eval(newCmd, _null_binding)
end


Expand Down Expand Up @@ -208,21 +213,21 @@ def tc_loadNamedColor(name)
else
case name.length
when 4
format = /#(.{1})(.{1})(.{1})/
fmt = /#(.{1})(.{1})(.{1})/
shift = 12
when 7
format = /#(.{2})(.{2})(.{2})/
fmt = /#(.{2})(.{2})(.{2})/
shift = 8
when 10
format = /#(.{3})(.{3})(.{3})/
fmt = /#(.{3})(.{3})(.{3})/
shift = 4
when 13
format = /#(.{4})(.{4})(.{4})/
fmt = /#(.{4})(.{4})(.{4})/
shift = 0
else
raise(eException,"syntax error in color name \"#{name}\"")
end
name.scan(format){|strlist|
name.scan(fmt){|strlist|
if strlist.length != 3
raise(eException,"syntax error in color name \"#{name}\"")
end
Expand Down
32 changes: 27 additions & 5 deletions ext/tk/sample/demos-en/widget
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,33 @@ else # ver >= 8.4
alias showVars showVars2
end

def _null_binding
# binding
Module.new.instance_eval{binding}
end
private :_null_binding

# invoke --
# This procedure is called when the user clicks on a demo description.
# It is responsible for invoking the demonstration.
#
# Arguments:
# txt - Name of text widget
# index - The index of the character that the user clicked on.
def invoke(txt, idx)
tag = txt.tag_names(idx).find{|t| t.kind_of?(String) && t =~ /^demo-/}
return unless tag

cursor = txt.cget('cursor')
txt.cursor('watch')
Tk.update
eval(IO.readlines("#{[$demo_dir, tag[5..-1]].join(File::Separator)}.rb").join, _null_binding)
Tk.update
txt.cursor(cursor)

$tag_visited.add("#{idx} linestart +1 chars", "#{idx} lineend +1 chars")
end
=begin
def invoke (txt, idx)
tag = txt.tag_names(idx).find{|t| t.kind_of?(String) && t =~ /^demo-/}
return unless tag
Expand All @@ -532,7 +551,7 @@ def invoke (txt, idx)
$tag_visited.add("#{idx} linestart +1 chars", "#{idx} lineend +1 chars")
end

=end
# showStatus --
#
# Show the name of the demo program in the status bar. This procedure
Expand Down Expand Up @@ -577,7 +596,7 @@ def showCode1(demo)
}.pack('side'=>'left', 'expand'=>'yes', 'pady'=>2)
TkButton.new(f) {
text "Rerun Demo"
command proc{eval($code_text.get('1.0','end'))}
command proc{eval($code_text.get('1.0','end'), _null_binding)}
}.pack('side'=>'left', 'expand'=>'yes', 'pady'=>2)
# f.pack('side'=>'bottom', 'expand'=>'yes', 'fill'=>'x')
f.pack('side'=>'bottom', 'fill'=>'x')
Expand Down Expand Up @@ -670,7 +689,9 @@ def showCode2(demo)
:command=>proc{printCode($code_text, file)},
:image=>$image['print'], :compound=>:left)
b_run = TkButton.new(bf, :text=>'Rerun Demo',
:command=>proc{eval($code_text.get('1.0','end'))},
:command=>proc{
eval($code_text.get('1.0','end'), _null_binding)
},
:image=>$image['refresh'], :compound=>:left)

TkGrid('x', b_run, b_prn, b_dis, :padx=>4, :pady=>[6,4])
Expand Down Expand Up @@ -791,7 +812,7 @@ end
#
def aboutBox
Tk.messageBox('icon'=>'info', 'type'=>'ok', 'title'=>'About Widget Demo',
'message'=>"Ruby/Tk widget demonstration Ver.1.5.3-en\n\n" +
'message'=>"Ruby/Tk widget demonstration Ver.1.5.4-en\n\n" +
"based on demos of Tk8.1 -- 8.5 " +
"( Copyright:: " +
"(c) 1996-1997 Sun Microsystems, Inc. / " +
Expand All @@ -812,7 +833,8 @@ ARGV.each{|cmd|
if cmd =~ /(.*).rb/
cmd = $1
end
eval IO.readlines("#{[$demo_dir, cmd].join(File::Separator)}.rb").join
eval(IO.readlines("#{[$demo_dir, cmd].join(File::Separator)}.rb").join,
_null_binding)
}
if no_launcher
$root.withdraw # hide root window
Expand Down
30 changes: 24 additions & 6 deletions ext/tk/sample/demos-jp/bind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,35 @@ def tag_binding_for_bind_demo(tag, enter_style, leave_style)
tag_binding_for_bind_demo(tag, tagstyle_bold, tagstyle_normal)
}
d1.bind('1',
proc{eval `cat #{[$demo_dir,'items.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'items.rb'].join(File::Separator)}`,
_null_binding)
})
d2.bind('1',
proc{eval `cat #{[$demo_dir,'plot.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'plot.rb'].join(File::Separator)}`,
_null_binding)
})
d3.bind('1',
proc{eval `cat #{[$demo_dir,'ctext.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'ctext.rb'].join(File::Separator)}`,
_null_binding)
})
d4.bind('1',
proc{eval `cat #{[$demo_dir,'arrow.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'arrow.rb'].join(File::Separator)}`,
_null_binding)
})
d5.bind('1',
proc{eval `cat #{[$demo_dir,'ruler.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'ruler.rb'].join(File::Separator)}`,
_null_binding)
})
d6.bind('1',
proc{eval `cat #{[$demo_dir,'cscroll.rb'].join(File::Separator)}`})
proc{
eval(`cat #{[$demo_dir,'cscroll.rb'].join(File::Separator)}`,
_null_binding)
})

TkTextMarkInsert.new(t, '0.0')
configure('state','disabled')
Expand Down
17 changes: 11 additions & 6 deletions ext/tk/sample/demos-jp/tcolor
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ def hsbToRgb(hue,sat,value)
end


def _null_binding
Module.new.instance_eval{binding}
end
private :_null_binding

def doUpdate
newCmd = $command.to_s.gsub("%%","\"#{$color}\"")
eval(newCmd)
eval(newCmd, _null_binding)
end


Expand Down Expand Up @@ -211,21 +216,21 @@ def tc_loadNamedColor(name)
else
case name.length
when 4
format = /#(.{1})(.{1})(.{1})/
fmt = /#(.{1})(.{1})(.{1})/
shift = 12
when 7
format = /#(.{2})(.{2})(.{2})/
fmt = /#(.{2})(.{2})(.{2})/
shift = 8
when 10
format = /#(.{3})(.{3})(.{3})/
fmt = /#(.{3})(.{3})(.{3})/
shift = 4
when 13
format = /#(.{4})(.{4})(.{4})/
fmt = /#(.{4})(.{4})(.{4})/
shift = 0
else
raise(eException,"syntax error in color name \"#{name}\"")
end
name.scan(format){|strlist|
name.scan(fmt){|strlist|
if strlist.length != 3
raise(eException,"syntax error in color name \"#{name}\"")
end
Expand Down
Loading

0 comments on commit cde3bdf

Please sign in to comment.