2 #include "headers/parser.h"
3 #include "headers/render.h"
4 #include "headers/menu.h"
5 #include "headers/utils.h"
11 typedef struct BoardMenu {
17 BoardMenu* create_board_menu(Board* board);
18 int set_selected_menu(BoardMenu* boardmenu, int index);
20 MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length);
21 Menu** make_menus(Board* board, int todolist_length);
22 int swap_menu(BoardMenu* boardmenu, int src_index, int dest_index);
25 main(int argc, char** argv)
27 /* read from todo file */
29 board = begin_parse("test_board.md");
30 /* log_todo(board); */
36 boardmenu = create_board_menu(board);
39 for (int i = 0; i < boardmenu->menu_count; i++) {
40 render_menu(boardmenu->menu_list[i]);
44 while ((ch = getch()) != BINDING_QUIT) {
47 active_menu = boardmenu->menu_list[boardmenu->selected];
51 case BINDING_SCROLL_UP:
52 menu_driver(active_menu, MENU_UP);
54 case BINDING_SCROLL_DOWN:
55 menu_driver(active_menu, MENU_DOWN);
57 case BINDING_SCROLL_LEFT:
58 if (boardmenu->selected-1 < 0) break;
59 set_selected_menu(boardmenu, boardmenu->selected-1);
61 case BINDING_SCROLL_RIGHT:
62 if (boardmenu->selected+1 > boardmenu->menu_count-1) break;
63 set_selected_menu(boardmenu, boardmenu->selected+1);
65 case BINDING_JUMP_TOP:
66 menu_driver(active_menu, MENU_TOP);
68 case BINDING_JUMP_BOTTOM:
69 menu_driver(active_menu, MENU_BOTTOM);
71 case BINDING_MOVE_ITEM_UP:
72 menu_driver(active_menu, MENU_MOVE_UP);
74 case BINDING_MOVE_ITEM_DOWN:
75 menu_driver(active_menu, MENU_MOVE_DOWN);
77 case BINDING_MOVE_ITEM_LEFT:
78 if (boardmenu->selected-1 < 0) break;
83 from_menu = boardmenu->menu_list[boardmenu->selected],
84 to_menu = boardmenu->menu_list[boardmenu->selected-1],
90 get_selected_item(from_menu)
93 get_selected_item(from_menu),
94 get_menu_length(to_menu)
99 get_selected_item(from_menu)
101 set_selected_menu(boardmenu, boardmenu->selected-1);
105 case BINDING_MOVE_ITEM_RIGHT:
106 if (boardmenu->selected >= boardmenu->menu_count-1) break;
107 // this is legit cpy paste please fix this
112 from_menu = boardmenu->menu_list[boardmenu->selected],
113 to_menu = boardmenu->menu_list[boardmenu->selected+1],
119 get_selected_item(from_menu)
122 get_selected_item(from_menu),
123 get_menu_length(to_menu)
128 get_selected_item(from_menu)
130 set_selected_menu(boardmenu, boardmenu->selected+1);
134 case BINDING_DELETE_ITEM:
135 menu_driver(active_menu, MENU_DELETE);
137 case BINDING_APPEND_ITEM:
138 menu_driver(active_menu, MENU_APPEND);
140 case BINDING_INSERT_ABOVE:
141 menu_driver(active_menu, MENU_INSERT_ABOVE);
143 case BINDING_INSERT_BELOW:
144 menu_driver(active_menu, MENU_INSERT_BELOW);
146 /* case BINDING_MOVE_MENU_LEFT: */
147 /* if (boardmenu->selected-1 < 0) break; */
149 /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected-1); */
150 /* boardmenu->selected -= 1; */
151 /* set_selected_menu(boardmenu, boardmenu->selected); */
154 /* case BINDING_MOVE_MENU_RIGHT: */
155 /* if (boardmenu->selected >= boardmenu->menu_count-1) break; */
156 /* swap_menu(boardmenu, boardmenu->selected, boardmenu->selected+1); */
157 /* boardmenu->selected += 1; */
158 /* set_selected_menu(boardmenu, boardmenu->selected); */
161 case BINDING_EDIT_ITEM:
162 menu_driver(active_menu, MENU_EDIT);
166 for (int i = 0; i < boardmenu->menu_count; i++) {
167 render_menu(boardmenu->menu_list[i]);
178 create_board_menu(Board* board)
180 BoardMenu* new_boardmenu;
182 new_boardmenu = malloc(sizeof(BoardMenu));
184 new_boardmenu->menu_list = make_menus(board, board->todolist_count);
185 new_boardmenu->menu_count = board->todolist_count;
186 new_boardmenu->selected = 0;
188 return new_boardmenu;
192 set_selected_menu(BoardMenu* boardmenu, int index)
198 old_menu = boardmenu->menu_list[boardmenu->selected];
199 new_menu = boardmenu->menu_list[index];
201 set_menu_focus(old_menu, false);
202 set_menu_focus(new_menu, true);
204 /* also try to jump to a similar position if possible */
205 /* rn theres a bug if old menu is empty */
206 new_pos = min(get_selected_item(old_menu), get_menu_length(new_menu)-1);
207 set_selected_item(new_menu, new_pos);
209 boardmenu->selected = index;
215 todolist_to_menuitem(TodoItem** item_list, int list_length)
219 items = malloc((list_length+1)*sizeof(MenuItem*));
220 for (int i = 0; i < list_length; i++) {
221 items[i] = create_menuitem(item_list[i]->item_name);
224 items[list_length] = 0; //null terminate
229 make_menus(Board* board, int todolist_length)
234 menu_list = malloc(todolist_length*sizeof(Menu*));
236 for (int i = 0; i < todolist_length; i++) {
238 /* read from parsed */
239 TodoList* todo_list = board->todolist_list[i];
240 MenuItem** item_list = todolist_to_menuitem(todo_list->item_list, todo_list->item_count);
242 Menu* new_menu = create_menu(todo_list->list_name, item_list);
245 WINDOW* win = newwin(20, MENU_WIDTH, 1, 1+MENU_WIDTH*i);
248 /* some menu settings */
249 set_menu_win(new_menu, win);
250 set_menu_focus(new_menu, i == 0); // make first win focused
256 menu_list[i] = new_menu;
263 swap_menu(BoardMenu* boardmenu, int src_index, int dest_index)
265 /* reposition menus */
266 mvwin(get_menu_win(boardmenu->menu_list[src_index]),
267 1, 1+MENU_WIDTH*dest_index
269 mvwin(get_menu_win(boardmenu->menu_list[dest_index]),
270 1, 1+MENU_WIDTH*src_index
272 wrefresh(get_menu_win(boardmenu->menu_list[src_index]));
273 wrefresh(get_menu_win(boardmenu->menu_list[dest_index]));
274 /* wclear(get_menu_win(boardmenu->menu_list[src_index])); */
275 /* wclear(get_menu_win(boardmenu->menu_list[dest_index])); */
276 /* touchwin(get_menu_win(boardmenu->menu_list[src_index])); */
277 /* touchwin(get_menu_win(boardmenu->menu_list[dest_index])); */
281 ar_swap_item(boardmenu->menu_list, src_index, dest_index);