/
dataentry.c
1844 lines (1633 loc) · 49.2 KB
/
dataentry.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
/*
* R : A Computer Langage for Statistical Data Analysis
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998--2007 Robert Gentleman, Ross Ihaka and the
* R Development Core Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* http://www.r-project.org/Licenses/
*/
/* TODO
- spreadsheet copy and paste?
*/
/* Use of strchr here is MBCS-safe */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "win-nls.h"
#include <wchar.h>
#include <R_ext/rlocale.h>
#include "Defn.h"
#include "Print.h"
#include "graphapp/ga.h"
#include "console.h"
#include "consolestructs.h"
#include "rui.h"
typedef enum {UNKNOWNN, NUMERIC, CHARACTER} CellType;
/* Used to check if eventloop needs to be run */
static Rboolean R_de_up;
#ifndef max
#define max(a, b) (((a)>(b))?(a):(b))
#endif
#ifndef min
#define min(a, b) (((a)<(b))?(a):(b))
#endif
#define BOXW(x) (min(((x<100 && DE->nboxchars == 0) ? DE->boxw[x] : DE->box_w), DE->p->w - DE->boxw[0] - 2*DE->bwidth - 2))
#define FIELDWIDTH 10
#define BUFSIZE 200
typedef struct {
dataeditor de;
ConsoleData p;
int box_w; /* width of a box */
int boxw[100]; /* widths of cells */
int box_h; /* height of a box */
int windowWidth; /* current width of the window */
int windowHeight; /* current height of the window */
int currentexp; /* boolean: whether an cell is active */
int crow; /* current row */
int ccol; /* current column */
int nwide, nhigh;
int colmax, colmin, rowmax, rowmin;
int ndecimal; /* count decimal points */
int ne; /* count exponents */
int nneg; /* indicate whether its a negative */
int clength; /* number of characters currently entered */
int inSpecial;
char buf[BUFSIZE];
char *bufp;
int bwidth; /* width of the border */
int hwidth; /* width of header */
int text_xoffset, text_yoffset;
field celledit;
Rboolean CellModified, CellEditable;
int xmaxused, ymaxused;
int oldWIDTH, oldHEIGHT;
int nboxchars;
char labform[6];
int xScrollbarScale, yScrollbarScale;
SEXP work, names, lens;
PROTECT_INDEX wpi, npi, lpi;
menuitem de_mvw;
SEXP ssNA_STRING;
Rboolean isEditor;
} destruct, *DEstruct;
/* Local Function Definitions */
static void advancerect(DEstruct, int);
static void bell(void);
static void cleararea(DEstruct, int, int, int, int, rgb);
static void clearrect(DEstruct);
static void closerect(DEstruct);
static void clearwindow(DEstruct);
static void de_closewin(DEstruct);
static void copyarea(DEstruct, int, int, int, int);
static void copyH(DEstruct, int, int, int);
static void deredraw(DEstruct);
static void downlightrect(DEstruct);
static void drawwindow(DEstruct);
static void drawcol(DEstruct, int);
/* static void de_drawline(int, int, int, int);*/
static void de_drawtext(DEstruct, int, int, const char *);
static void drawrectangle(DEstruct, int, int, int, int, int, int);
static void drawrow(DEstruct, int);
static void find_coords(DEstruct, int, int, int*, int*);
static void handlechar(DEstruct, const char *);
static void highlightrect(DEstruct);
static Rboolean initwin(DEstruct, const char *);
static void jumppage(DEstruct, int);
static void jumpwin(DEstruct, int, int);
static void de_popupmenu(DEstruct, int, int, int);
static void printlabs(DEstruct);
static void printrect(DEstruct, int, int);
static void printstring(DEstruct, const char *, int, int, int, int);
static void printelt(DEstruct, SEXP, int, int, int);
static void setcellwidths(DEstruct);
static dataeditor newdataeditor(DEstruct, const char *);
static void de_copy(control c);
static void de_paste(control c);
static void de_delete(control c);
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h> /* for Sleep */
int mb_char_len(const char *buf, int clength)
{
int i, mb_len = 0;
for(i = 0; i <= clength; i += mb_len)
mb_len = mbrtowc(NULL, buf+i, MB_CUR_MAX, NULL);
return mb_len;
}
static void moveback(DEstruct DE)
{
int mb_len;
if (DE->clength > 0) {
mb_len = mb_char_len(DE->buf, DE->clength-1);
DE->clength -= mb_len;
DE->bufp -= mb_len;
printstring(DE, DE->buf, DE->clength, DE->crow, DE->ccol, 1);
} else bell();
}
/*
Underlying assumptions (for this version R >= 1.8.0)
The data are stored in a list `work', with unused columns having
NULL entries. The names for the list are in `names', which should
have a name for all displayable columns (up to xmaxused).
The *used* lengths of the columns are in `lens': this needs only be
set for non-NULL columns.
If the list was originally length(0), that should work with
0 pre-defined cols. (It used to have 1 pre-defined numeric column.)
All row and col numbers are 1-based.
BDR May 2003
*/
/*
ssNewVector is just an interface to allocVector but it lets us set
the fields to NA. We need to have a special NA for strings so that
we can differentiate between uninitialized elements in the vectors
and user supplied NA's; hence ssNA_STRING
*/
static SEXP ssNewVector(DEstruct DE, SEXPTYPE type, int vlen)
{
SEXP tvec;
int j;
tvec = allocVector(type, vlen);
for (j = 0; j < vlen; j++)
if (type == REALSXP)
REAL(tvec)[j] = NA_REAL;
else if (type == STRSXP)
SET_STRING_ELT(tvec, j, DE->ssNA_STRING);
return (tvec);
}
static void de_closewin_cend(void *DE)
{
de_closewin((DEstruct) DE);
}
SEXP do_dataentry(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP colmodes, tnames, tvec, tvec2, work2;
SEXPTYPE type;
int i, j, cnt, len, nprotect;
RCNTXT cntxt;
char clab[25];
destruct DE1;
DEstruct DE = &DE1;
checkArity(op, args);
DE->isEditor = TRUE;
nprotect = 0;/* count the PROTECT()s */
PROTECT_WITH_INDEX(DE->work = duplicate(CAR(args)), &DE->wpi); nprotect++;
colmodes = CADR(args);
tnames = getAttrib(DE->work, R_NamesSymbol);
if (TYPEOF(DE->work) != VECSXP || TYPEOF(colmodes) != VECSXP)
errorcall(call, G_("invalid argument"));
/* initialize the constants */
DE->bufp = DE->buf;
DE->ne = 0;
DE->currentexp = 0;
DE->nneg = 0;
DE->ndecimal = 0;
DE->clength = 0;
DE->inSpecial = 0;
DE->ccol = 1;
DE->crow = 1;
DE->colmin = 1;
DE->rowmin = 1;
PROTECT(DE->ssNA_STRING = duplicate(NA_STRING));
nprotect++;
DE->bwidth = 0;
DE->hwidth = 5;
/* setup work, names, lens */
DE->xmaxused = length(DE->work); DE->ymaxused = 0;
PROTECT_WITH_INDEX(DE->lens = allocVector(INTSXP, DE->xmaxused), &DE->lpi);
nprotect++;
if (isNull(tnames)) {
PROTECT_WITH_INDEX(DE->names = allocVector(STRSXP, DE->xmaxused),
&DE->npi);
for(i = 0; i < DE->xmaxused; i++) {
sprintf(clab, "var%d", i);
SET_STRING_ELT(DE->names, i, mkChar(clab));
}
} else
PROTECT_WITH_INDEX(DE->names = duplicate(tnames), &DE->npi);
nprotect++;
for (i = 0; i < DE->xmaxused; i++) {
int len = LENGTH(VECTOR_ELT(DE->work, i));
INTEGER(DE->lens)[i] = len;
DE->ymaxused = max(len, DE->ymaxused);
type = TYPEOF(VECTOR_ELT(DE->work, i));
if (LENGTH(colmodes) > 0 && !isNull(VECTOR_ELT(colmodes, i)))
type = str2type(CHAR(STRING_ELT(VECTOR_ELT(colmodes, i), 0)));
if (type != STRSXP) type = REALSXP;
if (isNull(VECTOR_ELT(DE->work, i))) {
if (type == NILSXP) type = REALSXP;
SET_VECTOR_ELT(DE->work, i, ssNewVector(DE, type, 100));
} else if (!isVector(VECTOR_ELT(DE->work, i)))
errorcall(call, G_("invalid type for value"));
else {
if (TYPEOF(VECTOR_ELT(DE->work, i)) != type)
SET_VECTOR_ELT(DE->work, i,
coerceVector(VECTOR_ELT(DE->work, i), type));
}
}
/* scale scrollbars as needed */
if (DE->xmaxused > 10000) DE->xScrollbarScale = DE->xmaxused/1000;
if (DE->ymaxused > 10000) DE->yScrollbarScale = DE->ymaxused/1000;
/* start up the window, more initializing in here */
if (initwin(DE, G_("Data Editor")))
errorcall(call, G_("invalid device"));
R_de_up = TRUE;
/* set up a context which will close the window if there is an error */
begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
R_NilValue, R_NilValue);
cntxt.cend = &de_closewin_cend;
cntxt.cenddata = (void *)DE;
highlightrect(DE);
while (R_de_up) {
/* avoid consuming 100% CPU time here */
Sleep(10);
R_ProcessEvents();
}
endcontext(&cntxt);
/* drop out unused columns */
for(i = 0, cnt = 0; i < DE->xmaxused; i++)
if(!isNull(VECTOR_ELT(DE->work, i))) cnt++;
if (cnt < DE->xmaxused) {
PROTECT(work2 = allocVector(VECSXP, cnt)); nprotect++;
for(i = 0, j = 0; i < DE->xmaxused; i++) {
if(!isNull(VECTOR_ELT(DE->work, i))) {
SET_VECTOR_ELT(work2, j, VECTOR_ELT(DE->work, i));
INTEGER(DE->lens)[j] = INTEGER(DE->lens)[i];
SET_STRING_ELT(DE->names, j, STRING_ELT(DE->names, i));
j++;
}
}
REPROTECT(DE->names = lengthgets(DE->names, cnt), DE->npi);
} else work2 = DE->work;
for (i = 0; i < LENGTH(work2); i++) {
len = INTEGER(DE->lens)[i];
tvec = VECTOR_ELT(work2, i);
if (LENGTH(tvec) != len) {
tvec2 = ssNewVector(DE, TYPEOF(tvec), len);
for (j = 0; j < len; j++) {
if (TYPEOF(tvec) == REALSXP) {
REAL(tvec2)[j] = REAL(tvec)[j];
} else if (TYPEOF(tvec) == STRSXP) {
if (STRING_ELT(tvec, j) != DE->ssNA_STRING)
SET_STRING_ELT(tvec2, j, STRING_ELT(tvec, j));
else
SET_STRING_ELT(tvec2, j, NA_STRING);
} else
error(G_("dataentry: internal memory problem"));
}
SET_VECTOR_ELT(work2, i, tvec2);
}
}
setAttrib(work2, R_NamesSymbol, DE->names);
UNPROTECT(nprotect);
return work2;
}
/* Window Drawing Routines */
static rgb bbg;
static void setcellwidths(DEstruct DE)
{
int i, w, dw;
DE->windowWidth = w = 2*DE->bwidth + DE->boxw[0] + BOXW(DE->colmin);
DE->nwide = 2;
for (i = 2; i < 100; i++) { /* 100 on-screen columns cannot occur */
dw = BOXW(i + DE->colmin - 1);
if((w += dw) > DE->p->w ||
(!DE->isEditor && i > DE->xmaxused - DE->colmin + 1)
) {
DE->nwide = i;
DE->windowWidth = w - dw;
break;
}
}
}
static void drawwindow(DEstruct DE)
{
/* might have resized */
setcellwidths(DE);
DE->nhigh = (DE->p->h - 2 * DE->bwidth - DE->hwidth - 3) / DE->box_h;
if(!DE->isEditor && DE->nhigh > DE->ymaxused+1) DE->nhigh = DE->ymaxused+1;
DE->windowHeight = DE->nhigh * DE->box_h + 2 * DE->bwidth + DE->hwidth;
DE->oldWIDTH = DE->p->w;
DE->oldHEIGHT = DE->p->h;
clearwindow(DE);
deredraw(DE);
/* row/col 1 = pos 0 */
gchangescrollbar(DE->de, VWINSB, (DE->rowmin - 1)/DE->yScrollbarScale,
DE->ymaxused/DE->yScrollbarScale,
max(DE->nhigh/DE->yScrollbarScale, 1), 0);
gchangescrollbar(DE->de, HWINSB, (DE->colmin - 1)/DE->xScrollbarScale,
DE->xmaxused/DE->xScrollbarScale,
max(DE->nwide/DE->xScrollbarScale, 1), 0);
}
static void doHscroll(DEstruct DE, int oldcol)
{
int i, dw;
int oldnwide = DE->nwide, oldwindowWidth = DE->windowWidth;
/* horizontal re-position */
setcellwidths(DE);
DE->colmax = DE->colmin + (DE->nwide - 2);
if (oldcol < DE->colmin) { /* drop oldcol...colmin - 1 */
dw = DE->boxw[0];
for (i = oldcol; i < DE->colmin; i++) dw += BOXW(i);
copyH(DE, dw, DE->boxw[0], oldwindowWidth - dw + 1);
dw = oldwindowWidth - BOXW(oldcol) + 1;
cleararea(DE, dw, DE->hwidth, DE->p->w-dw, DE->p->h, DE->p->bg);
/* oldnwide includes the row labels */
for (i = oldcol+oldnwide-1; i <= DE->colmax; i++) drawcol(DE, i);
} else {
/* move one or more cols left */
dw = BOXW(DE->colmin);
copyH(DE, DE->boxw[0], DE->boxw[0] + dw, DE->windowWidth - dw + 1);
dw = DE->windowWidth + 1;
cleararea(DE, dw, DE->hwidth, DE->p->w-dw, DE->p->h, DE->p->bg);
drawcol(DE, DE->colmin);
}
gchangescrollbar(DE->de, HWINSB, (DE->colmin - 1)/DE->xScrollbarScale,
DE->xmaxused/DE->xScrollbarScale,
max(DE->nwide/DE->xScrollbarScale, 1), 0);
highlightrect(DE);
}
/* find_coords finds the coordinates of the upper left corner of the
given cell on the screen: row and col are on-screen coords */
static void find_coords(DEstruct DE,
int row, int col, int *xcoord, int *ycoord)
{
int i, w;
w = DE->bwidth;
if (col > 0) w += DE->boxw[0];
for(i = 1; i < col; i ++) w += BOXW(i + DE->colmin - 1);
*xcoord = w;
*ycoord = DE->bwidth + DE->hwidth + DE->box_h * row;
}
/* draw the window with the top left box at column wcol and row wrow */
static void jumpwin(DEstruct DE, int wcol, int wrow)
{
if (wcol < 0 || wrow < 0) {
bell();
return;
}
closerect(DE);
if (DE->colmin != wcol || DE->rowmin != wrow) {
DE->colmin = wcol;
DE->rowmin = wrow;
deredraw(DE);
gchangescrollbar(DE->de, VWINSB, (DE->rowmin - 1)/DE->yScrollbarScale,
DE->ymaxused/DE->yScrollbarScale,
max(DE->nhigh/DE->yScrollbarScale, 1), 0);
gchangescrollbar(DE->de, HWINSB, (DE->colmin - 1)/DE->xScrollbarScale,
DE->xmaxused/DE->xScrollbarScale,
max(DE->nwide/DE->xScrollbarScale, 1), 0);
} else highlightrect(DE);
}
static void advancerect(DEstruct DE, int which)
{
/* if we are in the header, changing a name then only down is
allowed */
if (DE->crow < 1 && which != DOWN) {
bell();
return;
}
closerect(DE);
switch (which) {
case UP:
if (DE->crow == 1) {
if (DE->rowmin == 1)
bell();
else
jumppage(DE, UP);
} else
DE->crow--;
break;
case DOWN:
if (DE->crow == (DE->nhigh - 1))
jumppage(DE, DOWN);
else
DE->crow++;
break;
case RIGHT:
if (DE->ccol == (DE->nwide - 1))
jumppage(DE, RIGHT);
else
DE->ccol++;
break;
case LEFT:
if (DE->ccol == 1) {
if (DE->colmin == 1)
bell();
else
jumppage(DE, LEFT);
} else
DE->ccol--;
break;
default:
UNIMPLEMENTED("advancerect");
}
highlightrect(DE);
}
static const char *get_col_name(DEstruct DE, int col)
{
static char clab[25];
if (col <= DE->xmaxused) {
/* don't use NA labels */
SEXP tmp = STRING_ELT(DE->names, col - 1);
if(tmp != NA_STRING) return(CHAR(tmp));
}
sprintf(clab, "var%d", col);
return clab;
}
static int get_col_width(DEstruct DE, int col)
{
int i, w = 0, w1, fw = FIELDWIDTH;
const char *strp;
SEXP tmp, lab;
if (DE->nboxchars > 0) return DE->nboxchars;
if (col <= DE->xmaxused) {
tmp = VECTOR_ELT(DE->work, col - 1);
if (isNull(tmp)) return fw;
/* don't use NA labels */
lab = STRING_ELT(DE->names, col - 1);
if(lab != NA_STRING) w = strlen(CHAR(lab)); else w = fw;
PrintDefaults(R_NilValue);
for (i = 0; i < INTEGER(DE->lens)[col - 1]; i++) {
strp = EncodeElement(tmp, i, 0, '.');
w1 = strlen(strp);
if (w1 > w) w = w1;
}
if(w < 5) w = 5;
if(w < 8) w++;
if(w > 50) w = 50;
return w;
}
return fw;
}
static CellType get_col_type(DEstruct DE, int col)
{
SEXP tmp;
CellType res = UNKNOWNN;
if (col <= DE->xmaxused) {
tmp = VECTOR_ELT(DE->work, col - 1);
if(TYPEOF(tmp) == REALSXP) res = NUMERIC;
if(TYPEOF(tmp) == STRSXP) res = CHARACTER;
}
return res;
}
/* whichcol is absolute col no, col is position on screen */
static void drawcol(DEstruct DE, int whichcol)
{
int i, src_x, src_y, len, col = whichcol - DE->colmin + 1,
bw = BOXW(whichcol);
const char *clab;
SEXP tmp;
find_coords(DE, 0, col, &src_x, &src_y);
cleararea(DE, src_x, src_y, bw, DE->windowHeight, DE->p->bg);
cleararea(DE, src_x, src_y, bw, DE->box_h, bbg);
for (i = 0; i < DE->nhigh; i++)
drawrectangle(DE, src_x, DE->hwidth + i * DE->box_h, bw, DE->box_h, 1, 1);
/* now fill it in if it is active */
clab = get_col_name(DE, whichcol);
printstring(DE, clab, strlen(clab), 0, col, 0);
if (DE->xmaxused >= whichcol) {
tmp = VECTOR_ELT(DE->work, whichcol - 1);
if (!isNull(tmp)) {
len = min(DE->rowmax, INTEGER(DE->lens)[whichcol - 1]);
for (i = (DE->rowmin - 1); i < len; i++)
printelt(DE, tmp, i, i - DE->rowmin + 2, col);
}
}
}
/* whichrow is absolute row no */
static void drawrow(DEstruct DE, int whichrow)
{
int i, src_x, src_y, row = whichrow - DE->rowmin + 1, w;
char rlab[15];
SEXP tvec;
find_coords(DE, row, 0, &src_x, &src_y);
cleararea(DE, src_x, src_y, DE->windowWidth, DE->box_h,
(whichrow > 0) ? DE->p->bg : bbg);
drawrectangle(DE, src_x, src_y, DE->boxw[0], DE->box_h, 1, 1);
sprintf(rlab, DE->labform, whichrow);
printstring(DE, rlab, strlen(rlab), row, 0, 0);
w = DE->bwidth + DE->boxw[0];
for (i = DE->colmin; i <= DE->colmax; i++) {
drawrectangle(DE, w, src_y, BOXW(i), DE->box_h, 1, 1);
w += BOXW(i);
}
for (i = DE->colmin; i <= DE->colmax; i++) {
if (i > DE->xmaxused) break;
if (!isNull(tvec = VECTOR_ELT(DE->work, i - 1)))
if (whichrow <= INTEGER(DE->lens)[i - 1])
printelt(DE, tvec, whichrow - 1, row, i - DE->colmin + 1);
}
}
/* printelt: print the correct value from vector[vrow] into the
spreadsheet in row ssrow and col sscol */
/* WARNING: This has no check that you're not beyond the end of the
vector. Caller must check. */
static void printelt(DEstruct DE, SEXP invec, int vrow, int ssrow, int sscol)
{
const char *strp;
PrintDefaults(R_NilValue);
if (TYPEOF(invec) == REALSXP) {
strp = EncodeElement(invec, vrow, 0, '.');
printstring(DE, strp, strlen(strp), ssrow, sscol, 0);
}
else if (TYPEOF(invec) == STRSXP) {
if (STRING_ELT(invec, vrow) != DE->ssNA_STRING) {
strp = EncodeElement(invec, vrow, 0, '.');
printstring(DE, strp, strlen(strp), ssrow, sscol, 0);
}
}
else
error(G_("dataentry: internal memory error"));
}
static void drawelt(DEstruct DE, int whichrow, int whichcol)
{
int i;
const char *clab;
SEXP tmp;
if (whichrow == 0) {
clab = get_col_name(DE, whichcol + DE->colmin - 1);
printstring(DE, clab, strlen(clab), 0, whichcol, 0);
} else {
if (DE->xmaxused >= whichcol + DE->colmin - 1) {
tmp = VECTOR_ELT(DE->work, whichcol + DE->colmin - 2);
if (!isNull(tmp) && (i = DE->rowmin + whichrow - 2) <
INTEGER(DE->lens)[whichcol + DE->colmin - 2] )
printelt(DE, tmp, i, whichrow, whichcol);
} else
printstring(DE, "", 0, whichrow, whichcol, 0);
}
}
static void jumppage(DEstruct DE, int dir)
{
int i, w, oldcol, wcol;
switch (dir) {
case UP:
DE->rowmin--;
DE->rowmax--;
copyarea(DE, 0, DE->hwidth + DE->box_h, 0, DE->hwidth + 2 * DE->box_h);
drawrow(DE, DE->rowmin);
gchangescrollbar(DE->de, VWINSB, (DE->rowmin - 1)/DE->yScrollbarScale,
DE->ymaxused/DE->yScrollbarScale,
max(DE->nhigh/DE->yScrollbarScale, 1), 0);
break;
case DOWN:
if (DE->rowmax >= 65535) return;
DE->rowmin++;
DE->rowmax++;
copyarea(DE, 0, DE->hwidth + 2 * DE->box_h, 0, DE->hwidth + DE->box_h);
drawrow(DE, DE->rowmax);
gchangescrollbar(DE->de, VWINSB, (DE->rowmin - 1)/DE->yScrollbarScale,
DE->ymaxused/DE->yScrollbarScale,
max(DE->nhigh/DE->yScrollbarScale, 1), 0);
break;
case LEFT:
DE->colmin--;
doHscroll(DE, DE->colmin + 1);
break;
case RIGHT:
oldcol = DE->colmin;
wcol = DE->colmin + DE->ccol + 1; /* column to be selected */
/* There may not be room to fit the next column in */
w = DE->p->w - DE->boxw[0] - BOXW(DE->colmax + 1);
for (i = DE->colmax; i >= oldcol; i--) {
w -= BOXW(i);
if(w < 0) {
DE->colmin = i + 1;
break;
}
}
DE->ccol = wcol - DE->colmin;
doHscroll(DE, oldcol);
break;
}
}
/* draw a rectangle, used to highlight/downlight the current box */
static void printrect(DEstruct DE, int lwd, int fore)
{
int x, y;
find_coords(DE, DE->crow, DE->ccol, &x, &y);
drawrectangle(DE, x + lwd - 1, y + lwd - 1,
BOXW(DE->ccol+DE->colmin-1) - lwd + 1,
DE->box_h - lwd + 1, lwd, fore);
}
static void downlightrect(DEstruct DE)
{
printrect(DE, 2, 0);
printrect(DE, 1, 1);
}
static void highlightrect(DEstruct DE)
{
if(DE->isEditor)
printrect(DE, 2, 1);
else
printrect(DE, 1, 1);
}
static Rboolean getccol(DEstruct DE)
{
SEXP tmp, tmp2;
int i, len, newlen, wcol, wrow;
SEXPTYPE type;
char clab[25];
Rboolean newcol = FALSE;
wcol = DE->ccol + DE->colmin - 1;
wrow = DE->crow + DE->rowmin - 1;
if (wcol > DE->xmaxused) {
/* extend work, names and lens */
REPROTECT(DE->work = lengthgets(DE->work, wcol), DE->wpi);
REPROTECT(DE->names = lengthgets(DE->names, wcol), DE->npi);
for (i = DE->xmaxused; i < wcol; i++) {
sprintf(clab, "var%d", i + 1);
SET_STRING_ELT(DE->names, i, mkChar(clab));
}
REPROTECT(DE->lens = lengthgets(DE->lens, wcol), DE->lpi);
DE->xmaxused = wcol;
}
if (isNull(VECTOR_ELT(DE->work, wcol - 1))) {
newcol = TRUE;
SET_VECTOR_ELT(DE->work, wcol - 1,
ssNewVector(DE, REALSXP, max(100, wrow)));
INTEGER(DE->lens)[wcol - 1] = 0;
}
if (!isVector(tmp = VECTOR_ELT(DE->work, wcol - 1)))
error(G_("internal type error in dataentry"));
len = INTEGER(DE->lens)[wcol - 1];
type = TYPEOF(tmp);
if (len < wrow) {
for (newlen = max(len * 2, 10) ; newlen < wrow ; newlen *= 2)
;
tmp2 = ssNewVector(DE, type, newlen);
for (i = 0; i < len; i++)
if (type == REALSXP)
REAL(tmp2)[i] = REAL(tmp)[i];
else if (type == STRSXP)
SET_STRING_ELT(tmp2, i, STRING_ELT(tmp, i));
else
error(G_("internal type error in dataentry"));
SET_VECTOR_ELT(DE->work, wcol - 1, tmp2);
}
return newcol;
}
/* close up the entry to a cell, put the value that has been entered
into the correct place and as the correct type */
static void closerect(DEstruct DE)
{
SEXP cvec;
int wcol = DE->ccol + DE->colmin - 1, wrow = DE->rowmin + DE->crow - 1,
wrow0;
Rboolean newcol;
*(DE->bufp) = '\0';
if (DE->CellModified || DE->CellEditable) {
if (DE->CellEditable) {
strncpy(DE->buf, GA_gettext(DE->celledit), BUFSIZE-1);
DE->clength = strlen(DE->buf);
hide(DE->celledit);
del(DE->celledit);
}
newcol = getccol(DE);
cvec = VECTOR_ELT(DE->work, wcol - 1);
wrow0 = INTEGER(DE->lens)[wcol - 1];
if (wrow > wrow0) INTEGER(DE->lens)[wcol - 1] = wrow;
DE->ymaxused = max(DE->ymaxused, wrow);
if (DE->clength != 0) {
/* do it this way to ensure NA, Inf, ... can get set */
char *endp;
double new = R_strtod(DE->buf, &endp);
int warn = !isBlankString(endp);
if (TYPEOF(cvec) == STRSXP)
SET_STRING_ELT(cvec, wrow - 1, mkChar(DE->buf));
else
REAL(cvec)[wrow - 1] = new;
if (newcol && warn) {
/* change mode to character */
SEXP tmp = coerceVector(cvec, STRSXP);
SET_STRING_ELT(tmp, wrow - 1, mkChar(DE->buf));
SET_VECTOR_ELT(DE->work, wcol - 1, tmp);
}
} else {
if (TYPEOF(cvec) == STRSXP)
SET_STRING_ELT(cvec, wrow - 1, NA_STRING);
else
REAL(cvec)[wrow - 1] = NA_REAL;
}
drawelt(DE, DE->crow, DE->ccol); /* to get the cell scrolling right */
if(wrow > wrow0) drawcol(DE, wcol); /* to fill in NAs */
}
DE->CellEditable = DE->CellModified = FALSE;
downlightrect(DE);
gsetcursor(DE->de, ArrowCursor);
DE->ndecimal = 0;
DE->nneg = 0;
DE->ne = 0;
DE->currentexp = 0;
DE->clength = 0;
DE->inSpecial = 0;
DE->bufp = DE->buf;
}
/* print a null terminated string, check to see if it is longer than
the print area and print it, left adjusted if necessary; clear the
area of previous text; */
/* This version will only display BUFSIZE chars, but the maximum col width
will not allow that many */
static void printstring(DEstruct DE, const char *ibuf, int buflen,
int row, int col, int left)
{
int x_pos, y_pos, bw, fw, bufw;
char buf[BUFSIZE+1];
find_coords(DE, row, col, &x_pos, &y_pos);
if (col == 0) bw = DE->boxw[0]; else bw = BOXW(col+DE->colmin-1);
cleararea(DE, x_pos + 1, y_pos + 1, bw - 1, DE->box_h - 1,
(row==0 || col==0) ? bbg:DE->p->bg);
fw = min(BUFSIZE, (bw - 8)/(DE->p->fw));
bufw = min(fw, buflen);
strncpy(buf, ibuf, bufw);
buf[bufw] = '\0';
if (buflen > fw) {
if(left) {
strncpy(buf, ibuf + buflen - fw, fw);
buf[fw] = '\0';
*buf = '<';
} else {
*(buf + fw - 1) = '>';
*(buf + fw) = '\0';
}
}
de_drawtext(DE, x_pos + DE->text_xoffset, y_pos - DE->text_yoffset, buf);
}
static void clearrect(DEstruct DE)
{
int x_pos, y_pos;
find_coords(DE, DE->crow, DE->ccol, &x_pos, &y_pos);
cleararea(DE, x_pos, y_pos, BOXW(DE->ccol+DE->colmin-1),
DE->box_h, DE->p->bg);
}
/* handlechar has to be able to parse decimal numbers and strings,
depending on the current column type, only printing characters
should get this far */
/* --- Not true! E.g. ESC ends up in here... */
static void handlechar(DEstruct DE, const char *text)
{
int c = text[0];
if ( c == '\033' ) {
DE->CellModified = FALSE;
DE->clength = 0;
drawelt(DE, DE->crow, DE->ccol);
gsetcursor(DE->de, ArrowCursor);
return;
} else {
DE->CellModified = TRUE;
gsetcursor(DE->de, TextCursor);
}
if (DE->clength == 0) {
switch(get_col_type(DE, DE->ccol + DE->colmin - 1)) {
case NUMERIC:
DE->currentexp = 1;
break;
default:
DE->currentexp = 2;
}
clearrect(DE);
highlightrect(DE);
}
if (DE->currentexp == 1) /* we are parsing a number */
switch (c) {
case '-':
if (DE->nneg == 0)
DE->nneg++;
else
goto donehc;
break;
case '.':
if (DE->ndecimal == 0)
DE->ndecimal++;
else
goto donehc;
break;
case 'e':
case 'E':
if (DE->ne == 0) {
DE->nneg = DE->ndecimal = 0; /* might have decimal in exponent */
DE->ne++;
}
else
goto donehc;
break;
case 'N':
if(DE->nneg) goto donehc;
case 'I':
DE->inSpecial++;
break;
default:
if (!DE->inSpecial && !isdigit((int)text[0]))
goto donehc;
break;
}
if (DE->clength++ > 199) {
warning(G_("dataentry: expression too long"));
DE->clength--;
goto donehc;
}
*(DE->bufp)++ = text[0];
printstring(DE, DE->buf, DE->clength, DE->crow, DE->ccol, 1);
return;
donehc:
bell();
}
static void printlabs(DEstruct DE)
{
char clab[15];
const char *p;
int i;
for (i = DE->colmin; i <= DE->colmax; i++) {
p = get_col_name(DE, i);
printstring(DE, p, strlen(p), 0, i - DE->colmin + 1, 0);
}
for (i = DE->rowmin; i <= DE->rowmax; i++) {
sprintf(clab, DE->labform, i);
printstring(DE, clab, strlen(clab), i - DE->rowmin + 1, 0, 0);
}
}
/* ================ GraphApp-specific ================ */
static void bell(void)
{
gabeep();
}
static void cleararea(DEstruct DE,
int xpos, int ypos, int width, int height, rgb col)
{
gfillrect(DE->de, col, rect(xpos, ypos, width, height));
}
static void clearwindow(DEstruct DE)
{
gfillrect(DE->de, DE->p->bg, rect(0, 0, DE->p->w, DE->p->h));
}
static void drawrectangle(DEstruct DE,
int xpos, int ypos, int width, int height,
int lwd, int fore)
{
/* only used on screen, so always fast */