1#
2#  tkextlib/winico/winico.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/winico/setup.rb'
13
14# TkPackage.require('winico', '0.5')
15# TkPackage.require('winico', '0.6')
16TkPackage.require('winico')
17
18module Tk
19  class Winico < TkObject
20    PACKAGE_NAME = 'winico'.freeze
21    def self.package_name
22      PACKAGE_NAME
23    end
24
25    def self.package_version
26      begin
27        TkPackage.require('winico')
28      rescue
29        ''
30      end
31    end
32  end
33end
34
35class Tk::Winico
36  WinicoID_TBL = TkCore::INTERP.create_table
37
38  TkCore::INTERP.init_ip_env{
39    WinicoID_TBL.mutex.synchronize{ WinicoID_TBL.clear }
40  }
41
42  def self.id2obj(id)
43    WinicoID_TBL.mutex.synchronize{
44      (WinicoID_TBL.key?(id))? WinicoID_TBL[id] : id
45    }
46  end
47
48  def self.info
49    simplelist(Tk.tk_call('winico', 'info')).collect{|id|
50      Tk::Winico.id2obj(id)
51    }
52  end
53
54  def self.icon_info(id)
55    simplelist(Tk.tk_call('winico', 'info', id)).collect{|inf|
56      h = Hash[*list(inf)]
57      h.keys.each{|k| h[k[1..-1]] = h.delete(k)}
58    }
59  end
60
61  #################################
62
63  def self.new_from_file(file_name)
64    self.new(file_name)
65  end
66
67  def self.new_from_resource(resource_name, file_name = nil)
68    self.new(file_name, resource_name)
69  end
70
71  def initialize(file_name, resource_name=nil, winico_id=nil)
72    if resource_name
73      # from resource
74      if file_name
75        @id = Tk.tk_call('winico', 'load', resource_name, file_name)
76      else
77        @id = Tk.tk_call('winico', 'load', resource_name)
78      end
79    elsif file_name
80      # from .ico file
81      @id = Tk.tk_call('winico', 'createfrom', file_name)
82    elsif winico_id
83      @id = winico_id
84    else
85      fail ArgumentError,
86           "must be given proper information from where loading icons"
87    end
88    @path = @id
89    WinicoID_TBL.mutex.synchronize{
90      WinicoID_TBL[@id] = self
91    }
92  end
93
94  def id
95    @id
96  end
97
98  def set_window(win_id, *opts) # opts := ?'big'|'small'?, ?pos?
99    # NOTE:: the window, which is denoted by win_id, MUST BE MAPPED.
100    #        If not, then this may fail or crash.
101    tk_call('winico', 'setwindow', win_id, @id, *opts)
102  end
103
104  def delete
105    tk_call('winico', 'delete', @id)
106    WinicoID_TBL.mutex.synchronize{
107      WinicoID_TBL.delete(@id)
108    }
109    self
110  end
111  alias destroy delete
112
113  def info
114    Tk::Winico.icon_info(@id)
115  end
116
117  #################################
118
119  class Winico_callback < TkValidateCommand
120    class ValidateArgs < TkUtil::CallbackSubst
121      KEY_TBL = [
122        [ ?m, ?s, :message ],
123        [ ?i, ?x, :icon ],
124        [ ?x, ?n, :x ],
125        [ ?y, ?n, :y ],
126        [ ?X, ?n, :last_x ],
127        [ ?Y, ?n, :last_y ],
128        [ ?t, ?n, :tickcount ],
129        [ ?w, ?n, :icon_idnum ],
130        [ ?l, ?n, :msg_idnum ],
131        nil
132      ]
133
134      PROC_TBL = [
135        [ ?n, TkComm.method(:number) ],
136        [ ?s, TkComm.method(:string) ],
137        [ ?x, proc{|id|
138            Tk::Winico::WinicoID_TBL.mutex.synchronize{
139              if Tk::Winico::WinicoID_TBL.key?(id)
140                obj = Tk::Winico::WinicoID_TBL[id]
141              else
142                # Tk::Winico.new(nil, nil, id)
143                obj = Tk::Winico.allocate
144                obj.instance_eval{ @path = @id = id }
145                Tk::Winico::WinicoID_TBL[id] = obj
146              end
147              obj
148            }
149          } ],
150        nil
151      ]
152
153=begin
154      # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
155      KEY_TBL.map!{|inf|
156        if inf.kind_of?(Array)
157          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
158          inf[1] = inf[1].getbyte(0) if inf[1].kind_of?(String)
159        end
160        inf
161      }
162
163      PROC_TBL.map!{|inf|
164        if inf.kind_of?(Array)
165          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
166        end
167        inf
168      }
169=end
170
171      _setup_subst_table(KEY_TBL, PROC_TBL);
172
173      def self.ret_val(val)
174        val
175      end
176    end
177
178    def self._config_keys
179      ['callback']
180    end
181  end
182
183  #################################
184
185  def add_to_taskbar(keys = {})
186    keys = _symbolkey2str(keys)
187    Winico_callback._config_keys.each{|k|
188      if keys[k].kind_of?(Array)
189        cmd, *args = keys[k]
190        #keys[k] = Winico_callback.new(cmd, args.join(' '))
191        keys[k] = Winico_callback.new(cmd, *args)
192       # elsif keys[k].kind_of?(Proc)
193      elsif TkComm._callback_entry?(keys[k])
194        keys[k] = Winico_callback.new(keys[k])
195      end
196    }
197    tk_call('winico', 'taskbar', 'add', @id, *(hash_kv(keys)))
198    self
199  end
200  alias taskbar_add add_to_taskbar
201
202  def modify_taskbar(keys = {})
203    keys = _symbolkey2str(keys)
204    Winico_callback._config_keys.each{|k|
205      if keys[k].kind_of?(Array)
206        cmd, *args = keys[k]
207        #keys[k] = Winico_callback.new(cmd, args.join(' '))
208        keys[k] = Winico_callback.new(cmd, *args)
209      # elsif keys[k].kind_of?(Proc)
210      elsif TkComm._callback_entry?(keys[k])
211        keys[k] = Winico_callback.new(keys[k])
212      end
213    }
214    tk_call('winico', 'taskbar', 'modify', @id, *(hash_kv(keys)))
215    self
216  end
217  alias taskbar_modify modify_taskbar
218
219  def delete_from_taskbar
220    tk_call('winico', 'taskbar', 'delete', @id)
221    self
222  end
223  alias taskbar_delete delete_from_taskbar
224end
225