PIKApp/app/widgets/pikapixbuf.c

155 lines
3.9 KiB
C

/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikapixbuf.c
* Copyright (C) 2005 Michael Natterer <mitch@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include "widgets-types.h"
#include "pikapixbuf.h"
/* local function prototypes */
static gint pika_pixbuf_format_compare (GdkPixbufFormat *a,
GdkPixbufFormat *b);
/* public functions */
GSList *
pika_pixbuf_get_formats (void)
{
return g_slist_sort (gdk_pixbuf_get_formats (),
(GCompareFunc) pika_pixbuf_format_compare);
}
void
pika_pixbuf_targets_add (GtkTargetList *target_list,
guint info,
gboolean writable)
{
GSList *formats;
GSList *list;
g_return_if_fail (target_list != NULL);
formats = pika_pixbuf_get_formats ();
for (list = formats; list; list = g_slist_next (list))
{
GdkPixbufFormat *format = list->data;
gchar **mime_types;
gchar **type;
if (writable && ! gdk_pixbuf_format_is_writable (format))
continue;
mime_types = gdk_pixbuf_format_get_mime_types (format);
for (type = mime_types; *type; type++)
{
/* skip Windows ICO as writable format */
if (writable && strcmp (*type, "image/x-icon") == 0)
continue;
gtk_target_list_add (target_list,
gdk_atom_intern (*type, FALSE), 0, info);
}
g_strfreev (mime_types);
}
g_slist_free (formats);
}
void
pika_pixbuf_targets_remove (GtkTargetList *target_list)
{
GSList *formats;
GSList *list;
g_return_if_fail (target_list != NULL);
formats = pika_pixbuf_get_formats ();
for (list = formats; list; list = g_slist_next (list))
{
GdkPixbufFormat *format = list->data;
gchar **mime_types;
gchar **type;
mime_types = gdk_pixbuf_format_get_mime_types (format);
for (type = mime_types; *type; type++)
{
gtk_target_list_remove (target_list,
gdk_atom_intern (*type, FALSE));
}
g_strfreev (mime_types);
}
g_slist_free (formats);
}
/* private functions */
static gint
pika_pixbuf_format_compare (GdkPixbufFormat *a,
GdkPixbufFormat *b)
{
gchar *a_name = gdk_pixbuf_format_get_name (a);
gchar *b_name = gdk_pixbuf_format_get_name (b);
gint retval = 0;
/* move PNG to the front of the list */
if (strcmp (a_name, "png") == 0)
retval = -1;
else if (strcmp (b_name, "png") == 0)
retval = 1;
/* move JPEG to the end of the list */
else if (strcmp (a_name, "jpeg") == 0)
retval = 1;
else if (strcmp (b_name, "jpeg") == 0)
retval = -1;
/* move GIF to the end of the list */
else if (strcmp (a_name, "gif") == 0)
retval = 1;
else if (strcmp (b_name, "gif") == 0)
retval = -1;
g_free (a_name);
g_free (b_name);
return retval;
}