script-fu-scripts.c 56.1 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an 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 2 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, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
18
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
19
20
#include "config.h"

Tor Lillqvist's avatar
Tor Lillqvist committed
21
22
#include <glib.h>		/* Include early for obscure Win32
				   build reasons */
Elliot Lee's avatar
Elliot Lee committed
23
24
25
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
26
#include <sys/types.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
27

28
#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
29

30
31
32
33
34
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

35
36
#include <libgimpbase/gimpbase.h>

37
38
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
39

Elliot Lee's avatar
Elliot Lee committed
40
41
42
#include "siod.h"
#include "script-fu-scripts.h"

Sven Neumann's avatar
Sven Neumann committed
43
#include "script-fu-intl.h"
44

Tor Lillqvist's avatar
Tor Lillqvist committed
45

46
47
48
#define TEXT_WIDTH           100
#define COLOR_SAMPLE_WIDTH   100
#define COLOR_SAMPLE_HEIGHT   15
49
#define SLIDER_WIDTH          80
Elliot Lee's avatar
Elliot Lee committed
50

51
#define MAX_STRING_LENGTH   4096
52

Elliot Lee's avatar
Elliot Lee committed
53

54
55
typedef struct
{
56
  GtkAdjustment    *adj;
57
58
59
60
61
  gdouble           value;
  gdouble           lower;
  gdouble           upper;
  gdouble           step;
  gdouble           page;
62
63
  gint              digits;
  SFAdjustmentType  type;
64
65
} SFAdjustment;

Manish Singh's avatar
Manish Singh committed
66
67
typedef struct
{
68
  GtkWidget *fileselection;
Manish Singh's avatar
Manish Singh committed
69
70
71
  gchar     *filename;
} SFFilename;

72
73
typedef struct 
{
74
75
76
77
  gchar                *name;
  gdouble               opacity;
  gint                  spacing;
  GimpLayerModeEffects  paint_mode;
78
79
} SFBrush;

80
81
82
83
84
85
typedef struct 
{
  GSList  *list;
  guint    history;
} SFOption;

Elliot Lee's avatar
Elliot Lee committed
86
87
typedef union
{
88
89
90
91
  gint32         sfa_image;
  gint32         sfa_drawable;
  gint32         sfa_layer;
  gint32         sfa_channel;
92
  GimpRGB        sfa_color;
93
94
95
96
  gint32         sfa_toggle;
  gchar         *sfa_value;
  SFAdjustment   sfa_adjustment;
  SFFilename     sfa_file;
97
  gchar         *sfa_font;
98
  gchar         *sfa_gradient;
99
  gchar         *sfa_pattern;
100
101
  SFBrush        sfa_brush;
  SFOption       sfa_option;
Elliot Lee's avatar
Elliot Lee committed
102
103
104
105
} SFArgValue;

typedef struct
{
106
107
108
109
110
111
112
113
114
115
116
117
118
  gchar         *script_name;
  gchar         *pdb_name;
  gchar         *description;
  gchar         *help;
  gchar         *author;
  gchar         *copyright;
  gchar         *date;
  gchar         *img_types;
  gint           num_args;
  SFArgType     *arg_types;
  gchar        **arg_labels;
  SFArgValue    *arg_defaults;
  SFArgValue    *arg_values;
119
  gboolean       image_based;
120
  GimpParamDef  *args;     /*  used only temporary until installed  */
Elliot Lee's avatar
Elliot Lee committed
121
122
123
124
} SFScript;

typedef struct
{
125
  GtkWidget     *dialog;
126
127
128
  GtkWidget    **args_widgets;
  GtkWidget     *status;
  GtkWidget     *about_dialog;
129
  gchar         *title;
130
131
132
  gchar         *last_command;
  gint           command_count;
  gint           consec_command_count;
Elliot Lee's avatar
Elliot Lee committed
133
134
} SFInterface;

135

Elliot Lee's avatar
Elliot Lee committed
136
137
/* External functions
 */
138
extern long  nlength (LISP obj);
Elliot Lee's avatar
Elliot Lee committed
139
140
141
142
143

/*
 *  Local Functions
 */

144
static void       script_fu_load_script      (GimpDatafileData *file_data);
145
static gboolean   script_fu_install_script   (gpointer    foo,
146
147
					      SFScript   *script,
					      gpointer    bar);
148
static gboolean   script_fu_remove_script    (gpointer    foo,
149
150
151
152
153
154
155
156
157
158
159
					      SFScript   *script,
					      gpointer    bar);
static void       script_fu_script_proc      (gchar      *name,
					      gint        nparams,
					      GimpParam  *params,
					      gint       *nreturn_vals,
					      GimpParam **return_vals);

static SFScript * script_fu_find_script      (gchar      *script_name);
static void       script_fu_free_script      (SFScript   *script);
static void       script_fu_interface        (SFScript   *script);
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static void       script_fu_interface_quit   (SFScript   *script);
static void       script_fu_error_msg        (gchar      *command);

static void       script_fu_ok_callback             (GtkWidget *widget,
						     gpointer   data);
static void       script_fu_about_callback          (GtkWidget *widget,
						     gpointer   data);
static void       script_fu_reset_callback          (GtkWidget *widget,
						     gpointer   data);
