1#
2#  tkextlib/tcllib/ico.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5#   * Part of tcllib extension
6#   * Reading and writing windows icons
7#
8
9require 'tk'
10require 'tk/image'
11#require 'tkextlib/tcllib.rb'
12
13# TkPackage.require('ico', '0.3')
14TkPackage.require('ico')
15
16module Tk
17  module Tcllib
18    class ICO < TkImage
19      PACKAGE_NAME = 'ico'.freeze
20      def self.package_name
21        PACKAGE_NAME
22      end
23
24      def self.package_version
25        begin
26          TkPackage.require('ico')
27        rescue
28          ''
29        end
30      end
31    end
32  end
33end
34
35class Tk::Tcllib::ICO
36  def self.list(file, keys=nil)
37    tk_split_list(tk_call_without_enc('::ico::getIconList', file,
38                                      *hash_kv(keys, true)))
39  end
40
41  def self.icons(file, keys=nil)
42    tk_split_simplelist(tk_call_without_enc('::ico::icons', file,
43                                            *hash_kv(keys, true))).map{|elem|
44      num_or_str(elem)
45    }
46  end
47
48  def self.get_members(file, name, keys=nil)
49    tk_split_simplelist(tk_call_without_enc('::ico::getMembers', file, name,
50                                            *hash_kv(keys, true))).map{|elem|
51      name, width, height, bpp =  tk_split_simplelist(elem)
52      [name, number(width), number(height), number(bpp)]
53    }
54  end
55
56  def self.get(file, index, keys=nil)
57    tk_call_without_enc('::ico::getIcon', file, index, *hash_kv(keys, true))
58  end
59  def self.get_icon(*args)
60    get(*args)
61  end
62
63  def self.get_by_name(file, name, keys=nil)
64    tk_call_without_enc('::ico::getIconByName', file, name,
65                        *hash_kv(keys, true))
66  end
67  def self.get_icon_by_name(*args)
68    get_by_name(*args)
69  end
70
71  def self.get_fileicon(file, keys=nil)
72    tk_call_without_enc('::ico::getFileIcon', file, *hash_kv(keys, true))
73  end
74
75  def self.get_image(file, index, keys={})
76    keys = _symbolkey2str(keys)
77    keys.delete('format')
78    self.new(file, index, keys)
79  end
80
81  def self.get_data(file, index, keys={})
82    keys['format'] = 'data'
83    tk_split_list(tk_call_without_enc('::ico::getIcon', file, index,
84                                      *hash_kv(keys, true)))
85  end
86
87  def self.write(file, index, depth, data, keys=nil)
88    tk_call_without_enc('::ico::writeIcon', file, index, depth, data,
89                        *hash_kv(keys, true))
90  end
91
92  def self.copy(from_file, from_index, to_file, to_index, keys=nil)
93    tk_call_without_enc('::ico::copyIcon',
94                        from_file, from_index, to_file, to_index,
95                        *hash_kv(keys, true))
96  end
97
98  def self.exe_to_ico(exe_file, ico_file, keys=nil)
99    tk_call_without_enc('::ico::copyIcon', exe_file, ico_file,
100                        *hash_kv(keys, true))
101  end
102
103  def self.clear_cache(file=None)
104    tk_call_without_enc('::ico::clearCache', file)
105  end
106
107  def self.transparent_color(image, color)
108    if image.kind_of?(Array)
109      tk_split_list(tk_call_without_enc('::ico::transparentColor',
110                                        image, color))
111    else
112      tk_call_without_enc('::ico::transparentColor', image, color)
113    end
114  end
115
116  def self.show(file, keys=nil)
117    tk_call_without_enc('::ico::Show', file, *hash_kv(keys, true))
118  end
119
120  ###########################
121
122  def initialize(file, index, keys=nil)
123    keys = _symbolkey2str(keys)
124    if keys.key?('name')
125      @path = keys['name'].to_s
126    else
127      Tk_Image_ID.mutex.synchronize{
128        @path = Tk_Image_ID.join(TkCore::INTERP._ip_id_)
129        Tk_Image_ID[1].succ!
130      }
131    end
132    tk_call_without_enc('::ico::getIcon', file, index, '-name', @path,
133                        '-format', 'image', *hash_kv(keys, true))
134    Tk_IMGTBL[@path] = self
135  end
136
137  def write(file, index, depth, keys=nil)
138    Tk::Tcllib::ICO.write(file, index, depth, @path, keys=nil)
139    self
140  end
141
142  def transparent_color(color)
143    tk_call_without_enc('::ico::transparentColor', @path, color)
144    self
145  end
146end
147