add new functions script_fu_script_collect_standard_args(),

2008-09-18  Michael Natterer  <mitch@gimp.org>

	* plug-ins/script-fu/script-fu-script.[ch]: add new functions
	script_fu_script_collect_standard_args(),
	script_fu_script_get_command() and
	script_fu_script_get_command_from_params().

	* plug-ins/script-fu/script-fu-scripts.c: remove the resp. code
	here and use above functions instead.

	* plug-ins/script-fu/script-fu-interface.c (script_fu_ok): changed
	loop over args to only copy all widget content into the script's
	values and then call script_fu_script_get_command().


svn path=/trunk/; revision=26993
This commit is contained in:
Michael Natterer 2008-09-18 13:10:07 +00:00 committed by Michael Natterer
parent 651cb5b325
commit 3b2661bd2b
5 changed files with 398 additions and 405 deletions

View File

@ -1,3 +1,17 @@
2008-09-18 Michael Natterer <mitch@gimp.org>
* plug-ins/script-fu/script-fu-script.[ch]: add new functions
script_fu_script_collect_standard_args(),
script_fu_script_get_command() and
script_fu_script_get_command_from_params().
* plug-ins/script-fu/script-fu-scripts.c: remove the resp. code
here and use above functions instead.
* plug-ins/script-fu/script-fu-interface.c (script_fu_ok): changed
loop over args to only copy all widget content into the script's
values and then call script_fu_script_get_command().
2008-09-18 Michael Natterer <mitch@gimp.org>
* app/widgets/gimpdock.c (gimp_dock_style_set): reset the RC

View File

@ -30,6 +30,7 @@
#include "script-fu-interface.h"
#include "script-fu-scripts.h"
#include "script-fu-script.h"
#include "script-fu-intl.h"
@ -770,22 +771,15 @@ script_fu_response (GtkWidget *widget,
static void
script_fu_ok (SFScript *script)
{
gchar *escaped;
GString *s, *output;
GString *output;
gchar *command;
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
gint i;
s = g_string_new ("(");
g_string_append (s, script->name);
for (i = 0; i < script->n_args; i++)
{
SFArgValue *arg_value = &script->args[i].value;
GtkWidget *widget = sf_interface->widgets[i];
g_string_append_c (s, ' ');
switch (script->args[i].type)
{
case SF_IMAGE:
@ -794,39 +788,15 @@ script_fu_ok (SFScript *script)
case SF_CHANNEL:
case SF_VECTORS:
case SF_DISPLAY:
g_string_append_printf (s, "%d", arg_value->sfa_image);
break;
case SF_COLOR:
{
guchar r, g, b;
gimp_rgb_get_uchar (&arg_value->sfa_color, &r, &g, &b);
g_string_append_printf (s, "'(%d %d %d)",
(gint) r, (gint) g, (gint) b);
}
break;
case SF_TOGGLE:
g_string_append (s, arg_value->sfa_toggle ? "TRUE" : "FALSE");
break;
case SF_VALUE:
g_free (arg_value->sfa_value);
arg_value->sfa_value =
g_strdup (gtk_entry_get_text (GTK_ENTRY (widget)));
g_string_append (s, arg_value->sfa_value);
break;
case SF_STRING:
g_free (arg_value->sfa_value);
arg_value->sfa_value =
g_strdup (gtk_entry_get_text (GTK_ENTRY (widget)));
escaped = script_fu_strescape (arg_value->sfa_value);
g_string_append_printf (s, "\"%s\"", escaped);
g_free (escaped);
break;
case SF_TEXT:
@ -845,66 +815,24 @@ script_fu_ok (SFScript *script)
arg_value->sfa_value = gtk_text_buffer_get_text (buffer,
&start, &end,
FALSE);
escaped = script_fu_strescape (arg_value->sfa_value);
g_string_append_printf (s, "\"%s\"", escaped);
g_free (escaped);
}
break;
case SF_ADJUSTMENT:
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_adjustment.value);
g_string_append (s, buffer);
break;
case SF_FILENAME:
case SF_DIRNAME:
escaped = script_fu_strescape (arg_value->sfa_file.filename);
g_string_append_printf (s, "\"%s\"", escaped);
g_free (escaped);
break;
case SF_FONT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_font);
break;
case SF_PALETTE:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_palette);
break;
case SF_PATTERN:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_pattern);
break;
case SF_GRADIENT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_gradient);
break;
case SF_BRUSH:
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_brush.opacity);
g_string_append_printf (s, "'(\"%s\" %s %d %d)",
arg_value->sfa_brush.name,
buffer,
arg_value->sfa_brush.spacing,
arg_value->sfa_brush.paint_mode);
break;
case SF_OPTION:
g_string_append_printf (s, "%d", arg_value->sfa_option.history);
break;
case SF_ENUM:
g_string_append_printf (s, "%d", arg_value->sfa_enum.history);
break;
}
}
g_string_append_c (s, ')');
command = g_string_free (s, FALSE);
command = script_fu_script_get_command (script);
/* run the command through the interpreter */
output = g_string_new (NULL);

