The R Project SVN R

Rev

Rev 9237 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9237 Rev 19110
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1999, 2000  Guido Masarotto
3
 *  Copyright (C) 1999, 2000  Guido Masarotto
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
18
 */
19
 
19
 
20
/* Support for printer
20
/* Support for printer
21
 *  printer newprinter()  - return a printer object - to draw to the
21
 *  printer newprinter()  - return a printer object - to draw to the
22
 *                          printer use drawto(...) and the drawXXX
22
 *                          printer use drawto(...) and the drawXXX
23
 *                          functions + nextpage(printer)
23
 *                          functions + nextpage(printer)
24
 *                          printers can be deleted by 'del(printer)'
24
 *                          printers can be deleted by 'del(printer)'
25
 */
25
 */
26
 
26
 
27
#include "internal.h"
27
#include "internal.h"
28
#include "rui.h"
28
#include "rui.h"
29
 
29
 
30
 
30
 
31
/*
31
/*
32
 *  Internal printer deletion function.
32
 *  Internal printer deletion function.
33
 */
33
 */
34
static void private_delprinter(printer obj)
34
static void private_delprinter(printer obj)
35
{
35
{
36
    HDC h = (HDC) obj->handle;
36
    HDC h = (HDC) obj->handle;
37
    if (!obj || !h || (obj->kind != PrinterObject)) return;
37
    if (!obj || !h || (obj->kind != PrinterObject)) return;
38
    EndPage(h);
38
    EndPage(h);
39
    EndDoc(h);
39
    EndDoc(h);
40
    DeleteDC(h);
40
    DeleteDC(h);
41
    Rwin_fpset();
41
    Rwin_fpset();
42
    return;
42
    return;
43
}
43
}
44
 
44
 
45
/*
45
/*
46
 *  Create/return the base printer object.
46
 *  Create/return the base printer object.
47
 */
47
 */
48
static object get_printer_base(void)
48
static object get_printer_base(void)
49
{
49
{
50
    static object printer_base = NULL;
50
    static object printer_base = NULL;
51
 
51
 
52
    if (! printer_base)
52
    if (! printer_base)
53
	printer_base = new_object(BaseObject, 0, NULL);
53
	printer_base = new_object(BaseObject, 0, NULL);
54
    return printer_base;
54
    return printer_base;
55
}
55
}
56
 
56
 
