ncurses 与 menu

ncurses 与 menu

一下位ncurses和菜单库menu的demo程序

#include <menu.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4

char *choices[] = {
        "Choice 1",
        "Choice 2",
        "Choice 3",
        "Choice 4",
        "Choice 5",
        "Choice 6",
        "Choice 7",
        "Choice 8",
        "Choice 9",
        "Choice 10",
        "Choice 11",
        "Choice 12",
        "Choice 13",
        "Choice 14",
        "Choice 15",
        "Choice 16",
        "Choice 17",
        "Choice 18",
        "Choice 19",
        "Choice 20",
        "Exit",
        NULL,
};
void print_in_middle(WINDOW *win, int y, int startx, int width, char *string, chtype color);

int main(){
    ITEM **my_items;
    int c;
    MENU *my_menu;
    WINDOW *my_menu_win;
    int n_choices;
    initscr();
    start_color();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    curs_set(0);
    init_pair(1, COLOR_RED, COLOR_BLACK);

    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
    for(int i = 0; i < n_choices; i++)
        my_items[i] = new_item(choices[i], choices[i]);
    item_opts_off(my_items[3], O_SELECTABLE);
    my_menu = new_menu((ITEM **)my_items);

    my_menu_win = newwin(10, 50, 4, 4);
    keypad(my_menu_win, TRUE);

    set_menu_win(my_menu, my_menu_win);
    set_menu_sub(my_menu, derwin(my_menu_win, 6, 48, 3, 1));
    set_menu_mark(my_menu, " * ");
    menu_opts_off(my_menu, O_SHOWDESC);
    menu_opts_off(my_menu, O_ONEVALUE);
    set_menu_format(my_menu, 6, 2);

    box(my_menu_win, 0, 0);
    print_in_middle(my_menu_win, 1, 0, 50, "My Menu", COLOR_PAIR(1));
    mvwhline(my_menu_win, 2, 1, ACS_HLINE, 48);
    mvprintw(LINES - 2, 0, "F1 to exit");
    refresh();

    post_menu(my_menu);
    wrefresh(my_menu_win);
    while((c = wgetch(my_menu_win)) != KEY_F(1))
    { switch(c)
        {
            case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(my_menu, REQ_UP_ITEM);
                break;
            case KEY_LEFT:
                menu_driver(my_menu, REQ_LEFT_ITEM);
                break;
            case KEY_RIGHT:
                menu_driver(my_menu, REQ_RIGHT_ITEM);
                break;
            case 'm':
                menu_driver(my_menu, REQ_SCR_DPAGE);
                break;
            case 'n':
                menu_driver(my_menu, REQ_SCR_UPAGE);
                break;
            case ' ':
                menu_driver(my_menu, REQ_TOGGLE_ITEM);
                break;
            case '\n':{
                int count = item_count(my_menu);
                char tmp[200]= {};
                ITEM **item = menu_items(my_menu);
                for (int i=0; i < count; i++) {
                    if (item_value(item[i]) == TRUE) {
                        strcat(tmp, item_name(item[i]));
                        strcat(tmp, " ");
                    }
                }
                move(LINES - 3, 0);
                clrtoeol();
                mvprintw(LINES - 3, 0, "%s", tmp);
                refresh();
                break;
            }
            case 'u':{
                ITEM *item = current_item(my_menu);
                move(LINES - 3, 0);
                clrtoeol();
                mvprintw(LINES - 3, 0, "%s", item_name(item));
                refresh();
                break;
            }
            case 'r':{
                set_current_item(my_menu, my_items[4]);
                break;
            }
        }
        wrefresh(my_menu_win);
    }
    unpost_menu(my_menu);
    free_menu(my_menu);
    for(int i = 0; i < n_choices; ++i)
        free_item(my_items[i]);
    free(my_items);
    endwin();
}

void print_in_middle(WINDOW *win, int y, int startx, int width, char *string, chtype color) {
    int x;

    if(win == NULL)
        win = stdscr;
    if(y == 0)
        getyx(win, y, x);
    x = startx + (width - strlen(string)) / 2;

    wattron(win, color);
    mvwprintw(win, y, x, "%s", string);
    wattroff(win, color);
    refresh();
}

使用方向键可选择选中项,按空格勾选,按回车确定
r显示当前选中项,n将选中Choice 5

posted @ 2020-08-23 17:35  桓公子  阅读(258)  评论(0编辑  收藏  举报