diff options
author | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-06-22 16:17:02 +0000 |
---|---|---|
committer | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-06-22 16:17:02 +0000 |
commit | f34b15e5d294dafa3e5f3eec00b0a14326589d47 (patch) | |
tree | 377c11a7f3f6fd39d644dd0958714825fe97b1c7 | |
parent | 25f8578b13a85fa0b64eee55c51b270528047c4a (diff) | |
download | ruby-f34b15e5d294dafa3e5f3eec00b0a14326589d47.tar.gz |
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')
* TkMenu :: add some methods
* TkOptionMenubutton :: bug fix
sample/tktimer2.rb
* add comments about the usage of a TkTimer object.
sample/tkmenubutton.rb
* sample of TkMenubutton and TkOptionMenubutton
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ext/tk/lib/tk.rb | 40 | ||||
-rw-r--r-- | ext/tk/sample/tkmenubutton.rb | 136 |
2 files changed, 165 insertions, 11 deletions
diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb index 0afaa07d35..f1226ba10f 100644 --- a/ext/tk/lib/tk.rb +++ b/ext/tk/lib/tk.rb @@ -4244,6 +4244,21 @@ class TkMenu<TkWindow tk_send 'add', type, *hash_kv(keys) self end + def add_cascade(keys=nil) + add('cascade', keys) + end + def add_checkbutton(keys=nil) + add('checkbutton', keys) + end + def add_command(keys=nil) + add('command', keys) + end + def add_radiobutton(keys=nil) + add('radiobutton', keys) + end + def add_separator(keys=nil) + add('separator', keys) + end def index(index) ret = tk_send('index', index) (ret == 'none')? nil: number(ret) @@ -4430,8 +4445,8 @@ end class TkOptionMenubutton<TkMenubutton class OptionMenu<TkMenu - def initialize(parent) - @path = parent.path + '.menu' + def initialize(path) #==> return value of tk_optionMenu + @path = path TkComm::Tk_WINDOWS[@path] = self end end @@ -4443,13 +4458,13 @@ class TkOptionMenubutton<TkMenubutton var = keys['variable'] if keys['variable'] firstval, *vals = keys['values'] end - fail unless var.kind_of? TkVariable + fail 'variable option must be TkVariable' unless var.kind_of? TkVariable @variable = var firstval = @variable.value unless firstval @variable.value = firstval install_win(if parent then parent.path end) - @menu = OptionMenu.new(self) - tk_call 'tk_optionMenu', @path, @variable.id, firstval, *vals + @menu = OptionMenu.new(tk_call('tk_optionMenu', @path, @variable.id, + firstval, *vals)) end def value @@ -4483,15 +4498,18 @@ class TkOptionMenubutton<TkMenubutton def yposition(index) @menu.yposition(index) end - def menucget(index, key) - @menu.cget(index, key) + def menu + @menu + end + def menucget(key) + @menu.cget(key) end - def menuconfigure(index, key, val=None) - @menu.configure(index, key, val) + def menuconfigure(key, val=None) + @menu.configure(key, val) self end - def menuconfiginfo(index, key=nil) - @menu.configinfo(index, key) + def menuconfiginfo(key=nil) + @menu.configinfo(key) end def entrycget(index, key) @menu.entrycget(index, key) diff --git a/ext/tk/sample/tkmenubutton.rb b/ext/tk/sample/tkmenubutton.rb new file mode 100644 index 0000000000..02a903ebb8 --- /dev/null +++ b/ext/tk/sample/tkmenubutton.rb @@ -0,0 +1,136 @@ +#!/usr/bin/env ruby +# +# menubutton sample : based on sample menubuttons on the Tcl/Tk demo script +# +require 'tk' + +TkLabel.new(:text=>'Sample of TkMenubutton').pack(:side=>:top) + +TkFrame.new{|f| + pack(:side=>:top) + + + TkMenubutton.new(:parent=>f, :text=>'Right', :underline=>0, + :direction=>:right, :relief=>:raised){|mb| + menu TkMenu.new(:parent=>mb, :tearoff=>0){ + add(:command, :label=>'Right menu: first item', + :command=>proc{print 'You have selected the first item' + + " from the Right menu.\n"}) + add(:command, :label=>'Right menu: second item', + :command=>proc{print 'You have selected the second item' + + " from the Right menu.\n"}) + } + pack(:side=>:left, :padx=>25, :pady=>25) + } + + TkMenubutton.new(:parent=>f, :text=>'Below', :underline=>0, + :direction=>:below, :relief=>:raised){|mb| + menu(TkMenu.new(:parent=>mb, :tearoff=>0){ + add(:command, :label=>'Below menu: first item', + :command=>proc{print 'You have selected the first item' + + " from the Below menu.\n"}) + add(:command, :label=>'Below menu: second item', + :command=>proc{print 'You have selected the second item' + + " from the Below menu.\n"}) + }) + pack(:side=>:left, :padx=>25, :pady=>25) + } + + TkMenubutton.new(:parent=>f, :text=>'Above', :underline=>0, + :direction=>:above, :relief=>:raised){|mb| + menu TkMenu.new(:parent=>mb, :tearoff=>0){ + add(:command, :label=>'Above menu: first item', + :command=>proc{print 'You have selected the first item' + + " from the Above menu.\n"}) + add(:command, :label=>'Above menu: second item', + :command=>proc{print 'You have selected the second item' + + " from the Above menu.\n"}) + } + pack(:side=>:left, :padx=>25, :pady=>25) + } + + TkMenubutton.new(:parent=>f, :text=>'Left', :underline=>0, + :direction=>:left, :relief=>:raised){|mb| + menu(TkMenu.new(:parent=>mb, :tearoff=>0){ + add(:command, :label=>'Left menu: first item', + :command=>proc{print 'You have selected the first item' + + " from the Left menu.\n"}) + add(:command, :label=>'Left menu: second item', + :command=>proc{print 'You have selected the second item' + + " from the Left menu.\n"}) + }) + pack(:side=>:left, :padx=>25, :pady=>25) + } +} + +############################ +TkFrame.new(:borderwidth=>2, :relief=>:sunken, + :height=>5).pack(:side=>:top, :fill=>:x, :padx=>20) +############################ + +TkLabel.new(:text=>'Sample of TkOptionMenu').pack(:side=>:top) + +colors = %w(Black red4 DarkGreen NavyBlue gray75 Red Green Blue gray50 + Yellow Cyan Magenta White Brown DarkSeaGreen DarkViolet) + +TkFrame.new{|f| + pack(:side=>:top) + + b1 = TkOptionMenubutton . + new(:parent=>f, :values=>%w(one two three)) . + pack(:side=>:left, :padx=>25, :pady=>25) + + b2 = TkOptionMenubutton.new(:parent=>f, :values=>colors) {|optMB| + colors.each{|color| + no_sel = TkPhotoImage.new(:height=>16, :width=>16){ + put 'gray50', *[ 0, 0, 16, 1] + put 'gray50', *[ 0, 1, 1, 16] + put 'gray75', *[ 0, 15, 16, 16] + put 'gray75', *[15, 1, 16, 16] + put color, *[ 1, 1, 15, 15] + } + sel = TkPhotoImage.new(:height=>16, :width=>16){ + put 'Black', *[ 0, 0, 16, 2] + put 'Black', *[ 0, 2, 2, 16] + put 'Black', *[ 2, 14, 16, 16] + put 'Black', *[14, 2, 16, 14] + put color, *[ 2, 2, 14, 14] + } + optMB.entryconfigure(color, :hidemargin=>1, + :image=>no_sel, :selectimage=>sel) + } + optMB.menuconfigure(:tearoff, 1) + %w(Black gray75 gray50 White).each{|color| + optMB.entryconfigure(color, :columnbreak=>true) + } + pack(:side=>:left, :padx=>25, :pady=>25) + } + + TkButton.new(:parent=>f){ + text 'show values' + command proc{p [b1.value, b2.value]} + pack(:side=>:left, :padx=>25, :pady=>5, :anchor=>:s) + } +} + + +############################ +TkFrame.new(:borderwidth=>2, :relief=>:sunken, + :height=>5).pack(:side=>:top, :fill=>:x, :padx=>20) +############################ + +root = TkRoot.new(:title=>'menubutton samples') + +TkButton.new(root, :text=>'exit', :command=>proc{exit}){ + pack(:side=>:top, :padx=>25, :pady=>5, :anchor=>:e) +} + +# VirtualEvent <<MenuSelect>> on Tcl/Tk ==> '<MenuSelect>' on Ruby/Tk +# ( remove the most external <, > for Ruby/Tk notation ) +TkMenu.bind('<MenuSelect>', proc{|widget| + p widget.entrycget('active', :label) + }, '%W') + +############################ + +Tk.mainloop |