diff --git a/gui/gui.c b/gui/gui.c index d053d44..f195e4e 100644 --- a/gui/gui.c +++ b/gui/gui.c @@ -1,4 +1,4 @@ -/* $Id: gui.c,v 1.10 2013/02/24 20:33:42 steffen Exp $ */ +/* $Id: gui.c,v 1.11 2013/02/26 22:13:04 steffen Exp $ */ /*************************************************************************** * gui.c * @@ -30,7 +30,6 @@ 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; @@ -95,22 +94,22 @@ void gui_draw () { } /* draw items.. */ - for (i = 0; i < GUI_MAX_ITEM; i++) - switch (currentwin->items[i].type) { + 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].item; - if (button->callback_draw != NULL) button->callback_draw (button); - else gui_button_draw (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 ((GUILabel*)currentwin->items[i].item); + gui_label_draw (currentwin->items[i]); break; case (GUI_ENTRY): - gui_entry_draw ((GUIEntry*)currentwin->items[i].item); + gui_entry_draw (currentwin->items[i]); break; case (GUI_LIST): - gui_list_draw ((GUIList*)currentwin->items[i].item); + gui_list_draw (currentwin->items[i]); break; // case (GUI_ENTRY): // gui_entry_draw ((GUIEntry*)currentwin->items[i].item); @@ -143,6 +142,7 @@ 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; @@ -151,62 +151,34 @@ int gui_event (GUIEvent event) { winpos.x = event.mousepos.x - currentwin->x; winpos.y = event.mousepos.y - currentwin->y; - for (i = 0; i < GUI_MAX_ITEM; i++) - switch (currentwin->items[i].type) { - case (GUI_BUTTON): { - GUIButton *button = (GUIButton *) currentwin->items[i].item; - if (GUI_ITEM_IS_INSIDE (button, winpos)) { - d_printf ("button: %d,%d,%d,%d pos:%d,%d event:%d,%d ", - button->x, button->y, button->w, button->h, winpos.x, winpos.y, event.mousepos.x, event.mousepos.y); - gui_button_event (button, &event); - event_called = 0; - return 1; - } - } - break; - - case (GUI_LABEL): - break; - - case (GUI_ENTRY): { - GUIEntry *entry = (GUIEntry*) currentwin->items[i].item; - if (GUI_ITEM_IS_INSIDE (entry, winpos)) { - d_printf ("entry event type: %d", event.event); - gui_entry_event (entry, &event); - event_called = 0; - return 1; - } - } - break; - - case (GUI_LIST): { - GUIList *list = (GUIList *) currentwin->items[i].item; - if (GUI_ITEM_IS_INSIDE (list, winpos)) { - d_printf ("list.."); - gui_list_event (list, &event); - event_called = 0; - return 1; - } - } - break; - } - - if (currentwin->focus) { - d_printf ("focus.."); - switch (currentwin->focus->type) { - case (GUI_ENTRY): - gui_entry_event ((GUIEntry*)currentwin->focus->item, &event); - event_called = 1; - return 1; - break; - case (GUI_LIST): - gui_list_event ((GUIList*)currentwin->focus->item, &event); - event_called = 1; - return 1; - break; - default: - break; - } + 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 ("%p", item); + + if (item) switch (item->type) { + case (GUI_ENTRY): + gui_event_addmousepos (&event, winpos, -1); + gui_button_event (item, &event); + event_called = 1; + return 1; + break; + case (GUI_BUTTON): + gui_event_addmousepos (&event, winpos, -1); + gui_entry_event (item, &event); + event_called = 1; + return 1; + break; + case (GUI_LIST): + gui_list_event (item, &event); + event_called = 1; + return 1; + break; + default: + break; } } @@ -230,3 +202,9 @@ int gui_event (GUIEvent event) { 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; +}; diff --git a/gui/gui.h b/gui/gui.h index 4c7d653..28f4264 100644 --- a/gui/gui.h +++ b/gui/gui.h @@ -61,6 +61,14 @@ enum { // #define WGUI_S_**** 0x0080 #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?) @@ -70,17 +78,15 @@ struct _GUIEvent_ { struct _GUIButton_ { - int x, y, w, h; char caption[GUI_TEXTLEN]; struct color *col; struct color *textcol; void (*callback_clicked) (int x, int y); - void (*callback_draw) (struct _GUIButton_ *btn); + void (*func_draw) (GUIItem *item); } typedef GUIButton; struct _GUIList_ { - int x, y, w, h; char **data; int vs; // first element to draw int selected; @@ -89,14 +95,12 @@ struct _GUIList_ { struct { - int x, y; char text[GUI_TEXTLEN]; struct color *textcol; } typedef GUILabel; struct { - int x, y, w, h; char text[GUI_TEXTLEN]; int curpos; int overwrite; @@ -106,18 +110,12 @@ struct { struct { - int x, y, w, h; char text[GUI_TEXTLEN]; int checked; void (*callback_changed) (); } typedef GUICheckbox; -struct _GUIItem_ { - int type; - void *item; -} typedef GUIItem; - struct _GUIWindow_ { void (*callback_close) (); @@ -132,7 +130,7 @@ struct _GUIWindow_ { int h; int style; - GUIItem items[GUI_MAX_ITEM]; + GUIItem *items[GUI_MAX_ITEM]; GUIItem *focus; } typedef GUIWindow; @@ -142,30 +140,31 @@ 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_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, int type, void *item); +extern void gui_window_item_add (GUIWindow *win, GUIItem *item); /* button functions */ -extern void gui_button_draw (GUIButton *button); -extern void gui_button_event (GUIButton *entry, GUIEvent *event); -extern GUIButton *gui_button_new (char *name, int x, int y, int w, int h); +extern void gui_button_draw (GUIItem *item); +extern void gui_button_event (GUIItem *item, GUIEvent *event); +extern GUIItem *gui_button_new (char *name, int x, int y, int w, int h); /* label functions */ -extern void gui_label_draw (GUILabel *label); -extern GUILabel *gui_label_new (char *text, int x, int y); +extern void gui_label_draw (GUIItem *item); +extern GUIItem *gui_label_new (char *text, int x, int y); /* entry functions */ -extern void gui_entry_draw (GUIEntry *entry); -extern void gui_entry_event (GUIEntry *entry, GUIEvent *event); -extern GUIEntry *gui_entry_new (char *text, int x, int y, int w, int h); +extern void gui_entry_draw (GUIItem *item); +extern void gui_entry_event (GUIItem *item, GUIEvent *event); +extern GUIItem *gui_entry_new (char *text, int x, int y, int w, int h); /* list functions */ -extern void gui_list_draw (GUIList *list); -extern void gui_list_event (GUIList *list, GUIEvent *event); -extern void gui_list_setselect (GUIList *list); -extern GUIList *gui_list_new (int x, int y, int w, int h); +extern void gui_list_draw (GUIItem *item); +extern void 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 diff --git a/gui/gui_button.c b/gui/gui_button.c index 601285f..7d109d1 100644 --- a/gui/gui_button.c +++ b/gui/gui_button.c @@ -1,4 +1,4 @@ -/* $Id: gui_button.c,v 1.4 2013/02/24 20:33:42 steffen Exp $ */ +/* $Id: gui_button.c,v 1.5 2013/02/26 22:13:04 steffen Exp $ */ /*************************************************************************** * gui_button.c * @@ -32,43 +32,61 @@ /**************************************************************************** * button */ -void gui_button_draw (GUIButton *button) { +void gui_button_draw (GUIItem *item) { struct line_style ls; + GUIButton *button = NULL; + if (item == NULL || item->type != GUI_BUTTON) { + d_printf ("GUIButton %p not type GUIButton", item); + errorexit (-1); + } + button = (GUIButton *) item->data; + 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_polygonadd (item->x, item->y); + draw_polygonadd (item->x, item->y + item->h); + draw_polygonadd (item->x + item->w, item->y + item->h); + draw_polygonadd (item->x + item->w, item->y); draw_polygonfinish (currentwin->screen, ls, *button->col, 1); - gfx_draw_text (currentwin->screen, button->x + 2, button->y + 2, button->caption, button->textcol); + gfx_draw_text (currentwin->screen, item->x + 2, item->y + 2, button->caption, button->textcol); }; -GUIButton *gui_button_new (char *caption, int x, int y, int w, int h) { - GUIButton *item = (GUIButton*) ml_malloc (sizeof (GUIButton)); +GUIItem *gui_button_new (char *caption, int x, int y, int w, int h) { + GUIItem *item = (GUIItem*) ml_malloc (sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT); + GUIButton *button = (GUIButton *) item->data; - if (caption == NULL) item->caption[0] = 0; - else strncpy (item->caption, caption, GUI_TEXTLEN); + memset (item, 0x0, sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT); + + if (caption == NULL) button->caption[0] = 0; + else strncpy (button->caption, caption, GUI_TEXTLEN); item->x = x; item->y = y; item->w = w; item->h = h; - item->col = &color[COLOR_white][1]; - item->textcol = &color[COLOR_white][3]; - item->callback_clicked = NULL; - item->callback_draw = NULL; + button->col = &color[COLOR_white][1]; + button->textcol = &color[COLOR_white][3]; + button->callback_clicked = NULL; + button->func_draw = NULL; return item; }; -void gui_button_event (GUIButton *button, GUIEvent *event) { +void gui_button_event (GUIItem *item, GUIEvent *event) { + GUIButton *button = NULL; + + if (item == NULL || item->type != GUI_BUTTON) { + d_printf ("GUIButton %p not type GUIButton", item); + errorexit (-1); + } + button = (GUIButton *) item->data; + if (button->callback_clicked != NULL && event->event == EGUI_MOUSEPRESSED) { - button->callback_clicked (event->mousepos.x-button->x, event->mousepos.y-button->y); + button->callback_clicked (event->mousepos.x-item->x, event->mousepos.y-item->y); } }; diff --git a/gui/gui_entry.c b/gui/gui_entry.c index 7c50516..6ecab94 100644 --- a/gui/gui_entry.c +++ b/gui/gui_entry.c @@ -1,4 +1,4 @@ -/* $Id: gui_entry.c,v 1.3 2013/02/24 20:33:42 steffen Exp $ */ +/* $Id: gui_entry.c,v 1.4 2013/02/26 22:13:04 steffen Exp $ */ /*************************************************************************** * gui_entry.c * @@ -67,9 +67,11 @@ void gui_entry_draw (GUIEntry *entry) { void gui_entry_event (GUIEntry *entry, GUIEvent *event) { char text1[GUI_TEXTLEN]; + d_printf ("event: %d pos: %d,%d", event->event, event->mousepos.x, event->mousepos.y); + 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) { + && event->mousepos.x >= 0 && entry->x+entry->w >= event->mousepos.x + && event->mousepos.y >= 0 && entry->y+entry->h >= event->mousepos.y) { d_printf ("gui_entry_event set focus"); currentwin->focus = entry; } diff --git a/gui/gui_list.c b/gui/gui_list.c index 606a647..3837b01 100644 --- a/gui/gui_list.c +++ b/gui/gui_list.c @@ -1,4 +1,4 @@ -/* $Id: gui_list.c,v 1.3 2013/02/21 23:07:19 steffen Exp $ */ +/* $Id: gui_list.c,v 1.4 2013/02/26 22:13:04 steffen Exp $ */ /*************************************************************************** * gui_list.c * @@ -32,38 +32,43 @@ /**************************************************************************** * list */ -void gui_list_draw (GUIList *list) { +void gui_list_draw (GUIItem *item) { struct line_style ls; int max, i, x, y, j; + GUIList *list = NULL; + + if (item == NULL) return; + else list = (GUIList *) item->data; ls.width = 1.0; - if (currentwin->focus != NULL && currentwin->focus->item == (void *) list) + if (currentwin->focus == item) 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); + gfx_draw_line (currentwin->screen, item->x, item->y, item->x+item->w, item->y, ls); + gfx_draw_line (currentwin->screen, item->x+item->w, item->y, item->x+item->w, item->y+item->h, ls); + gfx_draw_line (currentwin->screen, item->x+item->w, item->y+item->h, item->x, item->y+item->h, ls); + gfx_draw_line (currentwin->screen, item->x, item->y+item->h, item->x, item->y, ls); - if (list->data == NULL) return; - for (max = 0; list->data[max] != NULL; max++); + /* find last max */ + max = 0; + // for (max = 0; list->data[max] != NULL; max++); /* range to display */ - i = max - list->h/16; + i = max - item->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]); + for (i = list->vs; i < (list->vs + item->h/16) && i < max; i++) { + if (i == list->selected) gfx_draw_text (currentwin->screen, item->x + 5, item->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_red][1]); + else gfx_draw_text (currentwin->screen, item->x + 5, item->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; + if (i < 0) y = item->y + item->h; + else y = item->y; + x = item->x + item->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); @@ -75,17 +80,21 @@ void gui_list_draw (GUIList *list) { /* * event handling */ -void gui_list_event (GUIList *list, GUIEvent *event) { - int x = event->mousepos.x - list->x; - int y = event->mousepos.y - list->y; +void gui_list_event (GUIItem *item, GUIEvent *event) { + int x = event->mousepos.x - item->x; + int y = event->mousepos.y - item->y; int i; + GUIList *list = NULL; + if (item == NULL) return; + else list = (GUIList *) item->data; + /* set focus */ - if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= list->w && y >= 0 && y <= list->h) { - currentwin->focus = (void *) list; + if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= item->w && y >= 0 && y <= item->h) { + currentwin->focus = (void *) item; - if (y <= 5 && x >= list->w - 10) list->vs--; - else if (y >= list->h - 5 && x >= list->w - 10) list->vs++; + if (y <= 5 && x >= item->w - 10) list->vs--; + else if (y >= item->h - 5 && x >= item->w - 10) list->vs++; else { /* select entry */ i = (y-5)/16 + list->vs; list->selected = i; @@ -98,16 +107,16 @@ void gui_list_event (GUIList *list, GUIEvent *event) { }; -GUIList *gui_list_new (int x, int y, int w, int h) { - GUIList *item = (GUIList*) ml_malloc (sizeof (GUIList)); +GUIItem *gui_list_new (int x, int y, int w, int h) { + GUIItem *item = (GUIItem *) ml_malloc (sizeof (GUIList) + sizeof (GUIItem) + POINTERALIGNMENT); + GUIList *list = (GUIList *) item->data; item->x = x; item->y = y; item->w = w; item->h = h; - item->data = NULL; - item->selected = -1; - item->vs = 0; - item->callback_selectitem = NULL; + list->selected = -1; + list->vs = 0; + list->callback_selectitem = NULL; return item; -}; \ No newline at end of file +}; diff --git a/gui/gui_window.c b/gui/gui_window.c index 7d500d6..ccf30f9 100644 --- a/gui/gui_window.c +++ b/gui/gui_window.c @@ -1,4 +1,4 @@ -/* $Id: gui_window.c,v 1.4 2013/02/21 23:07:19 steffen Exp $ */ +/* $Id: gui_window.c,v 1.5 2013/02/26 22:13:04 steffen Exp $ */ /*************************************************************************** * gui_window.c * @@ -35,10 +35,7 @@ void gui_window_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; - } + for (i = 0; i < GUI_MAX_ITEM; i++) win->items[i] = NULL; if (title == NULL) win->title[0] = 0; else strncpy (win->title, title, GUI_TEXTLEN); @@ -71,14 +68,14 @@ void gui_window_close (GUIWindow *win) { * add pointer to item to the window data structure.. * the items will have to be hold inside the application */ -void gui_window_item_add (GUIWindow *win, int type, void *item) { +void gui_window_item_add (GUIWindow *win, GUIItem *item) { int i, ifree = -1; if (item == NULL || win == NULL) return; for (i = 0; i < GUI_MAX_ITEM; i++) { - if (win->items[i].type == GUI_NONE && ifree == -1) ifree = i; - if (win->items[i].item == item) { + if (win->items[i] == NULL && ifree == -1) ifree = i; + if (win->items[i] == item) { d_printf ("item already added to item list."); ifree = i; } @@ -89,7 +86,6 @@ void gui_window_item_add (GUIWindow *win, int type, void *item) { exit (1); } - win->items[ifree].type = type; - win->items[ifree].item = item; + win->items[ifree] = item; };