summaryrefslogtreecommitdiff
path: root/lib/tktext.rb
blob: 55e396c497376916119a1983127a6f00fc12c203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
#		tktext.rb - Tk text classes
#			$Date: 1995/12/07 08:37:10 $
#			by Yukihiro Matsumoto <matz@caelum.co.jp>

require 'tk.rb'

class TkText<TkTextWin
  include Scrollable
  def create_self
    tk_call 'text', @path
    @tags = {}
  end
  def index(index)
    tk_send 'index', index
  end
  def value
    tk_send 'get', "1.0", "end"
  end
  def value= (val)
    tk_send 'delete', "1.0", 'end'
    tk_send 'insert', "1.0", val
  end
  def _addcmd(cmd)
    @cmdtbl.push id
  end
  def _addtag(cmd)
    @cmdtbl.push id
  end
  private :_addcmd, :_addtag
  def tag_names
    tk_send('tag', 'names').collect{|elt|
      if not @tags[elt]
	elt
      else
	@tags[elt]
      end
    }
  end
  def window_names
    tk_send('window', 'names').collect{|elt|
      if not @tags[elt]
	elt
      else
	@tags[elt]
      end
    }
  end

  def destroy
    for t in @tags
      t.destroy
    end
    super
  end

  def backspace
    self.delete 'insert'
  end

  def compare(idx1, op, idx2)
    bool(tk_send('compare', idx1, op, idx2))
  end

  def debug
    bool(tk_send('debug'))
  end
  def debug=(boolean)
    tk_send 'debug', boolean
  end

  def yview(*what)
    tk_send 'yview', *what
  end
  def yview_pickplace(*what)
    tk_send 'yview', '-pickplace', *what
  end
end

class TkTextTag<TkObject
  $tk_text_tag = 'tag0000'
  def initialize(parent)
    if not parent.kind_of?(TkText)
      fail format("%s need to be TkText", parent.inspect)
    end
    @t = parent
    @path = parent.path
    @id = $tk_text_tag
    $tk_text_tag = $tk_text_tag.succ
    @t._addtag id, self
  end
  def id
    return @id
  end

  def add(*index)
    tk_call path, 'tag', 'add', @id, *index
  end

  def configure(slot, value)
    tk_call path, 'tag', 'configure', id, "-#{slot}", value
  end

  def bind(seq, cmd=Proc.new)
    id = install_cmd(cmd)
    tk_call path, 'tag', 'bind', tag, "<#{seq}>", id
    @t._addcmd cmd
  end

  def lower(below=None)
    tk_call path, 'tag', 'lower', below
  end

  def destroy
    tk_call path, 'tag', 'delete', @id
  end
end

class TkTextMark<TkObject
  $tk_text_mark = 'mark0000'
  def initialize(parent, index)
    if not parent.kind_of?(TkText)
      fail format("%s need to be TkText", parent.inspect)
    end
    @t = parent
    @path = parent.path
    @id = $tk_text_mark
    $tk_text_mark = $tk_text_mark.succ
    tk_call @t, 'set', @id, index
    @t._addtag id, self
  end
  def id
    return @id
  end

  def set(where)
    tk_call path, 'mark', 'unset', @id, where
  end

  def unset
    tk_call path, 'mark', 'unset', @id
  end
  alias destroy unset
end

class TkTextWindow<TkObject
  def initialize(parent, index, *args)
    if not parent.kind_of?(TkText)
      fail format("%s need to be TkText", parent.inspect)
    end
    @t = parent
    @path = parent.path
    @index = index
    tk_call @path, 'window', 'create', index, *args
  end

  def configure(slot, value)
    tk_call path, 'window', 'configure', @index, "-#{slot}", value
  end
end