1 /* gtkaspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin (original code for ispell).
3 * Copyright (c) 2002 Melvin Hadasht.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
21 * Adapted for Sylpheed (Claws) (c) 2001-2002 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001-2002 Melvin Hadasht
24 * Adapted for GNU/aspell (c) 2002 Melvin Hadasht
36 #include <sys/types.h>
49 #include <gtk/gtkoptionmenu.h>
50 #include <gtk/gtkmenu.h>
51 #include <gtk/gtkmenuitem.h>
52 #include <gdk/gdkkeysyms.h>
60 #include "gtkaspell.h"
61 #define ASPELL_FASTMODE 1
62 #define ASPELL_NORMALMODE 2
63 #define ASPELL_BADSPELLERMODE 3
65 #define GTKASPELLWORDSIZE 1024
67 /* size of the text buffer used in various word-processing routines. */
70 /* number of suggestions to display on each menu. */
73 /* 'config' must be defined as a 'AspellConfig *' */
74 #define RETURN_FALSE_IF_CONFIG_ERROR() \
76 if (aspell_config_error_number(config) != 0) { \
77 gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
82 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
83 aspell_config_replace(config, option, value); \
84 RETURN_FALSE_IF_CONFIG_ERROR(); \
87 typedef struct _GtkAspellCheckers {
89 GSList *dictionary_list;
93 typedef struct _Dictionary {
99 typedef struct _GtkAspeller {
100 Dictionary *dictionary;
102 AspellConfig *config;
103 AspellSpeller *checker;
106 typedef void (*ContCheckFunc) (gpointer *gtkaspell);
110 GtkAspeller *gtkaspeller;
111 GtkAspeller *alternate_speller;
112 gchar *dictionary_path;
113 gchar theword[GTKASPELLWORDSIZE];
119 gboolean check_while_typing;
120 gboolean use_alternate;
122 ContCheckFunc continue_check;
124 GtkWidget *config_menu;
125 GtkWidget *popup_config_menu;
127 GtkWidget *replace_entry;
129 gint default_sug_mode;
131 GList *suggestions_list;
137 typedef AspellConfig GtkAspellConfig;
139 /******************************************************************************/
141 static GtkAspellCheckers *gtkaspellcheckers;
143 /* Error message storage */
144 static void gtkaspell_checkers_error_message (gchar *message);
147 static void entry_insert_cb (GtkSText *gtktext,
151 GtkAspell *gtkaspell);
152 static void entry_delete_cb (GtkSText *gtktext,
155 GtkAspell *gtkaspell);
156 static gint button_press_intercept_cb (GtkSText *gtktext,
158 GtkAspell *gtkaspell);
160 /* Checker creation */
161 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
162 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
163 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
164 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
166 /* Checker configuration */
167 static gint set_dictionary (AspellConfig *config,
169 static void set_sug_mode_cb (GtkMenuItem *w,
170 GtkAspell *gtkaspell);
171 static void set_real_sug_mode (GtkAspell *gtkaspell,
172 const char *themode);
174 /* Checker actions */
175 static gboolean check_at (GtkAspell *gtkaspell,
177 static gboolean check_next_prev (GtkAspell *gtkaspell,
179 static GList* misspelled_suggest (GtkAspell *gtkaspell,
181 static void add_word_to_session_cb (GtkWidget *w,
183 static void add_word_to_personal_cb (GtkWidget *w,
185 static void replace_with_create_dialog_cb (GtkWidget *w,
187 static void replace_with_supplied_word_cb (GtkWidget *w,
188 GtkAspell *gtkaspell);
189 static void replace_word_cb (GtkWidget *w,
191 static void replace_real_word (GtkAspell *gtkaspell,
193 static void check_with_alternate_cb (GtkWidget *w,
195 static void use_alternate_dict (GtkAspell *gtkaspell);
196 static void toggle_check_while_typing_cb (GtkWidget *w,
200 static void popup_menu (GtkAspell *gtkaspell,
202 static GtkMenu* make_sug_menu (GtkAspell *gtkaspell);
203 static void populate_submenu (GtkAspell *gtkaspell,
205 static GtkMenu* make_config_menu (GtkAspell *gtkaspell);
206 static void set_menu_pos (GtkMenu *menu,
210 /* Other menu callbacks */
211 static gboolean cancel_menu_cb (GtkMenuShell *w,
213 static void change_dict_cb (GtkWidget *w,
214 GtkAspell *gtkaspell);
215 static void switch_to_alternate_cb (GtkWidget *w,
218 /* Misc. helper functions */
219 static void set_point_continue (GtkAspell *gtkaspell);
220 static void continue_check (gpointer *gtkaspell);
221 static gboolean iswordsep (unsigned char c);
222 static guchar get_text_index_whar (GtkAspell *gtkaspell,
224 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
230 static void allocate_color (GtkAspell *gtkaspell,
232 static void change_color (GtkAspell *gtkaspell,
237 static guchar* convert_to_aspell_encoding (const guchar *encoding);
238 static gint compare_dict (Dictionary *a,
240 static void dictionary_delete (Dictionary *dict);
241 static Dictionary * dictionary_dup (const Dictionary *dict);
242 static void free_suggestions_list (GtkAspell *gtkaspell);
243 static void reset_theword_data (GtkAspell *gtkaspell);
244 static void free_checkers (gpointer elt,
246 static gint find_gtkaspeller (gconstpointer aa,
248 static void gtkaspell_alert_dialog (gchar *message);
249 /* gtkspellconfig - only one config per session */
250 GtkAspellConfig * gtkaspellconfig;
252 /******************************************************************************/
254 void gtkaspell_checkers_init(void)
256 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
257 gtkaspellcheckers->checkers = NULL;
258 gtkaspellcheckers->dictionary_list = NULL;
259 gtkaspellcheckers->error_message = NULL;
262 void gtkaspell_checkers_quit(void)
267 if (gtkaspellcheckers == NULL)
270 if ((checkers = gtkaspellcheckers->checkers)) {
271 debug_print("Aspell: number of running checkers to delete %d\n",
272 g_slist_length(checkers));
274 g_slist_foreach(checkers, free_checkers, NULL);
275 g_slist_free(checkers);
278 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
279 debug_print("Aspell: number of dictionaries to delete %d\n",
280 g_slist_length(dict_list));
282 gtkaspell_free_dictionary_list(dict_list);
283 gtkaspellcheckers->dictionary_list = NULL;
286 g_free(gtkaspellcheckers->error_message);
291 static void gtkaspell_checkers_error_message (gchar *message)
294 if (gtkaspellcheckers->error_message) {
295 tmp = g_strdup_printf("%s\n%s",
296 gtkaspellcheckers->error_message, message);
298 g_free(gtkaspellcheckers->error_message);
299 gtkaspellcheckers->error_message = tmp;
301 gtkaspellcheckers->error_message = message;
304 const char *gtkaspell_checkers_strerror(void)
306 g_return_if_fail(gtkaspellcheckers);
307 return gtkaspellcheckers->error_message;
310 void gtkaspell_checkers_reset_error(void)
312 g_return_if_fail(gtkaspellcheckers);
314 g_free(gtkaspellcheckers->error_message);
316 gtkaspellcheckers->error_message = NULL;
319 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
320 const gchar *dictionary,
321 const gchar *encoding,
322 gint misspelled_color,
323 gboolean check_while_typing,
324 gboolean use_alternate,
328 GtkAspell *gtkaspell;
329 GtkAspeller *gtkaspeller;
331 g_return_val_if_fail(gtktext, NULL);
333 dict = g_new0(Dictionary, 1);
334 dict->fullname = g_strdup(dictionary);
335 dict->encoding = g_strdup(encoding);
337 gtkaspeller = gtkaspeller_new(dict);
338 dictionary_delete(dict);
343 gtkaspell = g_new0(GtkAspell, 1);
345 gtkaspell->dictionary_path = g_strdup(dictionary_path);
347 gtkaspell->gtkaspeller = gtkaspeller;
348 gtkaspell->alternate_speller = NULL;
349 gtkaspell->theword[0] = 0x00;
350 gtkaspell->start_pos = 0;
351 gtkaspell->end_pos = 0;
352 gtkaspell->orig_pos = -1;
353 gtkaspell->end_check_pos = -1;
354 gtkaspell->misspelled = -1;
355 gtkaspell->check_while_typing = check_while_typing;
356 gtkaspell->continue_check = NULL;
357 gtkaspell->config_menu = NULL;
358 gtkaspell->popup_config_menu = NULL;
359 gtkaspell->sug_menu = NULL;
360 gtkaspell->replace_entry = NULL;
361 gtkaspell->gtktext = gtktext;
362 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
363 gtkaspell->max_sug = -1;
364 gtkaspell->suggestions_list = NULL;
365 gtkaspell->use_alternate = use_alternate;
367 allocate_color(gtkaspell, misspelled_color);
369 gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
370 GTK_SIGNAL_FUNC(entry_insert_cb), gtkaspell);
371 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
372 GTK_SIGNAL_FUNC(entry_delete_cb), gtkaspell);
373 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
374 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkaspell);
376 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
381 void gtkaspell_delete(GtkAspell * gtkaspell)
383 GtkSText *gtktext = gtkaspell->gtktext;
385 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
386 GTK_SIGNAL_FUNC(entry_insert_cb),
388 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
389 GTK_SIGNAL_FUNC(entry_delete_cb),
391 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
392 GTK_SIGNAL_FUNC(button_press_intercept_cb),
395 gtkaspell_uncheck_all(gtkaspell);
397 gtkaspeller_delete(gtkaspell->gtkaspeller);
399 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
400 gtkaspeller_delete(gtkaspell->alternate_speller);
402 if (gtkaspell->sug_menu)
403 gtk_widget_destroy(gtkaspell->sug_menu);
405 if (gtkaspell->popup_config_menu)
406 gtk_widget_destroy(gtkaspell->popup_config_menu);
408 if (gtkaspell->config_menu)
409 gtk_widget_destroy(gtkaspell->config_menu);
411 if (gtkaspell->suggestions_list)
412 free_suggestions_list(gtkaspell);
414 g_free((gchar *)gtkaspell->dictionary_path);
416 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
423 static void entry_insert_cb(GtkSText *gtktext,
427 GtkAspell *gtkaspell)
431 g_return_if_fail(gtkaspell->gtkaspeller->checker);
433 if (!gtkaspell->check_while_typing)
436 /* We must insert ourselves the character so the
437 * color of the inserted character is the default color.
438 * Never mess with set_insertion when frozen.
441 gtk_stext_freeze(gtktext);
442 if (MB_CUR_MAX > 1) {
444 Xstrndup_a(str, newtext, len, return);
445 wlen = mbstowcs(NULL, str, 0);
451 gtk_stext_backward_delete(GTK_STEXT(gtktext), wlen);
452 gtk_stext_insert(GTK_STEXT(gtktext), NULL, NULL, NULL, newtext, len);
453 *ppos = gtk_stext_get_point(GTK_STEXT(gtktext));
455 if (iswordsep(newtext[0])) {
456 /* did we just end a word? */
458 check_at(gtkaspell, *ppos - 2);
460 /* did we just split a word? */
461 if (*ppos < gtk_stext_get_length(gtktext))
462 check_at(gtkaspell, *ppos + 1);
464 /* check as they type, *except* if they're typing at the end (the most
467 if (*ppos < gtk_stext_get_length(gtktext) &&
468 !iswordsep(get_text_index_whar(gtkaspell, *ppos))) {
469 check_at(gtkaspell, *ppos - 1);
473 gtk_stext_thaw(gtktext);
474 gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
477 static void entry_delete_cb(GtkSText *gtktext,
480 GtkAspell *gtkaspell)
484 g_return_if_fail(gtkaspell->gtkaspeller->checker);
486 if (!gtkaspell->check_while_typing)
489 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
491 check_at(gtkaspell, start - 1);
492 check_at(gtkaspell, start);
495 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
496 gtk_stext_set_point(gtktext, origpos);
497 /* this is to *UNDO* the selection, in case they were holding shift
498 * while hitting backspace. */
499 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
502 /* ok, this is pretty wacky:
503 * we need to let the right-mouse-click go through, so it moves the cursor,
504 * but we *can't* let it go through, because GtkText interprets rightclicks as
505 * weird selection modifiers.
507 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
510 static gint button_press_intercept_cb(GtkSText *gtktext, GdkEvent *e, GtkAspell *gtkaspell)
515 g_return_val_if_fail(gtkaspell->gtkaspeller->checker, FALSE);
517 if (e->type != GDK_BUTTON_PRESS)
519 eb = (GdkEventButton*) e;
524 /* forge the leftclick */
527 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
528 GTK_SIGNAL_FUNC(button_press_intercept_cb),
530 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
532 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
533 GTK_SIGNAL_FUNC(button_press_intercept_cb),
535 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
537 /* now do the menu wackiness */
538 popup_menu(gtkaspell, eb);
539 gtk_grab_remove(GTK_WIDGET(gtktext));
543 /* Checker creation */
544 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
547 GtkAspeller *gtkaspeller = NULL;
551 g_return_val_if_fail(gtkaspellcheckers, NULL);
553 g_return_val_if_fail(dictionary, NULL);
555 if (dictionary->fullname == NULL)
556 gtkaspell_checkers_error_message(g_strdup(_("No dictionary selected.")));
558 g_return_val_if_fail(dictionary->fullname, NULL);
560 if (dictionary->dictname == NULL) {
563 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
566 dictionary->dictname = dictionary->fullname;
568 dictionary->dictname = tmp + 1;
571 dict = dictionary_dup(dictionary);
573 tmp = g_new0(GtkAspeller, 1);
574 tmp->dictionary = dict;
576 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
581 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
582 gtkaspellcheckers->checkers = g_slist_append(
583 gtkaspellcheckers->checkers,
586 debug_print("Aspell: Created a new gtkaspeller %0x\n",
589 dictionary_delete(dict);
591 debug_print("Aspell: Could not create spell checker.\n");
594 debug_print("Aspell: number of existing checkers %d\n",
595 g_slist_length(gtkaspellcheckers->checkers));
600 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
602 GtkAspeller *gtkaspeller;
603 AspellConfig *config;
604 AspellCanHaveError *ret;
606 g_return_val_if_fail(gtkaspellcheckers, NULL);
607 g_return_val_if_fail(dict, NULL);
609 gtkaspeller = g_new(GtkAspeller, 1);
611 gtkaspeller->dictionary = dict;
612 gtkaspeller->sug_mode = ASPELL_FASTMODE;
614 config = new_aspell_config();
616 if (!set_dictionary(config, dict))
619 ret = new_aspell_speller(config);
620 delete_aspell_config(config);
622 if (aspell_error_number(ret) != 0) {
623 gtkaspellcheckers->error_message = g_strdup(aspell_error_message(ret));
625 delete_aspell_can_have_error(ret);
630 gtkaspeller->checker = to_aspell_speller(ret);
631 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
636 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
638 g_return_val_if_fail(gtkaspellcheckers, NULL);
640 gtkaspellcheckers->checkers =
641 g_slist_remove(gtkaspellcheckers->checkers,
644 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
647 gtkaspeller_real_delete(gtkaspeller);
649 debug_print("Aspell: number of existing checkers %d\n",
650 g_slist_length(gtkaspellcheckers->checkers));
655 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
657 g_return_val_if_fail(gtkaspeller, NULL);
658 g_return_val_if_fail(gtkaspeller->checker, NULL);
660 aspell_speller_save_all_word_lists(gtkaspeller->checker);
662 delete_aspell_speller(gtkaspeller->checker);
664 dictionary_delete(gtkaspeller->dictionary);
666 debug_print("Aspell: gtkaspeller %0x deleted.\n",
674 /*****************************************************************************/
675 /* Checker configuration */
677 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
679 gchar *language = NULL;
680 gchar *jargon = NULL;
684 g_return_val_if_fail(config, FALSE);
685 g_return_val_if_fail(dict, FALSE);
687 strncpy(buf, dict->fullname, BUFSIZE-1);
688 buf[BUFSIZE-1] = 0x00;
690 buf[dict->dictname - dict->fullname] = 0x00;
692 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
693 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
695 strncpy(buf, dict->dictname, BUFSIZE-1);
698 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
703 if ((jargon = strchr(language, '-')) != NULL)
706 if (size != NULL && jargon == size)
709 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
710 language, jargon, size);
713 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
715 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
717 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
718 if (dict->encoding) {
721 aspell_enc = convert_to_aspell_encoding (dict->encoding);
722 aspell_config_replace(config, "encoding", (const char *) aspell_enc);
725 RETURN_FALSE_IF_CONFIG_ERROR();
731 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
734 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
735 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
737 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
740 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
745 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
746 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
748 dict = gtkaspell->gtkaspeller->dictionary;
749 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
754 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
755 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
759 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
761 set_real_sug_mode(gtkaspell, themode);
763 if (gtkaspell->config_menu)
764 populate_submenu(gtkaspell, gtkaspell->config_menu);
767 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
770 gint mode = ASPELL_FASTMODE;
771 g_return_if_fail(gtkaspell);
772 g_return_if_fail(gtkaspell->gtkaspeller);
773 g_return_if_fail(themode);
775 if (!strcmp(themode,_("Normal Mode")))
776 mode = ASPELL_NORMALMODE;
777 else if (!strcmp( themode,_("Bad Spellers Mode")))
778 mode = ASPELL_BADSPELLERMODE;
780 result = gtkaspell_set_sug_mode(gtkaspell, mode);
783 debug_print("Aspell: error while changing suggestion mode:%s\n",
784 gtkaspellcheckers->error_message);
785 gtkaspell_checkers_reset_error();
789 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
790 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
792 AspellConfig *config;
794 g_return_val_if_fail(gtkaspell, FALSE);
795 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
796 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
798 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
799 (guint) gtkaspell->gtkaspeller, themode);
801 config = gtkaspell->gtkaspeller->config;
804 case ASPELL_FASTMODE:
805 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
807 case ASPELL_NORMALMODE:
808 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
810 case ASPELL_BADSPELLERMODE:
811 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
815 gtkaspellcheckers->error_message =
816 g_strdup(_("Unknown suggestion mode."));
820 gtkaspell->gtkaspeller->sug_mode = themode;
821 gtkaspell->default_sug_mode = themode;
826 /* misspelled_suggest() - Create a suggestion list for word */
827 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
829 const guchar *newword;
831 const AspellWordList *suggestions;
832 AspellStringEnumeration *elements;
834 g_return_val_if_fail(word, NULL);
836 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
837 free_suggestions_list(gtkaspell);
839 suggestions = aspell_speller_suggest(gtkaspell->gtkaspeller->checker,
840 (const char *)word, -1);
841 elements = aspell_word_list_elements(suggestions);
842 list = g_list_append(list, g_strdup(word));
844 while ((newword = aspell_string_enumeration_next(elements)) != NULL)
845 list = g_list_append(list, g_strdup(newword));
847 gtkaspell->max_sug = g_list_length(list) - 1;
848 gtkaspell->suggestions_list = list;
853 free_suggestions_list(gtkaspell);
858 /* misspelled_test() - Just test if word is correctly spelled */
859 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
861 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1) ? 0 : 1;
865 static gboolean iswordsep(unsigned char c)
867 return !isalpha(c) && c != '\'';
870 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
875 text = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->gtktext), pos,
887 /* get_word_from_pos () - return the word pointed to. */
888 /* Handles correctly the quotes. */
889 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
890 unsigned char* buf, gint buflen,
891 gint *pstart, gint *pend)
894 /* TODO : when correcting a word into quotes, change the color of */
895 /* the quotes too, as may be they were highlighted before. To do */
896 /* this, we can use two others pointers that points to the whole */
897 /* word including quotes. */
905 gtktext = gtkaspell->gtktext;
906 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
909 /* The apostrophe character is somtimes used for quotes
910 * So include it in the word only if it is not surrounded
911 * by other characters.
914 for (start = pos; start >= 0; --start) {
915 c = get_text_index_whar(gtkaspell, start);
918 if (!isalpha(get_text_index_whar(gtkaspell,
920 /* start_quote = TRUE; */
925 /* start_quote = TRUE; */
929 else if (!isalpha(c))
935 for (end = pos; end < gtk_stext_get_length(gtktext); end++) {
936 c = get_text_index_whar(gtkaspell, end);
938 if (end < gtk_stext_get_length(gtktext)) {
939 if (!isalpha(get_text_index_whar(gtkaspell,
941 /* end_quote = TRUE; */
946 /* end_quote = TRUE; */
960 if (end - start < buflen) {
961 for (pos = start; pos < end; pos++)
963 get_text_index_whar(gtkaspell, pos);
964 buf[pos - start] = 0;
972 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
975 unsigned char buf[GTKASPELLWORDSIZE];
978 g_return_val_if_fail(from_pos >= 0, FALSE);
980 gtktext = gtkaspell->gtktext;
982 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
986 if (misspelled_test(gtkaspell, buf)) {
987 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
988 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
989 gtkaspell->start_pos = start;
990 gtkaspell->end_pos = end;
991 free_suggestions_list(gtkaspell);
993 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
996 change_color(gtkaspell, start, end, buf, NULL);
1001 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1007 gboolean misspelled;
1010 maxpos = gtkaspell->end_check_pos;
1018 pos = gtk_editable_get_position(GTK_EDITABLE(gtkaspell->gtktext));
1019 gtkaspell->orig_pos = pos;
1020 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1021 pos > minpos && pos <= maxpos)
1023 while (!(misspelled = check_at(gtkaspell, pos)) &&
1024 pos > minpos && pos <= maxpos) {
1026 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1027 pos > minpos && pos <= maxpos)
1030 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1031 pos > minpos && pos <= maxpos)
1035 misspelled_suggest(gtkaspell, gtkaspell->theword);
1038 gtkaspell->orig_pos = gtkaspell->end_pos;
1040 gtk_stext_set_point(GTK_STEXT(gtkaspell->gtktext),
1041 gtkaspell->end_pos);
1042 gtk_editable_set_position(GTK_EDITABLE(gtkaspell->gtktext),
1043 gtkaspell->end_pos);
1044 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1045 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1047 reset_theword_data(gtkaspell);
1049 gtkaspell_alert_dialog(_("No misspelled word found."));
1050 gtk_stext_set_point(GTK_STEXT(gtkaspell->gtktext),
1051 gtkaspell->orig_pos);
1052 gtk_editable_set_position(GTK_EDITABLE(gtkaspell->gtktext),
1053 gtkaspell->orig_pos);
1060 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1062 gtkaspell->continue_check = NULL;
1063 gtkaspell->end_check_pos =
1064 gtk_stext_get_length(GTK_STEXT(gtkaspell->gtktext));
1065 check_next_prev(gtkaspell, FALSE);
1068 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1071 gtkaspell->continue_check = NULL;
1072 gtkaspell->end_check_pos
1073 = gtk_stext_get_length(GTK_STEXT(gtkaspell->gtktext));
1074 check_next_prev(gtkaspell, TRUE);
1077 void gtkaspell_check_all(GtkAspell *gtkaspell)
1082 g_return_if_fail(gtkaspell);
1083 g_return_if_fail(gtkaspell->gtktext);
1085 gtktext = (GtkWidget *) gtkaspell->gtktext;
1088 end = gtk_stext_get_length(GTK_STEXT(gtktext));
1090 if (GTK_EDITABLE(gtktext)->has_selection) {
1091 start = GTK_EDITABLE(gtktext)->selection_start_pos;
1092 end = GTK_EDITABLE(gtktext)->selection_end_pos;
1104 gtk_editable_set_position(GTK_EDITABLE(gtktext), start);
1105 gtk_stext_set_point(GTK_STEXT(gtktext), start);
1107 gtkaspell->continue_check = continue_check;
1108 gtkaspell->end_check_pos = end;
1110 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1114 static void continue_check(gpointer *data)
1116 GtkAspell *gtkaspell = (GtkAspell *) data;
1117 gint pos = gtk_editable_get_position(GTK_EDITABLE(gtkaspell->gtktext));
1118 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1119 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1121 gtkaspell->continue_check = NULL;
1125 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1133 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1135 gtktext = gtkaspell->gtktext;
1137 adj_value = gtktext->vadj->value;
1139 len = gtk_stext_get_length(gtktext);
1141 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1145 iswordsep(get_text_index_whar(gtkaspell, pos)))
1148 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1151 check_at(gtkaspell, pos - 1);
1153 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1154 gtk_stext_set_point(GTK_STEXT(gtktext), origpos);
1155 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1158 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1160 unsigned char *newword;
1161 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1163 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1166 if (strcmp(newword, gtkaspell->theword)) {
1167 replace_real_word(gtkaspell, newword);
1169 if ((e->type == GDK_KEY_PRESS &&
1170 ((GdkEventKey *) e)->state & GDK_MOD1_MASK)) {
1171 aspell_speller_store_replacement(gtkaspell->gtkaspeller->checker,
1172 gtkaspell->theword, -1,
1175 gtkaspell->replace_entry = NULL;
1180 set_point_continue(gtkaspell);
1184 static void replace_word_cb(GtkWidget *w, gpointer data)
1186 unsigned char *newword;
1187 GtkAspell *gtkaspell = (GtkAspell *) data;
1188 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1190 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
1192 replace_real_word(gtkaspell, newword);
1194 if ((e->type == GDK_KEY_PRESS &&
1195 ((GdkEventKey *) e)->state & GDK_MOD1_MASK) ||
1196 (e->type == GDK_BUTTON_RELEASE &&
1197 ((GdkEventButton *) e)->state & GDK_MOD1_MASK)) {
1198 aspell_speller_store_replacement(gtkaspell->gtkaspeller->checker,
1199 gtkaspell->theword, -1,
1203 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1205 set_point_continue(gtkaspell);
1208 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1210 int oldlen, newlen, wordlen;
1213 gint start = gtkaspell->start_pos;
1216 if (!newword) return;
1218 gtktext = gtkaspell->gtktext;
1220 gtk_stext_freeze(GTK_STEXT(gtktext));
1221 origpos = gtkaspell->orig_pos;
1223 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1224 wordlen = strlen(gtkaspell->theword);
1226 newlen = strlen(newword); /* FIXME: multybyte characters? */
1228 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1229 GTK_SIGNAL_FUNC(entry_insert_cb),
1231 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1232 GTK_SIGNAL_FUNC(entry_delete_cb),
1235 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "delete-text",
1236 gtkaspell->start_pos, gtkaspell->end_pos);
1237 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "insert-text",
1238 newword, newlen, &start);
1240 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1241 GTK_SIGNAL_FUNC(entry_insert_cb),
1243 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1244 GTK_SIGNAL_FUNC(entry_delete_cb),
1247 /* Put the point and the position where we clicked with the mouse
1248 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1249 * to let it update correctly the word insertion and then the
1250 * point & position position. If not, SEGV after the first replacement
1251 * If the new word ends before point, put the point at its end.
1254 if (origpos - gtkaspell->start_pos < oldlen &&
1255 origpos - gtkaspell->start_pos >= 0) {
1256 /* Original point was in the word.
1257 * Let it there unless point is going to be outside of the word
1259 if (origpos - gtkaspell->start_pos >= newlen) {
1260 pos = gtkaspell->start_pos + newlen;
1263 else if (origpos >= gtkaspell->end_pos) {
1264 /* move the position according to the change of length */
1265 pos = origpos + newlen - oldlen;
1268 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1270 gtk_stext_thaw(GTK_STEXT(gtktext));
1271 gtk_stext_freeze(GTK_STEXT(gtktext));
1273 if (GTK_STEXT(gtktext)->text_len < pos)
1274 pos = gtk_stext_get_length(GTK_STEXT(gtktext));
1276 gtkaspell->orig_pos = pos;
1278 gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkaspell->orig_pos);
1279 gtk_stext_set_point(GTK_STEXT(gtktext),
1280 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
1282 gtk_stext_thaw(GTK_STEXT(gtktext));
1285 /* Accept this word for this session */
1286 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1290 GtkAspell *gtkaspell = (GtkAspell *) data;
1291 gtktext = gtkaspell->gtktext;
1293 gtk_stext_freeze(GTK_STEXT(gtktext));
1295 pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1297 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1299 strlen(gtkaspell->theword));
1301 check_at(gtkaspell, gtkaspell->start_pos);
1303 gtk_stext_thaw(gtkaspell->gtktext);
1305 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1307 set_point_continue(gtkaspell);
1310 /* add_word_to_personal_cb() - add word to personal dict. */
1311 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1313 GtkAspell *gtkaspell = (GtkAspell *) data;
1314 GtkSText *gtktext = gtkaspell->gtktext;
1316 gtk_stext_freeze(GTK_STEXT(gtktext));
1318 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1320 strlen(gtkaspell->theword));
1322 check_at(gtkaspell, gtkaspell->start_pos);
1324 gtk_stext_thaw(gtkaspell->gtktext);
1326 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1327 set_point_continue(gtkaspell);
1330 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1332 GtkAspell *gtkaspell = (GtkAspell *) data;
1335 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1337 use_alternate_dict(gtkaspell);
1338 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1340 if (!gtkaspell->continue_check) {
1342 gtkaspell->misspelled = misspelled;
1344 if (gtkaspell->misspelled) {
1346 misspelled_suggest(gtkaspell, gtkaspell->theword);
1348 gtk_stext_set_point(GTK_STEXT(gtkaspell->gtktext),
1349 gtkaspell->end_pos);
1350 gtk_editable_set_position(GTK_EDITABLE(gtkaspell->gtktext),
1351 gtkaspell->end_pos);
1353 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1354 set_menu_pos, gtkaspell, 0,
1359 gtkaspell->orig_pos = gtkaspell->start_pos;
1361 set_point_continue(gtkaspell);
1364 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1370 GtkWidget *ok_button;
1371 GtkWidget *cancel_button;
1374 GtkAspell *gtkaspell = (GtkAspell *) data;
1376 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1378 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1380 dialog = gtk_dialog_new();
1382 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
1383 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1384 gtk_widget_set_uposition(dialog, xx, yy);
1386 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
1387 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1388 GTK_OBJECT(dialog));
1390 hbox = gtk_hbox_new(FALSE, 0);
1391 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
1393 thelabel = g_strdup_printf(_("Replace \"%s\" with: "),
1394 gtkaspell->theword);
1395 label = gtk_label_new(thelabel);
1397 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1399 entry = gtk_entry_new();
1400 gtkaspell->replace_entry = entry;
1401 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1402 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1403 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1404 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1406 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1407 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1408 GTK_OBJECT(dialog));
1409 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1411 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE,
1413 label = gtk_label_new(_("Holding down MOD1 key while pressing "
1414 "Enter\nwill learn from mistake.\n"));
1415 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1416 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1417 gtk_misc_set_padding(GTK_MISC(label), 8, 0);
1418 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label,
1421 hbox = gtk_hbox_new(TRUE, 0);
1423 ok_button = gtk_button_new_with_label(_("OK"));
1424 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
1425 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
1426 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1428 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
1429 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1430 GTK_OBJECT(dialog));
1432 cancel_button = gtk_button_new_with_label(_("Cancel"));
1433 gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
1434 gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked",
1435 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1436 GTK_OBJECT(dialog));
1438 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1440 gtk_widget_grab_focus(entry);
1442 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1444 gtk_widget_show_all(dialog);
1447 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1454 gtktext = gtkaspell->gtktext;
1456 adj_value = gtktext->vadj->value;
1458 gtk_stext_freeze(gtktext);
1460 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1462 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1464 gtk_stext_set_point(gtktext, 0);
1465 gtk_stext_forward_delete(gtktext, gtk_stext_get_length(gtktext));
1466 gtk_stext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1468 gtk_stext_thaw(gtktext);
1470 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1471 gtk_stext_set_point(gtktext, origpos);
1472 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1478 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1480 GtkAspell *gtkaspell = (GtkAspell *) data;
1482 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1484 if (!gtkaspell->check_while_typing)
1485 gtkaspell_uncheck_all(gtkaspell);
1487 if (gtkaspell->config_menu)
1488 populate_submenu(gtkaspell, gtkaspell->config_menu);
1491 static GSList *create_empty_dictionary_list(void)
1493 GSList *list = NULL;
1496 dict = g_new0(Dictionary, 1);
1497 dict->fullname = g_strdup(_("None"));
1498 dict->dictname = dict->fullname;
1499 dict->encoding = NULL;
1501 return g_slist_append(list, dict);
1504 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1505 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1509 AspellConfig *config;
1510 AspellDictInfoList *dlist;
1511 AspellDictInfoEnumeration *dels;
1512 const AspellDictInfo *entry;
1514 if (!gtkaspellcheckers)
1515 gtkaspell_checkers_init();
1517 if (gtkaspellcheckers->dictionary_list && !refresh)
1518 return gtkaspellcheckers->dictionary_list;
1520 gtkaspell_free_dictionary_list(gtkaspellcheckers->dictionary_list);
1523 config = new_aspell_config();
1525 aspell_config_replace(config, "rem-all-word-list-path", "");
1526 if (aspell_config_error_number(config) != 0) {
1527 gtkaspellcheckers->error_message = g_strdup(
1528 aspell_config_error_message(config));
1529 gtkaspellcheckers->dictionary_list =
1530 create_empty_dictionary_list();
1532 return gtkaspellcheckers->dictionary_list;
1535 aspell_config_replace(config, "dict-dir", aspell_path);
1536 if (aspell_config_error_number(config) != 0) {
1537 gtkaspellcheckers->error_message = g_strdup(
1538 aspell_config_error_message(config));
1539 gtkaspellcheckers->dictionary_list =
1540 create_empty_dictionary_list();
1542 return gtkaspellcheckers->dictionary_list;
1545 dlist = get_aspell_dict_info_list(config);
1546 delete_aspell_config(config);
1548 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1549 dels = aspell_dict_info_list_elements(dlist);
1550 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1552 dict = g_new0(Dictionary, 1);
1553 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1555 dict->dictname = dict->fullname + strlen(aspell_path);
1556 dict->encoding = g_strdup(entry->code);
1557 debug_print("Aspell: found dictionary %s %s\n", dict->fullname,
1559 list = g_slist_insert_sorted(list, dict,
1560 (GCompareFunc) compare_dict);
1563 delete_aspell_dict_info_enumeration(dels);
1567 debug_print("Aspell: error when searching for dictionaries: "
1568 "No dictionary found.\n");
1569 list = create_empty_dictionary_list();
1572 gtkaspellcheckers->dictionary_list = list;
1577 void gtkaspell_free_dictionary_list(GSList *list)
1581 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1583 dict = (Dictionary *) walk->data;
1584 dictionary_delete(dict);
1589 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1591 GSList *dict_list, *tmp;
1596 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1597 g_return_val_if_fail(dict_list, NULL);
1599 menu = gtk_menu_new();
1601 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1602 dict = (Dictionary *) tmp->data;
1603 item = gtk_menu_item_new_with_label(dict->dictname);
1604 gtk_object_set_data(GTK_OBJECT(item), "dict_name",
1607 gtk_menu_append(GTK_MENU(menu), item);
1608 gtk_widget_show(item);
1611 gtk_widget_show(menu);
1616 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1618 GtkWidget *menuitem;
1619 gchar *dict_fullname;
1622 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1624 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1625 dict_fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(menuitem),
1627 g_return_val_if_fail(dict_fullname, NULL);
1629 label = g_strdup(dict_fullname);
1635 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1640 menu = gtk_menu_new();
1641 gtk_widget_show(menu);
1643 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1644 gtk_widget_show(item);
1645 gtk_menu_append(GTK_MENU(menu), item);
1646 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(ASPELL_FASTMODE));
1648 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1649 gtk_widget_show(item);
1650 gtk_menu_append(GTK_MENU(menu), item);
1651 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(ASPELL_NORMALMODE));
1653 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1654 gtk_widget_show(item);
1655 gtk_menu_append(GTK_MENU(menu), item);
1656 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1661 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1663 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1665 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1666 sugmode == ASPELL_NORMALMODE ||
1667 sugmode == ASPELL_BADSPELLERMODE);
1669 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1672 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1677 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1679 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1681 sugmode = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
1687 static void use_alternate_dict(GtkAspell *gtkaspell)
1691 tmp = gtkaspell->gtkaspeller;
1692 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1693 gtkaspell->alternate_speller = tmp;
1695 if (gtkaspell->config_menu)
1696 populate_submenu(gtkaspell, gtkaspell->config_menu);
1699 static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb)
1703 gtktext = gtkaspell->gtktext;
1704 gtkaspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1706 if (!(eb->state & GDK_SHIFT_MASK)) {
1707 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
1709 gtk_editable_set_position(GTK_EDITABLE(gtktext),
1710 gtkaspell->orig_pos);
1711 gtk_stext_set_point(gtktext, gtkaspell->orig_pos);
1713 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
1714 gtk_menu_popup(make_sug_menu(gtkaspell),
1715 NULL, NULL, NULL, NULL,
1716 eb->button, GDK_CURRENT_TIME);
1721 gtk_editable_set_position(GTK_EDITABLE(gtktext),
1722 gtkaspell->orig_pos);
1723 gtk_stext_set_point(gtktext, gtkaspell->orig_pos);
1727 gtk_menu_popup(make_config_menu(gtkaspell), NULL, NULL, NULL, NULL,
1728 eb->button, GDK_CURRENT_TIME);
1731 /* make_sug_menu() - Add menus to accept this word for this session
1732 * and to add it to personal dictionary
1734 static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
1736 GtkWidget *menu, *item;
1737 unsigned char *caption;
1739 GtkAccelGroup *accel;
1740 GList *l = gtkaspell->suggestions_list;
1742 gtktext = gtkaspell->gtktext;
1744 accel = gtk_accel_group_new();
1745 menu = gtk_menu_new();
1747 if (gtkaspell->sug_menu)
1748 gtk_widget_destroy(gtkaspell->sug_menu);
1750 gtkaspell->sug_menu = menu;
1752 gtk_signal_connect(GTK_OBJECT(menu), "cancel",
1753 GTK_SIGNAL_FUNC(cancel_menu_cb), gtkaspell);
1755 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1756 (unsigned char*) l->data,
1757 gtkaspell->gtkaspeller->dictionary->dictname);
1758 item = gtk_menu_item_new_with_label(caption);
1759 gtk_widget_show(item);
1760 gtk_menu_append(GTK_MENU(menu), item);
1761 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1764 item = gtk_menu_item_new();
1765 gtk_widget_show(item);
1766 gtk_menu_append(GTK_MENU(menu), item);
1768 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1769 gtk_widget_show(item);
1770 gtk_menu_append(GTK_MENU(menu), item);
1771 gtk_signal_connect(GTK_OBJECT(item), "activate",
1772 GTK_SIGNAL_FUNC(add_word_to_session_cb),
1774 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1776 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1778 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1779 gtk_widget_show(item);
1780 gtk_menu_append(GTK_MENU(menu), item);
1781 gtk_signal_connect(GTK_OBJECT(item), "activate",
1782 GTK_SIGNAL_FUNC(add_word_to_personal_cb),
1784 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1786 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1788 item = gtk_menu_item_new_with_label(_("Replace with..."));
1789 gtk_widget_show(item);
1790 gtk_menu_append(GTK_MENU(menu), item);
1791 gtk_signal_connect(GTK_OBJECT(item), "activate",
1792 GTK_SIGNAL_FUNC(replace_with_create_dialog_cb),
1794 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1795 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1797 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1798 caption = g_strdup_printf(_("Check with %s"),
1799 gtkaspell->alternate_speller->dictionary->dictname);
1800 item = gtk_menu_item_new_with_label(caption);
1802 gtk_widget_show(item);
1803 gtk_menu_append(GTK_MENU(menu), item);
1804 gtk_signal_connect(GTK_OBJECT(item), "activate",
1805 GTK_SIGNAL_FUNC(check_with_alternate_cb),
1807 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1808 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1811 item = gtk_menu_item_new();
1812 gtk_widget_show(item);
1813 gtk_menu_append(GTK_MENU(menu), item);
1817 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1818 gtk_widget_show(item);
1819 gtk_menu_append(GTK_MENU(menu), item);
1821 GtkWidget *curmenu = menu;
1825 if (count == MENUCOUNT) {
1828 item = gtk_menu_item_new_with_label(_("More..."));
1829 gtk_widget_show(item);
1830 gtk_menu_append(GTK_MENU(curmenu), item);
1832 curmenu = gtk_menu_new();
1833 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
1837 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1838 gtk_widget_show(item);
1839 gtk_menu_append(GTK_MENU(curmenu), item);
1840 gtk_signal_connect(GTK_OBJECT(item), "activate",
1841 GTK_SIGNAL_FUNC(replace_word_cb),
1844 if (curmenu == menu && count < MENUCOUNT) {
1845 gtk_widget_add_accelerator(item, "activate",
1850 gtk_widget_add_accelerator(item, "activate",
1859 } while ((l = l->next) != NULL);
1862 gtk_accel_group_attach(accel, GTK_OBJECT(menu));
1863 gtk_accel_group_unref(accel);
1865 return GTK_MENU(menu);
1868 static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
1870 GtkWidget *item, *submenu;
1872 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
1874 if (GTK_MENU_SHELL(menu)->children) {
1875 GList *amenu, *alist;
1876 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
1877 alist = amenu->next;
1878 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1883 dictname = g_strdup_printf(_("Dictionary: %s"),
1884 gtkaspeller->dictionary->dictname);
1885 item = gtk_menu_item_new_with_label(dictname);
1886 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1888 gtk_widget_show(item);
1889 gtk_menu_append(GTK_MENU(menu), item);
1891 item = gtk_menu_item_new();
1892 gtk_widget_show(item);
1893 gtk_menu_append(GTK_MENU(menu), item);
1895 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1896 dictname = g_strdup_printf(_("Use alternate (%s)"),
1897 gtkaspell->alternate_speller->dictionary->dictname);
1898 item = gtk_menu_item_new_with_label(dictname);
1900 gtk_signal_connect(GTK_OBJECT(item), "activate",
1901 GTK_SIGNAL_FUNC(switch_to_alternate_cb),
1903 gtk_widget_show(item);
1904 gtk_menu_append(GTK_MENU(menu), item);
1907 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1908 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
1909 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1910 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1912 gtk_signal_connect(GTK_OBJECT(item), "activate",
1913 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1915 gtk_widget_show(item);
1916 gtk_menu_append(GTK_MENU(menu), item);
1918 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1919 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
1920 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1921 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1923 gtk_signal_connect(GTK_OBJECT(item), "activate",
1924 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1926 gtk_widget_show(item);
1927 gtk_menu_append(GTK_MENU(menu),item);
1929 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1930 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
1931 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1932 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1934 gtk_signal_connect(GTK_OBJECT(item), "activate",
1935 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1937 gtk_widget_show(item);
1938 gtk_menu_append(GTK_MENU(menu), item);
1940 item = gtk_menu_item_new();
1941 gtk_widget_show(item);
1942 gtk_menu_append(GTK_MENU(menu), item);
1944 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
1945 if (gtkaspell->check_while_typing)
1946 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1948 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
1949 gtk_signal_connect(GTK_OBJECT(item), "activate",
1950 GTK_SIGNAL_FUNC(toggle_check_while_typing_cb),
1952 gtk_widget_show(item);
1953 gtk_menu_append(GTK_MENU(menu), item);
1955 item = gtk_menu_item_new();
1956 gtk_widget_show(item);
1957 gtk_menu_append(GTK_MENU(menu), item);
1959 submenu = gtk_menu_new();
1960 item = gtk_menu_item_new_with_label(_("Change dictionary"));
1961 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
1962 gtk_widget_show(item);
1963 gtk_menu_append(GTK_MENU(menu), item);
1966 if (gtkaspellcheckers->dictionary_list == NULL)
1967 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
1969 GtkWidget * curmenu = submenu;
1973 tmp = gtkaspellcheckers->dictionary_list;
1975 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
1976 tmp = g_slist_next(tmp)) {
1977 if (count == MENUCOUNT) {
1980 newmenu = gtk_menu_new();
1981 item = gtk_menu_item_new_with_label(_("More..."));
1982 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
1985 gtk_menu_append(GTK_MENU(curmenu), item);
1986 gtk_widget_show(item);
1990 dict = (Dictionary *) tmp->data;
1991 item = gtk_check_menu_item_new_with_label(dict->dictname);
1992 gtk_object_set_data(GTK_OBJECT(item), "dict_name",
1994 if (strcmp2(dict->fullname,
1995 gtkaspell->gtkaspeller->dictionary->fullname))
1996 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
1998 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1999 gtk_widget_set_sensitive(GTK_WIDGET(item),
2002 gtk_signal_connect(GTK_OBJECT(item), "activate",
2003 GTK_SIGNAL_FUNC(change_dict_cb),
2005 gtk_widget_show(item);
2006 gtk_menu_append(GTK_MENU(curmenu), item);
2013 static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
2015 if (!gtkaspell->popup_config_menu)
2016 gtkaspell->popup_config_menu = gtk_menu_new();
2018 debug_print("Aspell: creating/using popup_config_menu %0x\n",
2019 (guint) gtkaspell->popup_config_menu);
2020 populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
2022 return GTK_MENU(gtkaspell->popup_config_menu);
2025 void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
2029 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2031 debug_print("Aspell: using config menu %0x\n",
2032 (guint) gtkaspell->popup_config_menu);
2033 populate_submenu(gtkaspell, menu);
2035 gtkaspell->config_menu = menu;
2039 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, gpointer data)
2041 GtkAspell *gtkaspell = (GtkAspell *) data;
2042 gint xx = 0, yy = 0;
2045 GtkSText *text = GTK_STEXT(gtkaspell->gtktext);
2048 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2050 sx = gdk_screen_width();
2051 sy = gdk_screen_height();
2053 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2058 *x = gtkaspell->gtktext->cursor_pos_x + xx +
2059 gdk_char_width(GTK_WIDGET(text)->style->font, ' ');
2060 *y = gtkaspell->gtktext->cursor_pos_y + yy;
2066 gdk_string_height((GTK_WIDGET(gtkaspell->gtktext))->style->font,
2067 gtkaspell->theword);
2071 /* Menu call backs */
2073 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2075 GtkAspell *gtkaspell = (GtkAspell *) data;
2077 gtkaspell->continue_check = NULL;
2078 set_point_continue(gtkaspell);
2084 /* change_dict_cb() - Menu callback : change dict */
2085 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2089 GtkAspeller *gtkaspeller;
2092 fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(w), "dict_name");
2094 if (!strcmp2(fullname, _("None")))
2097 sug_mode = gtkaspell->default_sug_mode;
2099 dict = g_new0(Dictionary, 1);
2100 dict->fullname = g_strdup(fullname);
2101 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2103 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2104 dict == gtkaspell->alternate_speller->dictionary) {
2105 use_alternate_dict(gtkaspell);
2106 dictionary_delete(dict);
2110 gtkaspeller = gtkaspeller_new(dict);
2114 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2115 gtkaspellcheckers->error_message);
2117 gtkaspell_alert_dialog(message);
2120 if (gtkaspell->use_alternate) {
2121 if (gtkaspell->alternate_speller)
2122 gtkaspeller_delete(gtkaspell->alternate_speller);
2123 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2125 gtkaspeller_delete(gtkaspell->gtkaspeller);
2127 gtkaspell->gtkaspeller = gtkaspeller;
2128 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2131 dictionary_delete(dict);
2133 if (gtkaspell->config_menu)
2134 populate_submenu(gtkaspell, gtkaspell->config_menu);
2137 static void switch_to_alternate_cb(GtkWidget *w,
2140 GtkAspell *gtkaspell = (GtkAspell *) data;
2141 use_alternate_dict(gtkaspell);
2144 /* Misc. helper functions */
2146 static void set_point_continue(GtkAspell *gtkaspell)
2150 gtktext = gtkaspell->gtktext;
2152 gtk_stext_freeze(gtktext);
2153 gtk_editable_set_position(GTK_EDITABLE(gtktext),gtkaspell->orig_pos);
2154 gtk_stext_set_point(gtktext, gtkaspell->orig_pos);
2155 gtk_stext_thaw(gtktext);
2157 if (gtkaspell->continue_check)
2158 gtkaspell->continue_check((gpointer *) gtkaspell);
2161 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2164 GdkColor *color = &(gtkaspell->highlight);
2166 gc = gtk_widget_get_colormap(GTK_WIDGET(gtkaspell->gtktext));
2168 if (gtkaspell->highlight.pixel)
2169 gdk_colormap_free_colors(gc, &(gtkaspell->highlight), 1);
2171 /* Shameless copy from Sylpheed's gtkutils.c */
2173 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2175 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2177 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2180 gdk_colormap_alloc_color(gc, &(gtkaspell->highlight), FALSE, TRUE);
2183 static void change_color(GtkAspell * gtkaspell,
2184 gint start, gint end,
2190 g_return_if_fail(start < end);
2192 gtktext = gtkaspell->gtktext;
2194 gtk_stext_freeze(gtktext);
2196 gtk_stext_set_point(gtktext, start);
2197 gtk_stext_forward_delete(gtktext, end - start);
2198 gtk_stext_insert(gtktext, NULL, color, NULL, newtext,
2201 gtk_stext_thaw(gtktext);
2204 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2205 * as needed by aspell. Returns an allocated string.
2208 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2210 guchar * aspell_encoding;
2212 if (strstr2(encoding, "ISO-8859-")) {
2213 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2216 if (!strcmp2(encoding, "US-ASCII"))
2217 aspell_encoding = g_strdup("iso8859-1");
2219 aspell_encoding = g_strdup(encoding);
2222 return aspell_encoding;
2225 /* compare_dict () - compare 2 dict names */
2226 static gint compare_dict(Dictionary *a, Dictionary *b)
2228 guint aparts = 0, bparts = 0;
2231 for (i=0; i < strlen(a->dictname); i++)
2232 if (a->dictname[i] == '-')
2234 for (i=0; i < strlen(b->dictname); i++)
2235 if (b->dictname[i] == '-')
2238 if (aparts != bparts)
2239 return (aparts < bparts) ? -1 : +1;
2242 compare = strcmp2(a->dictname, b->dictname);
2244 compare = strcmp2(a->fullname, b->fullname);
2249 static void dictionary_delete(Dictionary *dict)
2251 g_free(dict->fullname);
2252 g_free(dict->encoding);
2256 static Dictionary *dictionary_dup(const Dictionary *dict)
2260 dict2 = g_new(Dictionary, 1);
2262 dict2->fullname = g_strdup(dict->fullname);
2263 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2264 dict2->encoding = g_strdup(dict->encoding);
2269 static void free_suggestions_list(GtkAspell *gtkaspell)
2273 for (list = gtkaspell->suggestions_list; list != NULL;
2279 gtkaspell->max_sug = -1;
2280 gtkaspell->suggestions_list = NULL;
2283 static void reset_theword_data(GtkAspell *gtkaspell)
2285 gtkaspell->start_pos = 0;
2286 gtkaspell->end_pos = 0;
2287 gtkaspell->theword[0] = 0;
2288 gtkaspell->max_sug = -1;
2290 free_suggestions_list(gtkaspell);
2293 static void free_checkers(gpointer elt, gpointer data)
2295 GtkAspeller *gtkaspeller = elt;
2297 g_return_if_fail(gtkaspeller);
2299 gtkaspeller_real_delete(gtkaspeller);
2302 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2304 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2305 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2307 if (a && b && a->fullname && b->fullname &&
2308 strcmp(a->fullname, b->fullname) == 0 &&
2309 a->encoding && b->encoding)
2310 return strcmp(a->encoding, b->encoding);
2315 static void gtkaspell_alert_dialog(gchar *message)
2320 GtkWidget *ok_button;
2322 dialog = gtk_dialog_new();
2323 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
2324 gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_MOUSE);
2325 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
2326 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2327 GTK_OBJECT(dialog));
2329 label = gtk_label_new(message);
2330 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
2332 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
2334 hbox = gtk_hbox_new(FALSE, 0);
2336 ok_button = gtk_button_new_with_label(_("OK"));
2337 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
2338 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
2340 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
2341 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2342 GTK_OBJECT(dialog));
2343 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
2345 gtk_widget_grab_default(ok_button);
2346 gtk_widget_grab_focus(ok_button);
2347 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
2349 gtk_widget_show_all(dialog);