PIKApp/app/core/pikaparamspecs.c

361 lines
10 KiB
C
Raw Normal View History

2023-09-26 00:35:21 +02:00
/* 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
*
* 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libpikabase/pikabase.h"
#include "core-types.h"
#include "pika.h"
#include "pikabrush.h"
#include "pikadisplay.h"
#include "pikagradient.h"
#include "pikaimage.h"
#include "pikalayer.h"
#include "pikalayermask.h"
#include "pikapalette.h"
#include "pikaparamspecs.h"
#include "pikapattern.h"
#include "pikaselection.h"
#include "text/pikafont.h"
#include "text/pikatextlayer.h"
#include "vectors/pikavectors.h"
/*
* PIKA_TYPE_PARAM_STRING
*/
static void pika_param_string_class_init (GParamSpecClass *klass);
static void pika_param_string_init (GParamSpec *pspec);
static gboolean pika_param_string_validate (GParamSpec *pspec,
GValue *value);
static GParamSpecClass * pika_param_string_parent_class = NULL;
GType
pika_param_string_get_type (void)
{
static GType type = 0;
if (! type)
{
const GTypeInfo info =
{
sizeof (GParamSpecClass),
NULL, NULL,
(GClassInitFunc) pika_param_string_class_init,
NULL, NULL,
sizeof (PikaParamSpecString),
0,
(GInstanceInitFunc) pika_param_string_init
};
type = g_type_register_static (G_TYPE_PARAM_STRING,
"PikaParamString", &info, 0);
}
return type;
}
static void
pika_param_string_class_init (GParamSpecClass *klass)
{
pika_param_string_parent_class = g_type_class_peek_parent (klass);
klass->value_type = G_TYPE_STRING;
klass->value_validate = pika_param_string_validate;
}
static void
pika_param_string_init (GParamSpec *pspec)
{
PikaParamSpecString *sspec = PIKA_PARAM_SPEC_STRING (pspec);
G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
sspec->allow_non_utf8 = FALSE;
sspec->non_empty = FALSE;
}
static gboolean
pika_param_string_validate (GParamSpec *pspec,
GValue *value)
{
PikaParamSpecString *sspec = PIKA_PARAM_SPEC_STRING (pspec);
gchar *string = value->data[0].v_pointer;
if (pika_param_string_parent_class->value_validate (pspec, value))
return TRUE;
if (string)
{
gchar *s;
if (sspec->non_empty && ! string[0])
{
if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
g_free (string);
else
value->data[1].v_uint &= ~G_VALUE_NOCOPY_CONTENTS;
value->data[0].v_pointer = g_strdup ("none");
return TRUE;
}
if (! sspec->allow_non_utf8 &&
! g_utf8_validate (string, -1, (const gchar **) &s))
{
if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
{
value->data[0].v_pointer = g_strdup (string);
value->data[1].v_uint &= ~G_VALUE_NOCOPY_CONTENTS;
string = value->data[0].v_pointer;
}
for (s = string; *s; s++)
if (*s < ' ')
*s = '?';
return TRUE;
}
}
else if (sspec->non_empty)
{
value->data[1].v_uint &= ~G_VALUE_NOCOPY_CONTENTS;
value->data[0].v_pointer = g_strdup ("none");
return TRUE;
}
return FALSE;
}
/**
* pika_param_spec_string:
* @name: Canonical name of the property specified.
* @nick: Nick name of the property specified.
* @blurb: Description of the property specified.
* @allow_non_utf8: Whether non-UTF-8 strings are allowed.
* @null_ok: Whether %NULL is allowed.
* @non_empty: Whether a non-½NULL value must be set.
* @default_value: The default value.
* @flags: Flags for the property specified.
*
* Creates a new #PikaParamSpecString specifying a
* [type@GLib.String] property.
*
* If @allow_non_utf8 is %FALSE, non-valid UTF-8 strings will be
* replaced by question marks.
*
* If @null_ok is %FALSE, %NULL strings will be replaced by an empty
* string.
*
* If @non_empty is %TRUE, empty strings will be replaced by `"none"`.
*
* See g_param_spec_internal() for details on property names.
*
* Returns: (transfer full): The newly created #PikaParamSpecString.
*
* Since: 3.0
**/
GParamSpec *
pika_param_spec_string (const gchar *name,
const gchar *nick,
const gchar *blurb,
gboolean allow_non_utf8,
gboolean null_ok,
gboolean non_empty,
const gchar *default_value,
GParamFlags flags)
{
PikaParamSpecString *sspec;
g_return_val_if_fail (! (null_ok && non_empty), NULL);
sspec = g_param_spec_internal (PIKA_TYPE_PARAM_STRING,
name, nick, blurb, flags);
if (sspec)
{
g_free (G_PARAM_SPEC_STRING (sspec)->default_value);
G_PARAM_SPEC_STRING (sspec)->default_value = g_strdup (default_value);
G_PARAM_SPEC_STRING (sspec)->ensure_non_null = null_ok ? FALSE : TRUE;
sspec->allow_non_utf8 = allow_non_utf8 ? TRUE : FALSE;
sspec->non_empty = non_empty ? TRUE : FALSE;
}
return G_PARAM_SPEC (sspec);
}
/*
* PIKA_TYPE_PARAM_ENUM
*/
static void pika_param_enum_class_init (GParamSpecClass *klass);
static void pika_param_enum_init (GParamSpec *pspec);
static void pika_param_enum_finalize (GParamSpec *pspec);
static gboolean pika_param_enum_validate (GParamSpec *pspec,
GValue *value);
GType
pika_param_enum_get_type (void)
{
static GType type = 0;
if (! type)
{
const GTypeInfo info =
{
sizeof (GParamSpecClass),
NULL, NULL,
(GClassInitFunc) pika_param_enum_class_init,
NULL, NULL,
sizeof (PikaParamSpecEnum),
0,
(GInstanceInitFunc) pika_param_enum_init
};
type = g_type_register_static (G_TYPE_PARAM_ENUM,
"PikaParamEnum", &info, 0);
}
return type;
}
static void
pika_param_enum_class_init (GParamSpecClass *klass)
{
klass->value_type = G_TYPE_ENUM;
klass->finalize = pika_param_enum_finalize;
klass->value_validate = pika_param_enum_validate;
}
static void
pika_param_enum_init (GParamSpec *pspec)
{
PikaParamSpecEnum *espec = PIKA_PARAM_SPEC_ENUM (pspec);
espec->excluded_values = NULL;
}
static void
pika_param_enum_finalize (GParamSpec *pspec)
{
PikaParamSpecEnum *espec = PIKA_PARAM_SPEC_ENUM (pspec);
GParamSpecClass *parent_class;
parent_class = g_type_class_peek (g_type_parent (PIKA_TYPE_PARAM_ENUM));
g_slist_free (espec->excluded_values);
parent_class->finalize (pspec);
}
static gboolean
pika_param_enum_validate (GParamSpec *pspec,
GValue *value)
{
PikaParamSpecEnum *espec = PIKA_PARAM_SPEC_ENUM (pspec);
GParamSpecClass *parent_class;
GSList *list;
parent_class = g_type_class_peek (g_type_parent (PIKA_TYPE_PARAM_ENUM));
if (parent_class->value_validate (pspec, value))
return TRUE;
for (list = espec->excluded_values; list; list = g_slist_next (list))
{
if (GPOINTER_TO_INT (list->data) == value->data[0].v_long)
{
value->data[0].v_long = G_PARAM_SPEC_ENUM (pspec)->default_value;
return TRUE;
}
}
return FALSE;
}
GParamSpec *
pika_param_spec_enum (const gchar *name,
const gchar *nick,
const gchar *blurb,
GType enum_type,
gint default_value,
GParamFlags flags)
{
PikaParamSpecEnum *espec;
GEnumClass *enum_class;
g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
enum_class = g_type_class_ref (enum_type);
g_return_val_if_fail (g_enum_get_value (enum_class, default_value) != NULL,
NULL);
espec = g_param_spec_internal (PIKA_TYPE_PARAM_ENUM,
name, nick, blurb, flags);
G_PARAM_SPEC_ENUM (espec)->enum_class = enum_class;
G_PARAM_SPEC_ENUM (espec)->default_value = default_value;
G_PARAM_SPEC (espec)->value_type = enum_type;
return G_PARAM_SPEC (espec);
}
void
pika_param_spec_enum_exclude_value (PikaParamSpecEnum *espec,
gint value)
{
g_return_if_fail (PIKA_IS_PARAM_SPEC_ENUM (espec));
g_return_if_fail (g_enum_get_value (G_PARAM_SPEC_ENUM (espec)->enum_class,
value) != NULL);
espec->excluded_values = g_slist_prepend (espec->excluded_values,
GINT_TO_POINTER (value));
}
/* include the implementation of the remaining paramspecs, they are
* shared between app/ and libpika/ but need different headers.
*/
#define pika_image_is_valid(image) TRUE
#define pika_item_is_valid(item) TRUE
#define pika_display_is_valid(display) TRUE
#define pika_resource_is_valid(image) TRUE
#define pika_brush_is_valid(image) TRUE
#define pika_font_is_valid(image) TRUE
#define pika_gradient_is_valid(image) TRUE
#define pika_palette_is_valid(image) TRUE
#define pika_pattern_is_valid(image) TRUE
#include "../../libpika/pikaparamspecs-body.c"