Create GDI stock objects as normal objects instead of using magic
[wine/gsoc_dplay.git] / graphics / enhmetafiledrv / objects.c
blobf6f0d6024300d3fd3b5533ce98008d4a019fedcc
1 /*
2 * Enhanced MetaFile objects
4 * Copyright 1999 Huw D M Davies
5 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
11 #include "bitmap.h"
12 #include "enhmetafiledrv.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(enhmetafile);
17 /***********************************************************************
18 * EMFDRV_BITMAP_SelectObject
20 static HBITMAP EMFDRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap )
22 return 0;
26 /***********************************************************************
27 * EMFDRV_CreateBrushIndirect
29 DWORD EMFDRV_CreateBrushIndirect( DC *dc, HBRUSH hBrush )
31 DWORD index = 0;
32 LOGBRUSH logbrush;
34 if (!GetObjectA( hBrush, sizeof(logbrush), &logbrush )) return 0;
36 switch (logbrush.lbStyle) {
37 case BS_SOLID:
38 case BS_HATCHED:
39 case BS_NULL:
41 EMRCREATEBRUSHINDIRECT emr;
42 emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
43 emr.emr.nSize = sizeof(emr);
44 emr.ihBrush = index = EMFDRV_AddHandleDC( dc );
45 emr.lb = logbrush;
47 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
48 index = 0;
50 break;
51 case BS_DIBPATTERN:
53 EMRCREATEDIBPATTERNBRUSHPT *emr;
54 DWORD bmSize, biSize, size;
55 BITMAPINFO *info = GlobalLock16(logbrush.lbHatch);
57 if (info->bmiHeader.biCompression)
58 bmSize = info->bmiHeader.biSizeImage;
59 else
60 bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
61 info->bmiHeader.biHeight,
62 info->bmiHeader.biBitCount);
63 biSize = DIB_BitmapInfoSize(info, LOWORD(logbrush.lbColor));
64 size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
65 emr = HeapAlloc( GetProcessHeap(), 0, size );
66 if(!emr) break;
67 emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
68 emr->emr.nSize = size;
69 emr->ihBrush = index = EMFDRV_AddHandleDC( dc );
70 emr->iUsage = LOWORD(logbrush.lbColor);
71 emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
72 emr->cbBmi = biSize;
73 emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
74 memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
75 biSize + bmSize );
77 if(!EMFDRV_WriteRecord( dc, &emr->emr ))
78 index = 0;
79 HeapFree( GetProcessHeap(), 0, emr );
80 GlobalUnlock16(logbrush.lbHatch);
82 break;
84 case BS_PATTERN:
85 FIXME("Unsupported style %x\n",
86 logbrush.lbStyle);
87 break;
88 default:
89 FIXME("Unknown style %x\n", logbrush.lbStyle);
90 break;
92 return index;
96 /***********************************************************************
97 * EMFDRV_BRUSH_SelectObject
99 static HBRUSH EMFDRV_BRUSH_SelectObject(DC *dc, HBRUSH hBrush )
101 EMRSELECTOBJECT emr;
102 DWORD index;
103 HBRUSH hOldBrush;
104 int i;
106 /* If the object is a stock brush object, do not need to create it.
107 * See definitions in wingdi.h for range of stock brushes.
108 * We do however have to handle setting the higher order bit to
109 * designate that this is a stock object.
111 for (i = WHITE_BRUSH; i <= NULL_BRUSH; i++)
113 if (hBrush == GetStockObject(i))
115 index = i | 0x80000000;
116 goto found;
119 if (!(index = EMFDRV_CreateBrushIndirect(dc, hBrush ))) return 0;
121 found:
122 emr.emr.iType = EMR_SELECTOBJECT;
123 emr.emr.nSize = sizeof(emr);
124 emr.ihObject = index;
125 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
126 return FALSE;
128 hOldBrush = dc->hBrush;
129 dc->hBrush = hBrush;
130 return hOldBrush;
134 /******************************************************************
135 * EMFDRV_CreateFontIndirect
137 static BOOL EMFDRV_CreateFontIndirect(DC *dc, HFONT hFont )
139 DWORD index = 0;
140 EMREXTCREATEFONTINDIRECTW emr;
141 int i;
143 if (!GetObjectW( hFont, sizeof(emr.elfw.elfLogFont), &emr.elfw.elfLogFont )) return 0;
145 emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
146 emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
147 emr.ihFont = index = EMFDRV_AddHandleDC( dc );
148 emr.elfw.elfFullName[0] = '\0';
149 emr.elfw.elfStyle[0] = '\0';
150 emr.elfw.elfVersion = 0;
151 emr.elfw.elfStyleSize = 0;
152 emr.elfw.elfMatch = 0;
153 emr.elfw.elfReserved = 0;
154 for(i = 0; i < ELF_VENDOR_SIZE; i++)
155 emr.elfw.elfVendorId[i] = 0;
156 emr.elfw.elfCulture = PAN_CULTURE_LATIN;
157 emr.elfw.elfPanose.bFamilyType = PAN_NO_FIT;
158 emr.elfw.elfPanose.bSerifStyle = PAN_NO_FIT;
159 emr.elfw.elfPanose.bWeight = PAN_NO_FIT;
160 emr.elfw.elfPanose.bProportion = PAN_NO_FIT;
161 emr.elfw.elfPanose.bContrast = PAN_NO_FIT;
162 emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
163 emr.elfw.elfPanose.bArmStyle = PAN_NO_FIT;
164 emr.elfw.elfPanose.bLetterform = PAN_NO_FIT;
165 emr.elfw.elfPanose.bMidline = PAN_NO_FIT;
166 emr.elfw.elfPanose.bXHeight = PAN_NO_FIT;
168 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
169 index = 0;
170 return index;
174 /***********************************************************************
175 * EMFDRV_FONT_SelectObject
177 static HFONT EMFDRV_FONT_SelectObject( DC * dc, HFONT hFont )
179 EMRSELECTOBJECT emr;
180 DWORD index;
181 HFONT hOldFont;
182 int i;
184 /* If the object is a stock font object, do not need to create it.
185 * See definitions in wingdi.h for range of stock fonts.
186 * We do however have to handle setting the higher order bit to
187 * designate that this is a stock object.
190 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
192 if (i != DEFAULT_PALETTE && hFont == GetStockObject(i))
194 index = i | 0x80000000;
195 goto found;
198 if (!(index = EMFDRV_CreateFontIndirect(dc, hFont ))) return 0;
199 found:
200 emr.emr.iType = EMR_SELECTOBJECT;
201 emr.emr.nSize = sizeof(emr);
202 emr.ihObject = index;
203 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
204 return FALSE;
206 hOldFont = dc->hFont;
207 dc->hFont = hFont;
208 return hOldFont;
213 /******************************************************************
214 * EMFDRV_CreatePenIndirect
216 static HPEN EMFDRV_CreatePenIndirect(DC *dc, HPEN hPen )
218 EMRCREATEPEN emr;
219 DWORD index = 0;
221 if (!GetObjectA( hPen, sizeof(emr.lopn), &emr.lopn )) return 0;
223 emr.emr.iType = EMR_CREATEPEN;
224 emr.emr.nSize = sizeof(emr);
225 emr.ihPen = index = EMFDRV_AddHandleDC( dc );
227 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
228 index = 0;
229 return index;
232 /******************************************************************
233 * EMFDRV_PEN_SelectObject
235 static HPEN EMFDRV_PEN_SelectObject(DC *dc, HPEN hPen )
237 EMRSELECTOBJECT emr;
238 DWORD index;
239 HPEN hOldPen;
240 int i;
242 /* If the object is a stock pen object, do not need to create it.
243 * See definitions in wingdi.h for range of stock pens.
244 * We do however have to handle setting the higher order bit to
245 * designate that this is a stock object.
248 for (i = WHITE_PEN; i <= NULL_PEN; i++)
250 if (hPen == GetStockObject(i))
252 index = i | 0x80000000;
253 goto found;
256 if (!(index = EMFDRV_CreatePenIndirect(dc, hPen ))) return 0;
257 found:
258 emr.emr.iType = EMR_SELECTOBJECT;
259 emr.emr.nSize = sizeof(emr);
260 emr.ihObject = index;
261 if(!EMFDRV_WriteRecord( dc, &emr.emr ))
262 return FALSE;
264 hOldPen = dc->hPen;
265 dc->hPen = hPen;
266 return hOldPen;
270 /***********************************************************************
271 * EMFDRV_SelectObject
273 HGDIOBJ EMFDRV_SelectObject( DC *dc, HGDIOBJ handle )
275 GDIOBJHDR * ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
276 HGDIOBJ ret = 0;
278 if (!ptr) return 0;
279 TRACE("hdc=%04x %04x\n", dc->hSelf, handle );
281 switch(GDIMAGIC(ptr->wMagic))
283 case PEN_MAGIC:
284 ret = EMFDRV_PEN_SelectObject( dc, handle );
285 break;
286 case BRUSH_MAGIC:
287 ret = EMFDRV_BRUSH_SelectObject( dc, handle );
288 break;
289 case FONT_MAGIC:
290 ret = EMFDRV_FONT_SelectObject( dc, handle );
291 break;
292 case BITMAP_MAGIC:
293 ret = EMFDRV_BITMAP_SelectObject( dc, handle );
294 break;
296 GDI_ReleaseObj( handle );
297 return ret;