]> git.sesse.net Git - vlc/blob - activex/utils.cpp
Try to find seamonkey-config too.
[vlc] / activex / utils.cpp
1 /*****************************************************************************
2  * utils.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #include "utils.h"
24
25 /*
26 ** conversion facilities
27 */
28
29 using namespace std;
30
31 char *CStrFromBSTR(UINT codePage, BSTR bstr)
32 {
33     UINT len = SysStringLen(bstr);
34     if( len > 0 )
35     {
36         size_t mblen = WideCharToMultiByte(codePage,
37                 0, bstr, len, NULL, 0, NULL, NULL);
38         if( mblen > 0 )
39         {
40             char *buffer = (char *)CoTaskMemAlloc(mblen+1);
41             ZeroMemory(buffer, mblen+1);
42             if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) )
43             {
44                 buffer[mblen] = '\0';
45                 return buffer;
46             }
47         }
48     }
49     return NULL;
50 };
51
52 BSTR BSTRFromCStr(UINT codePage, LPCSTR s)
53 {
54     int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
55     if( wideLen > 0 )
56     {
57         WCHAR* wideStr = (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
58         if( NULL != wideStr )
59         {
60             BSTR bstr;
61
62             ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
63             MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
64             bstr = SysAllocStringLen(wideStr, wideLen);
65             CoTaskMemFree(wideStr);
66
67             return bstr;
68         }
69     }
70     return NULL;
71 };
72
73 /*
74 **  properties
75 */
76
77 HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v)
78 {
79     IDispatch *pDisp;
80     HRESULT hr = object->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp);
81     if( SUCCEEDED(hr) )
82     {
83         DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
84         VARIANT vres;
85         VariantInit(&vres);
86         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT,
87                 DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vres, NULL, NULL);
88         if( SUCCEEDED(hr) )
89         {
90             if( V_VT(&v) != V_VT(&vres) )
91             {
92                 hr = VariantChangeType(&v, &vres, 0, V_VT(&v));
93                 VariantClear(&vres);
94             }
95             else
96             {
97                 v = vres;
98             }
99         }
100         pDisp->Release();
101     }
102     return hr;
103 };
104
105 HDC CreateDevDC(DVTARGETDEVICE *ptd)
106 {
107         HDC hdc=NULL;
108         if( NULL == ptd )
109     {
110                 hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
111         }
112     else
113     {
114         LPDEVNAMES lpDevNames;
115         LPDEVMODE lpDevMode;
116         LPTSTR lpszDriverName;
117         LPTSTR lpszDeviceName;
118         LPTSTR lpszPortName;
119
120         lpDevNames = (LPDEVNAMES) ptd; // offset for size field
121
122         if (ptd->tdExtDevmodeOffset == 0)
123         {
124             lpDevMode = NULL;
125         }
126         else
127         {
128             lpDevMode  = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
129         }
130
131         lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
132         lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
133         lpszPortName   = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
134
135         hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
136     }
137         return hdc;
138 };
139
140 #define HIMETRIC_PER_INCH 2540
141
142 void DPFromHimetric(HDC hdc, LPPOINT pt, int count)
143 {
144     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
145     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
146     while( count-- )
147     {
148         pt->x = pt->x*lpX/HIMETRIC_PER_INCH;
149         pt->y = pt->y*lpY/HIMETRIC_PER_INCH;
150         ++pt;
151     }
152 };
153
154 void HimetricFromDP(HDC hdc, LPPOINT pt, int count)
155 {
156     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
157     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
158     while( count-- )
159     {
160         pt->x = pt->x*HIMETRIC_PER_INCH/lpX;
161         pt->y = pt->y*HIMETRIC_PER_INCH/lpY;
162         ++pt;
163     }
164 };
165