2 #include "headers/render.h"
6 int init_tscolors(void);
7 int create_todowin(void);
18 /* need to error check this */
19 if (has_colors() == FALSE) {
20 fprintf(stderr, "Your terminal does not support color.\n");
22 /* maybe just return 1 */
23 /* exit_tscurses(); */
44 init_pair(TS_SELECTED, selected_color, COLOR_BLACK);
45 init_pair(TS_NONSELECTED, non_selected_color, COLOR_BLACK);
46 init_pair(TS_MENU_SELECTED, menu_selected_color, COLOR_BLACK);
47 init_pair(TS_MENU_NONSELECTED, menu_non_selected_color, COLOR_BLACK);
78 create_board_menu(Board* board)
80 BoardMenu* new_boardmenu;
82 new_boardmenu = malloc(sizeof(BoardMenu));
84 new_boardmenu->menu_list = make_menus(board, board->todolist_count);
85 new_boardmenu->menu_count = board->todolist_count;
86 new_boardmenu->selected = 0;
92 make_menus(Board* board, int todolist_length)
97 menu_list = malloc(todolist_length*sizeof(Menu*));
99 for (int i = 0; i < todolist_length; i++) {
101 /* read from parsed */
102 TodoList* todo_list = board->todolist_list[i];
103 MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
105 Menu* new_menu = create_menu(todo_list->list_name, item_list);
108 WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
111 /* some menu settings */
112 set_menu_win(new_menu, win);
113 set_menu_focus(new_menu, i == 0); // make first win focused
119 menu_list[i] = new_menu;
126 todolist_to_menuitem(TodoItem** item_list, int list_length)
130 items = malloc((list_length+1)*sizeof(MenuItem*));
131 for (int i = 0; i < list_length; i++) {
132 MenuItem* new_menuitem;
133 new_menuitem = create_menuitem(item_list[i]->item_name);
134 /* using same struct, careful if it gets freed */
135 set_menuitem_userdata(new_menuitem, item_list[i]);
137 items[i] = new_menuitem;
140 items[list_length] = 0; //null terminate
145 boardmenu_to_board(BoardMenu* boardmenu)
146 { // STRINGS are sharing the same address as the one in MENU
147 // and MENUITEM, this may break something if u free this board
148 // consider copying the string
150 Board* newboard = malloc(sizeof(Board));
151 TodoList** new_todolist_list = malloc(sizeof(TodoList*));
153 for (int i = 0; i < boardmenu->menu_count; i++) {
154 Menu* curmenu = boardmenu->menu_list[i];
156 TodoList* new_todolist = malloc(sizeof(TodoList));
157 TodoItem** new_item_list = malloc(sizeof(TodoItem*));
158 new_todolist->list_name = strdup(get_menu_name(curmenu));
159 new_todolist->item_count = get_menu_length(curmenu);
161 for (int j = 0; j < get_menu_length(curmenu); j++) {
162 MenuItem* curmenuitem = get_menu_item(curmenu, j);
164 TodoItem* new_todoitem = malloc(sizeof(TodoItem));
166 new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
167 new_todoitem->description = strdup(get_menuitem_descrip(curmenuitem));
168 new_todoitem->due = strdup(""); //TEMP!
169 new_todoitem->subtask_list = malloc(0); //TEMP!
170 new_todoitem->subtask_count = 0; //TEMP!
172 new_item_list[j] = new_todoitem;
175 new_todolist->item_list = new_item_list;
176 new_todolist_list[i] = new_todolist;
180 newboard->board_name = strdup("");
181 newboard->todolist_list = new_todolist_list;
182 newboard->todolist_count = boardmenu->menu_count;
188 set_selected_menu(BoardMenu* boardmenu, int index)
194 old_menu = boardmenu->menu_list[boardmenu->selected];
195 new_menu = boardmenu->menu_list[index];
197 set_menu_focus(old_menu, false);
198 set_menu_focus(new_menu, true);
200 /* also try to jump to a similar position if possible */
201 /* rn theres a bug if old menu is empty */
202 new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
203 set_selected_item(new_menu, new_pos);
205 boardmenu->selected = index;
211 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
213 /* reposition menus */
214 mvwin(get_menu_win(boardmenu->menu_list[src_index]),
215 1, 1+MENU_WIDTH*dest_index
217 mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
218 1, 1+MENU_WIDTH*src_index
221 wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
222 wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
223 /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
224 /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
225 /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
226 /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
230 ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
237 update_menuitem_descrip(MenuItem* menuitem)
238 { /* need to do something about colored text */
243 item_data = (TodoItem*)get_menuitem_userdata(menuitem);
244 new_descrip = strdup("");
246 if (strlen(item_data->description) > 0) {
247 /* strcat(new_descrip, "☰ "); */
248 strcat(new_descrip, "~ ");
250 if (strlen(item_data->due) > 0) {
251 strcat(new_descrip, item_data->due);
252 strcat(new_descrip, " ");
254 if (item_data->subtask_count > 0) {
255 /* [, # done, /, # total, ], null */
256 int substask_len = 1+item_data->subtask_count+1+ 1+1;
257 char subtask_text[substask_len];
258 sprintf(subtask_text, "[%d/]", item_data->subtask_count);
259 strcat(new_descrip, subtask_text);
262 /* free old string */
263 if (strlen(new_descrip) > 0) {
264 free(get_menuitem_descrip(menuitem));
265 set_menuitem_descrip(menuitem, new_descrip);
282 // ignore null character (it's fine even if strlen = 0)
283 for (int i = strlen(str)-1; i >= 0; i--) {