ole32_main.c 4.25 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 22 23
#include "config.h"
#include "wine/port.h"

24
#include <stdarg.h>
25
#include <stdio.h>
26

27
#include "windef.h"
28 29 30
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
31
#include "winnls.h"
32
#include "objbase.h"
33
#include "ole2.h"
34
#include "wine/debug.h"
35

36
WINE_DEFAULT_DEBUG_CHANNEL(ole);
37

38 39
#define HIMETRIC_INCHES 2540

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

60
	TRACE("%p %p %s %d\n", hIcon, lpszLabel, debugstr_w(lpszSourceFile), iIconIndex);
61 62

	if( !hIcon )
63
		return NULL;
64

65 66 67 68 69 70 71
	if (!SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
		return NULL;

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

72 73
	hdc = CreateMetaFileW(NULL);
	if( !hdc )
74 75
	{
		DeleteObject(font);
76
		return NULL;
77 78 79
	}

	SelectObject(hdc, font);
80 81

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

83 84 85 86 87 88
	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)
	{
89
		HFONT screen_old_font;
90 91 92
		/* metafile DCs don't support GetTextExtentPoint32, so size the font
		 * using the desktop window DC */
		hdcScreen = GetDC(NULL);
93
		screen_old_font = SelectObject(hdcScreen, font);
94
		GetTextExtentPoint32W(hdcScreen, lpszLabel, lstrlenW(lpszLabel), &text_size);
95
		SelectObject(hdcScreen, screen_old_font);
96
		ReleaseDC(NULL, hdcScreen);
97 98

		width = 3 * icon_width;
99
	}
100 101
	else
		width = icon_width;
102

103
	SetMapMode(hdc, MM_ANISOTROPIC);
104 105 106 107 108
	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);
109
	if(lpszLabel)
110 111
		/* draw the label centred too, if provided */
		TextOutW(hdc, (width-text_size.cx) / 2, label_offset, lpszLabel, lstrlenW(lpszLabel));
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	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);
	}
130

131
	mfp.mm = MM_ANISOTROPIC;
132 133 134 135
	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);
136
	mfp.hMF = CloseMetaFile(hdc);
137
	DeleteObject(font);
138 139
	if( !mfp.hMF )
		return NULL;
140

141
	hmem = GlobalAlloc( GMEM_MOVEABLE, sizeof(mfp) );
142
	if( !hmem )
143 144 145 146
	{
		DeleteMetaFile(mfp.hMF);
		return NULL;
	}
147

148 149 150 151
	mfdata = GlobalLock( hmem );
	if( !mfdata )
	{
		GlobalFree( hmem );
152 153
		DeleteMetaFile(mfp.hMF);
		return NULL;
154 155
	}

156
	memcpy(mfdata,&mfp,sizeof(mfp));
157 158 159 160 161 162
	GlobalUnlock( hmem );

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

	return hmem;
}