static void       script_fu_menu_callback           (gint32     id,
						     gpointer   data);

static void       script_fu_file_selection_callback (GtkWidget *widget,
						     gpointer   data);

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
static void       script_fu_pattern_preview         (const gchar   *name,
						     gint           width,
						     gint           height,
						     gint           bytes,
						     const guchar  *mask_data,
						     gboolean       closing,
						     gpointer       data);
static void       script_fu_gradient_preview        (const gchar   *name,
						     gint           width,
						     const gdouble *mask_data,
						     gboolean       closing,
						     gpointer       data);
static void       script_fu_font_preview            (const gchar   *name,
                                                     gboolean       closing,
                                                     gpointer       data);
static void       script_fu_brush_preview           (const gchar   *name,
						     gdouble        opacity,
						     gint           spacing,
						     GimpLayerModeEffects  paint_mode,
						     gint           width,
						     gint           height,
						     const guchar  *mask_data,
						     gboolean       closing,
						     gpointer       data);
199

Elliot Lee's avatar
Elliot Lee committed
200
201
202
203
204

/*
 *  Local variables
 */

205
static GTree       *script_list  = NULL;
206
207
208
static SFInterface *sf_interface = NULL;  /*  there can only be at most one
					      interactive interface  */
					      
209
extern gchar        siod_err_msg[];
Elliot Lee's avatar
Elliot Lee committed
210
211
212
213
214
215

/*
 *  Function definitions
 */

void
216
script_fu_find_scripts (void)
Elliot Lee's avatar
Elliot Lee committed
217
{
218
  gchar *path_str;
Elliot Lee's avatar
Elliot Lee committed
219
220
221
222

  /*  Make sure to clear any existing scripts  */
  if (script_list != NULL)
    {
223
224
225
      g_tree_foreach (script_list, 
                      (GTraverseFunc) script_fu_remove_script, 
                      NULL);
226
      g_tree_destroy (script_list);
Elliot Lee's avatar
Elliot Lee committed
227
228
    }

229
  script_list = g_tree_new ((GCompareFunc) strcoll);
230

231
  path_str = gimp_gimprc_query ("script-fu-path");
Elliot Lee's avatar
Elliot Lee committed
232

233
234
  if (path_str == NULL)
    return;
Elliot Lee's avatar
Elliot Lee committed
235

236
237
238
  gimp_datafiles_read_directories (path_str, G_FILE_TEST_IS_REGULAR,
                                   script_fu_load_script,
                                   NULL);
239

240
  g_free (path_str);
241
242

  /*  now that all scripts are read in and sorted, tell gimp about them  */
243
244
245
  g_tree_foreach (script_list, 
                  (GTraverseFunc) script_fu_install_script, 
                  NULL);
Elliot Lee's avatar
Elliot Lee committed
246
247
248
249
250
}

LISP
script_fu_add_script (LISP a)
{
251
  GimpParamDef *args;
252
253
254
  SFScript     *script;
  gchar        *val;
  gint          i;
255
  guchar        r, g, b;
256
257
258
259
260
  LISP          color_list;
  LISP          adj_list;
  LISP          brush_list;
  LISP          option_list;
  gchar        *s;
Elliot Lee's avatar
Elliot Lee committed
261
262
263

  /*  Check the length of a  */
  if (nlength (a) < 7)
264
    return my_err ("Too few arguments to script-fu-register", NIL);
Elliot Lee's avatar
Elliot Lee committed
265
266

  /*  Create a new script  */
267
  script = g_new0 (SFScript, 1);
Elliot Lee's avatar
Elliot Lee committed
268
269
270
271
272
273

  /*  Find the script name  */
  val = get_c_string (car (a));
  script->script_name = g_strdup (val);
  a = cdr (a);

Marc Lehmann's avatar
Marc Lehmann committed
274
275
276
277
278
279
280
281
282
  /* transform the function name into a name containing "_" for each "-".
   * this does not hurt anybody, yet improves the life of many... ;)
   */
  script->pdb_name = g_strdup (val);

  for (s = script->pdb_name; *s; s++)
    if (*s == '-')
      *s = '_';

Elliot Lee's avatar
Elliot Lee committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  /*  Find the script description  */
  val = get_c_string (car (a));
  script->description = g_strdup (val);
  a = cdr (a);

  /*  Find the script help  */
  val = get_c_string (car (a));
  script->help = g_strdup (val);
  a = cdr (a);

  /*  Find the script author  */
  val = get_c_string (car (a));
  script->author = g_strdup (val);
  a = cdr (a);

  /*  Find the script copyright  */
  val = get_c_string (car (a));
  script->copyright = g_strdup (val);
  a = cdr (a);

  /*  Find the script date  */
  val = get_c_string (car (a));
  script->date = g_strdup (val);
  a = cdr (a);

  /*  Find the script image types  */
  if (TYPEP (a, tc_cons))
    {
      val = get_c_string (car (a));
      a = cdr (a);
    }
  else
    {
      val = get_c_string (a);
      a = NIL;
    }
  script->img_types = g_strdup (val);

  /*  Check the supplied number of arguments  */
  script->num_args = nlength (a) / 3;

324
  args = g_new (GimpParamDef, script->num_args + 1);
325
326
  args[0].type        = GIMP_PDB_INT32;
  args[0].name        = "run_mode";
327
328
  args[0].description = "Interactive, non-interactive";

329
  script->arg_types    = g_new (SFArgType, script->num_args);
330
  script->arg_labels   = g_new (gchar *, script->num_args);
331
332
  script->arg_defaults = g_new0 (SFArgValue, script->num_args);
  script->arg_values   = g_new0 (SFArgValue, script->num_args);
333

Elliot Lee's avatar
Elliot Lee committed
334
335
336
337
338
339
340
  if (script->num_args > 0)
    {
      for (i = 0; i < script->num_args; i++)
	{
	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_flonum))
341
		return my_err ("script-fu-register: argument types must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
342
343
344
345
	      script->arg_types[i] = get_c_long (car (a));
	      a = cdr (a);
	    }
	  else
346
	    return my_err ("script-fu-register: missing type specifier", NIL);
Elliot Lee's avatar
Elliot Lee committed
347
348
349
350

	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_string))
