~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/lib/tk/image.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# tk/image.rb : treat Tk image objects
 
3
#
 
4
 
 
5
require 'tk'
 
6
 
 
7
class TkImage<TkObject
 
8
  include Tk
 
9
 
 
10
  TkCommandNames = ['image'.freeze].freeze
 
11
 
 
12
  Tk_IMGTBL = TkCore::INTERP.create_table
 
13
  Tk_Image_ID = ['i'.freeze, '00000'.taint].freeze
 
14
 
 
15
  TkCore::INTERP.init_ip_env{ Tk_IMGTBL.clear }
 
16
 
 
17
  def self.new(keys=nil)
 
18
    if keys.kind_of?(Hash)
 
19
      name = nil
 
20
      if keys.key?(:imagename)
 
21
        name = keys[:imagename]
 
22
      elsif keys.key?('imagename')
 
23
        name = keys['imagename']
 
24
      end
 
25
      if name
 
26
        if name.kind_of?(TkImage)
 
27
          obj = name
 
28
        else
 
29
          name = _get_eval_string(name)
 
30
          obj = Tk_IMGTBL[name]
 
31
        end
 
32
        if obj
 
33
          if !(keys[:without_creating] || keys['without_creating'])
 
34
            keys = _symbolkey2str(keys)
 
35
            keys.delete('imagename')
 
36
            keys.delete('without_creating')
 
37
            obj.instance_eval{
 
38
              tk_call_without_enc('image', 'create', 
 
39
                                  @type, @path, *hash_kv(keys, true))
 
40
            }
 
41
          end
 
42
          return obj
 
43
        end
 
44
      end
 
45
    end
 
46
    super(keys)
 
47
  end
 
48
 
 
49
  def initialize(keys=nil)
 
50
    @path = nil
 
51
    without_creating = false
 
52
    if keys.kind_of?(Hash)
 
53
      keys = _symbolkey2str(keys)
 
54
      @path = keys.delete('imagename')
 
55
      without_creating = keys.delete('without_creating')
 
56
    end
 
57
    unless @path
 
58
      # @path = Tk_Image_ID.join('')
 
59
      @path = Tk_Image_ID.join(TkCore::INTERP._ip_id_)
 
60
      Tk_Image_ID[1].succ!
 
61
    end
 
62
    unless without_creating
 
63
      tk_call_without_enc('image', 'create', 
 
64
                          @type, @path, *hash_kv(keys, true))
 
65
    end
 
66
    Tk_IMGTBL[@path] = self
 
67
  end
 
68
 
 
69
  def delete
 
70
    Tk_IMGTBL.delete(@id) if @id
 
71
    tk_call_without_enc('image', 'delete', @path)
 
72
    self
 
73
  end
 
74
  def height
 
75
    number(tk_call_without_enc('image', 'height', @path))
 
76
  end
 
77
  def inuse
 
78
    bool(tk_call_without_enc('image', 'inuse', @path))
 
79
  end
 
80
  def itemtype
 
81
    tk_call_without_enc('image', 'type', @path)
 
82
  end
 
83
  def width
 
84
    number(tk_call_without_enc('image', 'width', @path))
 
85
  end
 
86
 
 
87
  def TkImage.names
 
88
    Tk.tk_call_without_enc('image', 'names').split.collect!{|id|
 
89
      (Tk_IMGTBL[id])? Tk_IMGTBL[id] : id
 
90
    }
 
91
  end
 
92
 
 
93
  def TkImage.types
 
94
    Tk.tk_call_without_enc('image', 'types').split
 
95
  end
 
96
end
 
97
 
 
98
class TkBitmapImage<TkImage
 
99
  def __strval_optkeys
 
100
    super() + ['maskdata', 'maskfile']
 
101
  end
 
102
  private :__strval_optkeys
 
103
 
 
104
  def initialize(*args)
 
105
    @type = 'bitmap'
 
106
    super(*args)
 
107
  end
 
108
end
 
109
 
 
110
class TkPhotoImage<TkImage
 
111
  NullArgOptionKeys = [ "shrink", "grayscale" ]
 
112
 
 
113
  def _photo_hash_kv(keys)
 
