/* $Id: gui_mainmenu.c,v 1.4 2013/02/21 23:07:19 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_search ();
void gui_mainmenu_quit ();
void gui_mainmenu_gps ();
void gui_mainmenu_map ();
void gui_mainmenu_routing ();
void gui_mainmenu_config ();

GUIButton *mbtn_close = NULL;
GUIButton *mbtn_quit = NULL;
GUIButton *mbtn_gps = NULL;
GUIButton *mbtn_map= NULL;
GUIButton *mbtn_search = NULL;
GUIButton *mbtn_routing= NULL;
GUIButton *mbtn_config= NULL;


static GUIWindow wmmenu = {0};

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

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

	gui_show (&wmmenu);
};


void gui_mainmenu_close () {
	gui_close ();
};


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_gpsconfig () {
	gui_close ();
#ifdef HAVE_WINAPI
	gps_wince_settings ();
#else
 #ifdef HAVE_GTK
	gpssetting_wnd_show (main_wnd);
 #endif
#endif
	config_save ();
};


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 () {
	char lfn[LEN_FILENAME];
#ifndef _WINDOWSCE_
	char rfn[LEN_FILENAME] = "search.mapidx";
#endif
	gui_close ();

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


void gui_mainmenu_quit () {
	gui_close ();
#ifdef _WINDOWSCE_
	wince_taskbar (hMainWnd, TRUE);
#endif
	route_stop ();
	map_save_all ();
	config_save ();
	exit (0);
};


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);
};


