/* $Id: gui_window.c,v 1.9 2013/06/18 22:24:21 steffen Exp $ */
/***************************************************************************
 *            gui_window.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"

/*
 * reset all window data, do not allocate anything
 */
void gui_window_new (GUIWindow *win, int w, int h, char *title) {
	int i;

	for (i = 0; i < GUI_MAX_ITEM; i++) win->items[i] = NULL;

	if (title == NULL) win->title[0] = 0;
	else strncpy (win->title, title, GUI_TEXTLEN);

	win->screen = NULL;
	win->screen_changed = 1;
	win->h = h;
	win->w = w;
	win->focus = NULL;

	d_printf ("gui_window_new [%s] %p width:%d height:%d", title, win, w, h);
};


/*
 * closes the window, but does not free the memory.
 */
void gui_window_close (GUIWindow *win) {
	d_printf ("gui_window_close [%s] %p", win->title, win);

	/* delete screen so we know it's disabled.. */
	if (win->screen) gfx_img_free (win->screen);
 	win->screen = NULL;
	win->screen_changed = 1;
	if (currentwin == win) currentwin = NULL; 
};


/*
 * add pointer to item to the window data structure..
 * the items will have to be hold inside the application
 */
void gui_window_item_add (GUIWindow *win, GUIItem *item) {
	int i, ifree = -1;
	
	if (item == NULL || win == NULL) return;

	for (i = 0; i < GUI_MAX_ITEM; i++) {
		if (win->items[i] == NULL && ifree == -1) ifree = i;
		if (win->items[i] == item) {
			d_printf ("item already added to item list.");
			ifree = i;
		}
	}

	if (ifree == -1) {
		d_printf ("no free item slot");
		exit (1);
	}

	win->items[ifree] = item;
	win->screen_changed = 1;
	d_printf ("gui_window_item_add win:%p ifree:%d item:%p", win, ifree, item); 
};


/*
 * Handle gui events within the gui.
 */
int gui_window_event (GUIWindow *win, GUIEvent *event) {
	GUIItem *item = win->focus;
	int i;

	if (win->event_called) return -1;
	win->event_called = 1;

	/* check if softkeyb is opened */
	if ((softkeyb && softkeyb->enabled && softkeyb->screen)
		&& (event->event == EGUI_MOUSERELEASED || event->event == EGUI_MOUSEPRESSED)) {
			int x1 = (gfx_screensize.x-softkeyb->screen->width)/2,
					x2 = gfx_screensize.x-(gfx_screensize.x-softkeyb->screen->width)/2,
					y1 = gfx_screensize.y-softkeyb->screen->height,
					y2 = gfx_screensize.y;

			if ( x1 <= event->scr_mpos.x	&& x2 >= event->scr_mpos.x
			&& y1 <= event->scr_mpos.y	&& y2 >= event->scr_mpos.y) {
			gui_softkeyb_event (event);
			win->event_called = 0;
			return 1;
		}
	}

	for (i = 0; i < GUI_MAX_ITEM; i++) if (currentwin->items[i] != NULL)
		if (GUI_ITEM_IS_INSIDE (currentwin->items[i], event->guiwin_mpos))
			item = currentwin->items[i];

	if (item)	switch (item->type) {
		case (GUI_BUTTON):
			gui_button_event (item, event);
			win->event_called = 0;
			return 1;
			break;
		case (GUI_ENTRY):
			gui_entry_event (item, event);
			win->event_called = 0;
			return 1;
			break;
		case (GUI_LIST):
			gui_list_event (item, event);
			win->event_called = 0;
			return 1;
			break;
		case (GUI_CHECKBOX):
			gui_checkbox_event (item, event);
			win->event_called = 0;
			return 1;
			break;
		default:
			break;
	}

	win->event_called = 0;

	if (event->event == EGUI_MOUSERELEASED || event->event == EGUI_MOUSEPRESSED) {
		if (event->guiwin_mpos.x >=0 && event->guiwin_mpos.x < win->w
			&& event->guiwin_mpos.y >=0 && event->guiwin_mpos.y < win->h)
			return 1;
	}

	return 0;
};
