/*  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 <gdk/gdkkeysyms.h>

#include "dicttext.h"
#include "pixmap.h"
#include "canvas.h"
#include "contentarea.h"
#include "history.h"
#include "dictheading.h"
#include "mainmenu.h"
#include "dictbar.h"
#include "statusbar.h"
#include "selection.h"
#include "eb.h"
#include "websearch.h"
#include "multi.h"
#include "jcode.h"

GtkWidget *container_child(GtkWidget *container);
static void search_selection();

GtkWidget *tree_viewport=NULL;
GtkWidget *dict_scroll=NULL;
GtkWidget *entry_box=NULL;
GtkWidget *multi_tree_scroll=NULL;
CONTENT_AREA *dict_area=NULL;
GtkWidget *note_tree=NULL;
GtkWidget *note_text=NULL;
GtkWidget *multi_tree=NULL;


static GtkWidget *dict_viewport;
static GtkItemFactory *dict_item_factory;

static gint about_usage = 1;

GdkAtom clipboard_atom = GDK_NONE;
gchar *clipboard=NULL;

static GtkItemFactoryEntry dict_menu_items[] = {
	{ N_("/Search This Word"),    NULL, search_selection, 0, NULL },
	{ N_("/Copy To Clipboard"),    NULL, claim_clipboard_owner, 0, NULL },
	{ N_("/Display"),    NULL, NULL, 0, "<Branch>" },
	{ N_("/Display/Menu bar"),    NULL, show_menu_bar, 0, NULL },
	{ N_("/Display/Dictionary Selection Bar"),    NULL, show_dict_bar, 0, NULL },
	{ N_("/Display/Status Bar"),    NULL, show_status_bar, 0, NULL },
	{ N_("/Display/Tree Frame Tab"),    NULL, show_tree_tab, 0, NULL },
};


static void toggle_web(GtkWidget *widget, gpointer *data){
	GtkWidget *pixmapwid;

	pixmapwid = container_child(widget);

	if(eb_web == 0){
		gtk_notebook_set_page(GTK_NOTEBOOK(note_tree),2);
		if(widget != NULL)
			gtk_pixmap_set(GTK_PIXMAP(pixmapwid), globe_pixmap, globe_mask);
		eb_web = 1;
	} else {
		gtk_notebook_set_page(GTK_NOTEBOOK(note_tree),0);
		if(widget != NULL)
			gtk_pixmap_set(GTK_PIXMAP(pixmapwid), ebook_pixmap, ebook_mask);
		eb_web = 0;
	}

}

void start_search(){
	do_search(NULL, NULL);
}

void do_search(GtkWidget *widget, gpointer *data){
	gchar *word;

	word = gtk_entry_get_text(GTK_ENTRY(word_entry));
	if(strlen(word) != 0){
		if(eb_web == 0){
			clear_tree(tree_root);
			ebook_search(word);
			save_word_history(word);

			if(!bauto_lookup)
				gtk_entry_select_region(GTK_ENTRY(word_entry), 0, GTK_ENTRY(word_entry)->text_length);

			gtk_notebook_set_page(GTK_NOTEBOOK(note_text),0);
		} else {
			web_search();
		}
	} else {
	}

}


static void toggle_auto_callback(GtkWidget *widget, gpointer *data){
	
        bauto_lookup = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_auto));

	if(tag_timeout != 0){
		gtk_timeout_remove(tag_timeout);
	}
	tag_timeout = 0;
	
	if(bauto_lookup){
		tag_timeout = gtk_timeout_add(auto_interval, copy_clipboard,NULL);
		if(button_popup)
			gtk_widget_set_sensitive(button_popup, TRUE);

	} else {
		if(button_popup)
			gtk_widget_set_sensitive(button_popup, FALSE);

	}

	save_preference();

}

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

	bshow_popup = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_popup));

	save_preference();
}


void toggle_auto(){

	gint active;

        active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_auto));
	if(active)
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_auto), FALSE);
	else
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_auto), TRUE);

}

void toggle_popup(){
	gint active;

        active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_popup));
	if(active)
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_popup), FALSE);
	else
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_popup), TRUE);

}

static gint method_changed (GtkWidget *combo){
	gchar *text;
	gint i;
	gint code=SEARCH_METHOD_UNKNOWN;

	
	// ここで表示するようにすると、マウスボタンを離す前に
	// このメソッドが呼ばれてしまう。
	// メニュー検索と著作権表示はメニューから選ぶようにしよう

	if(ebook_search_method() == SEARCH_METHOD_MULTI){
		show_multi();
		if(gtk_notebook_get_current_page(GTK_NOTEBOOK(note_tree)) != 1)
			gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 1);

		gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 1);

		if(bauto_lookup)
			toggle_auto();

	} else 	if(ebook_search_method() == SEARCH_METHOD_INTERNET){
		eb_web = 1;
		if(gtk_notebook_get_current_page(GTK_NOTEBOOK(note_tree)) != 2)
			gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 2);
		gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 0);

		if(bauto_lookup)
			toggle_auto();

	} else 	if(ebook_search_method() == SEARCH_METHOD_FULL_TEXT){

		if(gtk_notebook_get_current_page(GTK_NOTEBOOK(note_tree)) != 0)
			gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);

		if(bauto_lookup)
			toggle_auto();

	} else {
		if(gtk_notebook_get_current_page(GTK_NOTEBOOK(note_tree)) != 0)
			gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
	}
	return(FALSE);
}


static gint entry_key_press_event(GtkWidget *widget, GdkEventKey *event){

	if(event->keyval == 65293){
		//Enter key
		start_search(NULL, NULL);
	}

	return(FALSE);
}

static gint entry_focus_out_event(GtkWidget *widget, GdkEventKey *event)
{
	entry_focus_in = FALSE;
	return(FALSE);
}

static gint entry_focus_in_event(GtkWidget *widget, GdkEventKey *event)
{
	entry_focus_in = TRUE;
	return(FALSE);
}

static void launch_web_browser(gchar *url){
	pid_t pid;
	gint status;
	gchar *p;
	gchar *cmd_template;
	gchar cmd[512];

	switch(pid = fork()){
	case -1: 
		perror("fork");
		return;
	case  0:
		cmd_template = strdup(browser_template);
		p = strstr(cmd_template, "%f");
		if(p != NULL){
			*p = '%';
			p++;
			*p = 's';
		}
		sprintf(cmd, cmd_template, url);
		system(cmd);
		_exit(0);
		break;
	default:
		waitpid(pid, &status, 0);
		break;
	}
}


void show_about()
{
	gchar *lang;
	gchar buff[65536];
	gchar filename[512];
	FILE *fp;
	gint len;

	lang = getenv("LANG");
	if(lang == NULL){
		sprintf(filename, "%s/about.en", PACKAGEDIR);
	} else if(strncmp(lang, "ja_JP", 5) == 0){
		sprintf(filename, "%s/about.jp", PACKAGEDIR);
	} else {
		sprintf(filename, "%s/about.en", PACKAGEDIR);
	}

	fp = fopen(filename, "r");
	if(fp == NULL){
		fprintf(stderr, _("Failed to open %s. Check installation."), filename);
		sprintf(buff, _("Failed to open %s. Check installation."), filename);
		show_text(NULL, buff);
		return;
	}

	len = fread(buff, 1, 65535, fp);
	fclose(fp);

	buff[len] = '\0';
	if(len != 0)
		show_text(NULL, buff);
	current_book_info = NULL;

	about_usage = 1;
}

void show_usage_old()
{

	gchar *lang;
	gchar buff[65536];
	gchar filename[512];
	FILE *fp;
	gint len;

	lang = getenv("LANG");
	if(lang == NULL){
		sprintf(filename, "%s/usage.en", PACKAGEDIR);
	} else if(strncmp(lang, "ja_JP", 5) == 0){
		sprintf(filename, "%s/usage.jp", PACKAGEDIR);
	} else {
		sprintf(filename, "%s/usage.en", PACKAGEDIR);
	}

	fp = fopen(filename, "r");
	if(fp == NULL){
		fprintf(stderr, _("Failed to open %s. Check installation."), filename);
		sprintf(buff, _("Failed to open %s. Check installation."), filename);
		show_text(NULL, buff);
		return;
	}

	len = fread(buff, 1, 65535, fp);
	fclose(fp);

	buff[len] = '\0';
	if(len != 0)
		show_text(NULL, buff);

	current_book_info = NULL;

	about_usage = 2;

}

void show_usage()
{

	gchar *lang;
	gchar buff[65536];
	gchar filename[512];
	FILE *fp;
	gint len;

	lang = getenv("LANG");
	if(lang == NULL){
		sprintf(filename, "file://%s/help/en/index.html", PACKAGEDIR);
	} else if(strncmp(lang, "ja_JP", 5) == 0){
		sprintf(filename, "file://%s/help/ja/index.html", PACKAGEDIR);
	} else {
		sprintf(filename, "file://%s/help/en/index.html", PACKAGEDIR);
	}

	show_text(NULL, _("Help will be shown in external web browser."));

	launch_web_browser(filename);

	current_book_info = NULL;

	about_usage = 2;

}

void show_home()
{
	launch_web_browser("http://ebview.sourceforge.net/");
}

static gint configure_event(GtkWidget *widget, GdkEventConfigure *event)
{
	 gchar *text=NULL;
	 CONTENT_AREA *content_area;

	 content_area = gtk_object_get_user_data(GTK_OBJECT(widget));

	 if((widget->parent->allocation.width == content_area->old_width) &&
	      (widget->parent->allocation.height == content_area->old_height)) { 
		 return TRUE;
	 }


	 content_area->old_width = widget->parent->allocation.width;
	 content_area->old_height = widget->parent->allocation.height;

	 if(current_book_info!= NULL){
		 text = ebook_get_text(current_book_info,
				       current_position.page,
				       current_position.offset);
		 if(text != NULL){
			 show_text(current_book_info, text);
			 free(text);
		 }
	 } else {
		 if(about_usage == 1){
			 show_about();
		 } else {
			 show_usage();
		 }
	 }

	 return FALSE;
 }

static gint show_right_menu (GtkWidget *widget, GdkEventButton *event)
{

	if((event->type == GDK_BUTTON_PRESS)  &&  ((event->button == 2) || (event->button == 3))){
		gtk_item_factory_popup (GTK_ITEM_FACTORY (dict_item_factory), 
					event->x_root, event->y_root, 
					event->button, event->time);

		return FALSE;
	}
	
	return FALSE;
}

static void dict_history_back(GtkWidget *widget, gpointer *data)
{
	history_back(dict_area);
}

static void dict_history_forward(GtkWidget *widget, gpointer *data)
{
	history_forward(dict_area);
}


static void dict_forward_text(GtkWidget *widget, gpointer *data)
{
	gchar *text = NULL;
	gint page, offset;
	EB_Error_Code error_code;

	if(current_book_info  == NULL)
		return;

	error_code = ebook_forward_text(current_book_info);
	if(error_code != EB_SUCCESS)
		return;

	ebook_tell_text(current_book_info, &page, &offset);

	text = ebook_get_text(current_book_info,
			      page,
			      offset);
	if(text != NULL){
		show_text(current_book_info, text);
		free(text);
	}


//	current_book_info = current_book_info;
	current_position.page = page;
	current_position.offset = offset;

	save_history(dict_area,
		     current_book_info,
		     page, 
		     offset);

	if(hex_dlg != NULL)
		dump_hex();
	if(text_dlg != NULL)
		dump_text();

}


static void dict_backward_text(GtkWidget *widget, gpointer *data)
{
	gchar *text = NULL;
	gint page, offset;
	EB_Error_Code error_code;
	
	if(current_book_info  == NULL)
		return;

	error_code = ebook_backward_text(current_book_info);
	if(error_code != EB_SUCCESS)
		return;

	ebook_tell_text(current_book_info, &page, &offset);
	text = ebook_get_text(current_book_info,
			      page,
			      offset);
	if(text != NULL){
		show_text(current_book_info, text);
		free(text);
	}


//	current_book_info = current_book_info;
	current_position.page = page;
	current_position.offset = offset;

	save_history(dict_area,
		     current_book_info,
		     page, 
		     offset);

	if(hex_dlg != NULL)
		dump_hex();
	if(text_dlg != NULL)
		dump_text();

}

void go_up(){
	dict_forward_text(NULL, NULL);
}

void go_down(){
	dict_backward_text(NULL, NULL);
}

void note_switch_page(GtkNotebook *notebook, GtkNotebookPage *page, gint page_num, gpointer data)
{
	if(note_text) {
		switch (page_num) {
		case 0:
			eb_web = 0;
			gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 0);

			if(strcmp(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry)), _("Multiword Search")) == 0)
				gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Automatic Search"));

			if(strcmp(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry)), _("Internet Search")) == 0)
				gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Automatic Search"));
			break;

			break;

		case 1:
			eb_web = 0;
			gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 1);
			
			if(strcmp(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry)), _("Multiword Search")) != 0)
				gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Multiword Search"));

			if(bauto_lookup)
				toggle_auto();
			break;
		case 2:
			eb_web = 1;
			gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 0);
			if(strcmp(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry)), _("Internet Search")) != 0)
			gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Internet Search"));

			if(bauto_lookup)
				toggle_auto();

			break;
		}
	}
}


GtkWidget *create_dict_window()
{
	GtkWidget *vbox;
	GtkWidget *vbox2;
	GtkWidget *hbox;
	GtkWidget *label;
	GtkWidget *hpaned;
	GtkWidget *menubar;
	GtkWidget *dictbar;
	GtkWidget *separator;
	GtkWidget *button_up, *button_down;
	gint i;
	gint nmenu_items;	
	GList *method_list=NULL;
	GtkWidget *pixmap;


	vbox = gtk_vbox_new(FALSE, 0);

	menu_box = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), menu_box, FALSE, TRUE, 0);


	menubar = create_main_menu();
	gtk_box_pack_start(GTK_BOX(menu_box), menubar, TRUE, TRUE, 0);


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

#ifdef ENABLE_WEBSEARCH
/*
	button_mode = create_pixmap_button(ebook_pixmap, ebook_mask);
	gtk_box_pack_start(GTK_BOX(hbox), button_mode, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_mode), "clicked",
				   GTK_SIGNAL_FUNC(toggle_web),
				   (gpointer)button_mode);
	gtk_tooltips_set_tip(tooltip, button_mode, _("Toggle electric book/web search."), "Private");
*/
#endif

	label = gtk_label_new(_("Search Word"));
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);


	combo_word = gtk_combo_new();
	word_entry = GTK_COMBO(combo_word)->entry;
	gtk_combo_disable_activate(GTK_COMBO(combo_word));
	gtk_combo_set_case_sensitive(GTK_COMBO(combo_word), TRUE);
	gtk_box_pack_start(GTK_BOX(hbox), combo_word, TRUE, TRUE, 0);

	gtk_signal_connect( GTK_OBJECT(word_entry),"key_press_event",
			    (GtkSignalFunc)entry_key_press_event, NULL);

	gtk_signal_connect( GTK_OBJECT(word_entry),"focus_out_event",
			    (GtkSignalFunc)entry_focus_out_event, NULL);

	gtk_signal_connect( GTK_OBJECT(word_entry),"focus_in_event",
			    (GtkSignalFunc)entry_focus_in_event, NULL);

	gtk_tooltips_set_tip(tooltip, word_entry, _("Type word here. You can type multiple space-separated words for multi-search and keyword search"),"Private");

	button_start = create_pixmap_button(search_pixmap, search_mask);
	gtk_box_pack_start(GTK_BOX(hbox), button_start, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_start), "clicked",
				   GTK_SIGNAL_FUNC(do_search),
				   (gpointer)button_start);
	gtk_tooltips_set_tip(tooltip, button_start, _("Start search"),"Private");


	separator = gtk_vseparator_new();
	gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 5);

	// 検索方法
	for(i=0 ; search_method[i].name != 0 ; i ++){
		search_method[i].name = _(search_method[i].name);
	}


	combo_method = gtk_combo_new();
	gtk_widget_set_usize(GTK_WIDGET(combo_method), 140, 10);
	gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(combo_method)->entry), FALSE);

	for(i=0 ; ; i ++){
		if(search_method[i].name == NULL)
			break;
		method_list = g_list_append(method_list, search_method[i].name);
	}
	if(i != 0)
		gtk_combo_set_popdown_strings( GTK_COMBO(combo_method), method_list) ;

	gtk_box_pack_start(GTK_BOX (hbox), combo_method, FALSE, TRUE, 0);
	gtk_tooltips_set_tip(tooltip, GTK_COMBO(combo_method)->entry, _("Select search method."),"Private");
	gtk_signal_connect_object (GTK_OBJECT (GTK_COMBO(combo_method)->entry), "changed",
				   GTK_SIGNAL_FUNC(method_changed),
				   NULL);




	separator = gtk_vseparator_new();
	gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 5);


	button_auto = create_pixmap_toggle_button(paste_pixmap, paste_mask);
	gtk_box_pack_start(GTK_BOX (hbox), button_auto, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_auto), "toggled",
				   GTK_SIGNAL_FUNC(toggle_auto_callback),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_auto, _("When enabled, X selection is searched automatically"),"Private");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_auto), bauto_lookup);


	button_popup = create_pixmap_toggle_button(popup_pixmap, popup_mask);
	gtk_box_pack_start(GTK_BOX (hbox), button_popup, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_popup), "toggled",
				   GTK_SIGNAL_FUNC(toggle_popup_callback),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_popup, _("When enabled, result of X selection search will be shown in popup window"),"Private");

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_popup), bshow_popup);
	gtk_widget_set_sensitive(button_popup, bauto_lookup);




	separator = gtk_vseparator_new();
	gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 5);

	button_up = create_pixmap_button(up_pixmap, up_mask);
	gtk_box_pack_start(GTK_BOX(hbox),button_up, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_up), "pressed",
				   GTK_SIGNAL_FUNC(dict_backward_text),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_up, _("Previous Item"),"Private");


	button_down = create_pixmap_button(down_pixmap, down_mask);
	gtk_box_pack_start(GTK_BOX(hbox),button_down, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_down), "pressed",
				   GTK_SIGNAL_FUNC(dict_forward_text),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_down, _("Next Item"),"Private");


	separator = gtk_vseparator_new();
	gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 5);

	button_forward = create_pixmap_button(right_pixmap, right_mask);
	gtk_box_pack_end(GTK_BOX(hbox),button_forward, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_forward), "pressed",
				   GTK_SIGNAL_FUNC(dict_history_forward),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_forward, _("show next in history"),"Private");

	button_back = create_pixmap_button(left_pixmap, left_mask);
	gtk_box_pack_end(GTK_BOX(hbox),button_back, FALSE, FALSE, 0);
	gtk_signal_connect_object (GTK_OBJECT (button_back), "pressed",
				   GTK_SIGNAL_FUNC(dict_history_back),
				   NULL);
	gtk_tooltips_set_tip(tooltip, button_back, _("show previous in history"),"Private");


	// ツールバー
	dictbar = create_dict_bar();
	gtk_box_pack_start(GTK_BOX(vbox), dictbar, FALSE, FALSE, 0);





	hpaned = gtk_hpaned_new();
