1 #
2 # tk/itemfont.rb : control font of widget items
3 #
4 require 'tk'
5
6 module TkItemFontOptkeys
7 def __item_font_optkeys(id)
8 # maybe need to override
9 ['font']
10 end
11 private :__item_font_optkeys
12 end
13
14 module TkTreatItemFont
15 include TkItemFontOptkeys
16
17 def __item_pathname(id)
18 # maybe need to override
19 [self.path, id].join(';')
20 end
21 private :__item_pathname
22
23 ################################################
24
25 def tagfont_configinfo(tagOrId, key = nil)
26 optkeys = __item_font_optkeys(tagid(tagOrId))
27 if key && !optkeys.find{|opt| opt.to_s == key.to_s}
28 fail ArgumentError, "unknown font option name `#{key}'"
29 end
30
31 win, tag = __item_pathname(tagid(tagOrId)).split(';')
32
33 if key
34 pathname = [win, tag, key].join(';')
35 TkFont.used_on(pathname) ||
36 TkFont.init_widget_font(pathname,
37 *(__item_confinfo_cmd(tagid(tagOrId))))
38 elsif optkeys.size == 1
39 pathname = [win, tag, optkeys[0]].join(';')
40 TkFont.used_on(pathname) ||
41 TkFont.init_widget_font(pathname,
42 *(__item_confinfo_cmd(tagid(tagOrId))))
43 else
44 fonts = {}
45 optkeys.each{|key|
46 key = key.to_s
47 pathname = [win, tag, key].join(';')
48 fonts[key] =
49 TkFont.used_on(pathname) ||
50 TkFont.init_widget_font(pathname,
51 *(__item_confinfo_cmd(tagid(tagOrId))))
52 }
53 fonts
54 end
55 end
56 alias tagfontobj tagfont_configinfo
57
58 def tagfont_configure(tagOrId, slot)
59 pathname = __item_pathname(tagid(tagOrId))
60
61 slot = _symbolkey2str(slot)
62
63 __item_font_optkeys(tagid(tagOrId)).each{|optkey|
64 optkey = optkey.to_s
65 l_optkey = 'latin' << optkey
66 a_optkey = 'ascii' << optkey
67 k_optkey = 'kanji' << optkey
68
69 if slot.key?(optkey)
70 fnt = slot.delete(optkey)
71 if fnt.kind_of?(TkFont)
72 slot.delete(l_optkey)
73 slot.delete(a_optkey)
74 slot.delete(k_optkey)
75
76 fnt.call_font_configure([pathname, optkey],
77 *(__item_config_cmd(tagid(tagOrId)) << {}))
78 next
79 else
80 if fnt
81 if (slot.key?(l_optkey) ||
82 slot.key?(a_optkey) ||
83 slot.key?(k_optkey))
84 fnt = TkFont.new(fnt)
85
86 lfnt = slot.delete(l_optkey)
87 lfnt = slot.delete(a_optkey) if slot.key?(a_optkey)
88 kfnt = slot.delete(k_optkey)
89
90 fnt.latin_replace(lfnt) if lfnt
91 fnt.kanji_replace(kfnt) if kfnt
92
93 fnt.call_font_configure([pathname, optkey],
94 *(__item_config_cmd(tagid(tagOrId)) << {}))
95 next
96 else
97 fnt = hash_kv(fnt) if fnt.kind_of?(Hash)
98 unless TkItemConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
99 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << fnt))
100 else
101 begin
102 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << fnt))
103 rescue => e
104 # ignore
105 end
106 end
107 end
108 end
109 next
110 end
111 end
112
113 lfnt = slot.delete(l_optkey)
114 lfnt = slot.delete(a_optkey) if slot.key?(a_optkey)
115 kfnt = slot.delete(k_optkey)
116
117 if lfnt && kfnt
118 TkFont.new(lfnt, kfnt).call_font_configure([pathname, optkey],
119 *(__item_config_cmd(tagid(tagOrId)) << {}))
120 elsif lfnt
121 latintagfont_configure([lfnt, optkey])
122 elsif kfnt
123 kanjitagfont_configure([kfnt, optkey])
124 end
125 }
126
127 # configure other (without font) options
128 tk_call(*(__item_config_cmd(tagid(tagOrId)).concat(hash_kv(slot)))) if slot != {}
129 self
130 end
131
132 def latintagfont_configure(tagOrId, ltn, keys=nil)
133 if ltn.kind_of?(Array)
134 key = ltn[1]
135 ltn = ltn[0]
136 else
137 key = nil
138 end
139
140 optkeys = __item_font_optkeys(tagid(tagOrId))
141 if key && !optkeys.find{|opt| opt.to_s == key.to_s}
142 fail ArgumentError, "unknown font option name `#{key}'"
143 end
144
145 win, tag = __item_pathname(tagid(tagOrId)).split(';')
146
147 optkeys = [key] if key
148
149 optkeys.each{|optkey|
150 optkey = optkey.to_s
151
152 pathname = [win, tag, optkey].join(';')
153
154 if (fobj = TkFont.used_on(pathname))
155 fobj = TkFont.new(fobj) # create a new TkFont object
156 elsif Tk::JAPANIZED_TK
157 fobj = fontobj # create a new TkFont object
158 else
159 ltn = hash_kv(ltn) if ltn.kind_of?(Hash)
160 unless TkItemConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
161 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << ltn))
162 else
163 begin
164 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << ltn))
165 rescue => e
166 # ignore
167 end
168 end
169 next
170 end
171
172 if fobj.kind_of?(TkFont)
173 if ltn.kind_of?(TkFont)
174 conf = {}
175 ltn.latin_configinfo.each{|key,val| conf[key] = val}
176 if keys
177 fobj.latin_configure(conf.update(keys))
178 else
179 fobj.latin_configure(conf)
180 end
181 else
182 fobj.latin_replace(ltn)
183 end
184 end
185
186 fobj.call_font_configure([pathname, optkey], *(__item_config_cmd(tagid(tagOrId)) << {}))
187 }
188 self
189 end
190 alias asciitagfont_configure latintagfont_configure
191
192 def kanjitagfont_configure(tagOrId, knj, keys=nil)
193 if knj.kind_of?(Array)
194 key = knj[1]
195 knj = knj[0]
196 else
197 key = nil
198 end
199
200 optkeys = __item_font_optkeys(tagid(tagOrId))
201 if key && !optkeys.find{|opt| opt.to_s == key.to_s}
202 fail ArgumentError, "unknown font option name `#{key}'"
203 end
204
205 win, tag = __item_pathname(tagid(tagOrId)).split(';')
206
207 optkeys = [key] if key
208
209 optkeys.each{|optkey|
210 optkey = optkey.to_s
211
212 pathname = [win, tag, optkey].join(';')
213
214 if (fobj = TkFont.used_on(pathname))
215 fobj = TkFont.new(fobj) # create a new TkFont object
216 elsif Tk::JAPANIZED_TK
217 fobj = fontobj # create a new TkFont object
218 else
219 knj = hash_kv(knj) if knj.kind_of?(Hash)
220 unless TkItemConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
221 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << knj))
222 else
223 begin
224 tk_call(*(__item_config_cmd(tagid(tagOrId)) << "-#{optkey}" << knj))
225 rescue => e
226 # ignore
227 end
228 end
229 next
230 end
231
232 if fobj.kind_of?(TkFont)
233 if knj.kind_of?(TkFont)
234 conf = {}
235 knj.kanji_configinfo.each{|key,val| conf[key] = val}
236 if keys
237 fobj.kanji_configure(conf.update(keys))
238 else
239 fobj.kanji_configure(conf)
240 end
241 else
242 fobj.kanji_replace(knj)
243 end
244 end
245
246 fobj.call_font_configure([pathname, optkey], *(__item_config_cmd(tagid(tagOrId)) << {}))
247 }
248 self
249 end
250
251 def tagfont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
252 if wintag
253 if winkey
254 fnt = win.tagfontobj(wintag, winkey).dup
255 else
256 fnt = win.tagfontobj(wintag).dup
257 end
258 else
259 if winkey
260 fnt = win.fontobj(winkey).dup
261 else
262 fnt = win.fontobj.dup
263 end
264 end
265
266 if targetkey
267 fnt.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey],
268 *(__item_config_cmd(tagid(tagOrId)) << {}))
269 else
270 fnt.call_font_configure(__item_pathname(tagid(tagOrId)),
271 *(__item_config_cmd(tagid(tagOrId)) << {}))
272 end
273 self
274 end
275
276
277 def latintagfont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
278 if targetkey
279 fontobj(targetkey).dup.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey],
280 *(__item_config_cmd(tagid(tagOrId)) << {}))
281 else
282 fontobj.dup.call_font_configure(__item_pathname(tagid(tagOrId)),
283 *(__item_config_cmd(tagid(tagOrId)) << {}))
284 end
285
286 if wintag
287 if winkey
288 fontobj.latin_replace(win.tagfontobj(wintag, winkey).latin_font_id)
289 else
290 fontobj.latin_replace(win.tagfontobj(wintag).latin_font_id)
291 end
292 else
293 if winkey
294 fontobj.latin_replace(win.fontobj(winkey).latin_font_id)
295 else
296 fontobj.latin_replace(win.fontobj.latin_font_id)
297 end
298 end
299 self
300 end
301 alias asciitagfont_copy latintagfont_copy
302
303 def kanjifont_copy(tagOrId, win, wintag=nil, winkey=nil, targetkey=nil)
304 if targetkey
305 fontobj(targetkey).dup.call_font_configure([__item_pathname(tagid(tagOrId)), targetkey],
306 *(__item_config_cmd(tagid(tagOrId)) << {}))
307 else
308 fontobj.dup.call_font_configure(__item_pathname(tagid(tagOrId)),
309 *(__item_config_cmd(tagid(tagOrId)) << {}))
310 end
311
312 if wintag
313 if winkey
314 fontobj.kanji_replace(win.tagfontobj(wintag, winkey).kanji_font_id)
315 else
316 fontobj.kanji_replace(win.tagfontobj(wintag).kanji_font_id)
317 end
318 else
319 if winkey
320 fontobj.kanji_replace(win.fontobj(winkey).kanji_font_id)
321 else
322 fontobj.kanji_replace(win.fontobj.kanji_font_id)
323 end
324 end
325 self
326 end
327 end