2015-11-24 18:55:09 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2015 Benjamin Otte <otte@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkimagedefinitionprivate.h"
|
|
|
|
|
|
|
|
typedef struct _GtkImageDefinitionEmpty GtkImageDefinitionEmpty;
|
|
|
|
typedef struct _GtkImageDefinitionIconName GtkImageDefinitionIconName;
|
|
|
|
typedef struct _GtkImageDefinitionGIcon GtkImageDefinitionGIcon;
|
2018-02-16 09:50:49 +00:00
|
|
|
typedef struct _GtkImageDefinitionPaintable GtkImageDefinitionPaintable;
|
2015-11-24 18:55:09 +00:00
|
|
|
|
|
|
|
struct _GtkImageDefinitionEmpty {
|
|
|
|
GtkImageType type;
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2015-11-24 18:55:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkImageDefinitionIconName {
|
|
|
|
GtkImageType type;
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2015-11-24 18:55:09 +00:00
|
|
|
|
|
|
|
char *icon_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkImageDefinitionGIcon {
|
|
|
|
GtkImageType type;
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2015-11-24 18:55:09 +00:00
|
|
|
|
|
|
|
GIcon *gicon;
|
|
|
|
};
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
struct _GtkImageDefinitionPaintable {
|
|
|
|
GtkImageType type;
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2018-02-16 09:50:49 +00:00
|
|
|
|
|
|
|
GdkPaintable *paintable;
|
|
|
|
};
|
|
|
|
|
2015-11-24 18:55:09 +00:00
|
|
|
union _GtkImageDefinition
|
|
|
|
{
|
|
|
|
GtkImageType type;
|
|
|
|
GtkImageDefinitionEmpty empty;
|
|
|
|
GtkImageDefinitionIconName icon_name;
|
|
|
|
GtkImageDefinitionGIcon gicon;
|
2018-02-16 09:50:49 +00:00
|
|
|
GtkImageDefinitionPaintable paintable;
|
2015-11-24 18:55:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GtkImageDefinition *
|
|
|
|
gtk_image_definition_new_empty (void)
|
|
|
|
{
|
|
|
|
static GtkImageDefinitionEmpty empty = { GTK_IMAGE_EMPTY, 1 };
|
|
|
|
|
|
|
|
return gtk_image_definition_ref ((GtkImageDefinition *) &empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GtkImageDefinition *
|
|
|
|
gtk_image_definition_alloc (GtkImageType type)
|
|
|
|
{
|
|
|
|
static gsize sizes[] = {
|
|
|
|
sizeof (GtkImageDefinitionEmpty),
|
|
|
|
sizeof (GtkImageDefinitionIconName),
|
|
|
|
sizeof (GtkImageDefinitionGIcon),
|
2018-02-16 09:50:49 +00:00
|
|
|
sizeof (GtkImageDefinitionPaintable)
|
2015-11-24 18:55:09 +00:00
|
|
|
};
|
|
|
|
GtkImageDefinition *def;
|
|
|
|
|
|
|
|
g_assert (type < G_N_ELEMENTS (sizes));
|
|
|
|
|
|
|
|
def = g_malloc0 (sizes[type]);
|
|
|
|
def->type = type;
|
|
|
|
def->empty.ref_count = 1;
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkImageDefinition *
|
2015-12-01 03:44:29 +00:00
|
|
|
gtk_image_definition_new_icon_name (const char *icon_name)
|
2015-11-24 18:55:09 +00:00
|
|
|
{
|
|
|
|
GtkImageDefinition *def;
|
|
|
|
|
|
|
|
if (icon_name == NULL || icon_name[0] == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
def = gtk_image_definition_alloc (GTK_IMAGE_ICON_NAME);
|
|
|
|
def->icon_name.icon_name = g_strdup (icon_name);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkImageDefinition *
|
2015-12-01 03:44:29 +00:00
|
|
|
gtk_image_definition_new_gicon (GIcon *gicon)
|
2015-11-24 18:55:09 +00:00
|
|
|
{
|
|
|
|
GtkImageDefinition *def;
|
|
|
|
|
|
|
|
if (gicon == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
def = gtk_image_definition_alloc (GTK_IMAGE_GICON);
|
|
|
|
def->gicon.gicon = g_object_ref (gicon);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
GtkImageDefinition *
|
|
|
|
gtk_image_definition_new_paintable (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GtkImageDefinition *def;
|
|
|
|
|
|
|
|
if (paintable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
def = gtk_image_definition_alloc (GTK_IMAGE_PAINTABLE);
|
|
|
|
def->paintable.paintable = g_object_ref (paintable);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2015-11-24 18:55:09 +00:00
|
|
|
GtkImageDefinition *
|
|
|
|
gtk_image_definition_ref (GtkImageDefinition *def)
|
|
|
|
{
|
2022-04-19 14:33:21 +00:00
|
|
|
GtkImageDefinitionEmpty *empty = (GtkImageDefinitionEmpty *) def;
|
|
|
|
|
|
|
|
empty->ref_count++;
|
2015-11-24 18:55:09 +00:00
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_image_definition_unref (GtkImageDefinition *def)
|
|
|
|
{
|
2022-04-19 14:33:21 +00:00
|
|
|
GtkImageDefinitionEmpty *empty = (GtkImageDefinitionEmpty *) def;
|
|
|
|
|
|
|
|
empty->ref_count--;
|
2015-11-24 18:55:09 +00:00
|
|
|
|
2022-04-19 14:33:21 +00:00
|
|
|
if (empty->ref_count > 0)
|
2015-11-24 18:55:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (def->type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case GTK_IMAGE_EMPTY:
|
2015-12-02 11:29:58 +00:00
|
|
|
g_assert_not_reached ();
|
2015-11-24 18:55:09 +00:00
|
|
|
break;
|
2018-02-16 09:50:49 +00:00
|
|
|
case GTK_IMAGE_PAINTABLE:
|
2022-04-19 14:33:21 +00:00
|
|
|
{
|
|
|
|
GtkImageDefinitionPaintable *paintable = (GtkImageDefinitionPaintable *) def;
|
|
|
|
g_object_unref (paintable->paintable);
|
|
|
|
}
|
2018-02-16 09:50:49 +00:00
|
|
|
break;
|
2015-11-24 18:55:09 +00:00
|
|
|
case GTK_IMAGE_ICON_NAME:
|
2022-04-19 14:33:21 +00:00
|
|
|
{
|
|
|
|
GtkImageDefinitionIconName *icon_name = (GtkImageDefinitionIconName *) def;
|
|
|
|
g_free (icon_name->icon_name);
|
|
|
|
}
|
2015-11-24 18:55:09 +00:00
|
|
|
break;
|
|
|
|
case GTK_IMAGE_GICON:
|
2022-04-19 14:33:21 +00:00
|
|
|
{
|
|
|
|
GtkImageDefinitionGIcon *gicon = (GtkImageDefinitionGIcon *) def;
|
|
|
|
g_object_unref (gicon->gicon);
|
|
|
|
}
|
2015-11-24 18:55:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (def);
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:52:23 +00:00
|
|
|
GtkImageType
|
2015-11-24 18:55:09 +00:00
|
|
|
gtk_image_definition_get_storage_type (const GtkImageDefinition *def)
|
|
|
|
{
|
|
|
|
return def->type;
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
int
|
2015-11-24 18:55:09 +00:00
|
|
|
gtk_image_definition_get_scale (const GtkImageDefinition *def)
|
|
|
|
{
|
|
|
|
switch (def->type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
case GTK_IMAGE_EMPTY:
|
2018-02-16 09:50:49 +00:00
|
|
|
case GTK_IMAGE_PAINTABLE:
|
2015-11-24 18:55:09 +00:00
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *
|
2015-11-24 18:55:09 +00:00
|
|
|
gtk_image_definition_get_icon_name (const GtkImageDefinition *def)
|
|
|
|
{
|
2022-04-19 14:33:21 +00:00
|
|
|
const GtkImageDefinitionIconName *icon_name = (const GtkImageDefinitionIconName *) def;
|
|
|
|
|
2015-11-24 18:55:09 +00:00
|
|
|
if (def->type != GTK_IMAGE_ICON_NAME)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-19 14:33:21 +00:00
|
|
|
return icon_name->icon_name;
|
2015-11-24 18:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GIcon *
|
|
|
|
gtk_image_definition_get_gicon (const GtkImageDefinition *def)
|
|
|
|
{
|
2022-04-19 14:33:21 +00:00
|
|
|
const GtkImageDefinitionGIcon *gicon = (const GtkImageDefinitionGIcon *) def;
|
|
|
|
|
2015-11-24 18:55:09 +00:00
|
|
|
if (def->type != GTK_IMAGE_GICON)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-19 14:33:21 +00:00
|
|
|
return gicon->gicon;
|
2015-11-24 18:55:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
GdkPaintable *
|
|
|
|
gtk_image_definition_get_paintable (const GtkImageDefinition *def)
|
|
|
|
{
|
2022-04-19 14:33:21 +00:00
|
|
|
const GtkImageDefinitionPaintable *paintable = (const GtkImageDefinitionPaintable *) def;
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
if (def->type != GTK_IMAGE_PAINTABLE)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-19 14:33:21 +00:00
|
|
|
return paintable->paintable;
|
2018-02-16 09:50:49 +00:00
|
|
|
}
|