/* $Id: gui.c,v 1.6 2013/02/21 23:07:19 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;

/* 
 * 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_white][3];
			draw_polygonstart ();
			draw_polygonadd (0, 0);
			draw_polygonadd (0, currentwin->h-1);
			draw_polygonadd (currentwin->w-1, currentwin->h-1);
			draw_polygonadd (currentwin->w-1, 0);
			draw_polygonfinish (currentwin->screen, ls, color[COLOR_white][0], 1);
			gfx_draw_text (currentwin->screen, 4, 0, currentwin->title, &color[COLOR_white][3]);
		}

		/* draw items.. */
		for (i = 0; i < GUI_MAX_ITEM; i++) 
			switch (currentwin->items[i].type) {
				case (GUI_BUTTON): {
						GUIButton *button = (GUIButton *) currentwin->items[i].item;
						if (button->callback_draw != NULL)	button->callback_draw (button);
						else gui_button_draw (button);
					}
					break;
				case (GUI_LABEL):
					gui_label_draw ((GUILabel*)currentwin->items[i].item);
					break;
				case (GUI_ENTRY):
					gui_entry_draw ((GUIEntry*)currentwin->items[i].item);
					break;
				case (GUI_LIST):
					gui_list_draw ((GUIList*)currentwin->items[i].item);
					break;
			//	case (GUI_ENTRY):
			//		gui_entry_draw ((GUIEntry*)currentwin->items[i].item);
			//		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;

		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) {
	int i;
	static int event_called = 0;
	
	if (currentwin == NULL) return 0;

	event.mousepos.x -= currentwin->x;
	event.mousepos.y -= currentwin->y;
	if (currentwin->focus == NULL && (event.mousepos.x < 0 || event.mousepos.x > currentwin->w
	    || event.mousepos.y < 0 || event.mousepos.y > currentwin->h)) {
			event_called = 0;
			return 0;
		}

    // d_printf ("event called %d  event:%d mousepos:(%d,%d)", __LINE__, event.event, event.mousepos.x, event.mousepos.y);
    
	if (event_called) return 1;
	event_called = 1;

	for (i = 0; i < GUI_MAX_ITEM; i++)
		switch (currentwin->items[i].type) {
			case (GUI_BUTTON): {
				GUIButton *button = (GUIButton *) currentwin->items[i].item;
				if (button->x <= event.mousepos.x && 
				    button->x+button->w >= event.mousepos.x &&
				    button->y <= event.mousepos.y &&
				    button->y+button->h >= event.mousepos.y) {
						if (button->callback_clicked != NULL && event.event == EGUI_MOUSEPRESSED) {
							d_printf ("GUI BUTTON PRESSED: %s", button->caption);
							button->callback_clicked (event.mousepos.x-button->x, event.mousepos.y-button->y);
							currentwin->screen_changed = 1;
							event_called = 0;					
							return 1;
						}
					}
				}
				break;

			case (GUI_LABEL):
				break;

			case (GUI_ENTRY): {
				GUIEntry *entry = (GUIEntry*) currentwin->items[i].item;
				if (entry->x <= event.mousepos.x && 
				    entry->x+entry->w >= event.mousepos.x &&
				    entry->y <= event.mousepos.y && 
				    entry->y+entry->h >= event.mousepos.y) {
						gui_entry_event (entry, &event);
						event_called = 0;
						return 1;
					}
				}
				break;

			case (GUI_LIST): {
				GUIList *list = (GUIList *) currentwin->items[i].item;
				if (list->x <= event.mousepos.x && 
				    list->x+list->w >= event.mousepos.x &&
				    list->y <= event.mousepos.y && 
				    list->y+list->h >= event.mousepos.y) {
						gui_list_event (list, &event);
						event_called = 0;
						return 1;
					}
			}
		}

	if (currentwin->focus != NULL)          
		switch (currentwin->focus->type) {
			case (GUI_BUTTON):
				gui_entry_event ((GUIEntry*)currentwin->focus->item, &event);
				break;
			case (GUI_LIST):
				gui_list_event ((GUIList*)currentwin->focus->item, &event);
				break;
			default:
				break;
		}
	
	event_called = 0;
	return 1;
};

