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
5458 ripley 17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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);
8641 ripley 43
    Rwin_fpset();
4649 ripley 44
    return;
4394 ripley 45
}
46
 
47
/*
48
 *  Create/return the base printer object.
49
 */
50
static object get_printer_base(void)
51
{
4649 ripley 52
    static object printer_base = NULL;
4394 ripley 53
 
4649 ripley 54
    if (! printer_base)
55
	printer_base = new_object(BaseObject, 0, NULL);
56
    return printer_base;
4394 ripley 57
}
58
 
4649 ripley 59
static HDC chooseprinter()
60
{
4394 ripley 61
    PRINTDLG pd;
62
    HDC dc;
6098 pd 63
    DWORD rc;
4394 ripley 64
    char cwd[MAX_PATH];
65
 
66
    GetCurrentDirectory(MAX_PATH,cwd);
67
 
68
    pd.lStructSize = sizeof( PRINTDLG );
4649 ripley 69
    pd.hwndOwner = NULL;
70
    pd.hDevMode = (HANDLE)NULL;
71
    pd.hDevNames = (HANDLE)NULL;
4394 ripley 72
    pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS |
4649 ripley 73
	PD_USEDEVMODECOPIES;
74
    pd.nFromPage = 0;
75
    pd.nToPage = 0;
76
    pd.nMinPage = 0;
77
    pd.nMaxPage = 0;
78
    pd.nCopies = 1;
79
    pd.hInstance = (HINSTANCE)NULL;
4394 ripley 80
    pd.lCustData = (LPARAM)0;
81
    pd.lpfnPrintHook = 0;
82
    pd.lpfnSetupHook = 0;
83
    pd.lpPrintTemplateName = (LPCSTR) 0;
84
    pd.lpSetupTemplateName = (LPCSTR) 0;
85
    pd.hPrintTemplate = (HGLOBAL)0;
86
    pd.hSetupTemplate = (HGLOBAL)0;
87
 
88
    dc = PrintDlg( &pd ) ? pd.hDC : NULL;
89
    SetCurrentDirectory(cwd);
6098 pd 90
    if (!dc) {
91
	rc = CommDlgExtendedError(); /* 0 means user cancelled */
33093 ripley 92
	if (rc) R_ShowMessage(_("Unable to choose printer"));
6098 pd 93
    }
4394 ripley 94
    return dc;
95
}
96
 
97
 
19110 ripley 98
printer newprinter(double width, double height, char *name)
4394 ripley 99
{
100
    DOCINFO docinfo;
101
    printer obj;
19110 ripley 102
    HDC hDC;
4394 ripley 103
    double dd,AL;
104
    int ww,hh,x0,y0;
4649 ripley 105
 
19110 ripley 106
    if(strlen(name)) {
107
	OSVERSIONINFO verinfo;
108
	verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
109
	GetVersionEx(&verinfo);
110
	switch(verinfo.dwPlatformId) {
111
	case VER_PLATFORM_WIN32_NT:
112
	    hDC = CreateDC("WINSPOOL", name, NULL, NULL);
113
	default:
114
	    hDC = CreateDC(NULL, name, NULL, NULL);
115
	}
116
    } else hDC = chooseprinter();
6098 pd 117
    if ( !hDC ) return NULL;
118
    obj = new_object(PrinterObject, (HANDLE) hDC, get_printer_base());
4394 ripley 119
    if ( !obj ) {
33093 ripley 120
	R_ShowMessage(_("Insufficient memory for new printer"));
4649 ripley 121
	DeleteDC(hDC);
122
	return NULL;
4394 ripley 123
    }
4649 ripley 124
    if ((width == 0.0) && (height == 0.0)) {
125
	ww = GetDeviceCaps(hDC, HORZRES);
126
	hh = GetDeviceCaps(hDC, VERTRES);
4394 ripley 127
    }
128
    else {
4649 ripley 129
	if (width < 0.1) width = 0.1;
130
	if (height < 0.1) height = 0.1;
131
	dd =  GetDeviceCaps(hDC, HORZSIZE) / width;
132
	AL = (dd < 1.0) ? dd : 1.0;
133
	dd = GetDeviceCaps(hDC, VERTSIZE) / height;
134
	AL = (dd < AL) ? dd : AL;
6098 pd 135
	ww = (AL * width) * GetDeviceCaps(hDC, LOGPIXELSX) / 25.4;
136
	hh = (AL * height) * GetDeviceCaps(hDC, LOGPIXELSY) / 25.4;
4394 ripley 137
    }
6098 pd 138
    x0 = (GetDeviceCaps(hDC, HORZRES) - ww) / 2;
139
    y0 = (GetDeviceCaps(hDC, VERTRES) - hh) / 2;
140
    obj->rect = rect(x0, y0, ww, hh);
141
    obj->depth = GetDeviceCaps(hDC, BITSPIXEL)* GetDeviceCaps(hDC, PLANES);
4394 ripley 142
    obj->die = private_delprinter;
143
    obj->drawstate = copydrawstate();
144
    obj->drawstate->dest = obj;
145
 
146
    docinfo.cbSize = sizeof(DOCINFO);	/* set this size... */
147
    docinfo.lpszDocName = "GraphAppPrintJob";
148
    docinfo.lpszOutput = 0;		/* no file output... */
149
    docinfo.lpszDatatype = 0;
150
    docinfo.fwType = 0;
151
 
152
    if (StartDoc(hDC, &docinfo) <= 0) {
33093 ripley 153
	R_ShowMessage(_("Unable to start the print job"));
4649 ripley 154
	del(obj);
155
	return NULL;
4394 ripley 156
    }
157
 
158
    StartPage(hDC);
8641 ripley 159
    Rwin_fpset();
4394 ripley 160
    return obj;
161
}
162
 
163
 
4649 ripley 164
void nextpage(printer p)
165
{
166
    if (!p || (p->kind != PrinterObject)) return;
167
    EndPage((HDC) p->handle);
168
    StartPage((HDC) p->handle);
8641 ripley 169
    Rwin_fpset();
4394 ripley 170
}