/***************************************************************************
 *            gui_buttons.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"


enum {
	BTNWIN_UNDEF = 0,
	BTNWIN_ZOUT,
	BTNWIN_ZIN,
	BTNWIN_GPS,
	BTNWIN_MENU,
	BTNWIN_FAV	
};


static GUIWindow wbutton = {0};
static GUIItem	*btn_zoomin = NULL, 
				*btn_zoomout = NULL,
				*btn_gps = NULL, 
				*btn_menu = NULL, 
				*btn_fav = NULL;
void gui_buttons_closecallback ();
void gui_buttons_draw (GUIItem *btn);
void gui_buttons_zoomin();
void gui_buttons_zoomout();
void gui_buttons_gps();
void gui_buttons_menu();
void gui_buttons_fav();


void gui_buttons_show () {
	wbutton.screen_changed = 1;
	wbutton.callback_close = (void*)gui_buttons_closecallback;
	wbutton.style = WGUI_S_VCENTER | WGUI_S_HTOP;

	/* add buttons */
	if (btn_zoomin == NULL) {
		btn_zoomin = gui_button_new (_("+"), 1, 1, 30, 30);
		GUI_BUTTON_T(btn_zoomin)->callback_clicked = (void*)gui_buttons_zoomin;
		GUI_BUTTON_T(btn_zoomin)->func_draw = (void*)gui_buttons_draw;
	}
		
	if (btn_zoomout == NULL) {
		btn_zoomout = gui_button_new (_("-"), 41, 1, 30, 30);
		GUI_BUTTON_T(btn_zoomout)->callback_clicked = (void*)gui_buttons_zoomout;
		GUI_BUTTON_T(btn_zoomout)->func_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_gps == NULL) {
		btn_gps = gui_button_new (_("GPS"), 81, 1, 30, 30);
		GUI_BUTTON_T(btn_gps)->callback_clicked = (void*)gui_buttons_gps;
		GUI_BUTTON_T(btn_gps)->func_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_menu == NULL) {
		btn_menu = gui_button_new (_("M"), 121, 1, 30, 30);
		GUI_BUTTON_T(btn_menu)->callback_clicked = (void*)gui_buttons_menu;
		GUI_BUTTON_T(btn_menu)->func_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_fav == NULL) {
		btn_fav = gui_button_new (_("F"), 161, 1, 30, 30);
		GUI_BUTTON_T(btn_fav)->callback_clicked = (void*)gui_buttons_fav;
		GUI_BUTTON_T(btn_fav)->func_draw = (void*)gui_buttons_draw;
	}

	if (wbutton.screen == NULL) { 
		gui_window_new (&wbutton, 192, 32, NULL);
		gui_window_item_add (&wbutton, btn_fav);
		gui_window_item_add (&wbutton, btn_zoomin);
		gui_window_item_add (&wbutton, btn_zoomout);
		gui_window_item_add (&wbutton, btn_gps);
		gui_window_item_add (&wbutton, btn_menu);
	}

	gui_show (&wbutton);
};



void gui_buttons_closecallback () {
	d_printf ("GUI: buttons close");
	gui_window_close (&wbutton);
};


void gui_buttons_draw (GUIItem *btn) {
	int j;
	
	if (btn == btn_gps) {
		j = gps_isrunning ();
		if (j == 0) GUI_BUTTON_T(btn)->col = &color[COLOR_white][2];
		else if (j < 0) GUI_BUTTON_T(btn)->col = &color[COLOR_red][2];
		else if (view_flags & DRAW_GPSFOLLOW) GUI_BUTTON_T(btn)->col = &color[COLOR_green][3];
		else GUI_BUTTON_T(btn)->col = &color[COLOR_green][2];	
	}
		
	gui_button_draw (btn);
};



void gui_buttons_zoomin() {
	draw_setscale (-1);
};


void gui_buttons_zoomout() {
	draw_setscale (+1);
};


void gui_buttons_gps() {
	if (gps_isrunning () <= 0) {
		gps_start ();
	}
	else {
		if (view_flags & DRAW_GPSFOLLOW) {
			draw_del_flag (DRAW_GPSFOLLOW);
		}
		else {
			draw_set_flag (DRAW_GPSFOLLOW);
		}
		if (gps_file_get_state () > GPSF_NONE) gpswin_show ();
	}
};


void gui_buttons_menu() {
	gui_close ();
	gui_mainmenu_show ();
};


void gui_buttons_fav () {
	gui_close ();
	gui_fav_show ();
};
