menu: draw background only onto header row #179

This commit is contained in:
nick black 2020-02-02 08:18:09 -05:00 committed by Nick Black
parent 2ced7357ce
commit 2118c5d81a
2 changed files with 40 additions and 7 deletions

View File

@ -56,7 +56,7 @@ dup_menu_items(ncmenu* ncm, const ncmenu_options* opts, int* totalwidth, int* to
free_menu_section(&ncm->sections[i]);
}
}
*totalwidth += cols + 1;
*totalwidth += cols + 2;
if(dup_menu_section(&opts->sections[i], &ncm->sections[i])){
free(ncm->sections[i].name);
while(--i){
@ -75,16 +75,37 @@ dup_menu_items(ncmenu* ncm, const ncmenu_options* opts, int* totalwidth, int* to
static int
write_header(ncmenu* ncm){
ncm->ncp->channels = ncm->headerchannels;
ncplane_set_base(ncm->ncp, ncm->headerchannels, 0, " ");
int dimy = ncplane_dim_y(ncm->ncp);
int xoff = 1; // 1 character margin on left
int dimy, dimx;
ncplane_dim_yx(ncm->ncp, &dimy, &dimx);
int xoff = 2; // 2-column margin on left
int ypos = ncm->bottom ? dimy - 1 : 0;
if(ncplane_cursor_move_yx(ncm->ncp, ypos, 0)){
return -1;
}
cell c = CELL_INITIALIZER(' ', 0, ncm->headerchannels);
if(ncplane_putc(ncm->ncp, &c) < 0){
return -1;
}
if(ncplane_putc(ncm->ncp, &c) < 0){
return -1;
}
for(int i = 0 ; i < ncm->sectioncount ; ++i){
if(ncplane_putstr_yx(ncm->ncp, ypos, xoff, ncm->sections[i].name) < 0){
if(ncplane_putstr(ncm->ncp, ncm->sections[i].name) < 0){
return -1;
}
if(ncplane_putc(ncm->ncp, &c) < 0){
return -1;
}
if(ncplane_putc(ncm->ncp, &c) < 0){
return -1;
}
xoff += mbswidth(ncm->sections[i].name) + 2;
}
while(xoff++ < dimx){
if(ncplane_putc(ncm->ncp, &c) < 0){
return -1;
}
}
return 0;
}
@ -118,8 +139,10 @@ ncmenu* ncmenu_create(notcurses* nc, const ncmenu_options* opts){
ret->unrolledsection = -1;
ret->headerchannels = opts->headerchannels;
ret->sectionchannels = opts->sectionchannels;
write_header(ret);
return ret;
if(write_header(ret) == 0){
return ret;
}
ncplane_destroy(ret->ncp);
}
free_menu_sections(ret);
}

View File

@ -63,6 +63,9 @@ int main(void){
channels_set_fg(&mopts.headerchannels, 0x00ff00);
channels_set_bg(&mopts.headerchannels, 0x440000);
struct ncmenu* top = ncmenu_create(nc, &mopts);
if(top == NULL){
goto err;
}
notcurses_render(nc);
int dimy, dimx;
@ -77,6 +80,9 @@ int main(void){
ncplane_erase(n);
mopts.bottom = true;
struct ncmenu* bottom = ncmenu_create(nc, &mopts);
if(bottom == NULL){
goto err;
}
if(ncplane_putstr_aligned(n, 0, NCALIGN_RIGHT, " -=+ menu poc. press q to exit +=- ") < 0){
return EXIT_FAILURE;
}
@ -86,4 +92,8 @@ int main(void){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
err:
notcurses_stop(nc);
return EXIT_FAILURE;
}