Mercurial > pidgin.yaz
annotate src/gtkspell.c @ 1808:6a89897c8658
[gaim-migrate @ 1818]
nsanch's patch for zephyr subscriptions. the only changes i made to it were slight style changes, so if there's anything broken, blame him, not me ;)
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 05 May 2001 11:22:31 +0000 |
parents | 60b3fd819cce |
children | f15d449b3167 |
rev | line source |
---|---|
1117 | 1 /* gtkspell - a spell-checking addon for GtkText |
2 * Copyright (c) 2000 Evan Martin. | |
3 * vim: ts=4 sw=4 | |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Lesser General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Lesser General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Lesser General Public | |
15 * License along with this library; if not, write to the Free Software | |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 */ | |
18 | |
19 #include <gtk/gtk.h> | |
20 | |
21 #include <sys/types.h> | |
22 #include <sys/wait.h> | |
23 #include <sys/time.h> | |
24 #include <unistd.h> | |
25 #include <stdio.h> | |
26 #include <signal.h> | |
27 #include <ctype.h> | |
28 #include <string.h> | |
29 #include <stdlib.h> | |
30 #include <errno.h> | |
31 | |
32 /* TODO: | |
33 * handle dictionary changes | |
34 * asynchronous lookups | |
35 */ | |
36 | |
37 /* size of the text buffer used in various word-processing routines. */ | |
38 #define BUFSIZE 1024 | |
39 /* number of suggestions to display on each menu. */ | |
40 #define MENUCOUNT 10 | |
41 #define BUGEMAIL "gtkspell-devel@lists.sourceforge.net" | |
42 | |
43 /* because we keep only one copy of the spell program running, | |
44 * all ispell-related variables can be static. | |
45 */ | |
46 static pid_t spell_pid = -1; | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
47 static int fd_write[2] = {0}, fd_read[2] = {0}; |
1117 | 48 static int signal_set_up = 0; |
49 | |
50 /* FIXME? */ | |
51 static GdkColor highlight = { 0, 255*256, 0, 0 }; | |
52 | |
53 static void entry_insert_cb(GtkText *gtktext, | |
54 gchar *newtext, guint len, guint *ppos, gpointer d); | |
55 static void set_up_signal(); | |
56 | |
57 int gtkspell_running() { | |
58 return (spell_pid > 0); | |
59 } | |
60 | |
1794
60b3fd819cce
[gaim-migrate @ 1804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1496
diff
changeset
|
61 /* |
1117 | 62 static void error_print(const char *fmt, ...) { |
63 va_list ap; | |
64 va_start(ap, fmt); | |
65 fprintf(stderr, "gtkspell: "); | |
66 vfprintf(stderr, fmt, ap); | |
67 va_end(ap); | |
68 } | |
1794
60b3fd819cce
[gaim-migrate @ 1804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1496
diff
changeset
|
69 */ |
60b3fd819cce
[gaim-migrate @ 1804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1496
diff
changeset
|
70 #define error_print debug_printf |
1117 | 71 |
72 /* functions to interface with pipe */ | |
73 static void writetext(char *text) { | |
74 write(fd_write[1], text, strlen(text)); | |
75 } | |
76 static int readpipe(char *buf, int bufsize) { | |
77 int len; | |
78 len = read(fd_read[0], buf, bufsize-1); | |
79 if (len < 0) { | |
80 error_print("read: %s\n", strerror(errno)); | |
81 return -1; | |
82 } else if (len == 0) { | |
83 error_print("pipe closed.\n"); | |
84 return -1; | |
85 } else if (len == bufsize-1) { | |
86 error_print("buffer overflowed?\n"); | |
87 } | |
88 | |
89 buf[len] = 0; | |
90 return len; | |
91 } | |
92 static int readline(char *buf) { | |
93 return readpipe(buf, BUFSIZE); | |
94 } | |
95 | |
96 static int readresponse(char *buf) { | |
97 int len; | |
98 len = readpipe(buf, BUFSIZE); | |
99 | |
100 /* all ispell responses of any reasonable length should end in \n\n. | |
101 * depending on the speed of the spell checker, this may require more | |
102 * reading. */ | |
103 if (len >= 2 && (buf[len-1] != '\n' || buf[len-2] != '\n')) { | |
104 len += readpipe(buf+len, BUFSIZE-len); | |
105 } | |
106 | |
107 /* now we can remove all of the the trailing newlines. */ | |
108 while (len > 0 && buf[len-1] == '\n') | |
109 buf[--len] = 0; | |
110 | |
111 return len; | |
112 } | |
113 | |
114 | |
115 void gtkspell_stop() { | |
116 if (gtkspell_running()) { | |
1415
3dfd2a83fb5e
[gaim-migrate @ 1425]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1127
diff
changeset
|
117 kill(spell_pid, SIGHUP); |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
118 spell_pid = 0; |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
119 close(fd_read[0]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
120 close(fd_write[1]); |
1117 | 121 } |
122 } | |
123 | |
124 int gtkspell_start(char *path, char * args[]) { | |
125 int fd_error[2]; | |
126 char buf[BUFSIZE]; | |
127 | |
128 if (gtkspell_running()) { | |
129 error_print("gtkspell_start called while already running.\n"); | |
130 gtkspell_stop(); | |
131 } | |
132 | |
133 if (!signal_set_up) { | |
134 set_up_signal(); | |
135 signal_set_up = 1; | |
136 } | |
137 | |
138 pipe(fd_write); | |
139 pipe(fd_read); | |
140 pipe(fd_error); | |
141 | |
142 spell_pid = fork(); | |
143 if (spell_pid < 0) { | |
144 error_print("fork: %s\n", strerror(errno)); | |
145 return -1; | |
146 } else if (spell_pid == 0) { | |
147 dup2(fd_write[0], 0); | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
148 close(fd_write[0]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
149 close(fd_write[1]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
150 |
1117 | 151 dup2(fd_read[1], 1); |
152 close(fd_read[0]); | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
153 close(fd_read[1]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
154 |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
155 dup2(fd_error[1], 2); |
1117 | 156 close(fd_error[0]); |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
157 close(fd_error[1]); |
1117 | 158 |
159 if (path == NULL) { | |
160 if (execvp(args[0], args) < 0) | |
161 error_print("execvp('%s'): %s\n", args[0], strerror(errno)); | |
162 } else { | |
163 if (execv(path, args) < 0) | |
164 error_print("execv('%s'): %s\n", path, strerror(errno)); | |
165 } | |
166 /* if we get here, we failed. | |
167 * send some text on the pipe to indicate status. | |
168 */ | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
169 write(0, "!", 1); /* stdout _is_ the pipe. */ |
1117 | 170 |
171 _exit(0); | |
172 } else { | |
173 /* there are at least two ways to fail: | |
174 * - the exec() can fail | |
175 * - the exec() can succeed, but the program can dump the help screen | |
176 * we must check for both. | |
177 */ | |
178 fd_set rfds; | |
179 struct timeval tv; | |
180 | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
181 close(fd_write[0]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
182 close(fd_read[1]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
183 |
1117 | 184 FD_ZERO(&rfds); |
185 FD_SET(fd_error[0], &rfds); | |
186 FD_SET(fd_read[0], &rfds); | |
187 tv.tv_sec = 2; | |
188 tv.tv_usec = 0; | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
189 |
1117 | 190 if (select(MAX(fd_error[0], fd_read[0])+1, |
191 &rfds, NULL, NULL, &tv) < 0) { | |
192 /* FIXME: is this needed? */ | |
193 error_print("Timed out waiting for spell command.\n"); | |
194 gtkspell_stop(); | |
195 return -1; | |
196 } | |
197 | |
198 if (FD_ISSET(fd_error[0], &rfds)) { /* stderr readable? */ | |
199 error_print("Spell command printed on stderr -- probably failed.\n"); | |
200 gtkspell_stop(); | |
201 return -1; | |
202 } | |
203 | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
204 /* we're done with stderr, now. */ |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
205 close(fd_error[0]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
206 close(fd_error[1]); |
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
207 |
1117 | 208 /* otherwise, fd_read[0] is set. */ |
209 readline(buf); | |
210 | |
211 /* ispell should print something like this: | |
212 * @(#) International Ispell Version 3.1.20 10/10/95 | |
213 * if it doesn't, it's an error. */ | |
214 if (buf[0] != '@') { | |
215 gtkspell_stop(); | |
216 return -1; | |
217 } | |
218 } | |
219 | |
220 /* put ispell into terse mode. | |
221 * this makes it not respond on correctly spelled words. */ | |
222 sprintf(buf, "!\n"); | |
223 writetext(buf); | |
224 return 0; | |
225 } | |
226 | |
227 static GList* misspelled_suggest(char *word) { | |
228 char buf[BUFSIZE]; | |
229 char *newword; | |
230 GList *l = NULL; | |
231 int count; | |
232 | |
233 sprintf(buf, "^%s\n", word); /* guard against ispell control chars */ | |
234 writetext(buf); | |
235 readresponse(buf); | |
236 | |
237 switch (buf[0]) { /* first char is ispell command. */ | |
238 case 0: /* no response: word is ok. */ | |
239 return NULL; | |
240 case '&': /* misspelled, with suggestions */ | |
241 /* & <orig> <count> <ofs>: <miss>, <miss>, <guess>, ... */ | |
242 strtok(buf, " "); /* & */ | |
243 newword = strtok(NULL, " "); /* orig */ | |
244 l = g_list_append(l, g_strdup(newword)); | |
245 newword = strtok(NULL, " "); /* count */ | |
246 count = atoi(newword); | |
247 strtok(NULL, " "); /* ofs: */ | |
248 | |
249 while ((newword = strtok(NULL, ",")) != NULL) { | |
250 int len = strlen(newword); | |
251 if (newword[len-1] == ' ' || newword[len-1] == '\n') | |
252 newword[len-1] = 0; | |
253 if (count == 0) { | |
254 g_list_append(l, NULL); /* signal the "suggestions" */ | |
255 } | |
256 /* add it to the list, skipping the initial space. */ | |
257 l = g_list_append(l, | |
258 g_strdup(newword[0] == ' ' ? newword+1 : newword)); | |
259 | |
260 count--; | |
261 } | |
262 return l; | |
263 | |
264 case '#': /* misspelled, no suggestions */ | |
1467
7f7857c5036e
[gaim-migrate @ 1477]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1415
diff
changeset
|
265 case '?': /* ispell is guessing. */ |
1117 | 266 /* # <orig> <ofs> */ |
267 strtok(buf, " "); /* & */ | |
268 newword = strtok(NULL, " "); /* orig */ | |
269 l = g_list_append(l, g_strdup(newword)); | |
270 return l; | |
271 default: | |
272 error_print("Unsupported spell command '%c'.\n" | |
273 "This is a bug; mail " BUGEMAIL " about it.\n", buf[0]); | |
1467
7f7857c5036e
[gaim-migrate @ 1477]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1415
diff
changeset
|
274 error_print("Input [%s]\nOutput [%s]\n", word, buf); |
7f7857c5036e
[gaim-migrate @ 1477]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1415
diff
changeset
|
275 |
1117 | 276 } |
277 return NULL; | |
278 } | |
279 | |
280 static int misspelled_test(char *word) { | |
281 char buf[BUFSIZE]; | |
282 sprintf(buf, "^%s\n", word); /* guard against ispell control chars */ | |
283 writetext(buf); | |
284 readresponse(buf); | |
285 | |
286 if (buf[0] == 0) { | |
287 return 0; | |
1467
7f7857c5036e
[gaim-migrate @ 1477]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1415
diff
changeset
|
288 } else if (buf[0] == '&' || buf[0] == '#' || buf[0] == '?') { |
1117 | 289 return 1; |
290 } | |
291 | |
292 error_print("Unsupported spell command '%c'.\n" | |
293 "This is a bug; mail " BUGEMAIL " about it.\n", buf[0]); | |
1467
7f7857c5036e
[gaim-migrate @ 1477]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1415
diff
changeset
|
294 error_print("Input [%s]\nOutput [%s]\n", word, buf); |
1117 | 295 return -1; |
296 } | |
297 | |
298 static gboolean iswordsep(char c) { | |
299 return !isalpha(c) && c != '\''; | |
300 } | |
301 | |
302 static gboolean get_word_from_pos(GtkText* gtktext, int pos, char* buf, | |
303 int *pstart, int *pend) { | |
304 gint start, end; | |
305 | |
306 if (iswordsep(GTK_TEXT_INDEX(gtktext, pos))) return FALSE; | |
307 | |
308 for (start = pos; start >= 0; --start) { | |
309 if (iswordsep(GTK_TEXT_INDEX(gtktext, start))) break; | |
310 } | |
311 start++; | |
312 | |
313 for (end = pos; end <= gtk_text_get_length(gtktext); end++) { | |
314 if (iswordsep(GTK_TEXT_INDEX(gtktext, end))) break; | |
315 } | |
316 | |
317 if (buf) { | |
318 for (pos = start; pos < end; pos++) | |
319 buf[pos-start] = GTK_TEXT_INDEX(gtktext, pos); | |
320 buf[pos-start] = 0; | |
321 } | |
322 | |
323 if (pstart) *pstart = start; | |
324 if (pend) *pend = end; | |
325 | |
326 return TRUE; | |
327 } | |
328 | |
329 static gboolean get_curword(GtkText* gtktext, char* buf, | |
330 int *pstart, int *pend) { | |
331 int pos = gtk_editable_get_position(GTK_EDITABLE(gtktext)); | |
332 return get_word_from_pos(gtktext, pos, buf, pstart, pend); | |
333 } | |
334 | |
335 static void change_color(GtkText *gtktext, | |
336 int start, int end, GdkColor *color) { | |
337 char *newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end); | |
338 gtk_text_freeze(gtktext); | |
339 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext), | |
340 GTK_SIGNAL_FUNC(entry_insert_cb), NULL); | |
341 | |
342 gtk_text_set_point(gtktext, start); | |
343 gtk_text_forward_delete(gtktext, end-start); | |
344 | |
345 if (newtext && end-start > 0) | |
346 gtk_text_insert(gtktext, NULL, color, NULL, newtext, end-start); | |
347 | |
348 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext), | |
349 GTK_SIGNAL_FUNC(entry_insert_cb), NULL); | |
350 gtk_text_thaw(gtktext); | |
1415
3dfd2a83fb5e
[gaim-migrate @ 1425]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1127
diff
changeset
|
351 g_free(newtext); |
1117 | 352 } |
353 | |
354 static gboolean check_at(GtkText *gtktext, int from_pos) { | |
355 int start, end; | |
356 char buf[BUFSIZE]; | |
357 | |
358 if (!get_word_from_pos(gtktext, from_pos, buf, &start, &end)) { | |
359 return FALSE; | |
360 } | |
361 | |
362 if (misspelled_test(buf)) { | |
363 if (highlight.pixel == 0) { | |
364 /* add an entry for the highlight in the color map. */ | |
365 GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext)); | |
366 gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);; | |
367 } | |
368 change_color(gtktext, start, end, &highlight); | |
369 return TRUE; | |
370 } else { | |
371 change_color(gtktext, start, end, | |
372 &(GTK_WIDGET(gtktext)->style->fg[0])); | |
373 return FALSE; | |
374 } | |
375 } | |
376 | |
377 void gtkspell_check_all(GtkText *gtktext) { | |
378 guint origpos; | |
379 guint pos = 0; | |
380 guint len; | |
381 float adj_value; | |
382 | |
383 if (!gtkspell_running()) return; | |
384 | |
385 len = gtk_text_get_length(gtktext); | |
386 | |
387 adj_value = gtktext->vadj->value; | |
388 gtk_text_freeze(gtktext); | |
389 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext)); | |
390 while (pos < len) { | |
391 while (pos < len && iswordsep(GTK_TEXT_INDEX(gtktext, pos))) | |
392 pos++; | |
393 while (pos < len && !iswordsep(GTK_TEXT_INDEX(gtktext, pos))) | |
394 pos++; | |
395 if (pos > 0) | |
396 check_at(gtktext, pos-1); | |
397 } | |
398 gtk_text_thaw(gtktext); | |
399 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos); | |
400 } | |
401 | |
402 static void entry_insert_cb(GtkText *gtktext, | |
403 gchar *newtext, guint len, guint *ppos, gpointer d) { | |
404 int origpos; | |
405 | |
406 if (!gtkspell_running()) return; | |
407 | |
408 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext), | |
409 GTK_SIGNAL_FUNC(entry_insert_cb), | |
410 NULL); | |
411 gtk_text_insert(GTK_TEXT(gtktext), NULL, | |
412 &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len); | |
413 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext), | |
414 GTK_SIGNAL_FUNC(entry_insert_cb), | |
415 NULL); | |
416 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "insert-text"); | |
417 *ppos += len; | |
418 | |
419 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext)); | |
420 | |
421 if (iswordsep(newtext[0])) { | |
422 /* did we just end a word? */ | |
423 if (*ppos >= 2) check_at(gtktext, *ppos-2); | |
424 | |
425 /* did we just split a word? */ | |
426 if (*ppos < gtk_text_get_length(gtktext)) | |
427 check_at(gtktext, *ppos+1); | |
428 } else { | |
429 /* check as they type, *except* if they're typing at the end (the most | |
430 * common case. | |
431 */ | |
432 if (*ppos < gtk_text_get_length(gtktext) && | |
433 !iswordsep(GTK_TEXT_INDEX(gtktext, *ppos))) | |
434 check_at(gtktext, *ppos-1); | |
435 } | |
436 | |
437 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos); | |
1496
d33bf6548543
[gaim-migrate @ 1506]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1467
diff
changeset
|
438 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); |
1117 | 439 } |
440 | |
441 static void entry_delete_cb(GtkText *gtktext, | |
442 gint start, gint end, gpointer d) { | |
443 int origpos; | |
444 | |
445 if (!gtkspell_running()) return; | |
446 | |
447 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext)); | |
448 check_at(gtktext, start-1); | |
449 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos); | |
450 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); | |
451 /* this is to *UNDO* the selection, in case they were holding shift | |
452 * while hitting backspace. */ | |
453 } | |
454 | |
455 static void replace_word(GtkWidget *w, gpointer d) { | |
456 int start, end; | |
457 char *newword; | |
458 char buf[BUFSIZE]; | |
459 | |
460 /* we don't save their position, | |
461 * because the cursor is moved by the click. */ | |
462 | |
463 gtk_text_freeze(GTK_TEXT(d)); | |
464 | |
465 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), &newword); | |
466 get_curword(GTK_TEXT(d), buf, &start, &end); | |
467 | |
468 gtk_text_set_point(GTK_TEXT(d), end); | |
469 gtk_text_backward_delete(GTK_TEXT(d), end-start); | |
470 gtk_text_insert(GTK_TEXT(d), NULL, NULL, NULL, newword, strlen(newword)); | |
471 | |
472 gtk_text_thaw(GTK_TEXT(d)); | |
473 } | |
474 | |
475 static GtkMenu *make_menu(GList *l, GtkText *gtktext) { | |
476 GtkWidget *menu, *item; | |
477 char *caption; | |
478 menu = gtk_menu_new(); { | |
479 caption = g_strdup_printf("Not in dictionary: %s", (char*)l->data); | |
480 item = gtk_menu_item_new_with_label(caption); | |
481 /* I'd like to make it so this item is never selectable, like | |
482 * the menu titles in the GNOME panel... unfortunately, the GNOME | |
483 * panel creates their own custom widget to do this! */ | |
484 gtk_widget_show(item); | |
485 gtk_menu_append(GTK_MENU(menu), item); | |
486 | |
487 item = gtk_menu_item_new(); | |
488 gtk_widget_show(item); | |
489 gtk_menu_append(GTK_MENU(menu), item); | |
490 | |
491 l = l->next; | |
492 if (l == NULL) { | |
493 item = gtk_menu_item_new_with_label("(no suggestions)"); | |
494 gtk_widget_show(item); | |
495 gtk_menu_append(GTK_MENU(menu), item); | |
496 } else { | |
497 GtkWidget *curmenu = menu; | |
498 int count = 0; | |
499 do { | |
500 if (l->data == NULL && l->next != NULL) { | |
501 count = 0; | |
502 curmenu = gtk_menu_new(); | |
503 item = gtk_menu_item_new_with_label("Other Possibilities..."); | |
504 gtk_widget_show(item); | |
505 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu); | |
506 gtk_menu_append(GTK_MENU(curmenu), item); | |
507 l = l->next; | |
508 } else if (count > MENUCOUNT) { | |
509 count -= MENUCOUNT; | |
510 item = gtk_menu_item_new_with_label("More..."); | |
511 gtk_widget_show(item); | |
512 gtk_menu_append(GTK_MENU(curmenu), item); | |
513 curmenu = gtk_menu_new(); | |
514 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu); | |
515 } | |
516 item = gtk_menu_item_new_with_label((char*)l->data); | |
517 gtk_signal_connect(GTK_OBJECT(item), "activate", | |
518 GTK_SIGNAL_FUNC(replace_word), gtktext); | |
519 gtk_widget_show(item); | |
520 gtk_menu_append(GTK_MENU(curmenu), item); | |
521 count++; | |
522 } while ((l = l->next) != NULL); | |
523 } | |
524 } | |
525 return GTK_MENU(menu); | |
526 } | |
527 | |
528 static void popup_menu(GtkText *gtktext, GdkEventButton *eb) { | |
529 char buf[BUFSIZE]; | |
530 GList *list, *l; | |
531 | |
532 get_curword(gtktext, buf, NULL, NULL); | |
533 | |
534 list = misspelled_suggest(buf); | |
535 if (list != NULL) { | |
536 gtk_menu_popup(make_menu(list, gtktext), NULL, NULL, NULL, NULL, | |
537 eb->button, eb->time); | |
538 for (l = list; l != NULL; l = l->next) | |
539 g_free(l->data); | |
540 g_list_free(list); | |
541 } | |
542 } | |
543 | |
544 /* ok, this is pretty wacky: | |
545 * we need to let the right-mouse-click go through, so it moves the cursor, | |
546 * but we *can't* let it go through, because GtkText interprets rightclicks as | |
547 * weird selection modifiers. | |
548 * | |
549 * so what do we do? forge rightclicks as leftclicks, then popup the menu. | |
550 * HACK HACK HACK. | |
551 */ | |
552 static gint button_press_intercept_cb(GtkText *gtktext, GdkEvent *e, gpointer d) { | |
553 GdkEventButton *eb; | |
554 gboolean retval; | |
555 | |
556 if (!gtkspell_running()) return FALSE; | |
557 | |
558 if (e->type != GDK_BUTTON_PRESS) return FALSE; | |
559 eb = (GdkEventButton*) e; | |
560 | |
561 if (eb->button != 3) return FALSE; | |
562 | |
563 /* forge the leftclick */ | |
564 eb->button = 1; | |
565 | |
566 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext), | |
567 GTK_SIGNAL_FUNC(button_press_intercept_cb), d); | |
568 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event", | |
569 e, &retval); | |
570 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext), | |
571 GTK_SIGNAL_FUNC(button_press_intercept_cb), d); | |
572 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event"); | |
573 | |
574 /* now do the menu wackiness */ | |
575 popup_menu(gtktext, eb); | |
576 return TRUE; | |
577 } | |
578 | |
579 void gtkspell_uncheck_all(GtkText *gtktext) { | |
580 int origpos; | |
581 char *text; | |
582 float adj_value; | |
583 | |
584 adj_value = gtktext->vadj->value; | |
585 gtk_text_freeze(gtktext); | |
586 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext)); | |
587 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1); | |
588 gtk_text_set_point(gtktext, 0); | |
589 gtk_text_forward_delete(gtktext, gtk_text_get_length(gtktext)); | |
590 gtk_text_insert(gtktext, NULL, NULL, NULL, text, strlen(text)); | |
591 gtk_text_thaw(gtktext); | |
1794
60b3fd819cce
[gaim-migrate @ 1804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1496
diff
changeset
|
592 g_free(text); |
1117 | 593 |
594 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos); | |
595 gtk_adjustment_set_value(gtktext->vadj, adj_value); | |
596 } | |
597 | |
598 void gtkspell_attach(GtkText *gtktext) { | |
599 gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text", | |
600 GTK_SIGNAL_FUNC(entry_insert_cb), NULL); | |
601 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text", | |
602 GTK_SIGNAL_FUNC(entry_delete_cb), NULL); | |
603 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event", | |
604 GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL); | |
605 } | |
606 | |
607 void gtkspell_detach(GtkText *gtktext) { | |
608 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext), | |
609 GTK_SIGNAL_FUNC(entry_insert_cb), NULL); | |
610 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext), | |
611 GTK_SIGNAL_FUNC(entry_delete_cb), NULL); | |
612 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext), | |
613 GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL); | |
614 | |
615 gtkspell_uncheck_all(gtktext); | |
616 } | |
617 | |
618 static void sigchld(int param) { | |
619 if (gtkspell_running() && | |
620 (waitpid(spell_pid, NULL, WNOHANG) == spell_pid)) { | |
621 spell_pid = 0; | |
622 } else { | |
623 /* a default SIGCHLD handler. | |
624 * what else to do here? */ | |
625 waitpid(-1, NULL, WNOHANG); | |
626 } | |
627 } | |
628 | |
629 static void set_up_signal() { | |
630 struct sigaction sigact; | |
631 memset(&sigact, 0, sizeof(struct sigaction)); | |
632 | |
633 sigact.sa_handler = sigchld; | |
634 sigaction(SIGCHLD, &sigact, NULL); | |
635 } |