351
		return my_err ("script-fu-register: argument labels must be strings", NIL);
Elliot Lee's avatar
Elliot Lee committed
352
353
354
355
	      script->arg_labels[i] = g_strdup (get_c_string (car (a)));
	      a = cdr (a);
	    }
	  else
356
	    return my_err ("script-fu-register: missing arguments label", NIL);
Elliot Lee's avatar
Elliot Lee committed
357
358
359
360
361
362
363
364
365
366

	  if (a != NIL)
	    {
	      switch (script->arg_types[i])
		{
		case SF_IMAGE:
		case SF_DRAWABLE:
		case SF_LAYER:
		case SF_CHANNEL:
		  if (!TYPEP (car (a), tc_flonum))
367
		    return my_err ("script-fu-register: drawable defaults must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
368
369
370
371
372
373
		  script->arg_defaults[i].sfa_image = get_c_long (car (a));
		  script->arg_values[i].sfa_image = script->arg_defaults[i].sfa_image;

		  switch (script->arg_types[i])
		    {
		    case SF_IMAGE:
374
		      args[i + 1].type = GIMP_PDB_IMAGE;
Elliot Lee's avatar
Elliot Lee committed
375
376
		      args[i + 1].name = "image";
		      break;
377

Elliot Lee's avatar
Elliot Lee committed
378
		    case SF_DRAWABLE:
379
		      args[i + 1].type = GIMP_PDB_DRAWABLE;
Elliot Lee's avatar
Elliot Lee committed
380
381
		      args[i + 1].name = "drawable";
		      break;
382

Elliot Lee's avatar
Elliot Lee committed
383
		    case SF_LAYER:
384
		      args[i + 1].type = GIMP_PDB_LAYER;
Elliot Lee's avatar
Elliot Lee committed
385
386
		      args[i + 1].name = "layer";
		      break;
387

Elliot Lee's avatar
Elliot Lee committed
388
		    case SF_CHANNEL:
389
		      args[i + 1].type = GIMP_PDB_CHANNEL;
Elliot Lee's avatar
Elliot Lee committed
390
391
		      args[i + 1].name = "channel";
		      break;
392

Elliot Lee's avatar
Elliot Lee committed
393
394
395
396
397
398
399
400
401
		    default:
		      break;
		    }

		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_COLOR:
		  if (!TYPEP (car (a), tc_cons))
402
		    return my_err ("script-fu-register: color defaults must be a list of 3 integers", NIL);
Elliot Lee's avatar
Elliot Lee committed
403
		  color_list = car (a);
404
		  r = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
405
		  color_list = cdr (color_list);
406
		  g = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
407
		  color_list = cdr (color_list);
408
		  b = CLAMP (get_c_long (car (color_list)), 0, 255);
409
		  
410
411
		  gimp_rgb_set_uchar (&script->arg_defaults[i].sfa_color, 
				      r, g, b);
412
413
414

		  script->arg_values[i].sfa_color = 
		    script->arg_defaults[i].sfa_color;
Elliot Lee's avatar
Elliot Lee committed
415

416
		  args[i + 1].type = GIMP_PDB_COLOR;
Elliot Lee's avatar
Elliot Lee committed
417
418
419
420
421
422
		  args[i + 1].name = "color";
		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_TOGGLE:
		  if (!TYPEP (car (a), tc_flonum))
423
		    return my_err ("script-fu-register: toggle default must be an integer value", NIL);
424
425
426
427
		  script->arg_defaults[i].sfa_toggle = 
		    (get_c_long (car (a))) ? TRUE : FALSE;
		  script->arg_values[i].sfa_toggle = 
		    script->arg_defaults[i].sfa_toggle;
Elliot Lee's avatar
Elliot Lee committed
428

429
		  args[i + 1].type = GIMP_PDB_INT32;
Elliot Lee's avatar
Elliot Lee committed
430
431
432
433
		  args[i + 1].name = "toggle";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Sven Neumann's avatar
Sven Neumann committed
434
		case SF_VALUE:  
Elliot Lee's avatar
Elliot Lee committed
435
		  if (!TYPEP (car (a), tc_string))
436
		    return my_err ("script-fu-register: value defaults must be string values", NIL);
437
438
439
440
		  script->arg_defaults[i].sfa_value = 
		    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_value =  
		    g_strdup (script->arg_defaults[i].sfa_value);
Elliot Lee's avatar
Elliot Lee committed
441

442
		  args[i + 1].type = GIMP_PDB_STRING;
Elliot Lee's avatar
Elliot Lee committed
443
444
445
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;
446
447
448
449

		case SF_STRING:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: string defaults must be string values", NIL);
450
451
452
453
		  script->arg_defaults[i].sfa_value = 
		    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_value =  
		    g_strdup (script->arg_defaults[i].sfa_value);
454

455
		  args[i + 1].type = GIMP_PDB_STRING;
456
457
458
459
		  args[i + 1].name = "string";
		  args[i + 1].description = script->arg_labels[i];
		  break;

460
461
462
463
		case SF_ADJUSTMENT:
		  if (!TYPEP (car (a), tc_cons))
		    return my_err ("script-fu-register: adjustment defaults must be a list", NIL);
		  adj_list = car (a);
464
465
		  script->arg_defaults[i].sfa_adjustment.value = 
		    get_c_double (car (adj_list));
466
		  adj_list = cdr (adj_list);
467
468
		  script->arg_defaults[i].sfa_adjustment.lower = 
		    get_c_double (car (adj_list));
469
		  adj_list = cdr (adj_list);
470
471
		  script->arg_defaults[i].sfa_adjustment.upper = 
		    get_c_double (car (adj_list));
472
		  adj_list = cdr (adj_list);
473
474
		  script->arg_defaults[i].sfa_adjustment.step = 
		    get_c_double (car (adj_list));
475
		  adj_list = cdr (adj_list);
476
477
		  script->arg_defaults[i].sfa_adjustment.page = 
		    get_c_double (car (adj_list));
478
		  adj_list = cdr (adj_list);
479
480
		  script->arg_defaults[i].sfa_adjustment.digits = 
		    get_c_long (car (adj_list));
481
		  adj_list = cdr (adj_list);
482
483
		  script->arg_defaults[i].sfa_adjustment.type = 
		    get_c_long (car (adj_list));
484
		  script->arg_values[i].sfa_adjustment.adj = NULL;
485
486
		  script->arg_values[i].sfa_adjustment.value = 
		    script->arg_defaults[i].sfa_adjustment.value;
487

488
		  args[i + 1].type = GIMP_PDB_FLOAT;
489
490
491
492
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Manish Singh's avatar
Manish Singh committed
493
494
495
		case SF_FILENAME:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: filename defaults must be string values", NIL);
496
497
498
499
                  /* fallthrough */
		case SF_DIRNAME:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: dirname defaults must be string values", NIL);
500
501
		  script->arg_defaults[i].sfa_file.filename = 
		    g_strdup (get_c_string (car (a)));
Tor Lillqvist's avatar
Tor Lillqvist committed
502

503
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
504
505
506
507
508
509
510
511
512
513
514
515
		  /* Replace POSIX slashes with Win32 backslashes. This
		   * is just so script-fus can be written with only
		   * POSIX directory separators.
		   */
		  val = script->arg_defaults[i].sfa_file.filename;
		  while (*val)
		    {
		      if (*val == '/')
			*val = '\\';
		      val++;
		    }
#endif
516
517
		  script->arg_values[i].sfa_file.filename =  
		    g_strdup (script->arg_defaults[i].sfa_file.filename);
518
		  script->arg_values[i].sfa_file.fileselection = NULL;
Manish Singh's avatar
Manish Singh committed
519

520
		  args[i + 1].type = GIMP_PDB_STRING;
521
522
		  args[i + 1].name = (script->arg_types[i] == SF_FILENAME ? 
                                      "filename" : "dirname");
523
		  args[i + 1].description = script->arg_labels[i];
Manish Singh's avatar
Manish Singh committed
524
525
		 break;

Sven Neumann's avatar
Sven Neumann committed
526
527
528
		case SF_FONT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: font defaults must be string values", NIL);
529
530
531
532
		  script->arg_defaults[i].sfa_font =
                    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_font =
                    g_strdup (script->arg_defaults[i].sfa_font);
Sven Neumann's avatar
Sven Neumann committed
533
		  
534
		  args[i + 1].type = GIMP_PDB_STRING;
Sven Neumann's avatar
Sven Neumann committed
535
536
537
		  args[i + 1].name = "font";
		  args[i + 1].description = script->arg_labels[i];
		  break;
538
539
540
541

		case SF_PATTERN:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: pattern defaults must be string values", NIL);
542
543
544
545
		  script->arg_defaults[i].sfa_pattern = 
		    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_pattern =  
		    g_strdup (script->arg_defaults[i].sfa_pattern);
546

547
		  args[i + 1].type = GIMP_PDB_STRING;
548
549
550
551
		  args[i + 1].name = "pattern";
		  args[i + 1].description = script->arg_labels[i];
		  break;

552
553
554
555
		case SF_BRUSH:
		  if (!TYPEP (car (a), tc_cons))
		    return my_err ("script-fu-register: brush defaults must be a list", NIL);
		  brush_list = car (a);
556
557
		  script->arg_defaults[i].sfa_brush.name = 
		    g_strdup (get_c_string (car (brush_list)));
558
		  brush_list = cdr (brush_list);
559
560
		  script->arg_defaults[i].sfa_brush.opacity = 
		    get_c_double (car (brush_list));
561
		  brush_list = cdr (brush_list);
562
563
		  script->arg_defaults[i].sfa_brush.spacing = 
		    get_c_long (car (brush_list));
564
		  brush_list = cdr (brush_list);
565
566
567
568
		  script->arg_defaults[i].sfa_brush.paint_mode = 
		    get_c_long (car (brush_list));
		  script->arg_values[i].sfa_brush = 
		    script->arg_defaults[i].sfa_brush;
569
570
571
572
		  /* Need this since we need a copy of the string
		   * in the values area. We could free it later but the
		   * default one must hang around.
		   */
573
574
		  script->arg_values[i].sfa_brush.name = 
		    g_strdup(script->arg_defaults[i].sfa_brush.name);
575

576
		  args[i + 1].type = GIMP_PDB_STRING;
577
578
		  args[i + 1].name = "brush";
		  args[i + 1].description = script->arg_labels[i];
Sven Neumann's avatar
Sven Neumann committed
579
		  break;
580

581
582
583
		case SF_GRADIENT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: gradient defaults must be string values", NIL);
584
585
586
587
		  script->arg_defaults[i].sfa_gradient = 
		    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_gradient =  
		    g_strdup (script->arg_defaults[i].sfa_pattern);
588
		  
589
		  args[i + 1].type = GIMP_PDB_STRING;
590
591
592
593
		  args[i + 1].name = "gradient";
		  args[i + 1].description = script->arg_labels[i];
		  break;

594
595
596
		case SF_OPTION:
		  if (!TYPEP (car (a), tc_cons))
		    return my_err ("script-fu-register: option defaults must be a list", NIL);
597
598
599
		  for (option_list = car (a); 
		       option_list; 
		       option_list = cdr (option_list))
600
601
602
603
604
605
606
607
		    {
		      script->arg_defaults[i].sfa_option.list = 
			g_slist_append (script->arg_defaults[i].sfa_option.list, 
					g_strdup (get_c_string (car (option_list))));
		    }
		  script->arg_defaults[i].sfa_option.history = 0;
		  script->arg_values[i].sfa_option.history = 0;

608
		  args[i + 1].type = GIMP_PDB_INT32;
609
610
611
612
		  args[i + 1].name = "option";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Elliot Lee's avatar
Elliot Lee committed
613
614
615
616
617
618
619
		default:
		  break;
		}

	      a = cdr (a);
	    }
	  else
620
	    return my_err ("script-fu-register: missing default argument", NIL);
Elliot Lee's avatar
Elliot Lee committed
621
622
623
	}
    }

