gimp/app/core/gimpparamspecs-desc.c

206 lines
5.7 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
* 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 <string.h>
#include <gio/gio.h>
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "gimpparamspecs.h"
#include "gimpparamspecs-desc.h"
static inline const gchar *
gimp_param_spec_get_blurb (GParamSpec *pspec)
{
const gchar *blurb = g_param_spec_get_blurb (pspec);
return blurb ? blurb : "";
}
static gchar *
gimp_param_spec_boolean_desc (GParamSpec *pspec)
{
const gchar *blurb = gimp_param_spec_get_blurb (pspec);
return g_strconcat (blurb, " (TRUE or FALSE)", NULL);
}
Issue #8900 and #9923: reimplementing GimpUnit as a proper class. This fixes all our GObject Introspection issues with GimpUnit which was both an enum and an int-derived type of user-defined units *completing* the enum values. GIR clearly didn't like this! Now GimpUnit is a proper class and units are unique objects, allowing to compare them with an identity test (i.e. `unit == gimp_unit_pixel ()` tells us if unit is the pixel unit or not), which makes it easy to use, just like with int, yet adding also methods, making for nicer introspected API. As an aside, this also fixes #10738, by having all the built-in units retrievable even if libgimpbase had not been properly initialized with gimp_base_init(). I haven't checked in details how GIR works to introspect, but it looks like it loads the library to inspect and runs functions, hence triggering some CRITICALS because virtual methods (supposed to be initialized with gimp_base_init() run by libgimp) are not set. This new code won't trigger any critical because the vtable method are now not necessary, at least for all built-in units. Note that GimpUnit is still in libgimpbase. It could have been moved to libgimp in order to avoid any virtual method table (since we need to keep core and libgimp side's units in sync, PDB is required), but too many libgimpwidgets widgets were already using GimpUnit. And technically most of GimpUnit logic doesn't require PDB (only the creation/sync part). This is one of the reasons why user-created GimpUnit list is handled and stored differently from other types of objects. Globally this simplifies the code a lot too and we don't need separate implementations of various utils for core and libgimp, which means less prone to errors.
2024-07-26 02:55:21 +08:00
static gchar *
gimp_param_spec_unit_desc (GParamSpec *pspec)
{
GimpParamSpecUnit *uspec = GIMP_PARAM_SPEC_UNIT (pspec);
return g_strdup_printf ("<i>(default %s%s%s)</i>",
gimp_unit_get_abbreviation (uspec->default_value),
uspec->allow_pixel ? ", pixel allowed": "",
uspec->allow_percent ? ", percent allowed": "");
}
static gchar *
gimp_param_spec_int_desc (GParamSpec *pspec)
{
GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
const gchar *blurb = gimp_param_spec_get_blurb (pspec);
if (ispec->minimum == G_MININT32 && ispec->maximum == G_MAXINT32)
return g_strdup (blurb);
if (ispec->minimum == G_MININT32)
return g_strdup_printf ("%s (%s <= %d)", blurb,
g_param_spec_get_name (pspec),
ispec->maximum);
if (ispec->maximum == G_MAXINT32)
return g_strdup_printf ("%s (%s >= %d)", blurb,
g_param_spec_get_name (pspec),
ispec->minimum);
return g_strdup_printf ("%s (%d <= %s <= %d)", blurb,
ispec->minimum,
g_param_spec_get_name (pspec),
ispec->maximum);
}
static gchar *
gimp_param_spec_double_desc (GParamSpec *pspec)
{
GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (pspec);
const gchar *blurb = gimp_param_spec_get_blurb (pspec);
if (dspec->minimum == - G_MAXDOUBLE && dspec->maximum == G_MAXDOUBLE)
return g_strdup (blurb);
if (dspec->minimum == - G_MAXDOUBLE)
return g_strdup_printf ("%s (%s <= %g)", blurb,
g_param_spec_get_name (pspec),
dspec->maximum);
if (dspec->maximum == G_MAXDOUBLE)
return g_strdup_printf ("%s (%s >= %g)", blurb,
g_param_spec_get_name (pspec),
dspec->minimum);
return g_strdup_printf ("%s (%g <= %s <= %g)", blurb,
dspec->minimum,
g_param_spec_get_name (pspec),
dspec->maximum);
}
static gchar *
gimp_param_spec_enum_desc (GParamSpec *pspec)
{
const gchar *blurb = gimp_param_spec_get_blurb (pspec);
GString *str = g_string_new (blurb);
GEnumClass *enum_class = g_type_class_peek (pspec->value_type);
GEnumValue *enum_value;
GSList *excluded;
gint i, n;
if (GIMP_IS_PARAM_SPEC_ENUM (pspec))
excluded = GIMP_PARAM_SPEC_ENUM (pspec)->excluded_values;
else
excluded = NULL;
g_string_append (str, " { ");
for (i = 0, n = 0, enum_value = enum_class->values;
i < enum_class->n_values;
i++, enum_value++)
{
GSList *list;
gchar *name;
for (list = excluded; list; list = list->next)
{
gint value = GPOINTER_TO_INT (list->data);
if (value == enum_value->value)
break;
}
if (list)
continue;
if (n > 0)
g_string_append (str, ", ");
if (G_LIKELY (g_str_has_prefix (enum_value->value_name, "GIMP_")))
name = gimp_canonicalize_identifier (enum_value->value_name + 5);
else
name = gimp_canonicalize_identifier (enum_value->value_name);
g_string_append (str, name);
g_free (name);
g_string_append_printf (str, " (%d)", enum_value->value);
n++;
}
g_string_append (str, " }");
return g_string_free (str, FALSE);
}
/**
* gimp_param_spec_get_desc:
* @pspec: a #GParamSpec
*
* This function creates a description of the passed @pspec, which is
* suitable for use in the PDB. Actually, it currently only deals with
* parameter types used in the PDB and should not be used for anything
* else.
*
* Returns: A newly allocated string describing the parameter.
*/
gchar *
gimp_param_spec_get_desc (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
if (GIMP_IS_PARAM_SPEC_UNIT (pspec))
{
Issue #8900 and #9923: reimplementing GimpUnit as a proper class. This fixes all our GObject Introspection issues with GimpUnit which was both an enum and an int-derived type of user-defined units *completing* the enum values. GIR clearly didn't like this! Now GimpUnit is a proper class and units are unique objects, allowing to compare them with an identity test (i.e. `unit == gimp_unit_pixel ()` tells us if unit is the pixel unit or not), which makes it easy to use, just like with int, yet adding also methods, making for nicer introspected API. As an aside, this also fixes #10738, by having all the built-in units retrievable even if libgimpbase had not been properly initialized with gimp_base_init(). I haven't checked in details how GIR works to introspect, but it looks like it loads the library to inspect and runs functions, hence triggering some CRITICALS because virtual methods (supposed to be initialized with gimp_base_init() run by libgimp) are not set. This new code won't trigger any critical because the vtable method are now not necessary, at least for all built-in units. Note that GimpUnit is still in libgimpbase. It could have been moved to libgimp in order to avoid any virtual method table (since we need to keep core and libgimp side's units in sync, PDB is required), but too many libgimpwidgets widgets were already using GimpUnit. And technically most of GimpUnit logic doesn't require PDB (only the creation/sync part). This is one of the reasons why user-created GimpUnit list is handled and stored differently from other types of objects. Globally this simplifies the code a lot too and we don't need separate implementations of various utils for core and libgimp, which means less prone to errors.
2024-07-26 02:55:21 +08:00
return gimp_param_spec_unit_desc (pspec);
}
else if (G_IS_PARAM_SPEC_INT (pspec))
{
return gimp_param_spec_int_desc (pspec);
}
else
{
switch (G_TYPE_FUNDAMENTAL (pspec->value_type))
{
case G_TYPE_BOOLEAN:
return gimp_param_spec_boolean_desc (pspec);
case G_TYPE_DOUBLE:
return gimp_param_spec_double_desc (pspec);
case G_TYPE_ENUM:
return gimp_param_spec_enum_desc (pspec);
}
}
return g_strdup (g_param_spec_get_blurb (pspec));
}