script-fu-scripts.c 62.8 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
#if HAVE_DIRENT_H
Elliot Lee's avatar
Elliot Lee committed
28
#include <dirent.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
29
#endif
Elliot Lee's avatar
Elliot Lee committed
30
#include <sys/stat.h>
Sven Neumann's avatar
Sven Neumann committed
31
#include <ctype.h>		/* For toupper() */
Tor Lillqvist's avatar
Tor Lillqvist committed
32

33
#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
34

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

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

Sven Neumann's avatar
Sven Neumann committed
41
#include "script-fu-intl.h"
42

43
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#define STRICT
#include <windows.h>

#include <io.h>

#ifndef W_OK
#define W_OK 2
#endif
#ifndef S_ISDIR
#define S_ISDIR(m) ((m) & _S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(m) ((m) & _S_IFREG)
#endif

59
#endif /* G_OS_WIN32 */
Tor Lillqvist's avatar
Tor Lillqvist committed
60

61
#define ESCAPE(string) gimp_strescape (string, NULL)
62

63
64
65
66
67
68
69
#define TEXT_WIDTH           100
#define TEXT_HEIGHT           25
#define COLOR_SAMPLE_WIDTH   100
#define COLOR_SAMPLE_HEIGHT   15
#define SLIDER_WIDTH         100
#define SPINNER_WIDTH         75
#define FONT_PREVIEW_WIDTH   100
Sven Neumann's avatar
Sven Neumann committed
70

71
#define DEFAULT_FONT_SIZE    240
Elliot Lee's avatar
Elliot Lee committed
72

73
#define MAX_STRING_LENGTH   4096
74

Elliot Lee's avatar
Elliot Lee committed
75

76
77
typedef struct
{
78
79
80
81
82
83
84
85
  GtkAdjustment    *adj;
  gfloat            value;
  gfloat            lower;
  gfloat            upper;
  gfloat            step;
  gfloat            page;
  gint              digits;
  SFAdjustmentType  type;
86
87
} SFAdjustment;

Sven Neumann's avatar
Sven Neumann committed
88
89
90
91
92
93
94
typedef struct
{
  GtkWidget *preview;
  GtkWidget *dialog;
  gchar     *fontname;
} SFFont;

Manish Singh's avatar
Manish Singh committed
95
96
typedef struct
{
97
  GtkWidget *fileselection;
Manish Singh's avatar
Manish Singh committed
98
99
100
  gchar     *filename;
} SFFilename;

101
102
typedef struct 
{
103
  gchar   *name;
104
105
106
107
108
  gdouble  opacity;
  gint     spacing;
  gint     paint_mode;
} SFBrush;

109
110
111
112
113
114
typedef struct 
{
  GSList  *list;
  guint    history;
} SFOption;

Elliot Lee's avatar
Elliot Lee committed
115
116
typedef union
{
117
118
119
120
  gint32         sfa_image;
  gint32         sfa_drawable;
  gint32         sfa_layer;
  gint32         sfa_channel;
121
  GimpRGB        sfa_color;
122
123
124
125
126
127
128
129
130
  gint32         sfa_toggle;
  gchar         *sfa_value;
  SFAdjustment   sfa_adjustment;
  SFFont         sfa_font;
  SFFilename     sfa_file;
  gchar         *sfa_pattern;
  gchar         *sfa_gradient;
  SFBrush        sfa_brush;
  SFOption       sfa_option;
Elliot Lee's avatar
Elliot Lee committed
131
132
133
134
} SFArgValue;

typedef struct
{
135
136
137
138
139
140
141
142
143
144
145
146
147
  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;
148
  gboolean       image_based;
149
  GimpParamDef  *args;     /*  used only temporary until installed  */
Elliot Lee's avatar
Elliot Lee committed
150
151
152
153
} SFScript;

typedef struct
{
154
155
156
157
158
159
160
  GtkWidget    **args_widgets;
  GtkWidget     *status;
  GtkWidget     *about_dialog;
  gchar         *window_title;
  gchar         *last_command;
  gint           command_count;
  gint           consec_command_count;
Elliot Lee's avatar
Elliot Lee committed
161
162
} SFInterface;

163

Elliot Lee's avatar
Elliot Lee committed
164
165
/* External functions
 */
166
extern long  nlength (LISP obj);
Elliot Lee's avatar
Elliot Lee committed
167
168
169
170
171

