/* $Id: gui_mainmenu.c,v 1.15 2014/03/06 22:21:46 steffen Exp $ */
/***************************************************************************
 *            gui_mainmenu.c
 *
 *  2011-03-10
 *  Copyright (C) 2011  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 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c 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, see <http://www.gnu.org/licenses/>.
 */

#include "osmroute.h"
#include "draw.h"
#include "gui.h"
#include "system.h"
#include "routing.h"

void gui_mainmenu_close ();
void gui_mainmenu_refresh ();
void gui_mainmenu_refreshidx ();
void gui_mainmenu_search ();
void gui_mainmenu_quit ();
void gui_mainmenu_routing ();
void gui_mainmenu_config ();

GUIItem *mbtn_close = NULL;
GUIItem *mbtn_quit = NULL;
GUIItem *mbtn_refreshidx = NULL;
GUIItem *mbtn_refresh = NULL;
GUIItem *mbtn_search = NULL;
GUIItem *mbtn_routing= NULL;
GUIItem *mbtn_config= NULL;


static GUIWindow wmmenu = {0};

void gui_mainmenu_show () {
	if (wmmenu.screen == NULL) gui_window_new (&wmmenu, 200, 200, _("OSMroute Menu"));
	wmmenu.screen_changed = 1;
	wmmenu.style = WGUI_S_VCENTER | WGUI_S_HCENTER;

	/* add buttons */
	if (mbtn_refresh == NULL) {
		mbtn_refresh = gui_button_new (_("Update Map"), 5, 30, 90, 20);
		GUI_BUTTON_T(mbtn_refresh)->callback_clicked = (void*)gui_mainmenu_refresh;
	}
	gui_window_item_add (&wmmenu, mbtn_refresh);
	
	if (mbtn_refreshidx == NULL) {
		mbtn_refreshidx = gui_button_new (_("Update Index"), 105, 30, 90, 20);
		GUI_BUTTON_T(mbtn_refreshidx)->callback_clicked = (void*)gui_mainmenu_refreshidx;
	}
	gui_window_item_add (&wmmenu, mbtn_refreshidx);
	
	if (mbtn_routing == NULL) {
		mbtn_routing = gui_button_new (_("Routing"), 5, 60, 190, 20);
		GUI_BUTTON_T(mbtn_routing)->callback_clicked = (void*)gui_mainmenu_routing;
	}
	gui_window_item_add (&wmmenu, mbtn_routing);
	
	if (mbtn_search == NULL) {
		mbtn_search = gui_button_new (_("Search"), 5, 90, 190, 20);
		GUI_BUTTON_T(mbtn_search)->callback_clicked = (void*)gui_mainmenu_search;
	}
	gui_window_item_add (&wmmenu, mbtn_search);
	
	if (mbtn_config == NULL) {
		mbtn_config = gui_button_new (_("Config"), 5, 120, 130, 20);
		GUI_BUTTON_T(mbtn_config)->callback_clicked = (void*)gui_mainmenu_config;
	}
	gui_window_item_add (&wmmenu, mbtn_config);
	
	if (mbtn_quit == NULL) {
		mbtn_quit = gui_button_new (_("Quit"), 145, 120, 50, 20);
		GUI_BUTTON_T(mbtn_quit)->callback_clicked = (void*)gui_mainmenu_quit;
	}
	gui_window_item_add (&wmmenu, mbtn_quit);

	if (mbtn_close == NULL) {
		mbtn_close = gui_button_new (_("Close"), 5, 155, 190, 20);
		GUI_BUTTON_T(mbtn_close)->callback_clicked = (void*)gui_mainmenu_close;
	}
	gui_window_item_add (&wmmenu, mbtn_close);

	gui_show (&wmmenu);
};


void gui_mainmenu_close () {
	gui_close ();
};


void gui_mainmenu_routing () {
	gui_close ();
	wnd_routing_show ();
};


void gui_mainmenu_config () {
	gui_close ();
	gui_config_show ();
};


void gui_mainmenu_search () {
	gui_close ();
	gui_search_show ();
};


void gui_mainmenu_setdest () {
	static int _setdest = 0;
	struct map_pos p;

	if (_setdest) {
		d_printf ("set destination routing stop");
		route_stop();
	}
	else {
		d_printf ("set destination routing start");
		_setdest = 1;
		gui_close ();
		p.lon = view_lon;
		p.lat = view_lat;
		route_start (p);
		_setdest = 0;
	}
};


void gui_mainmenu_gpsstartstop () {
	gui_close ();
	if (gps_isrunning () != 0) gps_stop ();
	else gps_start ();
};


void gui_mainmenu_mapconfig () {
	char *fn;
	
	gui_close ();
#if defined (SPOSMROUTE) && (HAVE_GTK)
	fn = dialog_getfilename ("mapdata", "mapdata", 0);
	if (fn) {
		int i;
		for (i = strlen (fn)-1; i > 0; i--) 
			if (fn[i] == '\\' || fn[i] == '/') {
				fn[i+1] = '\0';
				break;
			}
	}
#else
 #if defined(HAVE_WINCE)
	fn = select_dir_dialog ("MapData", cfg.datapath);
 #else
	fn = NULL;
 #endif
#endif
	if (fn) {
		d_printf ("config: changed data path from '%s' to '%s'.", cfg.mappath, fn);
		map_clear();
		strncpy (cfg.mappath, fn, LEN_FILENAME);
		map_clear();
		draw_redrawmap ();
	}
	config_save ();
};


void gui_mainmenu_refresh () {
	gui_close ();
	if (select_enabled) {
		struct map_pos s, e;
		
		s.lon = view_lon;
		s.lat = view_lat;
		e = select_pos;
		
		map_refreshblock (s, e, 100.0);
	}
	else {
		map_refresh (view_lon, view_lat);
	}
};


void gui_mainmenu_refreshidx () {
	static int _working = 0;

	if (_working) return;
	_working = 1;
	
#ifndef _WINDOWSCE_
	char lfn[LEN_FILENAME];
	char rfn[LEN_FILENAME] = "search.mapidx";
	gui_close ();

	map_search_get_filename (lfn, LEN_FILENAME);
	map_webload (rfn, lfn, NULL);
	map_search_sort ();
	draw_redrawmap ();	
#endif
};


void gui_mainmenu_quit () {
	gui_close ();
#ifdef _WINDOWSCE_
	wince_taskbar (hMainWnd, TRUE);
#endif
	app.status = APPSTATUS_quit;
};


void gui_mainmenu_debug() {
	gui_close ();
	if (view_flags & DRAW_DEBUG)			draw_del_flag (DRAW_DEBUG);
	else if (!(view_flags & DRAW_DEBUG))	draw_set_flag (DRAW_DEBUG);
};


