/***************************************************************************
 *            gtk_wnd_main.c
 *
 *  Thu Jul 19 19:00:10 2007
 *  Copyright  2007  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 <gdk/gdkkeysyms.h>
#include "map.h"
#include "gtk_port.h"
#include "memoryleak.h"
#include "system.h"
#include "routing.h"

GtkWidget *main_wnd;
GtkWidget *statusbar;
GtkWidget *scale;
GtkWidget *positionlon;
GtkWidget *positionlat;
GtkWidget *search;
GtkWidget *progressbar;
GtkToggleAction *gpsenabled;

extern GtkActionGroup *main_menu_ac;
gint status_bar_contextid;


/******************************************************************************
 * update stausbar 
 */
void gtk_main_wnd_status (char *text, int progress) {
	static char oldtext[255];
	char sbtext[255];
	struct memoryleak_info m;
	
	/* scale */
	gtk_combo_box_set_active (GTK_COMBO_BOX(scale), view_scale);

	/* progress bar */
	if (progress != -2) {
		if (progress != -1)
			gtk_progress_bar_pulse (GTK_PROGRESS_BAR(progressbar));
		else
			gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(progressbar), 1.0);

		if (text) strncpy (oldtext, text, 255);
		snprintf (sbtext, 255, "%s", oldtext);
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR(progressbar), sbtext);
	}

	/* gps enabled menubutton? */
	gtk_toggle_action_set_active (gpsenabled, gps_isrunning ());
	ml_get_stats (&m);
};


/*
 * scale widget changed.
 */
gboolean main_wnd_search_keyrelease (GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
	gchar *text;
	struct map_search_data msres[16];
	int i, j;
	
	if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter) {
		text = gtk_combo_box_get_active_text (GTK_COMBO_BOX(widget));
		d_printf ("enter: %s", text);
		i = map_search (text, &msres[0], 16);
		g_free (text);

		if (i > 0) {
			for (j = 0; j < i; j++)
				d_printf ("res %-2d: c:'%s' n:'%s'", j, msres[j].country, msres[j].name);
		}
	}
	return FALSE;
};


/*
 * check if we can quit the programm 
 */
static gboolean main_wnd_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
    return FALSE; /* we can quit */
};


/*
 * callback function for the cyclic loop
 */
gboolean main_wnd_cyclicloop (gpointer data) {
//	d_printf ("%s:%d main_wnd_cyclicloop", __FILE__, __LINE__);
	
	while (gtk_events_pending())		// make sure we handle also other GTK events..
		gtk_main_iteration();

	app_loop ();
	
	return TRUE;
};


/*
 * to make sure the gtk subsystem will quit, without it the
 * windows just disapears but the programm is still in memory 
 */
void main_wnd_destroy (GtkWidget *widget, gpointer   data) {
	d_printf ("main_wnd_destroy");
	app_shutdown ();
	gtk_main_quit ();
};


/**********************************************************************
 *
 * the main window..
 *
 */

/*
 * creates the main window 
 */
GtkWidget *main_wnd_create () {
	GtkWidget *window;
	GtkWidget *main_vbox;
	GtkWidget *widget;
	GtkWidget *box;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW(window), _("spOSMroute"));
	gtk_window_set_default_size (GTK_WINDOW(window), 600, 400);

	g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (main_wnd_delete_event), NULL);
	g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (main_wnd_destroy), NULL);

	main_vbox = gtk_vbox_new (FALSE, 1);
	gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 1);
	gtk_container_add (GTK_CONTAINER (window), main_vbox);

	/*
	 * create our menu..
	 */
	gtk_box_pack_start (GTK_BOX (main_vbox), main_menu_create(window), FALSE, TRUE, 0);

	/*
	 * create the main part of the window (DRAWAREA)
	 */
	gtk_box_pack_start (GTK_BOX (main_vbox), map_da_create (), TRUE, TRUE, 0);

	/*
	 * create pos, search, scale, progressbar status bar
	 */
	box = gtk_hbox_new (FALSE, 1);

	widget = gtk_vseparator_new();
	gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 4);

	/* progress bar */
	progressbar = gtk_progress_bar_new ();
 	gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(progressbar), PANGO_ELLIPSIZE_END);
	gtk_box_pack_start (GTK_BOX (box), progressbar, TRUE, TRUE, 0);
	
	/* statusbar */	
	statusbar = gtk_statusbar_new ();
	status_bar_contextid = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "");
	gtk_box_pack_start (GTK_BOX (box), statusbar, TRUE, TRUE, 0);
	g_timeout_add (10, main_wnd_cyclicloop, 0);
	
	gtk_box_pack_start (GTK_BOX (main_vbox), box, FALSE, TRUE, 0);

	colormap = gdk_colormap_get_system ();
	draw_init_color ();

	return window;
};

/*
 * some other usefull functions..
 *
 *
 */
void widget_change_fontsize (GtkWidget *w, float s) {
	PangoFontDescription *pfd;
	pfd = pango_context_get_font_description(gtk_widget_get_pango_context(w));
	pango_font_description_set_size(pfd, ((int)((float)pango_font_description_get_size (pfd)*(float)s)));
	gtk_widget_modify_font(w, pfd);
};


void main_wnd_loop (long long int cur, long long int max) {
	static time_t to;
	time_t t1;

	t1 = time (NULL);
	if (to != t1) {
		app_status (-1, NULL, 0.0);
		to = t1;
	}

	if (!gtk_running) return;
	while (gtk_events_pending())
		gtk_main_iteration();	
};


void main_wnd_update () {
	if (!gtk_running) return;
	while (gtk_events_pending())
		gtk_main_iteration();	
};
