Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

utils.cpp

00001 /*****************************************************************************
00002  * utils.cpp: ActiveX control for VLC
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  *
00006  * Authors: Damien Fouilleul <[email protected]>
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00021  *****************************************************************************/
00022 
00023 #include "utils.h"
00024 
00025 /*
00026 ** conversion facilities
00027 */
00028 
00029 using namespace std;
00030 
00031 char *CStrFromBSTR(UINT codePage, BSTR bstr)
00032 {
00033     UINT len = SysStringLen(bstr);
00034     if( len > 0 )
00035     {
00036         size_t mblen = WideCharToMultiByte(codePage,
00037                 0, bstr, len, NULL, 0, NULL, NULL);
00038         if( mblen > 0 )
00039         {
00040             char *buffer = (char *)CoTaskMemAlloc(mblen+1);
00041             ZeroMemory(buffer, mblen+1);
00042             if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) )
00043             {
00044                 buffer[mblen] = '\0';
00045                 return buffer;
00046             }
00047         }
00048     }
00049     return NULL;
00050 };
00051 
00052 BSTR BSTRFromCStr(UINT codePage, LPCSTR s)
00053 {
00054     int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
00055     if( wideLen > 0 )
00056     {
00057         WCHAR* wideStr = (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
00058         if( NULL != wideStr )
00059         {
00060             BSTR bstr;
00061 
00062             ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
00063             MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
00064             bstr = SysAllocStringLen(wideStr, wideLen);
00065             free(wideStr);
00066 
00067             return bstr;
00068         }
00069     }
00070     return NULL;
00071 };
00072 
00073 /*
00074 **  properties
00075 */
00076 
00077 HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v)
00078 {
00079     IDispatch *pDisp;
00080     HRESULT hr = object->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp);
00081     if( SUCCEEDED(hr) )
00082     {
00083         DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
00084         VARIANT vres;
00085         VariantInit(&vres);
00086         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT,
00087                 DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vres, NULL, NULL);
00088         if( SUCCEEDED(hr) )
00089         {
00090             if( V_VT(&v) != V_VT(&vres) )
00091             {
00092                 hr = VariantChangeType(&v, &vres, 0, V_VT(&v));
00093                 VariantClear(&vres);
00094             }
00095             else
00096             {
00097                 v = vres;
00098             }
00099         }
00100         pDisp->Release();
00101     }
00102     return hr;
00103 };
00104 
00105 HDC CreateDevDC(DVTARGETDEVICE *ptd)
00106 {
00107         HDC hdc=NULL;
00108         LPDEVNAMES lpDevNames;
00109         LPDEVMODE lpDevMode;
00110         LPTSTR lpszDriverName;
00111         LPTSTR lpszDeviceName;
00112         LPTSTR lpszPortName;
00113 
00114         if (ptd == NULL) {
00115                 hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
00116                 goto errReturn;
00117         }
00118 
00119         lpDevNames = (LPDEVNAMES) ptd; // offset for size field
00120 
00121         if (ptd->tdExtDevmodeOffset == 0) {
00122                 lpDevMode = NULL;
00123         }else{
00124                 lpDevMode  = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
00125         }
00126 
00127         lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
00128         lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
00129         lpszPortName   = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
00130 
00131         hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
00132 
00133 errReturn:
00134         return hdc;
00135 };
00136 
00137 #define HIMETRIC_PER_INCH 2540
00138 
00139 void DPFromHimetric(HDC hdc, LPPOINT pt, int count)
00140 {
00141     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
00142     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
00143     while( count-- )
00144     {
00145         pt->x = pt->x*lpX/HIMETRIC_PER_INCH;
00146         pt->y = pt->y*lpY/HIMETRIC_PER_INCH;
00147         ++pt;
00148     }
00149 };
00150 
00151 void HimetricFromDP(HDC hdc, LPPOINT pt, int count)
00152 {
00153     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
00154     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
00155     while( count-- )
00156     {
00157         pt->x = pt->x*HIMETRIC_PER_INCH/lpX;
00158         pt->y = pt->y*HIMETRIC_PER_INCH/lpY;
00159         ++pt;
00160     }
00161 };
00162 

Generated on Tue Dec 20 10:14:18 2005 for vlc-0.8.4a by  doxygen 1.4.2