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
42PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
43{
44    if (filenames.isEmpty())
45        return 0;
46
47    bool useIconFromFirstFile;
48#ifdef BUILDING_ON_TIGER
49    // FIXME: find a better image for multiple files to use on Tiger.
50    useIconFromFirstFile = true;
51#else
52    useIconFromFirstFile = filenames.size() == 1;
53#endif
54    if (useIconFromFirstFile) {
55        // Don't pass relative filenames -- we don't want a result that depends on the current directory.
56        // Need 0U here to disambiguate String::operator[] from operator(NSString*, int)[]
57        if (filenames[0].isEmpty() || filenames[0][0U] != '/')
58            return 0;
59
60        NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile:filenames[0]];
61        if (!image)
62            return 0;
63
64        return adoptRef(new Icon(image));
65    }
66#ifdef BUILDING_ON_TIGER
67    return 0;
68#else
69    NSImage* image = [NSImage imageNamed:NSImageNameMultipleDocuments];
70    if (!image)
71        return 0;
72
73    return adoptRef(new Icon(image));
74#endif
75}
76
77void Icon::paint(GraphicsContext* context, const IntRect& rect)
78{
79    if (context->paintingDisabled())
80        return;
81
82    LocalCurrentGraphicsContext localCurrentGC(context);
83
84    [m_nsImage.get() drawInRect:rect
85        fromRect:NSMakeRect(0, 0, [m_nsImage.get() size].width, [m_nsImage.get() size].height)
86        operation:NSCompositeSourceOver fraction:1.0f];
87}
88
89}
90