/* $Id: gui_softkeyboard.c,v 1.10 2013/06/18 22:24:21 steffen Exp $ */
/***************************************************************************
 *            gui_softkeyboard.c
 *  
 *  Copyright (C) 2013  Steffen Pohle
 *  steffen@gulpe.de
 ***************************************************************************
 * softkeyboard: self written softkeyboard.
 ***************************************************************************/

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

GUISoftkeyboard *softkeyb = NULL;

void gui_softkeyb_getpos (int row, int btnnr, int *x1, int *x2, int *y1, int *y2);


void gui_softkeyb_show (int enable) {
	/* keys which are displayed.. */
	char *line[GUI_SOFTKEYB_MODE_MAX][GUI_SOFTKEYB_Y] = {
		{ 	"qwertzuiopü",
			"asdfghjklöä",
			"yxcvbnm,.-/" },
		{ 	"QWERTZUIOPÜ",
			"ASDFGHIKLÖÄ",
			"YXCVBNM;:+\\" },
		{ 	"0123456789=",
			"{([])}     ",
			"!\"§$%&<>~  " }};
	int i, j, x, size;

	/* disable software keyboard */
	if (cfg.softkeyboard == 0 || enable == 0) {
		if (softkeyb) softkeyb->enabled = FALSE;
		return;
	}

	/* check for allocation.. */
	if (softkeyb == NULL) {
		softkeyb = (GUISoftkeyboard*) ml_malloc (sizeof (GUISoftkeyboard));
		memset (softkeyb, 0x0, sizeof (GUISoftkeyboard));
	}

	/* reset data */
	for (i = 0; i < GUI_SOFTKEYB_MODE_MAX; i++) for (j = 0; j < 3; j++) {
		char *str = line[i][j];
		for (x = 0; x < GUI_SOFTKEYB_X; x++) {
			softkeyb->keys[i][j][x] = u8_decode (str, &size);
			str += size;
		}
	}

	softkeyb->enabled = TRUE;
	softkeyb->mode = GUI_SOFTKEYB_MODE_NORM;
	softkeyb->last_line = -1;
	softkeyb->last_col = -1;
};


/* only preparing the screen image... copying to the screen will be done later... */
static float _linepos[2][4] = { {  5.0, 25.0, 45.0, 65.0 },
								{ 20.0, 40.0, 60.0, 95.0 } };
static float _btnpos[2][8] = { {  2.0,  6.0, 10.0, 16.0, 28.0, 70.0, 80.0, 90.0 },
							   {  6.0, 10.0, 16.0, 26.0, 68.0, 80.0, 90.0, 98.0 } };

void gui_softkeyb_getpos (int row, int btnnr, int *x1, int *y1, int *x2, int *y2) {
	if (row < 3) {
		*x1 = 5 + (btnnr * (softkeyb->screen->width - 10)) / GUI_SOFTKEYB_X;
		*x2 = 5 + ((btnnr + 1) * (softkeyb->screen->width - 10)) / GUI_SOFTKEYB_X;
		*y1 = (_linepos[0][row] * softkeyb->screen->height)/100;
		*y2 = (_linepos[1][row] * softkeyb->screen->height)/100;
	}
	else if (row == 3) {
		*x1 = (_btnpos[0][btnnr] * softkeyb->screen->width)/100;
		*x2 = (_btnpos[1][btnnr] * softkeyb->screen->width)/100;
		*y1 = (_linepos[0][row] * softkeyb->screen->height)/100;
		*y2 = (_linepos[1][row] * softkeyb->screen->height)/100;
	}
};

