From 4c3328f5476f54c84b1f048eed075e6edf5e5b1d Mon Sep 17 00:00:00 2001 From: nagai Date: Sat, 2 Aug 2003 05:04:30 +0000 Subject: * (bug fix) TkEntry#delete * (bug fix) some widget demos * support == ( "coerce TkVariable" add to the TODO list :-) ) * freeze some object for security reason git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4282 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/tk/sample/demos-en/rmt | 23 ++++++------ ext/tk/sample/demos-en/rolodex | 8 ++--- ext/tk/sample/demos-en/rolodex-j | 9 +++-- ext/tk/sample/demos-en/square | 11 ++++-- ext/tk/sample/demos-en/tcolor | 18 ++++++---- ext/tk/sample/demos-en/timer | 20 +++++++++-- ext/tk/sample/demos-jp/browse1 | 2 +- ext/tk/sample/demos-jp/browse2 | 2 +- ext/tk/sample/demos-jp/hello | 2 +- ext/tk/sample/demos-jp/ixset | 4 +-- ext/tk/sample/demos-jp/rmt | 25 +++++++------ ext/tk/sample/demos-jp/rolodex | 10 +++--- ext/tk/sample/demos-jp/rolodex-j | 78 +++++++++++++++++----------------------- ext/tk/sample/demos-jp/square | 13 +++++-- ext/tk/sample/demos-jp/tcolor | 20 +++++++---- ext/tk/sample/demos-jp/timer | 22 ++++++++++-- ext/tk/sample/demos-jp/widget | 2 +- ext/tk/sample/safe-tk.rb | 25 +++++++++++++ ext/tk/sample/tkbiff.rb | 2 +- ext/tk/sample/tkbrowse.rb | 2 +- ext/tk/sample/tkdialog.rb | 2 +- ext/tk/sample/tkfrom.rb | 2 +- ext/tk/sample/tktimer.rb | 2 +- ext/tk/sample/tktimer3.rb | 59 ++++++++++++++++++++++++++++++ 24 files changed, 249 insertions(+), 114 deletions(-) create mode 100644 ext/tk/sample/tktimer3.rb (limited to 'ext/tk/sample') diff --git a/ext/tk/sample/demos-en/rmt b/ext/tk/sample/demos-en/rmt index d4b1d93760..73f631180a 100644 --- a/ext/tk/sample/demos-en/rmt +++ b/ext/tk/sample/demos-en/rmt @@ -53,10 +53,8 @@ class Rmt # Create text window and scrollbar. - @txt = TkText.new(root, 'relief'=>'sunken', 'bd'=>2, 'setgrid'=>true) {|t| - TkScrollbar.new(root, 'command'=>proc{|*args| t.yview *args}) { - pack('side'=>'right', 'fill'=>'both') - } + @txt = TkText.new(root, 'relief'=>'sunken', 'bd'=>2, 'setgrid'=>true) { + yscrollbar(TkScrollbar.new(root){pack('side'=>'right', 'fill'=>'y')}) pack('side'=>'left') } @@ -247,13 +245,18 @@ class Rmt rescue end TkWinfo.interps.sort.each{|ip| - if Tk.appsend(ip, false, 'info commands ruby') == "" - mode = 'Tcl' - else - mode = 'Ruby' + begin + if Tk.appsend(ip, false, 'info commands ruby') == "" + mode = 'Tcl' + else + mode = 'Ruby' + end + menu.add('command', 'label'=>format("%s (#{mode}/Tk)", ip), + 'command'=>proc{win.newApp ip, mode}) + rescue + menu.add('command', 'label'=>format("%s (unknown Tk)", ip), + 'command'=>proc{win.newApp ip, mode}, 'state'=>'disabled') end - menu.add('command', 'label'=>format("%s (#{mode}/Tk)", ip), - 'command'=>proc{win.newApp ip, mode}) } menu.add('command', 'label'=>format("local (Ruby/Tk)"), 'command'=>proc{win.newApp 'local', 'Ruby'}) diff --git a/ext/tk/sample/demos-en/rolodex b/ext/tk/sample/demos-en/rolodex index 84254d93bf..9a4030e902 100644 --- a/ext/tk/sample/demos-en/rolodex +++ b/ext/tk/sample/demos-en/rolodex @@ -4,7 +4,7 @@ # This script is a part of Tom LaStrange's rolodex # # Copyright (C) 1998 by Takaaki Tateishi -# Time-stamp: "98/07/20 12:00:02 ttate" +# Time-stamp: "03/08/02 06:23:06 nagai" # require "tk" @@ -176,8 +176,8 @@ end class Rolodex < TkRoot attr_reader :frame, :buttons, :menu - def initialize - super + def initialize(*args) + super(*args) @frame = RolodexFrame.new(self) @frame.pack("side"=>"top", "fill"=>"y", @@ -314,7 +314,7 @@ Ctrl+S: Search (dummy operation) EOF $helpTopics["version"] = < -# Time-stamp: "98/07/20 12:00:36 ttate" +# Time-stamp: "03/08/02 14:02:04 nagai" # require "tk" -require 'tkencoding' Tk.encoding = "euc-jp" $font = TkFont.new('k14') @@ -198,8 +197,8 @@ end class Rolodex < TkRoot attr_reader :frame, :buttons, :menu - def initialize - super + def initialize(*args) + super(*args) @frame = RolodexFrame.new(self) @frame.pack("side"=>"top", "fill"=>"y", @@ -318,7 +317,7 @@ Ctrl+S: EOF $helpTopics["バージョン情報"] = < proc{changeColorSpace(:hsb)} add "separator" add "radio", - "label" => "Qutomatic updates", + "label" => "Automatic updates", "variable" => $autoUpdate, "value" => "1", "underline" => "0" @@ -367,7 +373,7 @@ class TkColorMiddleLeftFrame"sunken", "borderwidth"=>"2", "exportselection"=>"false") - @scroll.command(proc{|*args| @names.yview *args}) + @scroll.command(proc{|*args| @names.yview(*args)}) @names.bind("Double-1",proc{ tc_loadNamedColor(@names.get(@names.curselection))}) @names.pack("side"=>"left") @@ -490,8 +496,8 @@ end class TkColor"top", "fill"=>"x") diff --git a/ext/tk/sample/demos-en/timer b/ext/tk/sample/demos-en/timer index 896568c0bc..58a41d00c0 100644 --- a/ext/tk/sample/demos-en/timer +++ b/ext/tk/sample/demos-en/timer @@ -80,12 +80,28 @@ end class ButtonFrame < TkFrame def initialize(parent=nil,keys=nil) super(parent,keys) +=begin @stop = TkButton.new(self, 'text'=>'Stop', 'command'=>proc{timer_stop}) @start = TkButton.new(self, 'text'=>'Start', 'command'=>proc{timer_start}) +=end + @stop = TkButton.new(self, :text=>'Stop', :state=>:disabled) + @start = TkButton.new(self, :text=>'Start', :state=>:normal) + + @stop.command proc{ + timer_stop + @start.state(:normal) + @stop.state(:disabled) + } + @start.command proc{ + timer_start + @stop.state(:normal) + @start.state(:disabled) + } + @reset = TkButton.new(self, 'text'=>'Reset', 'command'=>proc{timer_reset}) @@ -99,8 +115,8 @@ end class Timer < TkRoot attr_reader :countframe - def initialize - super + def initialize(*args) + super(*args) @countframe = CountFrame.new(self) @buttonframe = ButtonFrame.new(self) for f in [@buttonframe,@countframe] diff --git a/ext/tk/sample/demos-jp/browse1 b/ext/tk/sample/demos-jp/browse1 index 6bb8fb35ca..03e251035a 100644 --- a/ext/tk/sample/demos-jp/browse1 +++ b/ext/tk/sample/demos-jp/browse1 @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # browse -- # This script generates a directory browser, which lists the working diff --git a/ext/tk/sample/demos-jp/browse2 b/ext/tk/sample/demos-jp/browse2 index 755afa682c..304a5f547e 100644 --- a/ext/tk/sample/demos-jp/browse2 +++ b/ext/tk/sample/demos-jp/browse2 @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # browse -- # This script generates a directory browser, which lists the working diff --git a/ext/tk/sample/demos-jp/hello b/ext/tk/sample/demos-jp/hello index 94e450899c..859ebd950e 100644 --- a/ext/tk/sample/demos-jp/hello +++ b/ext/tk/sample/demos-jp/hello @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby require 'tk' TkButton.new(nil, diff --git a/ext/tk/sample/demos-jp/ixset b/ext/tk/sample/demos-jp/ixset index 5391012cfb..46c2378073 100644 --- a/ext/tk/sample/demos-jp/ixset +++ b/ext/tk/sample/demos-jp/ixset @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # ixset -- # A nice interface to "xset" to change X server settings @@ -166,7 +166,7 @@ class Xsettings l = TkLabel.new(bell, 'text'=>'Bell Settings') @w_bellvol = TkScale.new(bell, 'from'=>0, 'to'=>100, 'length'=>200, 'tickinterval'=>20, 'orient'=>'horizontal', - 'label'=>"Volume (%)") + 'label'=>"Volume (%)") f = TkFrame.new(bell) @w_bellpit = LabelEntry.new(f, "Pitch (Hz)", 6) diff --git a/ext/tk/sample/demos-jp/rmt b/ext/tk/sample/demos-jp/rmt index 64583d714e..73f631180a 100644 --- a/ext/tk/sample/demos-jp/rmt +++ b/ext/tk/sample/demos-jp/rmt @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # rmt -- # This script implements a simple remote-control mechanism for @@ -53,10 +53,8 @@ class Rmt # Create text window and scrollbar. - @txt = TkText.new(root, 'relief'=>'sunken', 'bd'=>2, 'setgrid'=>true) {|t| - TkScrollbar.new(root, 'command'=>proc{|*args| t.yview *args}) { - pack('side'=>'right', 'fill'=>'both') - } + @txt = TkText.new(root, 'relief'=>'sunken', 'bd'=>2, 'setgrid'=>true) { + yscrollbar(TkScrollbar.new(root){pack('side'=>'right', 'fill'=>'y')}) pack('side'=>'left') } @@ -247,13 +245,18 @@ class Rmt rescue end TkWinfo.interps.sort.each{|ip| - if Tk.appsend(ip, false, 'info commands ruby') == "" - mode = 'Tcl' - else - mode = 'Ruby' + begin + if Tk.appsend(ip, false, 'info commands ruby') == "" + mode = 'Tcl' + else + mode = 'Ruby' + end + menu.add('command', 'label'=>format("%s (#{mode}/Tk)", ip), + 'command'=>proc{win.newApp ip, mode}) + rescue + menu.add('command', 'label'=>format("%s (unknown Tk)", ip), + 'command'=>proc{win.newApp ip, mode}, 'state'=>'disabled') end - menu.add('command', 'label'=>format("%s (#{mode}/Tk)", ip), - 'command'=>proc{win.newApp ip, mode}) } menu.add('command', 'label'=>format("local (Ruby/Tk)"), 'command'=>proc{win.newApp 'local', 'Ruby'}) diff --git a/ext/tk/sample/demos-jp/rolodex b/ext/tk/sample/demos-jp/rolodex index 9ed1f6d7d1..9f87ae2829 100644 --- a/ext/tk/sample/demos-jp/rolodex +++ b/ext/tk/sample/demos-jp/rolodex @@ -1,10 +1,10 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # # rolodex -- # This script is a part of Tom LaStrange's rolodex # # Copyright (C) 1998 by Takaaki Tateishi -# Time-stamp: "98/07/20 12:00:02 ttate" +# Time-stamp: "03/08/02 12:45:21 nagai" # require "tk" @@ -176,8 +176,8 @@ end class Rolodex < TkRoot attr_reader :frame, :buttons, :menu - def initialize - super + def initialize(*args) + super(*args) @frame = RolodexFrame.new(self) @frame.pack("side"=>"top", "fill"=>"y", @@ -314,7 +314,7 @@ Ctrl+S: Search (dummy operation) EOF $helpTopics["version"] = < -# Time-stamp: "98/07/20 12:00:36 ttate" +# Time-stamp: "03/08/02 14:01:54 nagai" # require "tk" - -# UTF8への変換を行なう。 - -$encoding = "euc-jp" -def j(str) - Tk.toUTF8(str,$encoding) -end - +Tk.encoding = "euc-jp" def show_help(topic,x=0,y=0) if( topic.is_a?(TkWindow) ) @@ -33,17 +26,17 @@ def show_help(topic,x=0,y=0) msg = "このトピックについてのヘルプはまだ使用できません" end TkDialog.new("title"=>"Rolodex Help", - "message"=>j("「#{topic}」\n\n#{msg}"), + "message"=>"「#{topic}」\n\n#{msg}", "default_button"=>0, "buttons"=>["OK"]) end def fillCard clearAction - $root.frame.entry[1].insert(0,j("立石 孝彰")) - $root.frame.entry[2].insert(0,j("923-1292 石川県")) - $root.frame.entry[3].insert(0,j("辰口町 旭台 1-1")) - $root.frame.entry[4].insert(0,j("北陸先端科学技術大学院大学")) + $root.frame.entry[1].insert(0, "立石 孝彰") + $root.frame.entry[2].insert(0, "923-1292 石川県") + $root.frame.entry[3].insert(0, "辰口町 旭台 1-1") + $root.frame.entry[4].insert(0, "北陸先端科学技術大学院大学") $root.frame.entry[5].insert(0,"private") $root.frame.entry[6].insert(0,"***-***-****") $root.frame.entry[7].insert(0,"***-***-****") @@ -53,7 +46,7 @@ def addAction for i in 1..7 STDERR.print format("%-12s %s\n", RolodexFrame::LABEL[i], - Tk.fromUTF8($root.frame.entry[i].value,$encoding)) + $root.frame.entry[i].value) end end @@ -65,7 +58,7 @@ end def fileAction TkDialog.new("title"=>"File Selection", - "message"=>j("これはファイル選択ダイアログのダミーです。\n"), + "message"=>"これはファイル選択ダイアログのダミーです。\n", "default_button"=>0, "buttons"=>["OK"]) STDERR.print "dummy file name\n" @@ -73,9 +66,9 @@ end def deleteAction result = TkDialog.new("title"=>"Confirm Action", - "message"=>j("よろしいですか?"), + "message"=>"よろしいですか?", "default_button"=>0, - "buttons"=>[j("キャンセル")]) + "buttons"=>["キャンセル"]) if( result.value == 0 ) clearAction end @@ -90,7 +83,6 @@ class RolodexFrame < TkFrame def initialize(parent=nil,keys=nil) super(parent,keys) self["relief"] = "flat" - @i = [] @label = [] @entry = [] @@ -100,7 +92,7 @@ class RolodexFrame < TkFrame "pady"=>2, "anchor"=>"e") @label[i] = TkLabel.new(@i[i], - "text"=>j(LABEL[i]), + "text"=>LABEL[i], "anchor"=>"e") @entry[i] = TkEntry.new(@i[i], "width"=>30, @@ -116,14 +108,10 @@ class RolodexButtons < TkFrame def initialize(parent,keys=nil) super(parent,keys) - @clear = TkButton.new(self, - "text" => j("クリアー")) - @add = TkButton.new(self, - "text" => j("追加")) - @search = TkButton.new(self, - "text" => j("検索")) - @delete = TkButton.new(self, - "text" => j("消去")) + @clear = TkButton.new(self,"text" => "クリアー") + @add = TkButton.new(self, "text" => "追加") + @search = TkButton.new(self, "text" => "検索") + @delete = TkButton.new(self, "text" => "消去") for w in [@clear,@add,@search,@delete] w.pack("side"=>"left", "padx"=>2) end @@ -139,42 +127,42 @@ class RolodexMenuFrame < TkFrame "borderwidth"=>1) @file = TkMenubutton.new(self, - "text"=>j("ファイル"), + "text"=> "ファイル", "underline"=>0) @file_menu = TkMenu.new(@file) @file_menu.add("command", - "label" => j("読み込み ..."), + "label" => "読み込み ...", "command" => proc{fileAction}, "underline" => 0) @file_menu.add("command", - "label" => j("終了"), + "label" => "終了", "command" => proc{$root.destroy}, "underline" => 0) @file.menu(@file_menu) @file.pack("side"=>"left") @help = TkMenubutton.new(self, - "text"=>j("ヘルプ"), + "text"=> "ヘルプ", "underline"=>0) @help_menu = TkMenu.new(@help) @help_menu.add("command", - "label"=>j("コンテキストについて"), + "label"=> "コンテキストについて", "command"=>proc{show_help("コンテキスト")}, "underline"=>3) @help_menu.add("command", - "label"=>j("ヘルプについて"), + "label"=> "ヘルプについて", "command"=>proc{show_help("ヘルプ")}, "underline"=>3) @help_menu.add("command", - "label"=>j("ウィンドウについて"), + "label"=> "ウィンドウについて", "command"=>proc{show_help("ウィンドウ")}, "underline"=>3) @help_menu.add("command", - "label"=>j("キー操作について"), + "label"=> "キー操作について", "command"=>proc{show_help("キー操作")}, "underline"=>3) @help_menu.add("command", - "label"=>j("バージョン情報"), + "label"=> "バージョン情報", "command"=>proc{show_help("バージョン情報")}, "underline"=>3) @help.menu(@help_menu) @@ -185,8 +173,8 @@ end class Rolodex < TkRoot attr_reader :frame, :buttons, :menu - def initialize - super + def initialize(*args) + super(*args) @frame = RolodexFrame.new(self) @frame.pack("side"=>"top", "fill"=>"y", @@ -209,16 +197,16 @@ $root.buttons.add.configure("command"=>proc{addAction}) $root.buttons.clear.configure("command"=>proc{clearAction}) $root.buttons.search.configure("command"=>proc{addAction; fillCard}) -$root.buttons.clear.configure("text"=>j("クリアー Ctrl+C")) +$root.buttons.clear.configure("text"=> "クリアー Ctrl+C") $root.bind("Control-c",proc{clearAction}) -$root.buttons.add.configure("text"=>j("追加 Ctrl+A")) +$root.buttons.add.configure("text"=> "追加 Ctrl+A") $root.bind("Control-a",proc{addAction}) -$root.buttons.search.configure("text"=>j("検索 Ctrl+S")) +$root.buttons.search.configure("text"=> "検索 Ctrl+S") $root.bind("Control-s",proc{addAction; fillCard}) -$root.buttons.delete.configure("text"=>j("消去 Ctrl+D")) +$root.buttons.delete.configure("text"=> "消去 Ctrl+D") $root.bind("Control-d",proc{deleteAction}) $root.menu.file_menu.entryconfigure(1, "accel"=>"Ctrl+F") @@ -305,7 +293,7 @@ Ctrl+S: EOF $helpTopics["バージョン情報"] = < proc{changeColorSpace(:hsb)} add "separator" add "radio", - "label" => "Qutomatic updates", + "label" => "Automatic updates", "variable" => $autoUpdate, "value" => "1", "underline" => "0" @@ -367,7 +373,7 @@ class TkColorMiddleLeftFrame"sunken", "borderwidth"=>"2", "exportselection"=>"false") - @scroll.command(proc{|*args| @names.yview *args}) + @scroll.command(proc{|*args| @names.yview(*args)}) @names.bind("Double-1",proc{ tc_loadNamedColor(@names.get(@names.curselection))}) @names.pack("side"=>"left") @@ -490,8 +496,8 @@ end class TkColor"top", "fill"=>"x") diff --git a/ext/tk/sample/demos-jp/timer b/ext/tk/sample/demos-jp/timer index 232f4ae5ca..58a41d00c0 100644 --- a/ext/tk/sample/demos-jp/timer +++ b/ext/tk/sample/demos-jp/timer @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # # timer -- # This script generates a counter with start,stop and reset buttons. @@ -80,12 +80,28 @@ end class ButtonFrame < TkFrame def initialize(parent=nil,keys=nil) super(parent,keys) +=begin @stop = TkButton.new(self, 'text'=>'Stop', 'command'=>proc{timer_stop}) @start = TkButton.new(self, 'text'=>'Start', 'command'=>proc{timer_start}) +=end + @stop = TkButton.new(self, :text=>'Stop', :state=>:disabled) + @start = TkButton.new(self, :text=>'Start', :state=>:normal) + + @stop.command proc{ + timer_stop + @start.state(:normal) + @stop.state(:disabled) + } + @start.command proc{ + timer_start + @stop.state(:normal) + @start.state(:disabled) + } + @reset = TkButton.new(self, 'text'=>'Reset', 'command'=>proc{timer_reset}) @@ -99,8 +115,8 @@ end class Timer < TkRoot attr_reader :countframe - def initialize - super + def initialize(*args) + super(*args) @countframe = CountFrame.new(self) @buttonframe = ButtonFrame.new(self) for f in [@buttonframe,@countframe] diff --git a/ext/tk/sample/demos-jp/widget b/ext/tk/sample/demos-jp/widget index 3d4690d57a..5eb5d5f074 100644 --- a/ext/tk/sample/demos-jp/widget +++ b/ext/tk/sample/demos-jp/widget @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # tk 関係ライブラリの読み込み require 'tk' diff --git a/ext/tk/sample/safe-tk.rb b/ext/tk/sample/safe-tk.rb index 6b6de8f848..8be13a32d0 100644 --- a/ext/tk/sample/safe-tk.rb +++ b/ext/tk/sample/safe-tk.rb @@ -32,6 +32,7 @@ cmd = Proc.new{|txt| timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 }) +=begin TkButton.new(:text=>'Start') { command proc{ timer.continue unless timer.running? } pack(:side=>:left, :fill=>:both, :expand=>true) @@ -44,6 +45,30 @@ cmd = Proc.new{|txt| command proc{ timer.stop if timer.running? } pack('side'=>'right','fill'=>'both','expand'=>'yes') } +=end + b_start = TkButton.new(:text=>'Start', :state=>:disabled) { + pack(:side=>:left, :fill=>:both, :expand=>true) + } + b_stop = TkButton.new(:text=>'Stop', :state=>:normal) { + pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes') + } + + b_start.command { + timer.continue + b_stop.state(:normal) + b_start.state(:disabled) + } + + b_stop.command { + timer.stop + b_start.state(:normal) + b_stop.state(:disabled) + } + + TkButton.new(:text=>'Reset', :state=>:normal) { + command { timer.reset } + pack('side'=>'right', 'fill'=>'both', 'expand'=>'yes') + } ev_quit = TkVirtualEvent.new('Control-c', 'Control-q') Tk.root.bind(ev_quit, proc{Tk.exit}).focus diff --git a/ext/tk/sample/tkbiff.rb b/ext/tk/sample/tkbiff.rb index 5fe367fd97..e89644bd9d 100644 --- a/ext/tk/sample/tkbiff.rb +++ b/ext/tk/sample/tkbiff.rb @@ -1,4 +1,4 @@ -#! /usr/local/bin/ruby +#!/usr/bin/env ruby if ARGV[0] != '-d' unless $DEBUG diff --git a/ext/tk/sample/tkbrowse.rb b/ext/tk/sample/tkbrowse.rb index 882f0a489b..7395688e97 100644 --- a/ext/tk/sample/tkbrowse.rb +++ b/ext/tk/sample/tkbrowse.rb @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # # This script generates a directory browser, which lists the working # directory and allows you to open files or subdirectories by diff --git a/ext/tk/sample/tkdialog.rb b/ext/tk/sample/tkdialog.rb index 73708945fb..00cc12ab11 100644 --- a/ext/tk/sample/tkdialog.rb +++ b/ext/tk/sample/tkdialog.rb @@ -1,4 +1,4 @@ -#! /usr/local/bin/ruby +#!/usr/bin/env ruby require "tk" root = TkFrame.new diff --git a/ext/tk/sample/tkfrom.rb b/ext/tk/sample/tkfrom.rb index e0e7f4810f..25f1d7c429 100644 --- a/ext/tk/sample/tkfrom.rb +++ b/ext/tk/sample/tkfrom.rb @@ -1,4 +1,4 @@ -#! /usr/local/bin/ruby +#!/usr/bin/env ruby require "parsedate" require "base64" diff --git a/ext/tk/sample/tktimer.rb b/ext/tk/sample/tktimer.rb index 34377e2f39..b1adb5b506 100644 --- a/ext/tk/sample/tktimer.rb +++ b/ext/tk/sample/tktimer.rb @@ -1,4 +1,4 @@ -#!/usr/local/bin/ruby +#!/usr/bin/env ruby # This script generates a counter with start and stop buttons. require "tk" diff --git a/ext/tk/sample/tktimer3.rb b/ext/tk/sample/tktimer3.rb new file mode 100644 index 0000000000..e3bb4c3e4a --- /dev/null +++ b/ext/tk/sample/tktimer3.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# This script is a re-implementation of tktimer.rb with TkTimer(TkAfter) class. + +require "tk" + +# new notation : +# * symbols are acceptable as keys or values of the option hash +# * the parent widget can be given by :parent key on the option hash +root = TkRoot.new(:title=>'timer sample') +label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \ + .pack(:side=>:bottom, :fill=>:both) + +# define the procedure repeated by the TkTimer object +tick = proc{|aobj| #<== TkTimer object + cnt = aobj.return_value + 5 # return_value keeps a result of the last proc + label.text format("%d.%02d", *(cnt.divmod(100))) + cnt #==> return value is kept by TkTimer object + # (so, can be send to the next repeat-proc) +} + +timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 }) + # ==> repeat-interval : (about) 50 ms, + # repeat : infinite (-1) times, + # repeat-procedure : tick (only one, in this case) + # + # ==> wait-before-call-init-proc : 0 ms, + # init_proc : proc{ label.text('0.00'); 0 } + # + # (0ms)-> init_proc ->(50ms)-> tick ->(50ms)-> tick ->.... + +b_start = TkButton.new(:text=>'Start', :state=>:disabled) { + pack(:side=>:left, :fill=>:both, :expand=>true) +} + +b_stop = TkButton.new(:text=>'Stop', :state=>:normal) { + pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes') +} + +b_start.command { + timer.continue + b_stop.state(:normal) + b_start.state(:disabled) +} + +b_stop.command { + timer.stop + b_start.state(:normal) + b_stop.state(:disabled) +} + +TkButton.new(:text=>'Reset', :state=>:normal) { + command { timer.reset } + pack(:side=>:right, :fill=>:both, :expand=>:yes) +} + +ev_quit = TkVirtualEvent.new('Control-c', 'Control-q') +Tk.root.bind(ev_quit, proc{Tk.exit}).focus + +Tk.mainloop -- cgit v1.2.3