script-fu-scripts.c 61 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 <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

Tor Lillqvist's avatar
Tor Lillqvist committed
43

44
45
46
47
48
#define TEXT_WIDTH           100
#define COLOR_SAMPLE_WIDTH   100
#define COLOR_SAMPLE_HEIGHT   15
#define SLIDER_WIDTH         100
#define FONT_PREVIEW_WIDTH   100
Sven Neumann's avatar
Sven Neumann committed
49

50
#define DEFAULT_FONT_SIZE    240
Elliot Lee's avatar
Elliot Lee committed
51

52
#define MAX_STRING_LENGTH   4096
53

Elliot Lee's avatar
Elliot Lee committed
54

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

Sven Neumann's avatar
Sven Neumann committed
67
68
69
70
71
72
73
typedef struct
{
  GtkWidget *preview;
  GtkWidget *dialog;
  gchar     *fontname;
} SFFont;

Manish Singh's avatar
Manish Singh committed
74
75
typedef struct
{
76
  GtkWidget *fileselection;
Manish Singh's avatar
Manish Singh committed
77
78
79
  gchar     *filename;
} SFFilename;

80
81
typedef struct 
{
82
  gchar   *name;
83
84
85
86
87
  gdouble  opacity;
  gint     spacing;
  gint     paint_mode;
} SFBrush;

88
89
90
91
92
93
typedef struct 
{
  GSList  *list;
  guint    history;
} SFOption;

Elliot Lee's avatar
Elliot Lee committed
94
95
typedef union
{
96
97
98
99
  gint32         sfa_image;
  gint32         sfa_drawable;
  gint32         sfa_layer;
  gint32         sfa_channel;
100
  GimpRGB        sfa_color;
101
102
103
104
105
106
107
108
109
  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
110
111
112
113
} SFArgValue;

typedef struct
{
114
115
116
117
118
119
120
121
122
123
124
125
126
  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;
127
  gboolean       image_based;
128
  GimpParamDef  *args;     /*  used only temporary until installed  */
Elliot Lee's avatar
Elliot Lee committed
129
130
131
132
} SFScript;

typedef struct
{
133
  GtkWidget     *dialog;
134
135
136
  GtkWidget    **args_widgets;
  GtkWidget     *status;
  GtkWidget     *about_dialog;
137
  gchar         *title;
138
139
140
  gchar         *last_command;
  gint           command_count;
  gint           consec_command_count;
Elliot Lee's avatar
Elliot Lee committed
141
142
} SFInterface;

143

Elliot Lee's avatar
Elliot Lee committed
144
145
/* External functions
 */
146
extern long  nlength (LISP obj);
Elliot Lee's avatar
Elliot Lee committed
147
148
149
150
151

/*
 *  Local Functions
 */

152
static gboolean   script_fu_install_script   (gpointer    foo,
153
154
					      SFScript   *script,
					      gpointer    bar);
155
static gboolean   script_fu_remove_script    (gpointer    foo,
156
157
158
159
160
161
162
163
164
165
166
					      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);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);
189
static gboolean   script_fu_font_dialog_delete      (GtkWidget *widget,
190
191
192
193
194
195
196
197
198
199
						     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,
200
						     gboolean   closing,
201
202
203
204
						     gpointer   data);
static void       script_fu_gradient_preview        (gchar     *name,
						     gint       width,
						     gdouble   *mask_data,
205
						     gboolean   closing,
206
207
208
209
210
211
212
213
						     gpointer   data);
static void       script_fu_brush_preview           (gchar     *name,
						     gdouble    opacity,
						     gint       spacing,
						     gint       paint_mode,
						     gint       width,
						     gint       height,
						     gchar     *mask_data,
214
						     gboolean   closing,
215
						     gpointer   data);
216

Elliot Lee's avatar
Elliot Lee committed
217
218
219
220
221

/*
 *  Local variables
 */

