#include "config.h"
#include "BitmapImage.h"
#include "FileSystem.h"
#include "GdkCairoUtilities.h"
#include "GOwnPtrGtk.h"
#include "SharedBuffer.h"
#include <wtf/text/CString.h>
#include <cairo.h>
#include <gtk/gtk.h>
namespace WebCore {
static char* getPathToImageResource(char* resource)
{
if (g_getenv("WEBKIT_TOP_LEVEL"))
return g_build_filename(g_getenv("WEBKIT_TOP_LEVEL"), "Source", "WebCore", "Resources", resource, NULL);
return g_build_filename(sharedResourcesPath().data(), "images", resource, NULL);
}
static CString getThemeIconFileName(const char* name, int size)
{
GtkIconInfo* iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
name, size, GTK_ICON_LOOKUP_NO_SVG);
if (!iconInfo)
iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
GTK_STOCK_MISSING_IMAGE, size,
GTK_ICON_LOOKUP_NO_SVG);
if (iconInfo) {
GOwnPtr<GtkIconInfo> info(iconInfo);
return CString(gtk_icon_info_get_filename(info.get()));
}
return CString();
}
static PassRefPtr<SharedBuffer> loadResourceSharedBuffer(CString name)
{
GOwnPtr<gchar> content;
gsize length;
if (!g_file_get_contents(name.data(), &content.outPtr(), &length, 0))
return SharedBuffer::create();
return SharedBuffer::create(content.get(), length);
}
void BitmapImage::initPlatformData()
{
}
void BitmapImage::invalidatePlatformData()
{
}
PassRefPtr<Image> loadImageFromFile(CString fileName)
{
RefPtr<BitmapImage> img = BitmapImage::create();
if (!fileName.isNull()) {
RefPtr<SharedBuffer> buffer = loadResourceSharedBuffer(fileName);
img->setData(buffer.release(), true);
}
return img.release();
}
PassRefPtr<Image> Image::loadPlatformResource(const char* name)
{
CString fileName;
if (!strcmp("missingImage", name))
fileName = getThemeIconFileName(GTK_STOCK_MISSING_IMAGE, 16);
if (fileName.isNull()) {
GOwnPtr<gchar> imageName(g_strdup_printf("%s.png", name));
GOwnPtr<gchar> glibFileName(getPathToImageResource(imageName.get()));
fileName = glibFileName.get();
}
return loadImageFromFile(fileName);
}
PassRefPtr<Image> Image::loadPlatformThemeIcon(const char* name, int size)
{
return loadImageFromFile(getThemeIconFileName(name, size));
}
GdkPixbuf* BitmapImage::getGdkPixbuf()
{
NativeImageCairo* image = nativeImageForCurrentFrame();
return image ? cairoImageSurfaceToGdkPixbuf(image->surface()) : 0;
}
}