Implemented DrawTextW as Unicode and DrawTextA as call to DrawTextW.
[wine/gsoc_dplay.git] / dlls / oleaut32 / ole2disp.c
blob20a56ec1cdd7919586e2cdce0610d85343513649
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
6 #include <string.h>
7 #include "windef.h"
8 #include "wingdi.h"
9 #include "winuser.h"
10 #include "winerror.h"
11 #include "wine/windef16.h"
12 #include "ole2.h"
13 #include "olectl.h"
14 #include "oleauto.h"
15 #include "heap.h"
16 #include "ldt.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(ole);
21 /* This implementation of the BSTR API is 16-bit only. It
22 represents BSTR as a 16:16 far pointer, and the strings
23 as ISO-8859 */
25 /******************************************************************************
26 * BSTR_AllocBytes [Internal]
28 static BSTR16 BSTR_AllocBytes(int n)
30 void *ptr = SEGPTR_ALLOC(n);
31 return (BSTR16)SEGPTR_GET(ptr);
34 /******************************************************************************
35 * BSTR_Free [INTERNAL]
37 static void BSTR_Free(BSTR16 in)
39 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
42 /******************************************************************************
43 * BSTR_GetAddr [INTERNAL]
45 static void* BSTR_GetAddr(BSTR16 in)
47 return in ? PTR_SEG_TO_LIN(in) : 0;
50 /******************************************************************************
51 * SysAllocString16 [OLE2DISP.2]
53 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
55 BSTR16 out;
57 if (!in) return 0;
59 out = BSTR_AllocBytes(strlen(in)+1);
60 if(!out)return 0;
61 strcpy(BSTR_GetAddr(out),in);
62 return out;
65 /******************************************************************************
66 * SysAllocString [OLEAUT32.2]
68 BSTR WINAPI SysAllocString(LPCOLESTR in)
70 if (!in) return 0;
72 /* Delegate this to the SysAllocStringLen32 method. */
73 return SysAllocStringLen(in, lstrlenW(in));
76 /******************************************************************************
77 * SysReAllocString16 [OLE2DISP.3]
79 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
81 BSTR16 new=SysAllocString16(in);
82 BSTR_Free(*old);
83 *old=new;
84 return 1;
87 /******************************************************************************
88 * SysReAllocString [OLEAUT32.3]
90 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
93 * Sanity check
95 if (old==NULL)
96 return 0;
99 * Make sure we free the old string.
101 if (*old!=NULL)
102 SysFreeString(*old);
105 * Allocate the new string
107 *old = SysAllocString(in);
109 return 1;
112 /******************************************************************************
113 * SysAllocStringLen16 [OLE2DISP.4]
115 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
117 BSTR16 out=BSTR_AllocBytes(len+1);
119 if (!out)
120 return 0;
123 * Copy the information in the buffer.
124 * Since it is valid to pass a NULL pointer here, we'll initialize the
125 * buffer to nul if it is the case.
127 if (in != 0)
128 strcpy(BSTR_GetAddr(out),in);
129 else
130 memset(BSTR_GetAddr(out), 0, len+1);
132 return out;
135 /******************************************************************************
136 * SysAllocStringLen [OLEAUT32.4]
138 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
139 * section, he describes the DWORD value placed *before* the BSTR data type.
140 * he describes it as a "DWORD count of characters". By experimenting with
141 * a windows application, this count seems to be a DWORD count of bytes in
142 * the string. Meaning that the count is double the number of wide
143 * characters in the string.
145 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
147 DWORD bufferSize;
148 DWORD* newBuffer;
149 WCHAR* stringBuffer;
152 * Find the length of the buffer passed-in in bytes.
154 bufferSize = len * sizeof (WCHAR);
157 * Allocate a new buffer to hold the string.
158 * dont't forget to keep an empty spot at the beginning of the
159 * buffer for the character count and an extra character at the
160 * end for the NULL.
162 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
164 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
167 * If the memory allocation failed, return a null pointer.
169 if (newBuffer==0)
170 return 0;
173 * Copy the length of the string in the placeholder.
175 *newBuffer = bufferSize;
178 * Skip the byte count.
180 newBuffer++;
183 * Copy the information in the buffer.
184 * Since it is valid to pass a NULL pointer here, we'll initialize the
185 * buffer to nul if it is the case.
187 if (in != 0)
188 memcpy(newBuffer, in, bufferSize);
189 else
190 memset(newBuffer, 0, bufferSize);
193 * Make sure that there is a nul character at the end of the
194 * string.
196 stringBuffer = (WCHAR*)newBuffer;
197 stringBuffer[len] = L'\0';
199 return (LPWSTR)stringBuffer;
202 /******************************************************************************
203 * SysReAllocStringLen16 [OLE2DISP.5]
205 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
207 BSTR16 new=SysAllocStringLen16(in,len);
208 BSTR_Free(*old);
209 *old=new;
210 return 1;
214 /******************************************************************************
215 * SysReAllocStringLen [OLEAUT32.5]
217 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
220 * Sanity check
222 if (old==NULL)
223 return 0;
226 * Make sure we free the old string.
228 if (*old!=NULL)
229 SysFreeString(*old);
232 * Allocate the new string
234 *old = SysAllocStringLen(in, len);
236 return 1;
239 /******************************************************************************
240 * SysFreeString16 [OLE2DISP.6]
242 void WINAPI SysFreeString16(BSTR16 in)
244 BSTR_Free(in);
247 /******************************************************************************
248 * SysFreeString [OLEAUT32.6]
250 void WINAPI SysFreeString(BSTR in)
252 DWORD* bufferPointer;
254 /* NULL is a valid parameter */
255 if(!in) return;
258 * We have to be careful when we free a BSTR pointer, it points to
259 * the beginning of the string but it skips the byte count contained
260 * before the string.
262 bufferPointer = (DWORD*)in;
264 bufferPointer--;
267 * Free the memory from it's "real" origin.
269 HeapFree(GetProcessHeap(), 0, bufferPointer);
272 /******************************************************************************
273 * SysStringLen16 [OLE2DISP.7]
275 int WINAPI SysStringLen16(BSTR16 str)
277 return strlen(BSTR_GetAddr(str));
280 /******************************************************************************
281 * SysStringLen [OLEAUT32.7]
283 * The Windows documentation states that the length returned by this function
284 * is not necessarely the same as the length returned by the _lstrlenW method.
285 * It is the same number that was passed in as the "len" parameter if the
286 * string was allocated with a SysAllocStringLen method call.
288 int WINAPI SysStringLen(BSTR str)
290 DWORD* bufferPointer;
292 if (!str) return 0;
294 * The length of the string (in bytes) is contained in a DWORD placed
295 * just before the BSTR pointer
297 bufferPointer = (DWORD*)str;
299 bufferPointer--;
301 return (int)(*bufferPointer/sizeof(WCHAR));
304 /******************************************************************************
305 * SysStringByteLen [OLEAUT32.149]
307 * The Windows documentation states that the length returned by this function
308 * is not necessarely the same as the length returned by the _lstrlenW method.
309 * It is the same number that was passed in as the "len" parameter if the
310 * string was allocated with a SysAllocStringLen method call.
312 int WINAPI SysStringByteLen(BSTR str)
314 DWORD* bufferPointer;
316 if (!str) return 0;
318 * The length of the string (in bytes) is contained in a DWORD placed
319 * just before the BSTR pointer
321 bufferPointer = (DWORD*)str;
323 bufferPointer--;
325 return (int)(*bufferPointer);
328 /******************************************************************************
329 * CreateDispTypeInfo16 [OLE2DISP.31]
331 HRESULT WINAPI CreateDispTypeInfo16(
332 INTERFACEDATA *pidata,
333 LCID lcid,
334 ITypeInfo **pptinfo)
336 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
337 return 0;
340 /******************************************************************************
341 * CreateDispTypeInfo [OLE2DISP.31]
343 HRESULT WINAPI CreateDispTypeInfo(
344 INTERFACEDATA *pidata,
345 LCID lcid,
346 ITypeInfo **pptinfo)
348 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
349 return 0;
352 /******************************************************************************
353 * CreateStdDispatch16 [OLE2DISP.32]
355 HRESULT WINAPI CreateStdDispatch16(
356 IUnknown* punkOuter,
357 void* pvThis,
358 ITypeInfo* ptinfo,
359 IUnknown** ppunkStdDisp)
361 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
362 ppunkStdDisp);
363 return 0;
366 /******************************************************************************
367 * CreateStdDispatch [OLE2DISP.32]
369 HRESULT WINAPI CreateStdDispatch(
370 IUnknown* punkOuter,
371 void* pvThis,
372 ITypeInfo* ptinfo,
373 IUnknown** ppunkStdDisp)
375 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
376 ppunkStdDisp);
377 return 0;
380 /******************************************************************************
381 * RegisterActiveObject [OLE2DISP.35]
383 HRESULT WINAPI RegisterActiveObject16(
384 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
386 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
387 return 0;
390 /******************************************************************************
391 * OleTranslateColor [OLEAUT32.421]
393 * Converts an OLE_COLOR to a COLORREF.
394 * See the documentation for conversion rules.
395 * pColorRef can be NULL. In that case the user only wants to test the
396 * conversion.
398 HRESULT WINAPI OleTranslateColor(
399 OLE_COLOR clr,
400 HPALETTE hpal,
401 COLORREF* pColorRef)
403 COLORREF colorref;
404 BYTE b = HIBYTE(HIWORD(clr));
406 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
409 * In case pColorRef is NULL, provide our own to simplify the code.
411 if (pColorRef == NULL)
412 pColorRef = &colorref;
414 switch (b)
416 case 0x00:
418 if (hpal != 0)
419 *pColorRef = PALETTERGB(GetRValue(clr),
420 GetGValue(clr),
421 GetBValue(clr));
422 else
423 *pColorRef = clr;
425 break;
428 case 0x01:
430 if (hpal != 0)
432 PALETTEENTRY pe;
434 * Validate the palette index.
436 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
437 return E_INVALIDARG;
440 *pColorRef = clr;
442 break;
445 case 0x02:
446 *pColorRef = clr;
447 break;
449 case 0x80:
451 int index = LOBYTE(LOWORD(clr));
454 * Validate GetSysColor index.
456 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
457 return E_INVALIDARG;
459 *pColorRef = GetSysColor(index);
461 break;
464 default:
465 return E_INVALIDARG;
468 return S_OK;
471 /******************************************************************************
472 * SysAllocStringByteLen [OLEAUT32.150]
475 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
477 DWORD* newBuffer;
478 char* stringBuffer;
481 * Allocate a new buffer to hold the string.
482 * dont't forget to keep an empty spot at the begining of the
483 * buffer for the character count and an extra character at the
484 * end for the NULL.
486 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
488 len + sizeof(WCHAR) + sizeof(DWORD));
491 * If the memory allocation failed, return a null pointer.
493 if (newBuffer==0)
494 return 0;
497 * Copy the length of the string in the placeholder.
499 *newBuffer = len;
502 * Skip the byte count.
504 newBuffer++;
507 * Copy the information in the buffer.
508 * Since it is valid to pass a NULL pointer here, we'll initialize the
509 * buffer to nul if it is the case.
511 if (in != 0)
512 memcpy(newBuffer, in, len);
515 * Make sure that there is a nul character at the end of the
516 * string.
518 stringBuffer = (char *)newBuffer;
519 stringBuffer[len] = 0;
520 stringBuffer[len+1] = 0;
522 return (LPWSTR)stringBuffer;