Reverse the order for deleting the items in resetcontent to correctly
[wine] / dlls / gdi / mfdrv / init.c
1 /*
2  * Metafile driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "gdi.h"
28 #include "wownt32.h"
29 #include "gdi_private.h"
30 #include "mfdrv/metafiledrv.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
34
35 static const DC_FUNCTIONS MFDRV_Funcs =
36 {
37     NULL,                            /* pAbortDoc */
38     MFDRV_AbortPath,                 /* pAbortPath */
39     NULL,                            /* pAlphaBlend */
40     NULL,                            /* pAngleArc */
41     MFDRV_Arc,                       /* pArc */
42     NULL,                            /* pArcTo */
43     MFDRV_BeginPath,                 /* pBeginPath */
44     MFDRV_BitBlt,                    /* pBitBlt */
45     NULL,                            /* pChoosePixelFormat */
46     MFDRV_Chord,                     /* pChord */
47     MFDRV_CloseFigure,               /* pCloseFigure */
48     NULL,                            /* pCreateBitmap */
49     NULL,                            /* pCreateDC */
50     NULL,                            /* pCreateDIBSection */
51     NULL,                            /* pDeleteBitmap */
52     NULL,                            /* pDeleteDC */
53     MFDRV_DeleteObject,              /* pDeleteObject */
54     NULL,                            /* pDescribePixelFormat */
55     NULL,                            /* pDeviceCapabilities */
56     MFDRV_Ellipse,                   /* pEllipse */
57     NULL,                            /* pEndDoc */
58     NULL,                            /* pEndPage */
59     MFDRV_EndPath,                   /* pEndPath */
60     NULL,                            /* pEnumDeviceFonts */
61     MFDRV_ExcludeClipRect,           /* pExcludeClipRect */
62     NULL,                            /* pExtDeviceMode */
63     MFDRV_ExtEscape,                 /* pExtEscape */
64     MFDRV_ExtFloodFill,              /* pExtFloodFill */
65     MFDRV_ExtSelectClipRgn,          /* pExtSelectClipRgn */
66     MFDRV_ExtTextOut,                /* pExtTextOut */
67     MFDRV_FillPath,                  /* pFillPath */
68     MFDRV_FillRgn,                   /* pFillRgn */
69     MFDRV_FlattenPath,               /* pFlattenPath */
70     MFDRV_FrameRgn,                  /* pFrameRgn */
71     NULL,                            /* pGdiComment */
72     NULL,                            /* pGetBitmapBits */
73     NULL,                            /* pGetCharWidth */
74     NULL,                            /* pGetDCOrgEx */
75     NULL,                            /* pGetDIBColorTable */
76     NULL,                            /* pGetDIBits */
77     MFDRV_GetDeviceCaps,             /* pGetDeviceCaps */
78     NULL,                            /* pGetDeviceGammaRamp */
79     NULL,                            /* pGetNearestColor */
80     NULL,                            /* pGetPixel */
81     NULL,                            /* pGetPixelFormat */
82     NULL,                            /* pGetSystemPaletteEntries */
83     NULL,                            /* pGetTextExtentPoint */
84     NULL,                            /* pGetTextMetrics */
85     MFDRV_IntersectClipRect,         /* pIntersectClipRect */
86     MFDRV_InvertRgn,                 /* pInvertRgn */
87     MFDRV_LineTo,                    /* pLineTo */
88     NULL,                            /* pModifyWorldTransform */
89     MFDRV_MoveTo,                    /* pMoveTo */
90     MFDRV_OffsetClipRgn,             /* pOffsetClipRgn */
91     MFDRV_OffsetViewportOrg,         /* pOffsetViewportOrg */
92     MFDRV_OffsetWindowOrg,           /* pOffsetWindowOrg */
93     MFDRV_PaintRgn,                  /* pPaintRgn */
94     MFDRV_PatBlt,                    /* pPatBlt */
95     MFDRV_Pie,                       /* pPie */
96     MFDRV_PolyBezier,                /* pPolyBezier */
97     MFDRV_PolyBezierTo,              /* pPolyBezierTo */
98     NULL,                            /* pPolyDraw */
99     MFDRV_PolyPolygon,               /* pPolyPolygon */
100     NULL,                            /* pPolyPolyline */
101     MFDRV_Polygon,                   /* pPolygon */
102     MFDRV_Polyline,                  /* pPolyline */
103     NULL,                            /* pPolylineTo */
104     NULL,                            /* pRealizeDefaultPalette */
105     MFDRV_RealizePalette,            /* pRealizePalette */
106     MFDRV_Rectangle,                 /* pRectangle */
107     NULL,                            /* pResetDC */
108     MFDRV_RestoreDC,                 /* pRestoreDC */
109     MFDRV_RoundRect,                 /* pRoundRect */
110     MFDRV_SaveDC,                    /* pSaveDC */
111     MFDRV_ScaleViewportExt,          /* pScaleViewportExt */
112     MFDRV_ScaleWindowExt,            /* pScaleWindowExt */
113     MFDRV_SelectBitmap,              /* pSelectBitmap */
114     MFDRV_SelectBrush,               /* pSelectBrush */
115     MFDRV_SelectClipPath,            /* pSelectClipPath */
116     MFDRV_SelectFont,                /* pSelectFont */
117     MFDRV_SelectPalette,             /* pSelectPalette */
118     MFDRV_SelectPen,                 /* pSelectPen */
119     NULL,                            /* pSetArcDirection */
120     NULL,                            /* pSetBitmapBits */
121     MFDRV_SetBkColor,                /* pSetBkColor */
122     MFDRV_SetBkMode,                 /* pSetBkMode */
123     NULL,                            /* pSetDCBrushColor*/
124     NULL,                            /* pSetDCOrg */
125     NULL,                            /* pSetDCPenColor*/
126     NULL,                            /* pSetDIBColorTable */
127     NULL,                            /* pSetDIBits */
128     MFDRV_SetDIBitsToDevice,         /* pSetDIBitsToDevice */
129     NULL,                            /* pSetDeviceClipping */
130     NULL,                            /* pSetDeviceGammaRamp */
131     MFDRV_SetMapMode,                /* pSetMapMode */
132     MFDRV_SetMapperFlags,            /* pSetMapperFlags */
133     MFDRV_SetPixel,                  /* pSetPixel */
134     NULL,                            /* pSetPixelFormat */
135     MFDRV_SetPolyFillMode,           /* pSetPolyFillMode */
136     MFDRV_SetROP2,                   /* pSetROP2 */
137     MFDRV_SetRelAbs,                 /* pSetRelAbs */
138     MFDRV_SetStretchBltMode,         /* pSetStretchBltMode */
139     MFDRV_SetTextAlign,              /* pSetTextAlign */
140     MFDRV_SetTextCharacterExtra,     /* pSetTextCharacterExtra */
141     MFDRV_SetTextColor,              /* pSetTextColor */
142     MFDRV_SetTextJustification,      /* pSetTextJustification */
143     MFDRV_SetViewportExt,            /* pSetViewportExt */
144     MFDRV_SetViewportOrg,            /* pSetViewportOrg */
145     MFDRV_SetWindowExt,              /* pSetWindowExt */
146     MFDRV_SetWindowOrg,              /* pSetWindowOrg */
147     NULL,                            /* pSetWorldTransform */
148     NULL,                            /* pStartDoc */
149     NULL,                            /* pStartPage */
150     MFDRV_StretchBlt,                /* pStretchBlt */
151     MFDRV_StretchDIBits,             /* pStretchDIBits */
152     MFDRV_StrokeAndFillPath,         /* pStrokeAndFillPath */
153     MFDRV_StrokePath,                /* pStrokePath */
154     NULL,                            /* pSwapBuffers */
155     MFDRV_WidenPath                  /* pWidenPath */
156 };
157
158
159
160 /**********************************************************************
161  *           MFDRV_AllocMetaFile
162  */
163 static DC *MFDRV_AllocMetaFile(void)
164 {
165     DC *dc;
166     METAFILEDRV_PDEVICE *physDev;
167
168     if (!(dc = DC_AllocDC( &MFDRV_Funcs, METAFILE_DC_MAGIC ))) return NULL;
169
170     physDev = (METAFILEDRV_PDEVICE *)HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
171     if (!physDev)
172     {
173         GDI_FreeObject( dc->hSelf, dc );
174         return NULL;
175     }
176     dc->physDev = (PHYSDEV)physDev;
177     physDev->hdc = dc->hSelf;
178     physDev->dc = dc;
179
180     if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
181     {
182         HeapFree( GetProcessHeap(), 0, physDev );
183         GDI_FreeObject( dc->hSelf, dc );
184         return NULL;
185     }
186
187     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
188     physDev->handles_size = HANDLE_LIST_INC;
189     physDev->cur_handles = 0;
190
191     physDev->hFile = 0;
192
193     physDev->mh->mtHeaderSize   = sizeof(METAHEADER) / sizeof(WORD);
194     physDev->mh->mtVersion      = 0x0300;
195     physDev->mh->mtSize         = physDev->mh->mtHeaderSize;
196     physDev->mh->mtNoObjects    = 0;
197     physDev->mh->mtMaxRecord    = 0;
198     physDev->mh->mtNoParameters = 0;
199
200     return dc;
201 }
202
203
204 /**********************************************************************
205  *           MFDRV_DeleteDC
206  */
207 static BOOL MFDRV_DeleteDC( PHYSDEV dev )
208 {
209     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
210     DC *dc = physDev->dc;
211     DWORD index;
212
213     HeapFree( GetProcessHeap(), 0, physDev->mh );
214     for(index = 0; index < physDev->handles_size; index++)
215         if(physDev->handles[index])
216             GDI_hdc_not_using_object(physDev->handles[index], physDev->hdc);
217     HeapFree( GetProcessHeap(), 0, physDev->handles );
218     HeapFree( GetProcessHeap(), 0, physDev );
219     dc->physDev = NULL;
220     GDI_FreeObject( dc->hSelf, dc );
221     return TRUE;
222 }
223
224
225 /**********************************************************************
226  *           CreateMetaFileW   (GDI32.@)
227  *
228  *  Create a new DC and associate it with a metafile. Pass a filename
229  *  to create a disk-based metafile, NULL to create a memory metafile.
230  *
231  * RETURNS
232  *  A handle to the metafile DC if successful, NULL on failure.
233  */
234 HDC WINAPI CreateMetaFileW( LPCWSTR filename ) /* [in] Filename of disk metafile */
235 {
236     HDC ret;
237     DC *dc;
238     METAFILEDRV_PDEVICE *physDev;
239     HANDLE hFile;
240
241     TRACE("'%s'\n", debugstr_w(filename) );
242
243     if (!(dc = MFDRV_AllocMetaFile())) return 0;
244     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
245
246     if (filename)  /* disk based metafile */
247     {
248         physDev->mh->mtType = METAFILE_DISK;
249         if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
250                                 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
251             MFDRV_DeleteDC( dc->physDev );
252             return 0;
253         }
254         if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
255                         NULL )) {
256             MFDRV_DeleteDC( dc->physDev );
257             return 0;
258         }
259         physDev->hFile = hFile;
260
261         /* Grow METAHEADER to include filename */
262         physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
263     }
264     else  /* memory based metafile */
265         physDev->mh->mtType = METAFILE_MEMORY;
266
267     TRACE("returning %p\n", dc->hSelf);
268     ret = dc->hSelf;
269     GDI_ReleaseObj( dc->hSelf );
270     return ret;
271 }
272
273 /**********************************************************************
274  *          CreateMetaFileA   (GDI32.@)
275  */
276 HDC WINAPI CreateMetaFileA(LPCSTR filename)
277 {
278     LPWSTR filenameW;
279     DWORD len;
280     HDC hReturnDC;
281
282     if (!filename) return CreateMetaFileW(NULL);
283
284     len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
285     filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
286     MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
287
288     hReturnDC = CreateMetaFileW(filenameW);
289
290     HeapFree( GetProcessHeap(), 0, filenameW );
291
292     return hReturnDC;
293 }
294
295
296 /**********************************************************************
297  *          MFDRV_CloseMetaFile
298  */
299 static DC *MFDRV_CloseMetaFile( HDC hdc )
300 {
301     DC *dc;
302     METAFILEDRV_PDEVICE *physDev;
303
304     TRACE("(%p)\n", hdc );
305
306     if (!(dc = (DC *) GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
307     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
308
309     /* Construct the end of metafile record - this is documented
310      * in SDK Knowledgebase Q99334.
311      */
312
313     if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
314     {
315         MFDRV_DeleteDC( dc->physDev );
316         return 0;
317     }
318
319     if (physDev->mh->mtType == METAFILE_DISK)  /* disk based metafile */
320     {
321         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
322             MFDRV_DeleteDC( dc->physDev );
323             return 0;
324         }
325
326         physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
327         if (!WriteFile(physDev->hFile, (LPSTR)physDev->mh,
328                        sizeof(*physDev->mh), NULL, NULL)) {
329             MFDRV_DeleteDC( dc->physDev );
330             return 0;
331         }
332         CloseHandle(physDev->hFile);
333         physDev->mh->mtType = METAFILE_DISK;
334     }
335
336     return dc;
337 }
338
339 /******************************************************************
340  *           CloseMetaFile     (GDI.126)
341  */
342 HMETAFILE16 WINAPI CloseMetaFile16(
343                                    HDC16 hdc /* [in] Metafile DC to close */
344 )
345 {
346     HMETAFILE16 hmf;
347     METAFILEDRV_PDEVICE *physDev;
348     DC *dc = MFDRV_CloseMetaFile(HDC_32(hdc));
349     if (!dc) return 0;
350     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
351
352     /* Now allocate a global handle for the metafile */
353
354     hmf = MF_Create_HMETAFILE16( physDev->mh );
355
356     physDev->mh = NULL;  /* So it won't be deleted */
357     MFDRV_DeleteDC( dc->physDev );
358     return hmf;
359 }
360
361 /******************************************************************
362  *           CloseMetaFile   (GDI32.@)
363  *
364  *  Stop recording graphics operations in metafile associated with
365  *  hdc and retrieve metafile.
366  *
367  * RETURNS
368  *  Handle of newly created metafile on success, NULL on failure.
369  */
370 HMETAFILE WINAPI CloseMetaFile(
371                                    HDC hdc /* [in] Metafile DC to close */
372 )
373 {
374     HMETAFILE hmf;
375     METAFILEDRV_PDEVICE *physDev;
376     DC *dc = MFDRV_CloseMetaFile(hdc);
377     if (!dc) return 0;
378     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
379
380     /* Now allocate a global handle for the metafile */
381
382     hmf = MF_Create_HMETAFILE( physDev->mh );
383
384     physDev->mh = NULL;  /* So it won't be deleted */
385     MFDRV_DeleteDC( dc->physDev );
386     return hmf;
387 }
388
389
390 /******************************************************************
391  *         MFDRV_WriteRecord
392  *
393  * Warning: this function can change the pointer to the metafile header.
394  */
395 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
396 {
397     DWORD len, size;
398     METAHEADER *mh;
399     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
400
401     switch(physDev->mh->mtType)
402     {
403     case METAFILE_MEMORY:
404         len = physDev->mh->mtSize * 2 + rlen;
405         /* reallocate memory if needed */
406         size = HeapSize( GetProcessHeap(), 0, physDev->mh );
407         if (len > size)
408         {
409             /*expand size*/
410             size += size / 2 + rlen;
411             mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
412             if (!mh) return FALSE;
413             physDev->mh = mh;
414             TRACE("Reallocated metafile: new size is %ld\n",size);
415         }
416         memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
417         break;
418     case METAFILE_DISK:
419         TRACE("Writing record to disk\n");
420         if (!WriteFile(physDev->hFile, (char *)mr, rlen, NULL, NULL))
421             return FALSE;
422         break;
423     default:
424         ERR("Unknown metafile type %d\n", physDev->mh->mtType );
425         return FALSE;
426     }
427
428     physDev->mh->mtSize += rlen / 2;
429     physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
430     return TRUE;
431 }
432
433
434 /******************************************************************
435  *         MFDRV_MetaParam0
436  */
437
438 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
439 {
440     char buffer[8];
441     METARECORD *mr = (METARECORD *)&buffer;
442
443     mr->rdSize = 3;
444     mr->rdFunction = func;
445     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
446 }
447
448
449 /******************************************************************
450  *         MFDRV_MetaParam1
451  */
452 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
453 {
454     char buffer[8];
455     METARECORD *mr = (METARECORD *)&buffer;
456
457     mr->rdSize = 4;
458     mr->rdFunction = func;
459     *(mr->rdParm) = param1;
460     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
461 }
462
463
464 /******************************************************************
465  *         MFDRV_MetaParam2
466  */
467 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
468 {
469     char buffer[10];
470     METARECORD *mr = (METARECORD *)&buffer;
471
472     mr->rdSize = 5;
473     mr->rdFunction = func;
474     *(mr->rdParm) = param2;
475     *(mr->rdParm + 1) = param1;
476     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
477 }
478
479
480 /******************************************************************
481  *         MFDRV_MetaParam4
482  */
483
484 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
485                       short param3, short param4)
486 {
487     char buffer[14];
488     METARECORD *mr = (METARECORD *)&buffer;
489
490     mr->rdSize = 7;
491     mr->rdFunction = func;
492     *(mr->rdParm) = param4;
493     *(mr->rdParm + 1) = param3;
494     *(mr->rdParm + 2) = param2;
495     *(mr->rdParm + 3) = param1;
496     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
497 }
498
499
500 /******************************************************************
501  *         MFDRV_MetaParam6
502  */
503
504 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
505                       short param3, short param4, short param5, short param6)
506 {
507     char buffer[18];
508     METARECORD *mr = (METARECORD *)&buffer;
509
510     mr->rdSize = 9;
511     mr->rdFunction = func;
512     *(mr->rdParm) = param6;
513     *(mr->rdParm + 1) = param5;
514     *(mr->rdParm + 2) = param4;
515     *(mr->rdParm + 3) = param3;
516     *(mr->rdParm + 4) = param2;
517     *(mr->rdParm + 5) = param1;
518     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
519 }
520
521
522 /******************************************************************
523  *         MFDRV_MetaParam8
524  */
525 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
526                       short param3, short param4, short param5,
527                       short param6, short param7, short param8)
528 {
529     char buffer[22];
530     METARECORD *mr = (METARECORD *)&buffer;
531
532     mr->rdSize = 11;
533     mr->rdFunction = func;
534     *(mr->rdParm) = param8;
535     *(mr->rdParm + 1) = param7;
536     *(mr->rdParm + 2) = param6;
537     *(mr->rdParm + 3) = param5;
538     *(mr->rdParm + 4) = param4;
539     *(mr->rdParm + 5) = param3;
540     *(mr->rdParm + 6) = param2;
541     *(mr->rdParm + 7) = param1;
542     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
543 }
544
545
546 /**********************************************************************
547  *           MFDRV_ExtEscape
548  */
549 INT MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
550                      INT cbOutput, LPVOID out_data )
551 {
552     METARECORD *mr;
553     DWORD len;
554     INT ret;
555
556     len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
557     mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
558     mr->rdSize = len / 2;
559     mr->rdFunction = META_ESCAPE;
560     mr->rdParm[0] = nEscape;
561     mr->rdParm[1] = cbInput;
562     memcpy(&(mr->rdParm[2]), in_data, cbInput);
563     ret = MFDRV_WriteRecord( dev, mr, len);
564     HeapFree(GetProcessHeap(), 0, mr);
565     return ret;
566 }
567
568
569 /******************************************************************
570  *         MFDRV_GetDeviceCaps
571  *
572  *A very simple implementation that returns DT_METAFILE
573  */
574 INT MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
575 {
576     switch(cap)
577     {
578     case TECHNOLOGY:
579         return DT_METAFILE;
580     case TEXTCAPS:
581         return 0;
582     default:
583         TRACE(" unsupported capability %d, will return 0\n", cap );
584     }
585     return 0;
586 }