Пример #1
0
/**
 * gul_gui_image_set_from_mime_type:
 * @image: a #GtkImage
 * @mime_type: a MIME type
 * @icon_size: a stock icon size
 *
 * Set the @image to display the icon for MIME type @mime_type.  Sample MIME
 * types are "text/plain", "application/ogg".  Sample stock sizes are
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_DIALOG.
 */
void
gul_gui_image_set_from_mime_type (GtkWidget  *image,
		                  const char *mime_type,
				  GtkIconSize icon_size)
{
	GtkIconTheme *theme;
	char           *icon;
	GtkIconSource  *source;
	GtkIconSet     *icon_set;

	g_return_if_fail (GTK_IS_IMAGE(image));
	g_return_if_fail (mime_type != NULL);

	theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (image));

	icon = gnome_icon_lookup (theme, NULL, NULL, NULL, NULL,
				  mime_type,
				  GNOME_ICON_LOOKUP_FLAGS_NONE, NULL);

	if (!g_path_is_absolute (icon))
	{
		int   width, height;
		GtkIconInfo *icon_info;

		if (!gtk_icon_size_lookup_for_settings 
			(gtk_widget_get_settings (GTK_WIDGET(image)),
			 icon_size, &width, &height))
		{
			width = height = -1;
		}

		icon_info = gtk_icon_theme_lookup_icon (theme, icon, height,0);
		g_free (icon);

		g_return_if_fail (icon_info != NULL);

		icon = g_strdup (gtk_icon_info_get_filename (icon_info));
		gtk_icon_info_free (icon_info);
	}

	/* Now that we have the icon filename, wrap it into an GtkIconSet so
	 * that we really get the desired size; the icon size in the file may
	 * still be arbitrary, in case of SVG themes, for example.
	 */

	source = gtk_icon_source_new ();
	gtk_icon_source_set_filename (source, icon);
	g_free (icon);

	icon_set = gtk_icon_set_new ();
	gtk_icon_set_add_source (icon_set, source);
	gtk_icon_source_free (source);

	gtk_image_set_from_icon_set (GTK_IMAGE(image), icon_set, icon_size);

	gtk_icon_set_unref (icon_set);
}
Пример #2
0
/**
 * e_icon_for_mime_type:
 * @mime_type: a MIME type
 * @size_hint: the size the caller plans to display the icon at
 *
 * Tries to find an icon representing @mime_type that will display
 * nicely at @size_hint by @size_hint pixels. The returned icon
 * may or may not actually be that size.
 *
 * Return value: a pixbuf, which the caller must unref when it is done
 **/
