playlist_Clear() should delete only the local playlist, not the root playlist (includ...
[vlc.git] / activex / utils.cpp
blobb96ea8fc4b49f06d91d1f97bcdaa2f3fc16e50ca
1 /*****************************************************************************
2 * utils.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #include "utils.h"
25 #include <wchar.h>
26 #include <wctype.h>
29 ** conversion facilities
32 using namespace std;
34 char *CStrFromWSTR(UINT codePage, LPCWSTR wstr, UINT len)
36 if( len > 0 )
38 size_t mblen = WideCharToMultiByte(codePage,
39 0, wstr, len, NULL, 0, NULL, NULL);
40 if( mblen > 0 )
42 char *buffer = (char *)CoTaskMemAlloc(mblen+1);
43 ZeroMemory(buffer, mblen+1);
44 if( WideCharToMultiByte(codePage, 0, wstr, len, buffer, mblen, NULL, NULL) )
46 buffer[mblen] = '\0';
47 return buffer;
51 return NULL;
54 char *CStrFromBSTR(UINT codePage, BSTR bstr)
56 return CStrFromWSTR(codePage, bstr, SysStringLen(bstr));
59 BSTR BSTRFromCStr(UINT codePage, LPCSTR s)
61 int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
62 if( wideLen > 0 )
64 WCHAR* wideStr = (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
65 if( NULL != wideStr )
67 BSTR bstr;
69 ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
70 MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
71 bstr = SysAllocStringLen(wideStr, wideLen-1);
72 CoTaskMemFree(wideStr);
74 return bstr;
77 return NULL;
81 ** properties
84 HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v)
86 IDispatch *pDisp;
87 HRESULT hr = object->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp);
88 if( SUCCEEDED(hr) )
90 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
91 VARIANT vres;
92 VariantInit(&vres);
93 hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT,
94 DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vres, NULL, NULL);
95 if( SUCCEEDED(hr) )
97 if( V_VT(&v) != V_VT(&vres) )
99 hr = VariantChangeType(&v, &vres, 0, V_VT(&v));
100 VariantClear(&vres);
102 else
104 v = vres;
107 pDisp->Release();
109 return hr;
112 HDC CreateDevDC(DVTARGETDEVICE *ptd)
114 HDC hdc=NULL;
115 if( NULL == ptd )
117 hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
119 else
121 LPDEVNAMES lpDevNames;
122 LPDEVMODE lpDevMode;
123 LPTSTR lpszDriverName;
124 LPTSTR lpszDeviceName;
125 LPTSTR lpszPortName;
127 lpDevNames = (LPDEVNAMES) ptd; // offset for size field
129 if (ptd->tdExtDevmodeOffset == 0)
131 lpDevMode = NULL;
133 else
135 lpDevMode = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
138 lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
139 lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
140 lpszPortName = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
142 hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
144 return hdc;
147 #define HIMETRIC_PER_INCH 2540
149 void DPFromHimetric(HDC hdc, LPPOINT pt, int count)
151 LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
152 LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
153 while( count-- )
155 pt->x = pt->x*lpX/HIMETRIC_PER_INCH;
156 pt->y = pt->y*lpY/HIMETRIC_PER_INCH;
157 ++pt;
161 void HimetricFromDP(HDC hdc, LPPOINT pt, int count)
163 LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
164 LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
165 while( count-- )
167 pt->x = pt->x*HIMETRIC_PER_INCH/lpX;
168 pt->y = pt->y*HIMETRIC_PER_INCH/lpY;
169 ++pt;
174 LPWSTR CombineURL(LPCWSTR baseUrl, LPCWSTR url)
176 if( NULL != url )
178 // check whether URL is already absolute
179 const wchar_t *end=wcschr(url, L':');
180 if( (NULL != end) && (end != url) )
182 // validate protocol header
183 const wchar_t *start = url;
184 wchar_t c = *start;
185 if( iswalpha(c) )
187 ++start;
188 while( start != end )
190 c = *start;
191 if( ! (iswalnum(c)
192 || (L'-' == c)
193 || (L'+' == c)
194 || (L'.' == c)
195 || (L'/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
196 // not valid protocol header, assume relative URL
197 goto relativeurl;
198 ++start;
200 /* we have a protocol header, therefore URL is absolute */
201 UINT len = wcslen(url);
202 wchar_t *href = (LPWSTR)CoTaskMemAlloc((len+1)*sizeof(wchar_t));
203 if( href )
205 memcpy(href, url, len*sizeof(wchar_t));
206 href[len] = L'\0';
208 return href;
212 relativeurl:
214 if( baseUrl )
216 size_t baseLen = wcslen(baseUrl);
217 wchar_t *href = (LPWSTR)CoTaskMemAlloc((baseLen+wcslen(url)+1)*sizeof(wchar_t));
218 if( href )
220 /* prepend base URL */
221 wcscpy(href, baseUrl);
224 ** relative url could be empty,
225 ** in which case return base URL
227 if( L'\0' == *url )
228 return href;
231 ** locate pathname part of base URL
234 /* skip over protocol part */
235 wchar_t *pathstart = wcschr(href, L':');
236 wchar_t *pathend;
237 if( pathstart )
239 if( L'/' == *(++pathstart) )
241 if( L'/' == *(++pathstart) )
243 ++pathstart;
246 /* skip over host part */
247 pathstart = wcschr(pathstart, L'/');
248 pathend = href+baseLen;
249 if( ! pathstart )
251 // no path, add a / past end of url (over '\0')
252 pathstart = pathend;
253 *pathstart = L'/';
256 else
258 /* baseURL is just a UNIX file path */
259 if( L'/' != *href )
261 /* baseURL is not an absolute path */
262 return NULL;
264 pathstart = href;
265 pathend = href+baseLen;
268 /* relative URL made of an absolute path ? */
269 if( L'/' == *url )
271 /* replace path completely */
272 wcscpy(pathstart, url);
273 return href;
276 /* find last path component and replace it */
277 while( L'/' != *pathend )
278 --pathend;
281 ** if relative url path starts with one or more './' or '../',
282 ** factor them out of href so that we return a
283 ** normalized URL
285 while( pathend > pathstart )
287 const wchar_t *p = url;
288 if( L'.' != *p )
289 break;
290 ++p;
291 if( L'\0' == *p )
293 /* relative url is just '.' */
294 url = p;
295 break;
297 if( L'/' == *p )
299 /* relative url starts with './' */
300 url = ++p;
301 continue;
303 if( L'.' != *p )
304 break;
305 ++p;
306 if( L'\0' == *p )
308 /* relative url is '..' */
310 else
312 if( L'/' != *p )
313 break;
314 /* relative url starts with '../' */
315 ++p;
317 url = p;
320 --pathend;
322 while( L'/' != *pathend );
324 /* skip over '/' separator */
325 ++pathend;
326 /* concatenate remaining base URL and relative URL */
327 wcscpy(pathend, url);
329 return href;
332 return NULL;