/*
 *  Local Functions
 */

172
static gboolean   script_fu_install_script   (gpointer    foo,
173
174
					      SFScript   *script,
					      gpointer    bar);
175
static gboolean   script_fu_remove_script    (gpointer    foo,
176
177
178
179
180
181
182
183
184
185
186
					      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);
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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);

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

static void       script_fu_font_dialog_ok          (GtkWidget *widget,
						     gpointer   data);
static void       script_fu_font_dialog_cancel      (GtkWidget *widget,
						     gpointer   data);
209
static gboolean   script_fu_font_dialog_delete      (GtkWidget *widget,
210
211
212
213
214
215
216
217
218
219
						     GdkEvent  *event,
						     gpointer   data);

static void       script_fu_font_preview            (GtkWidget *preview,
						     gchar     *fontname);
static void       script_fu_pattern_preview         (gchar     *name,
						     gint       width,
						     gint       height,
						     gint       bytes,
						     gchar     *mask_data,
220
						     gboolean   closing,
221
222
223
224
						     gpointer   data);
static void       script_fu_gradient_preview        (gchar     *name,
						     gint       width,
						     gdouble   *mask_data,
225
						     gboolean   closing,
226
227
228
229
230
231
232
233
						     gpointer   data);
static void       script_fu_brush_preview           (gchar     *name,
						     gdouble    opacity,
						     gint       spacing,
						     gint       paint_mode,
						     gint       width,
						     gint       height,
						     gchar     *mask_data,
234
						     gboolean   closing,
235
						     gpointer   data);
236

Elliot Lee's avatar
Elliot Lee committed
237
238
239
240
241

/*
 *  Local variables
 */

242
static struct stat  filestat;
243
244
245
246
static GTree       *script_list = NULL;
static SFInterface *sf_interface = NULL;  /*  there can only be at most one
					      interactive interface  */
					      
247
extern gchar        siod_err_msg[];
Elliot Lee's avatar
Elliot Lee committed
248
249
250
251
252
253

/*
 *  Function definitions
 */

