1# 2# tk/fontchooser.rb -- "tk fontchooser" support (Tcl/Tk8.6 or later) 3# 4require 'tk' 5require 'tk/font' 6 7module TkFont::Chooser 8 extend TkCore 9end 10 11class << TkFont::Chooser 12 def method_missing(id, *args) 13 name = id.id2name 14 case args.length 15 when 1 16 if name[-1] == ?= 17 configure name[0..-2], args[0] 18 args[0] 19 else 20 configure name, args[0] 21 self 22 end 23 when 0 24 begin 25 cget(name) 26 rescue 27 super(id, *args) 28 end 29 else 30 super(id, *args) 31 end 32 end 33 34 def __configinfo_value(key, val) 35 case key 36 when 'parent' 37 window(val) 38 when 'title' 39 val 40 when 'font' 41 if (lst = tk_split_simplelist(val)).size == 1 42 lst[0] 43 else 44 lst.map{|elem| num_or_str(elem)} 45 end 46 when 'command' 47 tk_tcl2ruby(val) 48 when 'visible' 49 bool(val) 50 else # unkown 51 val 52 end 53 end 54 private :__configinfo_value 55 56 def configinfo(option=nil) 57 if !option && TkComm::GET_CONFIGINFOwoRES_AS_ARRAY 58 lst = tk_split_simplelist(tk_call('tk', 'fontchooser', 'configure')) 59 ret = [] 60 TkComm.slice_ary(lst, 2){|k, v| 61 k = k[1..-1] 62 ret << [k, __configinfo_value(k, v)] 63 } 64 ret 65 else 66 current_configinfo(option) 67 end 68 end 69 70 def current_configinfo(option=nil) 71 if option 72 opt = option.to_s 73 fail ArgumentError, "Invalid option `#{option.inspect}'" if opt.empty? 74 __configinfo_value(option.to_s, tk_call('tk','fontchooser', 75 'configure',"-#{opt}")) 76 else 77 lst = tk_split_simplelist(tk_call('tk', 'fontchooser', 'configure')) 78 ret = {} 79 TkComm.slice_ary(lst, 2){|k, v| 80 k = k[1..-1] 81 ret[k] = __configinfo_value(k, v) 82 } 83 ret 84 end 85 end 86 87 def configure(option, value=None) 88 if option.kind_of? Hash 89 tk_call('tk', 'fontchooser', 'configure', 90 *hash_kv(_symbolkey2str(option))) 91 else 92 opt = option.to_s 93 fail ArgumentError, "Invalid option `#{option.inspect}'" if opt.empty? 94 tk_call('tk', 'fontchooser', 'configure', "-#{opt}", value) 95 end 96 self 97 end 98 99 def configure_cmd(slot, value) 100 configure(slot, install_cmd(value)) 101 end 102 103 def command(cmd=nil, &b) 104 if cmd 105 configure_cmd('command', cmd) 106 elsif b 107 configure_cmd('command', Proc.new(&b)) 108 else 109 cget('command') 110 end 111 end 112 113 def cget(slot) 114 configinfo slot 115 end 116 117 def [](slot) 118 cget slot 119 end 120 121 def []=(slot, val) 122 configure slot, val 123 val 124 end 125 126 def show 127 tk_call('tk', 'fontchooser', 'show') 128 self 129 end 130 131 def hide 132 tk_call('tk', 'fontchooser', 'hide') 133 self 134 end 135 136 def toggle 137 cget(:visible) ? hide: show 138 self 139 end 140 141 def set_for(target, title="Font") 142 if target.kind_of? TkFont 143 configs = { 144 :font=>target.actual_hash, 145 :command=>proc{|fnt, *args| 146 target.configure(TkFont.actual_hash(fnt)) 147 } 148 } 149 elsif target.kind_of? Hash 150 # key=>value list or OptionObj 151 fnt = target[:font] rescue '' 152 fnt = fnt.actual_hash if fnt.kind_of?(TkFont) 153 configs = { 154 :font => fnt, 155 :command=>proc{|fnt, *args| 156 target[:font] = TkFont.actual_hash(fnt) 157 } 158 } 159 else 160 configs = { 161 :font=>target.cget_tkstring(:font), 162 :command=>proc{|fnt, *args| 163 target.font = TkFont.actual_hash_displayof(fnt, target) 164 } 165 } 166 end 167 168 configs[:title] = title if title 169 configure(configs) 170 target 171 end 172 173 def unset 174 configure(:command, nil) 175 end 176end 177