summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-21 12:55:17 +0000
committernagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-06-21 12:55:17 +0000
commit07cff4bd71d4d71e4901aac5df11746ce96f009b (patch)
tree27d9b56f85b13be8924e56ccc1f6009d5bd666d9
parent818157eae67c00be5610ce6e9488d448009a40ba (diff)
tk.rb :
* TkRoot.new and TkToplevel.new accept Wm commands as elements of a hash argument. e.g. TkRoot.new(:title=>'App Title') TkToplevel.new(:parent=>Tk.root, :title=>'XXX', :class=>'ZZZ') sample/tktimer2.rb * add comments about the usage of a TkTimer object. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3974 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ext/tk/lib/tk.rb36
-rw-r--r--ext/tk/sample/tktimer2.rb22
2 files changed, 52 insertions, 6 deletions
diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb
index bfa435f..0afaa07 100644
--- a/ext/tk/lib/tk.rb
+++ b/ext/tk/lib/tk.rb
@@ -3538,12 +3538,13 @@ end
class TkRoot<TkWindow
include Wm
ROOT = []
- def TkRoot.new
+ def TkRoot.new(keys=nil)
if ROOT[0]
Tk_WINDOWS["."] = ROOT[0]
return ROOT[0]
end
new = super(:without_creating=>true, :widgetname=>'.')
+ keys.each{|k,v| new.send(k,v)} if keys # wm commands
ROOT[0] = new
Tk_WINDOWS["."] = new
end
@@ -3597,11 +3598,38 @@ class TkToplevel<TkWindow
# end
#################
+ def _wm_command_option_chk(keys)
+ new_keys = {}
+ wm_cmds = {}
+ keys.each{|k,v|
+ if Wm.method_defined?(k)
+ case k
+ when 'screen','class','colormap','container','screen','use','visual'
+ new_keys[k] = v
+ else
+ case self.method(k).arity
+ when -1,1
+ wm_cmds[k] = v
+ else
+ new_keys[k] = v
+ end
+ end
+ else
+ new_keys[k] = v
+ end
+ }
+ [new_keys, wm_cmds]
+ end
+ private :_wm_command_option_chk
+
def initialize(parent=nil, screen=nil, classname=nil, keys=nil)
if parent.kind_of? Hash
keys = _symbolkey2str(parent)
- @screen = keys['screen']
+ if keys.key?('classname')
+ keys['class'] = keys.delete('classname')
+ end
@classname = keys['class']
+ @screen = keys['screen']
@colormap = keys['colormap']
@container = keys['container']
@screen = keys['screen']
@@ -3613,7 +3641,9 @@ class TkToplevel<TkWindow
else
@db_class = TkDatabaseClass.new(@classname)
end
+ keys, cmds = _wm_command_option_chk(keys)
super(keys)
+ cmds.each{|k,v| self.send(k,v)}
return
end
if screen.kind_of? Hash
@@ -3640,7 +3670,9 @@ class TkToplevel<TkWindow
else
@db_class = TkDatabaseClass.new(@classname)
end
+ keys, cmds = _wm_command_option_chk(keys)
super(parent, keys)
+ cmds.each{|k,v| self.send(k,v)}
end
def create_self(keys)
diff --git a/ext/tk/sample/tktimer2.rb b/ext/tk/sample/tktimer2.rb
index 0359ac4..dc4e8a6 100644
--- a/ext/tk/sample/tktimer2.rb
+++ b/ext/tk/sample/tktimer2.rb
@@ -3,16 +3,30 @@
require "tk"
-label = TkLabel.new(:relief=>:raised, :width=>10) \
+# 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)
-tick = proc{|aobj|
- cnt = aobj.return_value + 5
+# 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
+ 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 ->....
TkButton.new(:text=>'Start') {
command proc{ timer.continue unless timer.running? }