void
254
script_fu_find_scripts (void)
Elliot Lee's avatar
Elliot Lee committed
255
256
257
258
259
260
261
262
263
{
  gchar *path_str;
  gchar *home;
  gchar *local_path;
  gchar *path;
  gchar *filename;
  gchar *token;
  gchar *next_token;
  gchar *command;
264
  gint   my_err;
Elliot Lee's avatar
Elliot Lee committed
265
266
267
268
269
270
  DIR   *dir;
  struct dirent *dir_ent;

  /*  Make sure to clear any existing scripts  */
  if (script_list != NULL)
    {
271
      g_tree_traverse (script_list, 
272
273
274
		       (GTraverseFunc)script_fu_remove_script, 
		       G_IN_ORDER, 
		       NULL);
275
      g_tree_destroy (script_list);
Elliot Lee's avatar
Elliot Lee committed
276
277
    }

278
279
280
281
282
283
#ifdef ENABLE_NLS
  script_list = g_tree_new ((GCompareFunc)strcoll);
#else
  script_list = g_tree_new ((GCompareFunc)strcmp);
#endif

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

286
287
  if (path_str == NULL)
    return;
Elliot Lee's avatar
Elliot Lee committed
288

289
290
291
292
293
294
295
296
297
298
299
  /* Set local path to contain temp_path, where (supposedly)
   * there may be working files.
   */
  home = g_get_home_dir ();
  local_path = g_strdup (path_str);
  
  /* Search through all directories in the local path */
  
  next_token = local_path;
  
  token = strtok (next_token, G_SEARCHPATH_SEPARATOR_S);
Elliot Lee's avatar
Elliot Lee committed
300

301
302
303
  while (token)
    {
      if (*token == '~')
304
305
306
307
308
309
310
311
312
	{
	  path = g_malloc (strlen (home) + strlen (token) + 2);
	  sprintf (path, "%s%s", home, token + 1);
	}
      else 
	{
	  path = g_malloc (strlen (token) + 2);
	  strcpy (path, token);
	}
313
	      
314
315
316
317
318
319
      /* Check if directory exists and if it has any items in it */
      my_err = stat (path, &filestat);
      
      if (!my_err && S_ISDIR (filestat.st_mode))
	{
	  if (path[strlen (path) - 1] != G_DIR_SEPARATOR)
320
321
	    strcat (path, G_DIR_SEPARATOR_S);

322
323
324
325
326
	  /* Open directory */
	  dir = opendir (path);
	  
	  if (!dir)
	    g_message ("error reading script directory \"%s\"", path);
Elliot Lee's avatar
Elliot Lee committed
327
328
	  else
	    {
329
	      while ((dir_ent = readdir (dir)))
Elliot Lee's avatar
Elliot Lee committed
330
		{
331
332
333
		  filename = g_strdup_printf ("%s%s", path, dir_ent->d_name);
		  
		  if (g_strcasecmp (filename + strlen (filename) - 4, ".scm") == 0)
Elliot Lee's avatar
Elliot Lee committed
334
		    {
335
336
337
338
		      /* Check the file and see that it is not a sub-directory */
		      my_err = stat (filename, &filestat);
		      
		      if (!my_err && S_ISREG (filestat.st_mode))
Elliot Lee's avatar
Elliot Lee committed
339
			{
340
			  gchar *qf = ESCAPE (filename);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
341
#ifdef __EMX__
342
			  _fnslashify(qf);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
343
#endif
344
345
			  command = g_strdup_printf ("(load \"%s\")", qf);
			  g_free (qf);
346
347
348

			  if (repl_c_string (command, 0, 0, 1) != 0)
			    script_fu_error_msg (command);
349
#ifdef G_OS_WIN32
350
351
352
353
			  /* No, I don't know why, but this is 
			   * necessary on NT 4.0.
			   */
			  Sleep(0);
Tor Lillqvist's avatar
Tor Lillqvist committed
354
#endif
355
			  g_free (command);
Elliot Lee's avatar
Elliot Lee committed
356
			}
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
		    }
		  
		  g_free (filename);
		} /* while */
	      
	      closedir (dir);
	    } /* else */
	} /* if */

      g_free (path);
      
      token = strtok (NULL, G_SEARCHPATH_SEPARATOR_S);
    } /* while */
  
  g_free (local_path);
  g_free (path_str);
373
374
375
376

  /*  now that all scripts are read in and sorted, tell gimp about them  */
  g_tree_traverse (script_list, 
		   (GTraverseFunc)script_fu_install_script, G_IN_ORDER, NULL);
Elliot Lee's avatar
Elliot Lee committed
377
378
379
380
381
}

