/* $Id: gui_checkbox.c,v 1.3 2013/04/03 21:52:27 steffen Exp $ */
/***************************************************************************
 *            gui_checkbox.c
 *  
 *  Copyright (C) 2013  Steffen Pohle
 *  steffen@gulpe.de
 ***************************************************************************
 * checkbox gui element
 ***************************************************************************/

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

/****************************************************************************
 * label
 */
void gui_checkbox_draw (GUIItem *item) {
	GUICheckbox *checkbox = NULL;
	struct line_style ls;
	if (item) checkbox = (GUICheckbox*) item->data;

	ls.c = color[COLOR_white][3];
	ls.width = 1.0;
	ls.borderc = ls.c;
	draw_rectangle (currentwin->screen, item->x , item->y, item->x + 15, item->y + 15, ls);
	if (checkbox->checked) {
		gfx_draw_line (currentwin->screen, item->x , item->y, item->x + 15, item->y + 15, ls);
		gfx_draw_line (currentwin->screen, item->x + 15 , item->y, item->x, item->y + 15, ls);
	}
	gfx_draw_text (currentwin->screen, item->x + 17, item->y, checkbox->text, &ls.c);
};


GUIItem *gui_checkbox_new (char *text, int initialvalue, int x, int y) {
	GUIItem *item = (GUIItem*) ml_malloc (sizeof (GUICheckbox) + sizeof (GUIItem) + POINTERALIGNMENT);
	GUICheckbox *checkbox = NULL;
	if (item) checkbox = (GUICheckbox*) item->data;

	if (text == NULL) checkbox->text[0] = 0;
	else strncpy (checkbox->text, text, GUI_TEXTLEN);
	item->x = x;
	item->y = y;
	item->w = 15;
	item->h = 15;

	checkbox->checked = initialvalue;
	checkbox->callback_changed = NULL;
	item->type = GUI_CHECKBOX;

	return item;
};


int gui_checkbox_event (GUIItem *item, GUIEvent *event) {
	GUICheckbox *checkbox = NULL;
	if (item) checkbox = (GUICheckbox*) item->data;

	if (item == NULL || item->type != GUI_CHECKBOX) {
		d_printf ("GUIButton %p not type GUICheckbox", item);
		errorexit (-1);
	}

	d_printf ("checkbox:%s %d,%d,%d,%d    %d,%d", checkbox->text, item->x, item->y, item->w, item->h, event->guiwin_mpos.x, event->guiwin_mpos.y);
	if (event->event == EGUI_MOUSEPRESSED) {
		checkbox->checked = !checkbox->checked;
		if (checkbox->callback_changed != NULL) {
			gui_set_focus (NULL);
			checkbox->callback_changed ();
	    }
		currentwin->screen_changed = 1;
		draw();
		return 1;
	}

	return 0;
};

