/* $Id: gui_entry.c,v 1.18 2013/06/20 21:28:04 steffen Exp $ */
/***************************************************************************
 *            gui_entry.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"
#include "utf8.h"

/****************************************************************************
 * entry
 */
void gui_entry_draw (GUIItem *item) {
	char text1[GUI_TEXTLEN];
	char text2[GUI_TEXTLEN];
	struct line_style ls;
	GUIEntry *entry = NULL;

	if (item) entry = (GUIEntry *) item->data;
	else return;

//	d_printf ("gui_entry curpos:%d len:%d", entry->curpos, strlen(entry->text));
//	d_print_data (entry->text, strlen (entry->text)+1);

	ls.width = 1.0;
	if (currentwin->focus == (void*) 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->h, item->x+item->w, item->y+item->h, ls);
	gfx_draw_line (currentwin->screen, item->x, item->y+item->h, item->x, item->y+item->h-2, ls);
	gfx_draw_line (currentwin->screen, item->x+item->w, item->y+item->h, item->x+item->w, item->y+item->h-2, ls);

	if (entry->curpos >= strlen (entry->text)) {
		entry->curpos = strlen (entry->text);
		snprintf (text1, GUI_TEXTLEN, "%s|", entry->text);
	}
	else if (entry->curpos <= 0) {
		entry->curpos = 0;
		snprintf (text1, GUI_TEXTLEN, "|%s", entry->text);
	}
	else {
		u8_strcpy (text1, entry->text, 0, entry->curpos);
		u8_strcpy (text2, "|", 0, 1);
		u8_strncat (text1, GUI_TEXTLEN, text2);

		u8_strcpy (text2, entry->text, entry->curpos, u8_strlen (entry->text)-entry->curpos);
		u8_strncat (text1, GUI_TEXTLEN, text2);
	}
	gfx_draw_text (currentwin->screen, item->x+2, item->y, text1, &color[COLOR_white][3]);
};


/*
 * event handling 
 */
int gui_entry_event (GUIItem *item, GUIEvent *event) {
	char u8chr[12] = {0};
	GUIEntry *entry = NULL;

	if (item) entry = (GUIEntry *) item->data;

	if (event->event == EGUI_MOUSEPRESSED)	{
		gui_set_focus (item);
		gui_softkeyb_show (TRUE);
	}

	else if (event->event == EGUI_KEYCHAR) {
		d_printf ("gui_entry curpos:%d  key:%lx  keychar:%lx", entry->curpos, event->key, event->keychar);

		if (entry->curpos < 0) entry->curpos = 0;
		if (entry->curpos > u8_strlen (entry->text)) entry->curpos = u8_strlen (entry->text);

		if (event->key == 0x08) { // backspace
			d_printf ("gui_entry:backspace");
			if (entry->curpos > 0) {
				u8_strdel (entry->text, entry->curpos-1, 1);
				entry->curpos--;
			}
		}
		else if (event->key == 0x7f) { // delete
			d_printf ("gui_entry:delete");
			if (entry->curpos >= 0 && entry->curpos-1 < u8_strlen (entry->text)) {
				u8_strdel (entry->text, entry->curpos, 1);
			}
		}
		else if (event->key == 0x1b) {	// esc
			d_printf ("gui_entry:esc");
			if (entry->callback_esc) entry->callback_esc ();
		}
		else if (event->key == 0x0d) {	// enter
			d_printf ("gui_entry:enter");
			if (entry->callback_enter) entry->callback_enter ();
		}
		else if (event->key == GUIKEY_LEFT) { // left
			d_printf ("gui_entry:curpos--");
			if (entry->curpos > 0) entry->curpos--;
		}
		else if (event->key == GUIKEY_RIGHT) { // right
			d_printf ("gui_entry:curpos++");
			if (entry->curpos < u8_strlen (entry->text)) entry->curpos++;
		}
		else if (event->keychar != 0) {
			u8_encode (u8chr, event->keychar);
			u8_strninsert (entry->text, GUI_TEXTLEN, u8chr, entry->curpos);
			entry->curpos++;
		}
	}
	currentwin->screen_changed = 1;
	draw ();
	return 1;
};


GUIItem *gui_entry_new (char *text, int x, int y, int w, int h) {
	GUIItem *item = (GUIItem*) ml_malloc (sizeof (GUIItem) + sizeof (GUIEntry) + POINTERALIGNMENT);
	GUIEntry *entry = NULL;

	if (item) entry = (GUIEntry *) item->data;
	
	if (text == NULL) entry->text[0] = 0;
	else strncpy (entry->text, text, GUI_TEXTLEN);
	item->x = x;
	item->y = y;
	item->w = w;
	item->h = h;
	item->type = GUI_ENTRY;
	entry->callback_changed = NULL;
	entry->callback_enter = NULL;
	entry->callback_esc = NULL;

	return item;
};


void gui_entry_settext (GUIItem *item, char *text) {
	GUIEntry *entry = NULL;

	if (item) entry = (GUIEntry *) item->data;
	strncpy (entry->text, text, GUI_TEXTLEN);
	entry->curpos = strlen (entry->text);
	currentwin->screen_changed = 1;
};