LISP
script_fu_add_script (LISP a)
{
382
  GimpParamDef *args;
383
384
385
  SFScript     *script;
  gchar        *val;
  gint          i;
386
  guchar        r, g, b;
387
388
389
390
391
  LISP          color_list;
  LISP          adj_list;
  LISP          brush_list;
  LISP          option_list;
  gchar        *s;
Elliot Lee's avatar
Elliot Lee committed
392
393
394

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

  /*  Create a new script  */
398
  script = g_new0 (SFScript, 1);
Elliot Lee's avatar
Elliot Lee committed
399
400
401
402
403
404

  /*  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
405
406
407
408
409
410
411
412
413
  /* 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  /*  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;

455
  args = g_new (GimpParamDef, script->num_args + 1);
456
457
  args[0].type        = GIMP_PDB_INT32;
  args[0].name        = "run_mode";
458
459
  args[0].description = "Interactive, non-interactive";

460
  script->arg_types    = g_new (SFArgType, script->num_args);
461
  script->arg_labels   = g_new (gchar *, script->num_args);
462
463
  script->arg_defaults = g_new0 (SFArgValue, script->num_args);
  script->arg_values   = g_new0 (SFArgValue, script->num_args);
464

Elliot Lee's avatar
Elliot Lee committed
465
466
467
468
469
470
471
  if (script->num_args > 0)
    {
      for (i = 0; i < script->num_args; i++)
	{
	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_flonum))
472
		return my_err ("script-fu-register: argument types must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
473
474
475
476
	      script->arg_types[i] = get_c_long (car (a));
	      a = cdr (a);
	    }
	  else
477
	    return my_err ("script-fu-register: missing type specifier", NIL);
Elliot Lee's avatar
Elliot Lee committed
478
479
480
481

	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_string))
482
		return my_err ("script-fu-register: argument labels must be strings", NIL);
Elliot Lee's avatar
Elliot Lee committed
483
484
485
486
	      script->arg_labels[i] = g_strdup (get_c_string (car (a)));
	      a = cdr (a);
	    }
	  else
487
	    return my_err ("script-fu-register: missing arguments label", NIL);
Elliot Lee's avatar
Elliot Lee committed
488
489
490
491
492
493
494
495
496
497

	  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))
498
		    return my_err ("script-fu-register: drawable defaults must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
499
500
501
502
503
504
		  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:
505
		      args[i + 1].type = GIMP_PDB_IMAGE;
Elliot Lee's avatar
Elliot Lee committed
506
507
		      args[i + 1].name = "image";
		      break;
508

Elliot Lee's avatar
Elliot Lee committed
509
		    case SF_DRAWABLE:
510
		      args[i + 1].type = GIMP_PDB_DRAWABLE;
Elliot Lee's avatar
Elliot Lee committed
511
512
		      args[i + 1].name = "drawable";
		      break;
513

Elliot Lee's avatar
Elliot Lee committed
514
		    case SF_LAYER:
515
		      args[i + 1].type = GIMP_PDB_LAYER;
Elliot Lee's avatar
Elliot Lee committed
516
517
		      args[i + 1].name = "layer";
		      break;
518

Elliot Lee's avatar
Elliot Lee committed
519
		    case SF_CHANNEL:
520
		      args[i + 1].type = GIMP_PDB_CHANNEL;
Elliot Lee's avatar
Elliot Lee committed
521
522
		      args[i + 1].name = "channel";
		      break;
523

Elliot Lee's avatar
Elliot Lee committed
524
525
526
527
528
529
530
531
532
		    default:
		      break;
		    }

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

		case SF_COLOR:
		  if (!TYPEP (car (a), tc_cons))
533
		    return my_err ("script-fu-register: color defaults must be a list of 3 integers", NIL);
Elliot Lee's avatar
Elliot Lee committed
534
		  color_list = car (a);
535
		  r = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
536
		  color_list = cdr (color_list);
537
		  g = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
538
		  color_list = cdr (color_list);
539
		  b = CLAMP (get_c_long (car (color_list)), 0, 255);
540
		  
541
542
		  gimp_rgb_set_uchar (&script->arg_defaults[i].sfa_color, 
				      r, g, b);
543
544
545

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

547
		  args[i + 1].type = GIMP_PDB_COLOR;
Elliot Lee's avatar
Elliot Lee committed
548
549
550
551
552
553
		  args[i + 1].name = "color";
		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_TOGGLE:
		  if (!TYPEP (car (a), tc_flonum))
554
		    return my_err ("script-fu-register: toggle default must be an integer value", NIL);
555
556
557
558
		  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
559

560
		  args[i + 1].type = GIMP_PDB_INT32;
Elliot Lee's avatar
Elliot Lee committed
561
562
563
564
		  args[i + 1].name = "toggle";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Sven Neumann's avatar
Sven Neumann committed
565
		case SF_VALUE:  
Elliot Lee's avatar
Elliot Lee committed
566
		  if (!TYPEP (car (a), tc_string))
567
		    return my_err ("script-fu-register: value defaults must be string values", NIL);
568
569
570
571
		  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
572

573
		  args[i + 1].type = GIMP_PDB_STRING;
Elliot Lee's avatar
Elliot Lee committed
574
575
576
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;
577
578
579
580

		case SF_STRING:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: string defaults must be string values", NIL);
581
582
583
584
		  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);
585

586
		  args[i + 1].type = GIMP_PDB_STRING;
587
588
589
590
		  args[i + 1].name = "string";
		  args[i + 1].description = script->arg_labels[i];
		  break;

591
592
593
594
		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);
595
596
		  script->arg_defaults[i].sfa_adjustment.value = 
		    get_c_double (car (adj_list));
597
		  adj_list = cdr (adj_list);
598
599
		  script->arg_defaults[i].sfa_adjustment.lower = 
		    get_c_double (car (adj_list));
600
		  adj_list = cdr (adj_list);
601
602
		  script->arg_defaults[i].sfa_adjustment.upper = 
		    get_c_double (car (adj_list));
603
		  adj_list = cdr (adj_list);
604
605
		  script->arg_defaults[i].sfa_adjustment.step = 
		    get_c_double (car (adj_list));
606
		  adj_list = cdr (adj_list);
607
608
		  script->arg_defaults[i].sfa_adjustment.page = 
		    get_c_double (car (adj_list));
609
		  adj_list = cdr (adj_list);
610
611
		  script->arg_defaults[i].sfa_adjustment.digits = 
		    get_c_long (car (adj_list));
612
		  adj_list = cdr (adj_list);
613
614
		  script->arg_defaults[i].sfa_adjustment.type = 
		    get_c_long (car (adj_list));
615
		  script->arg_values[i].sfa_adjustment.adj = NULL;
616
617
		  script->arg_values[i].sfa_adjustment.value = 
		    script->arg_defaults[i].sfa_adjustment.value;
618

619
		  args[i + 1].type = GIMP_PDB_STRING;
620
621
622
623
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Manish Singh's avatar
Manish Singh committed
624
625
626
		case SF_FILENAME:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: filename defaults must be string values", NIL);
627
628
		  script->arg_defaults[i].sfa_file.filename = 
		    g_strdup (get_c_string (car (a)));
Tor Lillqvist's avatar
Tor Lillqvist committed
629

630
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
631
632
633
634
635
636
637
638
639
640
641
642
		  /* 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
643
644
		  script->arg_values[i].sfa_file.filename =  
		    g_strdup (script->arg_defaults[i].sfa_file.filename);
645
		  script->arg_values[i].sfa_file.fileselection = NULL;
Manish Singh's avatar
Manish Singh committed
646

647
		  args[i + 1].type = GIMP_PDB_STRING;
648
649
		  args[i + 1].name = "filename";
		  args[i + 1].description = script->arg_labels[i];
Manish Singh's avatar
Manish Singh committed
650
651
		 break;

Sven Neumann's avatar
Sven Neumann committed
652
653
654
		case SF_FONT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: font defaults must be string values", NIL);
655
656
657
658
		  script->arg_defaults[i].sfa_font.fontname = 
		    g_strdup (get_c_string (car (a)));
		  script->arg_values[i].sfa_font.fontname =  
		    g_strdup (script->arg_defaults[i].sfa_font.fontname);
Sven Neumann's avatar
Sven Neumann committed
659
660
661
		  script->arg_values[i].sfa_font.preview = NULL;
		  script->arg_values[i].sfa_font.dialog = NULL;
		  
662
		  args[i + 1].type = GIMP_PDB_STRING;
Sven Neumann's avatar
Sven Neumann committed
663
664
665
		  args[i + 1].name = "font";
		  args[i + 1].description = script->arg_labels[i];
		  break;
666
667
668
669

		case SF_PATTERN:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: pattern defaults must be string values", NIL);
670
671
672
673
		  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);
674

675
		  args[i + 1].type = GIMP_PDB_STRING;
676
677
678
679
		  args[i + 1].name = "pattern";
		  args[i + 1].description = script->arg_labels[i];
		  break;

680
681
682
683
		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);
684
685
		  script->arg_defaults[i].sfa_brush.name = 
		    g_strdup (get_c_string (car (brush_list)));
686
		  brush_list = cdr (brush_list);
687
688
		  script->arg_defaults[i].sfa_brush.opacity = 
		    get_c_double (car (brush_list));
689
		  brush_list = cdr (brush_list);
690
691
		  script->arg_defaults[i].sfa_brush.spacing = 
		    get_c_long (car (brush_list));
692
		  brush_list = cdr (brush_list);
693
694
695
696
		  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;
697
698
699
700
		  /* 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.
		   */
701
702
		  script->arg_values[i].sfa_brush.name = 
		    g_strdup(script->arg_defaults[i].sfa_brush.name);
703

704
		  args[i + 1].type = GIMP_PDB_STRING;
705
706
		  args[i + 1].name = "brush";
		  args[i + 1].description = script->arg_labels[i];
Sven Neumann's avatar
Sven Neumann committed
707
		  break;
708

709
710
711
		case SF_GRADIENT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: gradient defaults must be string values", NIL);
712
713
714
715
		  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);