GdkPixbuf *
e_icon_for_mime_type (const char *mime_type, int size_hint)
{
	gchar *icon_name;
	GdkPixbuf *pixbuf = NULL;

	icon_name = gnome_icon_lookup (
		gtk_icon_theme_get_default (),
		NULL, NULL, NULL, NULL, mime_type, 0, NULL);

	if (icon_name != NULL) {
		pixbuf = gtk_icon_theme_load_icon (
			gtk_icon_theme_get_default (),
			icon_name, size_hint, 0, NULL);
		g_free (icon_name);
	}

	return pixbuf;
}
Пример #3
0
JNIEXPORT jintLong JNICALL GNOME_NATIVE(_1gnome_1icon_1lookup)
	(JNIEnv *env, jclass that, jintLong arg0, jintLong arg1, jbyteArray arg2, jbyteArray arg3, jintLong arg4, jbyteArray arg5, jint arg6, jintArray arg7)
{
	jbyte *lparg2=NULL;
	jbyte *lparg3=NULL;
	jbyte *lparg5=NULL;
	jint *lparg7=NULL;
	jintLong rc = 0;
	GNOME_NATIVE_ENTER(env, that, _1gnome_1icon_1lookup_FUNC);
	if (arg2) if ((lparg2 = (*env)->GetByteArrayElements(env, arg2, NULL)) == NULL) goto fail;
	if (arg3) if ((lparg3 = (*env)->GetByteArrayElements(env, arg3, NULL)) == NULL) goto fail;
	if (arg5) if ((lparg5 = (*env)->GetByteArrayElements(env, arg5, NULL)) == NULL) goto fail;
	if (arg7) if ((lparg7 = (*env)->GetIntArrayElements(env, arg7, NULL)) == NULL) goto fail;
	rc = (jintLong)gnome_icon_lookup((GnomeIconTheme *)arg0, (GnomeThumbnailFactory *)arg1, (const char *)lparg2, (const char *)lparg3, (GnomeVFSFileInfo *)arg4, (const char *)lparg5, (GnomeIconLookupFlags)arg6, (GnomeIconLookupResultFlags *)lparg7);
fail:
	if (arg7 && lparg7) (*env)->ReleaseIntArrayElements(env, arg7, lparg7, 0);
	if (arg5 && lparg5) (*env)->ReleaseByteArrayElements(env, arg5, lparg5, 0);
	if (arg3 && lparg3) (*env)->ReleaseByteArrayElements(env, arg3, lparg3, 0);
	if (arg2 && lparg2) (*env)->ReleaseByteArrayElements(env, arg2, lparg2, 0);
	GNOME_NATIVE_EXIT(env, that, _1gnome_1icon_1lookup_FUNC);
	return rc;
}
Пример #4
0
static GdkPixbuf *
get_pixbuf_from_mime_type (GthFileList *file_list,
			   const char  *mime_type)
{
	GtkIconInfo *icon_info = NULL;
	int          icon_size;
	char        *icon_name;
	GdkPixbuf   *pixbuf = NULL;
	int          width, height;

	if (file_list->view == NULL)
		return NULL;

	if (mime_type == NULL)
		mime_type = "image/*";

	/* look in the hash table. */

	pixbuf = g_hash_table_lookup (file_list->priv->pixbufs, mime_type);
	if (pixbuf != NULL) {
		g_object_ref (G_OBJECT (pixbuf));
		return pixbuf;
	}

	/**/

	icon_size = get_default_icon_size (file_list->root_widget);
	icon_name = gnome_icon_lookup (file_list->priv->icon_theme,
				       NULL,
				       NULL,
				       NULL,
				       NULL,
				       mime_type,
				       GNOME_ICON_LOOKUP_FLAGS_NONE,
				       NULL);
	icon_info = gtk_icon_theme_lookup_icon (file_list->priv->icon_theme,
						icon_name,
						icon_size,
						0);
	g_free (icon_name);

	if (icon_info != NULL) {
		pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
		gtk_icon_info_free (icon_info);
	}

	if (pixbuf == NULL)
		pixbuf = gdk_pixbuf_new_from_inline (-1,
						     dir_16_rgba,
						     FALSE,
						     NULL);

	width = gdk_pixbuf_get_width (pixbuf);
	height = gdk_pixbuf_get_height (pixbuf);
	if (scale_keepping_ratio (&width, &height, icon_size, icon_size)) {
		GdkPixbuf *scaled;
		scaled = gdk_pixbuf_scale_simple (pixbuf,
						  width,
						  height,
						  GDK_INTERP_BILINEAR);
		g_object_unref (pixbuf);
		pixbuf = scaled;
	}

	g_hash_table_insert (file_list->priv->pixbufs, (gpointer) mime_type, pixbuf);
	g_object_ref (pixbuf);

	return pixbuf;
}
static void
tny_gtk_attach_list_model_add (TnyGtkAttachListModel *self, TnyMimePart *part, listaddfunc func)
{
	GtkListStore *model = GTK_LIST_STORE (self);
	GtkTreeIter iter;
	TnyGtkAttachListModelPriv *priv = TNY_GTK_ATTACH_LIST_MODEL_GET_PRIVATE (self);
	static GdkPixbuf *stock_file_pixbuf = NULL;
	GdkPixbuf *pixbuf;
	gchar *icon;

	if (tny_mime_part_get_content_type (part) &&
			tny_mime_part_is_attachment (part))
	{

		if (!priv->theme || !GTK_IS_ICON_THEME (priv->theme))
		{
			priv->theme = gtk_icon_theme_get_default ();
			g_object_ref (priv->theme);
		}

#ifdef GNOME
		if (priv->theme && GTK_IS_ICON_THEME (priv->theme))
		{
			icon = gnome_icon_lookup (priv->theme, NULL, 
				tny_mime_part_get_filename (part), NULL, NULL,
				tny_mime_part_get_content_type (part), 0, NULL);
		}
#else
		icon = GTK_STOCK_FILE;
#endif

		if (G_LIKELY (icon) && priv->theme && GTK_IS_ICON_THEME (priv->theme))
		{
			pixbuf = gtk_icon_theme_load_icon (priv->theme, icon, 
				GTK_ICON_SIZE_LARGE_TOOLBAR, 0, NULL);
#ifdef GNOME
			g_free (icon);
#endif
		} else {
			if (G_UNLIKELY (!stock_file_pixbuf) && priv->theme && GTK_IS_ICON_THEME (priv->theme))
				stock_file_pixbuf = gtk_icon_theme_load_icon (priv->theme, 
					GTK_STOCK_FILE, GTK_ICON_SIZE_LARGE_TOOLBAR, 
					0, NULL);

			pixbuf = stock_file_pixbuf;
		}

		func (model, &iter);

		gtk_list_store_set (model, &iter,
			TNY_GTK_ATTACH_LIST_MODEL_PIXBUF_COLUMN, 
			pixbuf,
			TNY_GTK_ATTACH_LIST_MODEL_FILENAME_COLUMN, 
			tny_mime_part_get_filename (part),
			TNY_GTK_ATTACH_LIST_MODEL_INSTANCE_COLUMN,
			part, -1);
	} else {

		func (model, &iter);

		gtk_list_store_set (model, &iter,
			TNY_GTK_ATTACH_LIST_MODEL_FILENAME_COLUMN, 
			tny_mime_part_get_description (part)?
				tny_mime_part_get_description (part):
				"Unknown attachment",
			TNY_GTK_ATTACH_LIST_MODEL_INSTANCE_COLUMN,
			part, -1);
	}

	return;
}
Пример #6
0
nsresult
nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI)
{
  nsresult rv;
  
  if (!gnome_program_get()) {
    // Get the brandShortName from the string bundle to pass to GNOME
    // as the application name.  This may be used for things such as
    // the title of grouped windows in the panel.
    nsCOMPtr<nsIStringBundleService> bundleService = 
      do_GetService(NS_STRINGBUNDLE_CONTRACTID);

    NS_ASSERTION(bundleService, "String bundle service must be present!");

    nsCOMPtr<nsIStringBundle> bundle;
    bundleService->CreateBundle("chrome://branding/locale/brand.properties",
                                getter_AddRefs(bundle));
    nsXPIDLString appName;

    if (bundle) {
      bundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(),
                                getter_Copies(appName));
    } else {
      NS_WARNING("brand.properties not present, using default application name");
      appName.AssignLiteral("Gecko");
    }

    char* empty[] = { "" };
    gnome_init(NS_ConvertUTF16toUTF8(appName).get(), "1.0", 1, empty);
  }

  nsCAutoString iconSizeString;
  aIconURI->GetIconSize(iconSizeString);

  PRUint32 iconSize;

  if (iconSizeString.IsEmpty()) {
    rv = aIconURI->GetImageSize(&iconSize);
    NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed");
  } else {
    int size;
    
    GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get());
    gtk_icon_size_lookup(icon_size, &size, NULL);
    iconSize = size;
  }

  nsCAutoString type;
  aIconURI->GetContentType(type);

  GnomeVFSFileInfo fileInfo = {0};
  fileInfo.refcount = 1; // In case some GnomeVFS function addrefs and releases it

  nsCAutoString spec;
  nsCOMPtr<nsIURI> fileURI;
  rv = aIconURI->GetIconFile(getter_AddRefs(fileURI));
  if (fileURI) {
    fileURI->GetAsciiSpec(spec);
    // Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a
    // network request
    PRBool isFile;
    if (NS_SUCCEEDED(fileURI->SchemeIs("file", &isFile)) && isFile) {
      gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT);
    }
    else {
      // We have to get a leaf name from our uri...
      nsCOMPtr<nsIURL> url(do_QueryInterface(fileURI));
      if (url) {
        nsCAutoString name;
        // The filename we get is UTF-8-compatible, which matches gnome expectations.
        // See also: http://lists.gnome.org/archives/gnome-vfs-list/2004-March/msg00049.html
        // "Whenever we can detect the charset used for the URI type we try to
        //  convert it to/from utf8 automatically inside gnome-vfs."
        // I'll interpret that as "otherwise, this field is random junk".
        url->GetFileName(name);
        fileInfo.name = g_strdup(name.get());
      }
      // If this is no nsIURL, nothing we can do really.

      if (!type.IsEmpty()) {
        fileInfo.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
        fileInfo.mime_type = g_strdup(type.get());
      }
    }
  }


  if (type.IsEmpty()) {
    nsCOMPtr<nsIMIMEService> ms(do_GetService("@mozilla.org/mime;1"));
    if (ms) {
      nsCAutoString fileExt;
      aIconURI->GetFileExtension(fileExt);
      ms->GetTypeFromExtension(fileExt, type);
    }
  }

  // Get the icon theme
  if (!gIconTheme) {
    gIconTheme = gnome_icon_theme_new();
    if (!gIconTheme) {
      gnome_vfs_file_info_clear(&fileInfo);
      return NS_ERROR_NOT_AVAILABLE;
    }
  }

  char* name = gnome_icon_lookup(gIconTheme, NULL, spec.get(), NULL, &fileInfo,
                                 type.get(), GNOME_ICON_LOOKUP_FLAGS_NONE,
                                 NULL);
  gnome_vfs_file_info_clear(&fileInfo);
  if (!name) {
    return NS_ERROR_NOT_AVAILABLE;
  }
 
  char* file = gnome_icon_theme_lookup_icon(gIconTheme, name, iconSize,
                                            NULL, NULL);
  g_free(name);
  if (!file)
    return NS_ERROR_NOT_AVAILABLE;

  // Create a GdkPixbuf buffer and scale it
  GError *err = nsnull;
  GdkPixbuf* buf = gdk_pixbuf_new_from_file(file, &err);
  g_free(file);
  if (!buf) {
    if (err)
      g_error_free(err);
    return NS_ERROR_UNEXPECTED;
  }

  GdkPixbuf* scaled = buf;
  if (gdk_pixbuf_get_width(buf)  != iconSize &&
      gdk_pixbuf_get_height(buf) != iconSize) {
    // scale...
    scaled = gdk_pixbuf_scale_simple(buf, iconSize, iconSize,
                                     GDK_INTERP_BILINEAR);
    gdk_pixbuf_unref(buf);
    if (!scaled)
      return NS_ERROR_OUT_OF_MEMORY;
  }

  // XXX Respect icon state
  
  rv = moz_gdk_pixbuf_to_channel(scaled, aIconURI,
                                 getter_AddRefs(mRealChannel));
  gdk_pixbuf_unref(scaled);
  return rv;
}