GUIList changes...

master
steffen 13 years ago
parent 0da7441a73
commit 66d2be5f8a

@ -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 * gui.c
* *
@ -30,7 +30,6 @@
GUIWindow *currentwin = NULL; GUIWindow *currentwin = NULL;
#define GUI_ITEM_IS_INSIDE(__item__,__pos__) gui_is_inside(__item__->x, __item__->y, __item__->w, __item__->h, __pos__) #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) { 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; if (x <= pos.x && x+w >= pos.x && y <= pos.y && y+h >= pos.y) return 1;
else return 0; else return 0;
@ -95,22 +94,22 @@ void gui_draw () {
} }
/* draw items.. */ /* draw items.. */
for (i = 0; i < GUI_MAX_ITEM; i++) for (i = 0; i < GUI_MAX_ITEM; i++) if (currentwin->items[i])
switch (currentwin->items[i].type) { switch (currentwin->items[i]->type) {
case (GUI_BUTTON): { case (GUI_BUTTON): {
GUIButton *button = (GUIButton *) currentwin->items[i].item; GUIButton *button = (GUIButton *) currentwin->items[i]->data;
if (button->callback_draw != NULL) button->callback_draw (button); if (button->func_draw != NULL) button->func_draw (currentwin->items[i]);
else gui_button_draw (button); else gui_button_draw (currentwin->items[i]);
} }
break; break;
case (GUI_LABEL): case (GUI_LABEL):
gui_label_draw ((GUILabel*)currentwin->items[i].item); gui_label_draw (currentwin->items[i]);
break; break;
case (GUI_ENTRY): case (GUI_ENTRY):
gui_entry_draw ((GUIEntry*)currentwin->items[i].item); gui_entry_draw (currentwin->items[i]);
break; break;
case (GUI_LIST): case (GUI_LIST):
gui_list_draw ((GUIList*)currentwin->items[i].item); gui_list_draw (currentwin->items[i]);
break; break;
// case (GUI_ENTRY): // case (GUI_ENTRY):
// gui_entry_draw ((GUIEntry*)currentwin->items[i].item); // gui_entry_draw ((GUIEntry*)currentwin->items[i].item);
@ -143,6 +142,7 @@ int gui_event (GUIEvent event) {
int i; int i;
iPoint winpos = { 0 }; iPoint winpos = { 0 };
static int event_called = 0; static int event_called = 0;
GUIItem *item = NULL;
if (event_called) return 1; if (event_called) return 1;
event_called = 1; event_called = 1;
@ -151,56 +151,29 @@ int gui_event (GUIEvent event) {
winpos.x = event.mousepos.x - currentwin->x; winpos.x = event.mousepos.x - currentwin->x;
winpos.y = event.mousepos.y - currentwin->y; winpos.y = event.mousepos.y - currentwin->y;
for (i = 0; i < GUI_MAX_ITEM; i++) item = currentwin->focus;
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): for (i = 0; i < GUI_MAX_ITEM; i++) if (currentwin->items[i] != NULL)
break; if (GUI_ITEM_IS_INSIDE (currentwin->items[i], winpos))
item = currentwin->items[i];
case (GUI_ENTRY): { d_printf ("%p", item);
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): { if (item) switch (item->type) {
GUIList *list = (GUIList *) currentwin->items[i].item; case (GUI_ENTRY):
if (GUI_ITEM_IS_INSIDE (list, winpos)) { gui_event_addmousepos (&event, winpos, -1);
d_printf ("list.."); gui_button_event (item, &event);
gui_list_event (list, &event); event_called = 1;
event_called = 0;
return 1; return 1;
}
}
break; break;
} case (GUI_BUTTON):
gui_event_addmousepos (&event, winpos, -1);
if (currentwin->focus) { gui_entry_event (item, &event);
d_printf ("focus..");
switch (currentwin->focus->type) {
case (GUI_ENTRY):
gui_entry_event ((GUIEntry*)currentwin->focus->item, &event);
event_called = 1; event_called = 1;
return 1; return 1;
break; break;
case (GUI_LIST): case (GUI_LIST):
gui_list_event ((GUIList*)currentwin->focus->item, &event); gui_list_event (item, &event);
event_called = 1; event_called = 1;
return 1; return 1;
break; break;
@ -208,7 +181,6 @@ int gui_event (GUIEvent event) {
break; break;
} }
} }
}
/* no gui active nor any window is responsible.. */ /* no gui active nor any window is responsible.. */
// d_printf ("no event.."); // d_printf ("no event..");
@ -230,3 +202,9 @@ int gui_event (GUIEvent event) {
return 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;
};

@ -61,6 +61,14 @@ enum {
// #define WGUI_S_**** 0x0080 // #define WGUI_S_**** 0x0080
#define WGUI_S_MODAL 0x0100 #define WGUI_S_MODAL 0x0100
struct _GUIItem_ {
int type;
int x, y, w, h;
char data[];
} typedef GUIItem;
struct _GUIEvent_ { struct _GUIEvent_ {
int event; int event;
uint32_t keyval; // needed for utf8 (max was 32bit?) uint32_t keyval; // needed for utf8 (max was 32bit?)
@ -70,17 +78,15 @@ struct _GUIEvent_ {
struct _GUIButton_ { struct _GUIButton_ {
int x, y, w, h;
char caption[GUI_TEXTLEN]; char caption[GUI_TEXTLEN];
struct color *col; struct color *col;
struct color *textcol; struct color *textcol;
void (*callback_clicked) (int x, int y); void (*callback_clicked) (int x, int y);
void (*callback_draw) (struct _GUIButton_ *btn); void (*func_draw) (GUIItem *item);
} typedef GUIButton; } typedef GUIButton;
struct _GUIList_ { struct _GUIList_ {
int x, y, w, h;
char **data; char **data;
int vs; // first element to draw int vs; // first element to draw
int selected; int selected;
@ -89,14 +95,12 @@ struct _GUIList_ {
struct { struct {
int x, y;
char text[GUI_TEXTLEN]; char text[GUI_TEXTLEN];
struct color *textcol; struct color *textcol;
} typedef GUILabel; } typedef GUILabel;
struct { struct {
int x, y, w, h;
char text[GUI_TEXTLEN]; char text[GUI_TEXTLEN];
int curpos; int curpos;
int overwrite; int overwrite;
@ -106,18 +110,12 @@ struct {
struct { struct {
int x, y, w, h;
char text[GUI_TEXTLEN]; char text[GUI_TEXTLEN];
int checked; int checked;
void (*callback_changed) (); void (*callback_changed) ();
} typedef GUICheckbox; } typedef GUICheckbox;
struct _GUIItem_ {
int type;
void *item;
} typedef GUIItem;
struct _GUIWindow_ { struct _GUIWindow_ {
void (*callback_close) (); void (*callback_close) ();
@ -132,7 +130,7 @@ struct _GUIWindow_ {
int h; int h;
int style; int style;
GUIItem items[GUI_MAX_ITEM]; GUIItem *items[GUI_MAX_ITEM];
GUIItem *focus; GUIItem *focus;
} typedef GUIWindow; } typedef GUIWindow;
@ -142,30 +140,31 @@ extern void gui_show (GUIWindow *win);
extern void gui_draw (); extern void gui_draw ();
extern void gui_close (); extern void gui_close ();
extern int gui_event (GUIEvent event); 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_new (GUIWindow *win, int w, int h, char *title);
extern void gui_window_close (GUIWindow *win); 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 */ /* button functions */
extern void gui_button_draw (GUIButton *button); extern void gui_button_draw (GUIItem *item);
extern void gui_button_event (GUIButton *entry, GUIEvent *event); extern void gui_button_event (GUIItem *item, GUIEvent *event);
extern GUIButton *gui_button_new (char *name, int x, int y, int w, int h); extern GUIItem *gui_button_new (char *name, int x, int y, int w, int h);
/* label functions */ /* label functions */
extern void gui_label_draw (GUILabel *label); extern void gui_label_draw (GUIItem *item);
extern GUILabel *gui_label_new (char *text, int x, int y); extern GUIItem *gui_label_new (char *text, int x, int y);
/* entry functions */ /* entry functions */
extern void gui_entry_draw (GUIEntry *entry); extern void gui_entry_draw (GUIItem *item);
extern void gui_entry_event (GUIEntry *entry, GUIEvent *event); extern void gui_entry_event (GUIItem *item, GUIEvent *event);
extern GUIEntry *gui_entry_new (char *text, int x, int y, int w, int h); extern GUIItem *gui_entry_new (char *text, int x, int y, int w, int h);
/* list functions */ /* list functions */
extern void gui_list_draw (GUIList *list); extern void gui_list_draw (GUIItem *item);
extern void gui_list_event (GUIList *list, GUIEvent *event); extern void gui_list_event (GUIItem *item, GUIEvent *event);
extern void gui_list_setselect (GUIList *list); extern void gui_list_setselect (GUIItem *item);
extern GUIList *gui_list_new (int x, int y, int w, int h); extern GUIItem *gui_list_new (int x, int y, int w, int h);
/************************************************************************** /**************************************************************************
* gui windows * gui windows

@ -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 * gui_button.c
* *
@ -32,43 +32,61 @@
/**************************************************************************** /****************************************************************************
* button * button
*/ */
void gui_button_draw (GUIButton *button) { void gui_button_draw (GUIItem *item) {
struct line_style ls; 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.width = 1.0;
ls.c = ls.borderc = color[COLOR_white][3]; ls.c = ls.borderc = color[COLOR_white][3];
draw_polygonstart (); draw_polygonstart ();
draw_polygonadd (button->x, button->y); draw_polygonadd (item->x, item->y);
draw_polygonadd (button->x, button->y + button->h); draw_polygonadd (item->x, item->y + item->h);
draw_polygonadd (button->x + button->w, button->y + button->h); draw_polygonadd (item->x + item->w, item->y + item->h);
draw_polygonadd (button->x + button->w, button->y); draw_polygonadd (item->x + item->w, item->y);
draw_polygonfinish (currentwin->screen, ls, *button->col, 1); 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) { GUIItem *gui_button_new (char *caption, int x, int y, int w, int h) {
GUIButton *item = (GUIButton*) ml_malloc (sizeof (GUIButton)); GUIItem *item = (GUIItem*) ml_malloc (sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT);
GUIButton *button = (GUIButton *) item->data;
memset (item, 0x0, sizeof (GUIItem) + sizeof (GUIButton) + POINTERALIGNMENT);
if (caption == NULL) item->caption[0] = 0; if (caption == NULL) button->caption[0] = 0;
else strncpy (item->caption, caption, GUI_TEXTLEN); else strncpy (button->caption, caption, GUI_TEXTLEN);
item->x = x; item->x = x;
item->y = y; item->y = y;
item->w = w; item->w = w;
item->h = h; item->h = h;
item->col = &color[COLOR_white][1]; button->col = &color[COLOR_white][1];
item->textcol = &color[COLOR_white][3]; button->textcol = &color[COLOR_white][3];
item->callback_clicked = NULL; button->callback_clicked = NULL;
item->callback_draw = NULL; button->func_draw = NULL;
return item; 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) { 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);
} }
}; };

@ -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 * gui_entry.c
* *
@ -67,9 +67,11 @@ void gui_entry_draw (GUIEntry *entry) {
void gui_entry_event (GUIEntry *entry, GUIEvent *event) { void gui_entry_event (GUIEntry *entry, GUIEvent *event) {
char text1[GUI_TEXTLEN]; char text1[GUI_TEXTLEN];
d_printf ("event: %d pos: %d,%d", event->event, event->mousepos.x, event->mousepos.y);
if (event->event == EGUI_MOUSERELEASED if (event->event == EGUI_MOUSERELEASED
&& entry->x <= event->mousepos.x && entry->x+entry->w >= event->mousepos.x && event->mousepos.x >= 0 && entry->x+entry->w >= event->mousepos.x
&& entry->y <= event->mousepos.y && entry->y+entry->h >= event->mousepos.y) { && event->mousepos.y >= 0 && entry->y+entry->h >= event->mousepos.y) {
d_printf ("gui_entry_event set focus"); d_printf ("gui_entry_event set focus");
currentwin->focus = entry; currentwin->focus = entry;
} }

@ -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 * gui_list.c
* *
@ -32,38 +32,43 @@
/**************************************************************************** /****************************************************************************
* list * list
*/ */
void gui_list_draw (GUIList *list) { void gui_list_draw (GUIItem *item) {
struct line_style ls; struct line_style ls;
int max, i, x, y, j; int max, i, x, y, j;
GUIList *list = NULL;
if (item == NULL) return;
else list = (GUIList *) item->data;
ls.width = 1.0; 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]; ls.c = ls.borderc = color[COLOR_white][3];
else else
ls.c = ls.borderc = color[COLOR_white][2]; 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, item->x, item->y, item->x+item->w, item->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, item->x+item->w, item->y, item->x+item->w, item->y+item->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, item->x+item->w, item->y+item->h, item->x, item->y+item->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->h, item->x, item->y, ls);
if (list->data == NULL) return; /* find last max */
for (max = 0; list->data[max] != NULL; max++); max = 0;
// for (max = 0; list->data[max] != NULL; max++);
/* range to display */ /* range to display */
i = max - list->h/16; i = max - item->h/16;
if (i < list->vs) list->vs = i; if (i < list->vs) list->vs = i;
if (list->vs < 0) list->vs = 0; if (list->vs < 0) list->vs = 0;
for (i = list->vs; i < (list->vs + list->h/16) && i < max; i++) { for (i = list->vs; i < (list->vs + item->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]); 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, list->x + 5, list->y + 5 + 16 * (i - list->vs), list->data[i], &color[COLOR_white][3]); 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]; ls.c = ls.borderc = color[COLOR_white][3];
for (i = -1; i < 2; i += 2) { for (i = -1; i < 2; i += 2) {
if (i < 0) y = list->y + list->h; if (i < 0) y = item->y + item->h;
else y = list->y; else y = item->y;
x = list->x + list->w; x = item->x + item->w;
for (j = 0; j <= 5; j++) { 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, y, x-j, y + i*5, ls);
gfx_draw_line (currentwin->screen, x - 5 -j, y+ i*5, x-5, y, 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 * event handling
*/ */
void gui_list_event (GUIList *list, GUIEvent *event) { void gui_list_event (GUIItem *item, GUIEvent *event) {
int x = event->mousepos.x - list->x; int x = event->mousepos.x - item->x;
int y = event->mousepos.y - list->y; int y = event->mousepos.y - item->y;
int i; int i;
GUIList *list = NULL;
if (item == NULL) return;
else list = (GUIList *) item->data;
/* set focus */ /* set focus */
if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= list->w && y >= 0 && y <= list->h) { if (event->event == EGUI_MOUSERELEASED && x >= 0 && x <= item->w && y >= 0 && y <= item->h) {
currentwin->focus = (void *) list; currentwin->focus = (void *) item;
if (y <= 5 && x >= list->w - 10) list->vs--; if (y <= 5 && x >= item->w - 10) list->vs--;
else if (y >= list->h - 5 && x >= list->w - 10) list->vs++; else if (y >= item->h - 5 && x >= item->w - 10) list->vs++;
else { /* select entry */ else { /* select entry */
i = (y-5)/16 + list->vs; i = (y-5)/16 + list->vs;
list->selected = i; 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) { GUIItem *gui_list_new (int x, int y, int w, int h) {
GUIList *item = (GUIList*) ml_malloc (sizeof (GUIList)); GUIItem *item = (GUIItem *) ml_malloc (sizeof (GUIList) + sizeof (GUIItem) + POINTERALIGNMENT);
GUIList *list = (GUIList *) item->data;
item->x = x; item->x = x;
item->y = y; item->y = y;
item->w = w; item->w = w;
item->h = h; item->h = h;
item->data = NULL; list->selected = -1;
item->selected = -1; list->vs = 0;
item->vs = 0; list->callback_selectitem = NULL;
item->callback_selectitem = NULL;
return item; return item;
}; };

@ -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 * gui_window.c
* *
@ -35,10 +35,7 @@
void gui_window_new (GUIWindow *win, int w, int h, char *title) { void gui_window_new (GUIWindow *win, int w, int h, char *title) {
int i; int i;
for (i = 0; i < GUI_MAX_ITEM; i++) { for (i = 0; i < GUI_MAX_ITEM; i++) win->items[i] = NULL;
win->items[i].item = NULL;
win->items[i].type = GUI_NONE;
}
if (title == NULL) win->title[0] = 0; if (title == NULL) win->title[0] = 0;
else strncpy (win->title, title, GUI_TEXTLEN); 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.. * add pointer to item to the window data structure..
* the items will have to be hold inside the application * 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; int i, ifree = -1;
if (item == NULL || win == NULL) return; if (item == NULL || win == NULL) return;
for (i = 0; i < GUI_MAX_ITEM; i++) { for (i = 0; i < GUI_MAX_ITEM; i++) {
if (win->items[i].type == GUI_NONE && ifree == -1) ifree = i; if (win->items[i] == NULL && ifree == -1) ifree = i;
if (win->items[i].item == item) { if (win->items[i] == item) {
d_printf ("item already added to item list."); d_printf ("item already added to item list.");
ifree = i; ifree = i;
} }
@ -89,7 +86,6 @@ void gui_window_item_add (GUIWindow *win, int type, void *item) {
exit (1); exit (1);
} }
win->items[ifree].type = type; win->items[ifree] = item;
win->items[ifree].item = item;
}; };

Loading…
Cancel
Save