/*  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"
#include "pixmap.h"
#include "dictgroup.h"
#include "preference.h"
#include "eb.h"
#include "multi.h"

extern GList *group_list;
extern GtkWidget *display_dictbar;

static GtkWidget *dict_bar;
GtkWidget *dict_box;
GtkWidget *combo_group;

void show_dict_bar()
{
	gtk_widget_show(dict_bar);
	bshow_dict_bar = 1;
	save_preference();

	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(display_dictbar),
				       bshow_dict_bar);
}

void hide_dict_bar()
{
	gtk_widget_hide(dict_bar);
	bshow_dict_bar = 0;
	save_preference();

	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(display_dictbar),
				       bshow_dict_bar);
}

void toggle_dict_bar(){
	if(bshow_dict_bar == 1)
		hide_dict_bar();
	else 
		show_dict_bar();
}

static void dict_toggled(GtkWidget *widget, gpointer data)
{
	DICT_GROUP *group;
	DICT_MEMBER *member;
	GList *group_item;
	GList *member_item;
	gboolean active;
	int i;


	active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

	group_item = g_list_first(group_list);
	while(group_item != NULL){
		group = (DICT_GROUP *)(group_item->data);
		if(group->active == TRUE){
			member_item = group->member;
			i = 0;
			while(member_item != NULL){
				member = (DICT_MEMBER *)(member_item->data);
				if(i == (int)data)
					member->active = active;
				i++;
				member_item = g_list_next(member_item);
			}
		}
		group_item = g_list_next(group_item);
	}

	if(ebook_search_method() == SEARCH_METHOD_MULTI)
		show_multi();

	//print_dict_group();

	save_dictgroup();
}

static void add_dict_buttons(GtkWidget *bar)
{
	GtkWidget *toggle;
	int idx;
	char buff[64];
	DICT_GROUP *group;
	DICT_MEMBER *member;
	GList *group_item;
	GList *member_item;

	idx = 0;
	group_item = g_list_first(group_list);
	while(group_item != NULL){
		group = (DICT_GROUP *)(group_item->data);
		if(group->active == TRUE){
			member_item = group->member;
			if(idx != 0){
			  fprintf(stderr, "multipe group active\n");
			  return;
			}
			while(member_item != NULL){
				gchar dict_info[512];
				gboolean have_multi=FALSE;
				gint i;

				member = (DICT_MEMBER *)(member_item->data);
				if(member->binfo == NULL) {
				  member_item = g_list_next(member_item);
				  continue;
				}
				//strncpy(buff, member->binfo->subbook_title, dict_label_bytes);
				strncpy(buff, member->name, dict_label_bytes);
				buff[dict_label_bytes] = '\0';
				toggle = gtk_toggle_button_new_with_label(buff);
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), member->active);
				gtk_signal_connect( GTK_OBJECT(toggle),"toggled",
						    (GtkSignalFunc)dict_toggled, (gpointer)idx);
#if 0
				for(i=0;i<MAX_MULTI_SEARCH;i++){
					if(member->binfo->search_method[SEARCH_METHOD_MULTI1+i])
						have_multi = TRUE;
				}

				sprintf(dict_info, "%s\n%s,%d\n\n%s:%s\n%s:%s\n%s:%s\n%s:%s\n%s:%s\n", 
					member->binfo->subbook_title, 
					member->binfo->book_path, 
					member->binfo->subbook_no,
					_("Exactword Search"),
					(member->binfo->search_method[SEARCH_METHOD_EXACTWORD]? "YES" : "NO"),
					_("Forward Search"),
					(member->binfo->search_method[SEARCH_METHOD_WORD]? "YES" : "NO"),
					_("Backward Search"),
					(member->binfo->search_method[SEARCH_METHOD_ENDWORD]? "YES" : "NO"),
					_("Keyword Search"),
					(member->binfo->search_method[SEARCH_METHOD_KEYWORD]? "YES" : "NO"),
					_("Multiword Search"),
					(have_multi ? "YES" : "NO")
);
				gtk_tooltips_set_tip(tooltip, toggle, dict_info,"Private");
#endif

				
				gtk_box_pack_start(GTK_BOX(bar), toggle, FALSE, FALSE, 2);
				if(member->binfo->available == FALSE) {
				  gtk_widget_set_sensitive(toggle, FALSE);
				}

				idx ++;

				member_item = g_list_next(member_item);
			}
		}
		group_item = g_list_next(group_item);
	}

}

void update_dict_bar()
{
	GList *children;
	GtkBoxChild *child;
	
	gtk_widget_hide(dict_box);

	// ボタンを作りなおす

	children = GTK_BOX(dict_box)->children;
	while(children){
		child = children->data;
		children = children->next;
		if(GTK_IS_TOGGLE_BUTTON(child->widget))
			gtk_widget_destroy(child->widget);
	}

	add_dict_buttons(dict_box);
	gtk_widget_show_all(dict_box);
}

static gint group_changed (GtkWidget *combo){
	gchar *text;
	DICT_GROUP *group;
	GList *group_item;

	text = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_group)->entry));
	
	group_item = g_list_first(group_list);
	while(group_item != NULL){
		group = (DICT_GROUP *)(group_item->data);
		if(strcmp(group->name, text) == 0){
			group->active = TRUE;
		} else {
			group->active = FALSE;
		}
		group_item = g_list_next(group_item);
	}

	update_dict_bar();

	save_dictgroup();

	if(ebook_search_method() == SEARCH_METHOD_MULTI)
		show_multi();

	//print_dict_group();

	return(FALSE);
}

GtkWidget *create_dict_bar()
{
	GList *list=NULL;
	GList *group_item;
	DICT_GROUP *group;
	gchar *old_group=NULL;
	GList *children;
	GtkBoxChild *child;
	gboolean active;
	gboolean old;
	gint active_no;
	gint old_no;
	gint i;


	if(dict_bar){
	        old_group = strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_group)->entry)));

	        children = GTK_BOX(dict_bar)->children;
		while(children){
		        child = children->data;
			children = children->next;
			gtk_widget_destroy(child->widget);
		}
	} else {
	        dict_bar = gtk_hbox_new(FALSE, 0);
	}

	combo_group = gtk_combo_new();
	gtk_widget_set_usize(GTK_WIDGET(combo_group), 120, 10);
	gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(combo_group)->entry), FALSE);
	gtk_box_pack_start(GTK_BOX(dict_bar), combo_group, FALSE, FALSE, 0);

	gtk_container_border_width(GTK_CONTAINER(dict_bar), 1);

	gtk_tooltips_set_tip(tooltip, GTK_COMBO(combo_group)->entry, _("Select dictionary group."),"Private");


	active = FALSE;
	old = FALSE;
        i = 0;
	active_no = 0;
	old_no = 0;
	group_item = g_list_first(group_list);
	while(group_item != NULL){
		group = (DICT_GROUP *)(group_item->data);
		if(group->active == TRUE){
			active = TRUE;
			active_no = i;
		}
		if(old_group && (strcmp(group->name, old_group) == 0)){
		        old = TRUE;
		        old_no = i;
		}
		list = g_list_append(list, group->name);
		group_item = g_list_next(group_item);
		i ++;
	}

	if(g_list_length(list) != 0)
	        gtk_combo_set_popdown_strings( GTK_COMBO(combo_group), list) ;

	if(active == TRUE){
	        group_item = g_list_nth(group_list, active_no);
		group = (DICT_GROUP *)(group_item->data);
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_group)->entry), group->name);
	} else if (old == TRUE){
	        group_item = g_list_nth(group_list, old_no);
		group = (DICT_GROUP *)(group_item->data);
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_group)->entry), group->name);
	} else {
	        group_item = g_list_first(group_list);
		if(group_item){
			group = (DICT_GROUP *)(group_item->data);
			gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_group)->entry), group->name);
			group->active = TRUE;
		}
	}

  

	gtk_signal_connect_object (GTK_OBJECT (GTK_COMBO(combo_group)->entry), "changed",
				   GTK_SIGNAL_FUNC(group_changed),
				   NULL);


	// ボタンを作りなおす
	dict_box = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX (dict_bar), dict_box, FALSE, FALSE, 0);
	add_dict_buttons(dict_box);





	gtk_widget_show_all(dict_bar);

	return(dict_bar);
}


syntax highlighted by Code2HTML, v. 0.9.1