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

201 lines
7.2 KiB

/* $Id: gui.c,v 1.4 2013/02/17 00:52:20 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);
}
// 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;
};