624
625
  script->args = args;
  g_tree_insert (script_list, gettext (script->description), script);
Elliot Lee's avatar
Elliot Lee committed
626
627
628
629
630
631
632

  return NIL;
}

void
script_fu_report_cc (gchar *command)
{
633
634
635
636
637
  if (sf_interface == NULL)
    return;

  if (sf_interface->last_command && 
      strcmp (sf_interface->last_command, command) == 0)
Elliot Lee's avatar
Elliot Lee committed
638
    {
639
      gchar *new_command;
Elliot Lee's avatar
Elliot Lee committed
640

641
642
643
644
      sf_interface->command_count++;

      new_command = g_strdup_printf ("%s <%d>", 
				     command, sf_interface->command_count);
645
      gtk_label_set_text (GTK_LABEL (sf_interface->status), new_command);
Elliot Lee's avatar
Elliot Lee committed
646
647
648
649
      g_free (new_command);
    }
  else
    {
650
      sf_interface->command_count = 1;
651
      gtk_label_set_text (GTK_LABEL (sf_interface->status), command);
652
653
      g_free (sf_interface->last_command);
      sf_interface->last_command = g_strdup (command);
Elliot Lee's avatar
Elliot Lee committed
654
    }
655
656

  while (gtk_main_iteration ());
Elliot Lee's avatar
Elliot Lee committed
657
658
}

