-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
2210 lines (1982 loc) · 90.2 KB
/
main.c
File metadata and controls
2210 lines (1982 loc) · 90.2 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// main.c - Enhanced LiveLedger with range selection, formatting, and XLOOKUP
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "console.h"
#include "sheet.h"
#include "charts.h"
#include "constants.h"
// Application state
typedef enum {
MODE_NORMAL,
MODE_INSERT_NUMBER,
MODE_INSERT_STRING,
MODE_INSERT_FORMULA,
MODE_COMMAND,
MODE_RANGE_SELECT, // Range selection mode
MODE_EDIT // Edit existing cell mode
} AppMode;
// Undo/Redo system
typedef enum {
UNDO_CELL_CHANGE,
UNDO_RANGE_CHANGE,
UNDO_CLEAR_CELL,
UNDO_FORMAT_CHANGE,
UNDO_RESIZE_COLUMN,
UNDO_RESIZE_ROW
} UndoType;
typedef struct {
int row, col;
CellType old_type;
CellType new_type;
union {
double number;
char* string;
struct {
char* expression;
double cached_value;
char* cached_string;
int is_string_result;
ErrorType error;
} formula;
} old_data;
union {
double number;
char* string;
struct {
char* expression;
double cached_value;
char* cached_string;
int is_string_result;
ErrorType error;
} formula;
} new_data;
// Formatting data
DataFormat old_format;
FormatStyle old_format_style;
DataFormat new_format;
FormatStyle new_format_style;
int old_text_color;
int old_background_color;
int new_text_color;
int new_background_color;
} CellUndoData;
typedef struct {
int start_row, start_col;
int end_row, end_col;
CellUndoData* cell_data;
int cell_count;
} RangeUndoData;
typedef struct {
int index; // Column or row index
int old_size;
int new_size;
} ResizeUndoData;
typedef struct UndoAction {
UndoType type;
union {
CellUndoData cell;
RangeUndoData range;
ResizeUndoData resize;
} data;
char description[DESCRIPTION_BUFFER_SIZE];
} UndoAction;
#define MAX_UNDO_ACTIONS 100
typedef struct {
UndoAction actions[MAX_UNDO_ACTIONS];
int current_index; // Points to the next action to be added
int count; // Number of actions in the buffer
} UndoBuffer;
typedef struct {
Sheet* sheet;
Console* console;
AppMode mode;
int cursor_row;
int cursor_col;
int view_top;
int view_left;
char input_buffer[INPUT_BUFFER_SIZE];
int input_pos;
char status_message[STATUS_BUFFER_SIZE];
BOOL running;
// Cursor blinking state
DWORD cursor_blink_time;
BOOL cursor_visible;
DWORD cursor_blink_rate;
// Range selection state
BOOL range_selection_active;
int range_start_row;
int range_start_col;
// Undo/Redo system
UndoBuffer undo_buffer;
// Autosave system
DWORD last_autosave_time;
DWORD autosave_interval; // 3 minutes in milliseconds
} AppState;
// Function prototypes
void app_init(AppState* state);
void app_cleanup(AppState* state);
void app_render(AppState* state);
void app_handle_input(AppState* state, KeyEvent* key);
void app_execute_command(AppState* state, const char* command);
void app_start_input(AppState* state, AppMode mode);
void app_start_edit(AppState* state);
void app_finish_input(AppState* state);
void app_cancel_input(AppState* state);
void app_update_cursor_blink(AppState* state);
void app_show_chart(AppState* state, ChartType type, const char* x_label, const char* y_label);
// Range selection functions
void app_start_range_selection(AppState* state);
void app_extend_range_selection(AppState* state, int row, int col);
void app_finish_range_selection(AppState* state);
void app_cancel_range_selection(AppState* state);
// Copy/paste operations
void app_copy_cell(AppState* state);
void app_paste_cell(AppState* state);
void app_copy_to_system_clipboard(AppState* state);
void app_paste_from_system_clipboard(AppState* state);
// Range copy/paste operations
void app_copy_range(AppState* state);
void app_paste_range(AppState* state);
// Chart display function
void display_chart_popup(Console* console, Chart* chart, const char* title);
// Formatting functions
void app_set_cell_format(AppState* state, DataFormat format, FormatStyle style);
void app_cycle_date_format(AppState* state);
void app_cycle_datetime_format(AppState* state);
// Undo/Redo system functions
void undo_buffer_init(UndoBuffer* buffer);
void undo_buffer_cleanup(UndoBuffer* buffer);
void undo_save_cell_state(AppState* state, int row, int col, const char* description);
void undo_save_range_state(AppState* state, int start_row, int start_col, int end_row, int end_col, const char* description);
void undo_save_resize_state(AppState* state, int index, int old_size, int new_size, UndoType type, const char* description);
void undo_perform(AppState* state);
void redo_perform(AppState* state);
void undo_copy_cell_data(Cell* src, CellUndoData* dest);
void undo_restore_cell_data(AppState* state, CellUndoData* src, int row, int col);
void undo_free_cell_data(CellUndoData* data);
// System clipboard functions
BOOL set_system_clipboard_text(const char* text);
char* get_system_clipboard_text(void);
// Autosave functions
void app_create_autosave_directory(void);
void app_perform_autosave(AppState* state);
void app_get_timestamp_filename(char* filename, size_t size);
// Initialize application
void app_init(AppState* state) {
state->sheet = sheet_new(1000, 100);
state->console = console_init();
if (!state->sheet || !state->console) {
if (state->sheet) sheet_free(state->sheet);
if (state->console) console_cleanup(state->console);
state->running = FALSE;
return;
}
state->mode = MODE_NORMAL;
state->cursor_row = 0;
state->cursor_col = 0;
state->view_top = 0;
state->view_left = 0;
state->input_buffer[0] = '\0';
state->input_pos = 0;
strcpy_s(state->status_message, sizeof(state->status_message), "Ready");
state->running = TRUE;
// Initialize range selection
state->range_selection_active = FALSE;
state->range_start_row = 0;
state->range_start_col = 0;
// Initialize undo buffer
undo_buffer_init(&state->undo_buffer);
state->cursor_blink_time = GetTickCount();
state->cursor_visible = TRUE;
state->cursor_blink_rate = CURSOR_BLINK_RATE_MS;
// Initialize autosave system
state->last_autosave_time = GetTickCount();
state->autosave_interval = AUTOSAVE_INTERVAL_MS;
app_create_autosave_directory();
console_hide_cursor(state->console);
sheet_recalculate(state->sheet);
}
void app_cleanup(AppState* state) {
// Cleanup undo buffer
undo_buffer_cleanup(&state->undo_buffer);
if (state->sheet) {
sheet_free(state->sheet);
state->sheet = NULL;
}
if (state->console) {
console_cleanup(state->console);
state->console = NULL;
}
}
void app_update_cursor_blink(AppState* state) {
DWORD current_time = GetTickCount();
if (current_time - state->cursor_blink_time > state->cursor_blink_rate) {
state->cursor_visible = !state->cursor_visible;
state->cursor_blink_time = current_time;
}
}
// Start range selection
void app_start_range_selection(AppState* state) {
state->range_selection_active = TRUE;
state->range_start_row = state->cursor_row;
state->range_start_col = state->cursor_col;
sheet_start_range_selection(state->sheet, state->cursor_row, state->cursor_col);
strcpy_s(state->status_message, sizeof(state->status_message), "Range selection started");
}
// Extend range selection
void app_extend_range_selection(AppState* state, int row, int col) {
if (state->range_selection_active) {
sheet_extend_range_selection(state->sheet, row, col);
char start_ref[CELL_REF_BUFFER_SIZE];
char end_ref[CELL_REF_BUFFER_SIZE];
cell_reference_to_string(state->range_start_row, state->range_start_col, start_ref, sizeof(start_ref));
cell_reference_to_string(row, col, end_ref, sizeof(end_ref));
sprintf_s(state->status_message, sizeof(state->status_message),
"Selected: %s:%s", start_ref, end_ref);
}
}
// Finish range selection
void app_finish_range_selection(AppState* state) {
state->range_selection_active = FALSE;
strcpy_s(state->status_message, sizeof(state->status_message), "Range selected");
}
// Cancel range selection
void app_cancel_range_selection(AppState* state) {
state->range_selection_active = FALSE;
sheet_clear_range_selection(state->sheet);
strcpy_s(state->status_message, sizeof(state->status_message), "Range selection cancelled");
}
// Render the spreadsheet
void app_render(AppState* state) {
Console* con = state->console;
if (!con || !con->backBuffer) {
return;
}
// Colors
WORD headerColor = MAKE_COLOR(COLOR_BLACK, COLOR_WHITE);
WORD cellColor = MAKE_COLOR(COLOR_WHITE, COLOR_BLACK);
WORD selectedColor = MAKE_COLOR(COLOR_BLACK, COLOR_CYAN);
WORD gridColor = MAKE_COLOR(COLOR_WHITE | COLOR_BRIGHT, COLOR_BLACK);
WORD rangeColor = MAKE_COLOR(COLOR_BLACK, COLOR_YELLOW); // Range selection color
// Clear back buffer
for (int i = 0; i < con->width * con->height; i++) {
con->backBuffer[i].Char.AsciiChar = ' ';
con->backBuffer[i].Attributes = cellColor;
}
// Calculate visible area (with dynamic column widths)
int col_header_width = 4;
int status_height = 2;
int visible_cols = 0;
int visible_rows = con->height - status_height - 1;
// Calculate how many columns fit in the screen width
int total_width = col_header_width;
for (int i = 0; i < state->sheet->cols && total_width < con->width; i++) {
int col_width = sheet_get_column_width(state->sheet, state->view_left + i);
if (total_width + col_width <= con->width) {
total_width += col_width;
visible_cols++;
} else {
break;
}
}
if (visible_rows < 1 || visible_cols < 1) {
return;
}
// Draw column headers (with dynamic widths)
int current_x = col_header_width;
for (int i = 0; i < visible_cols && state->view_left + i < state->sheet->cols; i++) {
char colName[COLUMN_NAME_BUFFER_SIZE];
int col = state->view_left + i;
int col_width = sheet_get_column_width(state->sheet, col);
if (col < 26) {
sprintf_s(colName, sizeof(colName), "%c", 'A' + col);
} else {
sprintf_s(colName, sizeof(colName), "%c%c", 'A' + (col / 26) - 1, 'A' + (col % 26));
}
// Center the column name in the column width
int center_x = current_x + (col_width / 2) - ((int)strlen(colName) / 2);
console_write_string(con, center_x, 0, colName, headerColor);
current_x += col_width;
}
// Draw row headers (accounting for variable row heights)
int visual_row = 0;
for (int sheet_row = state->view_top; sheet_row < state->sheet->rows && visual_row < visible_rows; sheet_row++) {
int row_height = sheet_get_row_height(state->sheet, sheet_row);
// Draw row number only on the first line of each row
char rowNum[ROW_NUMBER_BUFFER_SIZE];
sprintf_s(rowNum, sizeof(rowNum), "%3d", sheet_row + 1);
console_write_string(con, 0, visual_row + 1, rowNum, headerColor);
// Skip visual rows for the height of this row
visual_row += row_height;
}
// Draw grid and cell contents (with dynamic sizes)
current_x = col_header_width;
for (int row = 0; row < visible_rows && state->view_top + row < state->sheet->rows; row++) {
int sheet_row = state->view_top + row;
int row_height = sheet_get_row_height(state->sheet, sheet_row);
// Draw multiple lines for tall rows
for (int row_line = 0; row_line < row_height && row + row_line < visible_rows; row_line++) {
int y = row + 1 + row_line;
current_x = col_header_width;
for (int col = 0; col < visible_cols && state->view_left + col < state->sheet->cols; col++) {
int sheet_col = state->view_left + col;
int col_width = sheet_get_column_width(state->sheet, sheet_col);
// Draw vertical grid line
console_write_char(con, current_x, y, '|', gridColor);
// Only draw content on the first line of each cell
if (row_line == 0) {
// Get cell value
char* value = sheet_get_display_value(state->sheet, sheet_row, sheet_col);
// Truncate if too long for column width
char display[51]; // Max column width
int max_len = col_width - 1;
if (max_len > 50) max_len = 50;
strncpy_s(display, sizeof(display), value, max_len);
display[max_len] = '\0';
// Determine color
WORD color = cellColor;
BOOL is_current_cell = (sheet_row == state->cursor_row && sheet_col == state->cursor_col);
// Check if cell is in range selection
BOOL is_in_range = sheet_is_in_selection(state->sheet, sheet_row, sheet_col);
// Get cell for color formatting
Cell* cell = sheet_get_cell(state->sheet, sheet_row, sheet_col);
if (cell && (cell->text_color >= 0 || cell->background_color >= 0)) {
// Apply custom colors
int fg = (cell->text_color >= 0) ? cell->text_color : COLOR_WHITE;
int bg = (cell->background_color >= 0) ? cell->background_color : COLOR_BLACK;
color = MAKE_COLOR(fg, bg);
}
if (is_in_range) {
// Range selection takes priority, but distinguish current cell in range
if (is_current_cell) {
// Current cell in range gets a special highlight
color = MAKE_COLOR(COLOR_YELLOW, COLOR_BLUE);
} else {
color = rangeColor; // Normal range selection color
}
} else if (is_current_cell) {
if (state->cursor_visible) {
color = selectedColor;
} else {
color = MAKE_COLOR(COLOR_WHITE, COLOR_BLUE);
}
}
// Draw cell content
console_write_string(con, current_x + 1, y, display, color);
// Draw blinking cursor indicator in the current cell
if (is_current_cell && state->cursor_visible) {
int cursor_x = current_x + 1 + (int)strlen(display);
if (cursor_x < current_x + col_width) {
console_write_char(con, cursor_x, y, '_', selectedColor);
}
}
}
current_x += col_width;
if (current_x >= con->width) break;
}
}
// Skip additional rows if this row was taller than 1
if (row_height > 1) {
row += row_height - 1;
}
}
// Draw horizontal line above status
int status_y = con->height - status_height;
for (int x = 0; x < con->width; x++) {
console_write_char(con, x, status_y, '-', headerColor);
}
// Draw status line
char status[STATUS_BUFFER_SIZE];
char cellRef[CELL_REF_BUFFER_SIZE];
cell_reference_to_string(state->cursor_row, state->cursor_col, cellRef, sizeof(cellRef));
Cell* currentCell = sheet_get_cell(state->sheet, state->cursor_row, state->cursor_col);
if (state->mode == MODE_NORMAL) {
if (currentCell && currentCell->type == CELL_FORMULA) {
sprintf_s(status, sizeof(status), "[%s] %s: %s | %s",
state->sheet->name, cellRef,
currentCell->data.formula.expression,
state->status_message);
} else {
// Show cell formatting info
if (currentCell && currentCell->format != FORMAT_GENERAL) {
const char* format_name = "General";
switch (currentCell->format) {
case FORMAT_PERCENTAGE: format_name = "Percentage"; break;
case FORMAT_CURRENCY: format_name = "Currency"; break;
case FORMAT_DATE: format_name = "Date"; break;
case FORMAT_TIME: format_name = "Time"; break;
case FORMAT_DATETIME: format_name = "DateTime"; break;
case FORMAT_NUMBER: format_name = "Number"; break;
default: format_name = "General"; break;
}
sprintf_s(status, sizeof(status), "[%s] %s (%s) | %s",
state->sheet->name, cellRef, format_name, state->status_message);
} else {
sprintf_s(status, sizeof(status), "[%s] %s | %s",
state->sheet->name, cellRef, state->status_message);
}
}
} else if (state->mode == MODE_COMMAND && strlen(state->input_buffer) == 0 && strlen(state->status_message) > 0) {
sprintf_s(status, sizeof(status), "%s", state->status_message);
} else {
// Show input buffer with cursor
char input_with_cursor[300];
if (state->cursor_visible) {
strncpy_s(input_with_cursor, sizeof(input_with_cursor), state->input_buffer, state->input_pos);
input_with_cursor[state->input_pos] = '\0';
strcat_s(input_with_cursor, sizeof(input_with_cursor), "_");
strcat_s(input_with_cursor, sizeof(input_with_cursor), &state->input_buffer[state->input_pos]);
} else {
strcpy_s(input_with_cursor, sizeof(input_with_cursor), state->input_buffer);
}
sprintf_s(status, sizeof(status), "[%s] %s > %s",
state->sheet->name, cellRef, input_with_cursor);
}
console_write_string(con, 0, status_y + 1, status, headerColor);
console_flip(con);
}
// Start input mode
void app_start_input(AppState* state, AppMode mode) {
state->mode = mode;
state->input_buffer[0] = '\0';
state->input_pos = 0;
state->cursor_blink_rate = 300;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
// Pre-fill with current cell value for editing
if (mode == MODE_INSERT_FORMULA || mode == MODE_INSERT_NUMBER) {
Cell* cell = sheet_get_cell(state->sheet, state->cursor_row, state->cursor_col);
if (cell) {
if (cell->type == CELL_FORMULA) {
strcpy_s(state->input_buffer, sizeof(state->input_buffer), cell->data.formula.expression);
} else if (cell->type == CELL_NUMBER) {
sprintf_s(state->input_buffer, sizeof(state->input_buffer), "%g", cell->data.number);
}
state->input_pos = (int)strlen(state->input_buffer);
}
}
}
// Start edit mode - pre-fills buffer with current cell content
void app_start_edit(AppState* state) {
Cell* cell = sheet_get_cell(state->sheet, state->cursor_row, state->cursor_col);
state->mode = MODE_EDIT;
state->input_buffer[0] = '\0';
state->input_pos = 0;
state->cursor_blink_rate = 300;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
// Pre-fill buffer with current cell content
if (cell) {
switch (cell->type) {
case CELL_FORMULA:
strcpy_s(state->input_buffer, sizeof(state->input_buffer), cell->data.formula.expression);
break;
case CELL_NUMBER:
sprintf_s(state->input_buffer, sizeof(state->input_buffer), "%g", cell->data.number);
break;
case CELL_STRING:
strcpy_s(state->input_buffer, sizeof(state->input_buffer), cell->data.string);
break;
default:
break;
}
}
// Set cursor position to end of buffer
state->input_pos = (int)strlen(state->input_buffer);
}
void app_finish_input(AppState* state) {
// Save undo state before making changes
const char* action_desc;
switch (state->mode) {
case MODE_INSERT_NUMBER:
action_desc = "Enter number";
break;
case MODE_INSERT_FORMULA:
action_desc = "Enter formula";
break;
case MODE_INSERT_STRING:
action_desc = "Enter text";
break;
case MODE_EDIT:
action_desc = "Edit cell";
break;
case MODE_COMMAND:
action_desc = "Execute command";
break;
default:
action_desc = "Input";
break;
}
if (state->mode != MODE_COMMAND) {
undo_save_cell_state(state, state->cursor_row, state->cursor_col, action_desc);
}
switch (state->mode) {
case MODE_EDIT:
case MODE_INSERT_NUMBER:
case MODE_INSERT_FORMULA:
if (state->input_buffer[0] == '=') {
sheet_set_formula(state->sheet, state->cursor_row,
state->cursor_col, state->input_buffer);
} else {
char* endptr;
double value = strtod(state->input_buffer, &endptr);
if (*endptr == '\0') {
sheet_set_number(state->sheet, state->cursor_row,
state->cursor_col, value);
} else {
sheet_set_string(state->sheet, state->cursor_row,
state->cursor_col, state->input_buffer);
}
}
sheet_recalculate(state->sheet);
break;
case MODE_INSERT_STRING:
sheet_set_string(state->sheet, state->cursor_row,
state->cursor_col, state->input_buffer);
break;
case MODE_COMMAND:
app_execute_command(state, state->input_buffer);
break;
}
state->mode = MODE_NORMAL;
state->cursor_blink_rate = CURSOR_BLINK_RATE_MS;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
}
void app_cancel_input(AppState* state) {
state->mode = MODE_NORMAL;
state->cursor_blink_rate = CURSOR_BLINK_RATE_MS;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
strcpy_s(state->status_message, sizeof(state->status_message), "Cancelled");
}
// Set cell formatting
void app_set_cell_format(AppState* state, DataFormat format, FormatStyle style) {
undo_save_cell_state(state, state->cursor_row, state->cursor_col, "Format cell");
Cell* cell = sheet_get_or_create_cell(state->sheet, state->cursor_row, state->cursor_col);
if (cell) {
cell_set_format(cell, format, style);
// Update status message based on format type
switch (format) {
case FORMAT_PERCENTAGE:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatted as percentage");
break;
case FORMAT_CURRENCY:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatted as currency");
break;
case FORMAT_DATE:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatted as date");
break;
case FORMAT_TIME:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatted as time");
break;
case FORMAT_NUMBER:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatted as number");
break;
case FORMAT_GENERAL:
default:
strcpy_s(state->status_message, sizeof(state->status_message), "Cell formatting reset to general");
break;
} } else {
strcpy_s(state->status_message, sizeof(state->status_message), "Failed to format cell");
}
}
// Enhanced function to cycle through comprehensive date/time formats
void app_cycle_datetime_format(AppState* state) {
undo_save_cell_state(state, state->cursor_row, state->cursor_col, "Cycle datetime format");
Cell* cell = sheet_get_or_create_cell(state->sheet, state->cursor_row, state->cursor_col);
if (cell) {
// Define the cycling order through different formats
typedef struct {
DataFormat format;
FormatStyle style;
const char* description;
} FormatOption;
static const FormatOption format_cycle[] = {
// Date formats
{FORMAT_DATE, DATE_STYLE_MM_DD_YYYY, "Date format: MM/DD/YYYY"},
{FORMAT_DATE, DATE_STYLE_DD_MM_YYYY, "Date format: DD/MM/YYYY"},
{FORMAT_DATE, DATE_STYLE_YYYY_MM_DD, "Date format: YYYY-MM-DD"},
{FORMAT_DATE, DATE_STYLE_SHORT_DATE, "Date format: MM/DD/YY"},
{FORMAT_DATE, DATE_STYLE_MON_DD_YYYY, "Date format: Mon DD, YYYY"},
{FORMAT_DATE, DATE_STYLE_DD_MON_YYYY, "Date format: DD Mon YYYY"},
// Time formats
{FORMAT_TIME, TIME_STYLE_12HR, "Time format: 12-hour"},
{FORMAT_TIME, TIME_STYLE_24HR, "Time format: 24-hour"},
{FORMAT_TIME, TIME_STYLE_SECONDS, "Time format: with seconds"},
{FORMAT_TIME, TIME_STYLE_12HR_SECONDS, "Time format: 12-hour with seconds"},
// DateTime formats
{FORMAT_DATETIME, DATETIME_STYLE_SHORT, "DateTime format: Short"},
{FORMAT_DATETIME, DATETIME_STYLE_LONG, "DateTime format: Long"},
{FORMAT_DATETIME, DATETIME_STYLE_ISO, "DateTime format: ISO 8601"}
};
const int cycle_length = sizeof(format_cycle) / sizeof(format_cycle[0]);
int current_index = -1;
// Find current format in the cycle
for (int i = 0; i < cycle_length; i++) {
if (cell->format == format_cycle[i].format &&
cell->format_style == format_cycle[i].style) {
current_index = i;
break;
}
}
// Move to next format in cycle (or start at beginning)
int next_index = (current_index + 1) % cycle_length;
// Apply the new format
cell_set_format(cell, format_cycle[next_index].format, format_cycle[next_index].style);
// Update status message
strcpy_s(state->status_message, sizeof(state->status_message), format_cycle[next_index].description);
} else {
strcpy_s(state->status_message, sizeof(state->status_message), "Failed to format cell");
}
}
// Function to cycle through date formats
void app_cycle_date_format(AppState* state) {
undo_save_cell_state(state, state->cursor_row, state->cursor_col, "Cycle date format");
Cell* cell = sheet_get_or_create_cell(state->sheet, state->cursor_row, state->cursor_col);
if (cell) {
// Get current format style or start with first style
FormatStyle current_style = cell->format_style;
FormatStyle next_style;
// If cell is not currently a date format, start with first date style
if (cell->format != FORMAT_DATE) {
next_style = DATE_STYLE_MM_DD_YYYY;
} else {
// Cycle through date styles
switch (current_style) {
case DATE_STYLE_MM_DD_YYYY:
next_style = DATE_STYLE_DD_MM_YYYY;
break;
case DATE_STYLE_DD_MM_YYYY:
next_style = DATE_STYLE_YYYY_MM_DD;
break;
case DATE_STYLE_YYYY_MM_DD:
default:
next_style = DATE_STYLE_MM_DD_YYYY;
break;
}
}
// Apply the new date format
cell_set_format(cell, FORMAT_DATE, next_style);
// Update status message based on the new style
switch (next_style) {
case DATE_STYLE_MM_DD_YYYY:
strcpy_s(state->status_message, sizeof(state->status_message), "Date format: MM/DD/YYYY");
break;
case DATE_STYLE_DD_MM_YYYY:
strcpy_s(state->status_message, sizeof(state->status_message), "Date format: DD/MM/YYYY");
break;
case DATE_STYLE_YYYY_MM_DD:
strcpy_s(state->status_message, sizeof(state->status_message), "Date format: YYYY-MM-DD");
break;
default:
strcpy_s(state->status_message, sizeof(state->status_message), "Date format applied");
break;
}
} else {
strcpy_s(state->status_message, sizeof(state->status_message), "Failed to format cell");
}
}
int ask_preserve_formulas(AppState* state, const char* operation) {
AppMode old_mode = state->mode;
char old_status[256];
strcpy_s(old_status, sizeof(old_status), state->status_message);
state->mode = MODE_COMMAND;
sprintf_s(state->status_message, sizeof(state->status_message),
"%s: Type 'f' to flatten (save calculated values) or 'p' to preserve (save formulas as text): ", operation);
state->input_buffer[0] = '\0';
state->input_pos = 0;
state->cursor_blink_rate = 300;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
app_render(state);
KeyEvent key;
int result = -1;
while (state->running) {
if (console_get_key(state->console, &key)) {
if (key.type == 1 && key.key.special == VK_ESCAPE) {
strcpy_s(state->status_message, sizeof(state->status_message), "Cancelled");
result = -1;
break;
} else if (key.type == 0) {
if (key.key.ch == 'f' || key.key.ch == 'F') {
result = 0;
break;
} else if (key.key.ch == 'p' || key.key.ch == 'P') {
result = 1;
break;
}
}
}
Sleep(DEFAULT_SLEEP_MS);
}
state->mode = old_mode;
state->input_buffer[0] = '\0';
state->input_pos = 0;
state->cursor_blink_rate = CURSOR_BLINK_RATE_MS;
state->cursor_visible = TRUE;
state->cursor_blink_time = GetTickCount();
if (result != -1) {
strcpy_s(state->status_message, sizeof(state->status_message), old_status);
}
return result;
}
// Execute command
void app_execute_command(AppState* state, const char* command) {
if (strcmp(command, "q") == 0 || strcmp(command, "quit") == 0) {
state->running = FALSE;
} else if (strncmp(command, "savecsv ", 8) == 0) {
const char* filename = command + 8;
if (strlen(filename) == 0) {
strcpy_s(state->status_message, sizeof(state->status_message), "Usage: savecsv <filename>");
return;
}
int preserve = ask_preserve_formulas(state, "Save CSV");
if (preserve == -1) return;
if (sheet_save_csv(state->sheet, filename, preserve)) {
sprintf_s(state->status_message, sizeof(state->status_message),
"Saved to %s (%s)", filename, preserve ? "formulas preserved" : "values flattened");
} else {
sprintf_s(state->status_message, sizeof(state->status_message),
"Failed to save %s", filename);
}
} else if (strncmp(command, "loadcsv ", 8) == 0) {
const char* filename = command + 8;
if (strlen(filename) == 0) {
strcpy_s(state->status_message, sizeof(state->status_message), "Usage: loadcsv <filename>");
return;
}
int preserve = ask_preserve_formulas(state, "Load CSV");
if (preserve == -1) return;
if (sheet_load_csv(state->sheet, filename, preserve)) {
sprintf_s(state->status_message, sizeof(state->status_message),
"Loaded from %s (%s)", filename, preserve ? "formulas preserved" : "values only");
} else {
sprintf_s(state->status_message, sizeof(state->status_message),
"Failed to load %s", filename);
}
}
// Formatting commands
else if (strcmp(command, "format percentage") == 0) {
app_set_cell_format(state, FORMAT_PERCENTAGE, 0);
} else if (strcmp(command, "format currency") == 0) {
app_set_cell_format(state, FORMAT_CURRENCY, 0);
} else if (strcmp(command, "format date") == 0) {
app_set_cell_format(state, FORMAT_DATE, DATE_STYLE_MM_DD_YYYY);
} else if (strcmp(command, "format date dd/mm/yyyy") == 0) {
app_set_cell_format(state, FORMAT_DATE, DATE_STYLE_DD_MM_YYYY);
} else if (strcmp(command, "format date yyyy-mm-dd") == 0) {
app_set_cell_format(state, FORMAT_DATE, DATE_STYLE_YYYY_MM_DD);
} else if (strcmp(command, "format time") == 0) {
app_set_cell_format(state, FORMAT_TIME, TIME_STYLE_12HR);
} else if (strcmp(command, "format time 24hr") == 0) {
app_set_cell_format(state, FORMAT_TIME, TIME_STYLE_24HR);
} else if (strcmp(command, "format time seconds") == 0) {
app_set_cell_format(state, FORMAT_TIME, TIME_STYLE_SECONDS);
} else if (strcmp(command, "format datetime") == 0) {
app_set_cell_format(state, FORMAT_DATETIME, 0);
} else if (strcmp(command, "format general") == 0 || strcmp(command, "format number") == 0) {
app_set_cell_format(state, FORMAT_GENERAL, 0);
}
// Range formatting commands
else if (strncmp(command, "range format ", 13) == 0) {
if (!state->sheet->selection.is_active) {
strcpy_s(state->status_message, sizeof(state->status_message), "No range selected");
return;
}
const char* format_type = command + 13;
DataFormat format = FORMAT_GENERAL;
FormatStyle style = 0;
if (strcmp(format_type, "percentage") == 0) {
format = FORMAT_PERCENTAGE;
} else if (strcmp(format_type, "currency") == 0) {
format = FORMAT_CURRENCY;
} else if (strcmp(format_type, "date") == 0) {
format = FORMAT_DATE;
style = DATE_STYLE_MM_DD_YYYY;
} else if (strcmp(format_type, "time") == 0) {
format = FORMAT_TIME;
style = TIME_STYLE_12HR;
} else if (strcmp(format_type, "general") == 0) {
format = FORMAT_GENERAL;
}
// Apply formatting to selected range
int min_row = state->sheet->selection.start_row < state->sheet->selection.end_row ?
state->sheet->selection.start_row : state->sheet->selection.end_row;
int max_row = state->sheet->selection.start_row > state->sheet->selection.end_row ?
state->sheet->selection.start_row : state->sheet->selection.end_row;
int min_col = state->sheet->selection.start_col < state->sheet->selection.end_col ?
state->sheet->selection.start_col : state->sheet->selection.end_col;
int max_col = state->sheet->selection.start_col > state->sheet->selection.end_col ?
state->sheet->selection.start_col : state->sheet->selection.end_col;
for (int row = min_row; row <= max_row; row++) {
for (int col = min_col; col <= max_col; col++) {
Cell* cell = sheet_get_or_create_cell(state->sheet, row, col);
if (cell) {
cell_set_format(cell, format, style);
}
}
}
sprintf_s(state->status_message, sizeof(state->status_message),
"Range formatted as %s", format_type);
}
// Color commands for text
else if (strncmp(command, "clrtx ", 6) == 0) {
const char* color_str = command + 6;
int color = parse_color(color_str);
if (color >= 0) {
if (state->sheet->selection.is_active) {
// Apply to range
int min_row = state->sheet->selection.start_row < state->sheet->selection.end_row ?
state->sheet->selection.start_row : state->sheet->selection.end_row;
int max_row = state->sheet->selection.start_row > state->sheet->selection.end_row ?
state->sheet->selection.start_row : state->sheet->selection.end_row;
int min_col = state->sheet->selection.start_col < state->sheet->selection.end_col ?
state->sheet->selection.start_col : state->sheet->selection.end_col;
int max_col = state->sheet->selection.start_col > state->sheet->selection.end_col ?
state->sheet->selection.start_col : state->sheet->selection.end_col;
for (int row = min_row; row <= max_row; row++) {
for (int col = min_col; col <= max_col; col++) {
Cell* cell = sheet_get_or_create_cell(state->sheet, row, col);
if (cell) {
cell_set_text_color(cell, color);
}
}
}
sprintf_s(state->status_message, sizeof(state->status_message),
"Range text color set to %s", color_str);
} else {
// Apply to current cell
Cell* cell = sheet_get_or_create_cell(state->sheet, state->cursor_row, state->cursor_col);
if (cell) {
cell_set_text_color(cell, color);
sprintf_s(state->status_message, sizeof(state->status_message),
"Cell text color set to %s", color_str);
}
}
} else {
sprintf_s(state->status_message, sizeof(state->status_message),
"Invalid color: %s", color_str);
}
}
// Color commands for background
// Chart commands
else if (strncmp(command, "line", 4) == 0) {
char x_label[64] = "X";
char y_label[64] = "Y";
// Parse optional labels
const char* args = command + 4;
while (*args == ' ') args++;
if (*args) {
sscanf_s(args, "%63s %63s", x_label, (unsigned)sizeof(x_label),
y_label, (unsigned)sizeof(y_label));
}
app_show_chart(state, CHART_LINE, x_label, y_label);
}
else if (strncmp(command, "bar", 3) == 0) {