gimpparamspecs-desc.c 5.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20 21
#include <string.h>

22
#include <gio/gio.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#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);
}

static gchar *
gimp_param_spec_int32_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;
120
      gchar  *name;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

      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, ", ");

136
      if (G_LIKELY (g_str_has_prefix (enum_value->value_name, "GIMP_")))
137 138 139 140 141 142
        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);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

      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.
 *
 * Return value: 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))
    {
    }
  else if (GIMP_IS_PARAM_SPEC_INT32 (pspec))
    {
      return gimp_param_spec_int32_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));
}