diff --git a/libgimp/gimp.def b/libgimp/gimp.def index aa51b448c8..976df353b8 100644 --- a/libgimp/gimp.def +++ b/libgimp/gimp.def @@ -728,6 +728,7 @@ EXPORTS gimp_procedure_config_begin_run gimp_procedure_config_end_export gimp_procedure_config_end_run + gimp_procedure_config_get_choice_id gimp_procedure_config_get_procedure gimp_procedure_config_get_type gimp_procedure_config_get_values diff --git a/libgimp/gimpprocedureconfig.c b/libgimp/gimpprocedureconfig.c index 9d7aed98a4..e8fc399ea0 100644 --- a/libgimp/gimpprocedureconfig.c +++ b/libgimp/gimpprocedureconfig.c @@ -897,6 +897,59 @@ gimp_procedure_config_save_metadata (GimpProcedureConfig *config, } } +/** + * gimp_procedure_config_get_choice_id: + * @config: a #GimpProcedureConfig + * @property_name: the name of a [struct@ParamSpecChoice] property. + * + * A utility function which will get the current string value of a + * [struct@ParamSpecChoice] property in @config and convert it to the integer ID + * mapped to this value. + * This makes it easy to work with an Enum type locally, within a plug-in code. + * + * Since: 3.0 + **/ +gint +gimp_procedure_config_get_choice_id (GimpProcedureConfig *config, + const gchar *property_name) +{ + GParamSpec *param_spec; + GimpParamSpecChoice *cspec; + gchar *value = NULL; + gint id; + + param_spec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), + property_name); + + if (! param_spec) + { + g_warning ("%s: %s has no property named '%s'", + G_STRFUNC, + g_type_name (G_TYPE_FROM_INSTANCE (config)), + property_name); + return 0; + } + + if (! g_type_is_a (G_TYPE_FROM_INSTANCE (param_spec), GIMP_TYPE_PARAM_CHOICE)) + { + g_warning ("%s: property '%s' of %s is not a GimpParamSpecChoice.", + G_STRFUNC, + param_spec->name, + g_type_name (param_spec->owner_type)); + return 0; + } + + cspec = GIMP_PARAM_SPEC_CHOICE (param_spec); + g_object_get (config, + property_name, &value, + NULL); + id = gimp_choice_get_id (cspec->choice, value); + + g_free (value); + + return id; +} + /* private functions */ diff --git a/libgimp/gimpprocedureconfig.h b/libgimp/gimpprocedureconfig.h index 8114debb43..fbf90e691f 100644 --- a/libgimp/gimpprocedureconfig.h +++ b/libgimp/gimpprocedureconfig.h @@ -98,6 +98,12 @@ void gimp_procedure_config_save_metadata (GimpProcedureConfig *config, GFile *file); +/* Utility functions */ + +gint gimp_procedure_config_get_choice_id (GimpProcedureConfig *config, + const gchar *property_name); + + G_END_DECLS #endif /* __GIMP_PROCEDURE_CONFIG_H__ */