222
223
224
225
static GTree       *script_list = NULL;
static SFInterface *sf_interface = NULL;  /*  there can only be at most one
					      interactive interface  */
					      
226
extern gchar        siod_err_msg[];
Elliot Lee's avatar
Elliot Lee committed
227
228
229
230
231
232

/*
 *  Function definitions
 */

void
233
script_fu_find_scripts (void)
Elliot Lee's avatar
Elliot Lee committed
234
{
235
236
237
238
239
240
241
242
243
244
  const gchar *home;
  const gchar *entry;
  gchar       *path_str;
  gchar       *local_path;
  gchar       *path;
  gchar       *filename;
  gchar       *token;
  gchar       *next_token;
  gchar       *command;
  GDir        *dir;
Elliot Lee's avatar
Elliot Lee committed
245
246
247
248

  /*  Make sure to clear any existing scripts  */
  if (script_list != NULL)
    {
249
250
251
      g_tree_foreach (script_list, 
                      (GTraverseFunc) script_fu_remove_script, 
                      NULL);
252
      g_tree_destroy (script_list);
Elliot Lee's avatar
Elliot Lee committed
253
254
    }

255
#ifdef ENABLE_NLS
256
  script_list = g_tree_new ((GCompareFunc) strcoll);
257
#else
258
  script_list = g_tree_new ((GCompareFunc) strcmp);
259
260
#endif

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

263
264
  if (path_str == NULL)
    return;
Elliot Lee's avatar
Elliot Lee committed
265

266
267
268
269
270
271
272
273
274
275
276
  /* 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
277

278
279
280
  while (token)
    {
      if (*token == '~')
281
282
283
284
285
286
287
288
289
	{
	  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);
	}
290
	      
291
      /* Check if directory exists and if it has any items in it */
292
293
294
      if (g_file_test (path, G_FILE_TEST_IS_DIR))
        {
          GError *error;
295

296
	  dir = g_dir_open (path, 0, &error);
297
298
	  
	  if (!dir)
299
            {
300
              g_message ("Error reading script folder '%s'\n%s", 
301
302
303
                         path, error->message);
              g_clear_error (&error);
            }
Elliot Lee's avatar
Elliot Lee committed
304
305
	  else
	    {
306
	      while ((entry = g_dir_read_name (dir)))
Elliot Lee's avatar
Elliot Lee committed
307
		{
308
		  filename = g_build_filename (path, entry, NULL);
309
		  
310
		  if (g_ascii_strcasecmp (filename + strlen (filename) - 4, ".scm") == 0)
Elliot Lee's avatar
Elliot Lee committed
311
		    {
312
		      /* Check the file and see that it is not a sub-directory */
313
		      if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
Elliot Lee's avatar
Elliot Lee committed
314
			{
315
			  gchar *qf = g_strescape (filename, NULL);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
316
#ifdef __EMX__
317
			  _fnslashify(qf);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
318
#endif
319
320
			  command = g_strdup_printf ("(load \"%s\")", qf);
			  g_free (qf);
321
322
323

			  if (repl_c_string (command, 0, 0, 1) != 0)
			    script_fu_error_msg (command);
324
#ifdef G_OS_WIN32
325
326
327
328
			  /* No, I don't know why, but this is 
			   * necessary on NT 4.0.
			   */
			  Sleep(0);
Tor Lillqvist's avatar
Tor Lillqvist committed
329
#endif
330
			  g_free (command);
Elliot Lee's avatar
Elliot Lee committed
331
			}
332
333
334
335
336
		    }
		  
		  g_free (filename);
		} /* while */
	      
337
	      g_dir_close (dir);
338
339
340
341
342
343
344
345
346
347
	    } /* else */
	} /* if */

      g_free (path);
      
      token = strtok (NULL, G_SEARCHPATH_SEPARATOR_S);
    } /* while */
  
  g_free (local_path);
  g_free (path_str);
