1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
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  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * 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 #include "config.h"
19 
20 #include <string.h>
21 
22 #include <gio/gio.h>
23 
24 #include "libgimpbase/gimpbase.h"
25 
26 #include "core-types.h"
27 
28 #include "gimpparamspecs.h"
29 #include "gimpparamspecs-desc.h"
30 
31 
32 static inline const gchar *
gimp_param_spec_get_blurb(GParamSpec * pspec)33 gimp_param_spec_get_blurb (GParamSpec *pspec)
34 {
35   const gchar *blurb = g_param_spec_get_blurb (pspec);
36 
37   return blurb ? blurb : "";
38 }
39 
40 static gchar *
gimp_param_spec_boolean_desc(GParamSpec * pspec)41 gimp_param_spec_boolean_desc (GParamSpec *pspec)
42 {
43   const gchar *blurb = gimp_param_spec_get_blurb (pspec);
44 
45   return g_strconcat (blurb, " (TRUE or FALSE)", NULL);
46 }
47 
48 static gchar *
gimp_param_spec_int32_desc(GParamSpec * pspec)49 gimp_param_spec_int32_desc (GParamSpec *pspec)
50 {
51   GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
52   const gchar   *blurb = gimp_param_spec_get_blurb (pspec);
53 
54   if (ispec->minimum == G_MININT32 && ispec->maximum == G_MAXINT32)
55     return g_strdup (blurb);
56 
57   if (ispec->minimum == G_MININT32)
58     return g_strdup_printf ("%s (%s <= %d)", blurb,
59                             g_param_spec_get_name (pspec),
60                             ispec->maximum);
61 
62   if (ispec->maximum == G_MAXINT32)
63     return g_strdup_printf ("%s (%s >= %d)", blurb,
64                             g_param_spec_get_name (pspec),
65                             ispec->minimum);
66 
67   return g_strdup_printf ("%s (%d <= %s <= %d)", blurb,
68                           ispec->minimum,
69                           g_param_spec_get_name (pspec),
70                           ispec->maximum);
71 }
72 
73 static gchar *
gimp_param_spec_double_desc(GParamSpec * pspec)74 gimp_param_spec_double_desc (GParamSpec *pspec)
75 {
76   GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (pspec);
77   const gchar      *blurb = gimp_param_spec_get_blurb (pspec);
78 
79   if (dspec->minimum == - G_MAXDOUBLE && dspec->maximum == G_MAXDOUBLE)
80     return g_strdup (blurb);
81 
82   if (dspec->minimum == - G_MAXDOUBLE)
83     return g_strdup_printf ("%s (%s <= %g)", blurb,
84                             g_param_spec_get_name (pspec),
85                             dspec->maximum);
86 
87   if (dspec->maximum == G_MAXDOUBLE)
88     return g_strdup_printf ("%s (%s >= %g)", blurb,
89                             g_param_spec_get_name (pspec),
90                             dspec->minimum);
91 
92   return g_strdup_printf ("%s (%g <= %s <= %g)", blurb,
93                           dspec->minimum,
94                           g_param_spec_get_name (pspec),
95                           dspec->maximum);
96 }
97 
98 static gchar *
gimp_param_spec_enum_desc(GParamSpec * pspec)99 gimp_param_spec_enum_desc (GParamSpec *pspec)
100 {
101   const gchar    *blurb      = gimp_param_spec_get_blurb (pspec);
102   GString        *str        = g_string_new (blurb);
103   GEnumClass     *enum_class = g_type_class_peek (pspec->value_type);
104   GEnumValue     *enum_value;
105   GSList         *excluded;
106   gint            i, n;
107 
108   if (GIMP_IS_PARAM_SPEC_ENUM (pspec))
109     excluded = GIMP_PARAM_SPEC_ENUM (pspec)->excluded_values;
110   else
111     excluded = NULL;
112 
113   g_string_append (str, " { ");
114 
115   for (i = 0, n = 0, enum_value = enum_class->values;
116        i < enum_class->n_values;
117        i++, enum_value++)
118     {
119       GSList *list;
120       gchar  *name;
121 
122       for (list = excluded; list; list = list->next)
123         {
124           gint value = GPOINTER_TO_INT (list->data);
125 
126           if (value == enum_value->value)
127             break;
128         }
129 
130       if (list)
131         continue;
132 
133       if (n > 0)
134         g_string_append (str, ", ");
135 
136       if (G_LIKELY (g_str_has_prefix (enum_value->value_name, "GIMP_")))
137         name = gimp_canonicalize_identifier (enum_value->value_name + 5);
138       else
139         name = gimp_canonicalize_identifier (enum_value->value_name);
140 
141       g_string_append (str, name);
142       g_free (name);
143 
144       g_string_append_printf (str, " (%d)", enum_value->value);
145 
146       n++;
147     }
148 
149   g_string_append (str, " }");
150 
151   return g_string_free (str, FALSE);
152 }
153 
154 /**
155  * gimp_param_spec_get_desc:
156  * @pspec: a #GParamSpec
157  *
158  * This function creates a description of the passed @pspec, which is
159  * suitable for use in the PDB.  Actually, it currently only deals with
160  * parameter types used in the PDB and should not be used for anything
161  * else.
162  *
163  * Return value: A newly allocated string describing the parameter.
164  */
165 gchar *
gimp_param_spec_get_desc(GParamSpec * pspec)166 gimp_param_spec_get_desc (GParamSpec *pspec)
167 {
168   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
169 
170   if (GIMP_IS_PARAM_SPEC_UNIT (pspec))
171     {
172     }
173   else if (GIMP_IS_PARAM_SPEC_INT32 (pspec))
174     {
175       return gimp_param_spec_int32_desc (pspec);
176     }
177   else
178     {
179       switch (G_TYPE_FUNDAMENTAL (pspec->value_type))
180         {
181         case G_TYPE_BOOLEAN:
182           return gimp_param_spec_boolean_desc (pspec);
183 
184         case G_TYPE_DOUBLE:
185           return gimp_param_spec_double_desc (pspec);
186 
187         case G_TYPE_ENUM:
188           return gimp_param_spec_enum_desc (pspec);
189         }
190     }
191 
192   return g_strdup (g_param_spec_get_blurb (pspec));
193 }
194