From: Daniel Date: Wed, 31 Mar 2021 21:02:14 +0000 (-0400) Subject: callbacks for rendering X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=f06db8e7f1fd2769ad824d76ce5c047189da2a48;p=taskasaur.git callbacks for rendering --- diff --git a/config.h b/config.h index 61f5bf0..d8a1664 100644 --- a/config.h +++ b/config.h @@ -48,6 +48,7 @@ static int due_urgent_color = COLOR_RED; #define BINDING_MOVE_MENU_RIGHT '>' #define BINDING_EDIT_ITEM 'i' #define BINDING_SELECT '\n' +#define BINDING_TOGGLE_DONE ' ' #define BINDING_WRITE 'w' #endif diff --git a/headers/menu.h b/headers/menu.h index 9afda2e..63a410f 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -43,6 +43,8 @@ extern int set_menu_win(Menu* menu, WINDOW* win); extern int set_selected_item(Menu* menu, int selected_item); 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_descrip(MenuItem* menuitem, char* descrip); extern int set_menuitem_userdata(MenuItem* menuitem, void* userdata); diff --git a/menu.c b/menu.c index 374ccaf..da064ba 100644 --- a/menu.c +++ b/menu.c @@ -33,6 +33,8 @@ typedef struct Menu { int max_height; int max_width; void* userdata; + void (*render_item)(Menu*, int, int); + int (*item_height)(MenuItem*); } Menu; int swap_item(Menu* menu, int src_index, int dest_index); @@ -44,8 +46,8 @@ int menu_insert_mode(Menu* menu, int insert_index); MenuItem* create_blank_menuitem(void); /* rendering stuff */ -int render_item(Menu* menu, int item_index, int start_y); -int item_height(MenuItem* menuitem); +void default_render_item(Menu* menu, int item_index, int start_y); +int default_item_height(MenuItem* menuitem); int items_visible(Menu* menu, int offset); int items_visible_rev(Menu* menu, int offset); @@ -89,6 +91,9 @@ create_menu(char* menu_name, MenuItem** item_list) new_menu->selected_item = 0; new_menu->scroll_offset = 0; new_menu->focused = false; + new_menu->userdata = NULL; + new_menu->render_item = *default_render_item; + new_menu->item_height = *default_item_height; set_menu_win(new_menu, stdscr); return new_menu; @@ -188,6 +193,20 @@ set_menu_userdata(Menu* menu, void* userdata) return 0; } +int +set_menu_renderitem(Menu* menu, void (*render_item)(Menu*, int, int)) +{ + menu->render_item = render_item; + return 0; +} + +int +set_menu_itemheight(Menu* menu, int (*item_height)(MenuItem*)) +{ + menu->item_height = item_height; + return 0; +} + int set_menuitem_descrip(MenuItem* menuitem, char* descrip) { @@ -266,7 +285,7 @@ menu_insert_mode(Menu* menu, int insert_index) // account for multiline items insert_pos = menu->scroll_offset; for (int i = 0; i < insert_index; i++) { - insert_pos += item_height(menu->menu_items[i]); + insert_pos += menu->item_height(menu->menu_items[i]); } /* move cursor to right spot */ @@ -385,7 +404,8 @@ render_menu(Menu* menu) /* render menu items */ int curline = 0; for (int i = menu->scroll_offset; i < menu->menu_length; i++) { - curline += render_item(menu, i, curline); + menu->render_item(menu, i, curline); + curline += menu->item_height(menu->menu_items[i]); } wrefresh(menu->menu_win); @@ -393,8 +413,8 @@ render_menu(Menu* menu) return 0; } -int -render_item(Menu* menu, int item_index, int start_y) +void +default_render_item(Menu* menu, int item_index, int start_y) { MenuItem* curitem; int hlcolor; @@ -406,28 +426,49 @@ render_item(Menu* menu, int item_index, int start_y) mvwprintw(menu->menu_win, start_y, 0, curitem->title); wattroff(menu->menu_win, hlcolor); - /* display number of items */ - if (strlen(curitem->description) > 0) { - wattron(menu->menu_win, COLOR_PAIR(TS_ITEMCOUNT)); - mvwprintw(menu->menu_win, start_y+1, 0, curitem->description); - wattroff(menu->menu_win, COLOR_PAIR(TS_ITEMCOUNT)); - } - - return item_height(curitem); } +/* int */ +/* default_render_item(Menu* menu, int item_index, int start_y) */ +/* { */ +/* MenuItem* curitem; */ +/* int hlcolor; */ +/* curitem = menu->menu_items[item_index]; */ + +/* /1* color selected item *1/ */ +/* hlcolor = COLOR_PAIR((item_index == menu->selected_item && menu->focused == true) ? TS_SELECTED : TS_NONSELECTED); */ +/* wattron(menu->menu_win, hlcolor); */ +/* mvwprintw(menu->menu_win, start_y, 0, curitem->title); */ +/* wattroff(menu->menu_win, hlcolor); */ + +/* /1* display number of items *1/ */ +/* if (strlen(curitem->description) > 0) { */ +/* wattron(menu->menu_win, COLOR_PAIR(TS_ITEMCOUNT)); */ +/* mvwprintw(menu->menu_win, start_y+1, 0, curitem->description); */ +/* wattroff(menu->menu_win, COLOR_PAIR(TS_ITEMCOUNT)); */ +/* } */ + +/* return item_height(curitem); */ +/* } */ + int -item_height(MenuItem* menuitem) -{ - int lines; +default_item_height(MenuItem* menuitem) +{ + return 1; // maybe account for line wrap by default +} - lines = 1; - if (strlen(menuitem->description) > 0) { - lines += 1; - } +/* int */ +/* item_height(MenuItem* menuitem) */ +/* { */ +/* int lines; */ - return lines; -} +/* lines = 1; */ +/* if (strlen(menuitem->description) > 0) { */ +/* lines += 1; */ +/* } */ + +/* return lines; */ +/* } */ int items_visible(Menu* menu, int offset) @@ -441,7 +482,7 @@ items_visible(Menu* menu, int offset) int lines = 0; for (int i = offset ; i < menu->menu_length; i++) { - lines += item_height(menu->menu_items[i]); + lines += menu->item_height(menu->menu_items[i]); if (lines > maxheight) break; vis += 1; @@ -462,7 +503,7 @@ items_visible_rev(Menu* menu, int offset) int lines = 0; for (int i = offset; i > 0; i--) { - lines += item_height(menu->menu_items[i]); + lines += menu->item_height(menu->menu_items[i]); if (lines > maxheight) break; vis +=1; diff --git a/render.c b/render.c index 71d675e..0c0270e 100644 --- a/render.c +++ b/render.c @@ -3,7 +3,7 @@ #include #include "config.h" -#define POPUP_MENU_PAD_TOP 2 +#define POPUP_MENU_PAD_TOP 4 #define POPUP_MENU_PAD_BOTTOM 2 #define POPUP_MENU_PAD_LEFT 2 #define POPUP_MENU_PAD_RIGHT 1 @@ -346,6 +346,7 @@ make_popupmenu(TodoItem* itemdata) /* move this stuff to render phase later? */ mvwprintw(popupmenu_win, 1, 2, itemdata->item_name); + mvwprintw(popupmenu_win, 2, 2, (strlen(itemdata->description) > 0) ? itemdata->description : "no description"); /* don't forget to free popupmenu after */ new_popupmenu->win = popupmenu_win; diff --git a/taskasaur.c b/taskasaur.c index 100430e..d091e55 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -201,6 +201,10 @@ popup_handleinput(BoardMenu* boardmenu, int ch) clear(); break; + case BINDING_TOGGLE_DONE: + + break; + default: generic_handleinput(popupmenu_menu, ch); }