/* ***********************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_Bulle.h"
#include "Reseau.h"	
#include "Interface/Bulle_Port_Interface.h"
#include "Interface/Bulle_Network_Interface.h"
#include "Settings_Port.h"
#include "Debug.h"
#include "Macro.h"

gboolean  win_Bulle_Figer = FALSE;
gboolean  win_Bulle_Autoriser_A_Figer = FALSE;
GtkWidget *win_Bulle_Afficher = NULL;


void Fige_La_Bulle (void)
{
	if (win_Bulle_Autoriser_A_Figer) {
		win_Bulle_Figer = TRUE;
	}
	gdk_window_raise (win_Bulle_Afficher->window);
}


void Cacher_Fenetre_Bulle (void)
{

	if (win_Bulle_Afficher != NULL) {
	
		win_Bulle_Figer = FALSE;
		gtk_widget_hide (win_Bulle_Afficher);
	}
	win_Bulle_Afficher = NULL;
}



gboolean show_win_bulle_Reseau (GtkWidget * widget, GdkEventMotion * event, gpointer data)
{
#ifdef         __BULLE_NETWORK_ENABLE
	struct Struct_Liste_Reseau *Reseau = (struct Struct_Liste_Reseau *) data;
	GdkScreen *screen = NULL, *pointer_screen = NULL;
	gint      x, y, num_monitor, height = 0, width = 0;
	GdkRectangle monitor;
	gchar *temp = NULL;

	screen = gtk_widget_get_screen (widget);
	gdk_display_get_pointer (gdk_screen_get_display (screen), &pointer_screen, &x, &y, NULL);
	num_monitor = gdk_screen_get_monitor_at_point (screen, x, y);
	gdk_screen_get_monitor_geometry (screen, num_monitor, &monitor);
	gdk_window_get_geometry (Bulle_Reseau->window, NULL, NULL, &width, &height, NULL);

	gtk_label_set_text (GTK_LABEL (label_Network_Name), Reseau->Nom_Reseau);
	gtk_label_set_text (GTK_LABEL (label_Network_Interface), Reseau->Interface);
	temp = g_strdup_printf("%s:%d",Reseau->Ip_Agent,Reseau->Port_Agent);
	gtk_label_set_text (GTK_LABEL (label_Network_Localisation), temp);
	MYFREE(temp);
	temp = g_strdup_printf("%d",Reseau->Nb_Equipement);
	gtk_label_set_text (GTK_LABEL (label_Network_Equipement), temp);
	MYFREE(temp);
	gtk_window_move (GTK_WINDOW (Bulle_Reseau), x + 15, y + 15);
	gtk_widget_show (Bulle_Reseau);
#endif
	return FALSE;
}




gboolean show_win_bulle (GtkWidget * widget, GdkEventMotion * event, gpointer data)
{
	/*struct Struct_Modele_Objet *Objet = NULL;
	 * GdkScreen *screen = NULL, *pointer_screen = NULL;
	 * GtkTreePath *path = NULL;
	 * GtkTreeModel *model = NULL;
	 * gint      x, y, num_monitor, height = 0, width = 0;
	 * GdkModifierType state;
	 * GtkTreeIter iter;
	 * GdkRectangle monitor;
	 * 
	 * if (!win_Bulle_Figer) {     
	 * model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
	 * gdk_window_get_pointer (event->window, &x, &y, &state);
	 * if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), x, y, &path, NULL, NULL, NULL)) {
	 * printf("show_win_bulle\n");
	 * Cacher_Fenetre_Bulle ();
	 * return TRUE;
	 * }
	 * gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
	 * gtk_tree_model_get (model, &iter, POINTER, &Objet, -1);
	 * if (Objet->Win_Bulle != NULL) {
	 * screen = gtk_widget_get_screen (widget);
	 * gdk_display_get_pointer (gdk_screen_get_display (screen), &pointer_screen, &x, &y, NULL);
	 * num_monitor = gdk_screen_get_monitor_at_point (screen, x, y);
	 * gdk_screen_get_monitor_geometry (screen, num_monitor, &monitor);
	 * gdk_window_get_geometry (win_raccourci->window, NULL, NULL, &width, &height, NULL); 
	 *
	 * if (monitor.width < (x + width + 15))
	 * x = (monitor.width - width - 15);
	 * if (monitor.height < (y + height + 15))
	 * y = (monitor.height - (height * 2) - 40);
	 * gtk_window_move (GTK_WINDOW (Objet->Win_Bulle), x + 15, y + 15);
	 * gtk_widget_show (Objet->Win_Bulle);
	 * win_Bulle_Afficher = Objet->Win_Bulle;
	 * win_Bulle_Autoriser_A_Figer = TRUE;
	 * }
	 * else{
	 * Cacher_Fenetre_Bulle ();printf("show_win_bulle2\n");
	 * }
	 * } */
	return FALSE;
}


