diff --git a/src/lib/internal.h b/src/lib/internal.h index ea7f6da90..1d35f54e6 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -170,6 +170,7 @@ typedef struct ncmenu_int_section { int bodycols; // column width of longest item int itemselected; // current item selected, -1 for no selection int shortcut_offset; // column offset within name of shortcut EGC + int enabled_item_count; // number of enabled items: section is disabled iff 0 } ncmenu_int_section; typedef struct ncfdplane { diff --git a/src/lib/menu.c b/src/lib/menu.c index 4b83f519f..33e454aa8 100644 --- a/src/lib/menu.c +++ b/src/lib/menu.c @@ -81,6 +81,7 @@ dup_menu_section(ncmenu_int_section* dst, const struct ncmenu_section* src){ // we must reject any section which is entirely separators bool gotitem = false; dst->itemcount = 0; + dst->enabled_item_count = 0; dst->items = malloc(sizeof(*dst->items) * src->itemcount); if(dst->items == NULL){ return -1; @@ -114,6 +115,7 @@ dup_menu_section(ncmenu_int_section* dst, const struct ncmenu_section* src){ } ++dst->itemcount; } + dst->enabled_item_count = dst->itemcount; if(!gotitem){ while(dst->itemcount){ free(dst->items[--dst->itemcount].desc); @@ -452,36 +454,26 @@ int ncmenu_rollup(ncmenu* n){ } int ncmenu_nextsection(ncmenu* n){ - int nextsection = n->unrolledsection + 1; - if(nextsection){ - ncmenu_rollup(n); - if(nextsection == n->sectioncount){ - nextsection = 0; - } - } - if(n->sections[nextsection].name == NULL){ + int nextsection = n->unrolledsection; + // FIXME probably best to detect cycles + do{ if(++nextsection == n->sectioncount){ nextsection = 0; } - } + }while(n->sections[nextsection].name == NULL || + n->sections[nextsection].enabled_item_count == 0); return ncmenu_unroll(n, nextsection); } int ncmenu_prevsection(ncmenu* n){ int prevsection = n->unrolledsection; - if(n->unrolledsection < 0){ - prevsection = 1; - }else{ - ncmenu_rollup(n); - } - if(--prevsection == -1){ - prevsection = n->sectioncount - 1; - } - if(n->sections[prevsection].name == NULL){ + // FIXME probably best to detect cycles + do{ if(--prevsection < 0){ prevsection = n->sectioncount - 1; } - } + }while(n->sections[prevsection].name == NULL || + n->sections[prevsection].enabled_item_count == 0); return ncmenu_unroll(n, prevsection); } @@ -492,6 +484,7 @@ int ncmenu_nextitem(ncmenu* n){ } } ncmenu_int_section* sec = &n->sections[n->unrolledsection]; + // FIXME probably best to detect cycles do{ if(++sec->itemselected == sec->itemcount){ sec->itemselected = 0; @@ -507,6 +500,7 @@ int ncmenu_previtem(ncmenu* n){ } } ncmenu_int_section* sec = &n->sections[n->unrolledsection]; + // FIXME probably best to detect cycles do{ if(sec->itemselected-- == 0){ sec->itemselected = sec->itemcount - 1;