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