/*  Copyright (C) 2001-2002  Kenichi Suto
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


#include "defs.h"
#include "global.h"


GtkWidget *ending_dlg;
GtkWidget *clist_ending;
GtkWidget *entry_pattern;
GtkWidget *entry_normal;
GtkWidget *check_nohit;

void clear_ending()
{
	ENDING *ending;
	GList *list;

	list = g_list_first(ending_list);
	while(list){
		ending = (ENDING *)(list->data);
		free(ending->pattern);
		free(ending->normal);
		list = g_list_next(list);
	}

	g_list_free(ending_list);
	ending_list = NULL;
}

void add_ending(ENDING *ending){
	ending_list = g_list_append(ending_list, ending);
}

static void check_changed(GtkWidget *widget,gpointer *data){

	if(GTK_TOGGLE_BUTTON(widget)->active){
		gtk_widget_set_sensitive(clist_ending, TRUE);
		gtk_widget_set_sensitive(entry_pattern, TRUE);
		gtk_widget_set_sensitive(entry_normal, TRUE);
		gtk_widget_set_sensitive(check_nohit, TRUE);
		bending_correction = TRUE;
	} else {
		gtk_widget_set_sensitive(clist_ending, FALSE);
		gtk_widget_set_sensitive(entry_pattern, FALSE);
		gtk_widget_set_sensitive(entry_normal, FALSE);
		gtk_widget_set_sensitive(check_nohit, FALSE);
		bending_correction = FALSE;
	}

	save_ending();
}

static void check_changed2(GtkWidget *widget,gpointer *data){

	if(GTK_TOGGLE_BUTTON(widget)->active){
		bending_only_nohit = TRUE;
	} else {
		bending_only_nohit = FALSE;
	}

	save_ending();
}

static void ok_ending(GtkWidget *widget,gpointer *data){
	int i;
	gchar *list_pattern;
	gchar *list_normal;
	ENDING *ending;

	gtk_grab_remove(ending_dlg);

	clear_ending();

	for(i=0;;i++){
		list_pattern = list_normal = NULL;
		gtk_clist_get_text(GTK_CLIST(clist_ending),
				   i, 0, &list_pattern);
		gtk_clist_get_text(GTK_CLIST(clist_ending),
				   i, 1, &list_normal);
		if(list_pattern == NULL)
			break;

		ending = (ENDING *)calloc(sizeof(ENDING), 1);
		if(ending == NULL){
			fprintf(stderr, "No memory\n");
			exit(1);
		}
		ending->pattern = strdup(list_pattern);
		ending->normal = strdup(list_normal);
		add_ending(ending);
	}
	
	save_ending();

	gtk_widget_destroy(GTK_WIDGET(data));
	
	auto_lookup_resume();
}

static void delete_event( GtkWidget *widget,
		   GdkEvent  *event,
		   gpointer   data )
{
	ok_ending(NULL, NULL);
}

static void remove_entry(GtkWidget *widget,gpointer *data){

	GList *selection;
	int  line_no;

	selection = GTK_CLIST(clist_ending)->selection;
	if(selection == NULL){
//		warning(_("Please select entry."));
		return;
	}

	line_no = GPOINTER_TO_INT(selection->data);
	gtk_clist_remove(GTK_CLIST(clist_ending), line_no);
}

static void add_entry(GtkWidget *widget,gpointer *data){
	gchar *pattern;
	gchar *normal;
	gchar *list_pattern;
	gchar *list_normal;
	char *text[2];
	gint i;

	pattern = gtk_entry_get_text(GTK_ENTRY(entry_pattern));
	normal = gtk_entry_get_text(GTK_ENTRY(entry_normal));
	if((strlen(pattern) == 0) && (strlen(normal) == 0)){
		return;
	}

	for(i=0;;i++){
		list_pattern = list_normal = NULL;
		gtk_clist_get_text(GTK_CLIST(clist_ending),
				   0, i, &list_pattern);
		gtk_clist_get_text(GTK_CLIST(clist_ending),
				   1, i, &list_normal);
		if(list_pattern == NULL)
			break;
		if((strcmp(pattern, list_pattern)==0) ||
		   strcmp(normal, list_normal)==0){
			warning(_("entry already exists"));
			return;
		}
	}
	text[0] = pattern;
	text[1] = normal;
	gtk_clist_append(GTK_CLIST(clist_ending), text);
	
}


void preference_ending()
{
	GtkWidget *button;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *vbox2;
	GtkWidget *label;
	GtkWidget *scroll;
	GtkWidget *check_ending;
	char *text[2];
	char *title[2];
	ENDING *ending;
	GList *list;

	auto_lookup_suspend();

	title[0] = _("Pattern");
	title[1] = _("Correction");

	ending_dlg = gtk_dialog_new();
	gtk_window_set_position(GTK_WINDOW(ending_dlg), GTK_WIN_POS_CENTER_ALWAYS);

	gtk_signal_connect (GTK_OBJECT (ending_dlg), "delete_event",
			    GTK_SIGNAL_FUNC (delete_event), NULL);

//	gtk_widget_set_usize(ending_dlg,400,280);
	
	gtk_grab_add(ending_dlg);
	gtk_widget_realize(ending_dlg);

	button = gtk_button_new_with_label(_("Ok"));
	GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (ending_dlg)->action_area), button,
			    TRUE, TRUE, 0);

	gtk_signal_connect (GTK_OBJECT (button), "clicked",
			    GTK_SIGNAL_FUNC (ok_ending), (gpointer)ending_dlg);
	gtk_widget_grab_default (button);

	vbox = (GtkWidget *)(GTK_DIALOG(ending_dlg)->vbox);
	gtk_container_border_width(GTK_CONTAINER(vbox), 5);
//	gtk_container_add(GTK_CONTAINER(frame), vbox);

	check_ending = gtk_check_button_new_with_label(_("Perform stemming"));
	gtk_tooltips_set_tip(tooltip, check_ending, 
			     _("When ending of each words matches the pattern in the list, normal form of the word will also be tried. It takes longer."),"Private");
	gtk_box_pack_start (GTK_BOX(vbox)
			    , check_ending,FALSE,FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (check_ending), "clicked",
			    GTK_SIGNAL_FUNC (check_changed), NULL);


	check_nohit = gtk_check_button_new_with_label(_("Stemming only when no hit"));
	gtk_tooltips_set_tip(tooltip, check_nohit, 
			     _("Do not perform stemming when original words hit."),"Private");
	gtk_box_pack_start (GTK_BOX(vbox)
			    , check_nohit,FALSE,FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (check_nohit), "clicked",
			    GTK_SIGNAL_FUNC (check_changed2), NULL);


	hbox = gtk_hbox_new(FALSE,0);
	gtk_box_pack_start (GTK_BOX(vbox)
			    , hbox,FALSE, FALSE, 0);


	vbox2 = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start (GTK_BOX(hbox)
			    , vbox2,TRUE, TRUE, 0);

	label = gtk_label_new(_("Pattern"));
	gtk_box_pack_start (GTK_BOX(vbox2)
			    , label,FALSE, FALSE, 0);
	entry_pattern = gtk_entry_new();
	gtk_widget_set_usize(entry_pattern,90,20);
	gtk_box_pack_start (GTK_BOX(vbox2)
			    , entry_pattern,FALSE, FALSE, 0);


	vbox2 = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start (GTK_BOX(hbox)
			    , vbox2,TRUE, TRUE, 0);

	label = gtk_label_new(_("Correction"));
	gtk_box_pack_start (GTK_BOX(vbox2)
			    , label,FALSE, FALSE, 0);
	entry_normal = gtk_entry_new();
	gtk_widget_set_usize(entry_normal,90, 20);
	gtk_box_pack_start (GTK_BOX(vbox2)
			    , entry_normal, FALSE, FALSE, 0);
	
	button = gtk_button_new_with_label(_("Add"));
	GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
	gtk_box_pack_start (GTK_BOX (hbox), button,
			    FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (button), "clicked",
			    GTK_SIGNAL_FUNC (add_entry), (gpointer)NULL);

	button = gtk_button_new_with_label(_("Remove"));
	GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
	gtk_box_pack_start (GTK_BOX (hbox), button,
			    FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (button), "clicked",
			    GTK_SIGNAL_FUNC (remove_entry), (gpointer)NULL);

	scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_usize(scroll,200,150);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
					GTK_POLICY_NEVER, 
					GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start (GTK_BOX(vbox)
			    ,scroll ,TRUE, TRUE, 0);

	clist_ending = gtk_clist_new_with_titles(2, title);
	gtk_clist_column_titles_passive (GTK_CLIST (clist_ending));
	gtk_container_add (GTK_CONTAINER (scroll), clist_ending);
	gtk_clist_set_column_width(GTK_CLIST(clist_ending),
				   0, 100);
	gtk_clist_set_column_width(GTK_CLIST(clist_ending),
				   1, 100);
	gtk_clist_set_reorderable(GTK_CLIST(clist_ending), TRUE);


//	gtk_box_pack_start (GTK_BOX(vbox)
//			    , clist_ending,FALSE,FALSE, 0);

//	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll),
//					      clist_ending);


	list = g_list_first(ending_list);
	while(list){
		ending = (ENDING *)(list->data);
		text[0] = ending->pattern;
		text[1] = ending->normal;
		gtk_clist_append(GTK_CLIST(clist_ending), text);
		list = g_list_next(list);
	}

	gtk_widget_set_sensitive(clist_ending, bending_correction);
	gtk_widget_set_sensitive(entry_pattern, bending_correction);
	gtk_widget_set_sensitive(entry_normal, bending_correction);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_ending), bending_correction);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_nohit), bending_only_nohit);

	gtk_widget_show_all(ending_dlg);
}




syntax highlighted by Code2HTML, v. 0.9.1