Stop a listbox bug that causes a div by zero when the item height
[wine/hacks.git] / ole / ole2disp.c
blob510dc5c66b858d1bc7176bd87c4142c291974bb0
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
7 #include <string.h>
8 #include "winerror.h"
9 #include "ole2.h"
10 #include "oleauto.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ldt.h"
14 #include "debug.h"
16 DEFAULT_DEBUG_CHANNEL(ole)
18 /* This implementation of the BSTR API is 16-bit only. It
19 represents BSTR as a 16:16 far pointer, and the strings
20 as ISO-8859 */
22 /******************************************************************************
23 * BSTR_AllocBytes [Internal]
25 static BSTR16 BSTR_AllocBytes(int n)
27 void *ptr = SEGPTR_ALLOC(n);
28 return (BSTR16)SEGPTR_GET(ptr);
31 /******************************************************************************
32 * BSTR_Free [INTERNAL]
34 static void BSTR_Free(BSTR16 in)
36 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
39 /******************************************************************************
40 * BSTR_GetAddr [INTERNAL]
42 static void* BSTR_GetAddr(BSTR16 in)
44 return in ? PTR_SEG_TO_LIN(in) : 0;
47 /******************************************************************************
48 * SysAllocString16 [OLE2DISP.2]
50 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
52 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
53 if(!out)return 0;
54 strcpy(BSTR_GetAddr(out),in);
55 return out;
58 /******************************************************************************
59 * SysAllocString32 [OLEAUT32.2]
61 BSTR WINAPI SysAllocString(LPCOLESTR in)
63 /* Delegate this to the SysAllocStringLen32 method. */
64 return SysAllocStringLen(in, lstrlenW(in));
67 /******************************************************************************
68 * SysReAllocString16 [OLE2DISP.3]
70 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
72 BSTR16 new=SysAllocString16(in);
73 BSTR_Free(*old);
74 *old=new;
75 return 1;
78 /******************************************************************************
79 * SysReAllocString32 [OLEAUT32.3]
81 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
84 * Sanity check
86 if (old==NULL)
87 return 0;
90 * Make sure we free the old string.
92 if (*old!=NULL)
93 SysFreeString(*old);
96 * Allocate the new string
98 *old = SysAllocString(in);
100 return 1;
103 /******************************************************************************
104 * SysAllocStringLen16 [OLE2DISP.4]
106 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
108 BSTR16 out=BSTR_AllocBytes(len+1);
109 if(!out)return 0;
110 strcpy(BSTR_GetAddr(out),in);
111 return out;
114 /******************************************************************************
115 * SysAllocStringLen32 [OLEAUT32.4]
117 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
118 * section, he describes the DWORD value placed before the BSTR data type.
119 * he describes it as a "DWORD count of characters". By experimenting with
120 * a windows application, this count seems to be a DWORD count of bytes in
121 * the string. Meaning that the count is double the number of wide
122 * characters in the string.
124 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
126 DWORD bufferSize;
127 DWORD* newBuffer;
128 WCHAR* stringBuffer;
131 * Find the lenth of the buffer passed-in in bytes.
133 bufferSize = len * sizeof (WCHAR);
136 * Allocate a new buffer to hold the string.
137 * dont't forget to keep an empty spot at the begining of the
138 * buffer for the character count and an extra character at the
139 * end for the NULL.
141 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
143 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
146 * If the memory allocation failed, return a null pointer.
148 if (newBuffer==0)
149 return 0;
152 * Copy the length of the string in the placeholder.
154 *newBuffer = bufferSize;
157 * Skip the byte count.
159 newBuffer++;
162 * Copy the information in the buffer.
163 * Since it is valid to pass a NULL pointer here, we'll initialize the
164 * buffer to nul if it is the case.
166 if (in != 0)
167 memcpy(newBuffer, in, bufferSize);
168 else
169 memset(newBuffer, 0, bufferSize);
172 * Make sure that there is a nul character at the end of the
173 * string.
175 stringBuffer = (WCHAR*)newBuffer;
176 stringBuffer[len] = L'\0';
178 return (LPWSTR)stringBuffer;
181 /******************************************************************************
182 * SysReAllocStringLen16 [OLE2DISP.5]
184 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
186 BSTR16 new=SysAllocStringLen16(in,len);
187 BSTR_Free(*old);
188 *old=new;
189 return 1;
193 /******************************************************************************
194 * SysReAllocStringLen32 [OLEAUT32.5]
196 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
199 * Sanity check
201 if (old==NULL)
202 return 0;
205 * Make sure we free the old string.
207 if (*old!=NULL)
208 SysFreeString(*old);
211 * Allocate the new string
213 *old = SysAllocStringLen(in, len);
215 return 1;
218 /******************************************************************************
219 * SysFreeString16 [OLE2DISP.6]
221 void WINAPI SysFreeString16(BSTR16 in)
223 BSTR_Free(in);
226 /******************************************************************************
227 * SysFreeString32 [OLEAUT32.6]
229 void WINAPI SysFreeString(BSTR in)
231 DWORD* bufferPointer;
234 * We have to be careful when we free a BSTR pointer, it points to
235 * the beginning of the string but it skips the byte count contained
236 * before the string.
238 bufferPointer = (DWORD*)in;
240 bufferPointer--;
243 * Free the memory from it's "real" origin.
245 HeapFree(GetProcessHeap(), 0, bufferPointer);
248 /******************************************************************************
249 * SysStringLen16 [OLE2DISP.7]
251 int WINAPI SysStringLen16(BSTR16 str)
253 return strlen(BSTR_GetAddr(str));
256 /******************************************************************************
257 * SysStringLen32 [OLEAUT32.7]
259 * The Windows documentation states that the length returned by this function
260 * is not necessarely the same as the length returned by the _lstrlenW method.
261 * It is the same number that was passed in as the "len" parameter if the
262 * string was allocated with a SysAllocStringLen method call.
264 int WINAPI SysStringLen(BSTR str)
266 DWORD* bufferPointer;
269 * The length of the string (in bytes) is contained in a DWORD placed
270 * just before the BSTR pointer
272 bufferPointer = (DWORD*)str;
274 bufferPointer--;
276 return (int)(*bufferPointer/sizeof(WCHAR));
279 /******************************************************************************
280 * SysStringByteLen [OLEAUT32.149]
282 * The Windows documentation states that the length returned by this function
283 * is not necessarely the same as the length returned by the _lstrlenW method.
284 * It is the same number that was passed in as the "len" parameter if the
285 * string was allocated with a SysAllocStringLen method call.
287 int WINAPI SysStringByteLen(BSTR str)
289 return SysStringLen(str)*sizeof(WCHAR);
292 /******************************************************************************
293 * CreateDispTypeInfo [OLE2DISP.31]
295 HRESULT WINAPI CreateDispTypeInfo16(
296 INTERFACEDATA *pidata,
297 LCID lcid,
298 ITypeInfo **pptinfo
300 FIXME(ole,"(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
301 return 0;
304 /******************************************************************************
305 * RegisterActiveObject [OLE2DISP.35]
307 HRESULT WINAPI RegisterActiveObject16(
308 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
310 char buf[80];
311 WINE_StringFromCLSID(rclsid,buf);
312 FIXME(ole,"(%p,%s,0x%08lx,%p):stub\n",punk,buf,dwFlags,pdwRegister);
313 return 0;
316 /******************************************************************************
317 * OleTranslateColor [OLEAUT32.421]
319 * Converts an OLE_COLOR to a COLORREF.
320 * See the documentation for conversion rules.
321 * pColorRef can be NULL. In that case the user only wants to test the
322 * conversion.
324 INT WINAPI OleTranslateColor(
325 LONG clr,
326 HPALETTE hpal,
327 COLORREF* pColorRef)
329 COLORREF colorref;
330 BYTE b = HIBYTE(HIWORD(clr));
332 TRACE(ole,"(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
335 * In case pColorRef is NULL, provide our own to simplify the code.
337 if (pColorRef == NULL)
338 pColorRef = &colorref;
340 switch (b)
342 case 0x00:
344 if (hpal != 0)
345 *pColorRef = PALETTERGB(GetRValue(clr),
346 GetGValue(clr),
347 GetBValue(clr));
348 else
349 *pColorRef = clr;
351 break;
354 case 0x01:
356 if (hpal != 0)
358 PALETTEENTRY pe;
360 * Validate the palette index.
362 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
363 return E_INVALIDARG;
366 *pColorRef = clr;
368 break;
371 case 0x02:
372 *pColorRef = clr;
373 break;
375 case 0x80:
377 int index = LOBYTE(LOWORD(clr));
380 * Validate GetSysColor index.
382 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
383 return E_INVALIDARG;
385 *pColorRef = GetSysColor(index);
387 break;
390 default:
391 return E_INVALIDARG;
394 return S_OK;
397 /******************************************************************************
398 * SysAllocStringByteLen [OLEAUT32.150]
401 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
403 DWORD* newBuffer;
404 char* stringBuffer;
407 * Allocate a new buffer to hold the string.
408 * dont't forget to keep an empty spot at the begining of the
409 * buffer for the character count and an extra character at the
410 * end for the NULL.
412 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
414 len + sizeof(WCHAR) + sizeof(DWORD));
417 * If the memory allocation failed, return a null pointer.
419 if (newBuffer==0)
420 return 0;
423 * Copy the length of the string in the placeholder.
425 *newBuffer = len;
428 * Skip the byte count.
430 newBuffer++;
433 * Copy the information in the buffer.
434 * Since it is valid to pass a NULL pointer here, we'll initialize the
435 * buffer to nul if it is the case.
437 if (in != 0)
438 memcpy(newBuffer, in, len);
441 * Make sure that there is a nul character at the end of the
442 * string.
444 stringBuffer = (char *)newBuffer;
445 stringBuffer[len] = 0;
446 stringBuffer[len+1] = 0;
448 return (LPWSTR)stringBuffer;