114
    keys = _symbolkey2str(keys)
 
115
    NullArgOptionKeys.collect{|opt|
 
116
      if keys[opt]
 
117
        keys[opt] = None
 
118
      else
 
119
        keys.delete(opt)
 
120
      end
 
121
    }
 
122
    keys.collect{|k,v|
 
123
      ['-' << k, v]
 
124
    }.flatten
 
125
  end
 
126
  private :_photo_hash_kv
 
127
 
 
128
  def initialize(*args)
 
129
    @type = 'photo'
 
130
    super(*args)
 
131
  end
 
132
 
 
133
  def blank
 
134
    tk_send_without_enc('blank')
 
135
    self
 
136
  end
 
137
 
 
138
  def cget(option)
 
139
    case option.to_s
 
140
    when 'data', 'file'
 
141
      tk_send 'cget', '-' << option.to_s
 
142
    else
 
143
      tk_tcl2ruby(tk_send('cget', '-' << option.to_s))
 
144
    end
 
145
  end
 
146
 
 
147
  def copy(src, *opts)
 
148
    if opts.size == 0
 
149
      tk_send('copy', src)
 
150
    elsif opts.size == 1 && opts[0].kind_of?(Hash)
 
151
      tk_send('copy', src, *_photo_hash_kv(opts[0]))
 
152
    else
 
153
      # for backward compatibility
 
154
      args = opts.collect{|term|
 
155
        if term.kind_of?(String) && term.include?(?\s)
 
156
          term.split
 
157
        else
 
158
          term
 
159
        end
 
160
      }.flatten
 
161
      tk_send('copy', src, *args)
 
162
    end
 
163
    self
 
164
  end
 
165
 
 
166
  def data(keys={})
 
167
    #tk_send('data', *_photo_hash_kv(keys))
 
168
    tk_split_list(tk_send('data', *_photo_hash_kv(keys)))
 
169
  end
 
170
 
 
171
  def get(x, y)
 
172
    tk_send('get', x, y).split.collect{|n| n.to_i}
 
173
  end
 
174
 
 
175
  def put(data, *opts)
 
176
    if opts == []
 
177
      tk_send('put', data)
 
178
    elsif opts.size == 1 && opts[0].kind_of?(Hash)
 
179
      tk_send('put', data, *_photo_hash_kv(opts[0]))
 
180
    else
 
181
      # for backward compatibility
 
182
      tk_send('put', data, '-to', *opts)
 
183
    end
 
184
    self
 
185
  end
 
186
 
 
187
  def read(file, *opts)
 
188
    if opts.size == 0
 
189
      tk_send('read', file)
 
190
    elsif opts.size == 1 && opts[0].kind_of?(Hash)
 
191
      tk_send('read', file, *_photo_hash_kv(opts[0]))
 
192
    else
 
193
      # for backward compatibility
 
194
      args = opts.collect{|term|
 
195
        if term.kind_of?(String) && term.include?(?\s)
 
196
          term.split
 
197
        else
 
198
          term
 
199
        end
 
200
      }.flatten
 
201
      tk_send('read', file, *args)
 
202
    end
 
203
    self
 
204
  end
 
205
 
 
206
  def redither
 
207
    tk_send 'redither'
 
208
    self
 
209
  end
 
210
 
 
211
  def get_transparency(x, y)
 
212
    bool(tk_send('transparency', 'get', x, y))
 
213
  end
 
214
  def set_transparency(x, y, st)
 
215
    tk_send('transparency', 'set', x, y, st)
 
216
    self
 
217
  end
 
218
 
 
219
  def write(file, *opts)
 
220
    if opts.size == 0
 
221
      tk_send('write', file)
 
222
    elsif opts.size == 1 && opts[0].kind_of?(Hash)
 
223
      tk_send('write', file, *_photo_hash_kv(opts[0]))
 
224
    else
 
225
      # for backward compatibility
 
226
      args = opts.collect{|term|
 
227
        if term.kind_of?(String) && term.include?(?\s)
 
228
          term.split
 
229
        else
 
230
          term
 
231
        end
 
232
      }.flatten
 
233
      tk_send('write', file, *args)
 
234
    end
 
235
    self
 
236
  end
 
237
end