//	gtk_paned_set_position(GTK_PANED(hpaned), 160);
	gtk_box_pack_start(GTK_BOX(vbox), hpaned, TRUE, TRUE, 0);


	note_tree = gtk_notebook_new();
	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(note_tree), GTK_POS_BOTTOM);
	gtk_notebook_set_show_border(GTK_NOTEBOOK(note_tree), FALSE);
	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), TRUE);
	gtk_paned_add1 (GTK_PANED(hpaned), note_tree);

	gtk_signal_connect (GTK_OBJECT (note_tree), "switch_page",
			    GTK_SIGNAL_FUNC (note_switch_page),
			    NULL);



	// EBook ページ


	tree_scroll = gtk_scrolled_window_new (NULL, NULL);
//	gtk_paned_add1 (GTK_PANED(hpaned), tree_scroll);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tree_scroll),
					GTK_POLICY_ALWAYS,
					GTK_POLICY_ALWAYS);
	tree_root = gtk_ctree_new(1, 0);
	gtk_clist_set_row_height(GTK_CLIST(tree_root),HEADING_HEIGHT);
	gtk_ctree_set_line_style(GTK_CTREE(tree_root),GTK_CTREE_LINES_NONE);
	gtk_clist_set_column_width(GTK_CLIST(tree_root), 0, 640);


        gtk_signal_connect (GTK_OBJECT(tree_root), "select_row",
                            GTK_SIGNAL_FUNC(ctree_select_row), tree_root);

        gtk_signal_connect (GTK_OBJECT(tree_root), "unselect_row",
                            GTK_SIGNAL_FUNC(ctree_unselect_row), tree_root);

	gtk_container_add (GTK_CONTAINER (tree_scroll), tree_root);

	//label = gtk_label_new(_("Result"));
	pixmap = gtk_pixmap_new(list_pixmap, list_mask);
	gtk_notebook_append_page(GTK_NOTEBOOK(note_tree),tree_scroll, pixmap);



	// 複合検索ツリー

	multi_tree_scroll = gtk_scrolled_window_new (NULL, NULL);
	gtk_paned_add1 (GTK_PANED(hpaned), multi_tree_scroll);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (multi_tree_scroll),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	multi_tree = gtk_ctree_new(1, 0);
	gtk_clist_set_row_height(GTK_CLIST(multi_tree),HEADING_HEIGHT);
	gtk_ctree_set_line_style(GTK_CTREE(multi_tree),GTK_CTREE_LINES_NONE);
