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