summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 bfa435f326..0afaa07d35 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 0359ac4d08..dc4e8a6964 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? }