348
349

  /*  now that all scripts are read in and sorted, tell gimp about them  */
350
351
352
  g_tree_foreach (script_list, 
                  (GTraverseFunc) script_fu_install_script, 
                  NULL);
Elliot Lee's avatar
Elliot Lee committed
353
354
355
356
357
}

LISP
script_fu_add_script (LISP a)
{
358
  GimpParamDef *args;
359
360
361
  SFScript     *script;
  gchar        *val;
  gint          i;
362
  guchar        r, g, b;
363
364
365
366
367
  LISP          color_list;
  LISP          adj_list;
  LISP          brush_list;
  LISP          option_list;
  gchar        *s;
Elliot Lee's avatar
Elliot Lee committed
368
369
370

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

  /*  Create a new script  */
374
  script = g_new0 (SFScript, 1);
Elliot Lee's avatar
Elliot Lee committed
375
376
377
378
379
380

  /*  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
381
382
383
384
385
386
387
388
389
  /* 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  /*  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;

431
  args = g_new (GimpParamDef, script->num_args + 1);
432
433
  args[0].type        = GIMP_PDB_INT32;
  args[0].name        = "run_mode";
434
435
  args[0].description = "Interactive, non-interactive";

436
  script->arg_types    = g_new (SFArgType, script->num_args);
437
  script->arg_labels   = g_new (gchar *, script->num_args);
438
439
  script->arg_defaults = g_new0 (SFArgValue, script->num_args);
  script->arg_values   = g_new0 (SFArgValue, script->num_args);
440

Elliot Lee's avatar
Elliot Lee committed
441
442
443
444
445
446
447
  if (script->num_args > 0)
    {
      for (i = 0; i < script->num_args; i++)
	{
	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_flonum))
448
		return my_err ("script-fu-register: argument types must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
449
450
451
452
	      script->arg_types[i] = get_c_long (car (a));
	      a = cdr (a);
	    }
	  else
453
	    return my_err ("script-fu-register: missing type specifier", NIL);
Elliot Lee's avatar
Elliot Lee committed
454
455
456
457

	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_string))
458
		return my_err ("script-fu-register: argument labels must be strings", NIL);
Elliot Lee's avatar
Elliot Lee committed
459
460
461
462
	      script->arg_labels[i] = g_strdup (get_c_string (car (a)));
	      a = cdr (a);
	    }
	  else
463
	    return my_err ("script-fu-register: missing arguments label", NIL);
Elliot Lee's avatar
Elliot Lee committed
464
465
466
467
468
469
470
471
472
473

	  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))
474
		    return my_err ("script-fu-register: drawable defaults must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
475
476
477
478
479
480
		  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:
481
		      args[i + 1].type = GIMP_PDB_IMAGE;
Elliot Lee's avatar
Elliot Lee committed
482
483
		      args[i + 1].name = "image";
		      break;
484

Elliot Lee's avatar
Elliot Lee committed
485
		    case SF_DRAWABLE:
486
		      args[i + 1].type = GIMP_PDB_DRAWABLE;
Elliot Lee's avatar
Elliot Lee committed
487
488
		      args[i + 1].name = "drawable";
		      break;
489

Elliot Lee's avatar
Elliot Lee committed
490
		    case SF_LAYER:
491
		      args[i + 1].type = GIMP_PDB_LAYER;
Elliot Lee's avatar
Elliot Lee committed
492
493
		      args[i + 1].name = "layer";
		      break;
494

Elliot Lee's avatar
Elliot Lee committed
495
		    case SF_CHANNEL:
496
		      args[i + 1].type = GIMP_PDB_CHANNEL;
Elliot Lee's avatar
Elliot Lee committed
497
498
		      args[i + 1].name = "channel";
		      break;
499

Elliot Lee's avatar
Elliot Lee committed
500
501
502
503
504
505
506
507
508
		    default:
		      break;
		    }

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

		case SF_COLOR:
		  if (!TYPEP (car (a), tc_cons))
509
		    return my_err ("script-fu-register: color defaults must be a list of 3 integers", NIL);
Elliot Lee's avatar
Elliot Lee committed
510
		  color_list = car (a);
511
		  r = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
512
		  color_list = cdr (color_list);
513
		  g = CLAMP (get_c_long (car (color_list)), 0, 255);
Elliot Lee's avatar
Elliot Lee committed
514
		  color_list = cdr (color_list);
515
		  b = CLAMP (get_c_long (car (color_list)), 0, 255);
516
		  
517
518
		  gimp_rgb_set_uchar (&script->arg_defaults[i].sfa_color, 
				      r, g, b);
519
520
521

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

523
		  args[i + 1].type = GIMP_PDB_COLOR;
Elliot Lee's avatar
Elliot Lee committed
524
525
526
527
528
529
		  args[i + 1].name = "color";
		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_TOGGLE:
		  if (!TYPEP (car (a), tc_flonum))
530
		    return my_err ("script-fu-register: toggle default must be an integer value", NIL);
531
532
533
534
		  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
535

536
		  args[i + 1].type = GIMP_PDB_INT32;
Elliot Lee's avatar
Elliot Lee committed
537
538
539
540
		  args[i + 1].name = "toggle";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Sven Neumann's avatar
Sven Neumann committed
541
		case SF_VALUE:  
Elliot Lee's avatar
Elliot Lee committed
542
		  if (!TYPEP (car (a), tc_string))
543
		    return my_err ("script-fu-register: value defaults must be string values", NIL);
544
545
546
547
		  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
548

549
		  args[i + 1].type = GIMP_PDB_STRING;
Elliot Lee's avatar
Elliot Lee committed
550
551
552
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;
553
554
555
556

		case SF_STRING:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: string defaults must be string values", NIL);
557
558
559
560
		  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);
561

562
		  args[i + 1].type = GIMP_PDB_STRING;
563
564
565
566
		  args[i + 1].name = "string";
		  args[i + 1].description = script->arg_labels[i];
		  break;

567
568
569
570
		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);
571
572
		  script->arg_defaults[i].sfa_adjustment.value = 
		    get_c_double (car (adj_list));
573
		  adj_list = cdr (adj_list);
574
575
		  script->arg_defaults[i].sfa_adjustment.lower = 
		    get_c_double (car (adj_list));
576
		  adj_list = cdr (adj_list);
577
578
		  script->arg_defaults[i].sfa_adjustment.upper = 
		    get_c_double (car (adj_list));
579
		  adj_list = cdr (adj_list);
580
581
		  script->arg_defaults[i].sfa_adjustment.step = 
		    get_c_double (car (adj_list));
582
		  adj_list = cdr (adj_list);
583
584
		  script->arg_defaults[i].sfa_adjustment.page = 
		    get_c_double (car (adj_list));
585
		  adj_list = cdr (adj_list);
586
587
		  script->arg_defaults[i].sfa_adjustment.digits = 
		    get_c_long (car (adj_list));
588
		  adj_list = cdr (adj_list);
589
590
		  script->arg_defaults[i].sfa_adjustment.type = 
		    get_c_long (car (adj_list));
591
		  script->arg_values[i].sfa_adjustment.adj = NULL;
592
593
		  script->arg_values[i].sfa_adjustment.value = 
		    script->arg_defaults[i].sfa_adjustment.value;
594

595
		  args[i + 1].type = GIMP_PDB_FLOAT;
596
597
598
599
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Manish Singh's avatar
Manish Singh committed
600
601
602
		case SF_FILENAME:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: filename defaults must be string values", NIL);
603
604
605
606
                  /* fallthrough */
		case SF_DIRNAME:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: dirname defaults must be string values", NIL);
607
608
		  script->arg_defaults[i].sfa_file.filename = 
		    g_strdup (get_c_string (car (a)));
Tor Lillqvist's avatar
Tor Lillqvist committed
609

610
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
611
612
613
614
615
616
617
618
619
620
621
622
		  /* 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
623
624
		  script->arg_values[i].sfa_file.filename =  
		    g_strdup (script->arg_defaults[i].sfa_file.filename);
625
		  script->arg_values[i].sfa_file.fileselection = NULL;
Manish Singh's avatar
Manish Singh committed
626

627
		  args[i + 1].type = GIMP_PDB_STRING;
628
629
		  args[i + 1].name = (script->arg_types[i] == SF_FILENAME ? 
                                      "filename" : "dirname");
630
		  args[i + 1].description = script->arg_labels[i];
Manish Singh's avatar
Manish Singh committed
631
632
		 break;

Sven Neumann's avatar
Sven Neumann committed
633
634
635
		case SF_FONT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: font defaults must be string values", NIL);
636
637
638
639
		  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
640
641
642
		  script->arg_values[i].sfa_font.preview = NULL;
		  script->arg_values[i].sfa_font.dialog = NULL;
		  
643
		  args[i + 1].type = GIMP_PDB_STRING;
Sven Neumann's avatar
Sven Neumann committed
644
645
646
		  args[i + 1].name = "font";
		  args[i + 1].description = script->arg_labels[i];
		  break;
647
648
649
650

		case SF_PATTERN:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: pattern defaults must be string values", NIL);
651
652
653
654
		  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);
655

656
		  args[i + 1].type = GIMP_PDB_STRING;
657
658
659
660
		  args[i + 1].name = "pattern";
		  args[i + 1].description = script->arg_labels[i];
		  break;

661
662
663
664
		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);
665
666
		  script->arg_defaults[i].sfa_brush.name = 
		    g_strdup (get_c_string (car (brush_list)));
667
		  brush_list = cdr (brush_list);
668
669
		  script->arg_defaults[i].sfa_brush.opacity = 
		    get_c_double (car (brush_list));
670
		  brush_list = cdr (brush_list);
671
672
		  script->arg_defaults[i].sfa_brush.spacing = 
		    get_c_long (car (brush_list));
673
		  brush_list = cdr (brush_list);
674
675
676
677
		  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;
678
679
680
681
		  /* 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.
		   */
682
683
		  script->arg_values[i].sfa_brush.name = 
		    g_strdup(script->arg_defaults[i].sfa_brush.name);
684

685
		  args[i + 1].type = GIMP_PDB_STRING;
686
687
		  args[i + 1].name = "brush";
		  args[i + 1].description = script->arg_labels[i];
Sven Neumann's avatar
Sven Neumann committed
688
		  break;
689

690
691
692
		case SF_GRADIENT:
		  if (!TYPEP (car (a), tc_string))
		    return my_err ("script-fu-register: gradient defaults must be string values", NIL);
693
694
695
696
		  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);