void gui_softkeyb_draw () {
	struct line_style ls;
	int i, j, x1, x2, y1, y2;
	char c[16] = { 0 };

	if (softkeyb == NULL || gfx_screensize.x < 0 || softkeyb->enabled == FALSE) return;
	if (softkeyb->screen == NULL || softkeyb->screen->width != gfx_screensize.x) {
		if (softkeyb->screen) gfx_img_free (softkeyb->screen);
		softkeyb->screen = gfx_img_alloc (gfx_screensize.x, gfx_screensize.y * 0.8);
	}

	/* setup the lines... */
	for (j = 0; j < 2; j++) for (i = 0; i < 5; i++) {
			if (i < 4) softkeyb->linepos[j][i] = _linepos[j][i] * softkeyb->screen->width;
			softkeyb->btnpos[j][i] = _btnpos[j][i] * softkeyb->screen->height;
		}

	/* draw border */
	ls.c = color[COLOR_red][2];
	ls.width = 1;
	gfx_clear (softkeyb->screen, &color[COLOR_white][1]);
	draw_rectangle (softkeyb->screen, 1, 1, softkeyb->screen->width-2, softkeyb->screen->height-2, ls);

	/* draw 3 key rows.. */
	ls.c = color[COLOR_white][2];
	ls.width = 1;
	for (j = 0; j < 3; j++) {
		for (i = 0; i < GUI_SOFTKEYB_X; i++) {
			gui_softkeyb_getpos (j, i, &x1, &y1, &x2, &y2);
			if (softkeyb->last_col == i && softkeyb->last_line == j)
				draw_fillrectangle (softkeyb->screen, x1, y1, x2, y2, &color[COLOR_white][0]);
			draw_rectangle (softkeyb->screen, x1, y1, x2, y2, ls);
			u8_encode (c, softkeyb->keys[softkeyb->mode][j][i]);
			gfx_draw_text (softkeyb->screen, x1+(x2-x1-12)/2, y1+(y2-y1-12)/2, c, &color[COLOR_white][3]);
		}
	}

	/* draw the last keys */
	for (j = 3, i = 0; i < 8; i++) {
		gui_softkeyb_getpos (j, i, &x1, &y1, &x2, &y2);
		if (softkeyb->last_col == i && softkeyb->last_line == 3)
			draw_fillrectangle (softkeyb->screen, x1, y1, x2, y2, &color[COLOR_white][0]);
		draw_rectangle (softkeyb->screen, x1, y1, x2, y2, ls);
		switch (i) {
		case (0):
			snprintf (c, 16, _("<"));
			break;
		case (1):
			snprintf (c, 16, _(">"));
			break;
		case (2):
			snprintf (c, 16, _("SYM"));
			break;
		case (3):
			snprintf (c, 16, _("SHIFT"));
			break;
		case (4):
			snprintf (c, 16, _("   "));
			break;
		case (5):
			snprintf (c, 16, _("Del"));
			break;
		case (6):
			snprintf (c, 16, _("Enter"));
			break;
		case (7):
			snprintf (c, 16, _("Close"));
			break;
		}
		gfx_draw_text (softkeyb->screen, x1+(x2-x1-8*strlen (c))/2, y1+(y2-y1-12)/2, c, &color[COLOR_white][3]);
	}
};


int gui_softkeyb_event (GUIEvent *event) {
	int i, j, x1, y1, x2, y2;
	GUIEvent newevent;
	iPoint mp = { event->scr_mpos.x - softkeyb->offset.x, event->scr_mpos.y - softkeyb->offset.y };

	if (event->event != EGUI_MOUSEPRESSED && event->event != EGUI_MOUSERELEASED) return 0;

	/* check if we pressed one of the keys.. */
	for (j = 0; j < 3; j++)
		for (i = 0; i < GUI_SOFTKEYB_X; i++) {
			gui_softkeyb_getpos (j, i, &x1, &y1, &x2, &y2);
			if (x1 <= mp.x && mp.x <= x2 && y1 <= mp.y && mp.y <= y2) {
				if (event->event == EGUI_MOUSEPRESSED) {
					d_printf ("softkey: %d %d %d", softkeyb->mode, j, i);
					softkeyb->last_line = j;
					softkeyb->last_col = i;
					newevent.key = softkeyb->keys[softkeyb->mode][j][i];
					newevent.keychar = softkeyb->keys[softkeyb->mode][j][i];
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
				}
				else {
					softkeyb->last_line = -1;
					softkeyb->last_col = -1;
				}
				currentwin->screen_changed = 1;
				draw ();
				return 1;
			}
		}

	for (i = 0; i < 8; i++) {
		gui_softkeyb_getpos (3, i, &x1, &y1, &x2, &y2);
		if (x1 <= mp.x && mp.x <= x2 && y1 <= mp.y && mp.y <= y2) {
			if (event->event == EGUI_MOUSEPRESSED) {
				softkeyb->last_line = 3;
				softkeyb->last_col = i;
				switch (i) {
				case (0):
					newevent.key = GUIKEY_LEFT;
					newevent.keychar = 0x0;
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
					break;
				case (1):
					newevent.key = GUIKEY_RIGHT;
					newevent.keychar = 0x0;
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
					break;
				case (2):
					softkeyb->mode = (softkeyb->mode == GUI_SOFTKEYB_MODE_SYMBOL) ? 0 : GUI_SOFTKEYB_MODE_SYMBOL;
					break;
				case (3):
					softkeyb->mode = (softkeyb->mode == GUI_SOFTKEYB_MODE_SHIFT) ? 0 : GUI_SOFTKEYB_MODE_SHIFT;
					break;
				case (4):
					newevent.key = ' ';
					newevent.keychar = u8_decode (" ", NULL);
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
					return 1;
					break;
				case (5):
					newevent.key = 0x08;
					newevent.keychar = 0x0;
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
					return 1;
					break;
				case (6):
					newevent.key = 0x0d;
					newevent.keychar = 0x0;
					newevent.event = EGUI_KEYCHAR;
					newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1;
					newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1;
					gui_entry_event (currentwin->focus, &newevent);
					return 1;
					break;
				case (7):
					softkeyb->enabled = 0;
					currentwin->screen_changed = 1;
					draw ();
					return 1;
					break;
				}
			}
			else {
				softkeyb->last_line = -1;
				softkeyb->last_col = -1;
			}
		}
	}

	return 0;
};
