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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
--- src/fe-text/gui-entry.c.orig Sun Oct 26 13:45:02 2003
+++ src/fe-text/gui-entry.c Sun Dec 14 01:54:28 2003
@@ -68,28 +68,89 @@
g_free(entry);
}
-/* Fixes the cursor position if it at big5_lo .
- Direct: -1 , left shift 1 byte.
- Direct: 0, +1 , right shift 1 byte.
-*/
-static int _fix_big5_pos(unichar *p, int pos, int direct)
-{
- int newpos;
-
- if (term_type != TERM_TYPE_BIG5)
- return pos;
-
- for (newpos = 0; newpos < pos && p[newpos] != 0; ) {
- if (is_big5(p[newpos], p[newpos+1]))
- newpos += 2;
- else
- newpos++;
+/* big5 functions */
+#define big5_width(ch) ((ch)>0xff ? 2:1)
+
+void unichars_to_big5(const unichar *str, char *out)
+{
+ for (; *str != '\0'; str++) {
+ if(*str>0xff)
+ *out++ = (*str>>8)&0xff;
+ *out++ = *str&0xff;
+ }
+ *out = '\0';
+}
+
+int strlen_big5(const unsigned char *str)
+{
+ int len=0;
+
+ if(term_type!=TERM_TYPE_BIG5)
+ return strlen(str);
+ while(*str != '\0') {
+ if(is_big5(str[0],str[1]))
+ str++;
+ len++;
+ str++;
}
+ return len;
+}
+
+void big5_to_unichars(const char *str, unichar *out)
+{
+ const unsigned char *p = (const unsigned char *) str;
+
+ while(*p != '\0')
+ if (is_big5(p[0], p[1])) {
+ *out++ = p[0] << 8 | p[1];
+ p += 2;
+ } else
+ *out++ = *p++;
+ *out = '\0';
+}
+
+/* ----------------------------- */
+
+static int pos2scrpos(GUI_ENTRY_REC *entry, int pos)
+{
+ unichar *p;
+ int xpos=0;
+
+ for (p=entry->text;p-entry->text<pos;p++)
+ if (term_type == TERM_TYPE_BIG5)
+ xpos += big5_width(*p);
+ else if(entry->utf8)
+ xpos += utf8_width(*p);
+ else
+ xpos ++;
+
+ return xpos;
+}
+
+static int scrpos2pos(GUI_ENTRY_REC *entry, int pos)
+{
+ int i;
+ int xpos=0;
- if (newpos != pos)
- pos += direct > 0 ? 1 : -1;
+ for(i=0; entry->text[i]; i++) {
+ int width;
+ unichar *p=entry->text+i;
+
+ if(term_type == TERM_TYPE_BIG5)
+ width = big5_width(*p);
+ else if(entry->utf8)
+ width = utf8_width(*p);
+ else
+ width = 1;
+ if(xpos+width>pos)
+ break;
+ xpos+=width;
+ }
- return pos;
+ if(xpos==pos)
+ return i;
+ else
+ return i-1;
}
/* Fixes the cursor position in screen */
@@ -97,20 +158,23 @@
{
int old_scrstart;
- old_scrstart = entry->scrstart;
- if (entry->pos - entry->scrstart < entry->width-2 - entry->promptlen &&
- entry->pos - entry->scrstart > 0) {
- entry->scrpos = entry->pos - entry->scrstart;
- } else if (entry->pos < entry->width-1 - entry->promptlen) {
+ /* assume prompt len == prompt scrlen */
+ int start=pos2scrpos(entry,entry->scrstart);
+ int now=pos2scrpos(entry,entry->pos);
+
+ old_scrstart = entry->scrstart;
+ if (now-start < entry->width-2-entry->promptlen &&
+ now-start > 0)
+ entry->scrpos=now-start;
+ else if(now < entry->width-1-entry->promptlen) {
entry->scrstart = 0;
- entry->scrpos = entry->pos;
+ entry->scrpos=now;
} else {
- entry->scrpos = (entry->width - entry->promptlen)*2/3;
- entry->scrstart = entry->pos - entry->scrpos;
+ entry->scrstart = scrpos2pos(entry,now-(entry->width - entry->promptlen)*2/3);
+ start=pos2scrpos(entry,entry->scrstart);
+ entry->scrpos=now-start;
}
- entry->scrstart = _fix_big5_pos(entry->text, entry->scrstart, -1);
-
if (old_scrstart != entry->scrstart)
entry->redraw_needed_from = 0;
}
@@ -120,7 +184,9 @@
const unichar *p;
int xpos, end_xpos;
- xpos = entry->xpos + entry->promptlen + pos;
+ xpos = entry->xpos + entry->promptlen +
+ pos2scrpos(entry, pos + entry->scrstart) -
+ pos2scrpos(entry, entry->scrstart);
end_xpos = entry->xpos + entry->width;
if (xpos > end_xpos)
return;
@@ -131,7 +197,15 @@
p = entry->scrstart + pos < entry->text_len ?
entry->text + entry->scrstart + pos : empty_str;
for (; *p != '\0'; p++) {
- xpos += utf8_width(*p);
+ if (entry->hidden)
+ xpos++;
+ else if(term_type == TERM_TYPE_BIG5)
+ xpos += big5_width(*p);
+ else if(entry->utf8)
+ xpos += utf8_width(*p);
+ else
+ xpos++;
+
if (xpos > end_xpos)
break;
@@ -285,8 +359,11 @@
if (entry->utf8)
utf16_to_utf8(entry->text, buf);
else {
- for (i = 0; i <= entry->text_len; i++)
- buf[i] = entry->text[i];
+ if(term_type==TERM_TYPE_BIG5)
+ unichars_to_big5(entry->text, buf);
+ else
+ for (i = 0; i <= entry->text_len; i++)
+ buf[i] = entry->text[i];
}
return buf;
}
@@ -301,7 +378,7 @@
gui_entry_redraw_from(entry, entry->pos);
- len = !entry->utf8 ? strlen(str) : strlen_utf8(str);
+ len = !entry->utf8 ? strlen_big5(str) : strlen_utf8(str);
entry_text_grow(entry, len);
/* make space for the string */
@@ -309,8 +386,13 @@
(entry->text_len-entry->pos + 1) * sizeof(unichar));
if (!entry->utf8) {
- for (i = 0; i < len; i++)
- entry->text[entry->pos+i] = str[i];
+ if (term_type == TERM_TYPE_BIG5) {
+ chr = entry->text[entry->pos+len];
+ big5_to_unichars(str, entry->text+entry->pos);
+ entry->text[entry->pos+len] = chr;
+ } else
+ for (i = 0; i < len; i++)
+ entry->text[entry->pos + i] = str[i];
} else {
chr = entry->text[entry->pos+len];
utf8_to_utf16(str, entry->text+entry->pos);
@@ -361,8 +443,12 @@
if (entry->utf8)
utf16_to_utf8(entry->cutbuffer, buf);
else {
- for (i = 0; i <= entry->cutbuffer_len; i++)
- buf[i] = entry->cutbuffer[i];
+ if(term_type==TERM_TYPE_BIG5) {
+ unichars_to_big5(entry->cutbuffer, buf);
+ } else
+ for (i = 0; i <= entry->cutbuffer_len; i++) {
+ buf[i] = entry->cutbuffer[i];
+ }
}
return buf;
}
@@ -374,24 +460,17 @@
g_return_if_fail(entry != NULL);
for (newpos = gui_entry_get_pos(entry); newpos > pos; size++)
- newpos = _fix_big5_pos(entry->text, newpos - 1, -1);
+ newpos = newpos - 1;
gui_entry_erase(entry, size, update_cutbuffer);
}
void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer)
{
- int newpos;
-
g_return_if_fail(entry != NULL);
if (entry->pos < size)
return;
- /* recount the erase size with big5 charsets */
- for (newpos = entry->pos; newpos > 0 && size > 0; size--)
- newpos = _fix_big5_pos(entry->text, newpos-1, -1);
- size = entry->pos - newpos;
-
if (update_cutbuffer) {
/* put erased text to cutbuffer */
if (entry->cutbuffer == NULL || entry->cutbuffer_len < size) {
@@ -515,24 +594,10 @@
void gui_entry_move_pos(GUI_ENTRY_REC *entry, int pos)
{
- int newpos;
-
g_return_if_fail(entry != NULL);
- /* move cursor with big5 charset */
- newpos = _fix_big5_pos(entry->text, entry->pos, -1);
- if (pos > 0) {
- while (pos > 0 && newpos < entry->text_len) {
- newpos = _fix_big5_pos(entry->text, newpos+1, 1);
- pos--;
- }
- } else {
- while (pos < 0 && newpos > 0) {
- newpos = _fix_big5_pos(entry->text, newpos-1, -1);
- pos++;
- }
- }
- entry->pos = newpos;
+ if (entry->pos+pos >= 0 && entry->pos+pos <= entry->text_len)
+ entry->pos += pos;
gui_entry_fix_cursor(entry);
gui_entry_draw(entry);
|