summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/fontchooser.rb
blob: 325ca4dbd2f4779be0cac6b757eb39938798d06a (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
# frozen_string_literal: false
#
# tk/fontchooser.rb -- "tk fontchooser" support (Tcl/Tk8.6 or later)
#
require 'tk'
require 'tk/font'

module TkFont::Chooser
  extend TkCore
end

module Tk
  Fontchooser = TkFont::Chooser
end

class << TkFont::Chooser
  def method_missing(id, *args)
    name = id.id2name
    case args.length
    when 1
      if name[-1] == ?=
        configure name[0..-2], args[0]
        args[0]
      else
        configure name, args[0]
        self
      end
    when 0
      begin
        cget(name)
      rescue
        super(id, *args)
      end
    else
      super(id, *args)
    end
  end

  def __configinfo_value(key, val)
    case key
    when 'parent'
      window(val)
    when 'title'
      val
    when 'font'
      if (lst = tk_split_simplelist(val)).size == 1
        lst[0]
      else
        lst.map{|elem| num_or_str(elem)}
      end
    when 'command'
      tk_tcl2ruby(val)
    when 'visible'
      bool(val)
    else # unknown
      val
    end
  end
  private :__configinfo_value

  def configinfo(option=nil)
    if !option && TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      lst = tk_split_simplelist(tk_call('tk', 'fontchooser', 'configure'))
      ret = []
      TkComm.slice_ary(lst, 2){|k, v|
        k = k[1..-1]
        ret << [k, __configinfo_value(k, v)]
      }
      ret
    else
      current_configinfo(option)
    end
  end

  def current_configinfo(option=nil)
    if option
      opt = option.to_s
      fail ArgumentError, "Invalid option `#{option.inspect}'" if opt.empty?
      __configinfo_value(option.to_s, tk_call('tk','fontchooser',
                                              'configure',"-#{opt}"))
    else
      lst = tk_split_simplelist(tk_call('tk', 'fontchooser', 'configure'))
      ret = {}
      TkComm.slice_ary(lst, 2){|k, v|
        k = k[1..-1]
        ret[k] = __configinfo_value(k, v)
      }
      ret
    end
  end

  def configure(option, value=None)
    if option.kind_of? Hash
      tk_call('tk', 'fontchooser', 'configure',
              *hash_kv(_symbolkey2str(option)))
    else
      opt = option.to_s
      fail ArgumentError, "Invalid option `#{option.inspect}'" if opt.empty?
      tk_call('tk', 'fontchooser', 'configure', "-#{opt}", value)
    end
    self
  end

  def configure_cmd(slot, value)
    configure(slot, install_cmd(value))
  end

  def command(cmd=nil, &b)
    if cmd
      configure_cmd('command', cmd)
    elsif b
      configure_cmd('command', Proc.new(&b))
    else
      cget('command')
    end
  end

  def cget(slot)
    configinfo slot
  end

  def [](slot)
    cget slot
  end

  def []=(slot, val)
    configure slot, val
    val
  end

  def show
    tk_call('tk', 'fontchooser', 'show')
    self
  end

  def hide
    tk_call('tk', 'fontchooser', 'hide')
    self
  end

  def toggle
    cget(:visible) ?  hide: show
    self
  end

  def set_for(target, title="Font")
    if target.kind_of? TkFont
      configs = {
        :font=>target.actual_hash,
        :command=>proc{|fnt, *args|
          target.configure(TkFont.actual_hash(fnt))
        }
      }
    elsif target.kind_of? Hash
      # key=>value list or OptionObj
      fnt = target[:font] rescue ''
      fnt = fnt.actual_hash if fnt.kind_of?(TkFont)
      configs = {
        :font => fnt,
        :command=>proc{|fnt, *args|
          target[:font] = TkFont.actual_hash(fnt)
        }
      }
    else
      configs = {
        :font=>target.cget_tkstring(:font),
        :command=>proc{|fnt, *args|
          target.font = TkFont.actual_hash_displayof(fnt, target)
        }
      }
    end

    configs[:title] = title if title
    configure(configs)
    target
  end

  def unset
    configure(:command, nil)
  end
end