From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/ext/tk/sample/binding_sample.rb | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 jni/ruby/ext/tk/sample/binding_sample.rb (limited to 'jni/ruby/ext/tk/sample/binding_sample.rb') diff --git a/jni/ruby/ext/tk/sample/binding_sample.rb b/jni/ruby/ext/tk/sample/binding_sample.rb new file mode 100644 index 0000000..3c2eb5e --- /dev/null +++ b/jni/ruby/ext/tk/sample/binding_sample.rb @@ -0,0 +1,87 @@ +#!/usr/bin/env ruby + +require 'tk' + +class Button_clone < TkLabel + def initialize(*args) + @command = nil + + if args[-1].kind_of?(Hash) + keys = _symbolkey2str(args.pop) + @command = keys.delete('command') + + keys['highlightthickness'] = 1 unless keys.key?('highlightthickness') + keys['padx'] = '3m' unless keys.key?('padx') + keys['pady'] = '1m' unless keys.key?('pady') + keys['relief'] = 'raised' unless keys.key?('relief') + + args.push(keys) + end + + super(*args) + + @press = false + + self.bind('Enter', proc{self.background(self.activebackground)}) + self.bind('Leave', proc{ + @press = false + self.background(self.highlightbackground) + self.relief('raised') + }) + + self.bind('ButtonPress-1', proc{@press = true; self.relief('sunken')}) + self.bind('ButtonRelease-1', proc{ + self.relief('raised') + @command.call if @press && @command + @press = false + }) + end + + def command(cmd = Proc.new) + @command = cmd + end + + def invoke + if @command + @command.call + else + '' + end + end +end + +TkLabel.new(:text=><'red').pack(:pady=>3) + +v = TkVariable.new(0) + +TkFrame.new{|f| + TkLabel.new(f, :text=>'click count : ').pack(:side=>:left) + TkLabel.new(f, :textvariable=>v).pack(:side=>:left) +}.pack + +TkButton.new(:text=>'normal Button widget', + :command=>proc{ + puts 'button is clicked!!' + lbl.text 'button is clicked!!' + v.numeric += 1 + }){ + pack(:fill=>:x, :expand=>true) +} + +Button_clone.new(:text=>'Label with Button binding', + :command=>proc{ + puts 'label is clicked!!' + lbl.text 'label is clicked!!' + v.numeric += 1 + }){ + pack(:fill=>:x, :expand=>true) +} + +Tk.mainloop -- cgit v1.2.3