/
devga.c
2092 lines (1843 loc) · 58.7 KB
/
devga.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--2000 Guido Masarotto and Brian Ripley
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*--- Device Driver for Windows; this file started from
* ../unix/X11/devX11.c --
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "Defn.h"
#include "Graphics.h"
#include <stdio.h>
#include "opt.h"
#include "graphapp/ga.h"
#include "graphapp/stdimg.h"
#include "console.h"
#include "rui.h"
#include "devga.h" /* 'Public' routines from here */
#include "windows.h"
extern console RConsole;
extern int AllDevicesKilled;
/********************************************************/
/* This device driver has been documented so that it be */
/* used as a template for new drivers */
/********************************************************/
#define MM_PER_INCH 25.4 /* mm -> inch conversion */
#define TRACEDEVGA(a)
#define NOBM(a) if(xd->kind==SCREEN){a;}
#define CLIP if (xd->clip.width>0) gsetcliprect(_d,xd->clip)
#define DRAW(a) {drawing _d=xd->gawin;CLIP;a;NOBM(_d=xd->bm;CLIP;a;)}
#define SHOW gbitblt(xd->gawin,xd->bm,pt(0,0),getrect(xd->bm));
/********************************************************/
/* Each driver can have its own device-specic graphical */
/* parameters and resources. these should be wrapped */
/* in a structure (like the gadesc structure below) */
/* and attached to the overall device description via */
/* the dd->deviceSpecific pointer */
/* NOTE that there are generic graphical parameters */
/* which must be set by the device driver, but are */
/* common to all device types (see Graphics.h) */
/* so go in the GPar structure rather than this device- */
/* specific structure */
/********************************************************/
enum DeviceKinds {SCREEN=0, PRINTER, METAFILE, PNG, JPEG, BMP};
typedef struct {
/* R Graphics Parameters */
/* local device copy so that we can detect */
/* when parameter changes */
int col; /* Color */
int fg; /* Foreground */
int bg; /* Background */
int fontface; /* Typeface */
int fontsize, basefontsize; /* Size in points */
double fontangle;
/* X11 Driver Specific */
/* parameters with copy per x11 device */
enum DeviceKinds kind;
int windowWidth; /* Window width (pixels) */
int windowHeight; /* Window height (pixels) */
int resize; /* Window resized */
window gawin; /* Graphics window */
/*FIXME: we should have union for this stuff and
maybe change gawin to canvas*/
/* SCREEN section*/
popup locpopup, grpopup;
button stoploc;
menubar mbar, mbarloc;
menu msubsave;
menuitem mpng, mbmp, mjpeg50, mjpeg75, mjpeg100;
menuitem mps, mwm, mclpbm, mclpwm, mprint, mclose;
menuitem mrec, madd, mreplace, mprev, mnext, mclear, msvar, mgvar;
int recording, replaying, needsave;
bitmap bm;
/* PNG and JPEG section */
FILE *fp;
int quality;
/* Used to rescale font size so that bitmap devices have 72dpi */
int truedpi, wanteddpi;
rgb fgcolor; /* Foreground color */
rgb bgcolor; /* Background color */
rect clip; /* The clipping rectangle */
int usefixed;
font fixedfont;
font font;
int locator, clicked, px, py;
int lty, lwd;
} gadesc;
/********************************************************/
/* There are a number of actions that every device */
/* driver is expected to perform (even if, in some */
/* cases it does nothing - just so long as it doesn't */
/* crash !). this is how the graphics engine interacts */
/* with each device. ecah action will be documented */
/* individually. */
/* hooks for these actions must be set up when the */
/* device is first created */
/********************************************************/
/* Device Driver Actions */
static void GA_Activate(DevDesc *);
static void GA_Circle(double, double, int, double, int, int, DevDesc*);
static void GA_Clip(double, double, double, double, DevDesc*);
static void GA_Close(DevDesc*);
static void GA_Deactivate(DevDesc *);
static void GA_Hold(DevDesc*);
static void GA_Line(double, double, double, double, int, DevDesc*);
static int GA_Locator(double*, double*, DevDesc*);
static void GA_Mode(int);
static void GA_NewPage(DevDesc*);
static int GA_Open(DevDesc*, gadesc*, char*, double, double);
static void GA_Polygon(int, double*, double*, int, int, int, DevDesc*);
static void GA_Polyline(int, double*, double*, int, DevDesc*);
static void GA_Rect(double, double, double, double, int, int, int, DevDesc*);
static void GA_Resize(DevDesc*);
static double GA_StrWidth(char*, DevDesc*);
static void GA_Text(double, double, int, char*, double, double, DevDesc*);
static void GA_MetricInfo(int, double*, double*, double*, DevDesc*);
/********************************************************/
/* end of list of required device driver actions */
/********************************************************/
/* Support Routines */
static double pixelHeight(drawing d);
static double pixelWidth(drawing d);
static void SetColor(int, DevDesc *);
static void SetFont(int, int, double, DevDesc *);
static void SetLinetype(int, double, DevDesc *);
static int Load_Rbitmap_Dll();
void UnLoad_Rbitmap_Dll();
static void SaveAsPng(DevDesc *dd,char *fn);
static void SaveAsJpeg(DevDesc *dd,int quality,char *fn);
static void SaveAsBmp(DevDesc *dd,char *fn);
static void SaveAsBitmap(DevDesc *dd);
void R_ProcessEvents();
static void PrivateCopyDevice(DevDesc *dd,DevDesc *ndd, char *name)
{
gadesc *xd = (gadesc *) dd->deviceSpecific;
gsetcursor(xd->gawin, WatchCursor);
gsetVar(install(".Device"),
mkString(name), R_NilValue);
addDevice(ndd);
initDisplayList(ndd);
ndd->displayList = dd->displayList;
ndd->dpSaved = dd->dpSaved;
playDisplayList(ndd);
KillDevice(ndd);
gsetcursor(xd->gawin, ArrowCursor);
show(xd->gawin);
}
static void SaveAsWin(DevDesc *dd, char *display)
{
DevDesc *ndd = (DevDesc *) malloc(sizeof(DevDesc));
if (!ndd) {
R_ShowMessage("No enough memory to copy graphics window");
return;
}
ndd->displayList = R_NilValue;
GInit(&ndd->dp);
if (GADeviceDriver(ndd, display,
GConvertXUnits(1.0, NDC, INCHES, dd),
GConvertYUnits(1.0, NDC, INCHES, dd),
dd->gp.ps))
PrivateCopyDevice(dd, ndd, display);
}
static void SaveAsPostscript(DevDesc *dd, char *fn)
{
SEXP s = findVar(install(".PostScript.Options"), R_GlobalEnv);
DevDesc *ndd = (DevDesc *) malloc(sizeof(DevDesc));
char family[256], paper[256], bg[256], fg[256];
if (!ndd) {
R_ShowMessage("No enough memory to copy graphics window");
return;
}
ndd->displayList = R_NilValue;
GInit(&ndd->dp);
/* Set default values... */
strcpy(family, "Helvetica");
strcpy(paper, "default");
strcpy(bg, "white");
strcpy(fg, "black");
/* and then try to get it from .PostScript.Options */
if ((s!=R_UnboundValue) && (s!=R_NilValue)) {
SEXP names = getAttrib(s, R_NamesSymbol);
int i,done;
for (i=0, done=0; (done<4) && (i<length(s)) ; i++) {
if(!strcmp("family", CHAR(STRING(names)[i]))) {
strcpy(family, CHAR(STRING(VECTOR(s)[i])[0]));
done += 1;
}
if(!strcmp("paper", CHAR(STRING(names)[i]))) {
strcpy(paper, CHAR(STRING(VECTOR(s)[i])[0]));
done += 1;
}
if(!strcmp("bg", CHAR(STRING(names)[i]))) {
strcpy(bg, CHAR(STRING(VECTOR(s)[i])[0]));
done += 1;
}
if(!strcmp("fg", CHAR(STRING(names)[i]))) {
strcpy(fg, CHAR(STRING(VECTOR(s)[i])[0]));
done += 1;
}
}
}
if (PSDeviceDriver(ndd, fn, paper, family, bg, fg,
GConvertXUnits(1.0, NDC, INCHES, dd),
GConvertYUnits(1.0, NDC, INCHES, dd),
(double)0, dd->gp.ps, 0, 1, 0, ""))
/* horizontal=F, onefile=F, pagecentre=T, print.it=F */
PrivateCopyDevice(dd, ndd, "postscript");
}
/* Pixel Dimensions (Inches) */
static double pixelWidth(drawing obj)
{
return ((double) 1) / devicepixelsx(obj);
}
static double pixelHeight(drawing obj)
{
return ((double) 1) / devicepixelsy(obj);
}
/* Font information array. */
/* Point sizes: 6-24 */
/* Faces: plain, bold, oblique, bold-oblique */
/* Symbol may be added later */
#define NFONT 19
#define MAXFONT 32
static int fontnum;
static int fontinitdone = 0;
static char *fontname[MAXFONT];
static int fontstyle[MAXFONT];
static void RStandardFonts()
{
int i;
for (i = 0; i < 4; i++)
fontname[i] = "Times New Roman";
fontname[4] = "Symbol";
fontstyle[0] = fontstyle[4] = Plain;
fontstyle[1] = Bold;
fontstyle[2] = Italic;
fontstyle[3] = BoldItalic;
fontnum = 5;
fontinitdone = 2; /* =fontinit done & fonname must not be
free-ed */
}
static void RFontInit()
{
int i, notdone;
char *opt[2];
char oops[256];
sprintf(oops, "%s/Rdevga", getenv("R_USER"));
notdone = 1;
fontnum = 0;
fontinitdone = 1;
if (!optopenfile(oops)) {
sprintf(oops, "%s/etc/Rdevga", getenv("R_HOME"));
if (!optopenfile(oops)) {
RStandardFonts();
notdone = 0;
}
}
while (notdone) {
oops[0] = '\0';
notdone = optread(opt, ':');
if (notdone == 1)
sprintf(oops, "[%s] Error at line %d.", optfile(), optline());
else if (notdone == 2) {
fontname[fontnum] = winstrdup(opt[0]);
if (!fontname[fontnum])
strcpy(oops, "Insufficient memory. ");
else {
if (!strcmpi(opt[1], "plain"))
fontstyle[fontnum] = Plain;
else if (!strcmpi(opt[1], "bold"))
fontstyle[fontnum] = Bold;
else if (!strcmpi(opt[1], "italic"))
fontstyle[fontnum] = Italic;
else if (!strcmpi(opt[1], "bold&italic"))
fontstyle[fontnum] = BoldItalic;
else
sprintf(oops, "Unknown style at line %d. ", optline());
fontnum += 1;
}
}
if (oops[0]) {
optclosefile();
strcat(oops, optfile());
strcat(oops, " will be ignored.");
R_ShowMessage(oops);
for (i = 0; i < fontnum; i++) free(fontname);
RStandardFonts();
notdone = 0;
}
if (fontnum == MAXFONT) {
optclosefile();
notdone = 0;
}
}
}
static int SetBaseFont(gadesc *xd)
{
xd->fontface = 1;
xd->fontsize = xd->basefontsize;
xd->fontangle = 0.0;
xd->usefixed = 0;
xd->font = gnewfont(xd->gawin, fontname[0], fontstyle[0],
MulDiv(xd->fontsize, xd->wanteddpi, xd->truedpi), 0.0);
if (!xd->font) {
xd->usefixed = 1;
xd->font = xd->fixedfont = FixedFont;
if (!xd->fixedfont)
return 0;
}
return 1;
}
/* Set the font size and face */
/* If the font of this size and at that the specified */
/* rotation is not present it is loaded. */
/* 0 = plain text, 1 = bold */
/* 2 = oblique, 3 = bold-oblique */
#define SMALLEST 8
#define LARGEST 64
static void SetFont(int face, int size, double rot, DevDesc *dd)
{
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (face < 1 || face > fontnum)
face = 1;
size = MulDiv(size,xd->wanteddpi,xd->truedpi);
if (size < SMALLEST)
size = SMALLEST;
if (size > LARGEST)
size = LARGEST;
if (!xd->usefixed &&
(size != xd->fontsize || face != xd->fontface ||
rot != xd->fontangle)) {
del(xd->font); doevent();
xd->font = gnewfont(xd->gawin,
fontname[face - 1], fontstyle[face - 1],
size, rot);
if (xd->font) {
xd->fontface = face;
xd->fontsize = size;
xd->fontangle = rot;
} else {
SetBaseFont(xd);
}
}
}
static void SetColor(int color, DevDesc *dd)
{
int r, g, b;
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (color != xd->col) {
/* Gamma Correction */
/* This is very experimental! */
xd->col = color;
if (dd->gp.gamma != 1) {
r = (int) (255 * pow(R_RED(color) / 255.0, dd->gp.gamma));
g = (int) (255 * pow(R_GREEN(color) / 255.0, dd->gp.gamma));
b = (int) (255 * pow(R_BLUE(color) / 255.0, dd->gp.gamma));
} else {
r = R_RED(color);
g = R_GREEN(color);
b = R_BLUE(color);
}
xd->fgcolor = rgb(r, g, b);
}
}
/*
* Some Notes on Line Textures
*
* Line textures are stored as an array of 4-bit integers within
* a single 32-bit word. These integers contain the lengths of
* lines to be drawn with the pen alternately down and then up.
* The device should try to arrange that these values are measured
* in points if possible, although pixels is ok on most displays.
*
* If newlty contains a line texture description it is decoded
* as follows:
*
* ndash = 0;
* for(i=0 ; i<8 && newlty&15 ; i++) {
* dashlist[ndash++] = newlty&15;
* newlty = newlty>>4;
* }
* dashlist[0] = length of pen-down segment
* dashlist[1] = length of pen-up segment
* etc
*
* An integer containing a zero terminates the pattern. Hence
* ndash in this code fragment gives the length of the texture
* description. If a description contains an odd number of
* elements it is replicated to create a pattern with an
* even number of elements. (If this is a pain, do something
* different its not crucial).
*
* 27/5/98 Paul - change to allow lty and lwd to interact:
* the line texture is now scaled by the line width so that,
* for example, a wide (lwd=2) dotted line (lty=2) has bigger
* dots which are more widely spaced. Previously, such a line
* would have "dots" which were wide, but not long, nor widely
* spaced.
*/
static void SetLinetype(int newlty, double nlwd, DevDesc *dd)
{
gadesc *xd = (gadesc *) dd->deviceSpecific;
int newlwd;
newlwd = nlwd;
if (newlwd < 1)
newlwd = 1;
xd->lty = newlty;
xd->lwd = newlwd;
}
/* Callback functions */
static void HelpResize(window w,rect r)
{
if (AllDevicesKilled) return;
{
DevDesc *dd = (DevDesc *) getdata(w);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (r.width) {
if ((xd->windowWidth != r.width) ||
((xd->windowHeight != r.height))) {
xd->windowWidth = r.width;
xd->windowHeight = r.height;
xd->resize = 1;
}
}
}
}
static void HelpClose(window w)
{
if (AllDevicesKilled) return;
{
DevDesc *dd = (DevDesc *) getdata(w);
KillDevice(dd);
}
}
static void HelpExpose(window w,rect r)
{
if (AllDevicesKilled) return;
{
DevDesc *dd = (DevDesc *) getdata(w);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (xd->resize) {
dd->dp.resize(dd);
xd->replaying = 1;
playDisplayList(dd);
xd->replaying = 0;
R_ProcessEvents();
} else
SHOW;
}
}
static void HelpMouseClick(window w, int button, point pt)
{
if (AllDevicesKilled) return;
{
DevDesc *dd = (DevDesc *) getdata(w);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (!xd->locator)
return;
if (button & LeftButton) {
gabeep();
xd->clicked = 1;
xd->px = pt.x;
xd->py = pt.y;
} else
xd->clicked = 2;
}
}
static void menustop(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (!xd->locator)
return;
xd->clicked = 2;
}
void fixslash(char *);
static void menufilebitmap(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
char *fn;
if (m==xd->mpng) {
setuserfilter("Png files (*.png)\0*.png\0\0");
fn = askfilesave("Portable network graphics file", "");
} else if (m==xd->mbmp) {
setuserfilter("Windows bitmap files (*.bmp)\0*.bmp\0\0");
fn = askfilesave("Windows bitmap file", "");
} else {
setuserfilter("Jpeg files (*.jpeg,*jpg)\0*.jpeg;*.jpg\0\0");
fn = askfilesave("Jpeg file", "");
}
if (!fn) return;
fixslash(fn);
gsetcursor(xd->gawin, WatchCursor);
show(xd->gawin);
if (m==xd->mpng) SaveAsPng(dd, fn);
else if (m==xd->mbmp) SaveAsBmp(dd, fn);
else if (m==xd->mjpeg50) SaveAsJpeg(dd, 50, fn);
else if (m==xd->mjpeg75) SaveAsJpeg(dd, 75, fn);
else SaveAsJpeg(dd, 100, fn);
gsetcursor(xd->gawin, ArrowCursor);
show(xd->gawin);
}
static void menups(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
char *fn;
setuserfilter("Postscript files (*.ps)\0*.ps\0All files (*.*)\0*.*\0\0");
fn = askfilesave("Postscript file", "");
if (!fn) return;
fixslash(fn);
SaveAsPostscript(dd,fn);
}
static void menuwm(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
char display[512], *fn;
setuserfilter("Enhanced metafiles (*.emf)\0*.emf\0All files (*.*)\0*.*\0\0");
fn = askfilesave("Enhanced metafiles", "");
if (!fn) return;
fixslash(fn);
sprintf(display, "win.metafile:%s", fn);
SaveAsWin(dd,display);
}
static void menuclpwm(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
SaveAsWin(dd,"win.metafile");
}
static void menuclpbm(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
show(xd->gawin);
gsetcursor(xd->gawin, WatchCursor);
copytoclipboard(xd->bm);
gsetcursor(xd->gawin, ArrowCursor);
}
static void menuprint(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
SaveAsWin(dd,"win.print");
}
static void menuclose(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
HelpClose(xd->gawin);
}
/* plot history */
void copyGPar(GPar *, GPar *);
extern SEXP savedDisplayList;
extern GPar savedGPar;
#define GROWTH 4
#define GETDL SEXP vDL=findVar(install(".SavedPlots"), R_NilValue)
#define SETDL gsetVar(install(".SavedPlots"), vDL, R_NilValue)
#define PLOTHISTORYMAGIC 31415
#define pMAGIC (INTEGER(VECTOR(vDL)[0])[0])
#define pNUMPLOTS (INTEGER(VECTOR(vDL)[1])[0])
#define pMAXPLOTS (INTEGER(VECTOR(vDL)[2])[0])
#define pCURRENTPOS (INTEGER(VECTOR(vDL)[3])[0])
#define pHISTORY (VECTOR(vDL)[4])
#define pCURRENT (VECTOR(pHISTORY)[pCURRENTPOS])
#define pCURRENTdl (VECTOR(pCURRENT)[0])
#define pCURRENTgp (INTEGER(VECTOR(pCURRENT)[1]))
#define pCHECK if ((TYPEOF(vDL)!=VECSXP)||\
(TYPEOF(VECTOR(vDL)[0])!=INTSXP) ||\
(LENGTH(VECTOR(vDL)[0])!=1) ||\
(pMAGIC != PLOTHISTORYMAGIC)) {\
R_ShowMessage("Plot history seems corrupted");\
return;}
#define pMOVE(a) {pCURRENTPOS+=a;\
if(pCURRENTPOS<0) pCURRENTPOS=0;\
if(pCURRENTPOS>pNUMPLOTS-1) pCURRENTPOS=pNUMPLOTS-1;\
Replay(dd,vDL);SETDL;}
#define pEXIST ((vDL!=R_UnboundValue) && (vDL!=R_NilValue))
#define pMUSTEXIST if(!pEXIST){R_ShowMessage("No plot history!");return;}
static SEXP NewPlotHistory(int n)
{
SEXP vDL;
int i;
PROTECT(vDL = allocVector(VECSXP, 5));
for (i = 0; i < 4; i++)
PROTECT(VECTOR(vDL)[i] = allocVector(INTSXP, 1));
PROTECT(pHISTORY = allocVector(VECSXP, n));
pMAGIC = PLOTHISTORYMAGIC;
pNUMPLOTS = 0;
pMAXPLOTS = n;
pCURRENTPOS = -1;
for (i = 0; i < n; i++)
VECTOR(pHISTORY)[i] = R_NilValue;
SETDL;
UNPROTECT(6);
return vDL;
}
static SEXP GrowthPlotHistory(SEXP vDL)
{
SEXP vOLD;
int i, oldNPlots, oldCurrent;
PROTECT(vOLD = pHISTORY);
oldNPlots = pNUMPLOTS;
oldCurrent = pCURRENTPOS;
PROTECT(vDL = NewPlotHistory(pMAXPLOTS + GROWTH));
for (i = 0; i < oldNPlots; i++)
VECTOR(pHISTORY)[i] = VECTOR(vOLD)[i];
pNUMPLOTS = oldNPlots;
pCURRENTPOS = oldCurrent;
SETDL;
UNPROTECT(2);
return vDL;
}
static void AddtoPlotHistory(SEXP dl,GPar *gp,int replace)
{
SEXP nDL;
int lGPar;
int where;
GETDL;
if (dl == R_NilValue) {
R_ShowMessage("Display list is void!");
return;
}
if (!pEXIST)
vDL = NewPlotHistory(GROWTH);
else if (!replace && (pNUMPLOTS == pMAXPLOTS))
vDL = GrowthPlotHistory(vDL);
PROTECT(vDL);
pCHECK;
if (replace)
where = pCURRENTPOS;
else
where = pNUMPLOTS;
PROTECT(dl);
PROTECT(nDL = allocVector(VECSXP, 2));
VECTOR(nDL)[0] = dl;
lGPar = 1 + sizeof(GPar) / sizeof(int);
VECTOR(nDL)[1] = allocVector(INTSXP, lGPar);
copyGPar(gp, (GPar *) INTEGER(VECTOR(nDL)[1]));
VECTOR(pHISTORY)[where] = nDL;
pCURRENTPOS = where;
if (!replace) pNUMPLOTS += 1;
SETDL;
UNPROTECT(3);
}
static void Replay(DevDesc *dd,SEXP vDL)
{
gadesc *xd = (gadesc *) dd->deviceSpecific;
xd->replaying = 1;
dd->displayList = pCURRENTdl;
gsetcursor(xd->gawin, WatchCursor);
copyGPar((GPar *) pCURRENTgp, &dd->dpSaved);
playDisplayList(dd);
xd->replaying = 0;
if (!dd->displayListOn)
initDisplayList(dd);
gsetcursor(xd->gawin, ArrowCursor);
}
static void menurec(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (xd->recording) {
xd->recording = 0;
uncheck(m);
} else {
xd->recording = 1;
check(m);
}
}
static void menuadd(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
AddtoPlotHistory(dd->displayList, &dd->dpSaved, 0);
xd->needsave = 0;
}
static void menureplace(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
GETDL;
pMUSTEXIST;
pCHECK;
if (pCURRENTPOS < 0) {
R_ShowMessage("No plot to replace!");
return;
}
AddtoPlotHistory(dd->displayList, &dd->dpSaved, 1);
}
static void menunext(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
GETDL;
if (xd->needsave) return;
pMUSTEXIST;
pCHECK;
if (pCURRENTPOS!=(pNUMPLOTS - 1)) pMOVE(1);
}
static void menuprev(control m)
{
DevDesc *dd = (DevDesc*) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
GETDL;
pMUSTEXIST;
pCHECK;
if (pNUMPLOTS) {
if (xd->recording && xd->needsave && (dd->displayList != R_NilValue)) {
AddtoPlotHistory(dd->displayList, &dd->dpSaved, 0);
xd->needsave = 0;
}
pMOVE((xd->needsave) ? 0 : -1);
}
}
static void menuclear(control m)
{
gsetVar(install(".SavedPlots"), R_NilValue, R_NilValue);
}
static void menugvar(control m)
{
SEXP vDL;
char *v = askstring("Name", "");
DevDesc *dd = (DevDesc *) getdata(m);
if (!v)
return;
vDL = findVar(install(v), R_GlobalEnv);
if (!pEXIST || !pNUMPLOTS) {
R_ShowMessage("Variable doesn't exist or doesn't contain any plots!");
return;
}
pCHECK;
pCURRENTPOS = 0;
Replay(dd, vDL);
SETDL;
}
static void menusvar(control m)
{
char *v;
GETDL;
pMUSTEXIST;
pCHECK;
v = askstring("Name", "");
if (!v)
return;
setVar(install(v), vDL, R_GlobalEnv);
}
static void menuconsole(control m)
{
show(RConsole);
}
static void CHelpKeyIn(control w,int key)
{
DevDesc *dd = (DevDesc *) getdata(w);
gadesc *xd = (gadesc *) dd->deviceSpecific;
switch (key) {
case INS:
menuadd(xd->madd);
break;
case PGUP:
menuprev(xd->mprev);
break;
case PGDN:
menunext(xd->mnext);
break;
}
}
static void NHelpKeyIn(control w,int key)
{
DevDesc *dd = (DevDesc *) getdata(w);
gadesc *xd = (gadesc *) dd->deviceSpecific;
if (ggetkeystate() != CtrlKey)
return;
key = 'A' + key - 1;
if (key == 'C')
menuclpbm(xd->mclpbm);
if (dd->displayList == R_NilValue)
return;
if (key == 'W')
menuclpwm(xd->mclpwm);
if (key == 'P')
menuprint(xd->mprint);
}
static void mbarf(control m)
{
DevDesc *dd = (DevDesc *) getdata(m);
gadesc *xd = (gadesc *) dd->deviceSpecific;
GETDL;
if (pEXIST) {
enable(xd->mnext);
enable(xd->mprev);
if ((pCURRENTPOS >= 0) && (dd->displayList != R_NilValue))
enable(xd->mreplace);
else
disable(xd->mreplace);
enable(xd->msvar);
enable(xd->mclear);
} else {
disable(xd->mnext);
disable(xd->mprev);
disable(xd->mreplace);
disable(xd->msvar);
disable(xd->mclear);
}
if (dd->displayList != R_NilValue) {
enable(xd->madd);
enable(xd->mprint);
enable(xd->mpng);
enable(xd->mbmp);
enable(xd->mjpeg50);
enable(xd->mjpeg75);
enable(xd->mjpeg100);
enable(xd->mwm);
enable(xd->mps);
enable(xd->mclpwm);
enable(xd->mclpbm);
} else {
disable(xd->madd);
disable(xd->mprint);
disable(xd->msubsave);
disable(xd->mpng);
disable(xd->mbmp);
disable(xd->mjpeg50);
disable(xd->mjpeg75);
disable(xd->mjpeg100);
disable(xd->mwm);
disable(xd->mps);
disable(xd->mclpwm);
disable(xd->mclpbm);
}
draw(xd->mbar);
}
/********************************************************/
/* device_Open is not usually called directly by the */
/* graphics engine; it is usually only called from */
/* the device-driver entry point. */
/* this function should set up all of the device- */
/* specific resources for a new device */
/* this function is given a new structure for device- */
/* specific information AND it must FREE the structure */
/* if anything goes seriously wrong */
/* NOTE that it is perfectly acceptable for this */
/* function to set generic graphics parameters too */
/* (i.e., override the generic parameter settings */
/* which GInit sets up) all at the author's own risk */
/* of course :) */
/********************************************************/
#define MCHECK(m) {if(!(m)) {del(xd->gawin); return 0;}}
static int setupScreenDevice(DevDesc *dd, gadesc *xd, int w, int h)
{
menu m;
int iw, ih;
double dw, dh, d;
xd->kind = SCREEN;