ole32_main.c 4.21 KB
Newer Older
1 2 3
/*
 *  OLE32 Initialization
 *
4
 * Copyright 2000 Huw D M Davies for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20

21
#include <stdarg.h>
22
#include <stdio.h>
23

24
#include "windef.h"
25
#include "winerror.h"
26 27 28
#include "winbase.h"
#include "wingdi.h"
#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
#define HIMETRIC_INCHES 2540

37
/***********************************************************************
38
 *		OleMetafilePictFromIconAndLabel (OLE32.@)
39
 */
40 41 42
HGLOBAL WINAPI OleMetafilePictFromIconAndLabel(HICON hIcon, LPOLESTR lpszLabel,
                                               LPOLESTR lpszSourceFile, UINT iIconIndex)
{
43
	METAFILEPICT mfp;
44
	HDC hdc;
45
	HGLOBAL hmem = NULL;
46
	LPVOID mfdata;
47
	static const char szIconOnly[] = "IconOnly";
48 49 50 51 52 53
	SIZE text_size = { 0, 0 };
	INT width;
	INT icon_width;
	INT icon_height;
	INT label_offset;
	HDC hdcScreen;
54 55
	LOGFONTW lf;
	HFONT font;
56

57
	TRACE("%p %p %s %d\n", hIcon, lpszLabel, debugstr_w(lpszSourceFile), iIconIndex);
58 59

	if( !hIcon )
60
		return NULL;
61

62 63 64 65 66 67 68
	if (!SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
		return NULL;

	font = CreateFontIndirectW(&lf);
	if (!font)
		return NULL;

69 70
	hdc = CreateMetaFileW(NULL);
	if( !hdc )
71 72
	{
		DeleteObject(font);
73
		return NULL;
74 75 76
	}

	SelectObject(hdc, font);
77 78

	ExtEscape(hdc, MFCOMMENT, sizeof(szIconOnly), szIconOnly, 0, NULL);
79

80 81 82 83 84 85
	icon_width = GetSystemMetrics(SM_CXICON);
	icon_height = GetSystemMetrics(SM_CYICON);
	/* FIXME: should we give the label a bit of padding here? */
	label_offset = icon_height;
	if (lpszLabel)
	{
86
		HFONT screen_old_font;
87 88 89
		/* metafile DCs don't support GetTextExtentPoint32, so size the font
		 * using the desktop window DC */
		hdcScreen = GetDC(NULL);
90
		screen_old_font = SelectObject(hdcScreen, font);
91
		GetTextExtentPoint32W(hdcScreen, lpszLabel, lstrlenW(lpszLabel), &text_size);
92
		SelectObject(hdcScreen, screen_old_font);
93
		ReleaseDC(NULL, hdcScreen);
94 95

		width = 3 * icon_width;
96
	}
97 98
	else
		width = icon_width;
99

100
	SetMapMode(hdc, MM_ANISOTROPIC);
101 102 103 104 105
	SetWindowOrgEx(hdc, 0, 0, NULL);
	SetWindowExtEx(hdc, width, label_offset + text_size.cy, NULL);

	/* draw the icon centred */
	DrawIcon(hdc, (width-icon_width) / 2, 0, hIcon);
106
	if(lpszLabel)
107 108
		/* draw the label centred too, if provided */
		TextOutW(hdc, (width-text_size.cx) / 2, label_offset, lpszLabel, lstrlenW(lpszLabel));
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	if (lpszSourceFile)
	{
		char szIconIndex[10];
		int path_length = WideCharToMultiByte(CP_ACP,0,lpszSourceFile,-1,NULL,0,NULL,NULL);
		if (path_length > 1)
		{
			char * szPath = CoTaskMemAlloc(path_length * sizeof(CHAR));
			if (szPath)
			{
				WideCharToMultiByte(CP_ACP,0,lpszSourceFile,-1,szPath,path_length,NULL,NULL);
				ExtEscape(hdc, MFCOMMENT, path_length, szPath, 0, NULL);
				CoTaskMemFree(szPath);
			}
		}
		snprintf(szIconIndex, 10, "%u", iIconIndex);
		ExtEscape(hdc, MFCOMMENT, strlen(szIconIndex)+1, szIconIndex, 0, NULL);
	}
127

128
	mfp.mm = MM_ANISOTROPIC;
129 130 131 132
	hdcScreen = GetDC(NULL);
	mfp.xExt = MulDiv(width, HIMETRIC_INCHES, GetDeviceCaps(hdcScreen, LOGPIXELSX));
	mfp.yExt = MulDiv(label_offset + text_size.cy, HIMETRIC_INCHES, GetDeviceCaps(hdcScreen, LOGPIXELSY));
	ReleaseDC(NULL, hdcScreen);
133
	mfp.hMF = CloseMetaFile(hdc);
134
	DeleteObject(font);
135 136
	if( !mfp.hMF )
		return NULL;
137

138
	hmem = GlobalAlloc( GMEM_MOVEABLE, sizeof(mfp) );
139
	if( !hmem )
140 141 142 143
	{
		DeleteMetaFile(mfp.hMF);
		return NULL;
	}
144

145 146 147 148
	mfdata = GlobalLock( hmem );
	if( !mfdata )
	{
		GlobalFree( hmem );
149 150
		DeleteMetaFile(mfp.hMF);
		return NULL;
151 152
	}

153
	memcpy(mfdata,&mfp,sizeof(mfp));
154 155 156 157 158 159
	GlobalUnlock( hmem );

	TRACE("returning %p\n",hmem);

	return hmem;
}