2001-09-18 18:41:01 +02:00
|
|
|
/* gtkpspell - a spell-checking addon for GtkText
|
2001-07-29 01:20:15 +02:00
|
|
|
* Copyright (c) 2000 Evan Martin.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2001-09-23 14:31:49 +02:00
|
|
|
* Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
|
2001-07-29 01:20:15 +02:00
|
|
|
* Adapted for Sylpheed (Claws) (c) 2001 by Hiroyuki Yamamoto &
|
|
|
|
* The Sylpheed Claws Team.
|
2001-09-23 14:31:49 +02:00
|
|
|
* Adapted for pspell (c) 2001 Melvin Hadasht
|
2001-07-29 01:20:15 +02:00
|
|
|
*/
|
2001-09-23 14:31:49 +02:00
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
#if USE_PSPELL
|
2001-07-29 01:20:15 +02:00
|
|
|
#include "intl.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <prefs_common.h>
|
|
|
|
#include <utils.h>
|
|
|
|
|
2001-07-31 19:33:35 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include <gtk/gtkoptionmenu.h>
|
|
|
|
#include <gtk/gtkmenu.h>
|
|
|
|
#include <gtk/gtkmenuitem.h>
|
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
#include "gtkxtext.h"
|
|
|
|
|
2001-07-31 19:33:35 +02:00
|
|
|
#include "gtkspell.h"
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
#include <pspell/pspell.h>
|
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
/* size of the text buffer used in various word-processing routines. */
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
|
|
|
/* number of suggestions to display on each menu. */
|
|
|
|
#define MENUCOUNT 15
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/******************************************************************************/
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
/* Function called from menus */
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void add_word_to_session (GtkWidget *w, GtkPspell *d);
|
|
|
|
static void add_word_to_personal (GtkWidget *w, GtkPspell *d);
|
|
|
|
static void set_sug_mode (GtkWidget *w, GtkPspell *gtkpspell);
|
2001-09-24 03:49:01 +02:00
|
|
|
static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell);
|
2001-09-23 14:31:49 +02:00
|
|
|
static void check_all (GtkWidget *w, GtkPspell *gtkpspell);
|
|
|
|
static void menu_change_dict (GtkWidget *w, GtkPspell *gtkpspell);
|
|
|
|
static void entry_insert_cb (GtkXText *gtktext, gchar *newtext,
|
|
|
|
guint len, guint *ppos,
|
|
|
|
GtkPspell *gtkpspell);
|
2001-09-24 03:49:01 +02:00
|
|
|
static gint compare_dict (Dictionary *a, Dictionary *b);
|
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspellconfig - only one config per session */
|
|
|
|
GtkPspellConfig * gtkpspellconfig;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* TODO: configurable */
|
|
|
|
static GdkColor highlight = { 0, 255 * 256, 0, 0 };
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/******************************************************************************/
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_init() - run the first pspell_config from which every
|
|
|
|
* new config is cloned
|
|
|
|
*/
|
|
|
|
GtkPspellConfig * gtkpspell_init()
|
|
|
|
{
|
|
|
|
return new_pspell_config();
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_finished() - Finish all. No more spelling. Called when the
|
|
|
|
* program ends
|
|
|
|
*/
|
|
|
|
void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
|
|
|
|
{
|
|
|
|
if (gtkpspellconfig) {
|
|
|
|
delete_pspell_config(gtkpspellconfig);
|
|
|
|
gtkpspellconfig = NULL;
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_running - Test if there is a manager running
|
|
|
|
*/
|
2001-09-18 18:41:01 +02:00
|
|
|
int gtkpspell_running(GtkPspell * gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-18 18:41:01 +02:00
|
|
|
return (gtkpspell->config!=NULL);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The
|
|
|
|
* settings are defaults. If no path/dict is set afterwards, the default
|
|
|
|
* one is used
|
|
|
|
*/
|
|
|
|
GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
|
|
|
|
{
|
|
|
|
GtkPspell *gtkpspell;
|
|
|
|
|
|
|
|
if (gtkpspellconfig == NULL) {
|
|
|
|
gtkpspellconfig = gtkpspell_init();
|
|
|
|
if (gtkpspellconfig == NULL) {
|
|
|
|
debug_print(_("Pspell could not be started."));
|
|
|
|
prefs_common.enable_pspell=FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell = g_new(GtkPspell ,1);
|
|
|
|
gtkpspell->config = gtkpspellconfig;
|
|
|
|
gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
|
|
|
|
gtkpspell->checker = NULL;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (pspell_error_number(gtkpspell->possible_err) != 0) {
|
|
|
|
debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
|
|
|
|
delete_pspell_can_have_error( gtkpspell->possible_err );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->dictionary_list = NULL;
|
|
|
|
gtkpspell->path = NULL;
|
|
|
|
gtkpspell->dict = NULL;
|
|
|
|
gtkpspell->mode = PSPELL_FASTMODE;
|
2001-09-24 03:49:01 +02:00
|
|
|
gtkpspell->learn = TRUE;
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->gtktext = NULL;
|
|
|
|
return gtkpspell;
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_new_with_config() - Creates a new session and set path/dic
|
|
|
|
*/
|
2001-09-18 18:41:01 +02:00
|
|
|
GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
|
2001-09-23 14:31:49 +02:00
|
|
|
guchar *path, guchar *dict,
|
|
|
|
guint mode, guchar *encoding)
|
|
|
|
{
|
|
|
|
GtkPspell *gtkpspell;
|
|
|
|
|
|
|
|
if (gtkpspellconfig == NULL) {
|
|
|
|
gtkpspellconfig=gtkpspell_init();
|
|
|
|
if (gtkpspellconfig == NULL) {
|
|
|
|
debug_print(_("Pspell could not be started."));
|
|
|
|
prefs_common.enable_pspell = FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell = g_new( GtkPspell ,1);
|
|
|
|
gtkpspell->path = NULL;
|
|
|
|
gtkpspell->dict = NULL;
|
|
|
|
gtkpspell->dictionary_list = NULL;
|
|
|
|
gtkpspell->gtktext = NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->config = pspell_config_clone(gtkpspellconfig);
|
|
|
|
gtkpspell->mode = PSPELL_FASTMODE;
|
2001-09-24 03:49:01 +02:00
|
|
|
gtkpspell->learn = TRUE;
|
2001-09-23 14:31:49 +02:00
|
|
|
|
|
|
|
if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
|
|
|
|
debug_print(_("Pspell could not be configured."));
|
|
|
|
gtkpspell = gtkpspell_delete(gtkpspell);
|
|
|
|
return gtkpspell;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding)
|
|
|
|
pspell_config_replace(gtkpspell->config,"encoding",encoding);
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
|
|
|
|
gtkpspell->checker = NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (pspell_error_number(gtkpspell->possible_err) != 0) {
|
|
|
|
debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
|
|
|
|
delete_pspell_can_have_error(gtkpspell->possible_err);
|
|
|
|
gtkpspell = gtkpspell_delete(gtkpspell);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
|
|
|
|
}
|
|
|
|
return gtkpspell;
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkspell_delete() - Finishes a session
|
|
|
|
*/
|
|
|
|
GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
|
|
|
|
{
|
|
|
|
if ( gtkpspell->checker ) {
|
2001-09-24 03:49:01 +02:00
|
|
|
/* First save all word lists */
|
|
|
|
pspell_manager_save_all_word_lists(gtkpspell->checker);
|
2001-09-23 14:31:49 +02:00
|
|
|
delete_pspell_manager(gtkpspell->checker);
|
|
|
|
gtkpspell->checker = NULL;
|
|
|
|
gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (gtkpspell->dictionary_list)
|
|
|
|
gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
g_free(gtkpspell->path);
|
|
|
|
g_free(gtkpspell->dict);
|
|
|
|
gtkpspell->path = NULL;
|
|
|
|
gtkpspell->dict = NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
g_free(gtkpspell);
|
|
|
|
return NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
|
|
|
|
guchar *path, guchar * dict)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
guchar *module = NULL;
|
|
|
|
guchar *language = NULL;
|
|
|
|
guchar *spelling = NULL;
|
|
|
|
guchar *jargon = NULL;
|
|
|
|
guchar buf[BUFSIZE];
|
|
|
|
guchar *end;
|
|
|
|
guchar *temppath;
|
|
|
|
guchar *tempdict;
|
|
|
|
|
|
|
|
/* Change nothing if any of path/dict/config is NULL */
|
|
|
|
g_return_val_if_fail(path, 0);
|
|
|
|
g_return_val_if_fail(dict, 0);
|
|
|
|
g_return_val_if_fail(config, 0);
|
|
|
|
|
|
|
|
/* This is done, so we can free gtkpspell->path, even if it was
|
|
|
|
* given as an argument of the function */
|
|
|
|
temppath = g_strdup(path);
|
|
|
|
g_free(gtkpspell->path);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* pspell dict name format : */
|
|
|
|
/* <lang>[[-<spelling>[-<jargon>]]-<module>.pwli */
|
|
|
|
/* Strip off path */
|
2001-09-24 03:49:01 +02:00
|
|
|
|
|
|
|
if (!strrchr(dict,G_DIR_SEPARATOR)) {
|
|
|
|
/* plain dict name */
|
|
|
|
strncpy(buf,dict,BUFSIZE-1);
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-09-24 03:49:01 +02:00
|
|
|
else {
|
|
|
|
/* strip path */
|
|
|
|
strncpy(buf, strrchr(dict, G_DIR_SEPARATOR)+1, BUFSIZE-1);
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
g_free(gtkpspell->dict);
|
|
|
|
|
|
|
|
/* Ensure no buffers overflows if the dict is to long */
|
2001-09-24 03:49:01 +02:00
|
|
|
buf[BUFSIZE-1] = 0x00;
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
language = buf;
|
2001-09-24 03:49:01 +02:00
|
|
|
if ((module = strrchr(buf, '-')) != NULL) {
|
2001-09-23 14:31:49 +02:00
|
|
|
module++;
|
2001-09-24 03:49:01 +02:00
|
|
|
if ((end = strrchr(module, '.')) != NULL)
|
|
|
|
end[0] = 0x00;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* In tempdict, we have only the dict name, without path nor
|
2001-09-24 03:49:01 +02:00
|
|
|
extension. Useful in the popup menus */
|
2001-09-23 14:31:49 +02:00
|
|
|
|
|
|
|
tempdict = g_strdup(buf);
|
|
|
|
|
|
|
|
/* Probably I am too paranoied... */
|
2001-09-24 03:49:01 +02:00
|
|
|
if (!(language[0] != 0x00 && language[1] != 0x00))
|
2001-09-23 14:31:49 +02:00
|
|
|
language = NULL;
|
|
|
|
else {
|
2001-09-24 03:49:01 +02:00
|
|
|
spelling = strchr(language, '-');
|
|
|
|
if (spelling != NULL) {
|
|
|
|
spelling[0] = 0x00;
|
2001-09-23 14:31:49 +02:00
|
|
|
spelling++;
|
|
|
|
}
|
|
|
|
if (spelling != module) {
|
2001-09-24 03:49:01 +02:00
|
|
|
if ((end = strchr(spelling, '-')) != NULL) {
|
|
|
|
end[0] = 0x00;
|
2001-09-23 14:31:49 +02:00
|
|
|
jargon = end + 1;
|
|
|
|
if (jargon != module)
|
|
|
|
if ((end = strchr(jargon, '-')) != NULL)
|
2001-09-24 03:49:01 +02:00
|
|
|
end[0] = 0x00;
|
2001-09-23 14:31:49 +02:00
|
|
|
else
|
|
|
|
jargon = NULL;
|
|
|
|
else
|
|
|
|
jargon = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
spelling = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
spelling = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
|
|
|
|
language, spelling, jargon, module);
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (language)
|
|
|
|
pspell_config_replace(config, "language-tag", language);
|
|
|
|
if (spelling)
|
|
|
|
pspell_config_replace(config, "spelling", spelling);
|
|
|
|
if (jargon)
|
|
|
|
pspell_config_replace(config, "jargon", jargon);
|
|
|
|
if (module)
|
|
|
|
pspell_config_replace(config, "module", module);
|
|
|
|
if (temppath)
|
|
|
|
pspell_config_replace(config, "word-list-path", temppath);
|
|
|
|
|
|
|
|
switch(gtkpspell->mode) {
|
|
|
|
case PSPELL_FASTMODE:
|
|
|
|
pspell_config_replace(config, "sug_mode", "fast");
|
|
|
|
break;
|
|
|
|
case PSPELL_NORMALMODE:
|
|
|
|
pspell_config_replace(config, "sug_mode", "normal");
|
|
|
|
break;
|
|
|
|
case PSPELL_BADSPELLERMODE:
|
|
|
|
pspell_config_replace(config, "sug_mode", "bad-spellers");
|
|
|
|
break;
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->path = g_strdup(temppath);
|
|
|
|
gtkpspell->dict = g_strdup(tempdict);
|
|
|
|
g_free(temppath);
|
|
|
|
g_free(tempdict);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
return TRUE;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkpspell_set_path_and_dict() - Set path and dict. The session is
|
|
|
|
* resetted.
|
2001-09-24 03:49:01 +02:00
|
|
|
* FALSE on error, TRUE on success */
|
2001-09-23 14:31:49 +02:00
|
|
|
int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path,
|
|
|
|
guchar * dict)
|
|
|
|
{
|
|
|
|
PspellConfig * config2;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* It seems changing an already running config is not the way to go
|
|
|
|
*/
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
config2 = pspell_config_clone(gtkpspell->config);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
if (gtkpspell->checker) {
|
|
|
|
pspell_manager_save_all_word_lists(gtkpspell->checker);
|
2001-09-23 14:31:49 +02:00
|
|
|
delete_pspell_manager(gtkpspell->checker);
|
2001-09-24 03:49:01 +02:00
|
|
|
}
|
2001-09-23 14:31:49 +02:00
|
|
|
|
|
|
|
gtkpspell->checker = NULL;
|
|
|
|
gtkpspell->possible_err = NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
|
|
|
|
debug_print(_("Pspell set_path_and_dict error."));
|
2001-09-24 03:49:01 +02:00
|
|
|
return FALSE;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->possible_err = new_pspell_manager(config2);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
delete_pspell_config(config2);
|
|
|
|
config2 = NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (pspell_error_number(gtkpspell->possible_err) != 0) {
|
|
|
|
debug_print(_("Pspell path & dict. error %s\n"),
|
|
|
|
pspell_error_message(gtkpspell->possible_err));
|
|
|
|
delete_pspell_can_have_error(gtkpspell->possible_err);
|
|
|
|
gtkpspell->possible_err = NULL;
|
2001-09-24 03:49:01 +02:00
|
|
|
return FALSE;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
return TRUE;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
|
2001-09-18 18:41:01 +02:00
|
|
|
/* Actually, this function is not used and hence not tested. */
|
|
|
|
/* Returns an allocated string */
|
2001-09-23 14:31:49 +02:00
|
|
|
guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
guchar *dict;
|
|
|
|
guchar *language;
|
|
|
|
guchar *spelling;
|
|
|
|
guchar *jargon;
|
|
|
|
guint len;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
g_return_val_if_fail(gtkpspell->config, NULL);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
|
|
|
|
spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
|
|
|
|
jargon = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon" ));
|
|
|
|
len = strlen(language) + strlen(spelling) + strlen(jargon);
|
|
|
|
|
|
|
|
if (len < BUFSIZE) {
|
|
|
|
dict = g_new(char,len + 4);
|
|
|
|
strcpy(dict, language);
|
|
|
|
if (spelling) {
|
|
|
|
strcat(dict, "-");
|
|
|
|
strcat(dict, spelling);
|
|
|
|
if (jargon) {
|
|
|
|
strcat(dict, "-");
|
|
|
|
strcat(dict,jargon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(language);
|
|
|
|
g_free(spelling);
|
|
|
|
g_free(jargon);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
return dict;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkpspell_get_path() - Return the dict path as an allocated string */
|
2001-09-18 18:41:01 +02:00
|
|
|
/* Not used = not tested */
|
2001-09-23 14:31:49 +02:00
|
|
|
guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
guchar * path;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
g_return_val_if_fail(gtkpspell->config, NULL);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
return path;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* menu_change_dict() - Menu callback : change dict */
|
|
|
|
static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
guchar *thedict,
|
|
|
|
*thelabel;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* Dict is simply the menu label */
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
|
|
|
|
thedict = g_strdup(thelabel);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* Set path, dict, (and sug_mode ?) */
|
|
|
|
gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict);
|
|
|
|
g_free(thedict);
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* set_sug_mode() - Menu callback : Set the suggestion mode */
|
|
|
|
static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
unsigned char *themode;
|
|
|
|
|
|
|
|
gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
|
|
|
|
|
|
|
|
if (!strcmp(themode, _("Fast Mode"))) {
|
|
|
|
gtkpspell_set_sug_mode(gtkpspell, "fast");
|
|
|
|
gtkpspell->mode = PSPELL_FASTMODE;
|
|
|
|
}
|
|
|
|
if (!strcmp(themode,_("Normal Mode"))) {
|
|
|
|
gtkpspell_set_sug_mode(gtkpspell, "normal");
|
|
|
|
gtkpspell->mode = PSPELL_NORMALMODE;
|
|
|
|
}
|
|
|
|
if (!strcmp( themode,_("Bad Spellers Mode"))) {
|
|
|
|
gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
|
|
|
|
gtkpspell->mode = PSPELL_BADSPELLERMODE;
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* gtkpspell_set_sug_mode() - Set the suggestion mode */
|
2001-09-18 18:41:01 +02:00
|
|
|
/* Actually, the session is resetted and everything is reset. */
|
2001-09-23 14:31:49 +02:00
|
|
|
/* We take the used path/dict pair and create a new config with them */
|
|
|
|
int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
|
|
|
|
{
|
|
|
|
PspellConfig *config2;
|
|
|
|
guchar *path;
|
|
|
|
guchar *dict;
|
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
pspell_manager_save_all_word_lists(gtkpspell->checker);
|
2001-09-23 14:31:49 +02:00
|
|
|
delete_pspell_manager(gtkpspell->checker);
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->checker = NULL;
|
|
|
|
|
|
|
|
config2 = pspell_config_clone(gtkpspell->config);
|
|
|
|
|
|
|
|
if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
|
|
|
|
debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
|
2001-09-24 03:49:01 +02:00
|
|
|
return FALSE;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pspell_config_replace(config2, "sug-mode", themode);
|
|
|
|
|
|
|
|
gtkpspell->possible_err = new_pspell_manager(config2);
|
|
|
|
delete_pspell_config(config2);
|
|
|
|
config2 = NULL;
|
|
|
|
|
|
|
|
if (pspell_error_number(gtkpspell->possible_err) != 0) {
|
|
|
|
debug_print(_("Pspell set sug-mode error %s\n"),
|
|
|
|
pspell_error_message(gtkpspell->possible_err));
|
|
|
|
delete_pspell_can_have_error(gtkpspell->possible_err);
|
|
|
|
gtkpspell->possible_err = NULL;
|
2001-09-24 03:49:01 +02:00
|
|
|
return FALSE;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
|
|
|
gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
|
2001-09-24 03:49:01 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set_learn_mode() - menu callback to toggle learn mode */
|
|
|
|
static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
gtkpspell->learn = gtkpspell->learn == FALSE ;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* misspelled_suggest() - Create a suggestion list for word */
|
|
|
|
static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
const guchar *newword;
|
|
|
|
GList *list = NULL;
|
|
|
|
int count;
|
|
|
|
const PspellWordList *suggestions;
|
|
|
|
PspellStringEmulation *elements;
|
|
|
|
|
|
|
|
g_return_val_if_fail(word, NULL);
|
|
|
|
|
|
|
|
if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
|
|
|
|
suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
|
|
|
|
elements = pspell_word_list_elements(suggestions);
|
|
|
|
/* First one must be the misspelled (why this ?) */
|
|
|
|
list = g_list_append(list, g_strdup(word));
|
|
|
|
|
|
|
|
while ((newword = pspell_string_emulation_next(elements)) != NULL)
|
|
|
|
list = g_list_append(list, g_strdup(newword));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* misspelled_test() - Just test if word is correctly spelled */
|
|
|
|
static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word)
|
2001-09-18 18:41:01 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1;
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
static gboolean iswordsep(unsigned char c)
|
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
return !isalpha(c) && c != '\'';
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
guchar a;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
|
|
|
|
if (text == NULL)
|
|
|
|
return 0;
|
|
|
|
a = (guchar) *text;
|
|
|
|
g_free(text);
|
|
|
|
return a;
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
/* get_word_from_pos () - return the word pointed to. */
|
|
|
|
/* Handles correctly the quotes. */
|
2001-09-23 14:31:49 +02:00
|
|
|
static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos,
|
2001-09-18 18:41:01 +02:00
|
|
|
unsigned char* buf,
|
2001-07-29 01:20:15 +02:00
|
|
|
int *pstart, int *pend)
|
|
|
|
{
|
2001-09-24 03:49:01 +02:00
|
|
|
|
|
|
|
/* TODO : when correcting a word into quotes, change the color of */
|
|
|
|
/* the quotes too, as may be they were highlighted before. To do */
|
|
|
|
/* so, we can use two others pointers that points to the whole */
|
|
|
|
/* word including quotes. */
|
|
|
|
|
|
|
|
gint start,
|
|
|
|
end;
|
|
|
|
guchar c;
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
|
|
|
if (iswordsep(get_text_index_whar(gtkpspell, pos)))
|
|
|
|
return FALSE;
|
2001-09-24 03:49:01 +02:00
|
|
|
|
|
|
|
/* The apostrophe character is somtimes used for quotes
|
|
|
|
* So include it in the word only if it is not surrounded
|
|
|
|
* by other characters.
|
|
|
|
*/
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
for (start = pos; start >= 0; --start) {
|
2001-09-24 03:49:01 +02:00
|
|
|
c = get_text_index_whar(gtkpspell, start);
|
|
|
|
if (c == '\'') {
|
|
|
|
if (start > 0) {
|
|
|
|
if (!isalpha(get_text_index_whar(gtkpspell, start - 1))) {
|
|
|
|
/* start_quote = TRUE; */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* start_quote = TRUE; */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (!isalpha(c))
|
|
|
|
break;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
|
|
|
start++;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
|
2001-09-24 03:49:01 +02:00
|
|
|
c = get_text_index_whar(gtkpspell, end);
|
|
|
|
if (c == '\'') {
|
|
|
|
if (end < gtk_xtext_get_length(gtktext)) {
|
|
|
|
if (!isalpha(get_text_index_whar(gtkpspell, end + 1))) {
|
|
|
|
/* end_quote = TRUE; */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* end_quote = TRUE; */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if(!isalpha(c))
|
|
|
|
break;
|
2001-09-23 14:31:49 +02:00
|
|
|
}
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (buf) {
|
|
|
|
for (pos = start; pos < end; pos++)
|
|
|
|
buf[pos - start] = get_text_index_whar(gtkpspell, pos);
|
|
|
|
buf[pos - start] = 0;
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (pstart)
|
|
|
|
*pstart = start;
|
|
|
|
if (pend)
|
|
|
|
*pend = end;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
return TRUE;
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
|
2001-07-29 01:20:15 +02:00
|
|
|
int *pstart, int *pend)
|
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
|
|
|
|
return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void change_color(GtkPspell * gtkpspell,
|
|
|
|
int start, int end,
|
2001-09-18 18:41:01 +02:00
|
|
|
GdkColor *color)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
char *newtext;
|
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
g_return_if_fail(start < end);
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
|
|
|
|
|
|
|
gtk_xtext_freeze(gtktext);
|
|
|
|
newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
|
|
|
|
if (newtext) {
|
|
|
|
gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
|
|
|
|
GTK_SIGNAL_FUNC(entry_insert_cb),
|
|
|
|
gtkpspell);
|
|
|
|
gtk_xtext_set_point(gtktext, start);
|
|
|
|
gtk_xtext_forward_delete(gtktext, end - start);
|
|
|
|
|
|
|
|
gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
|
|
|
|
gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
|
|
|
|
GTK_SIGNAL_FUNC(entry_insert_cb),
|
|
|
|
gtkpspell);
|
|
|
|
}
|
|
|
|
gtk_xtext_thaw(gtktext);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static gboolean check_at(GtkPspell *gtkpspell, int from_pos)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
int start, end;
|
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
g_return_val_if_fail(from_pos >= 0, FALSE);
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
|
|
|
|
return FALSE;
|
2001-09-24 03:49:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
|
|
|
|
gtkpspell->theword[BUFSIZE - 1] = 0;
|
|
|
|
|
|
|
|
if (misspelled_test(gtkpspell, buf)) {
|
|
|
|
if (highlight.pixel == 0) {
|
|
|
|
/* add an entry for the highlight in the color map. */
|
|
|
|
GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
|
|
|
|
gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
|
|
|
|
}
|
|
|
|
change_color(gtkpspell, start, end, &highlight);
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
change_color(gtkpspell, start, end,
|
|
|
|
&(GTK_WIDGET(gtktext)->style->fg[0]));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
gtkpspell_check_all(gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
void gtkpspell_check_all(GtkPspell *gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-24 03:49:01 +02:00
|
|
|
guint origpos;
|
|
|
|
guint pos = 0;
|
|
|
|
guint len;
|
|
|
|
float adj_value;
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
|
|
|
|
if (!gtkpspell_running(gtkpspell)) return ;
|
|
|
|
gtktext = gtkpspell->gtktext;
|
|
|
|
|
|
|
|
len = gtk_xtext_get_length(gtktext);
|
|
|
|
|
|
|
|
adj_value = gtktext->vadj->value;
|
|
|
|
gtk_xtext_freeze(gtktext);
|
|
|
|
origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
|
|
|
|
while (pos < len) {
|
|
|
|
while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
|
|
|
|
pos++;
|
|
|
|
while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
|
|
|
|
pos++;
|
|
|
|
if (pos > 0)
|
|
|
|
check_at(gtkpspell, pos - 1);
|
|
|
|
}
|
|
|
|
gtk_xtext_thaw(gtktext);
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
|
|
|
|
gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void entry_insert_cb(GtkXText *gtktext, gchar *newtext,
|
|
|
|
guint len, guint *ppos,
|
2001-09-18 18:41:01 +02:00
|
|
|
GtkPspell * gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-24 03:49:01 +02:00
|
|
|
guint origpos;
|
2001-09-23 14:31:49 +02:00
|
|
|
if (!gtkpspell_running(gtkpspell))
|
|
|
|
return;
|
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
/* We must insert ourself the character to impose the */
|
|
|
|
/* color of the inserted character to be default */
|
|
|
|
/* Never mess with set_insertion when frozen */
|
|
|
|
gtk_xtext_freeze(gtktext);
|
|
|
|
gtk_xtext_backward_delete(GTK_XTEXT(gtktext), len);
|
|
|
|
gtk_xtext_insert(GTK_XTEXT(gtktext), NULL,
|
|
|
|
&(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
|
|
|
|
*ppos = gtk_xtext_get_point(GTK_XTEXT(gtktext));
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (iswordsep(newtext[0])) {
|
|
|
|
/* did we just end a word? */
|
|
|
|
if (*ppos >= 2)
|
|
|
|
check_at(gtkpspell, *ppos - 2);
|
|
|
|
|
|
|
|
/* did we just split a word? */
|
|
|
|
if (*ppos < gtk_xtext_get_length(gtktext))
|
|
|
|
check_at(gtkpspell, *ppos + 1);
|
|
|
|
} else {
|
|
|
|
/* check as they type, *except* if they're typing at the end (the most
|
|
|
|
* common case.
|
|
|
|
*/
|
|
|
|
if (*ppos < gtk_xtext_get_length(gtktext)
|
|
|
|
&& !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
|
|
|
|
check_at(gtkpspell, *ppos - 1);
|
|
|
|
}
|
2001-09-24 03:49:01 +02:00
|
|
|
gtk_xtext_thaw(gtktext);
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void entry_delete_cb(GtkXText *gtktext, gint start, gint end,
|
|
|
|
GtkPspell *gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
int origpos;
|
|
|
|
|
|
|
|
if (!gtkpspell_running(gtkpspell))
|
|
|
|
return;
|
|
|
|
|
|
|
|
origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
|
2001-09-24 03:49:01 +02:00
|
|
|
if (start)
|
|
|
|
check_at(gtkpspell, start - 1);
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
|
|
|
|
gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
|
|
|
|
/* this is to *UNDO* the selection, in case they were holding shift
|
|
|
|
* while hitting backspace. */
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void replace_word(GtkWidget *w, GtkPspell *gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
int start, end,oldlen, newlen;
|
|
|
|
guint origpos;
|
|
|
|
unsigned char *newword;
|
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
guint pos;
|
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
|
|
|
|
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtktext));
|
|
|
|
origpos = gtkpspell->orig_pos;
|
|
|
|
pos = origpos;
|
|
|
|
|
|
|
|
gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
|
|
|
|
newlen = strlen(newword);
|
|
|
|
|
|
|
|
get_curword(gtkpspell, buf, &start, &end);
|
|
|
|
oldlen = end - start;
|
|
|
|
|
|
|
|
gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
|
|
|
|
gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
|
|
|
|
gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
|
2001-09-21 21:03:36 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
if (end-start > 0 && gtkpspell->learn) {
|
|
|
|
/* Just be sure the buffer ends somewhere... */
|
|
|
|
buf[end-start] = 0;
|
|
|
|
/* Learn from common misspellings */
|
|
|
|
pspell_manager_store_replacement(gtkpspell->checker, buf,
|
|
|
|
end-start, newword,
|
|
|
|
strlen(newword));
|
|
|
|
}
|
2001-09-23 14:31:49 +02:00
|
|
|
|
|
|
|
/* Put the point and the position where we clicked with the mouse */
|
|
|
|
/* It seems to be a hack, as I must thaw,freeze,thaw the widget */
|
|
|
|
/* to let it update correctly the word insertion and then the */
|
|
|
|
/* point & position position. If not, SEGV after the first replacement */
|
|
|
|
/* If the new word ends before point, put the point at its end*/
|
2001-09-21 21:03:36 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (origpos-start <= oldlen && origpos-start >= 0) {
|
|
|
|
/* Original point was in the word. */
|
|
|
|
/* Put the insertion point in the same location */
|
|
|
|
/* with respect to the new length */
|
|
|
|
/* If the original position is still within the word, */
|
|
|
|
/* then keep the original position. If not, move to the */
|
|
|
|
/* end of the word */
|
|
|
|
if (origpos-start > newlen)
|
|
|
|
pos = start + newlen;
|
|
|
|
}
|
|
|
|
else if (origpos > end) {
|
|
|
|
/* move the position according to the change of length */
|
|
|
|
pos = origpos + newlen - oldlen;
|
|
|
|
}
|
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtktext));
|
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtktext));
|
|
|
|
if (GTK_XTEXT(gtktext)->text_len < pos)
|
|
|
|
pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
|
|
|
|
gtkpspell->orig_pos = pos;
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
|
|
|
|
gtk_xtext_set_point(GTK_XTEXT(gtktext),
|
|
|
|
gtk_editable_get_position(GTK_EDITABLE(gtktext)));
|
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtktext));
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
/* Accept this word for this session */
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
guint pos;
|
|
|
|
GtkXText *gtkxtext;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkxtext = gtkpspell->gtktext;
|
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
|
|
|
|
pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
|
|
|
|
|
|
|
|
pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
|
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
|
|
|
|
gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
|
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
/* add_word_to_personal() - add word to personal dict. */
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
guint pos;
|
|
|
|
GtkXText *gtkxtext;
|
|
|
|
|
|
|
|
gtkxtext = gtkpspell->gtktext;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
|
|
|
|
pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
|
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
|
|
|
|
gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
|
|
|
|
gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
|
2001-09-18 18:41:01 +02:00
|
|
|
gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
|
|
|
|
{
|
|
|
|
GtkWidget *menu, *item, *submenu;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
menu = gtk_menu_new();
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
item = gtk_menu_item_new_with_label(_("Spell check all"));
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-18 18:41:01 +02:00
|
|
|
gtk_signal_connect(GTK_OBJECT(item),"activate",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(check_all),
|
|
|
|
gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
item = gtk_menu_item_new();
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu),item);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
submenu = gtk_menu_new();
|
2001-09-23 14:31:49 +02:00
|
|
|
item = gtk_menu_item_new_with_label(_("Change dictionary"));
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
|
|
|
|
|
|
|
/* Dict list */
|
|
|
|
if (gtkpspell->dictionary_list==NULL)
|
|
|
|
gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkWidget * curmenu = submenu;
|
|
|
|
int count = 0;
|
|
|
|
Dictionary *dict;
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
tmp = gtkpspell->dictionary_list;
|
|
|
|
for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
|
|
|
|
dict = (Dictionary *) tmp->data;
|
|
|
|
item = gtk_check_menu_item_new_with_label(dict->name);
|
|
|
|
if (strcmp2(dict->name, gtkpspell->dict))
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
|
|
|
|
else {
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
|
|
|
|
gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
|
|
|
|
}
|
|
|
|
gtk_menu_append(GTK_MENU(curmenu), item);
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(menu_change_dict),
|
|
|
|
gtkpspell);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
count++;
|
|
|
|
if (count == MENUCOUNT) {
|
|
|
|
GtkWidget *newmenu;
|
|
|
|
newmenu = gtk_menu_new();
|
|
|
|
item = gtk_menu_item_new_with_label(_("More..."));
|
|
|
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
|
|
|
|
gtk_menu_append(GTK_MENU(curmenu), item);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
curmenu = newmenu;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
item = gtk_menu_item_new();
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
|
|
|
if (gtkpspell->mode == PSPELL_FASTMODE) {
|
|
|
|
gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
|
|
|
|
}
|
|
|
|
else
|
2001-09-24 03:49:01 +02:00
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(set_sug_mode),
|
|
|
|
gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu),item);
|
|
|
|
if (gtkpspell->mode == PSPELL_NORMALMODE) {
|
|
|
|
gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(set_sug_mode),
|
|
|
|
gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
|
2001-09-23 14:31:49 +02:00
|
|
|
gtk_widget_show(item);
|
2001-09-24 03:49:01 +02:00
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-23 14:31:49 +02:00
|
|
|
if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
|
|
|
|
gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
|
|
|
|
}
|
|
|
|
else
|
2001-09-24 03:49:01 +02:00
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(set_sug_mode),
|
|
|
|
gtkpspell);
|
2001-09-24 03:49:01 +02:00
|
|
|
item = gtk_menu_item_new();
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
item = gtk_check_menu_item_new_with_label(_("Learn from mistakes"));
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
|
|
|
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),
|
|
|
|
gtkpspell->learn);
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(set_learn_mode), gtkpspell);
|
|
|
|
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
return GTK_MENU(menu);
|
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* make_menu() - Add menus to accept this word for this session and to add it to
|
|
|
|
* personal dictionary */
|
|
|
|
static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkWidget *menu, *item;
|
|
|
|
unsigned char *caption;
|
|
|
|
GtkXText * gtktext;
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
menu = gtk_menu_new();
|
2001-09-18 18:41:01 +02:00
|
|
|
caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
|
2001-09-23 14:31:49 +02:00
|
|
|
item = gtk_menu_item_new_with_label(caption);
|
|
|
|
g_free(caption);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(add_word_to_session),
|
|
|
|
gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
|
2001-09-23 14:31:49 +02:00
|
|
|
item = gtk_menu_item_new_with_label(caption);
|
|
|
|
g_free(caption);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(add_word_to_personal),
|
|
|
|
gtkpspell);
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-07-29 01:20:15 +02:00
|
|
|
item = gtk_menu_item_new();
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
|
|
|
|
|
|
|
l = l->next;
|
|
|
|
if (l == NULL) {
|
2001-09-23 14:31:49 +02:00
|
|
|
item = gtk_menu_item_new_with_label(_("(no suggestions)"));
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
2001-07-29 01:20:15 +02:00
|
|
|
} else {
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkWidget *curmenu = menu;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (l->data == NULL && l->next != NULL) {
|
|
|
|
count = 0;
|
|
|
|
curmenu = gtk_menu_new();
|
|
|
|
item = gtk_menu_item_new_with_label(_("Others..."));
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
|
|
|
|
gtk_menu_append(GTK_MENU(curmenu), item);
|
|
|
|
l = l->next;
|
|
|
|
} else if (count > MENUCOUNT) {
|
|
|
|
count -= MENUCOUNT;
|
|
|
|
item = gtk_menu_item_new_with_label(_("More..."));
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(curmenu), item);
|
|
|
|
curmenu = gtk_menu_new();
|
|
|
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
|
|
|
|
}
|
|
|
|
item = gtk_menu_item_new_with_label((unsigned char*)l->data);
|
|
|
|
gtk_signal_connect(GTK_OBJECT(item), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(replace_word), gtkpspell);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
gtk_menu_append(GTK_MENU(curmenu), item);
|
|
|
|
count++;
|
|
|
|
} while ((l = l->next) != NULL);
|
|
|
|
}
|
|
|
|
return GTK_MENU(menu);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
GList *list, *l;
|
|
|
|
GtkXText * gtktext;
|
|
|
|
|
|
|
|
gtktext = gtkpspell->gtktext;
|
|
|
|
gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
|
|
|
|
|
|
|
|
if (!(eb->state & GDK_SHIFT_MASK))
|
|
|
|
if (get_curword(gtkpspell, buf, NULL, NULL)) {
|
|
|
|
if (buf != NULL) {
|
|
|
|
strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
|
|
|
|
gtkpspell->theword[BUFSIZE - 1] = 0;
|
|
|
|
list = misspelled_suggest(gtkpspell, buf);
|
|
|
|
if (list != NULL) {
|
|
|
|
gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
|
|
|
|
eb->button, eb->time);
|
|
|
|
for (l = list; l != NULL; l = l->next)
|
|
|
|
g_free(l->data);
|
|
|
|
g_list_free(list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
|
|
|
|
eb->button,eb->time);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ok, this is pretty wacky:
|
|
|
|
* we need to let the right-mouse-click go through, so it moves the cursor,
|
|
|
|
* but we *can't* let it go through, because GtkText interprets rightclicks as
|
|
|
|
* weird selection modifiers.
|
|
|
|
*
|
|
|
|
* so what do we do? forge rightclicks as leftclicks, then popup the menu.
|
|
|
|
* HACK HACK HACK.
|
|
|
|
*/
|
2001-09-23 14:31:49 +02:00
|
|
|
static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
GdkEventButton *eb;
|
|
|
|
gboolean retval;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (!gtkpspell_running(gtkpspell))
|
|
|
|
return FALSE;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (e->type != GDK_BUTTON_PRESS)
|
|
|
|
return FALSE;
|
|
|
|
eb = (GdkEventButton*) e;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
if (eb->button != 3)
|
|
|
|
return FALSE;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* forge the leftclick */
|
|
|
|
eb->button = 1;
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(button_press_intercept_cb),
|
|
|
|
gtkpspell);
|
|
|
|
gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
|
|
|
|
e, &retval);
|
|
|
|
gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
|
|
|
|
GTK_SIGNAL_FUNC(button_press_intercept_cb),
|
|
|
|
gtkpspell);
|
|
|
|
gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
|
2001-09-18 18:41:01 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
/* now do the menu wackiness */
|
|
|
|
popup_menu(gtkpspell, eb);
|
|
|
|
gtk_grab_remove(GTK_WIDGET(gtktext));
|
|
|
|
return TRUE;
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
int origpos;
|
|
|
|
unsigned char *text;
|
|
|
|
float adj_value;
|
|
|
|
GtkXText *gtktext;
|
|
|
|
|
|
|
|
gtktext=gtkpspell->gtktext;
|
|
|
|
|
|
|
|
adj_value = gtktext->vadj->value;
|
|
|
|
gtk_xtext_freeze(gtktext);
|
|
|
|
origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
|
|
|
|
text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
|
|
|
|
gtk_xtext_set_point(gtktext, 0);
|
|
|
|
gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
|
|
|
|
gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
|
|
|
|
gtk_xtext_thaw(gtktext);
|
|
|
|
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
|
|
|
|
gtk_adjustment_set_value(gtktext->vadj, adj_value);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell->gtktext=gtktext;
|
2001-09-24 03:49:01 +02:00
|
|
|
gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
|
2001-09-23 14:31:49 +02:00
|
|
|
GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
|
|
|
|
gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
|
|
|
|
GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
|
|
|
|
gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
|
|
|
|
GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
|
2001-07-29 01:20:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
void gtkpspell_detach(GtkPspell * gtkpspell)
|
2001-07-29 01:20:15 +02:00
|
|
|
{
|
2001-09-23 14:31:49 +02:00
|
|
|
GtkXText * gtktext;
|
|
|
|
|
|
|
|
gtktext =gtkpspell->gtktext;
|
|
|
|
/* if (prefs_common.auto_makepspell) { */
|
2001-07-29 01:20:15 +02:00
|
|
|
gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
|
2001-09-18 18:41:01 +02:00
|
|
|
GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
|
2001-07-29 01:20:15 +02:00
|
|
|
gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
|
2001-09-18 18:41:01 +02:00
|
|
|
GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
|
2001-09-23 14:31:49 +02:00
|
|
|
/* }; */
|
|
|
|
gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
|
|
|
|
GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
|
2001-07-29 01:20:15 +02:00
|
|
|
|
2001-09-23 14:31:49 +02:00
|
|
|
gtkpspell_uncheck_all(gtkpspell);
|
2001-07-29 01:20:15 +02:00
|
|
|
}
|
2001-07-31 19:33:35 +02:00
|
|
|
|
|
|
|
/*** Sylpheed (Claws) ***/
|
|
|
|
|
|
|
|
static GSList *create_empty_dictionary_list(void)
|
|
|
|
{
|
|
|
|
GSList *list = NULL;
|
|
|
|
Dictionary *dict;
|
|
|
|
|
|
|
|
dict = g_new0(Dictionary, 1);
|
|
|
|
dict->name = g_strdup(_("None"));
|
|
|
|
return g_slist_append(list, dict);
|
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
/* gtkpspell_get_dictionary_list() - returns list of dictionary names */
|
|
|
|
GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
|
2001-07-31 19:33:35 +02:00
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
gchar *dict_path, *tmp, *prevdir;
|
|
|
|
GSList *walk;
|
|
|
|
Dictionary *dict;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
list = NULL;
|
|
|
|
|
|
|
|
#ifdef USE_THREADS
|
|
|
|
#warning TODO: no directory change
|
|
|
|
#endif
|
2001-09-18 18:41:01 +02:00
|
|
|
dict_path=g_strdup(pspell_path);
|
2001-07-31 19:33:35 +02:00
|
|
|
prevdir = g_get_current_dir();
|
|
|
|
if (chdir(dict_path) <0) {
|
|
|
|
FILE_OP_ERROR(dict_path, "chdir");
|
|
|
|
g_free(prevdir);
|
|
|
|
g_free(dict_path);
|
|
|
|
return create_empty_dictionary_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print(_("Checking for dictionaries in %s\n"), dict_path);
|
|
|
|
|
|
|
|
if (NULL != (dir = opendir("."))) {
|
|
|
|
while (NULL != (ent = readdir(dir))) {
|
2001-09-18 18:41:01 +02:00
|
|
|
/* search for pwli */
|
|
|
|
if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
|
2001-07-31 19:33:35 +02:00
|
|
|
dict = g_new0(Dictionary, 1);
|
|
|
|
dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
|
2001-09-18 18:41:01 +02:00
|
|
|
debug_print(_("Found dictionary %s\n"), dict->name);
|
2001-09-24 03:49:01 +02:00
|
|
|
list = g_slist_insert_sorted(list, dict, (GCompareFunc) compare_dict);
|
2001-07-31 19:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FILE_OP_ERROR(dict_path, "opendir");
|
|
|
|
debug_print(_("No dictionary found\n"));
|
|
|
|
list = create_empty_dictionary_list();
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
if(list==NULL){
|
|
|
|
debug_print(_("No dictionary found"));
|
|
|
|
list = create_empty_dictionary_list();
|
|
|
|
}
|
2001-07-31 19:33:35 +02:00
|
|
|
chdir(prevdir);
|
|
|
|
g_free(dict_path);
|
|
|
|
g_free(prevdir);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2001-09-24 03:49:01 +02:00
|
|
|
/* compare_dict () - compare 2 dict names */
|
|
|
|
|
|
|
|
static gint compare_dict(Dictionary *a, Dictionary *b)
|
|
|
|
{
|
|
|
|
guchar *alanguage, *blanguage,
|
|
|
|
*aspelling, *bspelling,
|
|
|
|
*ajargon , *bjargon ,
|
|
|
|
*amodule , *bmodule ;
|
|
|
|
guint aparts = 0, bparts = 0, i;
|
|
|
|
|
|
|
|
for (i=0; i < strlen(a->name) ; i++)
|
|
|
|
if (a->name[i]=='-')
|
|
|
|
aparts++;
|
|
|
|
for (i=0; i < strlen(b->name) ; i++)
|
|
|
|
if (b->name[i]=='-')
|
|
|
|
bparts++;
|
|
|
|
|
|
|
|
if (aparts != bparts)
|
|
|
|
return (aparts < bparts) ? -1 : +1 ;
|
|
|
|
else
|
|
|
|
return strcmp2(a->name, b->name);
|
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
void gtkpspell_free_dictionary_list(GSList *list)
|
2001-07-31 19:33:35 +02:00
|
|
|
{
|
|
|
|
Dictionary *dict;
|
|
|
|
GSList *walk;
|
|
|
|
for (walk = list; walk != NULL; walk = g_slist_next(walk))
|
|
|
|
if (walk->data) {
|
|
|
|
dict = (Dictionary *) walk->data;
|
|
|
|
if (dict->name)
|
|
|
|
g_free(dict->name);
|
|
|
|
g_free(dict);
|
|
|
|
}
|
|
|
|
g_slist_free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dictionary_option_menu_item_data_destroy(gpointer data)
|
|
|
|
{
|
|
|
|
gchar *str = (gchar *) data;
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
|
2001-07-31 19:33:35 +02:00
|
|
|
{
|
|
|
|
GSList *dict_list, *tmp;
|
|
|
|
GtkWidget *item;
|
|
|
|
GtkWidget *menu;
|
|
|
|
Dictionary *dict;
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
dict_list = gtkpspell_get_dictionary_list(pspell_path);
|
2001-07-31 19:33:35 +02:00
|
|
|
g_return_val_if_fail(dict_list, NULL);
|
|
|
|
|
|
|
|
menu = gtk_menu_new();
|
|
|
|
|
|
|
|
for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
|
|
|
|
dict = (Dictionary *) tmp->data;
|
|
|
|
item = gtk_menu_item_new_with_label(dict->name);
|
2001-09-18 18:41:01 +02:00
|
|
|
if (dict->name)
|
|
|
|
gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
|
|
|
|
g_strdup(dict->name),
|
2001-07-31 19:33:35 +02:00
|
|
|
dictionary_option_menu_item_data_destroy);
|
|
|
|
gtk_menu_append(GTK_MENU(menu), item);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_show(menu);
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
gtkpspell_free_dictionary_list(dict_list);
|
2001-07-31 19:33:35 +02:00
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2001-09-18 18:41:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
|
2001-07-31 19:33:35 +02:00
|
|
|
{
|
|
|
|
GtkWidget *menuitem;
|
|
|
|
gchar *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
|
|
|
|
menuitem = gtk_menu_get_active(GTK_MENU(menu));
|
2001-09-18 18:41:01 +02:00
|
|
|
result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
|
|
|
|
g_return_val_if_fail(result, NULL);
|
|
|
|
return g_strdup(result);
|
|
|
|
|
2001-07-31 19:33:35 +02:00
|
|
|
}
|
2001-09-18 18:41:01 +02:00
|
|
|
#endif
|