716
		  
717
		  args[i + 1].type = GIMP_PDB_STRING;
718
719
720
721
		  args[i + 1].name = "gradient";
		  args[i + 1].description = script->arg_labels[i];
		  break;

722
723
724
		case SF_OPTION:
		  if (!TYPEP (car (a), tc_cons))
		    return my_err ("script-fu-register: option defaults must be a list", NIL);
725
726
727
		  for (option_list = car (a); 
		       option_list; 
		       option_list = cdr (option_list))
728
729
730
731
732
733
734
735
		    {
		      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;

736
		  args[i + 1].type = GIMP_PDB_INT32;
737
738
739
740
		  args[i + 1].name = "option";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Elliot Lee's avatar
Elliot Lee committed
741
742
743
744
745
746
747
		default:
		  break;
		}

	      a = cdr (a);
	    }
	  else
748
	    return my_err ("script-fu-register: missing default argument", NIL);
Elliot Lee's avatar
Elliot Lee committed
749
750
751
	}
    }

752
753
  script->args = args;
  g_tree_insert (script_list, gettext (script->description), script);
Elliot Lee's avatar
Elliot Lee committed
754
755
756
757
758
759
760

  return NIL;
}

void
script_fu_report_cc (gchar *command)
{
761
762
763
764
765
  if (sf_interface == NULL)
    return;

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

769
770
771
772
773
      sf_interface->command_count++;

      new_command = g_strdup_printf ("%s <%d>", 
				     command, sf_interface->command_count);
      gtk_entry_set_text (GTK_ENTRY (sf_interface->status), new_command);
Elliot Lee's avatar
Elliot Lee committed
774
775
776
777
      g_free (new_command);
    }
  else
    {
778
779
780
781
      sf_interface->command_count = 1;
      gtk_entry_set_text (GTK_ENTRY (sf_interface->status), command);
      g_free (sf_interface->last_command);
      sf_interface->last_command = g_strdup (command);
Elliot Lee's avatar
Elliot Lee committed
782
    }