697
		  
698
		  args[i + 1].type = GIMP_PDB_STRING;
699
700
701
702
		  args[i + 1].name = "gradient";
		  args[i + 1].description = script->arg_labels[i];
		  break;

703
704
705
		case SF_OPTION:
		  if (!TYPEP (car (a), tc_cons))
		    return my_err ("script-fu-register: option defaults must be a list", NIL);
706
707
708
		  for (option_list = car (a); 
		       option_list; 
		       option_list = cdr (option_list))
709
710
711
712
713
714
715
716
		    {
		      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;

717
		  args[i + 1].type = GIMP_PDB_INT32;
718
719
720
721
		  args[i + 1].name = "option";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Elliot Lee's avatar
Elliot Lee committed
722
723
724
725
726
727
728
		default:
		  break;
		}

	      a = cdr (a);
	    }
	  else
729
	    return my_err ("script-fu-register: missing default argument", NIL);
Elliot Lee's avatar
Elliot Lee committed
730
731
732
	}
    }

733
734
  script->args = args;
  g_tree_insert (script_list, gettext (script->description), script);
Elliot Lee's avatar
Elliot Lee committed
735
736
737
738
739
740
741

  return NIL;
}

void
script_fu_report_cc (gchar *command)
{
742
743
744
745
746
  if (sf_interface == NULL)
    return;

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

750
751
752
753
      sf_interface->command_count++;

      new_command = g_strdup_printf ("%s <%d>", 
				     command, sf_interface->command_count);
754
      gtk_label_set_text (GTK_LABEL (sf_interface->status), new_command);
Elliot Lee's avatar
Elliot Lee committed
755
756
757
758
      g_free (new_command);
    }
  else
    {
759
      sf_interface->command_count = 1;
760
      gtk_label_set_text (GTK_LABEL (sf_interface->status), command);
761
762
      g_free (sf_interface->last_command);
      sf_interface->last_command = g_strdup (command);
Elliot Lee's avatar
Elliot Lee committed
763
    }