//	gtk_clist_set_column_width(GTK_CLIST(multi_tree), 0, 640);


        gtk_signal_connect (GTK_OBJECT(multi_tree), "select_row",
                            GTK_SIGNAL_FUNC(multi_select_row), multi_tree);

	gtk_container_add (GTK_CONTAINER (multi_tree_scroll), multi_tree);

	//label = gtk_label_new(_("Multiword"));
	pixmap = gtk_pixmap_new(multi_pixmap, multi_mask);
	gtk_notebook_append_page(GTK_NOTEBOOK(note_tree),multi_tree_scroll, pixmap);


	// Web ページ

#ifdef ENABLE_WEBSEARCH
	vbox2 = gtk_vbox_new(FALSE, 0);
	//label = gtk_label_new(_("Internet"));
	pixmap = gtk_pixmap_new(html_pixmap, html_mask);
	gtk_notebook_append_page(GTK_NOTEBOOK(note_tree), vbox2, pixmap);

	web_tree = create_web_tree();

	gtk_box_pack_start(GTK_BOX(vbox2), web_tree, TRUE, TRUE, 0);
#endif

	if(bshow_tree_tab == 1)
		gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), TRUE);
	else 
		gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);

	// 右半分


	note_text = gtk_notebook_new();
	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(note_text), GTK_POS_BOTTOM);
	gtk_notebook_set_show_border(GTK_NOTEBOOK(note_text), FALSE);
	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_text), FALSE);
	gtk_paned_add2 (GTK_PANED(hpaned), note_text);

	// テキスト標示用

	dict_scroll = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (dict_scroll),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_ALWAYS);
	dict_viewport = gtk_viewport_new (
		gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(dict_scroll)),
		gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(dict_scroll)));
	gtk_container_add (GTK_CONTAINER (dict_scroll), dict_viewport);

	label = gtk_label_new(_("Text"));
	gtk_notebook_append_page(GTK_NOTEBOOK(note_text),dict_scroll, label);

	// 侯補一覧標示用

	entry_box = gtk_vbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(entry_box), 5);

	label = gtk_label_new(_("Candidate"));
	gtk_notebook_append_page(GTK_NOTEBOOK(note_text),entry_box, label);



	dict_area = create_content_area(0, 0);
	dict_area->show_func = show_text;

	gtk_signal_connect( GTK_OBJECT(dict_area->area),"button_press_event",
			    (GtkSignalFunc)show_right_menu, (gpointer)dict_area);

	gtk_signal_connect( GTK_OBJECT(dict_area->area),"configure_event",
			    (GtkSignalFunc)configure_event, (gpointer)dict_area);

	gtk_container_add(GTK_CONTAINER(dict_viewport), dict_area->area);


	status_bar = gtk_statusbar_new();
	context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar),
						  "mycontext");
	gtk_box_pack_start(GTK_BOX(vbox), status_bar, FALSE, TRUE, 0);


	gtk_widget_show_all(vbox);


	nmenu_items = sizeof (dict_menu_items) / sizeof (dict_menu_items[0]);
	for(i=0 ; i<nmenu_items ; i++){
		dict_menu_items[i].path = _(dict_menu_items[i].path);
	}
	dict_item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<dict>", 
						  NULL);
	gtk_item_factory_create_items (dict_item_factory, nmenu_items, 
				       dict_menu_items, NULL);


	return(vbox);
}

