/* $Id: gui.c,v 1.28 2014/03/06 22:21:46 steffen Exp $ */
/***************************************************************************
 *            gui.c
 *
 *  2011-03-10
 *  Copyright (C) 2011  Steffen Pohle
 *  steffen@gulpe.de
 ****************************************************************************/

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

GUIWindow *currentwin = NULL;

int gui_is_inside (int x, int y, int w, int h, iPoint pos) {
	if (x <= pos.x && x+w >= pos.x && y <= pos.y && y+h >= pos.y) return 1;
	else return 0;
};


/* 
 * show window
 */
void gui_show (GUIWindow *win) {
	win->parent = currentwin;
	currentwin = win;
	draw ();
};


/*
 * closes current windows
 */
void gui_close () {
	d_printf ("GUI close: %p", currentwin);
	GUIWindow *win;
	win = currentwin->parent;

	if (currentwin->callback_close != NULL)	currentwin->callback_close ();
	currentwin = win;
	draw ();
};


/*
 * draw current gui
 */
void gui_draw () {
	int i;
	static time_t lastupdate;
	time_t now = time (NULL);
	struct line_style ls;

	if (currentwin == NULL) gui_buttons_show ();
	if (currentwin->screen == NULL) {
		currentwin->screen = gfx_img_alloc (currentwin->w, currentwin->h);
		currentwin->screen_changed = 1;
	}

	// d_printf ("GUI draw: %p %s screen:%p(changed:%d) pos: %d,%d size:%d,%d", currentwin, currentwin->title, currentwin->screen, currentwin->screen_changed, currentwin->x, currentwin->y, currentwin->w, currentwin->h);
	if (currentwin && (currentwin->screen_changed || now-1 > lastupdate || now < lastupdate)) {
		lastupdate = now;
		if ((currentwin->style & WGUI_S_NOTITLE) == WGUI_S_NOTITLE) {
			gfx_clear (currentwin->screen, &color[COLOR_white][0]);
		}
		else {
			ls.width = 1.0;
			ls.c = ls.borderc = color[COLOR_red][3];
			draw_polygonstart ();
			draw_polygonadd (0, 0);
			draw_polygonadd (0, currentwin->h);
			draw_polygonadd (currentwin->w, currentwin->h);
			draw_polygonadd (currentwin->w, 0);
			draw_polygonfinish (currentwin->screen, ls, &color[COLOR_white][0], 1);
			gfx_draw_text (currentwin->screen, 4, 0, currentwin->title, &color[COLOR_white][3]);
			if (currentwin->title[0] != 0) gfx_draw_line (currentwin->screen, 0, 18, currentwin->w, 18, ls);
		}

		/* draw items.. */
		for (i = 0; i < GUI_MAX_ITEM; i++) if (currentwin->items[i]) 
			switch (currentwin->items[i]->type) {
				case (GUI_BUTTON): {
						GUIButton *button = (GUIButton *) currentwin->items[i]->data;
						if (button->func_draw != NULL)	button->func_draw (currentwin->items[i]);
						else gui_button_draw (currentwin->items[i]);
					}
					break;
				case (GUI_LABEL):
					gui_label_draw (currentwin->items[i]);
					break;
				case (GUI_ENTRY):
					gui_entry_draw (currentwin->items[i]);
					break;
				case (GUI_LIST):
					gui_list_draw (currentwin->items[i]);
					break;
				case (GUI_CHECKBOX):
					gui_checkbox_draw (currentwin->items[i]);
					break;
				default:
					break;
			}
		currentwin->screen_changed = 0;
	}

	if (currentwin) {
		if ((currentwin->style & WGUI_S_VCENTER) == WGUI_S_VCENTER)
			currentwin->x = gfx_screensize.x/2 - currentwin->w/2;
		if ((currentwin->style & WGUI_S_VCENTER) == WGUI_S_VLEFT)
			currentwin->x = 0;
		if ((currentwin->style & WGUI_S_VCENTER) == WGUI_S_VRIGHT)
			currentwin->x = gfx_screensize.x - currentwin->w;
		if ((currentwin->style & WGUI_S_HCENTER) == WGUI_S_HCENTER)
			currentwin->y = gfx_screensize.y/2 - currentwin->h/2;
		if ((currentwin->style & WGUI_S_HCENTER) == WGUI_S_HTOP)
			currentwin->y = 0;
		if ((currentwin->style & WGUI_S_HCENTER) == WGUI_S_HBOTTOM)
			currentwin->y = gfx_screensize.y - currentwin->h;

		/* draw the windows or just the part of the entry which is focused
		 * and draw the softkeyb
		 */
		if (currentwin->focus && softkeyb && softkeyb->enabled) {
			int ys, yh;

			/* drawing and display softkeyb */
			gui_softkeyb_draw ();
			softkeyb->offset.x = (gfx_screensize.x-softkeyb->screen->width)/2;
			softkeyb->offset.y = gfx_screensize.y-softkeyb->screen->height;
			gfx_draw_img (NULL, softkeyb->offset.x, softkeyb->offset.y,
					softkeyb->screen->width, softkeyb->screen->height, softkeyb->screen, 0, 0);

			/* drawing part of the screen where the entry element is set up.. */
			ys = currentwin->focus->y- 16;
			yh = gfx_screensize.y-softkeyb->screen->height;
			currentwin->y = -ys;
			if ((currentwin->h - (currentwin->focus->y-16)) < yh) yh = currentwin->h - (currentwin->focus->y - 16);
			gfx_draw_img (NULL, currentwin->x, 0, currentwin->w, yh, currentwin->screen, 0, ys);
		}
		else gfx_draw_img (NULL, currentwin->x, currentwin->y, currentwin->w, currentwin->h, currentwin->screen, 0, 0);
	}
};


/*
 * eventhandling
 * return 0 for nothing done.. or 1 for eventhandling done
 */
int gui_event (GUIEvent event) {
	d_printf ("gui_event...");
	if (currentwin) {
		event.guiwin_mpos.x = event.scr_mpos.x - currentwin->x;
		event.guiwin_mpos.y = event.scr_mpos.y - currentwin->y;
		if (gui_window_event (currentwin, &event)) return 1;
	}
	else {
		event.guiwin_mpos.x = -1;
		event.guiwin_mpos.y = -1;
	}

	/* no gui active nor any window is responsible.. */	
	//d_printf ("no event..");
	switch (event.event) {
		case (EGUI_MOUSERELEASED):
			draw_mousebtnup (event.scr_mpos.x, event.scr_mpos.y, event.mousebtn);
			break;
		case (EGUI_MOUSEPRESSED):
			draw_mousebtndown (event.scr_mpos.x, event.scr_mpos.y, event.mousebtn);
			break;
		case (EGUI_MOUSEMOVE):
			draw_mousemove (event.scr_mpos.x, event.scr_mpos.y, event.mousebtn);
			break;
		default:
			break;
	}
	
	return 0;
};


void gui_event_addmousepos (GUIEvent *event, iPoint pos, int neg) {
	event->scr_mpos.x += neg ? pos.x : -pos.x;
	event->scr_mpos.y += neg ? pos.y : -pos.y;
	return;
};


void gui_set_focus (GUIItem *item) {
	if (item == NULL || item->type != GUI_ENTRY) gui_softkeyb_show (FALSE);
	currentwin->focus = item;
	currentwin->screen_changed = 1;
};
