1#
2# tk/textwindow.rb - treat Tk text window object
3#
4require 'tk'
5require 'tk/text'
6
7class TkTextWindow<TkObject
8  include Tk::Text::IndexModMethods
9
10  def initialize(parent, index, keys = {})
11    #unless parent.kind_of?(Tk::Text)
12    #  fail ArgumentError, "expect Tk::Text for 1st argument"
13    #end
14    @t = parent
15    if index == 'end' || index == :end
16      @path = TkTextMark.new(@t, tk_call_without_enc(@t.path, 'index',
17                                                     'end - 1 chars'))
18    elsif index.kind_of?(TkTextMark)
19      if tk_call_without_enc(@t.path,'index',index.path) == tk_call_without_enc(@t.path,'index','end')
20        @path = TkTextMark.new(@t, tk_call_without_enc(@t.path, 'index',
21                                                       'end - 1 chars'))
22      else
23        @path = TkTextMark.new(@t, tk_call_without_enc(@t.path, 'index',
24                                                       index.path))
25      end
26    else
27      @path = TkTextMark.new(@t, tk_call_without_enc(@t.path, 'index', _get_eval_enc_str(index)))
28    end
29    @path.gravity = 'left'
30    @index = @path.path
31    keys = _symbolkey2str(keys)
32    @id = keys['window']
33    # keys['window'] = @id.epath if @id.kind_of?(TkWindow)
34    keys['window'] = _epath(@id) if @id
35    if keys['create']
36      @p_create = keys['create']
37      # if @p_create.kind_of?(Proc)
38      if TkComm._callback_entry?(@p_create)
39=begin
40        keys['create'] = install_cmd(proc{
41                                       @id = @p_create.call
42                                       if @id.kind_of?(TkWindow)
43                                         @id.epath
44                                       else
45                                         @id
46                                       end
47                                     })
48=end
49        keys['create'] = install_cmd(proc{@id = @p_create.call; _epath(@id)})
50      end
51    end
52    tk_call_without_enc(@t.path, 'window', 'create', @index,
53                        *hash_kv(keys, true))
54    @path.gravity = 'right'
55  end
56
57  def id
58    Tk::Text::IndexString.new(_epath(@id))
59  end
60  def mark
61    @path
62  end
63
64  def [](slot)
65    cget(slot)
66  end
67  def []=(slot, value)
68    configure(slot, value)
69    value
70  end
71
72  def cget(slot)
73    @t.window_cget(@index, slot)
74  end
75  def cget_strict(slot)
76    @t.window_cget_strict(@index, slot)
77  end
78
79  def configure(slot, value=None)
80    if slot.kind_of?(Hash)
81      slot = _symbolkey2str(slot)
82      if slot['window']
83        @id = slot['window']
84        # slot['window'] = @id.epath if @id.kind_of?(TkWindow)
85        slot['window'] = _epath(@id) if @id
86      end
87      if slot['create']
88        self.create=slot.delete('create')
89      end
90      if slot.size > 0
91        tk_call_without_enc(@t.path, 'window', 'configure', @index,
92                            *hash_kv(slot, true))
93      end
94    else
95      if slot == 'window' || slot == :window
96        @id = value
97        # value = @id.epath if @id.kind_of?(TkWindow)
98        value = _epath(@id) if @id
99      end
100      if slot == 'create' || slot == :create
101        self.create=value
102      else
103        tk_call_without_enc(@t.path, 'window', 'configure', @index,
104                            "-#{slot}", _get_eval_enc_str(value))
105      end
106    end
107    self
108  end
109
110  def configinfo(slot = nil)
111    @t.window_configinfo(@index, slot)
112  end
113
114  def current_configinfo(slot = nil)
115    @t.current_window_configinfo(@index, slot)
116  end
117
118  def window
119    @id
120  end
121
122  def window=(value)
123    @id = value
124    # value = @id.epath if @id.kind_of?(TkWindow)
125    value = _epath(@id) if @id
126    tk_call_without_enc(@t.path, 'window', 'configure', @index,
127                        '-window', _get_eval_enc_str(value))
128    value
129  end
130
131  def create
132    @p_create
133  end
134
135  def create=(value)
136    @p_create = value
137    # if @p_create.kind_of?(Proc)
138    if TkComm._callback_entry?(@p_create)
139      value = install_cmd(proc{
140                            @id = @p_create.call
141                            if @id.kind_of?(TkWindow)
142                              @id.epath
143                            else
144                              @id
145                            end
146                          })
147    end
148    tk_call_without_enc(@t.path, 'window', 'configure', @index,
149                        '-create', _get_eval_enc_str(value))
150    value
151  end
152end
153
154TktWindow = TkTextWindow
155