/* ***********************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.
 **************************************************************************************/
#ifdef  __VCN_ENABLE

#include "Vnc.h"
#include "Interface/Root_Interface.h"
#include "Equipement.h"




static void vnc_grab(GtkWidget *vnc, GtkWidget *window)
{
	printf("vnc_grab\n");

}

static void vnc_ungrab(GtkWidget *vnc, GtkWidget *window)
{

	printf("vnc_ungrab\n");
	gtk_widget_grab_focus (window_root);
}

static void vnc_connected(GtkWidget *vnc G_GNUC_UNUSED)
{
	printf("Connected to server\n");
}

static void vnc_initialized(GtkWidget *vnc, GtkWidget *window)
{
	printf("Connection initialized\n");

	gtk_widget_show_all(window);
}

static void vnc_auth_failure(GtkWidget *vnc, const char *msg)
{
	printf("Authentication failed '%s'\n", msg ? msg : "");
}

static void vnc_desktop_resize(GtkWidget *vnc, int width, int height)
{
	printf("Remote desktop size changed to %dx%d\n", width, height);
}

static void vnc_disconnected(GtkWidget *vnc G_GNUC_UNUSED)
{
	printf("Disconnected from server\n");
	 gtk_widget_grab_focus (window_root);
}

static void send_caf1(GtkWidget *menu G_GNUC_UNUSED, GtkWidget *vnc)
{
	guint keys[] = { GDK_Control_L, GDK_Alt_L, GDK_F1 };
	printf("Sending Ctrl+Alt+F1\n");
	vnc_display_send_keys(VNC_DISPLAY(vnc), keys, sizeof(keys)/sizeof(keys[0]));
}

static void send_caf7(GtkWidget *menu G_GNUC_UNUSED, GtkWidget *vnc)
{
	guint keys[] = { GDK_Control_L, GDK_Alt_L, GDK_F7 };
	printf("Sending Ctrl+Alt+F7\n");
	vnc_display_send_keys(VNC_DISPLAY(vnc), keys, sizeof(keys)/sizeof(keys[0]));
}

static void send_cad(GtkWidget *menu G_GNUC_UNUSED, GtkWidget *vnc)
{
	guint keys[] = { GDK_Control_L, GDK_Alt_L, GDK_Delete };
	printf("Sending Ctrl+Alt+Delete\n");
	vnc_display_send_keys(VNC_DISPLAY(vnc), keys, sizeof(keys)/sizeof(keys[0]));
}

static void send_cab(GtkWidget *menu G_GNUC_UNUSED, GtkWidget *vnc)
{
	guint keys[] = { GDK_Control_L, GDK_Alt_L, GDK_BackSpace };
	printf("Sending Ctrl+Alt+Backspace\n");
	vnc_display_send_keys(VNC_DISPLAY(vnc), keys, sizeof(keys)/sizeof(keys[0]));
}





void Vnc_Console () {
	char port[1024], hostname[1024];
	char *display = NULL;
	GtkWidget *vnc = NULL;
	GtkWidget *layout = NULL;
	GtkWidget *menubar = NULL;
	GtkWidget *sendkey = NULL;
	GtkWidget *submenu = NULL;
	GtkWidget *caf1 = NULL;
	GtkWidget *caf7 = NULL;
	GtkWidget *cad = NULL;
	GtkWidget *cab = NULL;
	GtkWidget *hbox_Vnc = NULL,*fixed_Vnc_Gauche = NULL,*vbox_Vnc_Milieu = NULL,*fixed_Vnc_Milieu_Bas = NULL,*fixed_Vnc_Droite = NULL;
  	
	

	/*hbox_Vnc = gtk_hbox_new (FALSE, 0);
  	gtk_widget_show (hbox_Vnc);
	gtk_container_add (GTK_CONTAINER (viewport_Modules), hbox_Vnc);

  	fixed_Vnc_Gauche = gtk_fixed_new ();
  	gtk_widget_show (fixed_Vnc_Gauche);
  	gtk_box_pack_start (GTK_BOX (hbox_Vnc), fixed_Vnc_Gauche, TRUE,FALSE, 0);

  	vbox_Vnc_Milieu = gtk_vbox_new (FALSE, 0);
  	gtk_widget_show (vbox_Vnc_Milieu);
  	gtk_box_pack_start (GTK_BOX (hbox_Vnc), vbox_Vnc_Milieu, TRUE, FALSE, 0);


  	fixed_Vnc_Droite = gtk_fixed_new ();
  	gtk_widget_show (fixed_Vnc_Droite);
  	gtk_box_pack_start (GTK_BOX (hbox_Vnc), fixed_Vnc_Droite, TRUE, FALSE, 0);*/


	scrolledwindow_Vnc = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow_Vnc);

	gtk_container_add (GTK_CONTAINER (viewport_Modules), scrolledwindow_Vnc);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow_Vnc),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);



	viewport_Vnc = gtk_viewport_new (NULL, NULL);
  	gtk_widget_show (viewport_Vnc);
	gtk_container_add (GTK_CONTAINER (scrolledwindow_Vnc), viewport_Vnc);
	gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport_Vnc), GTK_SHADOW_NONE);
		

