/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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 <gtk/gtk.h>
#include "Objet.h"
#include "Objet_Liste.h"
#include "Objet_Icone.h"
#include "Interface/Root_Interface.h"
#include "Declaration_Constante.h"
#include "Image.h"
#include "Objet_Bulle.h"
#include "Debug.h"
#include "Gconf.h"

static void Double_Clic_Icone (GtkIconView * icon_view, GtkTreePath * tree_path, gpointer user_data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	model = gtk_icon_view_get_model (GTK_ICON_VIEW (icon_view));
	if (gtk_tree_model_get_iter (model, &iter, tree_path))
		gtk_tree_model_get (model, &iter, POINTER_LIST, &Objet, -1);
	else {
	
		return;
	}
	if (Objet->Action_Double_Clique != NULL)
		(*Objet->Action_Double_Clique) ((gpointer *) Objet);
}


gboolean Selectionne_Icone (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTreeModel *model = NULL;
	GtkTreePath *path = NULL;
	GtkTreeIter iter;
	gint      x, y;

	if (event->type == GDK_BUTTON_PRESS) {
		GdkEventButton *bev = (GdkEventButton *) event;

		model = gtk_icon_view_get_model (GTK_ICON_VIEW (widget));
		gdk_window_get_pointer (event->window, &x, &y, NULL);
		if ((path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (widget), x, y)) == NULL) {
			return FALSE;
		}
		gtk_tree_model_get_iter (model, &iter, path);
		gtk_tree_model_get (model, &iter, POINTER_LIST, &Objet, -1);
		Objet_Selectionner = Objet;
		Modifie_Etat_Bouton (Objet);
		switch (bev->button) {
		    case 1:
			    if (Objet->Win_Bulle != NULL)
				    Fige_La_Bulle ();
			    if (Objet->Action_Selection != NULL)
				    (*Objet->Action_Selection) ((gpointer *) Objet);
			    break;
		    case 2:
			   
			   
			    break;
		    case 3:
			    if (Objet->Win_Bulle != NULL)
				    gdk_window_raise (Objet->Win_Bulle->window);
			    if (Objet->Action_Clique_Droit != NULL)
				    (*Objet->Action_Clique_Droit) ((gpointer *) Objet, bev);
			    break;
		}
	}
	return FALSE;
}


gboolean Souris_Sort_Icone (GtkWidget * widget, GdkEventCrossing * event, gpointer user_data)
{
	return FALSE;
}


gboolean Icone_out_event (GtkWidget * widget, GdkEventFocus * event, gpointer user_data)
{
	if (gdk_window_at_pointer (NULL, NULL) == NULL)
		Cacher_Fenetre_Bulle ();
	return FALSE;
}

/*gboolean win_bulle_leave_notify_event (GtkWidget * widget, GdkEventCrossing * event, gpointer user_data)
{
	printf ("Sort liste icone\n");
	Cacher_Fenetre_Bulle ();
	return FALSE;
}*/

GtkWidget *Create_Icone (gboolean TRIER, gboolean Bulle, gboolean DragDrop)
{
	GtkTreeModel *model = NULL;
	GtkWidget *view = NULL;
	GtkListStore *store = NULL;

	scrolledwindow_Icone_Liste = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow_Icone_Liste);
	gtk_container_add (GTK_CONTAINER (viewport_Modules), scrolledwindow_Icone_Liste);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow_Icone_Liste), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	store = gtk_list_store_new (NUM_COLS_LIST, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_INT, G_TYPE_BOOLEAN,GDK_TYPE_COLOR);
	if (TRIER) {
		gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), sort_func, NULL, NULL);
		gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
	}
	model = GTK_TREE_MODEL (store);
	view = gtk_icon_view_new_with_model (model);

	
	gtk_icon_view_set_item_width (GTK_ICON_VIEW (view), Lecture_Gconf_Int (INT_ICONLIST_ICON_WIDTH));
	gtk_widget_show (view);
	g_object_unref (model);	
	gtk_icon_view_set_text_column (GTK_ICON_VIEW (view), COL_TEXT_LIST);
	gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (view), COL_ICON_LIST);
	gtk_container_add (GTK_CONTAINER (scrolledwindow_Icone_Liste), view);
	if (DragDrop) {
/*
		enum {
			TARGET_STRING,
			TARGET_URL
		};
		static GtkTargetEntry target_table[] = {
			{"STRING", 0, TARGET_STRING},
			{"text/plain", 0, TARGET_STRING},
			{"text/uri-list", 0, TARGET_URL}
		};

		static guint n_targets = sizeof (target_table) / sizeof (target_table[0]);

		
	
		g_signal_connect ((gpointer) view, "drag_data_get", G_CALLBACK (source_drag_data_get), NULL);*/
	}
	g_signal_connect (view, "item_activated", G_CALLBACK (Double_Clic_Icone), store);
	g_signal_connect ((gpointer) view, "focus_in_event", G_CALLBACK (Icone_out_event), NULL);
	g_signal_connect ((gpointer) view, "focus_out_event", G_CALLBACK (Icone_out_event), NULL);
	if (Bulle) {
#ifdef         __BULLE_PORT_ENABLE
		g_signal_connect ((gpointer) view, "motion_notify_event", G_CALLBACK (show_win_bulle_Icone), NULL);
#endif
	
	}
	g_signal_connect ((gpointer) view, "button-press-event", G_CALLBACK (Selectionne_Icone), view);

	return view;
}
