comctl32: Shed unused parameter from TOOLTIPS_NCCreate.
[wine] / dlls / d3dx9_36 / font.c
1 /*
2  * Copyright (C) 2008 Tony Wasserka
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25 #include "d3dx9_36_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28
29 static HRESULT WINAPI ID3DXFontImpl_QueryInterface(LPD3DXFONT iface, REFIID riid, LPVOID *object)
30 {
31     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
32
33     TRACE("(%p): QueryInterface from %s\n", This, debugstr_guid(riid));
34     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXFont)) {
35         IUnknown_AddRef(iface);
36         *object=This;
37         return S_OK;
38     }
39     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI ID3DXFontImpl_AddRef(LPD3DXFONT iface)
44 {
45     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
46     ULONG ref=InterlockedIncrement(&This->ref);
47     TRACE("(%p)->(): AddRef from %d\n", This, ref-1);
48     return ref;
49 }
50
51 static ULONG WINAPI ID3DXFontImpl_Release(LPD3DXFONT iface)
52 {
53     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
54     ULONG ref=InterlockedDecrement(&This->ref);
55
56     TRACE("(%p)->(): ReleaseRef to %d\n", This, ref);
57
58     if(ref==0) {
59         DeleteObject(This->hfont);
60         DeleteDC(This->hdc);
61         IDirect3DDevice9_Release(This->device);
62         HeapFree(GetProcessHeap(), 0, This);
63     }
64     return ref;
65 }
66
67 static HRESULT WINAPI ID3DXFontImpl_GetDevice(LPD3DXFONT iface, LPDIRECT3DDEVICE9 *device)
68 {
69     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
70
71     TRACE("(%p)->(%p)\n", This, device);
72
73     if( !device ) return D3DERR_INVALIDCALL;
74     *device = This->device;
75     IDirect3DDevice9_AddRef(This->device);
76
77     return D3D_OK;
78 }
79
80 static HRESULT WINAPI ID3DXFontImpl_GetDescA(LPD3DXFONT iface, D3DXFONT_DESCA *desc)
81 {
82     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
83
84     TRACE("(%p)->(%p)\n", This, desc);
85
86     if( !desc ) return D3DERR_INVALIDCALL;
87     memcpy(desc, &This->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
88     WideCharToMultiByte(CP_ACP, 0, This->desc.FaceName, -1, desc->FaceName, sizeof(desc->FaceName) / sizeof(CHAR), NULL, NULL);
89
90     return D3D_OK;
91 }
92
93 static HRESULT WINAPI ID3DXFontImpl_GetDescW(LPD3DXFONT iface, D3DXFONT_DESCW *desc)
94 {
95     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
96
97     TRACE("(%p)->(%p)\n", This, desc);
98
99     if( !desc ) return D3DERR_INVALIDCALL;
100     *desc = This->desc;
101
102     return D3D_OK;
103 }
104
105 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(LPD3DXFONT iface, TEXTMETRICA *metrics)
106 {
107     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
108     TRACE("(%p)->(%p)\n", This, metrics);
109     return GetTextMetricsA(This->hdc, metrics);
110 }
111
112 static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(LPD3DXFONT iface, TEXTMETRICW *metrics)
113 {
114     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
115     TRACE("(%p)->(%p)\n", This, metrics);
116     return GetTextMetricsW(This->hdc, metrics);
117 }
118
119 static HDC WINAPI ID3DXFontImpl_GetDC(LPD3DXFONT iface)
120 {
121     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
122     TRACE("(%p)->()\n", This);
123     return This->hdc;
124 }
125
126 static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(LPD3DXFONT iface, UINT glyph, LPDIRECT3DTEXTURE9 *texture, RECT *blackbox, POINT *cellinc)
127 {
128     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
129     FIXME("(%p)->(%u, %p, %p, %p): stub\n", This, glyph, texture, blackbox, cellinc);
130     return D3D_OK;
131 }
132
133 static HRESULT WINAPI ID3DXFontImpl_PreloadCharacters(LPD3DXFONT iface, UINT first, UINT last)
134 {
135     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
136     FIXME("(%p)->(%u, %u): stub\n", This, first, last);
137     return D3D_OK;
138 }
139
140 static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(LPD3DXFONT iface, UINT first, UINT last)
141 {
142     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
143     FIXME("(%p)->(%u, %u): stub\n", This, first, last);
144     return D3D_OK;
145 }
146
147 static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(LPD3DXFONT iface, LPCSTR string, INT count)
148 {
149     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
150     FIXME("(%p)->(%s, %d): stub\n", This, string, count);
151     return D3D_OK;
152 }
153
154 static HRESULT WINAPI ID3DXFontImpl_PreloadTextW(LPD3DXFONT iface, LPCWSTR string, INT count)
155 {
156     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
157     FIXME("(%p)->(%s, %d): stub\n", This, debugstr_w(string), count);
158     return D3D_OK;
159 }
160
161 static INT WINAPI ID3DXFontImpl_DrawTextA(LPD3DXFONT iface, LPD3DXSPRITE sprite, LPCSTR string, INT count, LPRECT rect, DWORD format, D3DCOLOR color)
162 {
163     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
164     FIXME("(%p)->(%p, %s, %d, %p, %d, %#x): stub\n", This, sprite, string, count, rect,  format, color);
165     return 1;
166 }
167
168 static INT WINAPI ID3DXFontImpl_DrawTextW(LPD3DXFONT iface, LPD3DXSPRITE sprite, LPCWSTR string, INT count, LPRECT rect, DWORD format, D3DCOLOR color)
169 {
170     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
171     FIXME("(%p)->(%p, %s, %d, %p, %d, %#x): stub\n", This, sprite, debugstr_w(string), count, rect,  format, color);
172     return 1;
173 }
174
175 static HRESULT WINAPI ID3DXFontImpl_OnLostDevice(LPD3DXFONT iface)
176 {
177     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
178     FIXME("(%p)->(): stub\n", This);
179     return D3D_OK;
180 }
181
182 static HRESULT WINAPI ID3DXFontImpl_OnResetDevice(LPD3DXFONT iface)
183 {
184     ID3DXFontImpl *This=(ID3DXFontImpl*)iface;
185     FIXME("(%p)->(): stub\n", This);
186     return D3D_OK;
187 }
188
189 static const ID3DXFontVtbl D3DXFont_Vtbl =
190 {
191     /*** IUnknown methods ***/
192     ID3DXFontImpl_QueryInterface,
193     ID3DXFontImpl_AddRef,
194     ID3DXFontImpl_Release,
195     /*** ID3DXFont methods ***/
196     ID3DXFontImpl_GetDevice,
197     ID3DXFontImpl_GetDescA,
198     ID3DXFontImpl_GetDescW,
199     ID3DXFontImpl_GetTextMetricsA,
200     ID3DXFontImpl_GetTextMetricsW,
201     ID3DXFontImpl_GetDC,
202     ID3DXFontImpl_GetGlyphData,
203     ID3DXFontImpl_PreloadCharacters,
204     ID3DXFontImpl_PreloadGlyphs,
205     ID3DXFontImpl_PreloadTextA,
206     ID3DXFontImpl_PreloadTextW,
207     ID3DXFontImpl_DrawTextA,
208     ID3DXFontImpl_DrawTextW,
209     ID3DXFontImpl_OnLostDevice,
210     ID3DXFontImpl_OnResetDevice
211 };
212
213 HRESULT WINAPI D3DXCreateFontA(LPDIRECT3DDEVICE9 device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
214                                DWORD precision, DWORD quality, DWORD pitchandfamily, LPCSTR facename, LPD3DXFONT *font)
215 {
216     D3DXFONT_DESCA desc;
217
218     if( !device || !font ) return D3DERR_INVALIDCALL;
219
220     desc.Height=height;
221     desc.Width=width;
222     desc.Weight=weight;
223     desc.MipLevels=miplevels;
224     desc.Italic=italic;
225     desc.CharSet=charset;
226     desc.OutputPrecision=precision;
227     desc.Quality=quality;
228     desc.PitchAndFamily=pitchandfamily;
229     if(facename != NULL) lstrcpyA(desc.FaceName, facename);
230     else desc.FaceName[0] = '\0';
231
232     return D3DXCreateFontIndirectA(device, &desc, font);
233 }
234
235 HRESULT WINAPI D3DXCreateFontW(LPDIRECT3DDEVICE9 device, INT height, UINT width, UINT weight, UINT miplevels, BOOL italic, DWORD charset,
236                                DWORD precision, DWORD quality, DWORD pitchandfamily, LPCWSTR facename, LPD3DXFONT *font)
237 {
238     D3DXFONT_DESCW desc;
239
240     if( !device || !font ) return D3DERR_INVALIDCALL;
241
242     desc.Height=height;
243     desc.Width=width;
244     desc.Weight=weight;
245     desc.MipLevels=miplevels;
246     desc.Italic=italic;
247     desc.CharSet=charset;
248     desc.OutputPrecision=precision;
249     desc.Quality=quality;
250     desc.PitchAndFamily=pitchandfamily;
251     if(facename != NULL) strcpyW(desc.FaceName, facename);
252     else desc.FaceName[0] = '\0';
253
254     return D3DXCreateFontIndirectW(device, &desc, font);
255 }
256
257 /***********************************************************************
258  *           D3DXCreateFontIndirectA    (D3DX9_36.@)
259  */
260 HRESULT WINAPI D3DXCreateFontIndirectA(LPDIRECT3DDEVICE9 device, CONST D3DXFONT_DESCA *desc, LPD3DXFONT *font)
261 {
262     D3DXFONT_DESCW widedesc;
263
264     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
265
266     /* Copy everything but the last structure member. This requires the
267        two D3DXFONT_DESC structures to be equal until the FaceName member */
268     memcpy(&widedesc, desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
269     MultiByteToWideChar(CP_ACP, 0, desc->FaceName, -1,
270                         widedesc.FaceName, sizeof(widedesc.FaceName)/sizeof(WCHAR));
271     return D3DXCreateFontIndirectW(device, &widedesc, font);
272 }
273
274 /***********************************************************************
275  *           D3DXCreateFontIndirectW    (D3DX9_36.@)
276  */
277 HRESULT WINAPI D3DXCreateFontIndirectW(LPDIRECT3DDEVICE9 device, CONST D3DXFONT_DESCW *desc, LPD3DXFONT *font)
278 {
279     D3DDEVICE_CREATION_PARAMETERS cpars;
280     D3DDISPLAYMODE mode;
281     ID3DXFontImpl *object;
282     IDirect3D9 *d3d;
283     HRESULT hr;
284
285     FIXME("(%p, %p, %p): stub\n", device, desc, font);
286
287     if( !device || !desc || !font ) return D3DERR_INVALIDCALL;
288
289     /* the device MUST support D3DFMT_A8R8G8B8 */
290     IDirect3DDevice9_GetDirect3D(device, &d3d);
291     IDirect3DDevice9_GetCreationParameters(device, &cpars);
292     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
293     hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
294     if(FAILED(hr)) {
295         IDirect3D9_Release(d3d);
296         return D3DXERR_INVALIDDATA;
297     }
298     IDirect3D9_Release(d3d);
299
300     object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXFontImpl));
301     if(object==NULL) {
302         *font=NULL;
303         return E_OUTOFMEMORY;
304     }
305     object->lpVtbl=&D3DXFont_Vtbl;
306     object->ref=1;
307     object->device=device;
308     object->desc=*desc;
309
310     object->hdc = CreateCompatibleDC(NULL);
311     if( !object->hdc ) {
312         HeapFree(GetProcessHeap(), 0, object);
313         return D3DXERR_INVALIDDATA;
314     }
315
316     object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet,
317                                 desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName);
318     if( !object->hfont ) {
319         DeleteDC(object->hdc);
320         HeapFree(GetProcessHeap(), 0, object);
321         return D3DXERR_INVALIDDATA;
322     }
323     SelectObject(object->hdc, object->hfont);
324
325     IDirect3DDevice9_AddRef(device);
326     *font=(LPD3DXFONT)object;
327
328     return D3D_OK;
329 }