source: trunk/Lucide/SOURCE/gui/print.cpp @ 90

Last change on this file since 90 was 90, checked in by Eugene Romanenko, 15 years ago

intermediate commit, incomplete printing procedures

File size: 6.4 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#define INCL_WIN
36#define INCL_GPI
37#define INCL_DEV
38#define INCL_ERRORS
39#define INCL_SPL
40#define INCL_SPLDOSPRINT
41#include <os2.h>
42
43#include <ludoc.xh>
44
45#include <string.h>
46
47#include "print.h"
48
49// OpenWatcom headers doesn't have GpiDrawBits() declaration
50extern "C" {
51    LONG APIENTRY GpiDrawBits(HPS hps, PVOID pBits, PBITMAPINFO2 pbmiInfoTable,
52                              LONG lCount, PPOINTL aptlPoints, LONG lRop, ULONG flOptions);
53}
54
55#define UNITS_MULTIPLIER    100
56
57#define TEST_MARGIN         10
58
59void printPage( LuDocument *doc, long page, HPS hpsPrinter, PHCINFO pcurForm );
60void queryCurrentForm( HDC hdcPrinter, PHCINFO pcurForm );
61
62void printDocument( LuDocument *doc, void *q )
63{
64    PPRQINFO3    pQueueInfo = (PPRQINFO3)q;
65    CHAR         achDriverName[ DRIVERNAME_LENGTH ] = "";
66    SIZEL        sizel = { 0 };
67    DEVOPENSTRUC dos   = { 0 };
68
69    // build a devopenstruct for the call to DevOpenDC
70    dos.pszLogAddress = pQueueInfo->pszName;
71    strcpy( achDriverName, pQueueInfo->pszDriverName );
72    achDriverName[ strcspn( achDriverName, "." ) ] = '\0';
73    dos.pszDriverName = achDriverName;
74    dos.pdriv = pQueueInfo->pDriverData;
75
76    HDC hdcPrinter = DevOpenDC( hab, OD_QUEUED, "*", 3L, (PDEVOPENDATA)&dos, NULLHANDLE );
77    if ( hdcPrinter == DEV_ERROR ) {
78        somPrintf( "DevOpenDC error\n" );
79        return;
80    }
81
82    // PS in HiMetric, 0.01 mm
83    HPS hpsPrinter = GpiCreatePS( hab, hdcPrinter, &sizel, PU_HIMETRIC | GPIA_ASSOC );
84    if ( hpsPrinter == DEV_ERROR ) {
85        DevCloseDC( hdcPrinter );
86        somPrintf( "GpiCreatePS error\n" );
87        return;
88    }
89
90    HCINFO curForm = { 0 };
91    queryCurrentForm( hdcPrinter, &curForm );
92    char *psz = "test";
93    // Issue STARTDOC to begin printing
94    DevEscape( hdcPrinter, DEVESC_STARTDOC, (LONG)strlen(psz), (PBYTE)psz, NULL, NULL );
95
96    somPrintf( "begin!\n" );
97
98    printPage( doc, 0, hpsPrinter, &curForm );
99
100    somPrintf( "end!\n" );
101
102    DevEscape( hdcPrinter, DEVESC_ENDDOC, 0L, NULL, NULL, NULL );
103
104    // Release PS and DC
105    GpiAssociate( hpsPrinter, NULLHANDLE );
106    GpiDestroyPS( hpsPrinter );
107    DevCloseDC( hdcPrinter );
108}
109
110
111static void printPage( LuDocument *doc, long page, HPS hpsPrinter, PHCINFO pcurForm )
112{
113    long bpp = doc->getBpp( ev );
114    double w = 0, h = 0;
115    doc->getPageSize( ev, page, &w, &h );
116
117    // Magrins
118    LONG mLeft   = __max( TEST_MARGIN, pcurForm->xLeftClip );
119    LONG mBottom = __max( TEST_MARGIN, pcurForm->yBottomClip );
120    LONG mRight  = __max( TEST_MARGIN, pcurForm->cx - pcurForm->xRightClip );
121    LONG mTop    = __max( TEST_MARGIN, pcurForm->cy - pcurForm->yTopClip );
122
123    // Count paper page size in hi-metric
124    LONG pwidth = ( pcurForm->cx - mLeft - mRight ) * UNITS_MULTIPLIER;
125    LONG pheight = ( pcurForm->cy - mTop - mBottom ) * UNITS_MULTIPLIER;
126
127    double zoom = __min( (double)pwidth / w, (double)pheight / h );
128    if ( zoom > 2.0 ) {
129        zoom = 2.0;
130    }
131
132    somPrintf( "Doc pgsz: %g/%g  zoom: %g\n", w, h, zoom );
133    somPrintf( "Paper %s, pgsz: %d/%d\n", pcurForm->szFormname, pcurForm->cx, pcurForm->cy );
134    somPrintf( "pw/ph %d/%d\n", pwidth, pheight );
135    w *= zoom;
136    h *= zoom;
137    double pgzoom = __min( (double)pwidth / w, (double)pheight / h );
138
139    // Printed size
140    LONG prwidth = w * pgzoom;
141    LONG prheight = h * pgzoom;
142    LONG yPos = 0;
143    if ( prheight < pheight ) {
144        yPos = pheight - prheight;
145    }
146    RECTL rclDraw = { mLeft*UNITS_MULTIPLIER, (mBottom*UNITS_MULTIPLIER)+yPos,
147                      prwidth, prheight+yPos };
148
149    LONG rclx = w;
150    LONG rcly = h;
151    LuPixbuf *pixbuf = new LuPixbuf( ev, rclx, rcly, bpp );
152    POINTL aptlPoints[4]={ rclDraw.xLeft, rclDraw.yBottom,
153                           rclDraw.xRight-1, rclDraw.yTop-1,
154                           0, 0, rclx, rcly };
155
156    doc->renderPageToPixbuf( ev, page, 0, 0, rclx, rcly, zoom, 0, pixbuf );
157
158    LONG lRop = ROP_SRCCOPY;
159    BITMAPINFO2 pbmi;
160    pbmi.cbFix = 16L;
161    pbmi.cx = rclx;
162    pbmi.cy = rcly;
163    pbmi.cPlanes = 1;
164    pbmi.cBitCount = bpp * 8;
165    GpiDrawBits( hpsPrinter, pixbuf->getDataPtr( ev ), &pbmi, 4L,
166                 aptlPoints, lRop, BBO_IGNORE );
167    delete pixbuf;
168}
169
170
171static void queryCurrentForm( HDC hdcPrinter, PHCINFO pcurForm )
172{
173    LONG lForms = DevQueryHardcopyCaps( hdcPrinter, 0, 0, NULL );
174    if ( lForms == DQHC_ERROR ) {
175        return;
176    }
177
178    HCINFO *forms = new HCINFO[ lForms ];
179    memset( forms, 0, sizeof( HCINFO ) * lForms );
180    lForms = DevQueryHardcopyCaps( hdcPrinter, 0, lForms, forms );
181    if ( lForms == DQHC_ERROR ) {
182        delete forms;
183        return;
184    }
185
186    for ( LONG i = 0; i < lForms; i++ ) {
187        if ( forms[i].flAttributes & HCAPS_CURRENT ) {
188            memcpy( pcurForm, &( forms[i] ), sizeof( HCINFO ) );
189            break;
190        }
191    }
192    delete forms;
193}
194
Note: See TracBrowser for help on using the repository browser.