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#import "config.h"
22#import "Icon.h"
23
24#import "GraphicsContext.h"
25#import "LocalCurrentGraphicsContext.h"
26#import "PlatformString.h"
27#import <wtf/PassRefPtr.h>
28
29namespace WebCore {
30
31Icon::Icon(NSImage *image)
32    : m_nsImage(image)
33{
34    // Need this because WebCore uses AppKit's flipped coordinate system exclusively.
35    [image setFlipped:YES];
36}
37
38Icon::~Icon()
39{
40}
41
42// FIXME: Move the code to ChromeClient::iconForFiles().
43PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
44{
45    if (filenames.isEmpty())
46        return 0;
47
48    bool useIconFromFirstFile;
49    useIconFromFirstFile = filenames.size() == 1;
50    if (useIconFromFirstFile) {
51        // Don't pass relative filenames -- we don't want a result that depends on the current directory.
52        // Need 0U here to disambiguate String::operator[] from operator(NSString*, int)[]
53        if (filenames[0].isEmpty() || filenames[0][0U] != '/')
54            return 0;
55
56        NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile:filenames[0]];
57        if (!image)
58            return 0;
59
60        return adoptRef(new Icon(image));
61    }
62    NSImage* image = [NSImage imageNamed:NSImageNameMultipleDocuments];
63    if (!image)
64        return 0;
65
66    return adoptRef(new Icon(image));
67}
68
69void Icon::paint(GraphicsContext* context, const IntRect& rect)
70{
71    if (context->paintingDisabled())
72        return;
73
74    LocalCurrentGraphicsContext localCurrentGC(context);
75
76    [m_nsImage.get() drawInRect:rect
77        fromRect:NSMakeRect(0, 0, [m_nsImage.get() size].width, [m_nsImage.get() size].height)
78        operation:NSCompositeSourceOver fraction:1.0f];
79}
80
81}
82