659

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*  private functions  */

static void
script_fu_load_script (GimpDatafileData *file_data)
{
  if (gimp_datafiles_check_extension (file_data->filename, ".scm"))
    {
      gchar *command;
      gchar *qf = g_strescape (file_data->filename, NULL);

#ifdef __EMX__
      _fnslashify (qf);
#endif

      command = g_strdup_printf ("(load \"%s\")", qf);
      g_free (qf);

      if (repl_c_string (command, 0, 0, 1) != 0)
        script_fu_error_msg (command);

#ifdef G_OS_WIN32
      /* No, I don't know why, but this is 
       * necessary on NT 4.0.
       */
      Sleep(0);
#endif

      g_free (command);
    }
}

691
/* 
692
 *  The following function is a GTraverseFunction.  Please 
693
 *  note that it frees the script->args structure.  --Sven 
694
 */
695
static gboolean
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
script_fu_install_script (gpointer  foo,
			  SFScript *script,
			  gpointer  bar)
{
  gchar *menu_path = NULL;

  /* Allow scripts with no menus */
  if (strncmp (script->description, "<None>", 6) != 0)
    menu_path = script->description;

  gimp_install_temp_proc (script->pdb_name,
                          script->description,
                          script->help,
                          script->author,
                          script->copyright,
                          script->date,
                          menu_path,
                          script->img_types,
714
                          GIMP_TEMPORARY,
715
716
717
                          script->num_args + 1, 0,
                          script->args, NULL,
                          script_fu_script_proc);
718

719
720
721
722
723
724
725
726
727
  g_free (script->args);
  script->args = NULL;

  return FALSE;
}