static void search_selection()
{
	guchar *buffer;
	gint len;
	GdkWChar wc_buff[65536];
	LOCATION *loc;
	GList *loc_item;


	if (dict_area->selection_start == NULL)
	{
		return;
	}
	else
	{
		len = 0;
		if(dict_area->direction == DIRECTION_FORWARD){
			loc_item = dict_area->selection_start;
			while(loc_item){
				loc = (LOCATION *)(loc_item->data);
				if(loc->type == LOCATION_CHAR){
					wc_buff[len] = loc->wc;
					len++;
					wc_buff[len] = 0x00000000;
				}
				if(loc_item == dict_area->selection_end)
					break;
				loc_item = g_list_next(loc_item);
			}
		} else {
			loc_item = dict_area->selection_end;
			while(loc_item){
				loc = (LOCATION *)(loc_item->data);
				if(loc->type == LOCATION_CHAR){
					wc_buff[len] = loc->wc;
					len++;
					wc_buff[len] = 0x00000000;
				}
				if(loc_item == dict_area->selection_start)
					break;
				loc_item = g_list_previous(loc_item);
			}
		}
		buffer = gdk_wcstombs(wc_buff);
	}
	
	gtk_entry_set_text(GTK_ENTRY(word_entry), buffer);
	clear_tree(tree_root);
	ebook_search(buffer);	
	save_word_history(buffer);
}