764
765

  while (gtk_main_iteration ());
Elliot Lee's avatar
Elliot Lee committed
766
767
}

768
769

/* 
770
 *  The following function is a GTraverseFunction.  Please 
771
 *  note that it frees the script->args structure.  --Sven 
772
 */
773
static gboolean
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
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,
792
                          GIMP_TEMPORARY,
793
794
795
                          script->num_args + 1, 0,
                          script->args, NULL,
                          script_fu_script_proc);
796

797
798
799
800
801
802
803
804
805
  g_free (script->args);
  script->args = NULL;

  return FALSE;
}

/* 
 *  The following function is a GTraverseFunction.
 */
806
static gboolean
807
808
809
810
811
812
813
814
815
script_fu_remove_script (gpointer  foo,
			 SFScript *script,
			 gpointer  bar)
{
  script_fu_free_script (script);

  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
816
static void
817
818
819
820
821
script_fu_script_proc (gchar       *name,
		       gint         nparams,
		       GimpParam   *params,
		       gint        *nreturn_vals,
		       GimpParam  **return_vals)
Elliot Lee's avatar
Elliot Lee committed
822
{
823
824
825
826
827
828
  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
829
830
831
832

  run_mode = params[0].data.d_int32;

  if (! (script = script_fu_find_script (name)))
833
834
835
    {
      status = GIMP_PDB_CALLING_ERROR;
    }
Elliot Lee's avatar
Elliot Lee committed
836
837
  else
    {
838
      if (script->num_args == 0)
839
	run_mode = GIMP_RUN_NONINTERACTIVE;
840

Elliot Lee's avatar
Elliot Lee committed
841
842
      switch (run_mode)
	{
843
844
	case GIMP_RUN_INTERACTIVE:
	case GIMP_RUN_WITH_LAST_VALS:
845
	  /*  Determine whether the script is image based (runs on an image) */
Elliot Lee's avatar
Elliot Lee committed
846
847
	  if (strncmp (script->description, "<Image>", 7) == 0)
	    {
848
	      script->arg_values[0].sfa_image    = params[1].data.d_image;
Elliot Lee's avatar
Elliot Lee committed
849
850
851
852
853
854
855
	      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  */
856
857
	  /*  Skip this part if the script takes no parameters */ 
	  min_args = (script->image_based) ? 2 : 0;
858
859
860
861
	  if (script->num_args > min_args) 
	    {
	      script_fu_interface (script); 
	      break;
862
863
	    }  
	  /*  else fallthrough  */
Elliot Lee's avatar
Elliot Lee committed
864

865
	case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
866
867
	  /*  Make sure all the arguments are there!  */
	  if (nparams != (script->num_args + 1))
868
869
	    status = GIMP_PDB_CALLING_ERROR;
	  if (status == GIMP_PDB_SUCCESS)
Elliot Lee's avatar
Elliot Lee committed
870
	    {
871
	      guchar color[3];
872
873
874
875
876
877
	      gchar *text = NULL;
	      gchar *command;
	      gchar *c;
	      gchar  buffer[MAX_STRING_LENGTH];
	      gint   length;
	      gint   i;
Elliot Lee's avatar
Elliot Lee committed
878
879
880
881
882
883
884
885
886
887
888
889

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

Elliot Lee's avatar
Elliot Lee committed
891
892
893
		  case SF_COLOR:
		    length += 16;  /*  Maximum size of color string: '(XXX XXX XXX)  */
		    break;
894

Elliot Lee's avatar
Elliot Lee committed
895
896
897
		  case SF_TOGGLE:
		    length += 6;   /*  Maximum size of (TRUE, FALSE)  */
		    break;
898

Elliot Lee's avatar
Elliot Lee committed
899
900
901
		  case SF_VALUE:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
902

903
		  case SF_STRING:
Manish Singh's avatar
Manish Singh committed
904
		  case SF_FILENAME:
905
		  case SF_DIRNAME:
906
		    escaped = g_strescape (params[i + 1].data.d_string, NULL);
Tor Lillqvist's avatar
Tor Lillqvist committed
907
908
		    length += strlen (escaped) + 3;
		    g_free (escaped);
Manish Singh's avatar
Manish Singh committed
909
		    break;
910

911
912
913
		  case SF_ADJUSTMENT:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
914

Sven Neumann's avatar
Sven Neumann committed
915
		  case SF_FONT:
916
		  case SF_PATTERN:
917
		  case SF_GRADIENT:
918
919
		  case SF_BRUSH:
		    length += strlen (params[i + 1].data.d_string) + 3;
920
		    break;		    
921

922
923
		  case SF_OPTION:
		    length += strlen (params[i + 1].data.d_string) + 1;
924
		    break;
925

Elliot Lee's avatar
Elliot Lee committed
926
927
928
929
		  default:
		    break;
		  }

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

932
	      if (script->num_args)
Marc Lehmann's avatar
Marc Lehmann committed
933
934
935
                {
                  sprintf (command, "(%s ", script->script_name);
                  c += strlen (script->script_name) + 2;
936

Marc Lehmann's avatar
Marc Lehmann committed
937
938
939
940
941
942
943
944
                  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:
945
946
                          g_snprintf (buffer, sizeof (buffer), "%d",
				      params[i + 1].data.d_image);
Marc Lehmann's avatar
Marc Lehmann committed
947
948
                          text = buffer;
                          break;
949

Marc Lehmann's avatar
Marc Lehmann committed
950
                        case SF_COLOR:
951
952
			  gimp_rgb_get_uchar (&params[i + 1].data.d_color,
					      color, color + 1, color + 2);
953
                          g_snprintf (buffer, sizeof (buffer), "'(%d %d %d)",
954
				      color[0], color[1], color[2]);
Marc Lehmann's avatar
Marc Lehmann committed
955
956
                          text = buffer;
                          break;
957

Marc Lehmann's avatar
Marc Lehmann committed
958
                        case SF_TOGGLE:
959
                          g_snprintf (buffer, sizeof (buffer), "%s",
960
961
				      (params[i + 1].data.d_int32) ? "TRUE" 
				                                   : "FALSE");
Marc Lehmann's avatar
Marc Lehmann committed
962
963
                          text = buffer;
                          break;
964

Marc Lehmann's avatar
Marc Lehmann committed
965
966
967
                        case SF_VALUE:
                          text = params[i + 1].data.d_string;
                          break;
968

Marc Lehmann's avatar
Marc Lehmann committed
969
970
                        case SF_STRING:
                        case SF_FILENAME:
971
                        case SF_DIRNAME:
972
973
                          escaped = g_strescape (params[i + 1].data.d_string, 
                                                 NULL);
974
975
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      escaped);
Marc Lehmann's avatar
Marc Lehmann committed
976
977
978
                          g_free (escaped);
                          text = buffer;
                          break;
979

Marc Lehmann's avatar
Marc Lehmann committed
980
981
982
                        case SF_ADJUSTMENT:
                          text = params[i + 1].data.d_string;
                          break;
983

Marc Lehmann's avatar
Marc Lehmann committed
984
985
986
987
                        case SF_FONT:
                        case SF_PATTERN:
                        case SF_GRADIENT:
                        case SF_BRUSH:
988
989
                          g_snprintf (buffer, sizeof (buffer), "\"%s\"",
				      params[i + 1].data.d_string);
Marc Lehmann's avatar
Marc Lehmann committed
990
991
                          text = buffer;
                          break;
992

993
994
995
                        case SF_OPTION:
                          text = params[i + 1].data.d_string;
                          break;
996

Marc Lehmann's avatar
Marc Lehmann committed
997
998
999
1000
1001
1002
1003
1004
                        default:
                          break;
                        }

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

Marc Lehmann's avatar
Marc Lehmann committed
1006
1007
1008
                      c += strlen (text) + 1;
                    }
                }
1009
1010
	      else
		sprintf (command, "(%s)", script->script_name);
Elliot Lee's avatar
Elliot Lee committed
1011
1012

	      /*  run the command through the interpreter  */
1013
1014
1015
	      if (repl_c_string (command, 0, 0, 1) != 0)
		script_fu_error_msg (command);
     
Elliot Lee's avatar
Elliot Lee committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
	      g_free (command);
	    }
	  break;

	default:
	  break;
	}
    }

  *nreturn_vals = 1;
  *return_vals = values;

