misc clean-ups

This commit is contained in:
marschap
2007-10-11 15:22:17 +00:00
parent 8dae1de0cb
commit d079440691
+75 -63
View File
@@ -44,8 +44,13 @@
extern Menu *custom_main_menu; extern Menu *custom_main_menu;
/** Basicly a patched version of LL_GetByIndex() that ignores hidden
* entries completely. (But it takes a menu as an argument.) */ /**
* Search a menu for an entry by index, ignoring hidden entries.
* \param menu Pointer to menu to search in.
* \param index ID to search for.
* \return Pointer to entry found, 0 otherwise.
*/
static void * static void *
menu_get_subitem(Menu *menu, int index) menu_get_subitem(Menu *menu, int index)
{ {
@@ -56,24 +61,24 @@ menu_get_subitem(Menu *menu, int index)
((menu != NULL) ? menu->id : "(null)"), index); ((menu != NULL) ? menu->id : "(null)"), index);
for (item = LL_GetFirst(menu->data.menu.contents); for (item = LL_GetFirst(menu->data.menu.contents);
item != NULL; item != NULL;
item = LL_GetNext(menu->data.menu.contents)) item = LL_GetNext(menu->data.menu.contents)) {
{ /* hidden items don't count at all... */
if (! item->is_hidden) if (! item->is_hidden) {
{
if (i == index) if (i == index)
return item; return item;
/* hidden items don't count at all... */
++i; ++i;
} }
} }
return NULL; return NULL;
} }
/** /**
* Searches for a subitem with id item_id. This function ignores hidden * Search a menu for an entry by its ID, ignoring hidden entries.
* entries completely. * \param menu Pointer to menu to search in.
* * \param item_id ID to search for.
* @return index of subitem if found and -1 otherwise. */ * \return Index of subitem if found, and -1 otherwise.r
*/
static int static int
menu_get_index_of(Menu *menu, char *item_id) menu_get_index_of(Menu *menu, char *item_id)
{ {
@@ -84,19 +89,18 @@ menu_get_index_of(Menu *menu, char *item_id)
((menu != NULL) ? menu->id : "(null)"), item_id); ((menu != NULL) ? menu->id : "(null)"), item_id);
for (item = LL_GetFirst(menu->data.menu.contents); for (item = LL_GetFirst(menu->data.menu.contents);
item != NULL; item != NULL;
item = LL_GetNext(menu->data.menu.contents)) item = LL_GetNext(menu->data.menu.contents)) {
{ /* hidden items don't count at all... */
if (! item->is_hidden) if (! item->is_hidden) {
{
if (strcmp(item_id, item->id) == 0) if (strcmp(item_id, item->id) == 0)
return i; return i;
/* hidden items don't count at all... */
++i; ++i;
} }
} }
return -1; return -1;
} }
static int static int
menu_visible_item_count(Menu *menu) menu_visible_item_count(Menu *menu)
{ {
@@ -133,6 +137,7 @@ menu_create(char *id, MenuEventFunc(*event_func),
return new_menu; return new_menu;
} }
void void
menu_destroy(Menu *menu) menu_destroy(Menu *menu)
{ {
@@ -152,6 +157,7 @@ menu_destroy(Menu *menu)
/* After this the general menuitem routine destroys the rest... */ /* After this the general menuitem routine destroys the rest... */
} }
void void
menu_add_item(Menu *menu, MenuItem *item) menu_add_item(Menu *menu, MenuItem *item)
{ {
@@ -167,6 +173,7 @@ menu_add_item(Menu *menu, MenuItem *item)
item->parent = menu; item->parent = menu;
} }
void void
menu_remove_item(Menu *menu, MenuItem *item) menu_remove_item(Menu *menu, MenuItem *item)
{ {
@@ -196,6 +203,7 @@ menu_remove_item(Menu *menu, MenuItem *item)
} }
} }
void void
menu_destroy_all_items(Menu *menu) menu_destroy_all_items(Menu *menu)
{ {
@@ -213,6 +221,7 @@ menu_destroy_all_items(Menu *menu)
} }
} }
MenuItem *menu_get_current_item(Menu *menu) MenuItem *menu_get_current_item(Menu *menu)
{ {
return (MenuItem*) ((menu != NULL) return (MenuItem*) ((menu != NULL)
@@ -220,6 +229,7 @@ MenuItem *menu_get_current_item(Menu *menu)
: NULL); : NULL);
} }
MenuItem *menu_find_item(Menu *menu, char *id, bool recursive) MenuItem *menu_find_item(Menu *menu, char *id, bool recursive)
{ {
MenuItem *item; MenuItem *item;
@@ -236,22 +246,23 @@ MenuItem *menu_find_item(Menu *menu, char *id, bool recursive)
if (strcmp(item->id, id) == 0) { if (strcmp(item->id, id) == 0) {
return item; return item;
} }
else if (recursive && item->type == MENUITEM_MENU) { if (recursive && (item->type == MENUITEM_MENU)) {
MenuItem *res; MenuItem *res = menu_find_item(item, id, recursive);
res = menu_find_item(item, id, recursive);
if (res) { if (res != NULL)
return res; return res;
}
} }
} }
return NULL; return NULL;
} }
void menu_set_association(Menu *menu, void *assoc) void menu_set_association(Menu *menu, void *assoc)
{ {
menu->data.menu.association = assoc; menu->data.menu.association = assoc;
} }
void menu_reset(Menu *menu) void menu_reset(Menu *menu)
{ {
debug(RPT_DEBUG, "%s(menu=[%s])", __FUNCTION__, debug(RPT_DEBUG, "%s(menu=[%s])", __FUNCTION__,
@@ -264,6 +275,7 @@ void menu_reset(Menu *menu)
menu->data.menu.scroll = 0; menu->data.menu.scroll = 0;
} }
void menu_build_screen(MenuItem *menu, Screen *s) void menu_build_screen(MenuItem *menu, Screen *s)
{ {
Widget *w; Widget *w;
@@ -298,7 +310,7 @@ void menu_build_screen(MenuItem *menu, Screen *s)
if (subitem->is_hidden) if (subitem->is_hidden)
continue; continue;
snprintf(buf, sizeof(buf)-1, "text%d", itemnr); snprintf(buf, sizeof(buf)-1, "text%d", itemnr);
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = '\0';
w = widget_create(buf, WID_STRING, s); w = widget_create(buf, WID_STRING, s);
/* (buf will be copied) */ /* (buf will be copied) */
if (w != NULL) { if (w != NULL) {
@@ -310,13 +322,12 @@ void menu_build_screen(MenuItem *menu, Screen *s)
/* Limit string length */ /* Limit string length */
w->text = strdup(subitem->text); w->text = strdup(subitem->text);
if (strlen(subitem->text) >= display_props->width-2) { if (strlen(subitem->text) >= display_props->width-2)
(w->text)[display_props->width-2] = 0; w->text[display_props->width-2] = '\0';
}
/* Add icon for checkbox */ /* Add icon for checkbox */
snprintf(buf, sizeof(buf)-1, "icon%d", itemnr); snprintf(buf, sizeof(buf)-1, "icon%d", itemnr);
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = '\0';
w = widget_create(buf, WID_ICON, s); w = widget_create(buf, WID_ICON, s);
/* (buf will be copied) */ /* (buf will be copied) */
screen_add_widget(s, w); screen_add_widget(s, w);
@@ -332,20 +343,18 @@ void menu_build_screen(MenuItem *menu, Screen *s)
w->text = malloc(strlen(subitem->text) + 4); w->text = malloc(strlen(subitem->text) + 4);
strcpy(w->text, subitem->text); strcpy(w->text, subitem->text);
strcat(w->text, " >"); strcat(w->text, " >");
if (strlen(subitem->text) >= display_props->width-1) { if (strlen(subitem->text) >= display_props->width-1)
(w->text)[display_props->width-1] = '\0'; w->text[display_props->width-1] = '\0';
}
break; break;
case MENUITEM_ACTION: case MENUITEM_ACTION:
case MENUITEM_SLIDER: case MENUITEM_SLIDER:
case MENUITEM_NUMERIC: case MENUITEM_NUMERIC:
case MENUITEM_ALPHA: case MENUITEM_ALPHA:
case MENUITEM_IP: case MENUITEM_IP:
/* Limit string length */ /* Limit string length */
w->text = strdup(subitem->text); w->text = malloc(display_props->width);
if (strlen(subitem->text) >= display_props->width-1) { strncpy(w->text, subitem->text, display_props->width);
(w->text)[display_props->width-1] = '\0'; w->text[display_props->width-1] = '\0';
}
break; break;
default: default:
assert(!"unexpected menuitem type"); assert(!"unexpected menuitem type");
@@ -381,6 +390,7 @@ void menu_build_screen(MenuItem *menu, Screen *s)
} }
void menu_update_screen(MenuItem *menu, Screen *s) void menu_update_screen(MenuItem *menu, Screen *s)
{ {
Widget *w; Widget *w;
@@ -397,67 +407,63 @@ void menu_update_screen(MenuItem *menu, Screen *s)
/* Update widgets for the title */ /* Update widgets for the title */
w = screen_find_widget(s, "title"); w = screen_find_widget(s, "title");
if (!w) report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, "title"); if (w == NULL)
report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, "title");
w->y = 1 - menu->data.menu.scroll; w->y = 1 - menu->data.menu.scroll;
/* TODO: remove next 5 limes when rendering is safe */ /* TODO: remove next 3 limes when rendering is safe */
if (w->y > 0 && w->y <= display_props->height) { w->type = ((w->y > 0) && (w->y <= display_props->height))
w->type = WID_TITLE; ? WID_TITLE
} else { : WID_NONE; /* make invisible */
w->type = WID_NONE; /* make invisible */
}
/* Update widgets for each subitem in the menu */ /* Update widgets for each subitem in the menu */
for (subitem = LL_GetFirst(menu->data.menu.contents), itemnr = 0; for (subitem = LL_GetFirst(menu->data.menu.contents), itemnr = 0;
subitem; subitem != NULL;
subitem = LL_GetNext(menu->data.menu.contents), itemnr ++) subitem = LL_GetNext(menu->data.menu.contents), itemnr ++)
{ {
char buf[10]; char buf[10];
char *p; char *p;
if (subitem->is_hidden) if (subitem->is_hidden) {
{
debug(RPT_DEBUG, "%s: menu %s has hidden menu: %s", debug(RPT_DEBUG, "%s: menu %s has hidden menu: %s",
__FUNCTION__, menu->id, subitem->id); __FUNCTION__, menu->id, subitem->id);
++hidden_count; hidden_count++;
continue; continue;
} }
snprintf(buf, sizeof(buf)-1, "text%d", itemnr); snprintf(buf, sizeof(buf)-1, "text%d", itemnr);
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = '\0';
w = screen_find_widget(s, buf); w = screen_find_widget(s, buf);
if (!w) report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, buf); if (w == NULL)
report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, buf);
w->y = 2 + itemnr - hidden_count - menu->data.menu.scroll; w->y = 2 + itemnr - hidden_count - menu->data.menu.scroll;
/* TODO: remove next 5 lines when rendering is safe */ /* TODO: remove next 3 lines when rendering is safe */
if (w->y > 0 && w->y <= display_props->height) { w->type = ((w->y > 0) && (w->y <= display_props->height))
w->type = WID_STRING; ? WID_STRING
} else { : WID_NONE; /* make invisible */
w->type = WID_NONE; /* make invisible */
}
switch (subitem->type) { switch (subitem->type) {
case MENUITEM_CHECKBOX: case MENUITEM_CHECKBOX:
/* Update icon value for checkbox */ /* Update icon value for checkbox */
snprintf(buf, sizeof(buf)-1, "icon%d", itemnr); snprintf(buf, sizeof(buf)-1, "icon%d", itemnr);
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = '\0';
w = screen_find_widget(s, buf); w = screen_find_widget(s, buf);
if (!w) report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, buf); if (w == NULL)
report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, buf);
w->y = 2 + itemnr - menu->data.menu.scroll; w->y = 2 + itemnr - menu->data.menu.scroll;
w->length = ((int[]){ICON_CHECKBOX_OFF,ICON_CHECKBOX_ON,ICON_CHECKBOX_GRAY})[subitem->data.checkbox.value]; w->length = ((int[]){ICON_CHECKBOX_OFF,ICON_CHECKBOX_ON,ICON_CHECKBOX_GRAY})[subitem->data.checkbox.value];
/* TODO: remove next 5 lines when rendering is safe */ /* TODO: remove next 3 lines when rendering is safe */
if (w->y > 0 && w->y <= display_props->height) { w->type = ((w->y > 0) && (w->y <= display_props->height))
w->type = WID_ICON; ? WID_ICON
} else { : WID_NONE; /* make invisible */
w->type = WID_NONE; /* make invisible */
}
break; break;
case MENUITEM_RING: case MENUITEM_RING:
if (subitem->data.ring.value >= LL_Length(subitem->data.ring.strings)) { if (subitem->data.ring.value >= LL_Length(subitem->data.ring.strings)) {
/* No strings available */ /* No strings available */
memcpy(w->text, subitem->text, display_props->width - 2); memcpy(w->text, subitem->text, display_props->width - 2);
w->text[ display_props->width - 2 ] = 0; w->text[ display_props->width - 2] = '\0';
} }
else { else {
/* Limit string length and add ringstring */ /* Limit string length and add ringstring */
@@ -469,7 +475,7 @@ void menu_update_screen(MenuItem *menu, Screen *s)
* display the item text */ * display the item text */
strcpy(w->text, " "); strcpy(w->text, " ");
memcpy(w->text + 1, p, a); memcpy(w->text + 1, p, a);
w->text[a + 1] = 0; w->text[a + 1] = '\0';
} }
else { else {
short b = display_props->width - 2 - strlen(p); short b = display_props->width - 2 - strlen(p);
@@ -509,6 +515,7 @@ void menu_update_screen(MenuItem *menu, Screen *s)
report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, "downscroller"); report(RPT_ERR, "%s: could not find widget: %s", __FUNCTION__, "downscroller");
} }
MenuItem *menu_get_item_for_predecessor_check(Menu *menu) MenuItem *menu_get_item_for_predecessor_check(Menu *menu)
{ {
MenuItem *subitem = menu_get_subitem(menu, menu->data.menu.selector_pos); MenuItem *subitem = menu_get_subitem(menu, menu->data.menu.selector_pos);
@@ -536,6 +543,7 @@ MenuItem *menu_get_item_for_predecessor_check(Menu *menu)
} }
} }
MenuItem *menu_get_item_for_successor_check(Menu *menu) MenuItem *menu_get_item_for_successor_check(Menu *menu)
{ {
MenuItem *subitem = menu_get_subitem(menu, menu->data.menu.selector_pos); MenuItem *subitem = menu_get_subitem(menu, menu->data.menu.selector_pos);
@@ -557,6 +565,7 @@ MenuItem *menu_get_item_for_successor_check(Menu *menu)
} }
} }
MenuResult menu_process_input(Menu *menu, MenuToken token, const char *key, unsigned int keymask) MenuResult menu_process_input(Menu *menu, MenuToken token, const char *key, unsigned int keymask)
{ {
MenuItem *subitem; MenuItem *subitem;
@@ -703,6 +712,7 @@ MenuResult menu_process_input(Menu *menu, MenuToken token, const char *key, unsi
return MENURESULT_ERROR; return MENURESULT_ERROR;
} }
/** positions current item pointer on subitem subitem_id. If subitem_id is /** positions current item pointer on subitem subitem_id. If subitem_id is
* hidden or not valid subitem of menu this function does nothing. */ * hidden or not valid subitem of menu this function does nothing. */
void menu_select_subitem(Menu *menu, char *subitem_id) void menu_select_subitem(Menu *menu, char *subitem_id)
@@ -726,3 +736,5 @@ void menu_select_subitem(Menu *menu, char *subitem_id)
menu->data.menu.selector_pos = position; menu->data.menu.selector_pos = position;
menu->data.menu.scroll = position; menu->data.menu.scroll = position;
} }
/* EOF */