/***************************************************************************
 *            gtk_wnd_gpssetting.c
 *
 *  06. May 2010
 *  Copyright  2010  Steffen Pohle
 *  steffen@gulpe.de
 ****************************************************************************/

/*
 *  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 <gtk/gtk.h>
#include "map.h"
#include "gtk_port.h"
#include "gps.h"
#include "system.h"

GtkWidget *gpss_wnd = NULL;
GtkWidget *gpss_device;
GtkWidget *gpss_serialdevice;
GtkWidget *gpss_filename;
GtkWidget *gpss_createlog;

/*
 * delete and destroy signals and events..
 */
void gpss_wnd_destroy (GtkWidget *widget, gpointer data) {
	gpss_wnd = NULL;
};


static gboolean gpss_wnd_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
    return FALSE; /* we can quit */
};


void gpss_wnd_selectfile () {
	GtkWidget *dialog;
	dialog = gtk_file_chooser_dialog_new (_("Open GPSLog File"), GTK_WINDOW(gpss_wnd),
				      GTK_FILE_CHOOSER_ACTION_OPEN,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		gtk_entry_set_text (GTK_ENTRY (gpss_filename), (gchar*) filename);
		g_free (filename);
	}
	gtk_widget_destroy (dialog);
};


void gpss_wnd_entryedit (GtkEditable *editable, gpointer user_data) {
	char txt[GPS_DEVICELEN];
	
	if (editable == GTK_EDITABLE(gpss_filename)) {
		snprintf (txt, GPS_DEVICELEN, "file:%s", gtk_entry_get_text (GTK_ENTRY(gpss_filename)));
		gtk_entry_set_text (GTK_ENTRY(gpss_device), txt);
	}
	else if (editable == GTK_EDITABLE(gpss_serialdevice)) {
		snprintf (txt, GPS_DEVICELEN, "serial:%s", gtk_entry_get_text (GTK_ENTRY(gpss_serialdevice)));
		gtk_entry_set_text (GTK_ENTRY(gpss_device), txt);
	}
};


void gpss_wnd_simmulationtoggle (GtkToggleButton *togglebutton, gpointer user_data) {
	char txt[GPS_DEVICELEN];

	snprintf (txt, GPS_DEVICELEN, "file:%s", gtk_entry_get_text (GTK_ENTRY(gpss_filename)));
	gtk_entry_set_text (GTK_ENTRY(gpss_device), txt);
};


void gpss_wnd_ok () {
	gps_set_device  ((char*)gtk_entry_get_text (GTK_ENTRY(gpss_device)));
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(gpss_createlog)))
		gpsflags = gpsflags | GPSF_LOG;
	else 
		gpsflags = gpsflags & (GPSF_ALL-GPSF_LOG);
};


void gpss_wnd_cancel () {
};


/*
 * creates the gpssetting windows
 */
