abstract menu driver a bit
[taskasaur.git] / render.c
1
2 #include "headers/render.h"
3 #include <string.h>
4 #include "config.h"
5
6 #define POPUP_MENU_PAD_TOP 2
7 #define POPUP_MENU_PAD_BOTTOM 2
8 #define POPUP_MENU_PAD_LEFT 2
9 #define POPUP_MENU_PAD_RIGHT 1
10
11 int init_tscolors(void);
12 int create_todowin(void);
13
14 /* init stuff */
15 int
16 init_tscurses(void)
17 {
18     initscr();
19     cbreak();
20     curs_off();
21     keypad(stdscr, TRUE);
22
23     /* need to error check this */
24     if (has_colors() == FALSE) {
25         fprintf(stderr, "Your terminal does not support color.\n");
26
27         /* maybe just return 1 */
28         /* exit_tscurses(); */
29         /* exit(1); */
30         return 1;
31     }
32     start_color();
33     init_tscolors();
34
35     return 0;
36 }
37
38 int
39 exit_tscurses(void)
40 {
41     endwin();
42
43     return 0;
44 }
45
46 int
47 init_tscolors(void)
48 {
49     init_pair(TS_SELECTED, selected_color, COLOR_BLACK);
50     init_pair(TS_NONSELECTED, non_selected_color, COLOR_BLACK);
51     init_pair(TS_MENU_SELECTED, menu_selected_color, COLOR_BLACK);
52     init_pair(TS_MENU_NONSELECTED, menu_non_selected_color, COLOR_BLACK);
53     init_pair(TS_ITEMCOUNT, item_count_color, COLOR_BLACK);
54
55     return 0;
56 }
57
58 /* cursor */
59 int
60 curs_on(void)
61 {
62     echo();
63     curs_set(1);
64     return 0;
65 }
66
67 int
68 curs_off(void)
69 {
70     noecho();
71     curs_set(0);
72     return 0;
73 }
74
75 /* wins */
76 int
77 create_todowin(void)
78 {
79     return 0;
80 }
81
82 /* board menu */
83 BoardMenu*
84 create_board_menu(Board* board)
85 {
86     BoardMenu* new_boardmenu;
87
88     new_boardmenu = malloc(sizeof(BoardMenu));
89
90     new_boardmenu->menu_list = make_menus(board, board->todolist_count);
91     new_boardmenu->menu_count = board->todolist_count;
92     new_boardmenu->selected = 0;
93     new_boardmenu->popupmenu = NULL;
94     new_boardmenu->popup_open = 0;
95
96     return new_boardmenu;
97 }
98
99 Menu**
100 make_menus(Board* board, int todolist_length)
101 {
102
103     Menu** menu_list;
104
105     menu_list = malloc(todolist_length*sizeof(Menu*));
106
107     for (int i = 0; i < todolist_length; i++) {
108
109         /* read from parsed */
110         TodoList* todo_list = board->todolist_list[i];
111         MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
112
113         Menu* new_menu = create_menu(todo_list->list_name, item_list);
114
115         /* make window */
116         WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
117         box(win, 0, 0);
118
119         /* some menu settings */
120         set_menu_win(new_menu, win);
121         set_menu_focus(new_menu, i == 0); // make first win focused
122
123         /* refresh */
124         refresh();
125         wrefresh(win);
126
127         menu_list[i] = new_menu;
128     }
129
130     return menu_list;
131 }
132
133 MenuItem** 
134 todolist_to_menuitem(TodoItem** item_list, int list_length)
135 {
136     MenuItem** items;
137
138     items = malloc((list_length+1)*sizeof(MenuItem*));
139     for (int i = 0; i < list_length; i++) {
140         MenuItem* new_menuitem;
141         new_menuitem =  create_menuitem(item_list[i]->item_name);
142         /* using same struct, careful if it gets freed */
143         set_menuitem_userdata(new_menuitem, item_list[i]);
144
145         items[i] = new_menuitem;
146     }
147
148     items[list_length] = 0; //null terminate
149     return items;
150 }
151
152 Board*
153 boardmenu_to_board(BoardMenu* boardmenu)
154 { // STRINGS are sharing the same address as the one in MENU
155   // and MENUITEM, this may break something if u free this board
156   // consider copying the string
157
158     Board* newboard = malloc(sizeof(Board));
159     TodoList** new_todolist_list = malloc(sizeof(TodoList*));
160
161     for (int i = 0; i < boardmenu->menu_count; i++) {
162         Menu* curmenu = boardmenu->menu_list[i];
163
164         TodoList* new_todolist = malloc(sizeof(TodoList));
165         TodoItem** new_item_list = malloc(sizeof(TodoItem*));
166         new_todolist->list_name = strdup(get_menu_name(curmenu));
167         new_todolist->item_count = get_menu_length(curmenu);
168
169         for (int j = 0; j < get_menu_length(curmenu); j++) {
170             MenuItem* curmenuitem = get_menu_item(curmenu, j);
171             TodoItem* itemdata = get_menuitem_userdata(curmenuitem);
172
173             TodoItem* new_todoitem = malloc(sizeof(TodoItem));
174             SubTask** new_subtask_list = malloc(itemdata->subtask_count*sizeof(SubTask*));
175
176             new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
177             new_todoitem->description = strdup(itemdata->description);
178             new_todoitem->due = strdup(itemdata->due);
179             new_todoitem->subtask_count = itemdata->subtask_count;
180             for (int k = 0; k < itemdata->subtask_count; k++) {
181                 SubTask* new_subtask = malloc(sizeof(SubTask));
182
183                 new_subtask->subtask_name = itemdata->subtask_list[k]->subtask_name;
184                 new_subtask->done = itemdata->subtask_list[k]->done;
185
186                 new_subtask_list[k] = new_subtask;
187             }
188
189             new_todoitem->subtask_list = new_subtask_list;
190
191             new_item_list[j] = new_todoitem;
192         }
193
194         new_todolist->item_list = new_item_list;
195         new_todolist_list[i] = new_todolist;
196
197     }
198
199     newboard->board_name = strdup("");
200     newboard->todolist_list = new_todolist_list;
201     newboard->todolist_count = boardmenu->menu_count;
202
203     return newboard;
204 }
205
206 int
207 render_board(Board* board)
208 {
209
210     return 0;
211 }
212
213 int
214 set_selected_menu(BoardMenu* boardmenu, int index)
215 {
216     Menu* old_menu;
217     Menu* new_menu;
218     int new_pos;
219     
220     old_menu = boardmenu->menu_list[boardmenu->selected];
221     new_menu = boardmenu->menu_list[index];
222
223     set_menu_focus(old_menu, false);
224     set_menu_focus(new_menu, true);
225
226     /* also try to jump to a similar position if possible */
227     /* rn theres a bug if old menu is empty */
228     new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
229     if (new_pos < 0) new_pos = 0;
230     set_selected_item(new_menu, new_pos);
231
232     boardmenu->selected = index;
233
234     return 0;
235 }
236
237 int
238 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
239 {
240     /* reposition menus */
241     mvwin(get_menu_win(boardmenu->menu_list[src_index]),
242         1, 1+MENU_WIDTH*dest_index
243     );
244     mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
245         1, 1+MENU_WIDTH*src_index
246     );
247     refresh();
248     wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
249     wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
250     /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
251     /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
252     /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
253     /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
254     clear();
255
256     /* swap in array */
257     ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
258
259     return 0;
260 }
261
262 /* menuitem */
263 int
264 update_menuitem_descrip(MenuItem* menuitem)
265 { /* need to do something about colored text */
266
267     TodoItem* item_data;    
268     char* new_descrip;
269
270     item_data = (TodoItem*)get_menuitem_userdata(menuitem);
271     new_descrip = strdup("");
272
273     if (strlen(item_data->description) > 0) {
274         /* strcat(new_descrip, "☰ "); */
275         strcat(new_descrip, "# ");
276     }
277     if (strlen(item_data->due) > 0) {
278         strcat(new_descrip, item_data->due);
279         strcat(new_descrip, " ");
280     }
281     if (item_data->subtask_count > 0) {
282
283         int tasks_complete = 0;
284         for (int i = 0; i < item_data->subtask_count; i++) {
285             if (item_data->subtask_list[i]->done == SubTaskState_done) {
286                 tasks_complete += 1;
287             }
288         }
289
290         /* [, # done, /, # total, ], null */
291         char subtask_done[4]; // assume there wont be more than 999 subtasks (possibly danger?)
292         snprintf(subtask_done, 4, "%d", tasks_complete);
293         int substask_len = 1+item_data->subtask_count+1+strlen(subtask_done)+1+1;
294         char subtask_text[substask_len];
295         sprintf(subtask_text, "[%s/%d]", subtask_done, item_data->subtask_count);
296         strcat(new_descrip, subtask_text);
297     }
298
299     /* free old string */
300     if (strlen(new_descrip) > 0) {
301         free(get_menuitem_descrip(menuitem));
302         set_menuitem_descrip(menuitem, new_descrip); 
303     }
304
305     return 0;
306 }
307
308 /* popup */
309 PopupMenu*
310 make_popupmenu(TodoItem* itemdata)
311 {
312     PopupMenu* new_popupmenu;
313     MenuItem** subtask_menuitems;
314     Menu* popupmenu_menu;
315     WINDOW* popupmenu_win;
316     WINDOW* popupmenu_menu_win;
317
318     new_popupmenu = malloc(sizeof(PopupMenu));
319
320     subtask_menuitems = subtasklist_to_menuitem(itemdata->subtask_list, itemdata->subtask_count);
321     popupmenu_menu = create_menu(strdup(""), subtask_menuitems);
322
323     /* popup win */
324     int maxheight, maxwidth;
325     getmaxyx(stdscr, maxheight, maxwidth);
326     popupmenu_win = newwin(
327         maxheight-2*POPUP_BORDER,
328         maxwidth-2*2*POPUP_BORDER,
329         POPUP_BORDER,
330         POPUP_BORDER*2
331     );
332
333     int popup_maxheight, popup_maxwidth;
334     getmaxyx(popupmenu_win, popup_maxheight, popup_maxwidth);
335     popupmenu_menu_win = derwin(
336         popupmenu_win,
337         popup_maxheight-POPUP_MENU_PAD_TOP-POPUP_MENU_PAD_BOTTOM,
338         popup_maxwidth-POPUP_MENU_PAD_LEFT-POPUP_MENU_PAD_RIGHT,
339         POPUP_MENU_PAD_TOP,
340         POPUP_MENU_PAD_LEFT
341     );
342
343     set_menu_win(popupmenu_menu, popupmenu_menu_win);
344     set_menu_focus(popupmenu_menu, 1);
345     box(popupmenu_win, 0, 0);
346
347     /* move this stuff to render phase later? */
348     mvwprintw(popupmenu_win, 1, 2, itemdata->item_name);
349
350     /* don't forget to free popupmenu after */
351     new_popupmenu->win = popupmenu_win;
352     new_popupmenu->menu = popupmenu_menu;
353
354     return new_popupmenu;
355 }
356
357 int
358 render_popupmenu(PopupMenu* popupmenu)
359 {
360     render_menu(popupmenu->menu);
361     wrefresh(popupmenu->win);
362
363     return 0;
364 }
365
366 /* this is copy paste of other, prob abstract */
367 MenuItem**
368 subtasklist_to_menuitem(SubTask** subtask_list, int list_length)
369 {
370     MenuItem** items;
371
372     items = malloc((list_length+1)*sizeof(MenuItem*));
373     for (int i = 0; i < list_length; i++) {
374         MenuItem* new_menuitem;
375         new_menuitem =  create_menuitem(subtask_list[i]->subtask_name);
376         /* using same struct, careful if it gets freed */
377         set_menuitem_userdata(new_menuitem, subtask_list[i]);
378
379         items[i] = new_menuitem;
380     }
381
382     items[list_length] = 0; //null terminate
383     return items;
384 }
385
386 /* helpers */
387 int
388 ungetstr(char* str)
389 {
390     // ignore null character (it's fine even if strlen = 0)
391     for (int i = strlen(str)-1; i >= 0; i--) {
392         ungetch(str[i]);
393     }
394
395     return 0; 
396 }
397