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