[GLIB] Use GUniquePtr instead of GOwnPtr
[WebKit-https.git] / Source / WebCore / platform / graphics / gtk / ImageGtk.cpp
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "BitmapImage.h"
29 #include "FileSystem.h"
30 #include "GUniquePtrGtk.h"
31 #include "GdkCairoUtilities.h"
32 #include "SharedBuffer.h"
33 #include <wtf/gobject/GOwnPtr.h>
34 #include <wtf/gobject/GUniquePtr.h>
35 #include <wtf/text/CString.h>
36 #include <cairo.h>
37 #include <gtk/gtk.h>
38
39 namespace WebCore {
40
41 static char* getPathToImageResource(char* resource)
42 {
43     if (g_getenv("WEBKIT_TOP_LEVEL"))
44         return g_build_filename(g_getenv("WEBKIT_TOP_LEVEL"), "Source", "WebCore", "Resources", resource, NULL);
45
46     return g_build_filename(sharedResourcesPath().data(), "images", resource, NULL);
47 }
48
49 static CString getThemeIconFileName(const char* name, int size)
50 {
51     GtkIconInfo* iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
52                                                        name, size, GTK_ICON_LOOKUP_NO_SVG);
53     // Try to fallback on MISSING_IMAGE.
54     if (!iconInfo)
55         iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
56                                               GTK_STOCK_MISSING_IMAGE, size,
57                                               GTK_ICON_LOOKUP_NO_SVG);
58     if (iconInfo) {
59         GUniquePtr<GtkIconInfo> info(iconInfo);
60         return CString(gtk_icon_info_get_filename(info.get()));
61     }
62
63     // No icon was found, this can happen if not GTK theme is set. In
64     // that case an empty Image will be created.
65     return CString();
66 }
67
68 static PassRefPtr<SharedBuffer> loadResourceSharedBuffer(CString name)
69 {
70     GOwnPtr<gchar> content;
71     gsize length;
72     if (!g_file_get_contents(name.data(), &content.outPtr(), &length, 0))
73         return SharedBuffer::create();
74
75     return SharedBuffer::create(content.get(), length);
76 }
77
78 void BitmapImage::invalidatePlatformData()
79 {
80 }
81
82 PassRefPtr<Image> loadImageFromFile(CString fileName)
83 {
84     RefPtr<BitmapImage> img = BitmapImage::create();
85     if (!fileName.isNull()) {
86         RefPtr<SharedBuffer> buffer = loadResourceSharedBuffer(fileName);
87         img->setData(buffer.release(), true);
88     }
89     return img.release();
90 }
91
92 PassRefPtr<Image> Image::loadPlatformResource(const char* name)
93 {
94     CString fileName;
95     if (!strcmp("missingImage", name))
96         fileName = getThemeIconFileName(GTK_STOCK_MISSING_IMAGE, 16);
97     if (fileName.isNull()) {
98         GUniquePtr<gchar> imageName(g_strdup_printf("%s.png", name));
99         GUniquePtr<gchar> glibFileName(getPathToImageResource(imageName.get()));
100         fileName = glibFileName.get();
101     }
102
103     return loadImageFromFile(fileName);
104 }
105
106 PassRefPtr<Image> Image::loadPlatformThemeIcon(const char* name, int size)
107 {
108     return loadImageFromFile(getThemeIconFileName(name, size));
109 }
110
111 GdkPixbuf* BitmapImage::getGdkPixbuf()
112 {
113     RefPtr<cairo_surface_t> surface = nativeImageForCurrentFrame();
114     return surface ? cairoSurfaceToGdkPixbuf(surface.get()) : 0;
115 }
116
117 }