You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spOSMroute/gui/gui.c

458 lines
14 KiB

/* $id: 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);
}
// 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]);
}
for (i = 0; i < GUI_WIN_BUTTONS; i++) if (currentwin->buttons[i].id != 0) {
if (currentwin->buttons[i].callback_draw != NULL)
currentwin->buttons[i].callback_draw (&currentwin->buttons[i]);
else gui_button_draw (&currentwin->buttons[i]);
}
for (i = 0; i < GUI_WIN_LABELS; i++) if (currentwin->labels[i].id != 0) {
gui_label_draw (&currentwin->labels[i]);
}
for (i = 0; i < GUI_WIN_ENTRYS; i++) if (currentwin->entrys[i].id != 0) {
gui_entry_draw (&currentwin->entrys[i]);
}
for (i = 0; i < GUI_WIN_LISTS; i++) if (currentwin->lists[i].id != 0) {
gui_list_draw (&currentwin->lists[i]);
}
for (i = 0; i < GUI_WIN_CHECKBOXES; i++) if (currentwin->checkboxes[i].id != 0) {
// gui_draw_label (&currentwin->labels[i]);
}
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_WIN_BUTTONS; i++) {
// if (currentwin->buttons[i].id != 0)
// d_printf ("%d %d,%d %d,%d - %d,%d", currentwin->buttons[i].id, currentwin->buttons[i].x, currentwin->buttons[i].y, currentwin->buttons[i].w, currentwin->buttons[i].h, event.mousepos.x, event.mousepos.y);
if (currentwin->buttons[i].id != 0
&& currentwin->buttons[i].x <= event.mousepos.x && currentwin->buttons[i].x+currentwin->buttons[i].w >= event.mousepos.x
&& currentwin->buttons[i].y <= event.mousepos.y && currentwin->buttons[i].y+currentwin->buttons[i].h >= event.mousepos.y) {
if (currentwin->buttons[i].callback_clicked != NULL && event.event == EGUI_MOUSEPRESSED) {
d_printf ("GUI BUTTON PRESSED: %d:%s", currentwin->buttons[i].id, currentwin->buttons[i].caption);
currentwin->buttons[i].callback_clicked (event.mousepos.x-currentwin->buttons[i].x, event.mousepos.y-currentwin->buttons[i].y);
event_called = 0;
currentwin->screen_changed = 1;
return 1;
}
}
}
for (i = 0; i < GUI_WIN_LABELS; i++) if (currentwin->labels[i].id != 0) {
// gui_event_label (&currentwin->labels[i]);
}
for (i = 0; i < GUI_WIN_ENTRYS; i++) if (currentwin->entrys[i].id != 0) {
if (currentwin->entrys[i].id != 0
&& currentwin->entrys[i].x <= event.mousepos.x && currentwin->entrys[i].x+currentwin->entrys[i].w >= event.mousepos.x
&& currentwin->entrys[i].y <= event.mousepos.y && currentwin->entrys[i].y+currentwin->entrys[i].h >= event.mousepos.y) {
gui_entry_event (&currentwin->entrys[i], &event);
event_called = 0;
return 1;
}
}
for (i = 0; i < GUI_WIN_LISTS; i++) if (currentwin->lists[i].id != 0) {
if (currentwin->lists[i].x <= event.mousepos.x && currentwin->lists[i].x+currentwin->lists[i].w >= event.mousepos.x
&& currentwin->lists[i].y <= event.mousepos.y && currentwin->lists[i].y+currentwin->lists[i].h >= event.mousepos.y) {
gui_list_event (&currentwin->lists[i], &event);
event_called = 0;
return 1;
}
}
for (i = 0; i < GUI_WIN_CHECKBOXES; i++) if (currentwin->checkboxes[i].id != 0) {
// gui_event_label (&currentwin->labels[i]);
}
if (currentwin->focus != NULL) {
for (i = 0; i < GUI_WIN_ENTRYS; i++) if (currentwin->focus == &currentwin->entrys[i]) {
gui_entry_event (&currentwin->entrys[i], &event);
event_called = 0;
return 1;
}
for (i = 0; i < GUI_WIN_LISTS; i++) if (currentwin->focus == &currentwin->lists[i]) {
gui_list_event (&currentwin->lists[i], &event);
event_called = 0;
return 1;
}
}
event_called = 0;
return 1;
};
/***************************************************************************
* basic windows functions
***************************************************************************/
void guiwindow_new (GUIWindow *win, int w, int h, char *title) {
int i;
for (i = 0; i < GUI_MAX_ITEM; i++) {
win->items[i]->item = NULL;
win->items[i]->type = GUI_NONE;
}
strncpy (win->title, title, GUI_TEXTLEN);
win->screen = NULL;
win->screen_changed = 1;
win->h = h;
win->w = w;
win->focus = NULL;
d_printf ("guiwindow_new %p width:%d height:%d", win, w, h);
};
/*
* set focus
*/
/*
* closes the window, but does not free the memory.
*/
void guiwindow_close (GUIWindow *win) {
int i;
for (i = 0; i < GUI_WIN_BUTTONS; i++) {
win->buttons[i].caption[0] = '\0';
win->buttons[i].id = 0;
win->buttons[i].callback_clicked = NULL;
win->buttons[i].callback_draw = NULL;
}
for (i = 0; i < GUI_WIN_LABELS; i++) {
win->labels[i].text[0] = '\0';
win->labels[i].textcol = &color[COLOR_white][2];
win->labels[i].id = 0;
}
for (i = 0; i < GUI_WIN_ENTRYS; i++) {
win->entrys[i].text[0] = '\0';
win->entrys[i].callback_enter = NULL;
win->entrys[i].callback_changed = NULL;
win->entrys[i].id = 0;
}
for (i = 0; i < GUI_WIN_CHECKBOXES; i++) {
win->checkboxes[i].text[0] = '\0';
win->checkboxes[i].callback_changed = NULL;
win->checkboxes[i].id = 0;
}
win->title[0] = 0;
gfx_img_free (win->screen);
win->screen = NULL;
win->screen_changed = 1;
win->h = 0;
win->w = 0;
};
/****************************************************************************
****************************************************************************
** gui elements
**
**/
/****************************************************************************
* button
*/
void gui_button_draw (GUIButton *button) {
struct line_style ls;
ls.width = 1.0;
ls.c = ls.borderc = color[COLOR_white][3];
draw_polygonstart ();
draw_polygonadd (button->x, button->y);
draw_polygonadd (button->x, button->y + button->h);
draw_polygonadd (button->x + button->w, button->y + button->h);
draw_polygonadd (button->x + button->w, button->y);
draw_polygonfinish (currentwin->screen, ls, *button->col, 1);
gfx_draw_text (currentwin->screen, button->x + 2, button->y + 2, button->caption, button->textcol);
};
/****************************************************************************
* label
*/
void gui_label_draw (GUILabel *label) {
struct line_style ls;
ls.width = 1.0;
ls.c = ls.borderc = color[COLOR_white][3];
gfx_draw_text (currentwin->screen, label->x, label->y, label->text, label->textcol);
};
/****************************************************************************
* entry
*/
void gui_entry_draw (GUIEntry *entry) {
char text1[GUI_TEXTLEN];
char text2[GUI_TEXTLEN];
struct line_style ls;
ls.width = 1.0;
if (currentwin->focus == entry)
ls.c = ls.borderc = color[COLOR_white][3];
else
ls.c = ls.borderc = color[COLOR_white][2];
gfx_draw_line (currentwin->screen, entry->x, entry->y+entry->h, entry->x+entry->w, entry->y+entry->h, ls);
gfx_draw_line (currentwin->screen, entry->x, entry->y+entry->h, entry->x, entry->y+entry->h-2, ls);
gfx_draw_line (currentwin->screen, entry->x+entry->w, entry->y+entry->h, entry->x+entry->w, entry->y+entry->h-2, ls);
if (entry->curpos >= strlen (entry->text)) {
entry->curpos = strlen (entry->text);
snprintf (text1, GUI_TEXTLEN, "%s|", entry->text);
}
else if (entry->curpos <= 0) {
entry->curpos = 0;
snprintf (text1, GUI_TEXTLEN, "|%s", entry->text);
}
else {
strncpy (text2, entry->text, entry->curpos);
snprintf (text1, GUI_TEXTLEN, "%s|%s", text2, entry->text+entry->curpos);
}
gfx_draw_text (currentwin->screen, entry->x+2, entry->y, text1, &color[COLOR_white][3]);
};
/*
* event handling
*/
void gui_entry_event (GUIEntry *entry, GUIEvent *event) {
char text1[GUI_TEXTLEN];
if (event->event == EGUI_MOUSERELEASED
&& entry->x <= event->mousepos.x && entry->x+entry->w >= event->mousepos.x
&& entry->y <= event->mousepos.y && entry->y+entry->h >= event->mousepos.y) {
d_printf ("gui_entry_event set focus");
currentwin->focus = entry;
}
else if (event->event == EGUI_KEYRELEASED) {
d_printf ("gui_entry_event: curpos:%d len:%d text:%s char:%x", entry->curpos, strlen (entry->text), entry->text, event->keyval);
if (event->keyval == 0xff08) { // backspace
// strncpy (text1, entry->text, entry->curpos-1);
// snprintf (text2, GUI_TEXTLEN, "%s%s", text1, entry->text+entry->curpos);
// strncpy (entry->text, text2, GUI_TEXTLEN);
// entry->curpos--;
entry->text[0] = 0;
entry->curpos = 0;
}
else if (event->keyval == 0xff1b) { // esc
gui_close ();
}
else if (event->keyval == 0xff0d) { // enter
if (entry->callback_enter) entry->callback_enter ();
}
else if (event->keyval == 0xff51) { // left
// entry->curpos--;
}
else if (event->keyval == 0xff53) { // right
// entry->curpos++;
}
else if ((event->keyval & 0xff00) == 0xff00) {
// ignore all the rest
}
else {
strncpy (text1, entry->text, GUI_TEXTLEN );
snprintf (entry->text, GUI_TEXTLEN, "%s%c", text1, event->keyval);
entry->curpos++;
}
}
currentwin->screen_changed = 1;
return;
};
/****************************************************************************
* list
*/
void gui_list_draw (GUIList *list) {
struct line_style ls;
int max, i, x, y, j;
ls.width = 1.0;
if (currentwin->focus == list)
ls.c = ls.borderc = color[COLOR_white][3];
else
ls.c = ls.borderc = color[COLOR_white][2];
gfx_draw_line (currentwin->screen, list->x, list->y, list->x+list->w, list->y, ls);
gfx_draw_line (currentwin->screen, list->x+list->w, list->y, list->x+list->w, list->y+list->h, ls);
gfx_draw_line (currentwin->screen, list->x+list->w, list->y+list->h, list->x, list->y+list->h, ls);
gfx_draw_line (currentwin->screen, list->x, list->y+list->h, list->x, list->y, ls);
if (list->data == NULL) return;
for (max = 0; list->data[max] != NULL; max++);
/* range to display */
i = max - list->h/16;
if (i < list->vs) list->vs = i;
if (list->vs < 0) list->vs = 0;
for (i = list->vs; i < (list->vs + list->h/16) && i < max; i++) {
if (i == list->selected) gfx_draw_text (currentwin->screen, list->x + 5, list->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_red][1]);
else gfx_draw_text (currentwin->screen, list->x + 5, list->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_white][3]);
}
ls.c = ls.borderc = color[COLOR_white][3];
for (i = -1; i < 2; i += 2) {
if (i < 0) y = list->y + list->h;
else y = list->y;
x = list->x + list->w;
for (j = 0; j <= 5; j++) {
gfx_draw_line (currentwin->screen, x - 5, y, x-j, y + i*5, ls);
gfx_draw_line (currentwin->screen, x - 5 -j, y+ i*5, x-5, y, ls);
}
}
};
/*
* event handling
*/
void gui_list_event (GUIList *list, GUIEvent *event) {
int x = event->mousepos.x - list->x;
int y = event->mousepos.y - list->y;
int i;
/* set focus */
if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= list->w && y >= 0 && y <= list->h) {
currentwin->focus = list;
if (y <= 5 && x >= list->w - 10) list->vs--;
else if (y >= list->h - 5 && x >= list->w - 10) list->vs++;
else { /* select entry */
i = (y-5)/16 + list->vs;
list->selected = i;
if (list->callback_selectitem) list->callback_selectitem (i);
d_printf ("selected nr: %d", i);
}
}
currentwin->screen_changed = 1;
draw ();
};