OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / platform / graphics / win / IconWin.cpp
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 <tchar.h>
29 #include <windows.h>
30
31 #if OS(WINCE)
32 // SHGFI_SHELLICONSIZE is not available on WINCE
33 #define SHGFI_SHELLICONSIZE         0
34 #endif
35
36 namespace WebCore {
37
38 static const int shell32MultipleFileIconIndex = 54;
39
40 Icon::Icon(HICON icon)
41     : m_hIcon(icon)
42 {
43     ASSERT(icon);
44 }
45
46 Icon::~Icon()
47 {
48     DestroyIcon(m_hIcon);
49 }
50
51 // FIXME: Move the code to ChromeClient::iconForFiles().
52 PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
53 {
54     if (filenames.isEmpty())
55         return 0;
56
57     if (filenames.size() == 1) {
58         SHFILEINFO sfi;
59         memset(&sfi, 0, sizeof(sfi));
60
61         String tmpFilename = filenames[0];
62         if (!SHGetFileInfo(tmpFilename.charactersWithNullTermination(), 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON))
63             return 0;
64
65         return adoptRef(new Icon(sfi.hIcon));
66     }
67
68 #if OS(WINCE)
69     return 0;
70 #else
71     TCHAR buffer[MAX_PATH];    
72     UINT length = ::GetSystemDirectory(buffer, WTF_ARRAY_LENGTH(buffer));
73     if (!length)
74         return 0;
75     
76     if (_tcscat_s(buffer, TEXT("\\shell32.dll")))
77         return 0;
78
79     HICON hIcon;
80     if (!::ExtractIconEx(buffer, shell32MultipleFileIconIndex, 0, &hIcon, 1))
81         return 0;
82     return adoptRef(new Icon(hIcon));
83 #endif
84 }
85
86 void Icon::paint(GraphicsContext* context, const IntRect& r)
87 {
88     if (context->paintingDisabled())
89         return;
90
91 #if OS(WINCE)
92     context->drawIcon(m_hIcon, r, DI_NORMAL);
93 #else
94     LocalWindowsContext windowContext(context, r);
95     DrawIconEx(windowContext.hdc(), r.x(), r.y(), m_hIcon, r.width(), r.height(), 0, 0, DI_NORMAL);
96 #endif
97 }
98
99 }