1#
2#  tkextlib/blt/bitmap.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tkextlib/blt.rb'
8
9module Tk::BLT
10  class Bitmap < TkObject
11    extend TkCore
12
13    TkCommandNames = ['::blt::bitmap'.freeze].freeze
14
15    BITMAP_ID_TBL = TkCore::INTERP.create_table
16
17    (BITMAP_ID = ['blt_bitmap_id'.freeze, TkUtil.untrust('00000')]).instance_eval{
18      @mutex = Mutex.new
19      def mutex; @mutex; end
20      freeze
21    }
22
23    TkCore::INTERP.init_ip_env{
24      BITMAP_ID_TBL.mutex.synchronize{ BITMAP_ID_TBL.clear }
25    }
26
27    def self.data(name)
28      dat = tk_simple_list(tk_call('::blt::bitmap', 'data', name))
29      [ tk_split_list(dat[0]), tk_simple_list(dat[1]) ]
30    end
31
32    def self.exist?(name)
33      bool(tk_call('::blt::bitmap', 'exists', name))
34    end
35
36    def self.height(name)
37      number(tk_call('::blt::bitmap', 'height', name))
38    end
39
40    def self.width(name)
41      number(tk_call('::blt::bitmap', 'width', name))
42    end
43
44    def self.source(name)
45      tk_simple_list(tk_call('::blt::bitmap', 'source', name))
46    end
47
48    #################################
49
50    class << self
51      alias _new new
52
53      def new(data, keys={})
54        _new(:data, nil, data, keys)
55      end
56      alias define new
57
58      def new_with_name(name, data, keys={})
59        _new(:data, name, data, keys)
60      end
61      alias define_with_name new_with_name
62
63      def compose(text, keys={})
64        _new(:text, nil, text, keys)
65      end
66
67      def compose_with_name(name, text, keys={})
68        _new(:text, name, text, keys)
69      end
70    end
71
72    def initialize(type, name, data, keys = {})
73      if name
74        @id = name
75      else
76        BITMAP_ID.mutex.synchronize{
77          @id = BITMAP_ID.join(TkCore::INTERP._ip_id_)
78          BITMAP_ID[1].succ!
79        }
80        BITMAP_ID_TBL.mutex.synchronize{
81          BITMAP_ID_TBL[@id] = self
82        }
83      end
84
85      @path = @id
86
87      unless bool(tk_call('::blt::bitmap', 'exists', @id))
88        if type == :text
89          tk_call('::blt::bitmap', 'compose', @id, data, *hash_kv(keys))
90        else # :data
91          tk_call('::blt::bitmap', 'define', @id, data, *hash_kv(keys))
92        end
93      end
94    end
95
96    def exist?
97      bool(tk_call('::blt::bitmap', 'exists', @id))
98    end
99
100    def height
101      number(tk_call('::blt::bitmap', 'height', @id))
102    end
103
104    def width
105      number(tk_call('::blt::bitmap', 'width', @id))
106    end
107
108    def source
109      tk_simple_list(tk_call('::blt::bitmap', 'source', @id))
110    end
111  end
112end
113