From: Daniel Date: Sat, 3 Apr 2021 23:26:01 +0000 (-0400) Subject: changes in popup menu is now preserved X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=8dc9b3f9852c4f6d38d82100a5a3bf95a2e26fa2;p=taskasaur.git changes in popup menu is now preserved --- diff --git a/headers/menu.h b/headers/menu.h index 8c5cdbb..203fe1c 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -31,6 +31,7 @@ extern Menu* create_menu(char* menu_name, MenuItem** item_list); extern WINDOW* get_menu_win(Menu* menu); extern MenuItem* get_menu_item(Menu* menu, int index); +extern MenuItem** get_menu_itemlist(Menu* menu); extern int get_selected_item(Menu* menu); // rename this to selected_index or sm extern MenuItem* get_selected_menuitem(Menu* menu); extern bool get_menu_focused(Menu* menu); @@ -47,6 +48,7 @@ extern int set_menu_focus(Menu* menu, bool focus); extern int set_menu_userdata(Menu* menu, void* userdata); extern int set_menu_renderitem(Menu* menu, void (*render_item)(Menu*, int, int)); extern int set_menu_itemheight(Menu* menu, int (*item_height)(MenuItem*)); +extern int set_menuitem_title(MenuItem* menuitem, char* title); extern int set_menuitem_descrip(MenuItem* menuitem, char* descrip); extern int set_menuitem_userdata(MenuItem* menuitem, void* userdata); diff --git a/headers/render.h b/headers/render.h index 165f74b..58aa6db 100644 --- a/headers/render.h +++ b/headers/render.h @@ -55,8 +55,9 @@ extern int update_menuitem_descrip(MenuItem* menuitem); /* popup menu for menu item */ extern PopupMenu* make_popupmenu(TodoItem* itemdata); extern WINDOW* create_popup_win(TodoItem* item_info); -extern void save_popupmenu_state(Menu* popupmenu_menu); +extern void close_popupmenu(Menu* popupmenu_menu); extern MenuItem** subtasklist_to_menuitem(SubTask** subtask_list, int list_length); +extern SubTask** menuitem_to_subtasklist(MenuItem** menuitem_list, int list_length); /* some helpers */ extern int ungetstr(char* str); diff --git a/menu.c b/menu.c index a4a1639..816b5dd 100644 --- a/menu.c +++ b/menu.c @@ -114,6 +114,12 @@ get_menu_item(Menu* menu, int index) return menu->menu_items[index]; } +MenuItem** +get_menu_itemlist(Menu* menu) +{ + return menu->menu_items; +} + int get_selected_item(Menu* menu) { @@ -219,6 +225,13 @@ set_menu_itemheight(Menu* menu, int (*item_height)(MenuItem*)) return 0; } +int +set_menuitem_title(MenuItem* menuitem, char* title) +{ + menuitem->title = title; + return 0; +} + int set_menuitem_descrip(MenuItem* menuitem, char* descrip) { diff --git a/render.c b/render.c index 7187ec2..df82ce2 100644 --- a/render.c +++ b/render.c @@ -359,6 +359,7 @@ make_popupmenu(TodoItem* itemdata) ); set_menu_win(popupmenu_menu, popupmenu_menu_win); + set_menu_userdata(popupmenu_menu, itemdata); set_menu_focus(popupmenu_menu, 1); set_menu_renderitem(popupmenu_menu, render_popup_menuitem); set_menu_itemheight(popupmenu_menu, popup_menuitem_height); @@ -395,8 +396,10 @@ render_popup_menuitem(Menu* menu, int item_index, int start_y) wprintw( menu_win, (((SubTask*)get_menuitem_userdata(curitem))->done == SubTaskState_done) ? "[X] " : "[ ] " + /* (((TodoItem*)get_menu_userdata(menu))->subtask_list[item_index]->done == SubTaskState_done) ? "[X] " : "[ ] " */ ); wprintw(menu_win, get_menuitem_title(curitem)); + /* wprintw(menu_win, ((TodoItem*)get_menu_userdata(menu))->subtask_list[item_index]->subtask_name); */ wattroff(menu_win, hlcolor); } @@ -411,13 +414,29 @@ popup_menuitem_height(MenuItem* menuitem) /* sorta duct tape rn */ /* find a way to tie TodoItem and MenuItem together better in the future */ void -save_popupmenu_state(Menu* popupmenu_menu) +close_popupmenu(Menu* popupmenu_menu) { - /* TodoItem* item_data; */ + TodoItem* item_data; + SubTask** updated_subtasklist; - /* item_data = get_menu_userdata(popupmenu_menu); */ - /* item_data->subtask_count = array_length(SubTask*, item_data->subtask_list); */ + item_data = (TodoItem*)get_menu_userdata(popupmenu_menu); + updated_subtasklist = menuitem_to_subtasklist( + get_menu_itemlist(popupmenu_menu), + get_menu_length(popupmenu_menu) + ); + /* remember to free old subtask list */ + item_data->subtask_list = updated_subtasklist; + item_data->subtask_count = get_menu_length(popupmenu_menu); + + /* this is messy but since subtask rendering uses menuitem title instead + * of userdata subtask title, this needs to be done :( */ + /* for (int i = 0; i < get_menu_length(popupmenu_menu); i++) { */ + /* set_menuitem_title( */ + /* get_menu_item(popupmenu_menu, i), */ + /* strdup(item_data->subtask_list[i]->subtask_name) */ + /* ); */ + /* } */ } /* this is copy paste of other, prob abstract */ @@ -440,6 +459,32 @@ subtasklist_to_menuitem(SubTask** subtask_list, int list_length) return items; } +SubTask** +menuitem_to_subtasklist(MenuItem** menuitem_list, int list_length) +{ + SubTask** new_subtasklist; + + new_subtasklist = malloc(list_length*sizeof(SubTask*)); + + for (int i = 0; i < list_length; i++) { + SubTask* cur_subtask; + SubTask* new_subtask; + + cur_subtask = (SubTask*)get_menuitem_userdata(menuitem_list[i]); + new_subtask = malloc(sizeof(SubTask)); + + new_subtask->subtask_name = strdup(cur_subtask->subtask_name); + new_subtask->done = cur_subtask->done; + + new_subtasklist[i] = new_subtask; + } + + /* new_subtasklist[list_length] = 0; */ + + return new_subtasklist; +} + + /* helpers */ int ungetstr(char* str) diff --git a/taskasaur.c b/taskasaur.c index 8c44d26..c7c224f 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -192,6 +192,7 @@ popup_handleinput(BoardMenu* boardmenu, int ch) case BINDING_QUIT: boardmenu->popup_open = 0; + close_popupmenu(popupmenu_menu); /* reset screen */ clear(); @@ -202,7 +203,6 @@ popup_handleinput(BoardMenu* boardmenu, int ch) { SubTask* curitem_data = (SubTask*)get_menuitem_userdata(get_menu_item(popupmenu_menu, get_selected_item(popupmenu_menu))); curitem_data->done = (curitem_data->done == SubTaskState_todo) ? SubTaskState_done : SubTaskState_todo; - /* save_popupmenu_state(popupmenu_menu); */ } break;