void claim_clipboard_owner()
{
	gint len;
	GdkWChar wc_buff[65536];
	LOCATION *loc;
	GList *loc_item;
	
	gtk_selection_owner_set(dict_area->area, clipboard_atom,GDK_CURRENT_TIME);

	if(clipboard != NULL)
		free(clipboard);

	len = 0;
	if(dict_area->direction == DIRECTION_FORWARD){
		loc_item = dict_area->selection_start;
		while(loc_item){
			loc = (LOCATION *)(loc_item->data);
			if(loc->type == LOCATION_CHAR){
				wc_buff[len] = loc->wc;
				len++;
				wc_buff[len] = 0x00000000;
			}
			if(loc_item == dict_area->selection_end)
				break;
			loc_item = g_list_next(loc_item);
		}
	} else {
		loc_item = dict_area->selection_end;
		while(loc_item){
			loc = (LOCATION *)(loc_item->data);
			if(loc->type == LOCATION_CHAR){
				wc_buff[len] = loc->wc;
				len++;
				wc_buff[len] = 0x00000000;
			}
			if(loc_item == dict_area->selection_start)
				break;
			loc_item = g_list_previous(loc_item);
		}
	}
	clipboard = gdk_wcstombs(wc_buff);
}


void show_text(BOOK_INFO *binfo, char *text){
	gint x,y;
	gint max_width, max_height;
	GdkCursor *cursor;

  
	g_assert(text != NULL);
	g_assert(dict_area != NULL);

//	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
	gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 0);



