2007-12-17 Christian Dywan <christian@twotoasts.de>
authoralp@webkit.org <alp@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 17 Dec 2007 16:18:44 +0000 (16:18 +0000)
committeralp@webkit.org <alp@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 17 Dec 2007 16:18:44 +0000 (16:18 +0000)
        Reviewed by Alp Toker.

        http://bugs.webkit.org/show_bug.cgi?id=16378
        Implement Icon for Gtk

        Icon provides a GdkPixbuf containing a themed icon.
        The icon theme is probed for an icon name according to the
        Icon Naming Specification or conventional Gnome icon names respectively.

        See http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

        * platform/graphics/Icon.h:
        * platform/graphics/gtk/IconGtk.cpp:
        (WebCore::Icon::~Icon):
        (WebCore::lookupIconName):
        (WebCore::Icon::newIconForFile):
        (WebCore::Icon::paint):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@28809 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/graphics/Icon.h
WebCore/platform/graphics/gtk/IconGtk.cpp

index 7cb36a82834cf3b33574c68b78d05ee9eae4fb23..d0a8ffae389d04278cf0fbfba08872526ca1e933 100644 (file)
@@ -1,3 +1,23 @@
+2007-12-17  Christian Dywan  <christian@twotoasts.de>
+
+        Reviewed by Alp Toker.
+
+        http://bugs.webkit.org/show_bug.cgi?id=16378
+        Implement Icon for Gtk
+
+        Icon provides a GdkPixbuf containing a themed icon.
+        The icon theme is probed for an icon name according to the
+        Icon Naming Specification or conventional Gnome icon names respectively.
+
+        See http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
+
+        * platform/graphics/Icon.h:
+        * platform/graphics/gtk/IconGtk.cpp:
+        (WebCore::Icon::~Icon):
+        (WebCore::lookupIconName):
+        (WebCore::Icon::newIconForFile):
+        (WebCore::Icon::paint):
+
 2007-12-16  Sam Weinig  <sam@webkit.org>
 
         Reviewed by Mitz.
index c3ad84faf0752c20b6e5a6a560a3f1c5b8ab0174..310c25a4f4c6b0b1fd1bf7348e623ec3e01ab915 100644 (file)
@@ -35,6 +35,8 @@ class NSImage;
 typedef struct HICON__* HICON;
 #elif PLATFORM(QT)
 #include <QIcon>
+#elif PLATFORM(GTK)
+#include <gdk/gdk.h>
 #endif
 
 namespace WebCore {
@@ -66,6 +68,8 @@ private:
     HICON m_hIcon;
 #elif PLATFORM(QT)
     QIcon m_icon;
+#elif PLATFORM(GTK)
+    GdkPixbuf* m_icon;
 #endif
 };
 
index 15ed3aab7777943bdf29ffdd240e9639ad09e4b5..66c54c86683ef23995814597cfd98b6ec5972f2f 100644 (file)
 #include "config.h"
 #include "Icon.h"
 
+#include "CString.h"
+#include "GraphicsContext.h"
+#include "MIMETypeRegistry.h"
 #include "NotImplemented.h"
 #include "PassRefPtr.h"
 
+#include <gtk/gtk.h>
+
 namespace WebCore {
 
 Icon::Icon()
+    : m_icon(0)
 {
     notImplemented();
 }
 
 Icon::~Icon()
 {
-    notImplemented();
+    if(m_icon)
+        g_object_unref(m_icon);
 }
 
-PassRefPtr<Icon> Icon::newIconForFile(const String&)
+static String lookupIconName(String MIMEType)
 {
-    notImplemented();
-    return PassRefPtr<Icon>(new Icon());
+    /*
+     Lookup an appropriate icon according to either the Icon Naming Spec
+     or conventional Gnome icon names respectively.
+
+     See http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
+
+     The icon theme is probed for the following names:
+     1. media-subtype
+     2. gnome-mime-media-subtype
+     3. media-x-generic
+     4. gnome-mime-media
+
+     In the worst case it falls back to the stock file icon.
+    */
+    int pos = MIMEType.find('/');
+    if(pos >= 0) {
+        String media = MIMEType.substring(0, pos);
+        String subtype = MIMEType.substring(pos + 1);
+        GtkIconTheme* iconTheme = gtk_icon_theme_get_default();
+        String iconName = media + "-" + subtype;
+        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
+            return iconName;
+        iconName = "gnome-mime-" + media + "-" + subtype;
+        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
+            return iconName;
+        iconName = media + "-x-generic";
+        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
+            return iconName;
+        iconName = media + "gnome-mime-" + media;
+        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
+            return iconName;
+    }
+    return GTK_STOCK_FILE;
 }
 
-void Icon::paint(GraphicsContext*, const IntRect&)
+PassRefPtr<Icon> Icon::newIconForFile(const String& filename)
 {
-    notImplemented();
+    if (!g_path_skip_root(filename.utf8().data()))
+        return 0;
+
+    String MIMEType = MIMETypeRegistry::getMIMETypeForPath(filename);
+    String iconName = lookupIconName(MIMEType);
+
+    Icon* icon = new Icon;
+    icon->m_icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), iconName.utf8().data(), 16, GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
+    return icon->m_icon ? icon : 0;
+}
+
+void Icon::paint(GraphicsContext* context, const IntRect& rect)
+{
+    // TODO: Scale/clip the image if necessary.
+    cairo_t* cr = context->platformContext();
+    cairo_save(cr);
+    gdk_cairo_set_source_pixbuf(cr, m_icon, rect.x(), rect.y());
+    cairo_paint(cr);
+    cairo_restore(cr);
 }
 
 }