57
static HDC chooseprinter()
57
static HDC chooseprinter()
58
{
58
{
59
    PRINTDLG pd;
59
    PRINTDLG pd;
60
    HDC dc;
60
    HDC dc;
61
    DWORD rc;
61
    DWORD rc;
62
    char cwd[MAX_PATH];
62
    char cwd[MAX_PATH];
63
 
63
 
64
    GetCurrentDirectory(MAX_PATH,cwd);
64
    GetCurrentDirectory(MAX_PATH,cwd);
65
 
65
 
66
    pd.lStructSize = sizeof( PRINTDLG );
66
    pd.lStructSize = sizeof( PRINTDLG );
67
    pd.hwndOwner = NULL;
67
    pd.hwndOwner = NULL;
68
    pd.hDevMode = (HANDLE)NULL;
68
    pd.hDevMode = (HANDLE)NULL;
69
    pd.hDevNames = (HANDLE)NULL;
69
    pd.hDevNames = (HANDLE)NULL;
70
    pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS |
70
    pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS |
71
	PD_USEDEVMODECOPIES;
71
	PD_USEDEVMODECOPIES;
72
    pd.nFromPage = 0;
72
    pd.nFromPage = 0;
73
    pd.nToPage = 0;
73
    pd.nToPage = 0;
74
    pd.nMinPage = 0;
74
    pd.nMinPage = 0;
75
    pd.nMaxPage = 0;
75
    pd.nMaxPage = 0;
76
    pd.nCopies = 1;
76
    pd.nCopies = 1;
77
    pd.hInstance = (HINSTANCE)NULL;
77
    pd.hInstance = (HINSTANCE)NULL;
78
    pd.lCustData = (LPARAM)0;
78
    pd.lCustData = (LPARAM)0;
79
    pd.lpfnPrintHook = 0;
79
    pd.lpfnPrintHook = 0;
80
    pd.lpfnSetupHook = 0;
80
    pd.lpfnSetupHook = 0;
81
    pd.lpPrintTemplateName = (LPCSTR) 0;
81
    pd.lpPrintTemplateName = (LPCSTR) 0;
82
    pd.lpSetupTemplateName = (LPCSTR) 0;
82
    pd.lpSetupTemplateName = (LPCSTR) 0;
83
    pd.hPrintTemplate = (HGLOBAL)0;
83
    pd.hPrintTemplate = (HGLOBAL)0;
84
    pd.hSetupTemplate = (HGLOBAL)0;
84
    pd.hSetupTemplate = (HGLOBAL)0;
85
 
85
 
86
    dc = PrintDlg( &pd ) ? pd.hDC : NULL;
86
    dc = PrintDlg( &pd ) ? pd.hDC : NULL;
87
    SetCurrentDirectory(cwd);
87
    SetCurrentDirectory(cwd);
88
    if (!dc) {
88
    if (!dc) {
89
	rc = CommDlgExtendedError(); /* 0 means user cancelled */
89
	rc = CommDlgExtendedError(); /* 0 means user cancelled */
90
	if (rc) R_ShowMessage("Unable to choose printer");
90
	if (rc) R_ShowMessage("Unable to choose printer");
91
    }
91
    }
92
    return dc;
92
    return dc;
93
}
93
}
94
 
94
 
95
 
95
 
