1#
2#  tkextlib/ICONS/icons.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7
8# call setup script for general 'tkextlib' libraries
9require 'tkextlib/setup.rb'
10
11# call setup script
12require 'tkextlib/ICONS/setup.rb'
13
14# TkPackage.require('icons', '1.0')
15TkPackage.require('icons')
16
17module Tk
18  class ICONS < TkImage
19    extend Tk
20
21    PACKAGE_NAME = 'icons'.freeze
22    def self.package_name
23      PACKAGE_NAME
24    end
25
26    def self.package_version
27      begin
28        TkPackage.require('icons')
29      rescue
30        ''
31      end
32    end
33
34    def self.create(*args)  # icon, icon, ..., ?option=>value, ...?
35      if args[-1].kind_of?(Hash)
36        keys = args.pop
37        icons = simplelist(tk_call('::icons::icons', 'create',
38                                   *(hash_kv(keys) << (args.flatten))))
39      else
40        icons = simplelist(tk_call('::icons::icons', 'create',
41                                   args.flatten))
42      end
43
44      icons.collect{|icon| self.new(icon, :without_creating=>true)}
45    end
46
47    def self.delete(*icons)  # icon, icon, ...
48      icons = icons.flatten
49      return if icons.empty?
50      icons.map!{|icon|
51        if icon.kind_of?(Tk::ICONS)
52          Tk_IMGTBL.delete(icon.path)
53          icon.name
54        elsif icon.to_s =~ /^::icon::(.*)/
55          name = $1
56          Tk_IMGTBL.delete(icon)
57          name
58        else
59          Tk_IMGTBL.delete("::icon::#{icon}")
60          icon
61        end
62      }
63      tk_call('::icons::icons', 'delete', icons)
64    end
65
66    def self.query(*args)  # icon, icon, ..., ?option=>value, ...?
67      if args[-1].kind_of?(Hash)
68        keys = args.pop
69        simplelist(tk_call('::icons::icons', 'query',
70                           *(hash_kv(keys) << (args.flatten))))
71      else
72        simplelist(tk_call('::icons::icons', 'query', args.flatten))
73      end . map{|inf| list(inf) }
74    end
75
76    ##########################################
77
78    class << self
79      alias _new new
80
81      def new(name, keys=nil)
82        if obj = Tk_IMGTBL["::icon::#{name}"]
83          if keys
84            keys = _symbolkey2str(keys)
85            unless keys.delete('without_creating')
86              tk_call('::icons::icons', 'create', *(hash_kv(keys) << obj.name))
87            end
88          end
89        else
90          obj = _new(name, keys)
91        end
92        obj
93      end
94    end
95
96    ##########################################
97
98    def initialize(name, keys=nil)
99      if name.kind_of?(String) && name =~ /^::icon::(.+)$/
100          @name = $1
101          @path = name
102      else
103        @name = name.to_s
104        @path = "::icon::#{@name}"
105      end
106      keys = _symbolkey2str(keys)
107      unless keys.delete('without_creating')
108        tk_call('::icons::icons', 'create', *(hash_kv(keys) << @name))
109      end
110      Tk_IMGTBL[@path] = self
111    end
112
113    def name
114      @name
115    end
116
117    def delete
118      Tk_IMGTBL.delete(@path)
119      tk_call('::icons::icons', 'delete', @name)
120      self
121    end
122
123    def query(keys={})
124      list(simplelist(tk_call('::icons::icons', 'query',
125                               *(hash_kv(keys) << @name))
126                      )[0])
127    end
128  end
129end
130