Sync to Wine-20050524:
[reactos.git] / reactos / lib / ole32 / ole32_main.c
1 /*
2 * OLE32 Initialization
3 *
4 * Copyright 2000 Huw D M Davies for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winerror.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "objbase.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(ole);
34
35 /***********************************************************************
36 * OleMetafilePictFromIconAndLabel (OLE32.@)
37 */
38 HGLOBAL WINAPI OleMetafilePictFromIconAndLabel(HICON hIcon, LPOLESTR lpszLabel,
39 LPOLESTR lpszSourceFile, UINT iIconIndex)
40 {
41 METAFILEPICT mfp;
42 HDC hdc;
43 UINT dy;
44 HGLOBAL hmem = NULL;
45 LPVOID mfdata;
46 static const char szIconOnly[] = "IconOnly";
47
48 TRACE("%p %p %s %d\n", hIcon, lpszLabel, debugstr_w(lpszSourceFile), iIconIndex);
49
50 if( !hIcon )
51 return NULL;
52
53 hdc = CreateMetaFileW(NULL);
54 if( !hdc )
55 return NULL;
56
57 ExtEscape(hdc, MFCOMMENT, sizeof(szIconOnly), szIconOnly, 0, NULL);
58
59 /* FIXME: things are drawn in the wrong place */
60 DrawIcon(hdc, 0, 0, hIcon);
61 dy = GetSystemMetrics(SM_CXICON);
62 if(lpszLabel)
63 TextOutW(hdc, 0, dy, lpszLabel, lstrlenW(lpszLabel));
64
65 if (lpszSourceFile)
66 {
67 char szIconIndex[10];
68 int path_length = WideCharToMultiByte(CP_ACP,0,lpszSourceFile,-1,NULL,0,NULL,NULL);
69 if (path_length > 1)
70 {
71 char * szPath = CoTaskMemAlloc(path_length * sizeof(CHAR));
72 if (szPath)
73 {
74 WideCharToMultiByte(CP_ACP,0,lpszSourceFile,-1,szPath,path_length,NULL,NULL);
75 ExtEscape(hdc, MFCOMMENT, path_length, szPath, 0, NULL);
76 CoTaskMemFree(szPath);
77 }
78 }
79 snprintf(szIconIndex, 10, "%u", iIconIndex);
80 ExtEscape(hdc, MFCOMMENT, strlen(szIconIndex)+1, szIconIndex, 0, NULL);
81 }
82
83 mfp.mm = MM_ISOTROPIC;
84 mfp.xExt = mfp.yExt = 0; /* FIXME ? */
85 mfp.hMF = CloseMetaFile(hdc);
86 if( !mfp.hMF )
87 return NULL;
88
89 hmem = GlobalAlloc( GMEM_MOVEABLE, sizeof(mfp) );
90 if( !hmem )
91 {
92 DeleteMetaFile(mfp.hMF);
93 return NULL;
94 }
95
96 mfdata = GlobalLock( hmem );
97 if( !mfdata )
98 {
99 GlobalFree( hmem );
100 DeleteMetaFile(mfp.hMF);
101 return NULL;
102 }
103
104 memcpy(mfdata,&mfp,sizeof(mfp));
105 GlobalUnlock( hmem );
106
107 TRACE("returning %p\n",hmem);
108
109 return hmem;
110 }