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