96
printer newprinter(double width, double height)
96
printer newprinter(double width, double height, char *name)
97
{
97
{
98
    DOCINFO docinfo;
98
    DOCINFO docinfo;
99
    printer obj;
99
    printer obj;
100
    HDC hDC = chooseprinter();
100
    HDC hDC;
101
    double dd,AL;
101
    double dd,AL;
102
    int ww,hh,x0,y0;
102
    int ww,hh,x0,y0;
103
 
103
 
-
 
104
    if(strlen(name)) {
-
 
105
	OSVERSIONINFO verinfo;
-
 
106
	verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-
 
107
	GetVersionEx(&verinfo);
-
 
108
	switch(verinfo.dwPlatformId) {
-
 
109
	case VER_PLATFORM_WIN32_NT:
-
 
110
	    hDC = CreateDC("WINSPOOL", name, NULL, NULL);
-
 
111
	default:
-
 
112
	    hDC = CreateDC(NULL, name, NULL, NULL);
-
 
113
	}
-
 
114
    } else hDC = chooseprinter();
104
    if ( !hDC ) return NULL;
115
    if ( !hDC ) return NULL;
105
    obj = new_object(PrinterObject, (HANDLE) hDC, get_printer_base());
116
    obj = new_object(PrinterObject, (HANDLE) hDC, get_printer_base());
106
    if ( !obj ) {
117
    if ( !obj ) {
107
	R_ShowMessage("Insufficient memory for new printer");
118
	R_ShowMessage("Insufficient memory for new printer");
108
	DeleteDC(hDC);
119
	DeleteDC(hDC);
109
	return NULL;
120
	return NULL;
110
    }
121
    }
111
    if ((width == 0.0) && (height == 0.0)) {
122
    if ((width == 0.0) && (height == 0.0)) {
112
	ww = GetDeviceCaps(hDC, HORZRES);
123
	ww = GetDeviceCaps(hDC, HORZRES);
113
	hh = GetDeviceCaps(hDC, VERTRES);
124
	hh = GetDeviceCaps(hDC, VERTRES);
114
    }
125
    }
115
    else {
126
    else {
116
	if (width < 0.1) width = 0.1;
127
	if (width < 0.1) width = 0.1;
117
	if (height < 0.1) height = 0.1;
128
	if (height < 0.1) height = 0.1;
118
	dd =  GetDeviceCaps(hDC, HORZSIZE) / width;
129
	dd =  GetDeviceCaps(hDC, HORZSIZE) / width;
119
	AL = (dd < 1.0) ? dd : 1.0;
130
	AL = (dd < 1.0) ? dd : 1.0;
120
	dd = GetDeviceCaps(hDC, VERTSIZE) / height;
131
	dd = GetDeviceCaps(hDC, VERTSIZE) / height;
121
	AL = (dd < AL) ? dd : AL;
132
	AL = (dd < AL) ? dd : AL;
122
	ww = (AL * width) * GetDeviceCaps(hDC, LOGPIXELSX) / 25.4;
133
	ww = (AL * width) * GetDeviceCaps(hDC, LOGPIXELSX) / 25.4;
123
	hh = (AL * height) * GetDeviceCaps(hDC, LOGPIXELSY) / 25.4;
134
	hh = (AL * height) * GetDeviceCaps(hDC, LOGPIXELSY) / 25.4;
124
    }
135
    }
125
    x0 = (GetDeviceCaps(hDC, HORZRES) - ww) / 2;
136
    x0 = (GetDeviceCaps(hDC, HORZRES) - ww) / 2;
126
    y0 = (GetDeviceCaps(hDC, VERTRES) - hh) / 2;
137
    y0 = (GetDeviceCaps(hDC, VERTRES) - hh) / 2;
127
    obj->rect = rect(x0, y0, ww, hh);
138
    obj->rect = rect(x0, y0, ww, hh);
128
    obj->depth = GetDeviceCaps(hDC, BITSPIXEL)* GetDeviceCaps(hDC, PLANES);
139
    obj->depth = GetDeviceCaps(hDC, BITSPIXEL)* GetDeviceCaps(hDC, PLANES);
129
    obj->die = private_delprinter;
140
    obj->die = private_delprinter;
130
    obj->drawstate = copydrawstate();
141
    obj->drawstate = copydrawstate();
131
    obj->drawstate->dest = obj;
142
    obj->drawstate->dest = obj;
132
 
143
 
133
    docinfo.cbSize = sizeof(DOCINFO);	/* set this size... */
144
    docinfo.cbSize = sizeof(DOCINFO);	/* set this size... */
134
    docinfo.lpszDocName = "GraphAppPrintJob";
145
    docinfo.lpszDocName = "GraphAppPrintJob";
135
    docinfo.lpszOutput = 0;		/* no file output... */
146
    docinfo.lpszOutput = 0;		/* no file output... */
136
    docinfo.lpszDatatype = 0;
147
    docinfo.lpszDatatype = 0;
137
    docinfo.fwType = 0;
148
    docinfo.fwType = 0;
138
 
149
 
139
    if (StartDoc(hDC, &docinfo) <= 0) {
150
    if (StartDoc(hDC, &docinfo) <= 0) {
140
	R_ShowMessage("Unable to start the print job");
151
	R_ShowMessage("Unable to start the print job");
141
	del(obj);
152
	del(obj);
142
	return NULL;
153
	return NULL;
143
    }
154
    }
144
 
155
 
145
    StartPage(hDC);
156
    StartPage(hDC);
146
    Rwin_fpset();
157
    Rwin_fpset();
147
    return obj;
158
    return obj;
148
}
159
}
149
 
160
 
150
 
161
 
151
void nextpage(printer p)
162
void nextpage(printer p)
152
{
163
{
153
    if (!p || (p->kind != PrinterObject)) return;
164
    if (!p || (p->kind != PrinterObject)) return;
154
    EndPage((HDC) p->handle);
165
    EndPage((HDC) p->handle);
155
    StartPage((HDC) p->handle);
166
    StartPage((HDC) p->handle);
156
    Rwin_fpset();
167
    Rwin_fpset();
157
}
168
}