/* 
 *  The following function is a GTraverseFunction.
 */
728
static gboolean
729
730
731
732
733
734
735
736
737
script_fu_remove_script (gpointer  foo,
			 SFScript *script,
			 gpointer  bar)
{
  script_fu_free_script (script);

  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
738
static void
739
740
741
742
743
script_fu_script_proc (gchar       *name,
		       gint         nparams,
		       GimpParam   *params,
		       gint        *nreturn_vals,
		       GimpParam  **return_vals)
Elliot Lee's avatar
Elliot Lee committed
744
{
745
746
747
748
749
750
  static GimpParam   values[1];
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  GimpRunMode        run_mode;
  SFScript          *script;
  gint               min_args;
  gchar             *escaped;
Elliot Lee's avatar
Elliot Lee committed
751
752
753
754

  run_mode = params[0].data.d_int32;

  if (! (script = script_fu_find_script (name)))
755
756
757
    {
      status = GIMP_PDB_CALLING_ERROR;
    }
Elliot Lee's avatar
Elliot Lee committed
758
759
  else
    {
760
      if (script->num_args == 0)
761
	run_mode = GIMP_RUN_NONINTERACTIVE;
762

Elliot Lee's avatar
Elliot Lee committed
763
764
      switch (run_mode)
	{
765
766
	case GIMP_RUN_INTERACTIVE:
	case GIMP_RUN_WITH_LAST_VALS:
767
	  /*  Determine whether the script is image based (runs on an image) */
Elliot Lee's avatar
Elliot Lee committed
768
769
	  if (strncmp (script->description, "<Image>", 7) == 0)
	    {
770
	      script->arg_values[0].sfa_image    = params[1].data.d_image;
Elliot Lee's avatar
Elliot Lee committed
771
772
773
774
775
776
777
	      script->arg_values[1].sfa_drawable = params[2].data.d_drawable;
	      script->image_based = TRUE;
	    }
	  else
	    script->image_based = FALSE;

	  /*  First acquire information with a dialog  */
778
779
	  /*  Skip this part if the script takes no parameters */ 
	  min_args = (script->image_based) ? 2 : 0;
780
781
782
783
	  if (script->num_args > min_args) 
	    {
	      script_fu_interface (script); 
	      break;
784
785
	    }  
	  /*  else fallthrough  */
Elliot Lee's avatar
Elliot Lee committed
786

787
	case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
788
789
	  /*  Make sure all the arguments are there!  */
	  if (nparams != (script->num_args + 1))
790
791
	    status = GIMP_PDB_CALLING_ERROR;
	  if (status == GIMP_PDB_SUCCESS)
Elliot Lee's avatar
Elliot Lee committed
792
	    {
793
	      guchar color[3];
794
795
796
797
798
799
	      gchar *text = NULL;
	      gchar *command;
	      gchar *c;
	      gchar  buffer[MAX_STRING_LENGTH];
	      gint   length;
	      gint   i;
Elliot Lee's avatar
Elliot Lee committed
800
801
802
803
804
805
806
807
808
809
810
811

	      length = strlen (script->script_name) + 3;

	      for (i = 0; i < script->num_args; i++)
		switch (script->arg_types[i])
		  {
		  case SF_IMAGE:
		  case SF_DRAWABLE:
		  case SF_LAYER:
		  case SF_CHANNEL:
		    length += 12;  /*  Maximum size of integer value will not exceed this many characters  */
		    break;
812

Elliot Lee's avatar
Elliot Lee committed
813
814
815
		  case SF_COLOR:
		    length += 16;  /*  Maximum size of color string: '(XXX XXX XXX)  */
		    break;
816

Elliot Lee's avatar
Elliot Lee committed
817
818
819
		  case SF_TOGGLE:
		    length += 6;   /*  Maximum size of (TRUE, FALSE)  */
		    break;
820

Elliot Lee's avatar
Elliot Lee committed
821
822
823
		  case SF_VALUE:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
824

825
		  case SF_STRING:
Manish Singh's avatar
Manish Singh committed
826
		  case SF_FILENAME:
827
		  case SF_DIRNAME:
828
		    escaped = g_strescape (params[i + 1].data.d_string, NULL);
Tor Lillqvist's avatar
Tor Lillqvist committed
829
830
		    length += strlen (escaped) + 3;
		    g_free (escaped);
Manish Singh's avatar
Manish Singh committed
831
		    break;
832

833
834
835
		  case SF_ADJUSTMENT:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
836

Sven Neumann's avatar
Sven Neumann committed
837
		  case SF_FONT:
838
		  case SF_PATTERN:
839
		  case SF_GRADIENT:
840
841
		  case SF_BRUSH:
		    length += strlen (params[i + 1].data.d_string) + 3;
842
		    break;		    
843

844
845
		  case SF_OPTION:
		    length += strlen (params[i + 1].data.d_string) + 1;
846
		    break;
847

Elliot Lee's avatar
Elliot Lee committed
848
849
850
851
		  default:
		    break;
		  }

852
	      c = command = g_new (gchar, length);
Elliot Lee's avatar
Elliot Lee committed
853

854
	      if (script->num_args)
Marc Lehmann's avatar
Marc Lehmann committed
855
856
857
                {
                  sprintf (command, "(%s ", script->script_name);
                  c += strlen (script->script_name) + 2;
858

Marc Lehmann's avatar
Marc Lehmann committed
859
860
861
862
863
864
865
866
                  for (i = 0; i < script->num_args; i++)
                    {
                      switch (script->arg_types[i])
                        {
                        case SF_IMAGE:
                        case SF_DRAWABLE:
                        case SF_LAYER:
                        case SF_CHANNEL:
867
868
                          g_snprintf (buffer, sizeof (buffer), "%d",
				      params[i + 1].data.d_image);
Marc Lehmann's avatar
Marc Lehmann committed
869
870
                          text = buffer;
                          break;
871

Marc Lehmann's avatar
Marc Lehmann committed
872
                        case SF_COLOR:
873
874
			  gimp_rgb_get_uchar (&params[i + 1].data.d_color,
					      color, color + 1, color + 2);
875
                          g_snprintf (buffer, sizeof (buffer), "'(%d %d %d)",
876
				      color[0], color[1], color[2]);
Marc Lehmann's avatar
Marc Lehmann committed
877
878
                          text = buffer;
                          break;
879

Marc Lehmann's avatar
Marc Lehmann committed
880
                        case SF_TOGGLE:
881
                          g_snprintf (buffer, sizeof (buffer), "%s",
882
883
				      (params[i + 1].data.d_int32) ? "TRUE" 
				                                   : "FALSE");
Marc Lehmann's avatar
Marc Lehmann committed
884
885
                          text = buffer;
                          break;
886

Marc Lehmann's avatar
Marc Lehmann committed
887
888
889
                        case SF_VALUE:
                          text = params[i + 1].data.d_string;
                          break;
890

Marc Lehmann's avatar
Marc Lehmann committed
891
892
                        case SF_STRING:
                        case SF_FILENAME:
893
                        case SF_DIRNAME:
894
895
                          escaped = g_strescape (params[i + 1].data.d_string, 
                                                 NULL);
896
897
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      escaped);
Marc Lehmann's avatar
Marc Lehmann committed
898
899
900
                          g_free (escaped);
                          text = buffer;
                          break;
901

Marc Lehmann's avatar
Marc Lehmann committed
902
903
904
                        case SF_ADJUSTMENT:
                          text = params[i + 1].data.d_string;
                          break;
905

Marc Lehmann's avatar
Marc Lehmann committed
906
907
908
909
                        case SF_FONT:
                        case SF_PATTERN:
                        case SF_GRADIENT:
                        case SF_BRUSH:
910
911
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      params[i + 1].data.d_string);
Marc Lehmann's avatar
Marc Lehmann committed
912
913
                          text = buffer;
                          break;
914

915
916
917
                        case SF_OPTION:
                          text = params[i + 1].data.d_string;
                          break;
918

Marc Lehmann's avatar
Marc Lehmann committed
919
920
921
922
923
924
925
926
                        default:
                          break;
                        }

                      if (i == script->num_args - 1)
                        sprintf (c, "%s)", text);
                      else
                        sprintf (c, "%s ", text);
927

Marc Lehmann's avatar
Marc Lehmann committed
928
929
930
                      c += strlen (text) + 1;
                    }
                }
931
932
	      else
		sprintf (command, "(%s)", script->script_name);
Elliot Lee's avatar
Elliot Lee committed
933
934

	      /*  run the command through the interpreter  */
935
936
937
	      if (repl_c_string (command, 0, 0, 1) != 0)
		script_fu_error_msg (command);
     
Elliot Lee's avatar
Elliot Lee committed
938
939
940
941
942
943
944
945
946
947
948
949
	      g_free (command);
	    }
	  break;

	default:
	  break;
	}
    }

  *nreturn_vals = 1;
  *return_vals = values;

