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