783
784
  
  gdk_flush ();
Elliot Lee's avatar
Elliot Lee committed
785
786
}

787
788

/* 
789
 *  The following function is a GTraverseFunction.  Please 
790
 *  note that it frees the script->args structure.  --Sven 
791
 */
792
static gboolean
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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,
811
                          GIMP_TEMPORARY,
812
813
814
                          script->num_args + 1, 0,
                          script->args, NULL,
                          script_fu_script_proc);
815

816
817
818
819
820
821
822
823
824
  g_free (script->args);
  script->args = NULL;

  return FALSE;
}

/* 
 *  The following function is a GTraverseFunction.
 */
825
static gboolean
826
827
828
829
830
831
832
833
834
script_fu_remove_script (gpointer  foo,
			 SFScript *script,
			 gpointer  bar)
{
  script_fu_free_script (script);

  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
835
static void
836
837
838
839
840
script_fu_script_proc (gchar       *name,
		       gint         nparams,
		       GimpParam   *params,
		       gint        *nreturn_vals,
		       GimpParam  **return_vals)
Elliot Lee's avatar
Elliot Lee committed
841
{
842
  static GimpParam  values[1];
843
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
844
845
846
847
  GimpRunModeType   run_mode;
  SFScript         *script;
  gint              min_args;
  gchar            *escaped;
Elliot Lee's avatar
Elliot Lee committed
848
849
850
851

  run_mode = params[0].data.d_int32;

  if (! (script = script_fu_find_script (name)))
852
853
854
    {
      status = GIMP_PDB_CALLING_ERROR;
    }
Elliot Lee's avatar
Elliot Lee committed
855
856
  else
    {
857
      if (script->num_args == 0)
858
	run_mode = GIMP_RUN_NONINTERACTIVE;
859

Elliot Lee's avatar
Elliot Lee committed
860
861
      switch (run_mode)
	{
862
863
	case GIMP_RUN_INTERACTIVE:
	case GIMP_RUN_WITH_LAST_VALS:
864
	  /*  Determine whether the script is image based (runs on an image) */
Elliot Lee's avatar
Elliot Lee committed
865
866
	  if (strncmp (script->description, "<Image>", 7) == 0)
	    {
867
	      script->arg_values[0].sfa_image    = params[1].data.d_image;
Elliot Lee's avatar
Elliot Lee committed
868
869
870
871
872
873
874
	      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  */
875
876
	  /*  Skip this part if the script takes no parameters */ 
	  min_args = (script->image_based) ? 2 : 0;
877
878
879
880
	  if (script->num_args > min_args) 
	    {
	      script_fu_interface (script); 
	      break;
881
882
	    }  
	  /*  else fallthrough  */
Elliot Lee's avatar
Elliot Lee committed
883

884
	case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
885
886
	  /*  Make sure all the arguments are there!  */
	  if (nparams != (script->num_args + 1))
887
888
	    status = GIMP_PDB_CALLING_ERROR;
	  if (status == GIMP_PDB_SUCCESS)
Elliot Lee's avatar
Elliot Lee committed
889
	    {
890
	      guchar color[3];
891
892
893
894
895
896
	      gchar *text = NULL;
	      gchar *command;
	      gchar *c;
	      gchar  buffer[MAX_STRING_LENGTH];
	      gint   length;
	      gint   i;
Elliot Lee's avatar
Elliot Lee committed
897
898
899
900
901
902
903
904
905
906
907
908

	      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;
909

Elliot Lee's avatar
Elliot Lee committed
910
911
912
		  case SF_COLOR:
		    length += 16;  /*  Maximum size of color string: '(XXX XXX XXX)  */
		    break;
913

Elliot Lee's avatar
Elliot Lee committed
914
915
916
		  case SF_TOGGLE:
		    length += 6;   /*  Maximum size of (TRUE, FALSE)  */
		    break;
917

Elliot Lee's avatar
Elliot Lee committed
918
919
920
		  case SF_VALUE:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
921

922
		  case SF_STRING:
Manish Singh's avatar
Manish Singh committed
923
		  case SF_FILENAME:
924
		    escaped = ESCAPE (params[i + 1].data.d_string);
Tor Lillqvist's avatar
Tor Lillqvist committed
925
926
		    length += strlen (escaped) + 3;
		    g_free (escaped);
Manish Singh's avatar
Manish Singh committed
927
		    break;
928

929
930
931
		  case SF_ADJUSTMENT:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
932

Sven Neumann's avatar
Sven Neumann committed
933
		  case SF_FONT:
934
		  case SF_PATTERN:
935
		  case SF_GRADIENT:
936
937
		  case SF_BRUSH:
		    length += strlen (params[i + 1].data.d_string) + 3;
938
		    break;		    
939

940
941
		  case SF_OPTION:
		    length += strlen (params[i + 1].data.d_string) + 1;
942
		    break;
943

Elliot Lee's avatar
Elliot Lee committed
944
945
946
947
		  default:
		    break;
		  }

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

950
	      if (script->num_args)
Marc Lehmann's avatar
Marc Lehmann committed
951
952
953
                {
                  sprintf (command, "(%s ", script->script_name);
                  c += strlen (script->script_name) + 2;
954

Marc Lehmann's avatar
Marc Lehmann committed
955
956
957
958
959
960
961
962
                  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:
963
964
                          g_snprintf (buffer, sizeof (buffer), "%d",
				      params[i + 1].data.d_image);
Marc Lehmann's avatar
Marc Lehmann committed
965
966
                          text = buffer;
                          break;
967

Marc Lehmann's avatar
Marc Lehmann committed
968
                        case SF_COLOR:
969
970
			  gimp_rgb_get_uchar (&params[i + 1].data.d_color,
					      color, color + 1, color + 2);
971
                          g_snprintf (buffer, sizeof (buffer), "'(%d %d %d)",
972
				      color[0], color[1], color[2]);
Marc Lehmann's avatar
Marc Lehmann committed
973
974
                          text = buffer;
                          break;
975

Marc Lehmann's avatar
Marc Lehmann committed
976
                        case SF_TOGGLE:
977
                          g_snprintf (buffer, sizeof (buffer), "%s",
978
979
				      (params[i + 1].data.d_int32) ? "TRUE" 
				                                   : "FALSE");
Marc Lehmann's avatar
Marc Lehmann committed
980
981
                          text = buffer;
                          break;
982

Marc Lehmann's avatar
Marc Lehmann committed
983
984
985
                        case SF_VALUE:
                          text = params[i + 1].data.d_string;
                          break;
986

Marc Lehmann's avatar
Marc Lehmann committed
987
988
989
                        case SF_STRING:
                        case SF_FILENAME:
                          escaped = ESCAPE (params[i + 1].data.d_string);
990
991
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      escaped);
Marc Lehmann's avatar
Marc Lehmann committed
992
993
994
                          g_free (escaped);
                          text = buffer;
                          break;
995

Marc Lehmann's avatar
Marc Lehmann committed
996
997
998
                        case SF_ADJUSTMENT:
                          text = params[i + 1].data.d_string;
                          break;
999

Marc Lehmann's avatar
Marc Lehmann committed
1000
1001
1002
1003
                        case SF_FONT:
                        case SF_PATTERN:
                        case SF_GRADIENT:
                        case SF_BRUSH:
1004
1005
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      params[i + 1].data.d_string);
Marc Lehmann's avatar
Marc Lehmann committed
1006
1007
                          text = buffer;
                          break;
1008

1009
1010
1011
                        case SF_OPTION:
                          text = params[i + 1].data.d_string;
                          break;
1012

Marc Lehmann's avatar
Marc Lehmann committed
1013
1014
1015
1016
1017
1018
1019
1020
                        default:
                          break;
                        }

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

Marc Lehmann's avatar
Marc Lehmann committed
1022
1023
1024
                      c += strlen (text) + 1;
                    }
                }
1025
1026
	      else
		sprintf (command, "(%s)", script->script_name);
Elliot Lee's avatar
Elliot Lee committed
1027
1028

	      /*  run the command through the interpreter  */
1029
1030
1031
	      if (repl_c_string (command, 0, 0, 1) != 0)
		script_fu_error_msg (command);
     
Elliot Lee's avatar
Elliot Lee committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
	      g_free (command);
	    }
	  break;

	default:
	  break;
	}
    }

  *nreturn_vals = 1;
  *return_vals = values;

