• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PlatformString.h"
27 #include <tchar.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 
35 namespace WebCore {
36 
37 static const int shell32MultipleFileIconIndex = 54;
38 
Icon(HICON icon)39 Icon::Icon(HICON icon)
40     : m_hIcon(icon)
41 {
42     ASSERT(icon);
43 }
44 
~Icon()45 Icon::~Icon()
46 {
47     DestroyIcon(m_hIcon);
48 }
49 
createIconForFiles(const Vector<String> & filenames)50 PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
51 {
52     if (filenames.isEmpty())
53         return 0;
54 
55     if (filenames.size() == 1) {
56         SHFILEINFO sfi;
57         memset(&sfi, 0, sizeof(sfi));
58 
59         String tmpFilename = filenames[0];
60         if (!SHGetFileInfo(tmpFilename.charactersWithNullTermination(), 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON))
61             return 0;
62 
63         return adoptRef(new Icon(sfi.hIcon));
64     }
65 
66 #if OS(WINCE)
67     return 0;
68 #else
69     TCHAR buffer[MAX_PATH];
70     UINT length = ::GetSystemDirectory(buffer, ARRAYSIZE(buffer));
71     if (!length)
72         return 0;
73 
74     if (_tcscat_s(buffer, TEXT("\\shell32.dll")))
75         return 0;
76 
77     HICON hIcon;
78     if (!::ExtractIconEx(buffer, shell32MultipleFileIconIndex, 0, &hIcon, 1))
79         return 0;
80     return adoptRef(new Icon(hIcon));
81 #endif
82 }
83 
paint(GraphicsContext * context,const IntRect & r)84 void Icon::paint(GraphicsContext* context, const IntRect& r)
85 {
86     if (context->paintingDisabled())
87         return;
88 
89 #if OS(WINCE)
90     context->drawIcon(m_hIcon, r, DI_NORMAL);
91 #else
92     HDC hdc = context->getWindowsContext(r);
93 
94     DrawIconEx(hdc, r.x(), r.y(), m_hIcon, r.width(), r.height(), 0, 0, DI_NORMAL);
95 
96     context->releaseWindowsContext(hdc, r);
97 #endif
98 }
99 
100 }
101