From 8d7ccfe6509d39a118907d6d6b2547f4b14a587a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Alexandre=20Angl=C3=A8s=20d=27Auriac?= Date: Wed, 14 Dec 2011 11:22:15 +0100 Subject: [PATCH] Bug 562506 - Use gtk_icon_theme_add_builtin_icon and gtk_window_set_icon_name for nsWindow::SetIcon with the GTK widget. r=karlt --HG-- extra : rebase_source : d6f513b35dd4c1bc9fcd7d7609ccbe2bc4940c9e --- widget/src/gtk2/nsWindow.cpp | 89 ++++++++++++++++++------------------ widget/src/gtk2/nsWindow.h | 1 - 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/widget/src/gtk2/nsWindow.cpp b/widget/src/gtk2/nsWindow.cpp index 1e35eb9283e..5ff608d4f26 100644 --- a/widget/src/gtk2/nsWindow.cpp +++ b/widget/src/gtk2/nsWindow.cpp @@ -203,6 +203,8 @@ static void key_event_to_context_menu_event(nsMouseEvent &aEvent, 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, @@ -1846,39 +1848,63 @@ 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 iconFile; nsCAutoString path; - nsTArray 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. + bool foundIcon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), + iconName.get()); - const char extensions[6][7] = { ".png", "16.png", "32.png", "48.png", + 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; + 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]); + nsAutoString extension; + extension.AppendASCII(extensions[i]); - ResolveIconName(aIconSpec, extension, getter_AddRefs(iconFile)); - if (iconFile) { - iconFile->GetNativePath(path); - iconList.AppendElement(path); + 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; + if (foundIcon) { + gtk_window_set_icon_name(GTK_WINDOW(mShell), iconName.get()); + } - return SetWindowIconList(iconList); + return NS_OK; } + nsIntPoint nsWindow::WidgetToScreenOffset() { @@ -5145,33 +5171,6 @@ nsWindow::SetupPluginPort(void) return (void *)window; } -nsresult -nsWindow::SetWindowIconList(const nsTArray &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) { diff --git a/widget/src/gtk2/nsWindow.h b/widget/src/gtk2/nsWindow.h index 02e92aea3fe..fad9159657a 100644 --- a/widget/src/gtk2/nsWindow.h +++ b/widget/src/gtk2/nsWindow.h @@ -385,7 +385,6 @@ private: nsWindow *GetContainerWindow(); void SetUrgencyHint(GtkWidget *top_window, bool state); void *SetupPluginPort(void); - nsresult SetWindowIconList(const nsTArray &aIconList); void SetDefaultIcon(void); void InitButtonEvent(nsMouseEvent &aEvent, GdkEventButton *aGdkEvent); bool DispatchCommandEvent(nsIAtom* aCommand);