void gpssetting_wnd_show (GtkWidget *parent) {
	GtkWidget *main_box, *box1, *box2, *button, *label, *frame;
	char *ptxt;

	if (gpss_wnd == NULL) {
		ptxt = gps_get_device ();

		gpss_wnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
		gtk_window_set_title (GTK_WINDOW(gpss_wnd), _("spOSMroute - GPS Settings"));
		gtk_container_set_border_width (GTK_CONTAINER (gpss_wnd), 4);
		gtk_window_set_modal (GTK_WINDOW(gpss_wnd), TRUE);
		gtk_window_set_transient_for (GTK_WINDOW(gpss_wnd), GTK_WINDOW(parent));

		g_signal_connect (G_OBJECT (gpss_wnd), "delete_event", G_CALLBACK (gpss_wnd_delete_event), NULL);
		g_signal_connect (G_OBJECT (gpss_wnd), "destroy", G_CALLBACK (gpss_wnd_destroy), NULL);
		main_box = gtk_vbox_new (FALSE, 1);
		gtk_container_add (GTK_CONTAINER (gpss_wnd), main_box);

		/*  button leiste */
		box1 = gtk_hbox_new (FALSE, 1);
		gtk_box_pack_end (GTK_BOX (main_box), box1, FALSE, FALSE, 4);
		
		button = gtk_button_new_from_stock (GTK_STOCK_OK);	
		gtk_box_pack_end (GTK_BOX (box1), button, FALSE, FALSE, 8);
		g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (gpss_wnd_ok), NULL);
		gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT (gpss_wnd));

		button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);	
		gtk_box_pack_end (GTK_BOX (box1), button, FALSE, FALSE, 8);
		g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (gpss_wnd_cancel), NULL);
 		gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT (gpss_wnd));

		/*
		 * device string 
		 */

		/* device */
		label = gtk_label_new (_("As GPS source you can choose a file or serial device.\nSamples: 'file:/tmp/gps.glog', 'serial:/dev/ttyUBS0',\n'serial:/dev/ttyS0,4800'"));
		gtk_box_pack_start (GTK_BOX (main_box), label, FALSE, FALSE, 4);

		box1 = gtk_hbox_new (FALSE, 1);
		label = gtk_label_new (_("Source:"));
		gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 4);
		gpss_device = gtk_entry_new ();
		gtk_box_pack_start (GTK_BOX (box1), gpss_device, TRUE, TRUE, 4);
		gtk_box_pack_start (GTK_BOX (main_box), box1, FALSE, FALSE, 1);

		gpss_createlog = gtk_check_button_new_with_label (_("Create Logfile"));
		gtk_box_pack_start (GTK_BOX (main_box), gpss_createlog, FALSE, FALSE, 1);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gpss_createlog), (gpsflags & GPSF_LOG) ? TRUE : FALSE);

		
		/* serial */
		frame = gtk_frame_new (_("Serial Device"));
		gtk_container_set_border_width (GTK_CONTAINER (frame), 8);

		box1 = gtk_vbox_new (FALSE, 1);

		box2 = gtk_hbox_new (FALSE, 1);
		label = gtk_label_new (_("TTY Device:"));
		gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 4);
		gpss_serialdevice = gtk_entry_new ();
		g_signal_connect (G_OBJECT (gpss_serialdevice), "changed", G_CALLBACK (gpss_wnd_entryedit) , NULL);
		gtk_box_pack_start (GTK_BOX (box2), gpss_serialdevice, TRUE, TRUE, 4);

		gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 4);

		gtk_container_add (GTK_CONTAINER (frame), box1);
		gtk_box_pack_start (GTK_BOX (main_box), frame, FALSE, FALSE, 1);

		/* file */
		frame = gtk_frame_new (_("Log File"));
		gtk_container_set_border_width (GTK_CONTAINER (frame), 8);
		
		box1 = gtk_vbox_new (FALSE, 1);
		label = gtk_label_new (_("Using Logfiles is only usefull for testing the code."));
		gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 4);
		
		box2 = gtk_hbox_new (FALSE, 1);
		label = gtk_label_new (_("File:"));
		gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 4);
		gpss_filename = gtk_entry_new ();
		gtk_box_pack_start (GTK_BOX (box2), gpss_filename, TRUE, TRUE, 4);
		g_signal_connect (G_OBJECT (gpss_filename), "changed", G_CALLBACK (gpss_wnd_entryedit) , NULL);
		if (strncmp (ptxt, "file:", 5) == 0)
			gtk_entry_buffer_set_text (GTK_ENTRY_BUFFER(gtk_entry_get_buffer (GTK_ENTRY(gpss_filename))), ptxt+5, strlen (ptxt+5));
	
		button = gtk_button_new_from_stock (GTK_STOCK_OPEN);
		g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (gpss_wnd_selectfile) , NULL);
		gtk_box_pack_end (GTK_BOX (box2), button, FALSE, FALSE, 4);

		gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 4);

		gtk_container_add (GTK_CONTAINER (frame), box1);
		gtk_box_pack_start (GTK_BOX (main_box), frame, FALSE, FALSE, 1);

		/* update all elements */
		gtk_entry_set_text (GTK_ENTRY(gpss_device), (gchar*) gps_get_device ());
		
		/* show all elements */
		gtk_widget_show_all (gpss_wnd);
	}
	else {
		d_printf ("%s:%d this should not happen. the setting window is modal.", __FILE__, __LINE__);
		exit (0);
	}
};
