How to replace string menu item in ncurses & C

I am trying to figure out how to replace item_namencurses with a menu. From the man pages I cannot find set_item_nameor something like that. Any ideas if there is a workaround for this?

e.g. replace "Choice 1"w / "String 1"withKEY_ENTER

#include <curses.h> 
#include <menu.h>

char *choices[] = {
    "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit",
};

int main() {
    ITEM **my_items, *cur_item;
    int c, i;
    MENU *my_menu;

    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    my_items = (ITEM **) calloc(6, sizeof(ITEM * ));
    for (i = 0; i < 5; ++i)
        my_items[i] = new_item(choices[i], choices[i]);
    my_items[5] = (ITEM*) NULL;

    my_menu = new_menu((ITEM **) my_items);
    post_menu(my_menu);
    refresh();

    while ((c = getch()) != KEY_F(1)) {
        switch (c) {
        case KEY_ENTER:
            // e.g. replace "Choice 1" w/ "String 1"
            break;
        case KEY_DOWN:
            menu_driver(my_menu, REQ_DOWN_ITEM);
            break;
        case KEY_UP:
            menu_driver(my_menu, REQ_UP_ITEM);
            break;
        }
    }
    free_item(my_items[0]);
    free_item(my_items[1]);
    free_menu(my_menu);
    endwin();
}
+3
source share
2 answers

Looks like a call set_menu_items()again - the expected method.

+1
source

Yes, a workaround for the missing set_item_name()can be writtenset_item_name();

First, consider include file menu.h, where you can find the structure definition for the structure ITEM. There you see that you can write a function such as:

void set_item_name (ITEM *itm, const char* name)
{   int len = strlen(name);
    char* n;    
    if (itm->name.str!=NULL) free((void*)(itm->name).str);
    n=strdup(name);
    itm->name.length=len;
    itm->name.str=n;
}

About the arguments:

  • itm - ,

  • name - , .

"checkbox" , , ( linux debian 6.0 libncurses5 (5.7)).

+2

All Articles