The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

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