/*  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 "eb.h"
#include "selection.h"
#include "misc.h"
#include "popup.h"

static gchar previous[256];
static gboolean auto_lookup_suspended = FALSE;

extern GList *current_result;

void
selection_received (GtkWidget *widget, GtkSelectionData *data)
{

	gchar *str;
	gchar **list;
	gint count;
	gchar *text=NULL;
	SEARCH_RESULT *rp;


	if((data == NULL) || (data->data == NULL) || (data->length < 0)){
		goto END;
	}

	// STRING型ならば変換不要
	if (data->type == GDK_TARGET_STRING){
		data->data[data->length] = 0;
		str = data->data;
	// COMPOUND_TEXT型ならばカレントロケールのコード体系に変換する
	} else if ((data->type == gdk_atom_intern ("COMPOUND_TEXT", FALSE)) ||
		   (data->type == gdk_atom_intern ("TEXT", FALSE))){
		count = gdk_text_property_to_text_list (data->type,
							data->format, 
							data->data,
							data->length,
							&list);

		if((count == 0) || (list == NULL)){
			goto END;
		}
		str = list[0];

	} else {
		goto END;
	}


	str = strdup(str);
	remove_space(str);

	if((strcmp(previous, str) == 0) && (bauto_lookup == TRUE)){
		// 前と同じだったら何もしない
		;
	} else {
		if((strlen(str) <= auto_maxchar) &&
		   (strlen(str) >= auto_minchar)){
			gtk_entry_set_text(GTK_ENTRY(word_entry), str);
			clear_tree(tree_root);
			clear_search_result();
			ebook_search_auto(str);
			if(search_result){
				if(bshow_popup){
//					show_popup(result_head, FALSE);
					current_result = search_result;
					rp = (SEARCH_RESULT *)(search_result->data);
					current_book_info = rp->book_info;
					text = ebook_get_text(rp->book_info,
							      rp->pos_text.page, 
							      rp->pos_text.offset);
					show_popup(rp->book_info, text);
					free(text);
				} else {
					show_result_tree();
				}
				save_word_history(str);
			} else {
				current_result = NULL;
				current_book_info = NULL;
				if(bshow_popup){
//					show_popup(NULL, _("No hit."), FALSE);
					beep();
				} else {
					show_text(0, _("No hit."));
				}
			}
			
			sprintf(previous, "%s", str);
		}

	}
	
	free(str);

 END:
	if(bauto_lookup){
		if(tag_timeout != 0)
			gtk_timeout_remove(tag_timeout);
		tag_timeout = gtk_timeout_add(auto_interval, copy_clipboard,NULL);
	}
	return;
}


gint copy_clipboard(gpointer data){
	
	static GdkAtom ctext_atom = GDK_NONE;

/*	
	xwindow = XGetSelectionOwner (gdk_display, GDK_SELECTION_PRIMARY);
	if (xwindow == None){
		return(TRUE);
	}
*/
	gtk_entry_set_text(GTK_ENTRY(hidden_entry), "");
	// COMPOUND_TEXTを要求
	if (ctext_atom == GDK_NONE)
		ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
	gtk_selection_convert (hidden_entry, GDK_SELECTION_PRIMARY, ctext_atom, 
			       GDK_CURRENT_TIME);
	
	return (FALSE);
}

void auto_lookup_suspend()
{
	if(bauto_lookup) {
		auto_lookup_suspended = TRUE;
		if(tag_timeout != 0)
			gtk_timeout_remove(tag_timeout);
	}
}

void auto_lookup_resume()
{
	if(bauto_lookup && auto_lookup_suspended) {
		auto_lookup_suspended = FALSE;
		if(tag_timeout != 0)
			gtk_timeout_remove(tag_timeout);
		tag_timeout = gtk_timeout_add(auto_interval, copy_clipboard, NULL);
	}
}


syntax highlighted by Code2HTML, v. 0.9.1