1028
  values[0].type = GIMP_PDB_STATUS;
Elliot Lee's avatar
Elliot Lee committed
1029
1030
1031
  values[0].data.d_status = status;
}

1032
/* this is a GTraverseFunction */
1033
static gboolean
1034
1035
1036
script_fu_lookup_script (gpointer  *foo,
			 SFScript  *script,
			 gchar    **name)
Elliot Lee's avatar
Elliot Lee committed
1037
{
1038
1039
1040
1041
1042
  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
1043
    }
1044
1045
1046
  else
    return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
1047

1048
1049
1050
1051
1052
1053
static SFScript *
script_fu_find_script (gchar *pdb_name)
{
  gchar *script;
  
  script = pdb_name;
1054
1055
1056
  g_tree_foreach (script_list, 
                  (GTraverseFunc) script_fu_lookup_script, 
                  &script);
1057

1058
1059
1060
1061
  if (script == pdb_name)
    return NULL;
  else
    return (SFScript *)script;
Elliot Lee's avatar
Elliot Lee committed
1062
1063
1064
1065
1066
}

static void
script_fu_free_script (SFScript *script)
{
1067
  gint i;
Elliot Lee's avatar
Elliot Lee committed
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

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

Elliot Lee's avatar
Elliot Lee committed
1095
	    case SF_VALUE:
Sven Neumann's avatar
Sven Neumann committed
1096
	    case SF_STRING:
Elliot Lee's avatar
Elliot Lee committed
1097
	      g_free (script->arg_defaults[i].sfa_value);
1098
	      g_free (script->arg_values[i].sfa_value);
1099
	      break;
1100

1101
1102
	    case SF_ADJUSTMENT:
	      break;
1103

Manish Singh's avatar
Manish Singh committed
1104
	    case SF_FILENAME:
1105
	    case SF_DIRNAME:
Manish Singh's avatar
Manish Singh committed
1106
1107
1108
	      g_free (script->arg_defaults[i].sfa_file.filename);
	      g_free (script->arg_values[i].sfa_file.filename);
	      break;
1109

Sven Neumann's avatar
Sven Neumann committed