ncurses 与 panel

ncurses 与 panel

一下是ncurses使用面板库panel的一个demo程序。

#include <ncurses.h>
#include <panel.h>
#include <string.h>

void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);

int main()
{
    WINDOW *my_wins[3];
    PANEL *my_panels[3];
    PANEL *top;
    int ch;
    int new_x;
    int new_y;
    int widht;
    int height;
    int panel_c;
    enum {
        set_size,
        move_,
        none_
    } status = none_;

    /*初始化curses */
    initscr();
    start_color();
    raw();
    noecho();
    keypad(stdscr, TRUE);
    curs_set(0);

    /* 初始化所有的颜色*/
    init_pair(1, COLOR_RED, COLOR_WHITE);
    init_pair(2, COLOR_GREEN, COLOR_BLACK);
    init_pair(3, COLOR_BLUE, COLOR_BLACK);
    init_pair(4, COLOR_CYAN, COLOR_BLACK);
    init_wins(my_wins, 3);

    my_panels[0] = new_panel(my_wins[0]);
    my_panels[1] = new_panel(my_wins[1]);
    my_panels[2] = new_panel(my_wins[2]);

    set_panel_userptr(my_panels[0],my_panels[1]);
    set_panel_userptr(my_panels[1],my_panels[2]);
    set_panel_userptr(my_panels[2],my_panels[0]);

    update_panels();
    attron(COLOR_PAIR(4));
    attroff(COLOR_PAIR(4));
    doupdate();

    top = my_panels[2];
    panel_c = 2;
    mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]", panel_c + 1);
    while((ch = getch()) != KEY_F(1))
    {
        switch(ch) {
            case '\t':
                if (status == none_) {
                    bool is_hide;
                    top = (PANEL *) panel_userptr(top);
                    is_hide = panel_hidden(top);
                    if (panel_c == 2)
                        panel_c = 0;
                    else
                        panel_c ++;
                    mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]", panel_c + 1);
                    if (is_hide)
                        printw(" [hide] ");
                    else {
                        top_panel(top);
                        printw(" [show] ");
                    }
                }
                break;
            case 'm': {
                WINDOW *win = panel_window(top);
                getbegyx(win, new_y, new_x);
                status = move_;
                mvprintw(LINES-2, 0, "Moveing Windows (ENTER to Exit) [%d]    ", panel_c + 1);
                break;
            }
            case 'r': {
                WINDOW *win = panel_window(top);
                getbegyx(win, new_y, new_x);
                getmaxyx(win, height, widht);
                status = set_size;
                mvprintw(LINES-2, 0, "Set Windows Size (ENTER to Exit) [%d] %d %d    ", panel_c + 1, height, widht);
                break;
            }
            case 's':
                if (status == none_)
                    show_panel(top);
                break;
            case 'h':
                if (status == none_) {
                    if (panel_hidden(top))
                        show_panel(top);
                    else
                        hide_panel(top);
                }
                break;
            case KEY_RIGHT:
                if (status == move_)
                    new_x ++;
                else
                    widht ++;
                break;
            case KEY_LEFT:
                if (status == move_)
                    new_x --;
                else
                    widht --;
                break;
            case KEY_UP:
                if (status == move_)
                    new_y --;
                else
                    height --;
                break;
            case KEY_DOWN:
                if (status == move_)
                    new_y ++;
                else
                    height ++;
                break;
            case 10:
                status = none_;
                mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]\n", panel_c + 1);
                break;
            default:
                mvprintw(LINES-3, 0, "ch = %d\n", ch);
                break;
        }
        if (status == move_)
            move_panel(top, new_y, new_x);
        else if (status == set_size) {
            char label[80] = {};
            WINDOW *old = panel_window(top);
            WINDOW *win = newwin(height, widht, new_y, new_x);
            replace_panel(my_panels[panel_c], win);
            delwin(old);

            sprintf(label, "Window Number %d", panel_c + 1);
            win_show(win, label, panel_c + 1);
        }
        update_panels();
        doupdate();
    }
    endwin();
    return 0;
}

// 初始化三个窗体并放入数组wins中
void init_wins(WINDOW *wins[], int n) {
    char label[80] = {};
    int x = 10;
    int y = 2;

    for(int i = 0; i < n; i++) {
        wins[i] = newwin(10, 40, y, x);
        sprintf(label, "Window Number %d", i + 1);  // 设置窗体名字
        win_show(wins[i], label, i + 1);  // 绘制窗体内的内容
        y += 3;
        x += 7;
    }
}

void win_show(WINDOW *win, char *label, int label_color) {
    int startx;
    int starty;
    int height;
    int width;

    getbegyx(win, starty, startx);
    getmaxyx(win, height, width);
    box(win, 0, 0);
    mvwhline(win, 2, 1, '-', width-2);  // 画一条线
    print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));  // 在中间显示文字
}

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();
}

tab切换窗体,按m改变窗体位置,按r设定窗体大小。按h显示或隐藏窗体。

posted @ 2020-08-23 15:02  桓公子  阅读(193)  评论(0编辑  收藏  举报