950
  values[0].type = GIMP_PDB_STATUS;
Elliot Lee's avatar
Elliot Lee committed
951
952
953
  values[0].data.d_status = status;
}

954
/* this is a GTraverseFunction */
955
static gboolean
956
957
958
script_fu_lookup_script (gpointer  *foo,
			 SFScript  *script,
			 gchar    **name)
Elliot Lee's avatar
Elliot Lee committed
959
{
960
961
962
963
964
  if (strcmp (script->pdb_name, *name) == 0)
    {  
      /* store the script in the name pointer and stop the traversal */
      *name = (gchar *)script;
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
965
    }
966
967
968
  else
    return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
969

970
971
972
973
974
975
static SFScript *
script_fu_find_script (gchar *pdb_name)
{
  gchar *script;
  
  script = pdb_name;
976
977
978
  g_tree_foreach (script_list, 
                  (GTraverseFunc) script_fu_lookup_script, 
                  &script);
979

980
981
982
983
  if (script == pdb_name)
    return NULL;
  else
    return (SFScript *)script;
Elliot Lee's avatar
Elliot Lee committed
984
985
986
987
988
}

static void
script_fu_free_script (SFScript *script)
{
989
  gint i;
Elliot Lee's avatar
Elliot Lee committed
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

  /*  Uninstall the temporary procedure for this script  */
  gimp_uninstall_temp_proc (script->script_name);

  if (script)
    {
      g_free (script->script_name);
      g_free (script->description);
      g_free (script->help);
      g_free (script->author);
      g_free (script->copyright);
      g_free (script->date);
      g_free (script->img_types);
      g_free (script->arg_types);

      for (i = 0; i < script->num_args; i++)
	{
	  g_free (script->arg_labels[i]);
	  switch (script->arg_types[i])
	    {
	    case SF_IMAGE:
	    case SF_DRAWABLE:
	    case SF_LAYER:
	    case SF_CHANNEL:
	    case SF_COLOR:
	      break;
1016

Elliot Lee's avatar
Elliot Lee committed
1017
	    case SF_VALUE:
Sven Neumann's avatar
Sven Neumann committed
1018
	    case SF_STRING:
Elliot Lee's avatar
Elliot Lee committed
1019
	      g_free (script->arg_defaults[i].sfa_value);
1020
	      g_free (script->arg_values[i].sfa_value);
1021
	      break;
1022

1023
1024
	    case SF_ADJUSTMENT:
	      break;
1025

Manish Singh's avatar
Manish Singh committed
1026
	    case SF_FILENAME:
1027
	    case SF_DIRNAME:
Manish Singh's avatar
Manish Singh committed
1028
1029
1030
	      g_free (script->arg_defaults[i].sfa_file.filename);
	      g_free (script->arg_values[i].sfa_file.filename);
	      break;
1031

Sven Neumann's avatar
Sven Neumann committed
1032
	    case SF_FONT:
1033
1034
	      g_free (script->arg_defaults[i].sfa_font);
	      g_free (script->arg_values[i].sfa_font);
Sven Neumann's avatar
Sven Neumann committed
1035
	      break;
1036

1037
1038
1039
	    case SF_PATTERN:
	      g_free (script->arg_defaults[i].sfa_pattern);
	      g_free (script->arg_values[i].sfa_pattern);
1040
	      break;
1041

1042
1043
1044
1045
	    case SF_GRADIENT:
	      g_free (script->arg_defaults[i].sfa_gradient);
	      g_free (script->arg_values[i].sfa_gradient);
	      break;
1046

1047
1048
1049
1050
	    case SF_BRUSH:
	      g_free (script->arg_defaults[i].sfa_brush.name);
	      g_free (script->arg_values[i].sfa_brush.name);
	      break;
1051

1052
1053
1054
1055
1056
1057
	    case SF_OPTION:
	      g_slist_foreach (script->arg_defaults[i].sfa_option.list, 
			       (GFunc)g_free, NULL);
	      if (script->arg_defaults[i].sfa_option.list)
		g_slist_free (script->arg_defaults[i].sfa_option.list);
	      break;
1058

Elliot Lee's avatar
Elliot Lee committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
	    default:
	      break;
	    }
	}

      g_free (script->arg_labels);
      g_free (script->arg_defaults);
      g_free (script->arg_values);

      g_free (script);
    }
}

static void
script_fu_interface (SFScript *script)
{
  GtkWidget *dlg;
1076
  GtkWidget *frame;
Elliot Lee's avatar
Elliot Lee committed
1077
1078
1079
  GtkWidget *button;
  GtkWidget *menu;
  GtkWidget *table;
1080
1081
  GtkWidget *vbox;
  GtkWidget *hbox;
1082
  GtkWidget *menu_item;
1083
1084
1085
1086
1087
  GSList    *list;
  gchar     *buf;
  gint       start_args;
  gint       i;
  guint      j;
Elliot Lee's avatar
Elliot Lee committed
1088

1089
  static gboolean gtk_initted = FALSE;
Elliot Lee's avatar
Elliot Lee committed
1090

1091
1092
1093
1094
1095
1096
  /*  Simply return if there is already an interface. This is an
      ugly workaround for the fact that we can not process two
      scripts at a time.  */
  if (sf_interface != NULL)
    return;

1097
1098
  g_return_if_fail (script != NULL);

Elliot Lee's avatar
Elliot Lee committed
1099
1100
  if (!gtk_initted)
    {
1101
      INIT_I18N();
1102

1103
1104
      gimp_ui_init ("script-fu", TRUE);

Elliot Lee's avatar
Elliot Lee committed
1105
1106
1107
      gtk_initted = TRUE;
    }

1108
1109
  sf_interface = g_new0 (SFInterface, 1);
  sf_interface->args_widgets = g_new0 (GtkWidget *, script->num_args);
1110
  
Sven Neumann's avatar
Sven Neumann committed
1111
1112
  /* strip the first part of the menupath if it contains _("/Script-Fu/") */
  buf = strstr (gettext (script->description), _("/Script-Fu/"));
1113
  if (buf)
1114
    sf_interface->title = g_strdup (buf + strlen (_("/Script-Fu/")));
1115
  else