/* $Id: gui_list.c,v 1.3 2013/02/21 23:07:19 steffen Exp $ */
/***************************************************************************
 *            gui_list.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"


/****************************************************************************
 * list
 */
void gui_list_draw (GUIList *list) {
	struct line_style ls;
	int max, i, x, y, j;
	
	ls.width = 1.0;
	if (currentwin->focus != NULL && currentwin->focus->item == (void *) list) 
		ls.c = ls.borderc = color[COLOR_white][3];
	else 
		ls.c = ls.borderc = color[COLOR_white][2];
	gfx_draw_line (currentwin->screen, list->x, list->y, list->x+list->w, list->y, ls);
	gfx_draw_line (currentwin->screen, list->x+list->w, list->y, list->x+list->w, list->y+list->h, ls);
	gfx_draw_line (currentwin->screen, list->x+list->w, list->y+list->h, list->x, list->y+list->h, ls);
	gfx_draw_line (currentwin->screen, list->x, list->y+list->h, list->x, list->y, ls);

	if (list->data == NULL) return;
	for (max = 0; list->data[max] != NULL; max++);

	/* range to display */
	i = max - list->h/16;
	if (i < list->vs) list->vs = i;
	if (list->vs < 0) list->vs = 0;

	for (i = list->vs; i < (list->vs + list->h/16) && i < max; i++) {
		if (i == list->selected) gfx_draw_text (currentwin->screen, list->x + 5, list->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_red][1]);
		else gfx_draw_text (currentwin->screen, list->x + 5, list->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_white][3]);
	}

	ls.c = ls.borderc = color[COLOR_white][3];
	for (i = -1; i < 2; i += 2) {
		if (i < 0) y = list->y + list->h;
		else y = list->y;
		x = list->x + list->w;
		for (j = 0; j <= 5; j++) {
			gfx_draw_line (currentwin->screen, x - 5, y, x-j, y + i*5, ls);
			gfx_draw_line (currentwin->screen, x - 5 -j, y+ i*5, x-5, y, ls);
		}
	}
};


/*
 * event handling 
 */
void gui_list_event (GUIList *list, GUIEvent *event) {
	int x = event->mousepos.x - list->x;
	int y = event->mousepos.y - list->y;
	int i;

	/* set focus */
	if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= list->w && y >= 0 && y <= list->h) {
			currentwin->focus = (void *) list;

			if (y <= 5 && x >= list->w - 10) list->vs--;
			else if (y >= list->h - 5 && x >= list->w - 10) list->vs++;
			else { /* select entry */
				i = (y-5)/16 + list->vs;
				list->selected = i;
				if (list->callback_selectitem) list->callback_selectitem (i);
				d_printf ("selected nr: %d", i);
			}
		}
	currentwin->screen_changed = 1;
	draw ();
};


GUIList *gui_list_new (int x, int y, int w, int h) {
	GUIList *item = (GUIList*) ml_malloc (sizeof (GUIList));
	
	item->x = x;
	item->y = y;
	item->w = w;
	item->h = h;
	item->data = NULL;
	item->selected = -1;
	item->vs = 0;
	item->callback_selectitem = NULL;
	return item;
};