Home | History | Annotate | Download | only in gtk
      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 "GdkCairoUtilities.h"
     30 #include "GOwnPtrGtk.h"
     31 #include "SharedBuffer.h"
     32 #include <wtf/text/CString.h>
     33 #include <cairo.h>
     34 #include <gtk/gtk.h>
     35 
     36 #if PLATFORM(WIN)
     37 #include <mbstring.h>
     38 #include <shlobj.h>
     39 
     40 static HMODULE hmodule;
     41 
     42 extern "C" {
     43 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
     44 {
     45     if (fdwReason == DLL_PROCESS_ATTACH)
     46         hmodule = hinstDLL;
     47     return TRUE;
     48 }
     49 }
     50 
     51 static const char* getWebKitDataDirectory()
     52 {
     53     static char* dataDirectory = 0;
     54     if (dataDirectory)
     55         return dataDirectory;
     56 
     57     dataDirectory = new char[PATH_MAX];
     58     if (!GetModuleFileName(hmodule, static_cast<CHAR*>(dataDirectory), sizeof(dataDirectory) - 10))
     59         return DATA_DIR;
     60 
     61     // FIXME: This is pretty ugly. Ideally we should be using Windows API
     62     // functions or GLib methods to calculate paths.
     63     unsigned char *p;
     64     p = _mbsrchr(static_cast<const unsigned char *>(dataDirectory), '\\');
     65     *p = '\0';
     66     p = _mbsrchr(static_cast<const unsigned char *>(dataDirectory), '\\');
     67     if (p) {
     68         if (!stricmp((const char *) (p+1), "bin"))
     69             *p = '\0';
     70     }
     71     strcat(dataDirectory, "\\share");
     72 
     73     return dataDirectory;
     74 }
     75 
     76 #else
     77 
     78 static const char* getWebKitDataDirectory()
     79 {
     80     return DATA_DIR;
     81 }
     82 
     83 #endif
     84 
     85 namespace WebCore {
     86 
     87 static CString getThemeIconFileName(const char* name, int size)
     88 {
     89     GtkIconInfo* iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
     90                                                        name, size, GTK_ICON_LOOKUP_NO_SVG);
     91     // Try to fallback on MISSING_IMAGE.
     92     if (!iconInfo)
     93         iconInfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),
     94                                               GTK_STOCK_MISSING_IMAGE, size,
     95                                               GTK_ICON_LOOKUP_NO_SVG);
     96     if (iconInfo) {
     97         GOwnPtr<GtkIconInfo> info(iconInfo);
     98         return CString(gtk_icon_info_get_filename(info.get()));
     99     }
    100 
    101     // No icon was found, this can happen if not GTK theme is set. In
    102     // that case an empty Image will be created.
    103     return CString();
    104 }
    105 
    106 static PassRefPtr<SharedBuffer> loadResourceSharedBuffer(CString name)
    107 {
    108     GOwnPtr<gchar> content;
    109     gsize length;
    110     if (!g_file_get_contents(name.data(), &content.outPtr(), &length, 0))
    111         return SharedBuffer::create();
    112 
    113     return SharedBuffer::create(content.get(), length);
    114 }
    115 
    116 void BitmapImage::initPlatformData()
    117 {
    118 }
    119 
    120 void BitmapImage::invalidatePlatformData()
    121 {
    122 }
    123 
    124 PassRefPtr<Image> loadImageFromFile(CString fileName)
    125 {
    126     RefPtr<BitmapImage> img = BitmapImage::create();
    127     if (!fileName.isNull()) {
    128         RefPtr<SharedBuffer> buffer = loadResourceSharedBuffer(fileName);
    129         img->setData(buffer.release(), true);
    130     }
    131     return img.release();
    132 }
    133 
    134 PassRefPtr<Image> Image::loadPlatformResource(const char* name)
    135 {
    136     CString fileName;
    137     if (!strcmp("missingImage", name))
    138         fileName = getThemeIconFileName(GTK_STOCK_MISSING_IMAGE, 16);
    139     if (fileName.isNull()) {
    140         GOwnPtr<gchar> imageName(g_strdup_printf("%s.png", name));
    141         GOwnPtr<gchar> glibFileName(g_build_filename(getWebKitDataDirectory(), "webkitgtk-"WEBKITGTK_API_VERSION_STRING, "images", imageName.get(), NULL));
    142         fileName = glibFileName.get();
    143     }
    144 
    145     return loadImageFromFile(fileName);
    146 }
    147 
    148 PassRefPtr<Image> Image::loadPlatformThemeIcon(const char* name, int size)
    149 {
    150     return loadImageFromFile(getThemeIconFileName(name, size));
    151 }
    152 
    153 GdkPixbuf* BitmapImage::getGdkPixbuf()
    154 {
    155     cairo_surface_t* frame = frameAtIndex(currentFrame());
    156     if (!frame)
    157         return 0;
    158     return cairoImageSurfaceToGdkPixbuf(frame);
    159 }
    160 
    161 }
    162