/* $Id: gui_button.c,v 1.11 2013/04/03 21:52:27 steffen Exp $ */
/***************************************************************************
 *            gui_button.c
 *  
 *  Copyright (C) 2013  Steffen Pohle
 *  steffen@gulpe.de
 ***************************************************************************
 * basic windows functions
 ***************************************************************************/

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

/****************************************************************************
 * button
 */
void gui_button_draw (GUIItem *item) {
	struct line_style ls;
	GUIButton *button = NULL;

	if (item == NULL || item->type != GUI_BUTTON) {
		d_printf ("GUIButton %p not type GUIButton", item);
		errorexit (-1);
	}
	button = (GUIButton *) item->data;
	
	ls.width = 1.0;
	ls.c = ls.borderc = color[COLOR_white][3];
//	d_printf ("gui_button_draw \"%s\" %d,%d,%d,%d", button->caption, item->x, item->y, item->w, item->h);
	
	draw_polygonstart ();
	draw_polygonadd (item->x, item->y);
	draw_polygonadd (item->x, item->y + item->h);
	draw_polygonadd (item->x + item->w, item->y + item->h);
	draw_polygonadd (item->x + item->w, item->y);
	draw_polygonfinish (currentwin->screen, ls, button->col, 1);
	gfx_draw_text (currentwin->screen, item->x + 2, item->y + 2, button->caption, button->textcol);
};


GUIItem *gui_button_new (char *caption, int x, int y, int w, int h) {
	GUIItem *item = (GUIItem*) ml_malloc (sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT);
	GUIButton *button = (GUIButton *) item->data;

	memset (item, 0x0, sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT);
	
	if (caption == NULL) button->caption[0] = 0;
	else strncpy (button->caption, caption, GUI_TEXTLEN);
	item->x = x;
	item->y = y;
	item->w = w;
	item->h = h;
	item->type = GUI_BUTTON;
	button->col = &color[COLOR_white][1];
	button->textcol = &color[COLOR_white][3];
	button->callback_clicked = NULL;
	button->func_draw = NULL;

	return item;
};


int gui_button_event (GUIItem *item, GUIEvent *event) {
	GUIButton *button = NULL;

	if (item == NULL || item->type != GUI_BUTTON) {
		d_printf ("GUIButton %p not type GUIButton", item);
		errorexit (-1);
	}
	button = (GUIButton *) item->data;

	if (button->callback_clicked != NULL && event->event == EGUI_MOUSEPRESSED) {
		gui_set_focus (NULL);
		button->callback_clicked (event->guiwin_mpos.x-item->x, event->guiwin_mpos.y-item->y);
    }

	return 1;
};

