No longer directly accessing debuggee memory.
[wine] / graphics / enhmetafiledrv / objects.c
1 /*
2  * Enhanced MetaFile objects
3  *
4  * Copyright 1999 Huw D M Davies
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "bitmap.h"
12 #include "brush.h"
13 #include "font.h"
14 #include "enhmetafiledrv.h"
15 #include "pen.h"
16 #include "debugtools.h"
17 #include "heap.h"
18
19 DEFAULT_DEBUG_CHANNEL(enhmetafile)
20
21 /***********************************************************************
22  *           EMFDRV_BITMAP_SelectObject
23  */
24 static HBITMAP EMFDRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap )
25 {
26     return 0;
27 }
28
29
30 /***********************************************************************
31  *           EMFDRV_CreateBrushIndirect
32  */
33 DWORD EMFDRV_CreateBrushIndirect( DC *dc, HBRUSH hBrush )
34 {
35     DWORD index = 0;
36     BRUSHOBJ *brushObj = (BRUSHOBJ *)GDI_GetObjPtr( hBrush, BRUSH_MAGIC );
37
38     switch (brushObj->logbrush.lbStyle) {
39     case BS_SOLID:
40     case BS_HATCHED:
41     case BS_NULL:
42       {
43         EMRCREATEBRUSHINDIRECT emr;
44         emr.emr.iType = EMR_CREATEBRUSHINDIRECT;
45         emr.emr.nSize = sizeof(emr);
46         emr.ihBrush = index = EMFDRV_AddHandleDC( dc );
47         emr.lb = brushObj->logbrush;
48
49         if(!EMFDRV_WriteRecord( dc, &emr.emr ))
50             index = 0;
51       }
52       break;
53     case BS_DIBPATTERN:
54       {
55         EMRCREATEDIBPATTERNBRUSHPT *emr;
56         DWORD bmSize, biSize, size;
57         BITMAPINFO *info = GlobalLock16(brushObj->logbrush.lbHatch);
58
59         if (info->bmiHeader.biCompression)
60             bmSize = info->bmiHeader.biSizeImage;
61         else
62             bmSize = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
63                                           info->bmiHeader.biHeight,
64                                           info->bmiHeader.biBitCount);
65         biSize = DIB_BitmapInfoSize(info, LOWORD(brushObj->logbrush.lbColor)); 
66         size = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize + bmSize;
67         emr = HeapAlloc( GetProcessHeap(), 0, size );
68         if(!emr) break;
69         emr->emr.iType = EMR_CREATEDIBPATTERNBRUSHPT;
70         emr->emr.nSize = size;
71         emr->ihBrush = index = EMFDRV_AddHandleDC( dc );
72         emr->iUsage = LOWORD(brushObj->logbrush.lbColor);
73         emr->offBmi = sizeof(EMRCREATEDIBPATTERNBRUSHPT);
74         emr->cbBmi = biSize;
75         emr->offBits = sizeof(EMRCREATEDIBPATTERNBRUSHPT) + biSize;
76         memcpy((char *)emr + sizeof(EMRCREATEDIBPATTERNBRUSHPT), info,
77                biSize + bmSize ); 
78
79         if(!EMFDRV_WriteRecord( dc, &emr->emr ))
80             index = 0;
81         HeapFree( GetProcessHeap(), 0, emr );
82         GlobalUnlock16(brushObj->logbrush.lbHatch);
83       }
84       break;
85
86     case BS_PATTERN:
87         FIXME("Unsupported style %x\n",
88               brushObj->logbrush.lbStyle);
89         break;
90     default:
91         FIXME("Unknown style %x\n", brushObj->logbrush.lbStyle);
92         return FALSE;
93     }
94     GDI_HEAP_UNLOCK( hBrush );
95     return index;
96 }
97
98
99 /***********************************************************************
100  *           EMFDRV_BRUSH_SelectObject
101  */
102 static HBRUSH EMFDRV_BRUSH_SelectObject(DC *dc, HBRUSH hBrush )
103 {
104     EMRSELECTOBJECT emr;
105     DWORD index;
106     HBRUSH hOldBrush;
107
108     index = EMFDRV_CreateBrushIndirect(dc, hBrush );
109     if(!index) return FALSE;
110     
111     emr.emr.iType = EMR_SELECTOBJECT;
112     emr.emr.nSize = sizeof(emr);
113     emr.ihObject = index;
114     if(!EMFDRV_WriteRecord( dc, &emr.emr ))
115         return FALSE;
116
117     hOldBrush = dc->w.hBrush;
118     dc->w.hBrush = hBrush;
119     return hOldBrush;
120 }
121
122
123 /******************************************************************
124  *         EMFDRV_CreateFontIndirect
125  */
126 static BOOL EMFDRV_CreateFontIndirect(DC *dc, HFONT hFont )
127 {
128     DWORD index = 0;
129     FONTOBJ *fontObj = (FONTOBJ *)GDI_GetObjPtr( hFont, FONT_MAGIC );
130     EMREXTCREATEFONTINDIRECTW emr;
131     int i;
132     emr.emr.iType = EMR_EXTCREATEFONTINDIRECTW;
133     emr.emr.nSize = (sizeof(emr) + 3) / 4 * 4;
134     emr.ihFont = index = EMFDRV_AddHandleDC( dc );
135     FONT_LogFont16To32W( &(fontObj->logfont), &(emr.elfw.elfLogFont) );
136     emr.elfw.elfFullName[0] = '\0';
137     emr.elfw.elfStyle[0]    = '\0';
138     emr.elfw.elfVersion     = 0;
139     emr.elfw.elfStyleSize   = 0;
140     emr.elfw.elfMatch       = 0;
141     emr.elfw.elfReserved    = 0;
142     for(i = 0; i < ELF_VENDOR_SIZE; i++)
143         emr.elfw.elfVendorId[i] = 0;
144     emr.elfw.elfCulture                 = PAN_CULTURE_LATIN;
145     emr.elfw.elfPanose.bFamilyType      = PAN_NO_FIT;
146     emr.elfw.elfPanose.bSerifStyle      = PAN_NO_FIT;
147     emr.elfw.elfPanose.bWeight          = PAN_NO_FIT;
148     emr.elfw.elfPanose.bProportion      = PAN_NO_FIT;
149     emr.elfw.elfPanose.bContrast        = PAN_NO_FIT;
150     emr.elfw.elfPanose.bStrokeVariation = PAN_NO_FIT;
151     emr.elfw.elfPanose.bArmStyle        = PAN_NO_FIT;
152     emr.elfw.elfPanose.bLetterform      = PAN_NO_FIT;
153     emr.elfw.elfPanose.bMidline         = PAN_NO_FIT;
154     emr.elfw.elfPanose.bXHeight         = PAN_NO_FIT;
155
156     if(!EMFDRV_WriteRecord( dc, &emr.emr ))
157         index = 0;
158     GDI_HEAP_UNLOCK( hFont );
159     return index;
160 }
161
162
163 /***********************************************************************
164  *           EMFDRV_FONT_SelectObject
165  */
166 static HFONT EMFDRV_FONT_SelectObject( DC * dc, HFONT hFont )
167 {
168     EMRSELECTOBJECT emr;
169     DWORD index;
170     HFONT hOldFont;
171
172     index = EMFDRV_CreateFontIndirect(dc, hFont );
173     if(!index) return FALSE;
174
175     emr.emr.iType = EMR_SELECTOBJECT;
176     emr.emr.nSize = sizeof(emr);
177     emr.ihObject = index;
178     if(!EMFDRV_WriteRecord( dc, &emr.emr ))
179         return FALSE;
180
181     hOldFont = dc->w.hFont;
182     dc->w.hFont = hFont;
183     return hOldFont;
184 }
185
186
187
188 /******************************************************************
189  *         EMFDRV_CreatePenIndirect
190  */
191 static HPEN EMFDRV_CreatePenIndirect(DC *dc, HPEN hPen )
192 {
193     EMRCREATEPEN emr;
194     PENOBJ *penObj = (PENOBJ *)GDI_GetObjPtr( hPen, PEN_MAGIC );
195     DWORD index = 0;
196
197     emr.emr.iType = EMR_CREATEPEN;
198     emr.emr.nSize = sizeof(emr);
199     emr.ihPen = index = EMFDRV_AddHandleDC( dc );
200     emr.lopn = penObj->logpen;
201
202     if(!EMFDRV_WriteRecord( dc, &emr.emr ))
203         index = 0;
204     GDI_HEAP_UNLOCK( hPen );
205     return index;
206 }
207
208 /******************************************************************
209  *         EMFDRV_PEN_SelectObject
210  */
211 static HPEN EMFDRV_PEN_SelectObject(DC *dc, HPEN hPen )
212 {
213     EMRSELECTOBJECT emr;
214     DWORD index;
215     HFONT hOldPen;
216
217     index = EMFDRV_CreatePenIndirect(dc, hPen );
218     if(!index) return FALSE;
219     
220     emr.emr.iType = EMR_SELECTOBJECT;
221     emr.emr.nSize = sizeof(emr);
222     emr.ihObject = index;
223     if(!EMFDRV_WriteRecord( dc, &emr.emr ))
224         return FALSE;
225
226     hOldPen = dc->w.hPen;
227     dc->w.hPen = hPen;
228     return hOldPen; 
229 }
230
231
232 /***********************************************************************
233  *           EMFDRV_SelectObject
234  */
235 HGDIOBJ EMFDRV_SelectObject( DC *dc, HGDIOBJ handle )
236 {
237     GDIOBJHDR * ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
238     HGDIOBJ ret = 0;
239
240     if (!ptr) return 0;
241     TRACE("hdc=%04x %04x\n", dc->hSelf, handle );
242     
243     switch(ptr->wMagic)
244     {
245       case PEN_MAGIC:
246           ret = EMFDRV_PEN_SelectObject( dc, handle );
247           break;
248       case BRUSH_MAGIC:
249           ret = EMFDRV_BRUSH_SelectObject( dc, handle );
250           break;
251       case FONT_MAGIC:
252           ret = EMFDRV_FONT_SelectObject( dc, handle );
253           break;
254       case BITMAP_MAGIC:
255           ret = EMFDRV_BITMAP_SelectObject( dc, handle );
256           break;
257     }
258     GDI_HEAP_UNLOCK( handle );
259     return ret;
260 }
261
262