View File

@ -23,9 +23,23 @@
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "tinyscheme/scheme-private.h"
#include "script-fu-types.h"
#include "script-fu-script.h"
#include "script-fu-scripts.h"
/*
* Local Functions
*/
static gboolean script_fu_script_param_init (SFScript *script,
gint nparams,
const GimpParam *params,
SFArgType type,
gint n);
/*
@ -309,3 +323,336 @@ script_fu_script_uninstall_proc (SFScript *script)
gimp_uninstall_temp_proc (script->name);
}
gint
script_fu_script_collect_standard_args (SFScript *script,
gint n_params,
const GimpParam *params)
{
gint params_consumed = 0;
g_return_val_if_fail (script != NULL, 0);
/* the first parameter may be a DISPLAY id */
if (script_fu_script_param_init (script,
n_params, params, SF_DISPLAY,
params_consumed))
{
params_consumed++;
}
/* an IMAGE id may come first or after the DISPLAY id */
if (script_fu_script_param_init (script,
n_params, params, SF_IMAGE,
params_consumed))
{
params_consumed++;
/* and may be followed by a DRAWABLE, LAYER, CHANNEL or
* VECTORS id
*/
if (script_fu_script_param_init (script,
n_params, params, SF_DRAWABLE,
params_consumed) ||
script_fu_script_param_init (script,
n_params, params, SF_LAYER,
params_consumed) ||
script_fu_script_param_init (script,
n_params, params, SF_CHANNEL,
params_consumed) ||
script_fu_script_param_init (script,
n_params, params, SF_VECTORS,
params_consumed))
{
params_consumed++;
}
}
return params_consumed;
}
gchar *
script_fu_script_get_command (SFScript *script)
{
GString *s;
gint i;
g_return_val_if_fail (script != NULL, NULL);
s = g_string_new ("(");
g_string_append (s, script->name);
for (i = 0; i < script->n_args; i++)
{
SFArgValue *arg_value = &script->args[i].value;
g_string_append_c (s, ' ');
switch (script->args[i].type)
{
case SF_IMAGE:
case SF_DRAWABLE:
case SF_LAYER:
case SF_CHANNEL:
case SF_VECTORS:
case SF_DISPLAY:
g_string_append_printf (s, "%d", arg_value->sfa_image);
break;
case SF_COLOR:
{
guchar r, g, b;
gimp_rgb_get_uchar (&arg_value->sfa_color, &r, &g, &b);
g_string_append_printf (s, "'(%d %d %d)",
(gint) r, (gint) g, (gint) b);
}
break;
case SF_TOGGLE:
g_string_append (s, arg_value->sfa_toggle ? "TRUE" : "FALSE");
break;
case SF_VALUE:
g_string_append (s, arg_value->sfa_value);
break;
case SF_STRING:
case SF_TEXT:
{
gchar *tmp;
tmp = script_fu_strescape (arg_value->sfa_value);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_ADJUSTMENT:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_adjustment.value);
g_string_append (s, buffer);
}
break;
case SF_FILENAME:
case SF_DIRNAME:
{
gchar *tmp;
tmp = script_fu_strescape (arg_value->sfa_file.filename);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_FONT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_font);
break;
case SF_PALETTE:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_palette);
break;
case SF_PATTERN:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_pattern);
break;
case SF_GRADIENT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_gradient);
break;
case SF_BRUSH:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_brush.opacity);
g_string_append_printf (s, "'(\"%s\" %s %d %d)",
arg_value->sfa_brush.name,
buffer,
arg_value->sfa_brush.spacing,
arg_value->sfa_brush.paint_mode);
}
break;
case SF_OPTION:
g_string_append_printf (s, "%d", arg_value->sfa_option.history);
break;
case SF_ENUM:
g_string_append_printf (s, "%d", arg_value->sfa_enum.history);
break;
}
}
g_string_append_c (s, ')');
return g_string_free (s, FALSE);
}
gchar *
script_fu_script_get_command_from_params (SFScript *script,
const GimpParam *params)
{
GString *s;
gint i;
g_return_val_if_fail (script != NULL, NULL);
s = g_string_new ("(");
g_string_append (s, script->name);
for (i = 0; i < script->n_args; i++)
{
const GimpParam *param = &params[i + 1];
g_string_append_c (s, ' ');
switch (script->args[i].type)
{
case SF_IMAGE:
case SF_DRAWABLE:
case SF_LAYER:
case SF_CHANNEL:
case SF_VECTORS:
case SF_DISPLAY:
g_string_append_printf (s, "%d", param->data.d_int32);
break;
case SF_COLOR:
{
guchar r, g, b;
gimp_rgb_get_uchar (&param->data.d_color, &r, &g, &b);
g_string_append_printf (s, "'(%d %d %d)",
(gint) r, (gint) g, (gint) b);
}
break;
case SF_TOGGLE:
g_string_append_printf (s, (param->data.d_int32 ? "TRUE" : "FALSE"));
break;
case SF_VALUE:
g_string_append (s, param->data.d_string);
break;
case SF_STRING:
case SF_TEXT:
case SF_FILENAME:
case SF_DIRNAME:
{
gchar *tmp;
tmp = script_fu_strescape (param->data.d_string);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_ADJUSTMENT:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer), param->data.d_float);
g_string_append (s, buffer);
}
break;
case SF_FONT:
case SF_PALETTE:
case SF_PATTERN:
case SF_GRADIENT:
case SF_BRUSH:
g_string_append_printf (s, "\"%s\"", param->data.d_string);
break;
case SF_OPTION:
case SF_ENUM:
g_string_append_printf (s, "%d", param->data.d_int32);
break;
}
}
g_string_append_c (s, ')');
return g_string_free (s, FALSE);
}
/*
* Local Functions
*/
static gboolean
script_fu_script_param_init (SFScript *script,
gint nparams,
const GimpParam *params,
SFArgType type,
gint n)
{
SFArg *arg = &script->args[n];
if (script->n_args > n && arg->type == type && nparams > n + 1)
{
switch (type)
{
case SF_IMAGE:
if (params[n + 1].type == GIMP_PDB_IMAGE)
{
arg->value.sfa_image = params[n + 1].data.d_image;
return TRUE;
}
break;
case SF_DRAWABLE:
if (params[n + 1].type == GIMP_PDB_DRAWABLE)
{
arg->value.sfa_drawable = params[n + 1].data.d_drawable;
return TRUE;
}
break;
case SF_LAYER:
if (params[n + 1].type == GIMP_PDB_LAYER)
{
arg->value.sfa_layer = params[n + 1].data.d_layer;
return TRUE;
}
break;
case SF_CHANNEL:
if (params[n + 1].type == GIMP_PDB_CHANNEL)
{
arg->value.sfa_channel = params[n + 1].data.d_channel;
return TRUE;
}
break;
case SF_VECTORS:
if (params[n + 1].type == GIMP_PDB_VECTORS)
{
arg->value.sfa_vectors = params[n + 1].data.d_vectors;
return TRUE;
}
break;
case SF_DISPLAY:
if (params[n + 1].type == GIMP_PDB_DISPLAY)
{
arg->value.sfa_display = params[n + 1].data.d_display;
return TRUE;
}
break;
default:
break;
}
}
return FALSE;
}

