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