/* $Id: gui.c,v 1.12 2013/02/27 22:21:35 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 . */ #include "osmroute.h" #include "draw.h" #include "gui.h" #include "system.h" GUIWindow *currentwin = NULL; #define GUI_ITEM_IS_INSIDE(__item__,__pos__) gui_is_inside(__item__->x, __item__->y, __item__->w, __item__->h, __pos__) int gui_is_inside (int x, int y, int w, int h, iPoint pos) { if (x <= pos.x && x+w >= pos.x && y <= pos.y && y+h >= pos.y) return 1; else return 0; }; /* * 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_red][3]; draw_polygonstart (); draw_polygonadd (0, 0); draw_polygonadd (0, currentwin->h); draw_polygonadd (currentwin->w, currentwin->h); draw_polygonadd (currentwin->w, 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++) if (currentwin->items[i]) switch (currentwin->items[i]->type) { case (GUI_BUTTON): { GUIButton *button = (GUIButton *) currentwin->items[i]->data; if (button->func_draw != NULL) button->func_draw (currentwin->items[i]); else gui_button_draw (currentwin->items[i]); } break; case (GUI_LABEL): gui_label_draw (currentwin->items[i]); break; case (GUI_ENTRY): gui_entry_draw (currentwin->items[i]); break; case (GUI_LIST): gui_list_draw (currentwin->items[i]); 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; iPoint winpos = { 0 }; static int event_called = 0; GUIItem *item = NULL; if (event_called) return 1; event_called = 1; if (currentwin) { winpos.x = event.mousepos.x - currentwin->x; winpos.y = event.mousepos.y - currentwin->y; item = currentwin->focus; for (i = 0; i < GUI_MAX_ITEM; i++) if (currentwin->items[i] != NULL) if (GUI_ITEM_IS_INSIDE (currentwin->items[i], winpos)) item = currentwin->items[i]; d_printf ("item:%p , type:%d , focus:%p", item, item ? item->type : -1, currentwin->focus); if (item) switch (item->type) { case (GUI_BUTTON): gui_event_addmousepos (&event, winpos, -1); gui_button_event (item, &event); event_called = 0; return 1; break; case (GUI_ENTRY): gui_event_addmousepos (&event, winpos, -1); gui_entry_event (item, &event); event_called = 0; return 1; break; case (GUI_LIST): gui_list_event (item, &event); event_called = 0; return 1; break; default: break; } } /* no gui active nor any window is responsible.. */ d_printf ("no event.."); switch (event.event) { case (EGUI_MOUSERELEASED): draw_mousebtnup (event.mousepos.x, event.mousepos.y, event.mousebtn); break; case (EGUI_MOUSEPRESSED): draw_mousebtndown (event.mousepos.x, event.mousepos.y, event.mousebtn); break; case (EGUI_MOUSEMOVE): draw_mousemove (event.mousepos.x, event.mousepos.y, 0); break; default: break; } event_called = 0; return 0; }; void gui_event_addmousepos (GUIEvent *event, iPoint pos, int neg) { event->mousepos.x += neg ? pos.x : -pos.x; event->mousepos.y += neg ? pos.y : -pos.y; return; };