1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#if !PLATFORM(IOS)
22
23#import "config.h"
24#import "Icon.h"
25
26#import "GraphicsContext.h"
27#import "IntRect.h"
28#import "LocalCurrentGraphicsContext.h"
29#import <wtf/PassRefPtr.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34Icon::Icon(NSImage *image)
35    : m_nsImage(image)
36{
37    // Need this because WebCore uses AppKit's flipped coordinate system exclusively.
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wdeprecated-declarations"
40    [image setFlipped:YES];
41#pragma clang diagnostic pop
42}
43
44Icon::~Icon()
45{
46}
47
48// FIXME: Move the code to ChromeClient::iconForFiles().
49PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
50{
51    if (filenames.isEmpty())
52        return 0;
53
54    bool useIconFromFirstFile;
55    useIconFromFirstFile = filenames.size() == 1;
56    if (useIconFromFirstFile) {
57        // Don't pass relative filenames -- we don't want a result that depends on the current directory.
58        // Need 0U here to disambiguate String::operator[] from operator(NSString*, int)[]
59        if (filenames[0].isEmpty() || filenames[0][0U] != '/')
60            return 0;
61
62        NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile:filenames[0]];
63        if (!image)
64            return 0;
65
66        return adoptRef(new Icon(image));
67    }
68    NSImage* image = [NSImage imageNamed:NSImageNameMultipleDocuments];
69    if (!image)
70        return 0;
71
72    return adoptRef(new Icon(image));
73}
74
75void Icon::paint(GraphicsContext* context, const IntRect& rect)
76{
77    if (context->paintingDisabled())
78        return;
79
80    LocalCurrentGraphicsContext localCurrentGC(context);
81
82    [m_nsImage.get() drawInRect:rect
83        fromRect:NSMakeRect(0, 0, [m_nsImage.get() size].width, [m_nsImage.get() size].height)
84        operation:NSCompositeSourceOver fraction:1.0f];
85}
86
87}
88
89#endif // !PLATFORM(IOS)
90