From: Daniel Liu Date: Thu, 4 Feb 2021 05:04:09 +0000 (-0500) Subject: multiple menus X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=7dd95a0ec56529992866e603dcfaf395614d5bf2;p=taskasaur.git multiple menus --- diff --git a/config.h b/config.h index d805395..88b50d6 100644 --- a/config.h +++ b/config.h @@ -28,6 +28,8 @@ static int menu_non_selected_color = COLOR_GREEN; #define BINDING_QUIT 'q' #define BINDING_SCROLL_UP 'k' #define BINDING_SCROLL_DOWN 'j' +#define BINDING_SCROLL_LEFT 'h' +#define BINDING_SCROLL_RIGHT 'l' #define BINDING_JUMP_TOP 'g' #define BINDING_JUMP_BOTTOM 'G' #define BINDING_MOVE_ITEM_UP 'K' diff --git a/taskasaur.c b/taskasaur.c index 22e2f2a..03c3ab4 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -5,69 +5,119 @@ #include "headers/utils.h" #include "config.h" +typedef struct BoardMenu { + Menu** menu_list; + int menu_count; + int selected; +} BoardMenu; + +BoardMenu* create_board_menu(Board* board); +int set_selected_menu(BoardMenu* boardmenu, int index); + MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); +Menu** make_menus(Board* board, int todolist_length); + int main(int argc, char** argv) { - /* read from todo file */ Board* board; board = begin_parse("test_board.md"); - log_todo(board); + /* log_todo(board); */ /* init curses */ init_tscurses(); - TodoList* todo_list = board->todolist_list[0]; - MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count); - Menu* menu = create_menu(todo_list->list_name, item_list); - WINDOW* win = newwin(20, 40, 5, 5); - set_menu_win(menu, win); - set_menu_focus(menu, true); - box(win, 0, 0); - refresh(); - wrefresh(win); + BoardMenu* boardmenu; + boardmenu = create_board_menu(board); - render_menu(menu); + // this is temp + for (int i = 0; i < boardmenu->menu_count; i++) { + render_menu(boardmenu->menu_list[i]); + } char ch; while ((ch = getch()) != BINDING_QUIT) { + Menu* active_menu; + active_menu = boardmenu->menu_list[boardmenu->selected]; + switch (ch) { case BINDING_SCROLL_UP: - menu_driver(menu, MENU_UP); + menu_driver(active_menu, MENU_UP); break; case BINDING_SCROLL_DOWN: - menu_driver(menu, MENU_DOWN); + menu_driver(active_menu, MENU_DOWN); + break; + case BINDING_SCROLL_LEFT: + if (boardmenu->selected-1 < 0) break; + set_selected_menu(boardmenu, boardmenu->selected-1); + break; + case BINDING_SCROLL_RIGHT: + if (boardmenu->selected+1 > boardmenu->menu_count-1) break; + set_selected_menu(boardmenu, boardmenu->selected+1); break; case BINDING_JUMP_TOP: - menu_driver(menu, MENU_TOP); + menu_driver(active_menu, MENU_TOP); break; case BINDING_JUMP_BOTTOM: - menu_driver(menu, MENU_BOTTOM); + menu_driver(active_menu, MENU_BOTTOM); break; case BINDING_MOVE_ITEM_UP: - menu_driver(menu, MENU_MOVE_UP); + menu_driver(active_menu, MENU_MOVE_UP); break; case BINDING_MOVE_ITEM_DOWN: - menu_driver(menu, MENU_MOVE_DOWN); + menu_driver(active_menu, MENU_MOVE_DOWN); break; case BINDING_DELETE_ITEM: - menu_driver(menu, MENU_DELETE); + menu_driver(active_menu, MENU_DELETE); break; } - render_menu(menu); - } + for (int i = 0; i < boardmenu->menu_count; i++) { + render_menu(boardmenu->menu_list[i]); + } + } exit_tscurses(); return 0; } +BoardMenu* +create_board_menu(Board* board) +{ + BoardMenu* new_boardmenu; + + new_boardmenu = malloc(sizeof(BoardMenu)); + + new_boardmenu->menu_list = make_menus(board, board->todolist_count); + new_boardmenu->menu_count = board->todolist_count; + new_boardmenu->selected = 0; + + return new_boardmenu; +} + +int +set_selected_menu(BoardMenu* boardmenu, int index) +{ + Menu* old_menu; + Menu* new_menu; + + old_menu = boardmenu->menu_list[boardmenu->selected]; + new_menu = boardmenu->menu_list[index]; + + set_menu_focus(old_menu, false); + set_menu_focus(new_menu, true); + + boardmenu->selected = index; + + return 0; +} + MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length) { @@ -82,5 +132,38 @@ todolist_to_menuitem(TodoItem** item_list, int list_length) return items; } +Menu** +make_menus(Board* board, int todolist_length) +{ + // this is temp + #define MENU_WIDTH 40 + + Menu** menu_list; + + menu_list = malloc(todolist_length*sizeof(Menu*)); + for (int i = 0; i < todolist_length; i++) { + /* read from parsed */ + TodoList* todo_list = board->todolist_list[i]; + MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count); + + Menu* new_menu = create_menu(todo_list->list_name, item_list); + + /* make window */ + WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i); + box(win, 0, 0); + + /* some menu settings */ + set_menu_win(new_menu, win); + set_menu_focus(new_menu, i == 0); // make first win focused + + /* refresh */ + refresh(); + wrefresh(win); + + menu_list[i] = new_menu; + } + + return menu_list; +}