/*	fixed_Vnc_Milieu_Bas = gtk_fixed_new ();
  	gtk_widget_show (fixed_Vnc_Milieu_Bas);
  	gtk_box_pack_start (GTK_BOX (vbox_Vnc_Milieu), fixed_Vnc_Milieu_Bas, TRUE, FALSE, 0);*/
	

	vnc = vnc_display_new();

	layout = gtk_vbox_new(FALSE, 3);

	menubar = gtk_menu_bar_new();
	sendkey = gtk_menu_item_new_with_mnemonic("_Send Key");
	gtk_menu_bar_append(GTK_MENU_BAR(menubar), sendkey);
	submenu = gtk_menu_new();
	caf1 = gtk_menu_item_new_with_mnemonic("Ctrl+Alt+F_1");
	caf7 = gtk_menu_item_new_with_mnemonic("Ctrl+Alt+F_7");
	cad = gtk_menu_item_new_with_mnemonic("Ctrl+Alt+_Del");
	cab = gtk_menu_item_new_with_mnemonic("Ctrl+Alt+_Backspace");
	gtk_menu_append(GTK_MENU(submenu), caf1);
	gtk_menu_append(GTK_MENU(submenu), caf7);
	gtk_menu_append(GTK_MENU(submenu), cad);
	gtk_menu_append(GTK_MENU(submenu), cab);
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(sendkey), submenu);

	gtk_container_add(GTK_CONTAINER(viewport_Vnc), layout);
	gtk_container_add(GTK_CONTAINER(layout), menubar);
	gtk_container_add(GTK_CONTAINER(layout), vnc);
	


	gtk_widget_realize(vnc);

	vnc_display_open_host(VNC_DISPLAY(vnc), Equipement_Selectionner->Adresse_Ip_Char, "5900");

	vnc_display_set_keyboard_grab(VNC_DISPLAY(vnc), TRUE);
 	gtk_widget_grab_focus (VNC_DISPLAY(vnc));

	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-connected",  GTK_SIGNAL_FUNC(vnc_connected), NULL);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-initialized",  GTK_SIGNAL_FUNC(vnc_initialized), viewport_Vnc);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-disconnected",   GTK_SIGNAL_FUNC(vnc_disconnected), NULL);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-auth-failure",	   GTK_SIGNAL_FUNC(vnc_auth_failure), NULL);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-desktop-resize",  GTK_SIGNAL_FUNC(vnc_desktop_resize), NULL);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-pointer-grab",   GTK_SIGNAL_FUNC(vnc_grab), viewport_Vnc);
	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-pointer-ungrab",  GTK_SIGNAL_FUNC(vnc_ungrab), viewport_Vnc);
	gtk_signal_connect(GTK_OBJECT(caf1), "activate",   GTK_SIGNAL_FUNC(send_caf1), vnc);
	gtk_signal_connect(GTK_OBJECT(caf7), "activate",  GTK_SIGNAL_FUNC(send_caf7), vnc);
	gtk_signal_connect(GTK_OBJECT(cad), "activate",  GTK_SIGNAL_FUNC(send_cad), vnc);
	gtk_signal_connect(GTK_OBJECT(cab), "activate",   GTK_SIGNAL_FUNC(send_cab), vnc);
}
#endif
