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