diff --git a/gui/gui.c b/gui/gui.c index 5e68a5e..9a67b1d 100644 --- a/gui/gui.c +++ b/gui/gui.c @@ -1,4 +1,4 @@ -/* $Id: gui.c,v 1.22 2013/03/29 00:14:55 steffen Exp $ */ +/* $Id: gui.c,v 1.23 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui.c * @@ -164,93 +164,35 @@ void gui_draw () { * 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; - - /* check if softkeyb if 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.mousepos.x && x2 >= event.mousepos.x - && y1 <= event.mousepos.y && y2 >= event.mousepos.y) { - gui_softkeyb_event (event); - 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], winpos)) - item = currentwin->items[i]; - - if (item) switch (item->type) { - case (GUI_BUTTON): - event.mousepos = winpos; - gui_button_event (item, &event); - event_called = 0; - return 1; - break; - case (GUI_ENTRY): - event.mousepos = winpos; - gui_entry_event (item, &event); - event_called = 0; - return 1; - break; - case (GUI_LIST): - event.mousepos = winpos; - gui_list_event (item, &event); - event_called = 0; - return 1; - break; - case (GUI_CHECKBOX): - gui_checkbox_event (item, &event); - event_called = 0; - return 1; - break; - default: - break; - } + event.guiwin_mpos.x = event.scr_mpos.x - currentwin->x; + event.guiwin_mpos.y = event.scr_mpos.y - currentwin->y; + if (gui_window_event (currentwin, &event)) return 1; } /* 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); + draw_mousebtnup (event.scr_mpos.x, event.scr_mpos.y, event.mousebtn); break; case (EGUI_MOUSEPRESSED): - draw_mousebtndown (event.mousepos.x, event.mousepos.y, event.mousebtn); + draw_mousebtndown (event.scr_mpos.x, event.scr_mpos.y, event.mousebtn); break; case (EGUI_MOUSEMOVE): - draw_mousemove (event.mousepos.x, event.mousepos.y, 0); + draw_mousemove (event.scr_mpos.x, event.scr_mpos.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; + event->scr_mpos.x += neg ? pos.x : -pos.x; + event->scr_mpos.y += neg ? pos.y : -pos.y; return; }; diff --git a/gui/gui.h b/gui/gui.h index 58895ed..54d9b1f 100644 --- a/gui/gui.h +++ b/gui/gui.h @@ -61,21 +61,22 @@ enum { #define WGUI_S_MODAL 0x0100 -struct _GUIItem_ { - int type; - int x, y, w, h; - char data[]; -} typedef GUIItem; - - struct _GUIEvent_ { int event; uint32_t keyval; // needed for utf8 (max was 32bit?) - iPoint mousepos; + 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; @@ -121,7 +122,7 @@ struct _GUIWindow_ { 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; @@ -173,10 +174,11 @@ 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 int gui_softkeyb_event (GUIEvent *event); extern void gui_softkeyb_draw (); /* button functions */ diff --git a/gui/gui_button.c b/gui/gui_button.c index 5db60e0..91006fa 100644 --- a/gui/gui_button.c +++ b/gui/gui_button.c @@ -1,4 +1,4 @@ -/* $Id: gui_button.c,v 1.10 2013/03/25 21:12:04 steffen Exp $ */ +/* $Id: gui_button.c,v 1.11 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui_button.c * @@ -89,7 +89,7 @@ int gui_button_event (GUIItem *item, GUIEvent *event) { if (button->callback_clicked != NULL && event->event == EGUI_MOUSEPRESSED) { gui_set_focus (NULL); - button->callback_clicked (event->mousepos.x-item->x, event->mousepos.y-item->y); + button->callback_clicked (event->guiwin_mpos.x-item->x, event->guiwin_mpos.y-item->y); } return 1; diff --git a/gui/gui_checkbox.c b/gui/gui_checkbox.c index 82666b1..c89f877 100644 --- a/gui/gui_checkbox.c +++ b/gui/gui_checkbox.c @@ -1,4 +1,4 @@ -/* $Id: gui_checkbox.c,v 1.2 2013/03/29 00:14:55 steffen Exp $ */ +/* $Id: gui_checkbox.c,v 1.3 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui_checkbox.c * @@ -78,7 +78,7 @@ int gui_checkbox_event (GUIItem *item, GUIEvent *event) { errorexit (-1); } - d_printf ("checkbox:%s %d,%d,%d,%d %d,%d", checkbox->text, item->x, item->y, item->w, item->h, event->mousepos.x, event->mousepos.y); + d_printf ("checkbox:%s %d,%d,%d,%d %d,%d", checkbox->text, item->x, item->y, item->w, item->h, event->guiwin_mpos.x, event->guiwin_mpos.y); if (event->event == EGUI_MOUSEPRESSED) { checkbox->checked = !checkbox->checked; if (checkbox->callback_changed != NULL) { diff --git a/gui/gui_list.c b/gui/gui_list.c index a7f3ef3..25560c6 100644 --- a/gui/gui_list.c +++ b/gui/gui_list.c @@ -1,4 +1,4 @@ -/* $Id: gui_list.c,v 1.9 2013/03/24 00:38:50 steffen Exp $ */ +/* $Id: gui_list.c,v 1.10 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui_list.c * @@ -52,7 +52,8 @@ void gui_list_draw (GUIItem *item) { /* find last max */ max = 0; - for (max = 0; list->data[max] != NULL; max++); + if (list->data == NULL) max = 0; + else for (max = 0; list->data[max] != NULL; max++); /* range to display */ i = max - item->h/16; @@ -81,8 +82,8 @@ void gui_list_draw (GUIItem *item) { * event handling */ int gui_list_event (GUIItem *item, GUIEvent *event) { - int x = event->mousepos.x - item->x; - int y = event->mousepos.y - item->y; + int x = event->guiwin_mpos.x - item->x; + int y = event->guiwin_mpos.y - item->y; int i; GUIList *list = NULL; static GUIItem *olditem = NULL; @@ -94,7 +95,9 @@ int gui_list_event (GUIItem *item, GUIEvent *event) { if (event->event == EGUI_MOUSEPRESSED) { olditem = item; } - else if (event->event == EGUI_MOUSERELEASED && olditem == item) { + else if (event->event == EGUI_MOUSERELEASED && olditem == item && + x >= 0 && y >= 0 && x <= item->w && y <= item->h) { + gui_set_focus (item); if (y <= 5 && x >= item->w - 10) list->vs--; diff --git a/gui/gui_softkeyboard.c b/gui/gui_softkeyboard.c index caa2a55..29bea28 100644 --- a/gui/gui_softkeyboard.c +++ b/gui/gui_softkeyboard.c @@ -1,4 +1,4 @@ -/* $Id: gui_softkeyboard.c,v 1.5 2013/03/24 00:38:50 steffen Exp $ */ +/* $Id: gui_softkeyboard.c,v 1.6 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui_softkeyboard.c * @@ -157,25 +157,25 @@ void gui_softkeyb_draw () { }; -int gui_softkeyb_event (GUIEvent event) { +int gui_softkeyb_event (GUIEvent *event) { int i, j, x1, y1, x2, y2; GUIEvent newevent; - iPoint mp = { event.mousepos.x - softkeyb->offset.x, event.mousepos.y - softkeyb->offset.y }; + iPoint mp = { event->scr_mpos.x - softkeyb->offset.x, event->scr_mpos.y - softkeyb->offset.y }; - if (event.event != EGUI_MOUSEPRESSED && event.event != EGUI_MOUSERELEASED) return 0; + if (event->event != EGUI_MOUSEPRESSED && event->event != EGUI_MOUSERELEASED) return 0; /* check if we pressed one of the keys.. */ for (j = 0; j < 3; j++) for (i = 0; i < GUI_SOFTKEYB_X; i++) { gui_softkeyb_getpos (j, i, &x1, &y1, &x2, &y2); if (x1 <= mp.x && mp.x <= x2 && y1 <= mp.y && mp.y <= y2) { - if (event.event == EGUI_MOUSEPRESSED) { + if (event->event == EGUI_MOUSEPRESSED) { softkeyb->last_line = j; softkeyb->last_col = i; newevent.keyval = softkeyb->keys[softkeyb->mode][j][i]; newevent.event = EGUI_KEYCHAR; - newevent.mousepos.x = -1; - newevent.mousepos.y = -1; + newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1; + newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1; gui_entry_event (currentwin->focus, &newevent); } else { @@ -191,7 +191,7 @@ int gui_softkeyb_event (GUIEvent event) { for (i = 0; i < 5; i++) { gui_softkeyb_getpos (3, i, &x1, &y1, &x2, &y2); if (x1 <= mp.x && mp.x <= x2 && y1 <= mp.y && mp.y <= y2) { - if (event.event == EGUI_MOUSEPRESSED) { + if (event->event == EGUI_MOUSEPRESSED) { softkeyb->last_line = 3; softkeyb->last_col = i; switch (i) { @@ -204,16 +204,16 @@ int gui_softkeyb_event (GUIEvent event) { case (2): newevent.keyval = ' '; newevent.event = EGUI_KEYCHAR; - newevent.mousepos.x = -1; - newevent.mousepos.y = -1; + newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1; + newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1; gui_entry_event (currentwin->focus, &newevent); return 1; break; case (3): newevent.keyval = 0x08; newevent.event = EGUI_KEYCHAR; - newevent.mousepos.x = -1; - newevent.mousepos.y = -1; + newevent.guiwin_mpos.x = newevent.scr_mpos.x = -1; + newevent.guiwin_mpos.y = newevent.scr_mpos.y = -1; gui_entry_event (currentwin->focus, &newevent); return 1; break; diff --git a/gui/gui_window.c b/gui/gui_window.c index e3356b4..240aa54 100644 --- a/gui/gui_window.c +++ b/gui/gui_window.c @@ -1,4 +1,4 @@ -/* $Id: gui_window.c,v 1.7 2013/02/28 23:19:59 steffen Exp $ */ +/* $Id: gui_window.c,v 1.8 2013/04/03 21:52:27 steffen Exp $ */ /*************************************************************************** * gui_window.c * @@ -91,3 +91,62 @@ void gui_window_item_add (GUIWindow *win, GUIItem *item) { 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; + return 0; +};