-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
378 lines (328 loc) · 13.4 KB
/
input.c
File metadata and controls
378 lines (328 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "input.h"
#include <conio.h>
#include <stdio.h>
// Global to track the last key pressed for context
static int g_last_key_pressed = 0;
InputAction process_input(int key, UIState *state, AppointmentList *appointments, TodoList *todos) {
g_last_key_pressed = key; // Track the key that was pressed
// Global keys that work in any view
switch (key) {
case 'q':
case 'Q':
return ACTION_QUIT;
case 'h':
case 'H':
return ACTION_HELP;
case 'a':
case 'A':
if (state->selected_view == VIEW_CALENDAR || state->selected_view == VIEW_APPOINTMENTS) {
return ACTION_ADD_APPOINTMENT;
} else if (state->selected_view == VIEW_TODO) {
return ACTION_ADD_TODO;
}
break;
case 'd':
case 'D':
// Only allow delete in appointments and todo views
if (state->selected_view == VIEW_APPOINTMENTS || state->selected_view == VIEW_TODO) {
return ACTION_DELETE;
}
break;
case 'e':
case 'E':
// Only allow edit in appointments and todo views
if (state->selected_view == VIEW_APPOINTMENTS || state->selected_view == VIEW_TODO) {
return ACTION_EDIT;
}
break;
case KEY_TAB:
// Cycle through views
state->selected_view = (state->selected_view + 1) % 3;
state->cursor_x = 0;
state->cursor_y = 0;
state->appointment_display_index = 0;
return ACTION_REDRAW;
case KEY_SPACE:
// Toggle completion for todos only
if (state->selected_view == VIEW_TODO) {
return ACTION_EDIT; // This will be handled specially in edit_selected_item
}
break;
}
// View-specific navigation
switch (state->selected_view) {
case VIEW_CALENDAR:
navigate_calendar(key, state);
break;
case VIEW_APPOINTMENTS:
navigate_appointments(key, state, appointments);
break;
case VIEW_TODO:
navigate_todos(key, state, todos);
break;
}
g_last_key_pressed = key; // Track the last key pressed
return ACTION_REDRAW;
}
void navigate_calendar(int key, UIState *state) {
int days_in_month = get_days_in_month(state->selected_date.year, state->selected_date.month);
switch (key) {
case KEY_LEFT:
state->selected_date.day--;
if (state->selected_date.day < 1) {
// Go to previous month
state->selected_date.month--;
if (state->selected_date.month < 1) {
state->selected_date.month = 12;
state->selected_date.year--;
}
state->selected_date.day = get_days_in_month(state->selected_date.year, state->selected_date.month);
}
break;
case KEY_RIGHT:
state->selected_date.day++;
if (state->selected_date.day > days_in_month) {
// Go to next month
state->selected_date.day = 1;
state->selected_date.month++;
if (state->selected_date.month > 12) {
state->selected_date.month = 1;
state->selected_date.year++;
}
}
break;
case KEY_UP:
state->selected_date.day -= 7;
if (state->selected_date.day < 1) {
// Go to previous month
state->selected_date.month--;
if (state->selected_date.month < 1) {
state->selected_date.month = 12;
state->selected_date.year--;
}
int prev_days = get_days_in_month(state->selected_date.year, state->selected_date.month);
state->selected_date.day = prev_days + state->selected_date.day;
}
break;
case KEY_DOWN:
state->selected_date.day += 7;
if (state->selected_date.day > days_in_month) {
// Go to next month
state->selected_date.day = state->selected_date.day - days_in_month;
state->selected_date.month++;
if (state->selected_date.month > 12) {
state->selected_date.month = 1;
state->selected_date.year++;
}
}
break;
case KEY_PGUP:
// Previous month
state->selected_date.month--;
if (state->selected_date.month < 1) {
state->selected_date.month = 12;
state->selected_date.year--;
}
// Adjust day if necessary
days_in_month = get_days_in_month(state->selected_date.year, state->selected_date.month);
if (state->selected_date.day > days_in_month) {
state->selected_date.day = days_in_month;
}
break;
case KEY_PGDN:
// Next month
state->selected_date.month++;
if (state->selected_date.month > 12) {
state->selected_date.month = 1;
state->selected_date.year++;
}
// Adjust day if necessary
days_in_month = get_days_in_month(state->selected_date.year, state->selected_date.month);
if (state->selected_date.day > days_in_month) {
state->selected_date.day = days_in_month;
}
break;
case KEY_HOME:
// Go to today
state->selected_date = state->current_date;
break;
}
// Reset appointment display index when date changes
state->appointment_display_index = 0;
}
void navigate_appointments(int key, UIState *state, AppointmentList *appointments) {
// Get the count of appointments for the current selected date
int appointment_indices[100];
int appointment_count = 0;
if (appointments) {
appointment_count = find_appointments_by_date(appointments, state->selected_date, appointment_indices, 100);
}
switch (key) {
case KEY_UP:
if (state->cursor_y > 0) {
state->cursor_y -= 2; // Each appointment takes 2 lines
state->appointment_display_index--;
} else if (state->appointment_scroll > 0) {
state->appointment_scroll--;
}
break;
case KEY_DOWN:
// Check if we can move down (appointment_display_index is 0-based)
if (state->appointment_display_index < appointment_count - 1) {
state->cursor_y += 2; // Each appointment takes 2 lines
state->appointment_display_index++;
}
break;
case KEY_PGUP:
state->appointment_scroll -= 5;
if (state->appointment_scroll < 0) {
state->appointment_scroll = 0;
}
break;
case KEY_PGDN:
state->appointment_scroll += 5;
break;
}
// Ensure cursor_y and appointment_display_index stay in sync
if (state->appointment_display_index < 0) {
state->appointment_display_index = 0;
state->cursor_y = 0;
} else if (state->appointment_display_index >= appointment_count && appointment_count > 0) {
state->appointment_display_index = appointment_count - 1;
state->cursor_y = state->appointment_display_index * 2;
} else if (appointment_count == 0) {
state->appointment_display_index = 0;
state->cursor_y = 0;
}
}
void navigate_todos(int key, UIState *state, TodoList *todos) {
int todo_count = todos ? todos->count : 0;
switch (key) {
case KEY_UP:
if (state->cursor_y > 0) {
state->cursor_y--;
} else if (state->todo_scroll > 0) {
state->todo_scroll--;
}
break;
case KEY_DOWN:
// Check if we can move down (considering scroll offset)
if (state->cursor_y + state->todo_scroll < todo_count - 1) {
state->cursor_y++;
}
break;
case KEY_PGUP:
state->todo_scroll -= 5;
if (state->todo_scroll < 0) {
state->todo_scroll = 0;
}
break;
case KEY_PGDN:
if (state->todo_scroll + 5 < todo_count) {
state->todo_scroll += 5;
}
break;
}
// Ensure cursor position stays within bounds
if (state->cursor_y < 0) {
state->cursor_y = 0;
}
// Ensure we don't go beyond the available todos
if (state->cursor_y + state->todo_scroll >= todo_count && todo_count > 0) {
if (todo_count > 0) {
state->cursor_y = todo_count - 1 - state->todo_scroll;
if (state->cursor_y < 0) {
state->todo_scroll = todo_count - 1;
state->cursor_y = 0;
}
} else {
state->cursor_y = 0;
state->todo_scroll = 0;
}
}
}
void delete_selected_item(UIState *state, AppointmentList *appointments, TodoList *todos) {
// Confirmation dialog
int window_width, window_height;
update_console_size(state);
window_width = state->window_width;
window_height = state->window_height;
int dialog_x = window_width / 2 - 20;
int dialog_y = window_height / 2 - 3;
// Clear the background area first
clear_area(dialog_x, dialog_y, 40, 6);
draw_box(dialog_x, dialog_y, 40, 6, "Confirm Delete");
set_color(NORMAL_FG, NORMAL_BG);
gotoxy(dialog_x + 2, dialog_y + 2);
printf("Delete selected item? (y/n)");
int ch = _getch();
if (ch == 'y' || ch == 'Y') {
switch (state->selected_view) {
case VIEW_APPOINTMENTS:
{
// Find the actual appointment index based on cursor position
int appointment_indices[100];
int appointment_count = find_appointments_by_date(
appointments,
state->selected_date,
appointment_indices,
100
);
// Calculate which appointment is selected (each appointment takes 2 lines)
int selected_appointment_index = state->cursor_y / 2;
if (selected_appointment_index >= 0 && selected_appointment_index < appointment_count) {
int actual_index = appointment_indices[selected_appointment_index];
delete_appointment(appointments, actual_index);
// Adjust cursor if necessary
if (state->cursor_y > 0 && selected_appointment_index >= appointment_count - 1) {
state->cursor_y -= 2;
if (state->cursor_y < 0) state->cursor_y = 0;
}
}
}
break;
case VIEW_TODO:
if (state->cursor_y + state->todo_scroll < todos->count) {
delete_todo(todos, state->cursor_y + state->todo_scroll);
// Adjust cursor if at the end
if (state->cursor_y > 0 && state->cursor_y + state->todo_scroll >= todos->count - 1) {
state->cursor_y--;
}
}
break;
}
}
}
void edit_selected_item(UIState *state, AppointmentList *appointments, TodoList *todos) {
switch (state->selected_view) {
case VIEW_APPOINTMENTS:
{
// Find the actual appointment index based on cursor position
int appointment_indices[100];
int appointment_count = find_appointments_by_date(
appointments,
state->selected_date,
appointment_indices,
100
);
// Calculate which appointment is selected (each appointment takes 2 lines)
int selected_appointment_index = state->cursor_y / 2;
if (selected_appointment_index >= 0 && selected_appointment_index < appointment_count) {
int actual_index = appointment_indices[selected_appointment_index];
edit_appointment_interactive(appointments, actual_index);
}
}
break;
case VIEW_TODO:
if (state->cursor_y + state->todo_scroll < todos->count) {
// If space was pressed, just toggle completion
if (g_last_key_pressed == KEY_SPACE) {
toggle_todo_completion(todos, state->cursor_y + state->todo_scroll);
} else {
// Otherwise, open edit dialog
edit_todo_interactive(todos, state->cursor_y + state->todo_scroll);
}
}
break;
}
}