View File

@ -20,19 +20,27 @@
#define __SCRIPT_FU_SCRIPT_H__
SFScript * script_fu_script_new (const gchar *name,
const gchar *menu_path,
const gchar *blurb,
const gchar *author,
const gchar *copyright,
const gchar *date,
const gchar *image_types,
gint n_args);
void script_fu_script_free (SFScript *script);
SFScript * script_fu_script_new (const gchar *name,
const gchar *menu_path,
const gchar *blurb,
const gchar *author,
const gchar *copyright,
const gchar *date,
const gchar *image_types,
gint n_args);
void script_fu_script_free (SFScript *script);
void script_fu_script_install_proc (SFScript *script,
GimpRunProc run_proc);
void script_fu_script_uninstall_proc (SFScript *script);
void script_fu_script_install_proc (SFScript *script,
GimpRunProc run_proc);
void script_fu_script_uninstall_proc (SFScript *script);
gint script_fu_script_collect_standard_args (SFScript *script,
gint n_params,
const GimpParam *params);
gchar * script_fu_script_get_command (SFScript *script);
gchar * script_fu_script_get_command_from_params (SFScript *script,
const GimpParam *params);
#endif /* __SCRIPT_FU_SCRIPT__ */

View File

@ -650,8 +650,12 @@ script_fu_install_script (gpointer foo G_GNUC_UNUSED,
GList *list;
for (list = scripts; list; list = g_list_next (list))
script_fu_script_install_proc (list->data,
script_fu_script_proc);
{
SFScript *script = list->data;
script_fu_script_install_proc (script,
script_fu_script_proc);
}
return FALSE;
}
@ -688,119 +692,6 @@ script_fu_remove_script (gpointer foo G_GNUC_UNUSED,
return FALSE;
}
static gboolean
script_fu_param_init (SFScript *script,
gint nparams,
const GimpParam *params,
SFArgType type,
gint n)
{
SFArg *arg = &script->args[n];
if (script->n_args > n && arg->type == type && nparams > n + 1)
{
switch (type)
{
case SF_IMAGE:
if (params[n + 1].type == GIMP_PDB_IMAGE)
{
arg->value.sfa_image = params[n + 1].data.d_image;
return TRUE;
}
break;
case SF_DRAWABLE:
if (params[n + 1].type == GIMP_PDB_DRAWABLE)
{
arg->value.sfa_drawable = params[n + 1].data.d_drawable;
return TRUE;
}
break;
case SF_LAYER:
if (params[n + 1].type == GIMP_PDB_LAYER)
{
arg->value.sfa_layer = params[n + 1].data.d_layer;
return TRUE;
}
break;
case SF_CHANNEL:
if (params[n + 1].type == GIMP_PDB_CHANNEL)
{
arg->value.sfa_channel = params[n + 1].data.d_channel;
return TRUE;
}
break;
case SF_VECTORS:
if (params[n + 1].type == GIMP_PDB_VECTORS)
{
arg->value.sfa_vectors = params[n + 1].data.d_vectors;
return TRUE;
}
break;
case SF_DISPLAY:
if (params[n + 1].type == GIMP_PDB_DISPLAY)
{
arg->value.sfa_display = params[n + 1].data.d_display;
return TRUE;
}
break;
default:
break;
}
}
return FALSE;
}
static gint
script_fu_collect_standard_args (SFScript *script,
gint nparams,
const GimpParam *params)
{
gint params_consumed = 0;
/* the first parameter may be a DISPLAY id */
if (script_fu_param_init (script,
nparams, params, SF_DISPLAY, params_consumed))
{
params_consumed++;
}
/* an IMAGE id may come first or after the DISPLAY id */
if (script_fu_param_init (script,
nparams, params, SF_IMAGE, params_consumed))
{
params_consumed++;
/* and may be followed by a DRAWABLE, LAYER, CHANNEL or
* VECTORS id
*/
if (script_fu_param_init (script,
nparams, params, SF_DRAWABLE,
params_consumed) ||
script_fu_param_init (script,
nparams, params, SF_LAYER,
params_consumed) ||
script_fu_param_init (script,
nparams, params, SF_CHANNEL,
params_consumed) ||
script_fu_param_init (script,
nparams, params, SF_VECTORS,
params_consumed))
{
params_consumed++;
}
}
return params_consumed;
}
static void
script_fu_script_proc (const gchar *name,
gint nparams,
@ -836,8 +727,8 @@ script_fu_script_proc (const gchar *name,
gint min_args = 0;
/* First, try to collect the standard script arguments... */
min_args = script_fu_collect_standard_args (script,
nparams, params);
min_args = script_fu_script_collect_standard_args (script,
nparams, params);
/* ...then acquire the rest of arguments (if any) with a dialog */
if (script->n_args > min_args)
@ -857,91 +748,10 @@ script_fu_script_proc (const gchar *name,
if (status == GIMP_PDB_SUCCESS)
{
GString *s;
gchar *command;
gint i;
gchar *command;
s = g_string_new ("(");
g_string_append (s, script->name);
for (i = 0; i < script->n_args; i++)
{
const GimpParam *param = &params[i + 1];
g_string_append_c (s, ' ');
switch (script->args[i].type)
{
case SF_IMAGE:
case SF_DRAWABLE:
case SF_LAYER:
case SF_CHANNEL:
case SF_VECTORS:
case SF_DISPLAY:
g_string_append_printf (s, "%d", param->data.d_int32);
break;
case SF_COLOR:
{
guchar r, g, b;
gimp_rgb_get_uchar (&param->data.d_color, &r, &g, &b);
g_string_append_printf (s, "'(%d %d %d)",
(gint) r, (gint) g, (gint) b);
}
break;
case SF_TOGGLE:
g_string_append_printf (s, (param->data.d_int32 ?
"TRUE" : "FALSE"));
break;
case SF_VALUE:
g_string_append (s, param->data.d_string);
break;
case SF_STRING:
case SF_TEXT:
case SF_FILENAME:
case SF_DIRNAME:
{
gchar *tmp;
tmp = script_fu_strescape (param->data.d_string);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_ADJUSTMENT:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer),
param->data.d_float);
g_string_append (s, buffer);
}
break;
case SF_FONT:
case SF_PALETTE:
case SF_PATTERN:
case SF_GRADIENT:
case SF_BRUSH:
g_string_append_printf (s, "\"%s\"",
param->data.d_string);
break;
case SF_OPTION:
case SF_ENUM:
g_string_append_printf (s, "%d", param->data.d_int32);
break;
}
}
g_string_append_c (s, ')');
command = g_string_free (s, FALSE);
command = script_fu_script_get_command_from_params (script,
params);
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))
@ -957,126 +767,12 @@ script_fu_script_proc (const gchar *name,
case GIMP_RUN_WITH_LAST_VALS:
{
GString *s;
gchar *command;
gint i;
gchar *command;
/* First, try to collect the standard script arguments */
script_fu_collect_standard_args (script, nparams, params);
script_fu_script_collect_standard_args (script, nparams, params);
s = g_string_new ("(");
g_string_append (s, script->name);
for (i = 0; i < script->n_args; i++)
{
SFArgValue *arg_value = &script->args[i].value;
g_string_append_c (s, ' ');
switch (script->args[i].type)
{
case SF_IMAGE:
case SF_DRAWABLE:
case SF_LAYER:
case SF_CHANNEL:
case SF_VECTORS:
case SF_DISPLAY:
g_string_append_printf (s, "%d", arg_value->sfa_image);
break;
case SF_COLOR:
{
guchar r, g, b;
gimp_rgb_get_uchar (&arg_value->sfa_color, &r, &g, &b);
g_string_append_printf (s, "'(%d %d %d)",
(gint) r, (gint) g, (gint) b);
}
break;
case SF_TOGGLE:
g_string_append (s, arg_value->sfa_toggle ? "TRUE" : "FALSE");
break;
case SF_VALUE:
g_string_append (s, arg_value->sfa_value);
break;
case SF_STRING:
case SF_TEXT:
{
gchar *tmp;
tmp = script_fu_strescape (arg_value->sfa_value);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_ADJUSTMENT:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_adjustment.value);
g_string_append (s, buffer);
}
break;
case SF_FILENAME:
case SF_DIRNAME:
{
gchar *tmp;
tmp = script_fu_strescape (arg_value->sfa_file.filename);
g_string_append_printf (s, "\"%s\"", tmp);
g_free (tmp);
}
break;
case SF_FONT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_font);
break;
case SF_PALETTE:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_palette);
break;
case SF_PATTERN:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_pattern);
break;
case SF_GRADIENT:
g_string_append_printf (s, "\"%s\"", arg_value->sfa_gradient);
break;
case SF_BRUSH:
{
gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buffer, sizeof (buffer),
arg_value->sfa_brush.opacity);
g_string_append_printf (s, "'(\"%s\" %s %d %d)",
arg_value->sfa_brush.name,
buffer,
arg_value->sfa_brush.spacing,
arg_value->sfa_brush.paint_mode);
}
break;
case SF_OPTION:
g_string_append_printf (s, "%d", arg_value->sfa_option.history);
break;
case SF_ENUM:
g_string_append_printf (s, "%d", arg_value->sfa_enum.history);
break;
}
}
g_string_append_c (s, ')');
command = g_string_free (s, FALSE);
command = script_fu_script_get_command (script);
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))