Home | History | Annotate | Download | only in gtk
      1 /*
      2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2007 Holger Hans Peter Freyther
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1.  Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  * 2.  Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     15  *     its contributors may be used to endorse or promote products derived
     16  *     from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "config.h"
     31 #include "Icon.h"
     32 
     33 #include "GraphicsContext.h"
     34 #include "MIMETypeRegistry.h"
     35 #include "PassRefPtr.h"
     36 #include "PlatformContextCairo.h"
     37 #include <gtk/gtk.h>
     38 #include <wtf/text/CString.h>
     39 
     40 namespace WebCore {
     41 
     42 Icon::Icon()
     43     : m_icon(0)
     44 {
     45 }
     46 
     47 Icon::~Icon()
     48 {
     49     if (m_icon)
     50         g_object_unref(m_icon);
     51 }
     52 
     53 static String lookupIconName(String MIMEType)
     54 {
     55     /*
     56      Lookup an appropriate icon according to either the Icon Naming Spec
     57      or conventional Gnome icon names respectively.
     58 
     59      See http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
     60 
     61      The icon theme is probed for the following names:
     62      1. media-subtype
     63      2. gnome-mime-media-subtype
     64      3. media-x-generic
     65      4. gnome-mime-media
     66 
     67      In the worst case it falls back to the stock file icon.
     68     */
     69     int pos = MIMEType.find('/');
     70     if(pos >= 0) {
     71         String media = MIMEType.substring(0, pos);
     72         String subtype = MIMEType.substring(pos + 1);
     73         GtkIconTheme* iconTheme = gtk_icon_theme_get_default();
     74         String iconName = media + "-" + subtype;
     75         if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
     76             return iconName;
     77         iconName = "gnome-mime-" + media + "-" + subtype;
     78         if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
     79             return iconName;
     80         iconName = media + "-x-generic";
     81         if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
     82             return iconName;
     83         iconName = media + "gnome-mime-" + media;
     84         if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
     85             return iconName;
     86     }
     87     return GTK_STOCK_FILE;
     88 }
     89 
     90 // FIXME: Move the code to ChromeClient::iconForFiles().
     91 PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
     92 {
     93     if (filenames.isEmpty())
     94         return 0;
     95 
     96     if (filenames.size() == 1) {
     97         if (!g_path_skip_root(filenames[0].utf8().data()))
     98             return 0;
     99 
    100         String MIMEType = MIMETypeRegistry::getMIMETypeForPath(filenames[0]);
    101         String iconName = lookupIconName(MIMEType);
    102 
    103         RefPtr<Icon> icon = adoptRef(new Icon);
    104         icon->m_icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), iconName.utf8().data(), 16, GTK_ICON_LOOKUP_USE_BUILTIN, 0);
    105         if (!icon->m_icon)
    106             return 0;
    107         return icon.release();
    108     }
    109 
    110     //FIXME: Implement this
    111     return 0;
    112 }
    113 
    114 void Icon::paint(GraphicsContext* context, const IntRect& rect)
    115 {
    116     if (context->paintingDisabled())
    117         return;
    118 
    119     // TODO: Scale/clip the image if necessary.
    120     cairo_t* cr = context->platformContext()->cr();
    121     cairo_save(cr);
    122     gdk_cairo_set_source_pixbuf(cr, m_icon, rect.x(), rect.y());
    123     cairo_paint(cr);
    124     cairo_restore(cr);
    125 }
    126 
    127 }
    128