/***************************************************************************
 *            gtk_map_screen.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 "map.h"
#include "gtk_port.h"
#include "gui.h"
#include "memoryleak.h"


/*
 * map drawarea
 */
GtkWidget *map_da = NULL;
GdkGC  *gc;
GdkGCValues gcvalues;
GdkColormap *colormap = NULL;
guint last_event_keyval;

/*
 * draw event callback
 */
gboolean map_da_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data) {
	gfx_resize (map_da->allocation.width, map_da->allocation.height); 
	draw ();
	return FALSE;
};


gboolean map_da_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
	int x = event->x;
	int y = event->y;
	int btn;
	GUIEvent gevent;

	if (event->button == 1) btn = 1;
	if (event->button == 2) btn = 4;
	if (event->button == 3) btn = 2;
	
	gevent.scr_mpos.x = x;
	gevent.scr_mpos.y = y;
	gevent.mousebtn = btn;
	gevent.event = EGUI_MOUSEPRESSED;
	if (gui_event (gevent) == 0) draw_mousebtndown (x, y, btn);
	
	return FALSE;
};


gboolean map_da_button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
	int x = event->x;
	int y = event->y;
	int btn;
	GUIEvent gevent;

	if (event->button == 1) btn = 1;
	if (event->button == 2) btn = 4;
	if (event->button == 3) btn = 2;

	gevent.scr_mpos.x = x;
	gevent.scr_mpos.y = y;
	gevent.mousebtn = btn;
	gevent.event = EGUI_MOUSERELEASED;
	if (gui_event (gevent) == 0) draw_mousebtnup (x, y, btn);

	return FALSE;
};


gboolean map_da_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer user_data) {
	int x = event->x;
	int y = event->y;
	GUIEvent gevent;
	
	gevent.scr_mpos.x = x;
	gevent.scr_mpos.y = y;
	gevent.mousebtn = 0;
	gevent.event = EGUI_MOUSEMOVE;

	if (gtk_events_pending () == 0) if (gui_event (gevent) == 0) {
		draw_mousemove (x, y, 0);
	}
	
	return FALSE;
};


gboolean map_da_key_release_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
	last_event_keyval = 0;
	return FALSE;
};


gboolean map_da_key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
	GUIEvent gevent = {0};
	int lo, hi;

	if (last_event_keyval == event->keyval) return FALSE;

	lo = event->keyval & 0x00FF;
	hi = (event->keyval & 0xFF00) > 8;

	if (hi == 0) 				gevent.keychar = lo;
	if (hi == 1 && lo == 0x08)  gevent.keychar = lo;
	if (hi == 1 && lo == 0x1b)  gevent.keychar = lo;
	if (hi == 1 && lo == 0x0d)  gevent.keychar = lo;
	if (hi == 1 && lo == 0x51)  gevent.keychar = lo;
	if (hi == 1 && lo == 0x53)  gevent.keychar = lo;

	d_printf ("keyval: '%c'  lo(%x) hi(%x)  gevent:%lx", event->keyval, lo, hi, gevent.keychar);

	gevent.event = EGUI_KEYCHAR;
	gui_event (gevent);

	main_wnd_update ();
	draw ();

	last_event_keyval = event->keyval;

	return FALSE;
};


gboolean map_da_scroll_event (GtkWidget *widget, GdkEventScroll *event, gpointer user_data) {
	int x = event->x - (map_da->allocation.width/2);
	int y = event->y - (map_da->allocation.height/2);
	float lon, lat;

	lat = -map_km2lat(((float)y) * view_scale);
	lon = map_km2lon(((float)x) * view_scale, lat);

	view_lat = view_lat + lat;
	view_lon = view_lon + lon;

	if (event->direction == GDK_SCROLL_UP) draw_setscale (- 1);
	if (event->direction == GDK_SCROLL_DOWN) draw_setscale (+ 1);

	main_wnd_update ();
	draw ();

	while (gtk_events_pending())
		gtk_main_iteration();	
	
	return FALSE;
};


/*
 * create the map drawarea
 */
GtkWidget *map_da_create () {
	map_da = gtk_drawing_area_new ();
	gtk_widget_set_size_request (map_da, 100, 100);
	gtk_widget_set_events (map_da, GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK
			| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK
			| GDK_KEY_RELEASE_MASK	| GDK_POINTER_MOTION_MASK );
	GTK_WIDGET_SET_FLAGS(map_da, GTK_CAN_FOCUS);	
	g_signal_connect (G_OBJECT (map_da), "expose_event", G_CALLBACK (map_da_expose_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "button_press_event", G_CALLBACK (map_da_button_press_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "button_release_event", G_CALLBACK (map_da_button_release_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "scroll_event", G_CALLBACK (map_da_scroll_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "key_press_event", G_CALLBACK (map_da_key_press_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "key_release_event", G_CALLBACK (map_da_key_release_event), NULL);
	g_signal_connect (G_OBJECT (map_da), "motion_notify_event", G_CALLBACK (map_da_motion_notify_event), NULL);
	gfx_resize (100, 100);

	return (void *) map_da;
};


