/***************************************************************************
 *            gui.h
 *
 *  2011-03-10
 *  Copyright  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef _GUI_H_
#define _GUI_H_

#include "osmroute.h"

#define GUI_TEXTLEN 256
#define GUI_MAX_ITEM 256

enum {
	EGUI_NONE,
	EGUI_KEYCHAR,
	EGUI_MOUSEMOVE,
	EGUI_MOUSEPRESSED,
	EGUI_MOUSERELEASED
};

enum {
	GUI_NONE,
	GUI_BUTTON,
	GUI_LIST,
	GUI_ENTRY,
	GUI_CHECKBOX,
	GUI_LABEL
};

/*
 * styles for the windows.. 
 */
#define WGUI_S_VCENTER 0x0003
#define WGUI_S_VLEFT   0x0002
#define WGUI_S_VRIGHT  0x0001
#define WGUI_S_HCENTER 0x000C
#define WGUI_S_HTOP    0x0004
#define WGUI_S_HBOTTOM 0x0008
#define WGUI_S_NOTITLE 0x0010
// #define WGUI_S_**** 0x0080
#define WGUI_S_MODAL   0x0100


#define GUIKEY_UP		0x0111
#define GUIKEY_DOWN		0x0112
#define GUIKEY_RIGHT	0x0113
#define GUIKEY_LEFT		0x0114


struct _GUIEvent_ {
	int event;
	uint16_t key;				// the key which was pressed
	uint32_t keychar;			// the utf char for this key
	iPoint scr_mpos;			// mousepos on screen
	iPoint guiwin_mpos;			// mousepos on gui (setup from gui_event)
	int mousebtn;
} typedef GUIEvent;


struct _GUIItem_ {
	int type;
	int x, y, w, h;
	char data[];
} typedef GUIItem;


struct _GUIButton_ {
	char caption[GUI_TEXTLEN];
	struct color *col;
	struct color *textcol;
	void (*callback_clicked) (int x, int y);
	void (*func_draw) (GUIItem *item);
} typedef GUIButton;


struct _GUIList_ {
	char **data;
	int vs;					// first element to draw
	int selected;
	void (*callback_selectitem) (int nr);
} typedef GUIList;


struct {
	char text[GUI_TEXTLEN];
	struct color *textcol;
} typedef GUILabel;


struct {
	char text[GUI_TEXTLEN];
	int curpos;
	int overwrite;
	void (*callback_enter) ();
	void (*callback_esc) ();
	void (*callback_changed) ();
} typedef GUIEntry;


struct {
	char text[GUI_TEXTLEN];
	int checked;
	void (*callback_changed) ();
} typedef GUICheckbox;


struct _GUIWindow_ {
	void (*callback_close) ();

	struct _GUIWindow_ *parent;
	struct image *screen;
	int screen_changed;
	int event_called;				// mark if we proceed any event call already.
	char title[GUI_TEXTLEN];
	int x;
	int y;
	int w;
	int h;
	int style;

	void (*callback_clicked) (int x, int y);
	
	GUIItem *items[GUI_MAX_ITEM];
	GUIItem *focus;
} typedef GUIWindow;

#define GUI_SOFTKEYB_X 11
#define GUI_SOFTKEYB_Y 3
enum {
	GUI_SOFTKEYB_MODE_NORM = 0,
	GUI_SOFTKEYB_MODE_SHIFT,
	GUI_SOFTKEYB_MODE_SYMBOL,

	GUI_SOFTKEYB_MODE_MAX
};

struct _GUISoftkeyboard_ {
	int mode;
	int enabled;
	int linepos[2][4];	/* y positions of lines */
	int btnpos[2][8];	/* holds position keys: Left, Right, Shift, Symbols, Space, Del, Close */
	iPoint offset;		/* screenoffset .. will be set during last draw */
	struct image *screen;
	int last_col;
	int last_line;
	uint32_t keys[GUI_SOFTKEYB_MODE_MAX][GUI_SOFTKEYB_Y][GUI_SOFTKEYB_X];
} typedef GUISoftkeyboard;

extern GUIWindow *currentwin;
extern GUISoftkeyboard *softkeyb;

extern void gui_show (GUIWindow *win);
extern void gui_draw ();
extern void gui_close ();
extern int gui_event (GUIEvent event);
extern void gui_event_addmousepos (GUIEvent *event, iPoint pos, int neg);
extern void gui_set_focus (GUIItem *item);

#define GUI_ITEM_IS_INSIDE(__item__,__pos__) gui_is_inside(__item__->x, __item__->y, __item__->w, __item__->h, __pos__)
extern int gui_is_inside (int x, int y, int w, int h, iPoint pos);

extern void gui_window_new (GUIWindow *win, int w, int h, char *title);
extern void gui_window_close (GUIWindow *win);
extern void gui_window_item_add (GUIWindow *win, GUIItem *item);
extern int gui_window_event (GUIWindow *win, GUIEvent *event);

/* softkeyboard functions. */
extern void gui_softkeyb_show (int enable);
extern int gui_softkeyb_event (GUIEvent *event);
extern void gui_softkeyb_draw ();

/* button functions */
#define GUI_BUTTON_T(_item_) ((GUIButton*)(_item_)->data)
extern void gui_button_draw (GUIItem *item); 
extern int gui_button_event (GUIItem *item, GUIEvent *event);
extern GUIItem *gui_button_new (char *name, int x, int y, int w, int h);

/* checkbox functions */
#define GUI_CHECKBOX_T(_item_) ((GUICheckbox*)(_item_)->data)
extern void gui_checkbox_draw (GUIItem *item);
extern int gui_checkbox_event (GUIItem *item, GUIEvent *event);
extern GUIItem *gui_checkbox_new (char *text, int initialvalue, int x, int y);

/* label functions */
#define GUI_LABEL_T(_item_) ((GUILabel*)(_item_)->data)
extern void gui_label_draw (GUIItem *item);
extern GUIItem *gui_label_new (char *text, int x, int y);

/* entry functions */
#define GUI_ENTRY_T(_item_) ((GUIEntry*)(_item_)->data)
extern void gui_entry_draw (GUIItem *item);
extern int gui_entry_event (GUIItem *item, GUIEvent *event);
extern GUIItem *gui_entry_new (char *text, int x, int y, int w, int h);
extern void gui_entry_settext (GUIItem *item, char *text);

/* list functions */
#define GUI_LIST_T(_item_) ((GUIList*)(_item_)->data)
extern void gui_list_draw (GUIItem *item);
extern int gui_list_event (GUIItem *item, GUIEvent *event);
extern void gui_list_setselect (GUIItem *item);
extern GUIItem *gui_list_new (int x, int y, int w, int h);

/**************************************************************************
 * gui windows
 */
extern void gui_search_show ();
extern void gui_fav_show ();
extern void gui_mainmenu_show ();
extern void gui_buttons_show ();
extern char *gui_sfile_show (char *startpath);
extern void gui_config_show ();
extern void gpswin_show ();
extern void wnd_routing_show ();
extern void wnd_mapcontext_show ();

#endif