gboolean show_win_bulle_Icone (GtkWidget * widget, GdkEventMotion * event, gpointer data)
{
#ifdef         __BULLE_PORT_ENABLE
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Port *Port = NULL;
	GdkScreen *screen = NULL, *pointer_screen = NULL;
	GtkTreePath *path = NULL;
	GtkTreeModel *model = NULL;
	gchar    *Nom_Port = NULL;
	gchar    *Titre_Fenetre = NULL;
	gint      x, y, num_monitor, height = 0, width = 0;
	GdkModifierType state;
	GtkTreeIter iter;
	GdkRectangle monitor;
	gchar     Date[30];
	
	if (!win_Bulle_Figer) {
		model = gtk_icon_view_get_model (GTK_ICON_VIEW (widget));
		gdk_window_get_pointer (event->window, &x, &y, &state);
		if ((path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (widget), x, y)) == NULL) {
			Cacher_Fenetre_Bulle ();
			return TRUE;
		}
		gtk_tree_model_get_iter (model, &iter, path);
		gtk_tree_model_get (model, &iter, COL_TEXT_LIST, &Nom_Port, POINTER_LIST, &Objet, -1);
	
		if (Objet->Win_Bulle != NULL) {
			screen = gtk_widget_get_screen (widget);
			gdk_display_get_pointer (gdk_screen_get_display (screen), &pointer_screen, &x, &y, NULL);
			num_monitor = gdk_screen_get_monitor_at_point (screen, x, y);
			gdk_screen_get_monitor_geometry (screen, num_monitor, &monitor);
			gdk_window_get_geometry (win_raccourci->window, NULL, NULL, &width, &height, NULL);
		
			if (monitor.width < (x + width + 15))
				x = (monitor.width - width - 15);
			if (monitor.height < (y + height + 15))
				y = (monitor.height - (height * 2) - 40);
			gtk_window_move (GTK_WINDOW (Objet->Win_Bulle), x + 15, y + 15);
		
			Port = (struct Struct_Port *) Objet->Pointeur_Attacher;
		
			if ((Port != NULL) && (Port != Ancien_Port_Selectionner)) {
				Ancien_Port_Selectionner = Port;
				ctime_r (&Port->Derniere_Modification_Etat_Port, Date);
				Date[strlen (Date) - 1] = '\0';
				gtk_label_set_text (GTK_LABEL (label_Bulle_Port_Info_Etat), Date);
				Titre_Fenetre = g_strdup_printf ("%s (Port %d)", Nom_Port, Port->Num_Port);
				gtk_window_set_title (GTK_WINDOW (Bulle_Port), Titre_Fenetre);
				MYFREE (Titre_Fenetre);
				if (Port_Avec_Program_Associe (Port->Num_Port)) {
					win_Bulle_Autoriser_A_Figer = TRUE;
					gtk_widget_show (hbox_Bulle_Bouton);
					Afficher_Icone_Program_Associe (Port->Num_Port);
				}
				else {
					win_Bulle_Autoriser_A_Figer = FALSE;
					gtk_widget_hide (hbox_Bulle_Bouton);
				}
			}
			if (Port != NULL) {
				gtk_widget_realize (Objet->Win_Bulle);
				gtk_widget_show (Objet->Win_Bulle);
				win_Bulle_Afficher = Objet->Win_Bulle;
			}
		}
		else {
			Cacher_Fenetre_Bulle ();
		}
		MYFREE (Nom_Port);
	}
#endif
	return FALSE;
}
