lots of fixes
[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
88     return new_boardmenu;
89 }
90
91 Menu**
92 make_menus(Board* board, int todolist_length)
93 {
94
95     Menu** menu_list;
96
97     menu_list = malloc(todolist_length*sizeof(Menu*));
98
99     for (int i = 0; i < todolist_length; i++) {
100
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);
104
105         Menu* new_menu = create_menu(todo_list->list_name, item_list);
106
107         /* make window */
108         WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
109         box(win, 0, 0);
110
111         /* some menu settings */
112         set_menu_win(new_menu, win);
113         set_menu_focus(new_menu, i == 0); // make first win focused
114
115         /* refresh */
116         refresh();
117         wrefresh(win);
118
119         menu_list[i] = new_menu;
120     }
121
122     return menu_list;
123 }
124
125 MenuItem** 
126 todolist_to_menuitem(TodoItem** item_list, int list_length)
127 {
128     MenuItem** items;
129
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]);
136
137         items[i] = new_menuitem;
138     }
139
140     items[list_length] = 0; //null terminate
141     return items;
142 }
143
144 Board*
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
149
150     Board* newboard = malloc(sizeof(Board));
151     TodoList** new_todolist_list = malloc(sizeof(TodoList*));
152
153     for (int i = 0; i < boardmenu->menu_count; i++) {
154         Menu* curmenu = boardmenu->menu_list[i];
155
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);
160
161         for (int j = 0; j < get_menu_length(curmenu); j++) {
162             MenuItem* curmenuitem = get_menu_item(curmenu, j);
163             TodoItem* itemdata = get_menuitem_userdata(curmenuitem);
164
165             TodoItem* new_todoitem = malloc(sizeof(TodoItem));
166             SubTask** new_subtask_list = malloc(itemdata->subtask_count*sizeof(SubTask*));
167
168             new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
169             new_todoitem->description = strdup(itemdata->description);
170             new_todoitem->due = strdup(itemdata->due);
171             new_todoitem->subtask_count = itemdata->subtask_count;
172             for (int k = 0; k < itemdata->subtask_count; k++) {
173                 SubTask* new_subtask = malloc(sizeof(SubTask));
174
175                 new_subtask->subtask_name = itemdata->subtask_list[k]->subtask_name;
176                 new_subtask->done = itemdata->subtask_list[k]->done;
177
178                 new_subtask_list[k] = new_subtask;
179             }
180
181             new_todoitem->subtask_list = new_subtask_list;
182
183             new_item_list[j] = new_todoitem;
184         }
185
186         new_todolist->item_list = new_item_list;
187         new_todolist_list[i] = new_todolist;
188
189     }
190
191     newboard->board_name = strdup("");
192     newboard->todolist_list = new_todolist_list;
193     newboard->todolist_count = boardmenu->menu_count;
194
195     return newboard;
196 }
197
198 int
199 set_selected_menu(BoardMenu* boardmenu, int index)
200 {
201     Menu* old_menu;
202     Menu* new_menu;
203     int new_pos;
204     
205     old_menu = boardmenu->menu_list[boardmenu->selected];
206     new_menu = boardmenu->menu_list[index];
207
208     set_menu_focus(old_menu, false);
209     set_menu_focus(new_menu, true);
210
211     /* also try to jump to a similar position if possible */
212     /* rn theres a bug if old menu is empty */
213     new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
214     if (new_pos < 0) new_pos = 0;
215     set_selected_item(new_menu, new_pos);
216
217     boardmenu->selected = index;
218
219     return 0;
220 }
221
222 int
223 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
224 {
225     /* reposition menus */
226     mvwin(get_menu_win(boardmenu->menu_list[src_index]),
227         1, 1+MENU_WIDTH*dest_index
228     );
229     mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
230         1, 1+MENU_WIDTH*src_index
231     );
232     refresh();
233     wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
234     wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
235     /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
236     /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
237     /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
238     /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
239     clear();
240
241     /* swap in array */
242     ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
243
244     return 0;
245 }
246
247 /* menuitem */
248 int
249 update_menuitem_descrip(MenuItem* menuitem)
250 { /* need to do something about colored text */
251
252     TodoItem* item_data;    
253     char* new_descrip;
254
255     item_data = (TodoItem*)get_menuitem_userdata(menuitem);
256     new_descrip = strdup("");
257
258     if (strlen(item_data->description) > 0) {
259         /* strcat(new_descrip, "☰ "); */
260         strcat(new_descrip, "# ");
261     }
262     if (strlen(item_data->due) > 0) {
263         strcat(new_descrip, item_data->due);
264         strcat(new_descrip, " ");
265     }
266     if (item_data->subtask_count > 0) {
267
268         int tasks_complete = 0;
269         for (int i = 0; i < item_data->subtask_count; i++) {
270             if (item_data->subtask_list[i]->done == SubTaskState_done) {
271                 tasks_complete += 1;
272             }
273         }
274
275         /* [, # done, /, # total, ], null */
276         char subtask_done[4]; // assume there wont be more than 999 subtasks (possibly danger?)
277         snprintf(subtask_done, 4, "%d", tasks_complete);
278         int substask_len = 1+item_data->subtask_count+1+strlen(subtask_done)+1+1;
279         char subtask_text[substask_len];
280         sprintf(subtask_text, "[%s/%d]", subtask_done, item_data->subtask_count);
281         strcat(new_descrip, subtask_text);
282     }
283
284     /* free old string */
285     if (strlen(new_descrip) > 0) {
286         free(get_menuitem_descrip(menuitem));
287         set_menuitem_descrip(menuitem, new_descrip); 
288     }
289
290     return 0;
291 }
292
293 /* popup */
294 WINDOW*
295 create_popup_win()
296 {
297     return NULL;
298 }
299
300 /* helpers */
301 int
302 ungetstr(char* str)
303 {
304     // ignore null character (it's fine even if strlen = 0)
305     for (int i = strlen(str)-1; i >= 0; i--) {
306         ungetch(str[i]);
307     }
308
309     return 0; 
310 }
311