summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/itemfont.rb
blob: 6e92fc001c2f7a5014d203ddd23b30f9c7e7efd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#
# tk/itemfont.rb : control font of widget items
#
require 'tk'

module TkTreatItemFont
  def __conf_cmd(idx)
    raise NotImplementedError, "need to define `__conf_cmd'"
  end
  def __item_pathname(tagOrId)
    raise NotImplementedError, "need to define `__item_pathname'"
  end
  private :__conf_cmd, :__item_pathname

  def tagfont_configinfo(tagOrId, name = nil)
    pathname = __item_pathname(tagOrId)
    ret = TkFont.used_on(pathname)
    if ret == nil
=begin
      if name
	ret = name
      else
	ret = TkFont.init_widget_font(pathname, self.path, 
				      __conf_cmd(0), __conf_cmd(1), tagOrId)
      end
=end
      ret = TkFont.init_widget_font(pathname, self.path, 
				    __conf_cmd(0), __conf_cmd(1), tagOrId)
    end
    ret
  end
  alias tagfontobj tagfont_configinfo

  def tagfont_configure(tagOrId, slot)
    pathname = __item_pathname(tagOrId)
    slot = _symbolkey2str(slot)

    if slot.key?('font')
      fnt = slot.delete('font')
      if fnt.kind_of? TkFont
	return fnt.call_font_configure(pathname, self.path,
				       __conf_cmd(0), __conf_cmd(1), 
				       tagOrId, slot)
      else
	if fnt 
	  if (slot.key?('kanjifont') || 
	      slot.key?('latinfont') || 
	      slot.key?('asciifont'))
	    fnt = TkFont.new(fnt)

	    lfnt = slot.delete('latinfont')
	    lfnt = slot.delete('asciifont') if slot.key?('asciifont')
	    kfnt = slot.delete('kanjifont')

	    fnt.latin_replace(lfnt) if lfnt
	    fnt.kanji_replace(kfnt) if kfnt
	  end

	  slot['font'] = fnt
	  tk_call(self.path, __conf_cmd(0), __conf_cmd(1), 
		  tagOrId, *hash_kv(slot))
	end
	return self
      end
    end

    lfnt = slot.delete('latinfont')
    lfnt = slot.delete('asciifont') if slot.key?('asciifont')
    kfnt = slot.delete('kanjifont')

    if lfnt && kfnt
      return TkFont.new(lfnt, kfnt).call_font_configure(pathname, self.path,
							__conf_cmd(0), 
							__conf_cmd(1), 
							tagOrId, slot)
    end

    latintagfont_configure(tagOrId, lfnt) if lfnt
    kanjitagfont_configure(tagOrId, kfnt) if kfnt
      
    tk_call(self.path, __conf_cmd(0), __conf_cmd(1), 
	    tagOrId, *hash_kv(slot)) if slot != {}
    self
  end

  def latintagfont_configure(tagOrId, ltn, keys=nil)
    pathname = __item_pathname(tagOrId)
    if (fobj = TkFont.used_on(pathname))
      fobj = TkFont.new(fobj)    # create a new TkFont object
    elsif Tk::JAPANIZED_TK
      fobj = tagfontobj(tagOrId) # create a new TkFont object
    else
      tk_call(self.path, __conf_cmd(0), __conf_cmd(1), tagOrId, '-font', ltn)
      return self
    end

    if fobj.kind_of?(TkFont)
      if ltn.kind_of? TkFont
	conf = {}
	ltn.latin_configinfo.each{|key,val| conf[key] = val}
	if keys
	  fobj.latin_configure(conf.update(keys))
	else
	  fobj.latin_configure(conf)
	end
      else
	fobj.latin_replace(ltn)
      end
    end

    return fobj.call_font_configure(pathname, self.path,
				    __conf_cmd(0), __conf_cmd(1), tagOrId, {})
  end
  alias asciitagfont_configure latintagfont_configure

  def kanjitagfont_configure(tagOrId, knj, keys=nil)
    pathname = __item_pathname(tagOrId)
    if (fobj = TkFont.used_on(pathname))
      fobj = TkFont.new(fobj)    # create a new TkFont object
    elsif Tk::JAPANIZED_TK
      fobj = tagfontobj(tagOrId) # create a new TkFont object
    else
      tk_call(self.path, __conf_cmd(0), __conf_cmd(1), tagOrId, '-font', knj)
      return self
    end

    if fobj.kind_of?(TkFont)
      if knj.kind_of? TkFont
	conf = {}
	knj.kanji_configinfo.each{|key,val| conf[key] = val}
	if keys
	  fobj.kanji_configure(conf.update(keys))
	else
	  fobj.kanji_configure(conf)
	end
      else
	fobj.kanji_replace(knj)
      end
    end

    return fobj.call_font_configure(pathname, self.path,
				    __conf_cmd(0), __conf_cmd(1), tagOrId, {})
  end

  def tagfont_copy(tagOrId, window, wintag=nil)
    pathname = __item_pathname(tagOrId)
    if wintag
      fnt = window.tagfontobj(wintag).dup
    else
      fnt = window.fontobj.dup
    end
    fnt.call_font_configure(pathname, self.path, 
			    __conf_cmd(0), __conf_cmd(1), tagOrId, {})
    return self
  end

  def latintagfont_copy(tagOrId, window, wintag=nil)
    pathname = __item_pathname(tagOrId)
    tagfontobj(tagOrId).dup.call_font_configure(pathname, self.path, 
						__conf_cmd(0), __conf_cmd(1), 
						tagOrId, {})
    if wintag
      tagfontobj(tagOrId).
	latin_replace(window.tagfontobj(wintag).latin_font_id)
    else
      tagfontobj(tagOrId).latin_replace(window.fontobj.latin_font_id)
    end
    self
  end
  alias asciitagfont_copy latintagfont_copy

  def kanjitagfont_copy(tagOrId, window, wintag=nil)
    pathname = __item_pathname(tagOrId)
    tagfontobj(tagOrId).dup.call_font_configure(pathname, self.path, 
						__conf_cmd(0), __conf_cmd(1), 
						tagOrId, {})
    if wintag
      tagfontobj(tagOrId).
	kanji_replace(window.tagfontobj(wintag).kanji_font_id)
    else
      tagfontobj(tagOrId).kanji_replace(window.fontobj.kanji_font_id)
    end
    self
  end
end