/***************************************************************************
 *            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 GUIButton *btn_zoomin = NULL, 
				 *btn_zoomout = NULL,
				 *btn_gps = NULL, 
				 *btn_menu = NULL, 
				 *btn_fav = NULL;
void gui_buttons_closecallback ();
void gui_buttons_draw (GUIButton *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 (_("+"), 0, 0, 31, 31);
		btn_zoomin->callback_clicked = (void*)gui_buttons_zoomin;
		btn_zoomin->callback_draw = (void*)gui_buttons_draw;
	}
		
	if (btn_zoomout == NULL) {
		btn_zoomout = gui_button_new (_("-"), 40, 0, 31, 31);
		btn_zoomout->callback_clicked = (void*)gui_buttons_zoomout;
		btn_zoomout->callback_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_gps == NULL) {
		btn_gps = gui_button_new (_("GPS"), 80, 0, 31, 31);
		btn_gps->callback_clicked = (void*)gui_buttons_gps;
		btn_gps->callback_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_menu == NULL) {
		btn_menu = gui_button_new (_("M"), 120, 0, 31, 31);
		btn_menu->callback_clicked = (void*)gui_buttons_menu;
		btn_menu->callback_draw = (void*)gui_buttons_draw;
	}
				
	if (btn_fav == NULL) {
		btn_fav = gui_button_new (_("F"), 160, 0, 31, 31);
		btn_fav->callback_clicked = (void*)gui_buttons_fav;
		btn_fav->callback_draw = (void*)gui_buttons_draw;
	}

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

	gui_show (&wbutton);
};



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


void gui_buttons_draw (GUIButton *btn) {
	int j;

	if (btn == btn_gps) {
		j = gps_isrunning ();
		if (j == 0) btn->col = &color[COLOR_white][2];
		else if (j < 0) btn->col = &color[COLOR_red][2];
		else if (view_flags & DRAW_GPSFOLLOW) btn->col = &color[COLOR_green][3];
		else 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 ();
		main_wnd_update();
	}
	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 ();
};
