Mon Jun 3 15:21:17 PDT 2002
[netwalk.git] / textbox.e
blob389293f189c5055e488aeb73a5cc2f651d3fc8c5
1 class TEXTBOX
2 inherit
3 WIDGET
4 creation make
5 feature
6 string : STRING
8 put_string(s : STRING) is
9 do
10 !!string.copy(s)
11 cursor := string.count + 1
12 update_image
13 end
15 cursor : INTEGER
17 text_insert(c : CHARACTER) is
19 string.insert_character(c, cursor)
20 raise_signal(signal_change)
21 update_image
22 end
24 text_backspace is
26 if string.count > 0 and then cursor > 1 then
27 cursor := cursor - 1
28 string.remove(cursor)
29 raise_signal(signal_change)
30 update_image
31 end
32 end
34 text_delete is
36 if string.count > 0 and then cursor <= string.upper then
37 string.remove(cursor)
38 raise_signal(signal_change)
39 update_image
40 end
41 end
43 is_empty : BOOLEAN is
45 Result := string.is_empty
46 end
48 clear is
50 string.clear
51 cursor := 1
52 update_image
53 end
55 make is
57 widget_init
58 !!string.make(128)
59 cursor := 1
60 !!textimg.make
61 end
63 process_event(e : EVENT) is
65 if e.type = sdl_keydown then
66 process_key(e)
67 end
68 end
70 process_key(e : EVENT) is
71 local
72 k : INTEGER
74 k := e.i1
75 if k < 256 and then is_normal_key(k) then
76 if is_kmod(e.kmod, kmod_shift) then
77 text_insert(shifted_key(k))
78 else
79 text_insert(k.to_character)
80 end
81 cursor := cursor + 1
82 elseif k = sdlk_right then
83 cursor := cursor + 1
84 if cursor > string.count + 1 then
85 cursor := string.count + 1
86 end
87 elseif k = sdlk_left then
88 cursor := cursor - 1
89 if cursor < 1 then
90 cursor := 1
91 end
92 elseif k = sdlk_backspace then
93 text_backspace
94 elseif k = sdlk_delete then
95 text_delete
96 elseif k = sdlk_return then
97 raise_signal(signal_activate)
98 end
99 end
101 shifted_key_table : ARRAY[CHARACTER] is
102 local
103 i : INTEGER
104 once
105 !!Result.make(0, 127)
106 from i := sdlk_a
107 until i > sdlk_z
108 loop
109 Result.put((i - 32).to_character, i)
110 i := i + 1
112 Result.put('~', sdlk_backquote)
113 Result.put('!', sdlk_1)
114 Result.put('@', sdlk_2)
115 Result.put('#', sdlk_3)
116 Result.put('$', sdlk_4)
117 Result.put('%%', sdlk_5)
118 Result.put('^', sdlk_6)
119 Result.put('&', sdlk_7)
120 Result.put('*', sdlk_8)
121 Result.put('(', sdlk_9)
122 Result.put(')', sdlk_0)
123 Result.put('_', sdlk_minus)
124 Result.put('+', sdlk_equals)
125 Result.put('|', sdlk_backslash)
127 Result.put('{', sdlk_leftbracket)
128 Result.put('}', sdlk_rightbracket)
130 Result.put(':', sdlk_semicolon)
131 Result.put('"', sdlk_quote)
133 Result.put('<', sdlk_comma)
134 Result.put('>', sdlk_period)
135 Result.put('?', sdlk_slash)
137 Result.put(' ', sdlk_space)
140 shifted_key(k : INTEGER) : CHARACTER is
142 if shifted_key_table.item(k) /= '%U' then
143 Result := shifted_key_table.item(k)
144 else
145 Result := k.to_character
149 is_normal_key(k : INTEGER) : BOOLEAN is
151 if shifted_key_table.item(k) /= '%U' then
152 Result := True
156 update is
157 local
158 x : INTEGER
160 if has_focus then
161 fill_rect(0, 0, width, height, white)
162 else
163 fill_rect(0, 0, width, height, grey)
165 fill_rect(1, 1, width - 2, height - 2, black)
166 if string.count > 0 then
167 blit(textimg, 2, 2)
169 if cursor = 1 then
170 x := 2
171 else
172 measure_text_size(string.substring(1, cursor - 1))
173 x := 2 + text_width
175 if has_focus then
176 fill_rect(x, 2, 1, height - 4, white)
180 textimg : IMAGE
182 update_image is
184 if textimg.is_connected then
185 textimg.free
187 if string.count > 0 then
188 textimg.render_string(string, font, white)