refactor + man
[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         items[i] = create_menuitem(item_list[i]->item_name);
133     }
134
135     items[list_length] = 0; //null terminate
136     return items;
137 }
138
139 Board*
140 boardmenu_to_board(BoardMenu* boardmenu)
141 { // STRINGS are sharing the same address as the one in MENU
142   // and MENUITEM, this may break something if u free this board
143   // consider copying the string
144
145     Board* newboard = malloc(sizeof(Board));
146     TodoList** new_todolist_list = malloc(sizeof(TodoList*));
147
148     for (int i = 0; i < boardmenu->menu_count; i++) {
149         Menu* curmenu = boardmenu->menu_list[i];
150
151         TodoList* new_todolist = malloc(sizeof(TodoList));
152         TodoItem** new_item_list = malloc(sizeof(TodoItem*));
153         new_todolist->list_name = strdup(get_menu_name(curmenu));
154         new_todolist->item_count = get_menu_length(curmenu);
155
156         for (int j = 0; j < get_menu_length(curmenu); j++) {
157             MenuItem* curmenuitem = get_menu_item(curmenu, j);
158
159             TodoItem* new_todoitem = malloc(sizeof(TodoItem));
160
161             new_todoitem->item_name = strdup(get_menuitem_title(curmenuitem));
162             new_todoitem->description = strdup(get_menuitem_descrip(curmenuitem));
163             new_todoitem->due = strdup(""); //TEMP! 
164             new_todoitem->subtask_list = malloc(0); //TEMP!
165             new_todoitem->subtask_count = 0; //TEMP!
166
167             new_item_list[j] = new_todoitem;
168         }
169
170         new_todolist->item_list = new_item_list;
171         new_todolist_list[i] = new_todolist;
172
173     }
174
175     newboard->board_name = strdup("");
176     newboard->todolist_list = new_todolist_list;
177     newboard->todolist_count = boardmenu->menu_count;
178
179     return newboard;
180 }
181
182 int
183 set_selected_menu(BoardMenu* boardmenu, int index)
184 {
185     Menu* old_menu;
186     Menu* new_menu;
187     int new_pos;
188     
189     old_menu = boardmenu->menu_list[boardmenu->selected];
190     new_menu = boardmenu->menu_list[index];
191
192     set_menu_focus(old_menu, false);
193     set_menu_focus(new_menu, true);
194
195     /* also try to jump to a similar position if possible */
196     /* rn theres a bug if old menu is empty */
197     new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
198     set_selected_item(new_menu, new_pos);
199
200     boardmenu->selected = index;
201
202     return 0;
203 }
204
205 int
206 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
207 {
208     /* reposition menus */
209     mvwin(get_menu_win(boardmenu->menu_list[src_index]),
210         1, 1+MENU_WIDTH*dest_index
211     );
212     mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
213         1, 1+MENU_WIDTH*src_index
214     );
215     refresh();
216     wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
217     wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
218     /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
219     /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
220     /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
221     /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
222     clear();
223
224     /* swap in array */
225     ar_swap_item((void*)boardmenu->menu_list, src_index, dest_index);
226
227     return 0;
228 }
229
230 /* popup */
231 WINDOW*
232 create_popup_win()
233 {
234     return NULL;
235 }
236
237
238 int
239 ungetstr(char* str)
240 {
241     // ignore null character (it's fine even if strlen = 0)
242     for (int i = strlen(str)-1; i >= 0; i--) {
243         ungetch(str[i]);
244     }
245
246     return 0; 
247 }
248