From: Daniel Liu Date: Mon, 14 Dec 2020 05:42:32 +0000 (-0500) Subject: menu in subwin X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=commitdiff_plain;h=f270512184ab1c41eee46c76a149b7cbddb1b131 menu in subwin --- diff --git a/menutest.c b/menutest.c deleted file mode 100644 index f8c3dac..0000000 --- a/menutest.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include - -WINDOW* create_list_win(int height, int width, int y, int x); - -int -main(int argc, char** argv) -{ - int height, width; - int ch; - - WINDOW* todo_win; - char* todos[] = {"eat bread", "eat milk", "eat cheese"}; - int todo_length; - ITEM** todo_items; - MENU* todo_menu; - ITEM* cur_item; - - // start ncurses - initscr(); - cbreak(); - noecho(); - curs_set(0); - start_color(); - - getmaxyx(stdscr, height, width); - refresh(); - - /* init todo menu stuff */ - - todo_win = create_list_win(20, 40, 5, 5); - todo_length = sizeof(todos)/sizeof(todos[0]); - - todo_items = malloc((todo_length+1)*sizeof(ITEM*)); - for (int i = 0; i < todo_length; i++) { - todo_items[i] = new_item(todos[i], ""); - } - todo_items[todo_length] = NULL; // last item needs to be a null pointer for some reason? - - todo_menu = new_menu(todo_items); - post_menu(todo_menu); - - /* todo_menu = new_menu(); */ - - /* for (int i = 0; i < todo_length; i++) { */ - /* mvwprintw(todo_win, i, 0, todos[i]); */ - /* } */ - wrefresh(todo_win); - - while ((ch = getch()) != 113) { // while not q - - // ofc the first thing we need is vim keys - switch (ch) { - case 'j': - menu_driver(todo_menu, REQ_DOWN_ITEM); - break; - case 'k': - menu_driver(todo_menu, REQ_UP_ITEM); - break; - case 'G': // try to implement gg too - menu_driver(todo_menu, REQ_LAST_ITEM); - break; - } - - refresh(); - /* clear(); */ - } - - endwin(); - - return 0; -} - -WINDOW* -create_list_win(int height, int width, int y, int x) -{ - WINDOW* new_win = newwin(height, width, y, x); - wrefresh(new_win); - return new_win; -} diff --git a/taskasaur.c b/taskasaur.c index 83bbab1..119411e 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -11,7 +11,9 @@ void winch_handler(int sig); char** read_todo(FILE* file, int* length); WINDOW* create_win(int height, int width, int y, int x); -MENU* create_todo_menu(char** todo_list, int todo_length); +MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); + +void free_todo(char** todo_list, int todo_length); #include "config.h" @@ -87,13 +89,12 @@ main(int argc, char** argv) start_color(); getmaxyx(stdscr, height, width); - refresh(); - /* todo_win = create_win(20, 40, 5, 5); */ - char* test_todo[] = {"1lmao", "2pee", "3poo", "4feces"}; - /* todo_menu = create_todo_menu(test_todo, 4); */ - todo_menu = create_todo_menu(todos, todo_length); + todo_win = create_win(20, 40, 5, 5); + + todo_menu = create_todo_menu(todo_win, todos, todo_length); post_menu(todo_menu); + wrefresh(todo_win); while ((ch = getch()) != 'q') { @@ -109,12 +110,14 @@ main(int argc, char** argv) break; } + wrefresh(todo_win); } endwin(); /* Free mem */ - free(todos); // prob need to free each string in list too + unpost(todo_menu); + free_todo(todos, todo_length); return 0; } @@ -145,7 +148,6 @@ read_todo(FILE* file, int* length) out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? lineptr[strcspn(lineptr, "\n")] = 0; // remove newline - out_arr[out_len-1] = lineptr; lineptr = NULL; @@ -164,20 +166,36 @@ create_win(int height, int width, int y, int x) } MENU* -create_todo_menu(char** todo_list, int todo_length) +create_todo_menu(WINDOW* win, char** todo_list, int todo_length) { MENU* todo_menu; ITEM** item_list; ITEM* cur_item; + int wheight, wwidth; item_list = malloc((todo_length+1)*sizeof(ITEM*)); for (int i = 0; i < todo_length; i++) { - printf(todo_list[i]); item_list[i] = new_item(todo_list[i], ""); } item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? todo_menu = new_menu(item_list); + getmaxyx(stdscr, wheight, wwidth); + set_menu_win(todo_menu, win); + set_menu_sub(todo_menu, derwin(win, wheight, wwidth, 0, 0)); + + box(win, 0, 0); //temp + return todo_menu; } + +void +free_todo(char** todo_list, int todo_length) +{ + // probably check if list is too short or too long + for (int i = 0; i < todo_length; i++) { + free(todo_list[i]); + } + free(todo_list); +}