1/*
2* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3* Copyright (C) 2007-2009 Torch Mobile, Inc.
4*
5* This library is free software; you can redistribute it and/or
6* modify it under the terms of the GNU Library General Public
7* License as published by the Free Software Foundation; either
8* version 2 of the License, or (at your option) any later version.
9*
10* This library is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13* Library General Public License for more details.
14*
15* You should have received a copy of the GNU Library General Public License
16* along with this library; see the file COPYING.LIB.  If not, write to
17* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18* Boston, MA 02110-1301, USA.
19*
20*/
21
22#include "config.h"
23#include "Icon.h"
24
25#include "GraphicsContext.h"
26#include "LocalWindowsContext.h"
27#include "PlatformString.h"
28#include <windows.h>
29
30#if OS(WINCE)
31// SHGFI_SHELLICONSIZE is not available on WINCE
32#define SHGFI_SHELLICONSIZE         0
33#endif
34
35namespace WebCore {
36
37static const int shell32MultipleFileIconIndex = 54;
38
39Icon::Icon(HICON icon)
40    : m_hIcon(icon)
41{
42    ASSERT(icon);
43}
44
45Icon::~Icon()
46{
47    DestroyIcon(m_hIcon);
48}
49
50// FIXME: Move the code to ChromeClient::iconForFiles().
51PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
52{
53    if (filenames.isEmpty())
54        return 0;
55
56    if (filenames.size() == 1) {
57        SHFILEINFO sfi;
58        memset(&sfi, 0, sizeof(sfi));
59
60        String tmpFilename = filenames[0];
61        if (!SHGetFileInfo(tmpFilename.charactersWithNullTermination(), 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON))
62            return 0;
63
64        return adoptRef(new Icon(sfi.hIcon));
65    }
66
67#if OS(WINCE)
68    return 0;
69#else
70    WCHAR buffer[MAX_PATH];
71    UINT length = ::GetSystemDirectoryW(buffer, WTF_ARRAY_LENGTH(buffer));
72    if (!length)
73        return 0;
74
75    if (wcscat_s(buffer, L"\\shell32.dll"))
76        return 0;
77
78    HICON hIcon;
79    if (!::ExtractIconExW(buffer, shell32MultipleFileIconIndex, 0, &hIcon, 1))
80        return 0;
81    return adoptRef(new Icon(hIcon));
82#endif
83}
84
85void Icon::paint(GraphicsContext* context, const IntRect& r)
86{
87    if (context->paintingDisabled())
88        return;
89
90#if OS(WINCE)
91    context->drawIcon(m_hIcon, r, DI_NORMAL);
92#else
93    LocalWindowsContext windowContext(context, r);
94    DrawIconEx(windowContext.hdc(), r.x(), r.y(), m_hIcon, r.width(), r.height(), 0, 0, DI_NORMAL);
95#endif
96}
97
98}
99