//	g_print("show_text :\n%s\n", text);


	if(text[strlen(text)-1] == '\n')
		text[strlen(text)-1] = '\0';

	// スクロールバーの位置を先頭に戻す

	gtk_adjustment_set_value(
		gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(dict_scroll)), 0);

	if(dict_area->canvas)
		destroy_canvas(dict_area->canvas);
	dict_area->canvas = create_canvas(window);
	dict_area->canvas->content_area = dict_area;

	clear_content_area(dict_area);

	max_width = dict_viewport->allocation.width - 4;
	max_height = dict_viewport->allocation.height - 4;

	simulate_canvas(dict_area->canvas, binfo, text, max_width, max_height);
	resize_content_area(dict_area, dict_area->canvas->max_width, dict_area->canvas->height);
	
	create_pixmap(dict_area->canvas);
	clear_canvas(dict_area->canvas);
	fill_canvas(dict_area->canvas);
	draw_canvas(dict_area->canvas, binfo, text);

/*
	cursor = gdk_cursor_new (CURSOR_NORMAL);
	gdk_window_set_cursor (dict_area->area->window, cursor);
	gdk_cursor_destroy (cursor);
*/
	gtk_widget_show_all(dict_area->area);

	gtk_adjustment_set_value(
		gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(dict_scroll)), 0);

}

void select_any_search()
{
	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Automatic Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);
}

void select_exactword_search()
{
	if(menu_exactword_search == TRUE)
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Exactword Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);
}

void select_word_search()
{
	if(menu_word_search == TRUE)
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Forward Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);
}

void select_endword_search()
{
	if(menu_endword_search == TRUE)
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Backward Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);
}

void select_keyword_search()
{
	if(menu_keyword_search == TRUE)
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Keyword Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);
}

void select_multi_search()
{
	if(menu_multi_search == TRUE)
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Multiword Search"));;
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 1);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), TRUE);

	if(bauto_lookup)
		toggle_auto();

}

void select_fulltext_search()
{
	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Fulltext Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 0);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);

	if(bauto_lookup)
		toggle_auto();

}


#ifdef ENABLE_WEBSEARCH

void select_internet_search()
{
	eb_web = 1;
	gtk_notebook_set_page(GTK_NOTEBOOK(note_text), 0);
	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo_method)->entry), _("Internet Search"));
	gtk_notebook_set_page(GTK_NOTEBOOK(note_tree), 2);
//	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(note_tree), FALSE);

	if(bauto_lookup)
		toggle_auto();

}

#endif



syntax highlighted by Code2HTML, v. 0.9.1