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