forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.c
1974 lines (1805 loc) · 65.4 KB
/
screen.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
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
/* SC A Spreadsheet Calculator
* Curses based Screen driver
*
* original by James Gosling, September 1982
* modifications by Mark Weiser and Bruce Israel,
* University of Maryland
* More mods Robert Bond, 12/86
* More mods by Alan Silverstein, 3-4/88, see list of changes.
* updated by Charlie Gordon: June, 2021
*
* $Revision: 9.1 $
*/
#include <time.h>
#include "icurses.h"
#include "sc.h"
static char under_cursor = ' '; /* Data under the < cursor */
char mode_ind = 'i';
char search_ind = ' ';
static int lines, cols, rows;
int lcols;
static int lastmx, lastmy; /* Screen address of the cursor */
static int sc_lastrow, sc_lastcol; /* Spreadsheet row/col the cursor was in last */
int lastendrow = -1; /* Last bottom row of screen */
static int lastftoprows = 0; /* Rows in top of frame cursor was in last */
static int lastfbottomrows = 0; /* Rows in bottom of frame cursor was in last */
static int lastfleftcols = 0; /* Columns in left side of frame cursor was
in last */
static int lastfrightcols = 0;
struct frange *lastfr = 0; /* Last framed range we were in */
static sc_bool_t frTooLarge = 0; /* If set, either too many rows or too many columns
exist in frame to allow room for the scrolling
portion of the framed range */
#ifdef RIGHT_CBUG
static int wasforw = FALSE; /* Causes screen to be redisplay if on lastcol */
#endif
int framerows; /* Rows in current frame */
int framecols; /* Columns in current frame */
int screen_COLS = 80, screen_LINES = 25;
int seenerr;
static int standlast = FALSE;
#ifdef XENIX2_3
struct termio tmio;
#endif
#ifdef VMS
static int VMS_read_raw = 0;
#endif
static void repaint(int x, int y, int len, int attron, int attroff);
static void showstring(sheet_t *sp, const char *string, int align, int hasvalue, int row,
int col, int *nextcolp, int mxcol, int *fieldlenp, int r, int c,
struct frange *fr, int frightcols, int flcols, int frcols);
static int addustr(const char *s) {
int i, pos, res = OK, wc;
for (i = pos = 0;;) {
unsigned char c = s[i];
if (c > 0 && !(c & 0x80)) {
i++;
} else {
res |= addnstr(s + pos, i - pos);
if (!c)
break;
pos = i += utf8_decode(s + pos, &wc);
res |= addnwstr(&wc, 1);
}
}
return res;
}
static int addqstr(const char *s) {
int i, pos, res = OK, wc;
for (i = pos = 0;;) {
unsigned char c = s[i];
if (c > 0 && !(c & 0x80) && c != '"' && c != '\\') {
i++;
} else {
res |= addnstr(s + pos, i - pos);
if (!c)
break;
if (c < 0x80) {
pos = i++;
/* only escape `"`, `\"` and `\\` */
if (c != '\\' || s[i] == '\\' || s[i] == '"')
res |= addch('\\');
} else {
pos = i += utf8_decode(s + pos, &wc);
res |= addnwstr(&wc, 1);
}
}
}
return res;
}
void error(const char *fmt, ...) {
char buf[256];
va_list ap;
va_start(ap, fmt);
// Prevent warning: format string is not a string literal [-Werror,-Wformat-nonliteral]
((int (*)(char *, size_t, const char *, va_list))vsnprintf)(buf, sizeof buf, fmt, ap);
va_end(ap);
if (usecurses) {
/* seenerr is cleared in vi_interaction().
show only a single error per input cycle. */
if (seenerr) return;
seenerr++;
select_style(STYLE_CELL, 0);
screen_clear_line(1);
// XXX: should clip message to screen width
addustr(buf);
} else {
if (*buf) fprintf(stderr, "%s\n", buf);
}
}
void select_style(int n, int rev) {
if (usecurses) {
if (rev) {
if (rev > 0)
standout();
else
standend();
}
if (color && has_colors()) {
attron(COLOR_PAIR(n));
}
}
}
int rows_height(sheet_t *sp, int r, int n) {
int height = 0;
while (n --> 0 /* && r < sp->maxrows */) {
if (!row_hidden(sp, r))
height += 1;
r++;
}
return height;
}
int cols_width(sheet_t *sp, int c, int n) {
int width = 0;
while (n --> 0 /* && c < sp->maxcols */) {
if (!col_hidden(sp, c))
width += col_fwidth(sp, c);
c++;
}
return width;
}
/* update() does general screen update */
void update(sheet_t *sp, int anychanged) { /* did any cell really change in value? */
char field[FBUFLEN];
int row, col;
struct ent *p;
int rescol;
int mxrow, mxcol;
int minsr = 0, minsc = 0, maxsr = 0, maxsc = 0;
int r, i;
struct frange *fr;
struct crange *cr;
int ftoprows, fbottomrows, fleftcols, frightcols;
int ftrows, fbrows, flcols, frcols;
sc_bool_t message;
/*
* If receiving input from a pipeline, don't display spreadsheet data
* on screen.
*/
if (!usecurses) return;
// XXX: this code is a mess. Should at least document if not simplify
getmaxyx(stdscr, lines, cols);
fr = frange_get_current(sp);
if (fr != lastfr) { /* If we've left the previous frame range */
FullUpdate++;
}
ftoprows = fbottomrows = fleftcols = frightcols = 0;
ftrows = fbrows = flcols = frcols = 0;
framerows = framecols = 0;
if (fr) {
ftoprows = fr->irr.left.row - fr->orr.left.row;
ftrows = rows_height(sp, fr->orr.left.row, ftoprows);
fbottomrows = fr->orr.right.row - fr->irr.right.row;
fbrows = rows_height(sp, fr->irr.right.row + 1, fbottomrows);
fleftcols = fr->irr.left.col - fr->orr.left.col;
flcols = cols_width(sp, fr->orr.left.col, fleftcols);
frightcols = fr->orr.right.col - fr->irr.right.col;
frcols = cols_width(sp, fr->irr.right.col + 1, frightcols);
framerows = RESROW + ftrows + fbrows;
framecols = sp->rescol + flcols + frcols;
if (framerows >= lines || framecols >= cols) {
frTooLarge = TRUE;
if (FullUpdate) {
error("Frame too large for screen size - ignoring");
}
ftoprows = fbottomrows = fleftcols = frightcols = 0;
ftrows = fbrows = flcols = frcols = 0;
framerows = framecols = 0;
sp->strow -= lastftoprows;
sp->stcol -= lastfleftcols;
} else {
frTooLarge = FALSE;
if (sp->strow >= fr->orr.left.row) {
if (fr == lastfr && sp->strow < fr->orr.left.row + ftoprows)
sp->strow = fr->orr.left.row;
else if (sp->strow > fr->irr.right.row) {
sp->strow = fr->irr.right.row;
FullUpdate++;
}
}
if (sp->stcol >= fr->orr.left.col) {
if (sp->stcol < fr->orr.left.col + fleftcols)
sp->stcol = fr->orr.left.col;
else if (sp->stcol > fr->irr.right.col) {
sp->stcol = fr->irr.right.col;
FullUpdate++;
}
}
if (fr == lastfr && sp->currow == sc_lastrow)
fbottomrows = lastfbottomrows;
else if (sp->currow < fr->irr.right.row)
fbottomrows = fr->orr.right.row - fr->irr.right.row;
else
fbottomrows = fr->orr.right.row - sp->currow;
if (fr == lastfr && sp->curcol == sc_lastcol)
frightcols = lastfrightcols;
else if (sp->curcol < fr->irr.right.col)
frightcols = fr->orr.right.col - fr->irr.right.col;
else
frightcols = fr->orr.right.col - sp->curcol;
}
fbrows = rows_height(sp, fr->irr.right.row + 1, fbottomrows);
frcols = cols_width(sp, fr->irr.right.col + 1, frightcols);
}
if (fr != lastfr && !gs.stflag && lastfr) {
if (sp->strow >= lastfr->irr.left.row)
sp->strow -= lastftoprows;
if (sp->stcol >= lastfr->irr.left.col)
sp->stcol -= lastfleftcols;
}
/*
* Place the cursor on the screen. Set col, sp->curcol, stcol, sc_lastcol as
* needed. If strow and stcol are negative, centering is forced.
*/
if ((sp->curcol != sc_lastcol) || FullUpdate) {
// XXX: update should not have a side effect on sp->currow/sp->curcol
while (col_hidden(sp, sp->curcol)) /* You can't hide the last row or col */
sp->curcol++;
if (col_fwidth(sp, sp->curcol) > cols - sp->rescol - 2) {
// XXX: update should not have a side effect on the database
error("column %s too wide - resizing", coltoa(sp->curcol));
cmd_format(sp, sp->curcol, sp->curcol, cols - sp->rescol - 2,
sp->colfmt[sp->curcol].precision,
sp->colfmt[sp->curcol].realfmt);
}
/* First see if the last display still covers sp->curcol */
if (sp->stcol >= 0 && sp->stcol <= sp->curcol) {
int c = 0;
if (fr) {
if (fr != lastfr) {
if (sp->stcol == fr->orr.left.col)
sp->stcol += fleftcols;
else if (sp->stcol >= fr->orr.left.col && !gs.stflag) {
sp->stcol += fleftcols;
if (sp->stcol > fr->irr.right.col)
sp->stcol = fr->irr.right.col + 1;
}
} else if (sp->stcol == fr->orr.left.col)
sp->stcol += fleftcols;
}
i = sp->stcol;
lcols = 0;
col = sp->rescol + frcols;
if (fr && sp->stcol >= fr->orr.left.col) {
if (sp->stcol < fr->irr.left.col)
i = fr->orr.left.col;
else
col += flcols;
}
for (; /* i < sp->maxcols && */
(col + col_fwidth(sp, i) < cols - 1 || col_hidden(sp, i) || i < sp->curcol);
i++) {
lcols++;
if (fr && i == fr->irr.right.col + 1) {
col -= frcols;
frcols = frightcols = 0;
}
if (col_hidden(sp, i))
continue;
/* If there isn't room for more columns, and we haven't yet
* reached the current column, start removing columns from
* the left.
*/
while (col + col_fwidth(sp, i) > cols - 2) {
lcols--;
col -= col_fwidth(sp, sp->stcol);
while (col_hidden(sp, ++sp->stcol))
continue;
FullUpdate++;
c++;
}
col += col_fwidth(sp, i);
}
if (!frTooLarge && fr && sp->curcol <= sp->stcol + lcols &&
fr->irr.left.col >= sp->stcol + lcols) {
while (sp->stcol + lcols < fr->irr.left.col) {
col -= col_fwidth(sp, sp->stcol);
lcols--;
while (col_hidden(sp, ++sp->stcol))
lcols--;
while (col + col_fwidth(sp, sp->stcol + lcols) < cols - 1) {
col += col_fwidth(sp, sp->stcol + lcols);
lcols++;
}
}
} else if (c)
sp->stcol = -1;
}
if (!rowsinrange) rowsinrange = 1;
if (!colsinrange) colsinrange = col_fwidth(sp, sp->curcol);
// XXX: should compute goto target area and shift screen?
while (sp->stcol < 0 || sp->curcol < sp->stcol || sp->stcol + lcols - 1 < sp->curcol ||
(colsinrange != col_fwidth(sp, sp->curcol) && sp->stcol != sp->curcol &&
sp->stcol + lcols - 1 < gs.g_rr.right.col)) {
FullUpdate++;
/* How about back one? */
if (sp->stcol - 1 == sp->curcol) {
sp->stcol--;
/* Forward one? */
} else if (sp->stcol >= 0 && sp->stcol + lcols == sp->curcol) {
sp->stcol++;
} else if (sp->stcol >= 0 && fr && sp->curcol >= fr->orr.left.col &&
sp->curcol <= fr->irr.left.col && sp->stcol < sp->curcol &&
sp->curcol <= sp->stcol + lcols + fr->irr.left.col - fr->orr.left.col) {
while ((sp->stcol + lcols < fr->irr.left.col && !frTooLarge) ||
(colsinrange != col_fwidth(sp, sp->curcol) && sp->stcol != sp->curcol &&
sp->stcol + lcols - 1 < gs.g_rr.right.col)) {
if (col_hidden(sp, ++sp->stcol)) lcols--;
}
} else {
/* Try to put the cursor in the center of the screen.
* If we've just jumped to a range using the goto command,
* center the range instead.
*/
if (colsinrange > cols - sp->rescol - flcols - frcols - 2)
colsinrange = cols - sp->rescol - flcols - frcols - 2;
col = (cols - sp->rescol - flcols - frcols - colsinrange) / 2;
sp->stcol = sp->curcol;
for (i = sp->curcol - 1;
i >= (fr ? fr->orr.left.col + fleftcols : 0) &&
(col - col_fwidth(sp, i) > 0 || col_hidden(sp, i));
i--) {
sp->stcol--;
if (col_hidden(sp, i))
continue;
col -= col_fwidth(sp, i);
}
if (fr && sp->stcol < fr->orr.left.col + fleftcols) {
sp->stcol = fr->orr.left.col + fleftcols;
if (sp->curcol < sp->stcol)
sp->stcol = sp->curcol;
}
}
/* Now pick up the counts again */
i = sp->stcol;
lcols = 0;
col = sp->rescol + frcols;
if (fr && sp->stcol >= fr->orr.left.col) {
if (sp->stcol < fr->irr.left.col)
i = fr->orr.left.col;
else
col += flcols;
}
for (; /* i < sp->maxcols && */
(col + col_fwidth(sp, i) < cols-1 || col_hidden(sp, i) || i < sp->curcol);
i++) {
lcols++;
if (fr && i == fr->irr.right.col + 1) {
col -= frcols;
frcols = frightcols = 0;
}
if (col_hidden(sp, i))
continue;
col += col_fwidth(sp, i);
}
}
}
if (fleftcols && sp->stcol >= fr->orr.left.col && sp->stcol < fr->orr.left.col + fleftcols) {
lcols += (fr->orr.left.col - sp->stcol);
sp->stcol = fr->orr.left.col + fleftcols;
if (sp->curcol < sp->stcol)
sp->stcol = sp->curcol;
}
/* Now - same process on the rows as the columns */
if ((sp->currow != sc_lastrow) || FullUpdate) {
// XXX: update should not have a side effect on sp->currow/sp->curcol
while (row_hidden(sp, sp->currow)) /* You can't hide the last row or col */
sp->currow++;
if (sp->strow >= 0 && sp->strow <= sp->currow) {
int c = 0;
if (fr) {
if (fr != lastfr) {
if (sp->strow == fr->orr.left.row)
sp->strow += ftoprows;
else if (sp->strow >= fr->orr.left.row && !gs.stflag) {
sp->strow += ftoprows;
if (sp->strow > fr->irr.right.row)
sp->strow = fr->irr.right.row + 1;
}
} else if (sp->strow == fr->orr.left.row)
sp->strow += ftoprows;
}
i = sp->strow;
rows = 0;
row = RESROW + fbrows;
if (fr && sp->strow >= fr->orr.left.row) {
if (sp->strow < fr->irr.left.row)
i = fr->orr.left.row;
else
row += ftrows;
}
for (; (row < lines || row_hidden(sp, i) || i < sp->currow) /* && i < sp->maxrows */; i++) {
rows++;
if (fr && i == fr->irr.right.row + 1) {
row -= fbrows;
fbrows = fbottomrows = 0;
}
if (row_hidden(sp, i))
continue;
/* If there isn't room for more rows, and we haven't yet
* reached the current row, start removing rows from the
* top.
*/
if (row >= lines) {
rows--;
row--;
while (row_hidden(sp, ++sp->strow))
continue;
FullUpdate++;
c++;
}
row++;
}
if (!frTooLarge && fr && sp->currow <= sp->strow + rows &&
fr->irr.left.row >= sp->strow + rows) {
while (sp->strow + rows < fr->irr.left.row) {
while (row_hidden(sp, ++sp->strow))
continue;
}
} else if (c && sp->currow > lastendrow)
sp->strow = -1;
}
// XXX: this code is bogus
while (sp->strow < 0 || sp->currow < sp->strow || sp->strow + rows - 1 < sp->currow ||
sp->strow + rows < sp->currow + rowsinrange) {
FullUpdate++;
/* How about up one? */
if (sp->strow - 1 == sp->currow) {
sp->strow--;
/* Down one? */
} else if (sp->strow >= 0 && sp->strow + rows == sp->currow) {
sp->strow++;
} else if (sp->strow >= 0 && fr && sp->currow >= fr->orr.left.row &&
sp->currow <= fr->irr.left.row && sp->strow < sp->currow &&
sp->currow <= sp->strow + rows + fr->irr.left.row - fr->orr.left.row) {
while ((sp->strow + rows < fr->irr.left.row && !frTooLarge) ||
(rowsinrange > 1 && sp->strow != sp->currow &&
sp->strow + rows - 1 < gs.g_rr.right.row)) {
if (row_hidden(sp, ++sp->strow)) rows--;
}
} else {
/* Try to put the cursor in the center of the screen.
* If we've just jumped to a range using the goto command,
* center the range instead.
*/
rowsinrange = (rowsinrange > lines - RESROW - ftrows - fbrows ?
lines - RESROW - ftrows - fbrows : rowsinrange);
row = (lines - RESROW - ftrows - fbrows - rowsinrange) / 2;
sp->strow = sp->currow;
for (i = sp->currow - 1;
i >= (fr ? fr->orr.left.row + ftoprows : 0) &&
(row > 0 || row_hidden(sp, i)); i--) {
sp->strow--;
if (row_hidden(sp, i))
continue;
row--;
}
if (fr && sp->strow < fr->orr.left.row + ftoprows) {
sp->strow = fr->orr.left.row + ftoprows;
if (sp->currow < sp->strow)
sp->strow = sp->currow;
}
}
/* Now pick up the counts again */
i = sp->strow;
rows = 0;
row = RESROW + fbrows;
if (fr && sp->strow >= fr->orr.left.row) {
if (sp->strow < fr->irr.left.row)
i = fr->orr.left.row;
else
row += ftrows;
}
for (; (row < lines || row_hidden(sp, i) || i < sp->currow) /* && i < sp->maxrows */; i++) {
rows++;
if (fr && i == fr->irr.right.row + 1) {
row -= fbrows;
fbrows = fbottomrows = 0;
}
if (row_hidden(sp, i))
continue;
row++;
}
}
}
rowsinrange = colsinrange = 0;
if (ftoprows && sp->strow >= fr->orr.left.row && sp->strow < fr->orr.left.row + ftoprows) {
rows += (fr->orr.left.row - sp->strow);
sp->strow = fr->orr.left.row + ftoprows;
if (sp->currow < sp->strow)
sp->strow = sp->currow;
}
mxcol = frightcols ? fr->orr.right.col : sp->stcol + lcols - 1;
mxrow = fbottomrows ? fr->orr.right.row : sp->strow + rows - 1;
gs.stflag = 0;
lastfr = fr;
lastftoprows = ftoprows;
lastfbottomrows = fbottomrows;
lastfleftcols = fleftcols;
lastfrightcols = frightcols;
/* update rescol */
row = mxrow;
for (rescol = 4; row >= 1000; rescol++) {
row /= 10;
}
if (rescol != sp->rescol) {
sp->rescol = rescol;
FullUpdate++;
}
/* Get rid of cursor standout on the cell at previous cursor position */
if (!FullUpdate) {
repaint_cursor(sp, -showcell);
// XXX: check this stuff
move(lastmy, lastmx + col_fwidth(sp, sc_lastcol));
if ((inch() & A_CHARTEXT) == '<')
addch(under_cursor | (inch() & A_ATTRIBUTES));
/* remove the frame cursor */
select_style(STYLE_FRAME, 0);
repaint(lastmx, RESROW - 1, col_fwidth(sp, sc_lastcol), 0/*A_STANDOUT*/, A_COLOR);
repaint(0, lastmy, sp->rescol - 1, 0/*A_STANDOUT*/, A_COLOR);
select_style(STYLE_CELL, 0);
}
sc_lastrow = sp->currow;
sc_lastcol = sp->curcol;
lastendrow = sp->strow + rows;
/* where is the the cursor now? */
lastmy = RESROW;
row = sp->strow;
if (fr && row >= fr->orr.left.row) {
if (row < fr->irr.left.row)
row = fr->orr.left.row;
else
lastmy += ftrows;
}
lastmy += rows_height(sp, row, sp->currow - row);
lastmx = sp->rescol;
col = sp->stcol;
if (fr && col >= fr->orr.left.col) {
if (col < fr->irr.left.col)
col = fr->orr.left.col;
else
lastmx += flcols;
}
lastmx += cols_width(sp, col, sp->curcol - col);
select_style(STYLE_CELL, 0);
if (FullUpdate || standlast) {
move(2, 0);
clrtobot();
/* display the row numbers */
// XXX: this loop is a mess
for (row = RESROW, i = (ftoprows && sp->strow >= fr->orr.left.row ?
fr->orr.left.row : sp->strow);
i <= mxrow; i++) {
if (ftoprows && sp->strow >= fr->orr.left.row && row == RESROW + ftrows)
i = sp->strow < i ? i : sp->strow;
if (fbottomrows && row == lines - fbrows)
i = fr->orr.right.row - fbottomrows + 1;
if (row_hidden(sp, i))
continue;
move(row, 0);
if (i == sp->currow)
select_style(STYLE_FRAME_CUR, 0);
else
select_style(STYLE_FRAME, 0);
printw("%-*d", sp->rescol - 1, i);
row++;
}
#ifdef RIGHT_CBUG
if (wasforw) {
select_style(STYLE_CELL, 0);
screen_rebuild();
wasforw = 0;
}
#endif
/* display the column headings */
select_style(STYLE_FRAME, 0);
move(2, 0);
printw("%*s", sp->rescol, "");
col = sp->rescol;
for (i = (fleftcols && sp->stcol >= fr->orr.left.col) ? fr->orr.left.col : sp->stcol;
i <= mxcol;
i++)
{
const char *colname;
int len, width;
if (fleftcols && sp->stcol >= fr->orr.left.col && col == sp->rescol + flcols)
i = sp->stcol < i ? i : sp->stcol;
if (frightcols && col + col_fwidth(sp, i) >= cols - 1 - frcols && i < fr->orr.right.col - frightcols + 1)
i = fr->orr.right.col - frightcols + 1;
if (col_hidden(sp, i))
continue;
if (i == sp->curcol)
select_style(STYLE_FRAME_CUR, 0);
else
select_style(STYLE_FRAME, 0);
move(2, col);
colname = coltoa(i);
len = strlen(colname);
width = col_fwidth(sp, i);
if (width <= len) {
printw("%s", colname + len - col_fwidth(sp, i));
} else
if (braille) {
printw("%-*s", width, colname);
} else {
int k = (width - len) / 2;
printw("%*s%-*s", k, "", width - k, colname);
}
col += width;
}
select_style(STYLE_CELL, 0);
}
move(1, 0);
message = (inch() & A_CHARTEXT) != ' ';
if (sp->showrange) {
if (sp->showrange == SHOWROWS) {
minsr = sp->showsr < sp->currow ? sp->showsr : sp->currow;
minsc = fr ? fr->orr.left.col : 0;
maxsr = sp->showsr > sp->currow ? sp->showsr : sp->currow;
maxsc = fr ? fr->orr.right.col : sp->maxcols - 1;
if (sp->showtop && !message) {
clrtoeol();
printw("Default range: %d:%d", minsr, maxsr);
}
} else if (sp->showrange == SHOWCOLS) {
minsr = 0;
minsc = sp->showsc < sp->curcol ? sp->showsc : sp->curcol;
maxsr = sp->maxrows - 1;
maxsc = sp->showsc > sp->curcol ? sp->showsc : sp->curcol;
if (sp->showtop && !message) {
clrtoeol();
printw("Default range: %s:%s", coltoa(minsc), coltoa(maxsc));
}
} else {
minsr = sp->showsr < sp->currow ? sp->showsr : sp->currow;
minsc = sp->showsc < sp->curcol ? sp->showsc : sp->curcol;
maxsr = sp->showsr > sp->currow ? sp->showsr : sp->currow;
maxsc = sp->showsc > sp->curcol ? sp->showsc : sp->curcol;
if (sp->showtop && !message) {
clrtoeol();
printw("Default range: %s",
range_addr(sp, rangeref(minsr, minsc, maxsr, maxsc)));
}
}
} else if (braille && braillealt && !message && mode_ind == 'v') {
clrtoeol();
printw("Current cell: %s%d ", coltoa(sp->curcol), sp->currow);
}
/* Repaint the visible screen */
if (sp->showrange || anychanged || FullUpdate || standlast) {
/* may be reset in loop, if not next time we will do a FullUpdate */
if (standlast) {
FullUpdate = TRUE;
standlast = FALSE;
}
for (row = (ftoprows && sp->strow >= fr->orr.left.row ?
fr->orr.left.row : sp->strow), r = RESROW;
row <= mxrow;
row++)
{
int c = sp->rescol;
int do_stand = 0;
int fieldlen;
int nextcol;
int len;
#if 0
if (row < 0 || row >= sp->maxrows) {
fprintf(stderr, "invalid row: %d\n", row);
continue;
}
#endif
if (row_hidden(sp, row))
continue;
if (ftoprows && sp->strow >= fr->orr.left.row && r == RESROW + ftrows)
row = (sp->strow < row ? row : sp->strow);
if (fbottomrows && r == lines - fbrows)
row = fr->orr.right.row - fbottomrows + 1;
for (col = (fleftcols && sp->stcol >= fr->orr.left.col ? fr->orr.left.col : sp->stcol);
col <= mxcol;
col = nextcol, c += fieldlen)
{
if (fleftcols && sp->stcol >= fr->orr.left.col && c == sp->rescol + flcols) {
col = (sp->stcol < col ? col : sp->stcol);
}
if (frightcols && c + col_fwidth(sp, col) >= cols - 1 - frcols && col < fr->orr.right.col - frightcols + 1) {
col = fr->orr.right.col - frightcols + 1;
}
nextcol = col + 1;
fieldlen = 0;
#if 0
if (col < 0 || col >= sp->maxcols) {
fprintf(stderr, "invalid cell: %d.%d\n", col, row);
continue;
}
#endif
if (col_hidden(sp, col))
continue;
fieldlen = col_fwidth(sp, col);
p = getcell(sp, row, col);
select_style(STYLE_CELL, 0);
/*
* Set standout if:
*
* - showing ranges, and not showing cells which need to be filled
* in, and not showing cell expressions, and in a range, OR
*
* - showing cells which need to be filled in and this one is
* of that type (has a value and doesn't have an expression,
* or it is a string expression), OR
*
* - showing cells which have expressions and this one does.
*/
if ((sp->showrange && !showneed && !showexpr
&& (row >= minsr) && (row <= maxsr)
&& (col >= minsc) && (col <= maxsc))
|| (showneed && p && !p->expr && (p->type != SC_EMPTY))
|| (showexpr && p && p->expr)
|| (shownote && p && (p->flags & HAS_NOTE)))
{
move(r, c);
//standout();
if ((cr = crange_find(sp, row, col)))
select_style(cr->color, 0);
else
select_style(STYLE_RANGE, 0);
standlast++;
if (!p) { /* no cell, but standing out */
printw("%*s", fieldlen, "");
select_style(STYLE_CELL, -1);
continue;
} else
do_stand = 1;
} else {
do_stand = 0;
}
if ((cr = crange_find(sp, row, col)))
select_style(cr->color, 0);
if (p && ((p->flags & IS_CHANGED) || FullUpdate || do_stand)) {
if (do_stand) {
p->flags |= IS_CHANGED;
} else {
move(r, c);
p->flags &= ~IS_CHANGED;
}
if (p->type == SC_ERROR) {
// XXX: right alignment is ignored
if (sp->colorerr)
select_style(STYLE_ERROR, 0);
printw("%*.*s", fieldlen, fieldlen, error_name[p->cellerror]);
} else
if (p->expr && showexpr) {
/* Show expression; takes priority over other displays */
decompile(sp, field, sizeof field, p->expr, 0, 0, DCP_DEFAULT);
showstring(sp, field, ALIGN_LEFT, /* hasvalue = */ 0,
row, col, &nextcol, mxcol, &fieldlen, r, c,
fr, frightcols, flcols, frcols);
} else
if (p->type == SC_NUMBER || p->type == SC_BOOLEAN) {
/* Show cell's numeric value: */
int note = (p->flags & HAS_NOTE) != 0;
int align = p->flags & ALIGN_MASK;
if (p->type == SC_BOOLEAN) {
len = pstrcpy(field, sizeof field, boolean_name[!!p->v]);
if (!align)
align = ALIGN_CENTER;
} else {
if (sp->colorneg && p->v < 0) {
if (cr)
select_style(((cr->color) % CPAIRS) + 1, 0);
else
select_style(STYLE_NEG, 0);
}
/* convert cell contents, do not test width, should not align */
*field = '\0';
if (p->format) {
len = format(field, sizeof field, s2c(p->format), sp->colfmt[col].precision, p->v, &align);
} else {
len = engformat(field, sizeof field, sp->colfmt[col].realfmt, sp->colfmt[col].precision, p->v, &align);
}
}
if (align & ALIGN_CLIP) {
align &= ~ALIGN_CLIP;
if (len < 0)
field[len = 0] = '\0';
// XXX: handle ALIGN_CENTER?
if (len > fieldlen)
field[len = fieldlen] = '\0';
}
if (len < 0 || len > fieldlen) {
/* fill column with stars, set the color of
the first one according to note presence */
if (note) {
#ifndef NO_ATTR_GET
attr_t attr;
short curcolor = 0;
if (color && has_colors()) {
/* silence warning */
attr_t *attrp = &attr;
short *curcolorp = &curcolor;
attr_get(attrp, curcolorp, NULL);
select_style(STYLE_NOTE, 0);
}
#endif
addch('*');
#ifndef NO_ATTR_GET
select_style(curcolor, 0);
#endif
}
for (i = note; i < fieldlen; i++) {
addch('*');
}
} else {
int lpad = 0;
int rpad = 0;
if (align == ALIGN_LEFT) {
rpad = fieldlen - len;
} else
if (align == ALIGN_CENTER) {
lpad = (fieldlen - len) / 2;
rpad = fieldlen - len - lpad;
} else {
lpad = fieldlen - len;
}
if (note) {
if (lpad)
lpad--;
else
if (rpad)
rpad--;
else
note = 0;
}
for (i = 0; i < lpad; i++)
addch(' ');
if (note) {
#ifndef NO_ATTR_GET
attr_t attr;
short curcolor = 0;
if (color && has_colors()) {
/* silence warning */
attr_t *attrp = &attr;
short *curcolorp = &curcolor;
attr_get(attrp, curcolorp, NULL);
select_style(STYLE_NOTE, 0);
}
#endif
addch('*');
#ifndef NO_ATTR_GET
select_style(curcolor, 0);
#endif
}
addustr(field);
for (i = 0; i < rpad; i++)
addch(' ');
}
} else
if (p->type == SC_STRING) {
/* Show cell's label string: */
// XXX: should handle notes too */
showstring(sp, s2str(p->label),
p->flags & ALIGN_MASK,
/* hasvalue = */ 0,
row, col, &nextcol, mxcol, &fieldlen,
r, c, fr, frightcols, flcols, frcols);
} else /* repaint a blank cell: XXX: too complicated */
if ((((do_stand || !FullUpdate) && (p->flags & IS_CHANGED)) ||
(cr && cr->color != 1)))
{
printw("%*s", fieldlen, "");
}
} else
if (!p && cr && cr->color != 1) {
move(r, c);
select_style(cr->color, 0);
printw("%*s", fieldlen, "");
}
select_style(STYLE_CELL, 0);
if (do_stand) {
//standend();
do_stand = 0;
}
}
r++;
}
}
/* place 'cursor marker' */
if (showcell && !showneed && !showexpr && !shownote) {
repaint_cursor(sp, showcell);
}
/* highlite the frame cursor */
select_style(STYLE_FRAME_CUR, 0);
repaint(lastmx, RESROW - 1, col_fwidth(sp, sc_lastcol), 0, A_COLOR | A_STANDOUT);
repaint(0, lastmy, sp->rescol - 1, 0, A_COLOR | A_STANDOUT);
select_style(STYLE_CELL, 0);
move(lastmy, lastmx + col_fwidth(sp, sc_lastcol));
under_cursor = (inch() & A_CHARTEXT);
if (!showcell)
addch('<' | (inch() & A_ATTRIBUTES));
move(0, 0);
clrtoeol();
if (linelim >= 0) {
// XXX: display the current edit line
// XXX: should scroll line to make cursor position visible
// XXX: should clip the line to the visible portion
int ctlchars = 0;
for (i = 0; i < linelim; i++) {
if ((unsigned char)line[i] < ' ')
ctlchars++;
}
addch(mode_ind);
addch('>');
addch(search_ind);
addustr(line);
if (!braille || (!message && mode_ind != 'v'))
move((linelim + 3 + ctlchars) / cols, (linelim + 3 + ctlchars) % cols);
else if (message)
move(1, 0);
else if (braillealt)
move(1, 16);
else