/* $Id: gui_list.c,v 1.13 2014/02/12 21:21:07 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"

#define GUI_ARROW_WIDTH 16
#define GUI_ARROW_HEIGHT 16

/****************************************************************************
 * list
 */
void gui_list_draw (GUIItem *item) {
	struct line_style ls;
	int max, i, x, y, j;
	GUIList *list = NULL;

	if (item == NULL) return;
	else list = (GUIList *) item->data;
	
	ls.width = 1.0;
	if (currentwin->focus == item) 
		ls.c = ls.borderc = color[COLOR_white][3];
	else 
		ls.c = ls.borderc = color[COLOR_white][2];
	gfx_draw_line (currentwin->screen, item->x, item->y, item->x+item->w, item->y, ls);
	gfx_draw_line (currentwin->screen, item->x+item->w, item->y, item->x+item->w, item->y+item->h, ls);
	gfx_draw_line (currentwin->screen, item->x+item->w, item->y+item->h, item->x, item->y+item->h, ls);
	gfx_draw_line (currentwin->screen, item->x, item->y+item->h, item->x, item->y, ls);

	/* find last max */
	max = 0;
	if (list->data == NULL) max = 0;
	else for (max = 0; list->data[max] != NULL; max++);

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

	for (i = list->vs; i < (list->vs + item->h/16) && i < max; i++) {
		if (i == list->selected) gfx_draw_text (currentwin->screen, item->x + 5, item->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_red][2]);
		else gfx_draw_text (currentwin->screen, item->x + 5, item->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 = item->y + item->h;
		else y = item->y;
		x = item->x + item->w;
		for (j = 0; j <= GUI_ARROW_WIDTH/2; j++) {
			gfx_draw_line (currentwin->screen, x - GUI_ARROW_WIDTH/2, y, x-j, y + i*GUI_ARROW_HEIGHT, ls);
			gfx_draw_line (currentwin->screen, x - GUI_ARROW_WIDTH/2 -j, y+ i*GUI_ARROW_HEIGHT, x-GUI_ARROW_WIDTH/2, y, ls);
		}
	}
};


/*
 * event handling 
 */
int gui_list_event (GUIItem *item, GUIEvent *event) {
	int x = event->guiwin_mpos.x - item->x;
	int y = event->guiwin_mpos.y - item->y;
	int i;
	GUIList *list = NULL;
	static GUIItem *olditem = NULL;

	if (item == NULL) return 0;
	else list = (GUIList *) item->data;

	/* set focus */
	if (event->event == EGUI_MOUSEPRESSED) {
		olditem = item;
	}
	else if (event->event == EGUI_MOUSERELEASED && olditem == item &&
			x >= 0 && y >= 0 && x <= item->w && y <= item->h) {

		gui_set_focus (item);

		if (y <= GUI_ARROW_HEIGHT && x >= item->w - GUI_ARROW_WIDTH) list->vs--;
		else if (y >= item->h - GUI_ARROW_HEIGHT && x >= item->w - GUI_ARROW_WIDTH) list->vs++;
		else { /* select entry */
			i = (y-5)/16 + list->vs;
			list->selected = i;
			if (list->callback_selectitem) list->callback_selectitem (i);
		}
	}
	currentwin->screen_changed = 1;
	draw ();

	return 1;
};


GUIItem *gui_list_new (int x, int y, int w, int h) {
	GUIItem *item = (GUIItem *) ml_malloc (sizeof (GUIList) + sizeof (GUIItem) + POINTERALIGNMENT);
	GUIList *list = (GUIList *) item->data;

	item->type = GUI_LIST;
	item->x = x;
	item->y = y;
	item->w = w;
	item->h = h;
	list->selected = -1;
	list->vs = 0;
	list->callback_selectitem = NULL;
	list->data = NULL;
	return item;
};
