forked from geoffmoss0/VisiCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.c
271 lines (250 loc) · 6.16 KB
/
cursor.c
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
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include "data.h"
#include "cursor.h"
#include "layout.h"
#include "functions.h"
//These don't really need to be static, I'm just a slave to my java habits
//Ok don't yell at me I just didn't feel like throwing all these values around
static int col; //The column of the cursor
static int row; //The row of the cursor
static int x; //The on-screen position of the actual cursor
static int y; // -----------------------------------------
static int max_x; //bounds of the screen
static int max_y; //(how big it is)
static int corner_row; //the row and column in the upper left corner
static int corner_col; //---------------------------------------
static int entry_size;
static cell (*table)[64];
///Called by the driver file, sets up the layout and sets all various x and y values
///Calls input(), which handles all user input
void start() {
draw_size = 8;
draw_screenyx();
//will need to be fixed once I implement column sizing
draw_axes(1, 1);
corner_row = 1;
corner_col = 1;
//draw_screenyx();
refresh();
//char cursor[9] = " ";
//draw_axes(20, 20);
move(4, 3);
printw(" ");
move(4, 3);
x = 3;
y = 4;
row = 1;
col = 1;
getmaxyx(curscr, max_y, max_x);
entry_size = max_x - 12;
refresh();
cell *tab = init_table();
table = (cell (*)[64])tab;
input();
char ch = getch();
endwin();
}
void set_icon(int row, int col) {
color_on();
char *letters = to_char(col - 1);
//printw("%u", strlen(letters));
move(0, 1);
if (col < 27) {
printw(" ");
}
printw("%s", letters);
free(letters);
printw("%d ", row); //padding to make sure any other numbers are overwritten
//TODO add the indicator for <L> (label), <V> (value), etc
move(0, 6);
if (table[row-1][col-1] != NULL) {
printw("<");
if (table[row-1][col-1]->contents < 2)
printw("V");
else
printw("L");
printw(">");
} else {
printw(" ");
}
//TODO replace this with the data entry for the next thing, clear the rest
char *inside = get_raw(row, col, entry_size, &table);
if (inside == NULL) {
for (int i = 9; i < max_x; i++) {
printw(" ");
}
} else {
printw(" %s", inside);
for (int i = 11; i < max_x; i++) {
printw(" ");
}
}
color_off();
}
///Gets entry when anything besides the arrow keys are typed
///Handles screen sizing automatically, will not scroll past size of screen
void entry(int ch) {
char *entry_line = calloc(entry_size, sizeof(char));
color_on();
int typed = 0;
move(2, 0);
if (ch >= 32 && ch <= 122) {
color_off();
for (int i = 1; i < max_x; i++) {
printw(" ");
}
move(2, 1);
printw("%c", ch);
entry_line[typed] = ch;
typed++;
}
while((ch = getch()) != 10) {
if (ch == 127) {
if (typed > 0) {
move(2, typed);
printw(" ");
move(2, typed);
entry_line[typed] = 0;
typed--;
}
} else if (ch == 27){
free(entry_line);
return;
} else {
if (typed < entry_size && ch <= 122 && ch >= 32) {
printw("%c", ch);
entry_line[typed] = ch;
typed++;
} else if (ch == '\033') {
//flushinp();
getch(); //clearing out arrow key notation
getch();
}
}
}
move(2, 0);
for (int i = 0; i < max_x; i++) {
printw(" ");
}
set_icon(row, col);
move(y, x);
set_data(entry_line, row, col, table);
fill_in(y, x, row, col);
set_icon(row, col);
free(entry_line);
}
///Draw the cursor at the new location with the data inside
void fill_in(int y, int x, int row, int col) {
color_on();
move(y, x);
char *print = print_data(row, col, draw_size, table);
printw("%s", print);
free(print);
move(y, x);
}
//Rewrite the cell that just had the cursor whlie keeping the data
void refill(int y, int x, int row, int col) {
color_off();
move(y, x);
char *print = print_data(row, col, draw_size, table);
printw("%s", print);
free(print);
move(y, x);
color_on();
}
void input() {
int ch;
while((ch = getch()) != '\0') {
if (ch == '\033') {
getch(); //Arrow keys are in the form of [[A, this clears out the second bracket
int real = getch();
if (real == 'A') { //up
if (row > 1 && row > corner_row) {
refill(y, x, row, col);
move(y-1, x);
row--;
y--;
//printw(" ");
fill_in(y, x, row, col);
set_icon(row, col); //This should have been a one time thing
move(y, x); //But it broke everything when I tried it
} else if(corner_row > 1) {
draw_axes(corner_row-1, corner_col);
draw_cells(corner_row-1, corner_col, max_y, max_x, draw_size, &table);
row--;
corner_row--;
set_icon(row, col);
move(y, x);
fill_in(y, x, row, col);
}
} else if(real == 'B') { //down
if (y < max_y - 1) {
refill(y, x, row, col);
move(y+1, x);
row++;
y++;
fill_in(y, x, row, col);
set_icon(row, col);
move(y, x);
} else if (row < 254) {
draw_axes(corner_row + 1, corner_col);
draw_cells(corner_row + 1, corner_col, max_y, max_x, draw_size, &table);
row++;
corner_row++;
fill_in(y, x, row, col);
set_icon(row, col);
move(y, x);
//TODO
}
} else if (real == 'C') { //right
if (x <= max_x - (2 * draw_size)) {
refill(y, x, row, col);
move(y, x+draw_size);
col++;
x+=draw_size;
fill_in(y, x, row, col);
set_icon(row, col);
move(y, x);
} else if(col < 63) {
draw_axes(corner_row, corner_col + 1);
draw_cells(corner_row, corner_col + 1, max_y, max_x, draw_size, &table);
col++;
corner_col++;
set_icon(row, col);
fill_in(y, x, row, col);
move(y, x);
}
} else if (real == 'D') { //left
if (x >= draw_size) {
refill(y, x, row, col);
move(y, x-draw_size);
//printw(" ");
col--;
x-=draw_size;
set_icon(row, col);
fill_in(y, x, row, col);
move(y, x);
} else if(col > 1) {
draw_axes(corner_row, corner_col - 1);
draw_cells(corner_row, corner_col - 1, max_y, max_x, draw_size, &table);
col--;
corner_col--;
set_icon(row, col);
fill_in(y, x, row, col);
move(y, x);
}
}
} else if (ch <= 127) {
entry(ch);
//refill(y, x);
}
}
}
void main(char *argc, char **argv) {
initscr();
refresh();
setup();
start();
}