dplayx: Introduce impl_from_IDirectPlayLobby3A().
[wine] / dlls / dwrite / gdiinterop.c
1 /*
2  *    GDI Interop
3  *
4  * Copyright 2012 Nikolay Sivov for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "dwrite.h"
29 #include "dwrite_private.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dwrite);
34
35 struct rendertarget {
36     IDWriteBitmapRenderTarget IDWriteBitmapRenderTarget_iface;
37     LONG ref;
38
39     SIZE size;
40     HDC hdc;
41 };
42
43 static inline struct rendertarget *impl_from_IDWriteBitmapRenderTarget(IDWriteBitmapRenderTarget *iface)
44 {
45     return CONTAINING_RECORD(iface, struct rendertarget, IDWriteBitmapRenderTarget_iface);
46 }
47
48 static HRESULT WINAPI rendertarget_QueryInterface(IDWriteBitmapRenderTarget *iface, REFIID riid, void **obj)
49 {
50     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
51
52     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
53
54     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteBitmapRenderTarget))
55     {
56         *obj = iface;
57         IDWriteBitmapRenderTarget_AddRef(iface);
58         return S_OK;
59     }
60
61     *obj = NULL;
62
63     return E_NOINTERFACE;
64 }
65
66 static ULONG WINAPI rendertarget_AddRef(IDWriteBitmapRenderTarget *iface)
67 {
68     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
69     ULONG ref = InterlockedIncrement(&This->ref);
70     TRACE("(%p)->(%d)\n", This, ref);
71     return ref;
72 }
73
74 static ULONG WINAPI rendertarget_Release(IDWriteBitmapRenderTarget *iface)
75 {
76     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
77     ULONG ref = InterlockedDecrement(&This->ref);
78
79     TRACE("(%p)->(%d)\n", This, ref);
80
81     if (!ref)
82     {
83         DeleteDC(This->hdc);
84         heap_free(This);
85     }
86
87     return ref;
88 }
89
90 static HRESULT WINAPI rendertarget_DrawGlyphRun(IDWriteBitmapRenderTarget *iface,
91     FLOAT baselineOriginX, FLOAT baselineOriginY, DWRITE_MEASURING_MODE measuring_mode,
92     DWRITE_GLYPH_RUN const* glyph_run, IDWriteRenderingParams* params, COLORREF textColor,
93     RECT *blackbox_rect)
94 {
95     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
96     FIXME("(%p)->(%f %f %d %p %p 0x%08x %p): stub\n", This, baselineOriginX, baselineOriginY,
97         measuring_mode, glyph_run, params, textColor, blackbox_rect);
98     return E_NOTIMPL;
99 }
100
101 static HDC WINAPI rendertarget_GetMemoryDC(IDWriteBitmapRenderTarget *iface)
102 {
103     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
104     TRACE("(%p)\n", This);
105     return This->hdc;
106 }
107
108 static FLOAT WINAPI rendertarget_GetPixelsPerDip(IDWriteBitmapRenderTarget *iface)
109 {
110     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
111     FIXME("(%p): stub\n", This);
112     return 1.0;
113 }
114
115 static HRESULT WINAPI rendertarget_SetPixelsPerDip(IDWriteBitmapRenderTarget *iface, FLOAT pixels_per_dip)
116 {
117     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
118     FIXME("(%p)->(%f): stub\n", This, pixels_per_dip);
119     return E_NOTIMPL;
120 }
121
122 static HRESULT WINAPI rendertarget_GetCurrentTransform(IDWriteBitmapRenderTarget *iface, DWRITE_MATRIX *transform)
123 {
124     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
125     FIXME("(%p)->(%p): stub\n", This, transform);
126     return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI rendertarget_SetCurrentTransform(IDWriteBitmapRenderTarget *iface, DWRITE_MATRIX const *transform)
130 {
131     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
132     FIXME("(%p)->(%p): stub\n", This, transform);
133     return E_NOTIMPL;
134 }
135
136 static HRESULT WINAPI rendertarget_GetSize(IDWriteBitmapRenderTarget *iface, SIZE *size)
137 {
138     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
139
140     TRACE("(%p)->(%p)\n", This, size);
141     *size = This->size;
142     return S_OK;
143 }
144
145 static HRESULT WINAPI rendertarget_Resize(IDWriteBitmapRenderTarget *iface, UINT32 width, UINT32 height)
146 {
147     struct rendertarget *This = impl_from_IDWriteBitmapRenderTarget(iface);
148     FIXME("(%p)->(%u %u): stub\n", This, width, height);
149     return E_NOTIMPL;
150 }
151
152 static const IDWriteBitmapRenderTargetVtbl rendertargetvtbl = {
153     rendertarget_QueryInterface,
154     rendertarget_AddRef,
155     rendertarget_Release,
156     rendertarget_DrawGlyphRun,
157     rendertarget_GetMemoryDC,
158     rendertarget_GetPixelsPerDip,
159     rendertarget_SetPixelsPerDip,
160     rendertarget_GetCurrentTransform,
161     rendertarget_SetCurrentTransform,
162     rendertarget_GetSize,
163     rendertarget_Resize
164 };
165
166 static HRESULT create_rendertarget(HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
167 {
168     char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
169     BITMAPINFO *bmi = (BITMAPINFO*)bmibuf;
170     struct rendertarget *This;
171     HBITMAP dib;
172
173     *target = NULL;
174
175     This = heap_alloc(sizeof(struct rendertarget));
176     if (!This) return E_OUTOFMEMORY;
177
178     This->IDWriteBitmapRenderTarget_iface.lpVtbl = &rendertargetvtbl;
179     This->ref = 1;
180
181     This->size.cx = width;
182     This->size.cy = height;
183
184     This->hdc = CreateCompatibleDC(hdc);
185
186     memset(bmi, 0, sizeof(bmibuf));
187     bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
188     bmi->bmiHeader.biHeight = height;
189     bmi->bmiHeader.biWidth = width;
190     bmi->bmiHeader.biBitCount = 32;
191     bmi->bmiHeader.biPlanes = 1;
192     bmi->bmiHeader.biCompression = BI_RGB;
193
194     dib = CreateDIBSection(This->hdc, bmi, DIB_RGB_COLORS, NULL, NULL, 0);
195     SelectObject(This->hdc, dib);
196
197     *target = &This->IDWriteBitmapRenderTarget_iface;
198
199     return S_OK;
200 }
201
202 static HRESULT WINAPI gdiinterop_QueryInterface(IDWriteGdiInterop *iface, REFIID riid, void **obj)
203 {
204     TRACE("(%s %p)\n", debugstr_guid(riid), obj);
205
206     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDWriteGdiInterop))
207     {
208         *obj = iface;
209         return S_OK;
210     }
211
212     *obj = NULL;
213
214     return E_NOINTERFACE;
215 }
216
217 static ULONG WINAPI gdiinterop_AddRef(IDWriteGdiInterop *iface)
218 {
219     return 2;
220 }
221
222 static ULONG WINAPI gdiinterop_Release(IDWriteGdiInterop *iface)
223 {
224     return 1;
225 }
226
227 static HRESULT WINAPI gdiinterop_CreateFontFromLOGFONT(IDWriteGdiInterop *iface,
228     LOGFONTW const *logfont, IDWriteFont **font)
229 {
230     TRACE("(%p %p)\n", logfont, font);
231
232     if (!logfont) return E_INVALIDARG;
233
234     return create_font_from_logfont(logfont, font);
235 }
236
237 static HRESULT WINAPI gdiinterop_ConvertFontToLOGFONT(IDWriteGdiInterop *iface,
238     IDWriteFont *font, LOGFONTW *logfont, BOOL *is_systemfont)
239 {
240     FIXME("(%p %p %p): stub\n", font, logfont, is_systemfont);
241     return E_NOTIMPL;
242 }
243
244 static HRESULT WINAPI gdiinterop_ConvertFontFaceToLOGFONT(IDWriteGdiInterop *iface,
245     IDWriteFontFace *font, LOGFONTW *logfont)
246 {
247     FIXME("(%p %p): stub\n", font, logfont);
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI gdiinterop_CreateFontFaceFromHdc(IDWriteGdiInterop *iface,
252     HDC hdc, IDWriteFontFace **fontface)
253 {
254     FIXME("(%p %p): stub\n", hdc, fontface);
255     return E_NOTIMPL;
256 }
257
258 static HRESULT WINAPI gdiinterop_CreateBitmapRenderTarget(IDWriteGdiInterop *iface,
259     HDC hdc, UINT32 width, UINT32 height, IDWriteBitmapRenderTarget **target)
260 {
261     TRACE("(%p %u %u %p)\n", hdc, width, height, target);
262     return create_rendertarget(hdc, width, height, target);
263 }
264
265 static const struct IDWriteGdiInteropVtbl gdiinteropvtbl = {
266     gdiinterop_QueryInterface,
267     gdiinterop_AddRef,
268     gdiinterop_Release,
269     gdiinterop_CreateFontFromLOGFONT,
270     gdiinterop_ConvertFontToLOGFONT,
271     gdiinterop_ConvertFontFaceToLOGFONT,
272     gdiinterop_CreateFontFaceFromHdc,
273     gdiinterop_CreateBitmapRenderTarget
274 };
275
276 static IDWriteGdiInterop gdiinterop = { &gdiinteropvtbl };
277
278 HRESULT get_gdiinterop(IDWriteGdiInterop **ret)
279 {
280     *ret = &gdiinterop;
281     return S_OK;
282 }