Bug 562506 - Use gtk_icon_theme_add_builtin_icon and gtk_window_set_icon_name for nsWindow::SetIcon with the GTK widget. r=karlt
authorJean-Alexandre Anglès d'Auriac <jagw40k@free.fr>
Wed, 14 Dec 2011 11:22:15 +0100
changeset 84183 ffea93b21d4d78de21b1ba5e6933698cbd6a6480
parent 84182 c7ffe35493891b03a83cd6326910d17e21460baa
child 84184 fd6ab19f312ca587ac73500eedb603736b0e5806
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt
bugs562506
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 562506 - Use gtk_icon_theme_add_builtin_icon and gtk_window_set_icon_name for nsWindow::SetIcon with the GTK widget. r=karlt
widget/src/gtk2/nsWindow.cpp
widget/src/gtk2/nsWindow.h
--- a/widget/src/gtk2/nsWindow.cpp
+++ b/widget/src/gtk2/nsWindow.cpp
@@ -198,16 +198,18 @@ static GdkWindow *get_inner_gdk_window (
 
 static inline bool is_context_menu_key(const nsKeyEvent& inKeyEvent);
 static void   key_event_to_context_menu_event(nsMouseEvent &aEvent,
                                               GdkEventKey *aGdkEvent);
 
 static int    is_parent_ungrab_enter(GdkEventCrossing *aEvent);
 static int    is_parent_grab_leave(GdkEventCrossing *aEvent);
 
+static void GetBrandName(nsXPIDLString& brandName);
+
 /* callbacks from widgets */
 #if defined(MOZ_WIDGET_GTK2)
 static gboolean expose_event_cb           (GtkWidget *widget,
                                            GdkEventExpose *event);
 #else
 static gboolean expose_event_cb           (GtkWidget *widget,
                                            cairo_t *rect);
 #endif
@@ -1841,48 +1843,72 @@ nsWindow::SetTitle(const nsAString& aTit
 }
 
 NS_IMETHODIMP
 nsWindow::SetIcon(const nsAString& aIconSpec)
 {
     if (!mShell)
         return NS_OK;
 
+    nsCAutoString iconName;
+    
+    if (aIconSpec.EqualsLiteral("default")) {
+        nsXPIDLString brandName;
+        GetBrandName(brandName);
+        AppendUTF16toUTF8(brandName, iconName);
+        ToLowerCase(iconName);
+    } else {
+        AppendUTF16toUTF8(aIconSpec, iconName);
+    }
+    
     nsCOMPtr<nsILocalFile> iconFile;
     nsCAutoString path;
-    nsTArray<nsCString> iconList;
-
-    // Look for icons with the following suffixes appended to the base name.
-    // The last two entries (for the old XPM format) will be ignored unless
-    // no icons are found using the other suffixes. XPM icons are depricated.
-
-    const char extensions[6][7] = { ".png", "16.png", "32.png", "48.png",
+
+    bool foundIcon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(),
+                                             iconName.get());
+
+    if (!foundIcon) {
+        // Look for icons with the following suffixes appended to the base name
+        // The last two entries (for the old XPM format) will be ignored unless
+        // no icons are found using other suffixes. XPM icons are deprecated.
+
+        const char extensions[6][7] = { ".png", "16.png", "32.png", "48.png",
                                     ".xpm", "16.xpm" };
 
-    for (PRUint32 i = 0; i < ArrayLength(extensions); i++) {
-        // Don't bother looking for XPM versions if we found a PNG.
-        if (i == ArrayLength(extensions) - 2 && iconList.Length())
-            break;
-
-        nsAutoString extension;
-        extension.AppendASCII(extensions[i]);
-
-        ResolveIconName(aIconSpec, extension, getter_AddRefs(iconFile));
-        if (iconFile) {
-            iconFile->GetNativePath(path);
-            iconList.AppendElement(path);
+        for (PRUint32 i = 0; i < ArrayLength(extensions); i++) {
+            // Don't bother looking for XPM versions if we found a PNG.
+            if (i == ArrayLength(extensions) - 2 && foundIcon)
+                break;
+
+            nsAutoString extension;
+            extension.AppendASCII(extensions[i]);
+
+            ResolveIconName(aIconSpec, extension, getter_AddRefs(iconFile));
+            if (iconFile) {
+                iconFile->GetNativePath(path);
+                GdkPixbuf *icon = gdk_pixbuf_new_from_file(path.get(), NULL);
+                if (icon){
+                    gtk_icon_theme_add_builtin_icon(iconName.get(),
+                                                    gdk_pixbuf_get_height(icon),
+                                                    icon);
+                    g_object_unref(icon);
+                    foundIcon = true;
+                }
+            }
         }
     }
 
     // leave the default icon intact if no matching icons were found
-    if (iconList.Length() == 0)
-        return NS_OK;
-
-    return SetWindowIconList(iconList);
-}
+    if (foundIcon) {
+        gtk_window_set_icon_name(GTK_WINDOW(mShell), iconName.get());
+    }
+
+    return NS_OK;
+}
+
 
 nsIntPoint
 nsWindow::WidgetToScreenOffset()
 {
     gint x = 0, y = 0;
 
     if (mGdkWindow) {
         gdk_window_get_origin(mGdkWindow, &x, &y);
@@ -5140,43 +5166,16 @@ nsWindow::SetupPluginPort(void)
     gdk_window_add_filter(mGdkWindow, plugin_window_filter_func, this);
 
     XSync(display, False);
 #endif /* MOZ_X11 */
     
     return (void *)window;
 }
 
-nsresult
-nsWindow::SetWindowIconList(const nsTArray<nsCString> &aIconList)
-{
-    GList *list = NULL;
-
-    for (PRUint32 i = 0; i < aIconList.Length(); ++i) {
-        const char *path = aIconList[i].get();
-        LOG(("window [%p] Loading icon from %s\n", (void *)this, path));
-
-        GdkPixbuf *icon = gdk_pixbuf_new_from_file(path, NULL);
-        if (!icon)
-            continue;
-
-        list = g_list_append(list, icon);
-    }
-
-    if (!list)
-        return NS_ERROR_FAILURE;
-
-    gtk_window_set_icon_list(GTK_WINDOW(mShell), list);
-
-    g_list_foreach(list, (GFunc) g_object_unref, NULL);
-    g_list_free(list);
-
-    return NS_OK;
-}
-
 void
 nsWindow::SetDefaultIcon(void)
 {
     SetIcon(NS_LITERAL_STRING("default"));
 }
 
 void
 nsWindow::SetPluginType(PluginType aPluginType)
--- a/widget/src/gtk2/nsWindow.h
+++ b/widget/src/gtk2/nsWindow.h
@@ -380,17 +380,16 @@ protected:
 
 private:
     void               DestroyChildWindows();
     void               GetToplevelWidget(GtkWidget **aWidget);
     GtkWidget         *GetMozContainerWidget();
     nsWindow          *GetContainerWindow();
     void               SetUrgencyHint(GtkWidget *top_window, bool state);
     void              *SetupPluginPort(void);
-    nsresult           SetWindowIconList(const nsTArray<nsCString> &aIconList);
     void               SetDefaultIcon(void);
     void               InitButtonEvent(nsMouseEvent &aEvent, GdkEventButton *aGdkEvent);
     bool               DispatchCommandEvent(nsIAtom* aCommand);
     bool               DispatchContentCommandEvent(PRInt32 aMsg);
     void               SetWindowClipRegion(const nsTArray<nsIntRect>& aRects,
                                            bool aIntersectWithExisting);
     bool               GetDragInfo(nsMouseEvent* aMouseEvent,
                                    GdkWindow** aWindow, gint* aButton,