HTMLify

menu.c
Views: 2 | Author: abh
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *get_item_name(int *number) {
    static char name[10];
    sprintf(name, "Item %d", (*number)++);
    return name;
}

void draw_box(int y, int x, int width, int height) {
    int c_x = getcurx(stdscr), c_y = getcury(stdscr);
    int i;

    // draw top and bottm line
    for (i=0; i<width; i++) {
        if (i == 0) {
            mvaddch(y, x, '#');
            mvaddch(y+height-1, x, '#');
            addch('3');
        } else if (i == width-1) {
            mvaddch(y, x+i, '#');
            mvaddch(y+height-1, x+i, '#');
        } else {
            mvaddch(y, x+i, '-');
            mvaddch(y+height-1, x+i, '-');
        }
    }

    // draw left right lien
    for (i=1; i<height-1; i++) {
        mvaddch(y + i, x, '|');
        mvaddch(y + i, x + width - 1, '|');
    }
    
    move(c_y, c_x);
}

void draw_list(char items[][10], int item_count, int selected_item, int y, int x) {
    int cx = getcurx(stdscr), cy = getcury(stdscr);

    for (int i=1; i<=item_count; i++) {
        if (i == selected_item) {
            attron(A_BOLD);
            attron(A_REVERSE);
        }
        move(y + i, x + 2);
        addstr(items[i-1]);
        if (i == selected_item) {
            attroff(A_BOLD);
            attroff(A_REVERSE);
        }
    }

    draw_box(y, x, 10 + 4, item_count + 2);

    move(cy, cx);
}

void draw_active_list(char items[][10], int item_count, int selected_item, int y, int x) {
    // just add some colored marks in list
    draw_list(items, item_count, selected_item, y, x);
    int cx = getcurx(stdscr), cy = getcury(stdscr);
    attron(A_REVERSE);
    mvaddstr(y, x+1, "____________");
    attroff(A_REVERSE);
    move(cy, cx);
}

int main() {
    int key;
    int menu_depth, current_menu_depth = -1, max_item_len = 10, in=1, i, j, current_menu = 0, opened_menu=0;
    int *selected_items_indexes = NULL, *item_count = NULL, *item_hover = NULL;
    char items[32][128][10];

    printf("Enter the depth of combo menu : ");
    scanf("%d", &menu_depth);

    if (menu_depth == 0) {
        exit(0);
    }

    item_count = realloc(item_count, sizeof(int) * menu_depth);
    item_hover = realloc(item_hover, sizeof(int) * menu_depth);
    selected_items_indexes = realloc(selected_items_indexes, sizeof(int) * menu_depth);


    printf("Enter number of items in menu:\n");
    for (i=0; i<menu_depth; i++) {
        printf("    for menu %d: ", i+1);
        scanf("%d", &item_count[i]);
    }

    for (i=0; i<menu_depth; i++) {
        for (j=0; j<item_count[i]; j++) {
            strcpy(items[i][j], get_item_name(&in));
        }
    }


    initscr();
    start_color();
    keypad(stdscr, true);
    curs_set(0);


    int CENTER_LINE = LINES / 2;
    int CENTER_COL = COLS / 2;
    int menu_width = 14;
    char *heading = "Combo Menu";

    mvaddstr(3, CENTER_COL - 4, heading);
    move(10, 0);

    while (true) {

        clrtobot();

        for (i=0; i<=current_menu; i++) {
            if (i != current_menu) {
                draw_list(items[i], item_count[i], item_hover[i], 10, i * menu_width + 10);
            } else {
                draw_active_list(items[i], item_count[i], item_hover[i], 10, i * menu_width + 10);
            }
        }

        refresh();

        key = getch();
        switch (key) {
            case 27: // ESC
                endwin();
                exit(0);
                break;
            case 'q':
                endwin();
                exit(0);
            case 259: // Up Key
                if (item_hover[current_menu] > 1)
                    item_hover[current_menu]--;
                break;
            case 258: // Down Key
                if (item_hover[current_menu] < item_count[current_menu])
                    item_hover[current_menu]++;
                break;
            case 260: // Left Key
                if (current_menu > 0)
                    current_menu--;
                break;
            case 261: // Right Key
                if (current_menu < menu_depth-1 && current_menu < opened_menu)
                    current_menu++;
                break;
            case '\n': // Enter
                if (opened_menu == current_menu && opened_menu != menu_depth-1) {
                    if (item_hover[current_menu] > 0) {
                        opened_menu++;
                        current_menu++;
                    }
                }
                break;
        }
    }

    endwin();
    return 0;
}

Comments