1044
  values[0].type = GIMP_PDB_STATUS;
Elliot Lee's avatar
Elliot Lee committed
1045
1046
1047
  values[0].data.d_status = status;
}

1048
/* this is a GTraverseFunction */
1049
static gboolean
1050
1051
1052
script_fu_lookup_script (gpointer  *foo,
			 SFScript  *script,
			 gchar    **name)
Elliot Lee's avatar
Elliot Lee committed
1053
{
1054
1055
1056
1057
1058
  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
1059
    }
1060
1061
1062
  else
    return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
1063

1064
1065
1066
1067
1068
1069
1070
static SFScript *
script_fu_find_script (gchar *pdb_name)
{
  gchar *script;
  
  script = pdb_name;
  g_tree_traverse (script_list, 
1071
1072
1073
1074
		   (GTraverseFunc) script_fu_lookup_script, 
		   G_IN_ORDER, 
		   &script);

1075
1076
1077
1078
  if (script == pdb_name)
    return NULL;
  else
    return (SFScript *)script;
Elliot Lee's avatar
Elliot Lee committed
1079
1080
1081
1082
1083
}

static void
script_fu_free_script (SFScript *script)
{
1084
  gint i;
Elliot Lee's avatar
Elliot Lee committed
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110

  /*  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;
1111

Elliot Lee's avatar
Elliot Lee committed
1112
	    case SF_VALUE:
Sven Neumann's avatar
Sven Neumann committed
1113
	    case SF_STRING:
Elliot Lee's avatar
Elliot Lee committed
1114
	      g_free (script->arg_defaults[i].sfa_value);
1115
	      g_free (script->arg_values[i].sfa_value);
1116
	      break;
1117

1118
1119
	    case SF_ADJUSTMENT:
	      break;
1120

Manish Singh's avatar
Manish Singh committed
1121
1122
1123
1124
	    case SF_FILENAME:
	      g_free (script->arg_defaults[i].sfa_file.filename);
	      g_free (script->arg_values[i].sfa_file.filename);
	      break;
1125

Sven Neumann's avatar
Sven Neumann committed
1126
1127
1128
1129
	    case SF_FONT:
	      g_free (script->arg_defaults[i].sfa_font.fontname);
	      g_free (script->arg_values[i].sfa_font.fontname);
	      break;
1130

1131
1132
1133
	    case SF_PATTERN:
	      g_free (script->arg_defaults[i].sfa_pattern);
	      g_free (script->arg_values[i].sfa_pattern);
1134
	      break;
1135

1136
1137
1138
1139
	    case SF_GRADIENT:
	      g_free (script->arg_defaults[i].sfa_gradient);
	      g_free (script->arg_values[i].sfa_gradient);
	      break;
1140

1141
1142
1143
1144
	    case SF_BRUSH:
	      g_free (script->arg_defaults[i].sfa_brush.name);
	      g_free (script->arg_values[i].sfa_brush.name);
	      break;
1145