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