-
Notifications
You must be signed in to change notification settings - Fork 78
/
dataentry.c
2617 lines (2303 loc) · 70 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 Language for Statistical Data Analysis
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998--2022 The R 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
* https://www.R-project.org/Licenses/
*/
/* <UTF8>
Used XTextWidth and XDrawText, so need to use fontsets
Also needed input context.
*/
/* The version for R 2.1.0 is partly based on patches by
Ei-ji Nakama <nakama@ki.rim.or.jp> for use with Japanese fonts. */
#define DPRINTS(x) printf(#x "=[%s]\n", x)
#define DPRINTX(x) printf(#x "=%x\n", x)
#define DPRINTD(x) printf(#x "=%d\n", x)
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define R_USE_SIGNALS 1
#include <Defn.h>
#include <stdlib.h>
#include <Rinternals.h>
#include <R_ext/Parse.h> /* parsing is used in handling escape codes */
#ifndef _Xconst
#define _Xconst const
#endif
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/Intrinsic.h>
#include <Print.h>
/* For the input handlers of the event loop mechanism: */
#include <R_ext/eventloop.h>
#include <R_ext/RS.h> /* for CallocCharBuf */
# define USE_FONTSET 1
/* In theory we should do this, but it works less well
# ifdef X_HAVE_UTF8_STRING
# define HAVE_XUTF8TEXTEXTENTS 1
# define HAVE_XUTF8DRAWSTRING 1
# define HAVE_XUTF8DRAWIMAGESTRING 1
# endif */
#ifndef HAVE_KEYSYM
#define KeySym int
#endif
#define DEEvent XEvent
typedef enum { UP, DOWN, LEFT, RIGHT } DE_DIRECTION;
typedef enum {UNKNOWNN, NUMERIC, CHARACTER} CellType;
/* EXPORTS : */
SEXP in_RX11_dataentry(SEXP call, SEXP op, SEXP args, SEXP rho);
/* Global variables needed for the graphics */
static Display *iodisplay = NULL;
static XContext deContext;
static int nView = 0; /* number of open data windows */
static int fdView = -1;
typedef struct {
Window iowindow;
GC iogc;
XFontStruct *font_info;
SEXP work, names, lens;
PROTECT_INDEX wpi, npi, lpi;
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 fullwindowWidth;
int windowHeight; /* current height of the window */
int fullwindowHeight;
int crow; /* current row */
int ccol; /* current column */
int nwide, nhigh;
int colmax, colmin, rowmax, rowmin;
int bwidth; /* width of the border */
int hht; /* height of header */
int text_offset;
int nboxchars;
int xmaxused, ymaxused;
char labform[15]; // increased from 6 to pacify gcc 8
Rboolean isEditor;
Atom prot;
} destruct, *DEstruct;
/* Local Function Definitions */
static void advancerect(DEstruct, DE_DIRECTION);
static int CheckControl(DEEvent *);
static int CheckShift(DEEvent *);
static int checkquit(int);
static void clearrect(DEstruct);
static void closerect(DEstruct);
static void clearwindow(DEstruct);
static void closewin(DEstruct);
static void copycell(DEstruct);
static void doControl(DEstruct, DEEvent*);
static int doMouseDown(DEstruct, DEEvent*);
static void doSpreadKey(DEstruct, int, DEEvent*);
static void downlightrect(DEstruct);
static void drawwindow(DEstruct);
static void drawcol(DEstruct, int);
static void drawrow(DEstruct, int);
static void eventloop(DEstruct);
static void find_coords(DEstruct, int, int, int*, int*);
static int findcell(DEstruct);
static char *GetCharP(DEEvent*);
static KeySym GetKey(DEEvent*);
static void handlechar(DEstruct, char *);
static void highlightrect(DEstruct);
static Rboolean initwin(DEstruct, const char *);
static void jumppage(DEstruct, DE_DIRECTION);
static void jumpwin(DEstruct, int, int);
static void pastecell(DEstruct, int, int);
static void popdownmenu(DEstruct);
static void popupmenu(DEstruct, int, 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 RefreshKeyboardMapping(DEEvent*);
static void cell_cursor_init(DEstruct);
/* Functions to hide Xlib calls */
static void bell(void);
static void cleararea(DEstruct, int, int, int, int);
static void copyH(DEstruct, int, int, int);
static void copyarea(DEstruct, int, int, int, int);
static void doConfigure(DEstruct, DEEvent *ioevent);
static void drawrectangle(DEstruct, int, int, int, int, int, int);
static void drawtext(DEstruct, int, int, char*, int);
static void RefreshKeyboardMapping(DEEvent *ioevent);
static void Rsync(DEstruct);
static int textwidth(DEstruct, const char*, int);
static int WhichEvent(DEEvent ioevent);
static void R_ProcessX11Events(void *data);
static const char *get_col_name(DEstruct, int col);
static int get_col_width(DEstruct, int col);
static CellType get_col_type(DEstruct, int col);
static void calc_pre_edit_pos(DEstruct DE);
static int last_wchar_bytes(char *);
static SEXP ssNewVector(SEXPTYPE, int);
static SEXP ssNA_STRING;
/* only used in the editor */
static Atom _XA_WM_PROTOCOLS = 0;
static Window menuwindow, menupanes[4];
static Rboolean CellModified;
static int box_coords[6];
static int currentexp; /* whether an cell is active */
static int ndecimal; /* count decimal points */
static int ne; /* count exponents */
static int nneg; /* indicate whether its a negative */
static int clength; /* number of characters currently entered */
static int inSpecial;
#define BOOSTED_BUF_SIZE 201
static char buf[BOOSTED_BUF_SIZE]; /* boosted to allow for MBCS */
static char *bufp;
static char copycontents[sizeof(buf)+1] ;
/* The next few and used only for the editor in MBCS locales */
static Status status;
static XFontSet font_set = NULL;
static XFontStruct **fs_list;
static int font_set_cnt;
static char fontset_name[]="-*-fixed-medium-r-*-*-*-120-*-*-*-*-*-*";
static XIM ioim;
static XIMStyle ioim_style;
static XIMStyles *ioim_styles;
/*
* XIM:
* OverTheSpot XIMPreeditPosition | XIMStatusArea;
* OffTheSpot XIMPreeditArea | XIMStatusArea;
* Root XIMPreeditNothing | XIMStatusNothing;
*/
static XIMStyle preedit_styles[] = {
XIMPreeditPosition,
XIMPreeditArea,
XIMPreeditNothing,
XIMPreeditNone,
(XIMStyle)NULL,
};
static XIMStyle status_styles[] = {
XIMStatusArea,
XIMStatusNothing,
XIMStatusNone,
(XIMStyle)NULL,
};
static XIC ioic = NULL;
#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->fullwindowWidth-DE->boxw[0]-2*DE->bwidth-2))
/*
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
*/
/*
The spreadsheet function returns a list of vectors. The types of
these vectors can be specified by the user as can their names. It
the names are specified they are set during initialization. The
user can change these via a menu interface, they can also change
the type.
The vectors are created too long and if they need to be increased
this is done by using the next higher power of 2. They start 100
long. Vectors are initialized to NA when they are created so that
NA is returned for any cell that was not set by the user. We use
a special type of NA to distinguish this from user-supplied NAs.
In Macintosh we needed to call the main event loop to get
events. This ensures that the spreadsheet interacts well with the
other windows. Under X windows we let the window manager handle
those sorts of details.
*/
static char *menu_label[] =
{
" Real",
" Character",
"Change Name ",
};
/*
ssNewVector is just an interface to allocVector but it lets us
set the fields to NA. We need to have a special NA for reals and
strings so that we can differentiate between uninitialized elements
in the vectors and user supplied NA's; hence ssNA_STRING
*/
static SEXP ssNewVector(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, ssNA_STRING);
return (tvec);
}
static void closewin_cend(void *data)
{
DEstruct DE = (DEstruct) data;
closewin(DE);
}
SEXP in_RX11_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];
char *title = "R Data Editor";
destruct DE1;
DEstruct DE = &DE1;
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, "invalid argument");
/* initialize the constants */
bufp = buf;
ne = 0;
currentexp = 0;
nneg = 0;
ndecimal = 0;
clength = 0;
inSpecial = 0;
DE->ccol = 1;
DE->crow = 1;
DE->colmin = 1;
DE->rowmin = 1;
PROTECT(ssNA_STRING = duplicate(NA_STRING));
nprotect++;
DE->bwidth = 5;
DE->hht = 30;
DE->isEditor = TRUE;
/* 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(type, 100));
} else if (!isVector(VECTOR_ELT(DE->work, i)))
errorcall(call, "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));
}
}
/* start up the window, more initializing in here */
if (initwin(DE, title))
errorcall(call, "unable to start data editor");
/* 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 = &closewin_cend;
cntxt.cenddata = (void *) DE;
highlightrect(DE);
cell_cursor_init(DE);
eventloop(DE);
endcontext(&cntxt);
closewin(DE);
if(nView == 0) {
if(fdView >= 0) { /* might be open after viewers, but unlikely */
removeInputHandler(&R_InputHandlers,
getInputHandler(R_InputHandlers,fdView));
fdView = -1;
}
if(font_set) {
XFreeFontSet(iodisplay, font_set);
font_set = NULL;
}
XCloseDisplay(iodisplay);
iodisplay = NULL;
}
/* 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(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) != ssNA_STRING)
SET_STRING_ELT(tvec2, j, STRING_ELT(tvec, j));
else
SET_STRING_ELT(tvec2, j, NA_STRING);
} else
error("dataentry: internal memory problem");
}
SET_VECTOR_ELT(work2, i, tvec2);
}
}
setAttrib(work2, R_NamesSymbol, DE->names);
UNPROTECT(nprotect);
return work2;
}
static void dv_closewin_cend(void *data)
{
DEstruct DE = (DEstruct) data;
R_ReleaseObject(DE->lens);
R_ReleaseObject(DE->work);
closewin(DE);
free(DE);
nView--;
}
SEXP in_R_X11_dataviewer(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP stitle;
SEXPTYPE type;
int i, nprotect;
RCNTXT cntxt;
// FIXME: this should be checked
DEstruct DE = (DEstruct) malloc(sizeof(destruct));
if(!DE) error("allocation failed in in_R_X11_dataviewer");
nView++;
nprotect = 0;/* count the PROTECT()s */
DE->work = CAR(args);
DE->names = getAttrib(DE->work, R_NamesSymbol);
if (TYPEOF(DE->work) != VECSXP)
errorcall(call, "invalid argument");
stitle = CADR(args);
if (!isString(stitle) || LENGTH(stitle) != 1)
errorcall(call, "invalid argument");
/* initialize the constants */
bufp = buf;
ne = 0;
currentexp = 0;
nneg = 0;
ndecimal = 0;
clength = 0;
inSpecial = 0;
DE->ccol = 1;
DE->crow = 1;
DE->colmin = 1;
DE->rowmin = 1;
DE->bwidth = 5;
DE->hht = 10;
DE->isEditor = FALSE;
/* setup work, names, lens */
DE->xmaxused = length(DE->work); DE->ymaxused = 0;
PROTECT_WITH_INDEX(DE->lens = allocVector(INTSXP, DE->xmaxused), &DE->lpi);
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 (type != STRSXP && type != REALSXP)
errorcall(call, "invalid argument");
}
/* start up the window, more initializing in here */
if (initwin(DE, CHAR(STRING_ELT(stitle, 0))))
errorcall(call, "unable to start data viewer");
/* 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 = &dv_closewin_cend;
cntxt.cenddata = (void *) DE;
highlightrect(DE);
cell_cursor_init(DE);
if(fdView < 0) {
fdView = ConnectionNumber(iodisplay);
addInputHandler(R_InputHandlers, fdView,
R_ProcessX11Events, XActivity);
}
drawwindow(DE);
R_PreserveObject(DE->work); /* also preserves names */
R_PreserveObject(DE->lens);
UNPROTECT(nprotect);
return R_NilValue;
}
/* Window Drawing Routines */
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->fullwindowWidth ||
(!DE->isEditor && i > DE->xmaxused - DE->colmin + 1)) {
DE->nwide = i;
DE->windowWidth = w - dw;
break;
}
}
}
static void drawwindow(DEstruct DE)
{
int i, st;
XWindowAttributes attribs;
/* if there is an active cell enter the data in it */
/*
* case colname input Expose not use.
* closerect();
*/
/* now set up the window with the new dimensions */
XGetWindowAttributes(iodisplay, DE->iowindow, &attribs);
DE->bwidth = attribs.border_width;
DE->fullwindowWidth = attribs.width;
DE->fullwindowHeight = attribs.height;
setcellwidths(DE);
DE->nhigh = (DE->fullwindowHeight - 2 * DE->bwidth - DE->hht) / DE->box_h;
DE->windowHeight = DE->nhigh * DE->box_h + 2 * DE->bwidth;
clearwindow(DE);
for (i = 1; i < DE->nhigh; i++)
drawrectangle(DE, 0, DE->hht + i * DE->box_h, DE->boxw[0], DE->box_h,
1, 1);
/* so row 0 and col 0 are reserved for labels */
DE->colmax = DE->colmin + (DE->nwide - 2);
DE->rowmax = DE->rowmin + (DE->nhigh - 2);
printlabs(DE);
for (i = DE->colmin; i <= DE->colmax; i++) drawcol(DE, i);
if(DE->isEditor) {
/* draw the quit etc boxes */
i = textwidth(DE, "Quit", 4);
box_coords[0] = st = DE->fullwindowWidth - 6 - DE->bwidth;
box_coords[1] = st - i;
drawrectangle(DE, st - i, 3, i + 4, DE->hht - 6, 1, 1);
drawtext(DE, st + 2 - i, DE->hht - 7, "Quit", 4);
box_coords[4] = st = st - 5*i;
i = textwidth(DE, "Paste", 5);
box_coords[5] = st - i;
drawrectangle(DE, st - i, 3, i + 4, DE->hht - 6, 1, 1);
drawtext(DE, st + 2 - i, DE->hht - 7, "Paste", 5);
box_coords[2] = st = st - 2*i;
i = textwidth(DE, "Copy", 4);
box_coords[3] = st - i;
drawrectangle(DE, st - i, 3, i + 4, DE->hht - 6, 1, 1);
drawtext(DE, st + 2 - i, DE->hht - 7, "Copy", 4);
}
highlightrect(DE);
Rsync(DE);
}
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->hht, DE->fullwindowWidth-dw,
DE->fullwindowHeight);
/* 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->hht, DE->fullwindowWidth-dw,
DE->fullwindowHeight);
drawcol(DE, DE->colmin);
}
highlightrect(DE);
cell_cursor_init(DE);
Rsync(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->hht + 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;
closerect(DE);
drawwindow(DE);
} else highlightrect(DE);
}
static void advancerect(DEstruct DE, DE_DIRECTION 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->isEditor && DE->crow+DE->rowmin > DE->ymaxused) {
bell();
break;
}
if (DE->crow == (DE->nhigh - 1))
jumppage(DE, DOWN);
else
DE->crow++;
break;
case RIGHT:
if (!DE->isEditor && DE->ccol+DE->colmin > DE->xmaxused) {
bell();
break;
}
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);
cell_cursor_init(DE);
}
static void cell_cursor_init(DEstruct DE)
{
int i, whichrow = DE->crow + DE->rowmin - 1,
whichcol = DE->ccol + DE->colmin -1;
SEXP tmp;
memset(buf,0,sizeof(buf));
if (DE->crow == 0 ){
strncpy(buf,
get_col_name(DE, whichcol),
BOOSTED_BUF_SIZE-1);
} else {
if (length(DE->work) >= whichcol) {
tmp = VECTOR_ELT(DE->work, whichcol - 1);
if (tmp != R_NilValue &&
(i = whichrow - 1) < LENGTH(tmp) ) {
PrintDefaults();
if (TYPEOF(tmp) == REALSXP) {
strncpy(buf, EncodeElement(tmp, i, 0, '.'),
BOOSTED_BUF_SIZE-1);
} else if (TYPEOF(tmp) == STRSXP) {
if (STRING_ELT(tmp, i) != ssNA_STRING)
strncpy(buf, EncodeElement(tmp, i, 0, '.'),
BOOSTED_BUF_SIZE-1);
}
}
}
}
buf[BOOSTED_BUF_SIZE-1] = '\0';
clength = (int) strlen(buf);
bufp = buf + clength;
}
static const char *get_col_name(DEstruct DE, int col)
{
static char clab[25];
int nwrote;
if (col <= DE->xmaxused) {
/* don't use NA labels */
SEXP tmp = STRING_ELT(DE->names, col - 1);
if(tmp != NA_STRING) return(CHAR(tmp));
}
nwrote = snprintf(clab, 25, "var%d", col);
if (nwrote >= 25)
error("get_col_name: column number too big to stringify");
return (const char *)clab;
}
static int get_col_width(DEstruct DE, int col)
{
int i, w = 0, w1;
const char *strp;
SEXP tmp, lab;
if (DE->nboxchars > 0) return DE->box_w;
if (col <= DE->xmaxused) {
tmp = VECTOR_ELT(DE->work, col - 1);
if (isNull(tmp)) return DE->box_w;
/* don't use NA labels */
lab = STRING_ELT(DE->names, col - 1);
if(lab != NA_STRING) strp = CHAR(lab); else strp = "var12";
PrintDefaults();
w = textwidth(DE, strp, (int) strlen(strp));
for (i = 0; i < INTEGER(DE->lens)[col - 1]; i++) {
strp = EncodeElement(tmp, i, 0, '.');
w1 = textwidth(DE, strp, (int) strlen(strp));
if (w1 > w) w = w1;
}
if(w < 0.5*DE->box_w) w = (int) (0.5*DE->box_w);
if(w < 0.8*DE->box_w) w+= (int) (0.1*DE->box_w);
if(w > 600) w = 600;
return w+8;
}
return DE->box_w;
}
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);
for (i = 0; i < DE->nhigh; i++)
drawrectangle(DE, src_x, DE->hht + 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, (int) 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);
}
}
Rsync(DE);
}
/* 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);
drawrectangle(DE, src_x, src_y, DE->boxw[0], DE->box_h, 1, 1);
sprintf(rlab, DE->labform, whichrow);
printstring(DE, rlab, (int)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);
}
Rsync(DE);
}
/* 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();
if (TYPEOF(invec) == REALSXP) {
strp = EncodeElement(invec, vrow, 0, '.');
printstring(DE ,strp, (int) strlen(strp), ssrow, sscol, 0);
}
else if (TYPEOF(invec) == STRSXP) {
if (STRING_ELT(invec, vrow) != ssNA_STRING) {
strp = EncodeElement(invec, vrow, 0, '.');
printstring(DE ,strp, (int) strlen(strp), ssrow, sscol, 0);
}
}
else
error("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, (int) 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);
}
Rsync(DE);
}
static void jumppage(DEstruct DE, DE_DIRECTION dir)
{
int i, w, oldcol, wcol;
switch (dir) {
case UP:
DE->rowmin--;
DE->rowmax--;
copyarea(DE, 0, DE->hht + DE->box_h, 0, DE->hht + 2 * DE->box_h);
drawrow(DE, DE->rowmin);
break;
case DOWN:
if (DE->rowmax >= 65535) return;
DE->rowmin++;
DE->rowmax++;
copyarea(DE, 0, DE->hht + 2 * DE->box_h, 0, DE->hht + DE->box_h);
drawrow(DE, DE->rowmax);
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->fullwindowWidth - 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);
Rsync(DE);
}
static void downlightrect(DEstruct DE)
{
printrect(DE, 2, 0);
printrect(DE, 1, 1);
}
static void highlightrect(DEstruct DE)