Extend DRAWMODE struct to win95 spec.
[wine] / graphics / win16drv / init.c
1 /*
2  * Windows Device Context initialisation functions
3  *
4  * Copyright 1996 John Harvey
5  *           1998 Huw Davies
6  */
7
8 #include <string.h>
9 #include <ctype.h>
10 #include "win16drv.h"
11 #include "gdi.h"
12 #include "bitmap.h"
13 #include "heap.h"
14 #include "font.h"
15 #include "options.h"
16 #include "xmalloc.h"
17 #include "debugtools.h"
18 #include "dc.h"
19
20 DEFAULT_DEBUG_CHANNEL(win16drv)
21
22 #define SUPPORT_REALIZED_FONTS 1
23 #include "pshpack1.h"
24 typedef struct
25 {
26   SHORT nSize;
27   SEGPTR lpindata;
28   SEGPTR lpFont;
29   SEGPTR lpXForm;
30   SEGPTR lpDrawMode;
31 } EXTTEXTDATA, *LPEXTTEXTDATA;
32 #include "poppack.h"
33
34 SEGPTR          win16drv_SegPtr_TextXForm;
35 LPTEXTXFORM16   win16drv_TextXFormP;
36 SEGPTR          win16drv_SegPtr_DrawMode;
37 LPDRAWMODE      win16drv_DrawModeP;
38
39
40 static BOOL WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
41                                  LPCSTR output, const DEVMODEA* initData );
42 static INT WIN16DRV_Escape( DC *dc, INT nEscape, INT cbInput, 
43                               SEGPTR lpInData, SEGPTR lpOutData );
44
45 static const DC_FUNCTIONS WIN16DRV_Funcs =
46 {
47     NULL,                            /* pAbortDoc */
48     NULL,                            /* pArc */
49     NULL,                            /* pBitBlt */
50     NULL,                            /* pBitmapBits */
51     NULL,                            /* pChord */
52     NULL,                            /* pCreateBitmap */
53     WIN16DRV_CreateDC,               /* pCreateDC */
54     NULL,                            /* pCreateDIBSection */
55     NULL,                            /* pCreateDIBSection16 */
56     NULL,                            /* pDeleteDC */
57     NULL,                            /* pDeleteObject */
58     WIN16DRV_DeviceCapabilities,     /* pDeviceCapabilities */
59     WIN16DRV_Ellipse,                /* pEllipse */
60     NULL,                            /* pEndDoc */
61     NULL,                            /* pEndPage */
62     WIN16DRV_EnumDeviceFonts,        /* pEnumDeviceFonts */
63     WIN16DRV_Escape,                 /* pEscape */
64     NULL,                            /* pExcludeClipRect */
65     WIN16DRV_ExtDeviceMode,          /* pExtDeviceMode */
66     NULL,                            /* pExtFloodFill */
67     WIN16DRV_ExtTextOut,             /* pExtTextOut */
68     NULL,                            /* pFillRgn */
69     NULL,                            /* pFrameRgn */
70     WIN16DRV_GetCharWidth,           /* pGetCharWidth */
71     NULL,                            /* pGetPixel */
72     WIN16DRV_GetTextExtentPoint,     /* pGetTextExtentPoint */
73     WIN16DRV_GetTextMetrics,         /* pGetTextMetrics */
74     NULL,                            /* pIntersectClipRect */
75     NULL,                            /* pInvertRgn */
76     WIN16DRV_LineTo,                 /* pLineTo */
77     NULL,                            /* pLoadOEMResource */
78     WIN16DRV_MoveToEx,               /* pMoveToEx */
79     NULL,                            /* pOffsetClipRgn */
80     NULL,                            /* pOffsetViewportOrgEx */
81     NULL,                            /* pOffsetWindowOrgEx */
82     NULL,                            /* pPaintRgn */
83     WIN16DRV_PatBlt,                 /* pPatBlt */
84     NULL,                            /* pPie */
85     NULL,                            /* pPolyPolygon */
86     NULL,                            /* pPolyPolyline */
87     WIN16DRV_Polygon,                /* pPolygon */
88     WIN16DRV_Polyline,               /* pPolyline */
89     NULL,                            /* pPolyBezier */
90     NULL,                            /* pRealizePalette */
91     WIN16DRV_Rectangle,              /* pRectangle */
92     NULL,                            /* pRestoreDC */
93     NULL,                            /* pRoundRect */
94     NULL,                            /* pSaveDC */
95     NULL,                            /* pScaleViewportExtEx */
96     NULL,                            /* pScaleWindowExtEx */
97     NULL,                            /* pSelectClipRgn */
98     WIN16DRV_SelectObject,           /* pSelectObject */
99     NULL,                            /* pSelectPalette */
100     NULL,                            /* pSetBkColor */
101     NULL,                            /* pSetBkMode */
102     NULL,                            /* pSetDeviceClipping */
103     NULL,                            /* pSetDIBitsToDevice */
104     NULL,                            /* pSetMapMode */
105     NULL,                            /* pSetMapperFlags */
106     NULL,                            /* pSetPixel */
107     NULL,                            /* pSetPolyFillMode */
108     NULL,                            /* pSetROP2 */
109     NULL,                            /* pSetRelAbs */
110     NULL,                            /* pSetStretchBltMode */
111     NULL,                            /* pSetTextAlign */
112     NULL,                            /* pSetTextCharacterExtra */
113     NULL,                            /* pSetTextColor */
114     NULL,                            /* pSetTextJustification */
115     NULL,                            /* pSetViewportExtEx */
116     NULL,                            /* pSetViewportOrgEx */
117     NULL,                            /* pSetWindowExtEx */
118     NULL,                            /* pSetWindowOrgEx */
119     NULL,                            /* pStartDoc */
120     NULL,                            /* pStartPage */
121     NULL,                            /* pStretchBlt */
122     NULL                             /* pStretchDIBits */
123 };
124
125
126
127
128
129 /**********************************************************************
130  *           WIN16DRV_Init
131  */
132 BOOL WIN16DRV_Init(void)
133 {
134     return DRIVER_RegisterDriver( NULL /* generic driver */, &WIN16DRV_Funcs );
135         
136 }
137
138 /* Tempory functions, for initialising structures */
139 /* These values should be calculated, not hardcoded */
140 void InitTextXForm(LPTEXTXFORM16 lpTextXForm)
141 {
142     lpTextXForm->txfHeight      = 0x0001;
143     lpTextXForm->txfWidth       = 0x000c;
144     lpTextXForm->txfEscapement  = 0x0000;
145     lpTextXForm->txfOrientation = 0x0000;
146     lpTextXForm->txfWeight      = 0x0190;
147     lpTextXForm->txfItalic      = 0x00;
148     lpTextXForm->txfUnderline   = 0x00;
149     lpTextXForm->txfStrikeOut   = 0x00; 
150     lpTextXForm->txfOutPrecision = 0x02;
151     lpTextXForm->txfClipPrecision = 0x01;
152     lpTextXForm->txfAccelerator = 0x0001;
153     lpTextXForm->txfOverhang    = 0x0000;
154 }
155
156
157 void InitDrawMode(LPDRAWMODE lpDrawMode)
158 {
159     lpDrawMode->Rop2            = 0x000d;       
160     lpDrawMode->bkMode          = 0x0001;     
161     lpDrawMode->bkColor         = 0x3fffffff;    
162     lpDrawMode->TextColor       = 0x20000000;  
163     lpDrawMode->TBreakExtra     = 0x0000;
164     lpDrawMode->BreakExtra      = 0x0000; 
165     lpDrawMode->BreakErr        = 0x0000;   
166     lpDrawMode->BreakRem        = 0x0000;   
167     lpDrawMode->BreakCount      = 0x0000; 
168     lpDrawMode->CharExtra       = 0x0000;  
169     lpDrawMode->LbkColor        = 0x00ffffff;   
170     lpDrawMode->LTextColor      = 0x00000000;     
171     lpDrawMode->ICMCXform       = 0; /* ? */
172     lpDrawMode->StretchBltMode  = STRETCH_ANDSCANS;
173     lpDrawMode->eMiterLimit     = 1;
174 }
175
176 BOOL WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device, LPCSTR output,
177                           const DEVMODEA* initData )
178 {
179     LOADED_PRINTER_DRIVER *pLPD;
180     WORD wRet;
181     DeviceCaps *printerDevCaps;
182     int nPDEVICEsize;
183     PDEVICE_HEADER *pPDH;
184     WIN16DRV_PDEVICE *physDev;
185     char printerEnabled[20];
186     PROFILE_GetWineIniString( "wine", "printer", "off",
187                              printerEnabled, sizeof(printerEnabled) );
188     if (lstrcmpiA(printerEnabled,"on"))
189     {
190         MESSAGE("Printing disabled in wine.conf or .winerc file\n");
191         MESSAGE("Use \"printer=on\" in the \"[wine]\" section to enable it.\n");
192         return FALSE;
193     }
194
195     TRACE("In creatdc for (%s,%s,%s) initData 0x%p\n",
196           driver, device, output, initData);
197
198     physDev = (WIN16DRV_PDEVICE *)HeapAlloc( SystemHeap, 0, sizeof(*physDev) );
199     if (!physDev) return FALSE;
200     dc->physDev = physDev;
201
202     pLPD = LoadPrinterDriver(driver);
203     if (pLPD == NULL)
204     {
205         WARN("Failed to find printer driver\n");
206         HeapFree( SystemHeap, 0, physDev );
207         return FALSE;
208     }
209     TRACE("windevCreateDC pLPD 0x%p\n", pLPD);
210
211     /* Now Get the device capabilities from the printer driver */
212     
213     printerDevCaps = (DeviceCaps *) xmalloc(sizeof(DeviceCaps));
214     memset(printerDevCaps, 0, sizeof(DeviceCaps));
215
216     if(!output) output = "LPT1:";
217     /* Get GDIINFO which is the same as a DeviceCaps structure */
218     wRet = PRTDRV_Enable(printerDevCaps, GETGDIINFO, device, driver, output,NULL); 
219
220     /* Add this to the DC */
221     dc->w.devCaps = printerDevCaps;
222     dc->w.hVisRgn = CreateRectRgn(0, 0, dc->w.devCaps->horzRes, dc->w.devCaps->vertRes);
223     dc->w.bitsPerPixel = dc->w.devCaps->bitsPixel;
224     
225     TRACE("Got devcaps width %d height %d bits %d planes %d\n",
226           dc->w.devCaps->horzRes, dc->w.devCaps->vertRes, 
227           dc->w.devCaps->bitsPixel, dc->w.devCaps->planes);
228
229     /* Now we allocate enough memory for the PDEVICE structure */
230     /* The size of this varies between printer drivers */
231     /* This PDEVICE is used by the printer DRIVER not by the GDI so must */
232     /* be accessable from 16 bit code */
233     nPDEVICEsize = dc->w.devCaps->pdeviceSize + sizeof(PDEVICE_HEADER);
234
235     /* TTD Shouldn't really do pointer arithmetic on segment points */
236     physDev->segptrPDEVICE = WIN16_GlobalLock16(GlobalAlloc16(GHND, nPDEVICEsize))+sizeof(PDEVICE_HEADER);
237     *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+0) = 'N'; 
238     *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+1) = 'B'; 
239
240     /* Set up the header */
241     pPDH = (PDEVICE_HEADER *)((BYTE*)PTR_SEG_TO_LIN(physDev->segptrPDEVICE) - sizeof(PDEVICE_HEADER)); 
242     pPDH->pLPD = pLPD;
243     
244     TRACE("PDEVICE allocated %08lx\n",(DWORD)(physDev->segptrPDEVICE));
245     
246     /* Now get the printer driver to initialise this data */
247     wRet = PRTDRV_Enable((LPVOID)physDev->segptrPDEVICE, INITPDEVICE, device, driver, output, NULL); 
248
249     physDev->FontInfo = NULL;
250     physDev->BrushInfo = NULL;
251     physDev->PenInfo = NULL;
252     win16drv_SegPtr_TextXForm = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(TEXTXFORM16)));
253     win16drv_TextXFormP = PTR_SEG_TO_LIN(win16drv_SegPtr_TextXForm);
254     
255     InitTextXForm(win16drv_TextXFormP);
256
257     /* TTD Lots more to do here */
258     win16drv_SegPtr_DrawMode = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(DRAWMODE)));
259     win16drv_DrawModeP = PTR_SEG_TO_LIN(win16drv_SegPtr_DrawMode);
260     
261     InitDrawMode(win16drv_DrawModeP);
262
263     return TRUE;
264 }
265
266 BOOL WIN16DRV_PatBlt( struct tagDC *dc, INT left, INT top,
267                         INT width, INT height, DWORD rop )
268 {
269   
270     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
271     BOOL bRet = 0;
272
273     bRet = PRTDRV_StretchBlt( physDev->segptrPDEVICE, left, top, width, height, (SEGPTR)NULL, 0, 0, width, height,
274                        PATCOPY, physDev->BrushInfo, win16drv_SegPtr_DrawMode, NULL);
275
276     return bRet;
277 }
278 /* 
279  * Escape (GDI.38)
280  */
281 static INT WIN16DRV_Escape( DC *dc, INT nEscape, INT cbInput, 
282                               SEGPTR lpInData, SEGPTR lpOutData )
283 {
284     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
285     int nRet = 0;
286
287     /* We should really process the nEscape parameter, but for now just
288        pass it all to the driver */
289     if (dc != NULL && physDev->segptrPDEVICE != 0)
290     {
291         switch(nEscape)
292           {
293           case ENABLEPAIRKERNING:
294             FIXME("Escape: ENABLEPAIRKERNING ignored.\n");
295             nRet = 1;
296             break;
297           case GETPAIRKERNTABLE:
298             FIXME("Escape: GETPAIRKERNTABLE ignored.\n");
299             nRet = 0;
300             break;
301           case SETABORTPROC: {
302                 /* FIXME: The AbortProc should be called:
303                 - After every write to printer port or spool file
304                 - Several times when no more disk space
305                 - Before every metafile record when GDI does banding
306                 */ 
307
308        /* Call Control with hdc as lpInData */
309             HDC16 *seghdc = SEGPTR_NEW(HDC16);
310             *seghdc = dc->hSelf;
311             nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
312                                   SEGPTR_GET(seghdc), lpOutData);
313             SEGPTR_FREE(seghdc);
314             break;
315           }
316
317           case NEXTBAND:
318             {
319               LPPOINT16 newInData =  SEGPTR_NEW(POINT16);
320
321               nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
322                                     SEGPTR_GET(newInData), lpOutData);
323               SEGPTR_FREE(newInData);
324               break;
325             }
326
327           case GETEXTENDEDTEXTMETRICS:
328             {
329               EXTTEXTDATA *textData = SEGPTR_NEW(EXTTEXTDATA);
330
331               textData->nSize = cbInput;
332               textData->lpindata = lpInData;
333               textData->lpFont = SEGPTR_GET( physDev->FontInfo );
334               textData->lpXForm = win16drv_SegPtr_TextXForm;
335               textData->lpDrawMode = win16drv_SegPtr_DrawMode;
336               nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
337                                     SEGPTR_GET(textData), lpOutData);
338               SEGPTR_FREE(textData);
339             }
340           break;
341           case STARTDOC:
342             nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
343                                   lpInData, lpOutData);
344             if (nRet != -1)
345             {
346               HDC *tmpHdc = SEGPTR_NEW(HDC);
347
348 #define SETPRINTERDC SETABORTPROC
349
350               *tmpHdc = dc->hSelf;
351               PRTDRV_Control(physDev->segptrPDEVICE, SETPRINTERDC,
352                              SEGPTR_GET(tmpHdc), (SEGPTR)NULL);
353               SEGPTR_FREE(tmpHdc);
354             }
355             break;
356           default:
357             nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
358                                   lpInData, lpOutData);
359             break;
360         }
361     }
362     else
363         WARN("Escape(nEscape = %04x) - ???\n", nEscape);      
364     return nRet;
365 }
366
367