script-fu-interface.c 36.6 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include "gtk/gtk.h"
#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"
#include "siod.h"
#include "script-fu-scripts.h"

#define TEXT_WIDTH  100
#define TEXT_HEIGHT 25
#define COLOR_SAMPLE_WIDTH 100
#define COLOR_SAMPLE_HEIGHT 15
34 35 36
#define SLIDER_WIDTH  100
#define SLIDER_HEIGHT 30
#define SPINNER_WIDTH 75
Elliot Lee's avatar
Elliot Lee committed
37

38 39
#define MAX_STRING_LENGTH 4096

Elliot Lee's avatar
Elliot Lee committed
40 41 42 43 44 45 46 47
typedef struct
{
  GtkWidget *preview;
  GtkWidget *dialog;
  gdouble    color[3];
  gdouble    old_color[3];
} SFColor;

48 49 50 51 52 53 54 55 56 57 58 59
typedef struct
{
  GtkAdjustment *adj;
  gfloat         value;
  gfloat         lower;
  gfloat         upper;
  gfloat         step;
  gfloat         page;
  gint           digits;
  SFAdjustmentType type;
} SFAdjustment;

Elliot Lee's avatar
Elliot Lee committed
60 61
typedef union
{
62 63 64 65 66 67 68 69 70
  gint32        sfa_image;
  gint32        sfa_drawable;
  gint32        sfa_layer;
  gint32        sfa_channel;
  SFColor       sfa_color;
  gint32        sfa_toggle;
  gchar *       sfa_value;
  gchar *       sfa_string;
  SFAdjustment  sfa_adjustment;
Elliot Lee's avatar
Elliot Lee committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
} SFArgValue;

typedef struct
{
  GtkWidget ** args_widgets;
  gchar *      script_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;
  gint32       image_based;
} SFScript;

typedef struct
{
93 94
  GtkWidget *status;
  GtkWidget *entry;
Elliot Lee's avatar
Elliot Lee committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  SFScript  *script;
} SFInterface;

/* External functions
 */
extern long  nlength      (LISP obj);

/*
 *  Local Functions
 */

static void       script_fu_script_proc      (char     *name,
					      int       nparams,
					      GParam   *params,
					      int      *nreturn_vals,
					      GParam  **return_vals);

static SFScript  *script_fu_find_script      (gchar    *script_name);
static void       script_fu_free_script      (SFScript *script);
static void       script_fu_enable_cc        (void);
static void       script_fu_disable_cc       (gint    err_msg);
static void       script_fu_interface        (SFScript *script);
static void       script_fu_color_preview    (GtkWidget *preview,
					      gdouble   *color);
static void       script_fu_cleanup_widgets  (SFScript *script);
static void       script_fu_ok_callback      (GtkWidget *widget,
					      gpointer   data);
static void       script_fu_close_callback   (GtkWidget *widget,
					      gpointer   data);
static void       script_fu_menu_callback    (gint32     id,
					      gpointer   data);
static void       script_fu_toggle_update    (GtkWidget *widget,
					      gpointer   data);
128 129 130 131 132 133 134 135 136
static void       script_fu_color_preview_callback (GtkWidget *widget,
						    gpointer   data);
static void       script_fu_color_preview_changed  (GtkWidget *widget,
						    gpointer   data);
static void       script_fu_color_preview_cancel   (GtkWidget *widget,
						    gpointer   data);
static gint       script_fu_color_preview_delete   (GtkWidget *widget,
						    GdkEvent  *event,
						    gpointer   data);
Elliot Lee's avatar
Elliot Lee committed
137 138 139 140 141 142 143

/*
 *  Local variables
 */

static SFInterface sf_interface =
{
144
  NULL,  /*  status (current command) */
Elliot Lee's avatar
Elliot Lee committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  NULL   /*  active script    */
};

static struct stat filestat;
static gint   current_command_enabled = FALSE;
static gint   command_count = 0;
static gint   consec_command_count = 0;
static gchar *last_command = NULL;
static GList *script_list = NULL;

extern char   siod_err_msg[];

/*
 *  Function definitions
 */

void
script_fu_find_scripts ()
{
  GParam *return_vals;
  gint nreturn_vals;
  gchar *path_str;
  gchar *home;
  gchar *local_path;
  gchar *path;
  gchar *filename;
  gchar *token;
  gchar *next_token;
  gchar *command;
174
  gint   my_err;
Elliot Lee's avatar
Elliot Lee committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  DIR   *dir;
  struct dirent *dir_ent;

  /*  Make sure to clear any existing scripts  */
  if (script_list != NULL)
    {
      GList *list;
      SFScript *script;

      list = script_list;
      while (list)
	{
	  script = (SFScript *) list->data;
	  script_fu_free_script (script);
	  list = list->next;
	}

      if (script_list)
	g_list_free (script_list);
      script_list = NULL;
    }

  return_vals = gimp_run_procedure ("gimp_gimprc_query",
				    &nreturn_vals,
				    PARAM_STRING, "script-fu-path",
				    PARAM_END);

  if (return_vals[0].data.d_status == STATUS_SUCCESS)
    {
      path_str = return_vals[1].data.d_string;

      if (path_str == NULL)
	return;

      /* Set local path to contain temp_path, where (supposedly)
       * there may be working files.
       */
      home = getenv ("HOME");
      local_path = g_strdup (path_str);

      /* Search through all directories in the local path */

      next_token = local_path;

      token = strtok (next_token, ":");

      while (token)
	{
	  if (*token == '~')
	    {
	      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);
	    } /* else */

	  /* Check if directory exists and if it has any items in it */
235
	  my_err = stat (path, &filestat);
Elliot Lee's avatar
Elliot Lee committed
236

237
	  if (!my_err && S_ISDIR (filestat.st_mode))
Elliot Lee's avatar
Elliot Lee committed
238 239 240 241 242 243 244 245
	    {
	      if (path[strlen (path) - 1] != '/')
		strcat (path, "/");

	      /* Open directory */
	      dir = opendir (path);

	      if (!dir)
246
		  g_message ("error reading script directory \"%s\"", path);
Elliot Lee's avatar
Elliot Lee committed
247 248 249 250 251 252 253 254 255 256 257
	      else
		{
		  while ((dir_ent = readdir (dir)))
		    {
		      filename = g_malloc (strlen(path) + strlen (dir_ent->d_name) + 1);

		      sprintf (filename, "%s%s", path, dir_ent->d_name);

		      if (strcmp (filename + strlen (filename) - 4, ".scm") == 0)
			{
			  /* Check the file and see that it is not a sub-directory */
258
			  my_err = stat (filename, &filestat);
Elliot Lee's avatar
Elliot Lee committed
259

260
			  if (!my_err && S_ISREG (filestat.st_mode))
Elliot Lee's avatar
Elliot Lee committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
			    {
			      command = g_new (char, strlen ("(load \"\")") + strlen (filename) + 1);
			      sprintf (command, "(load \"%s\")", filename);

			      repl_c_string (command, 0, 0, 1);

			      g_free (command);
			    }
			}

		      g_free (filename);
		    } /* while */

		  closedir (dir);
		} /* else */
	    } /* if */

	  g_free (path);

	  token = strtok (NULL, ":");
	} /* while */

      g_free(local_path);
    }

  gimp_destroy_params (return_vals, nreturn_vals);
}

LISP
script_fu_add_script (LISP a)
{
  SFScript *script;
  GParamDef *args;
  char *val;
  int i;
  gdouble color[3];
  LISP color_list;
298
  LISP adj_list;
Manish Singh's avatar
Manish Singh committed
299
  gchar *menu_path = NULL;
Elliot Lee's avatar
Elliot Lee committed
300 301 302

  /*  Check the length of a  */
  if (nlength (a) < 7)
303
    return my_err ("Too few arguments to script-fu-register", NIL);
Elliot Lee's avatar
Elliot Lee committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317

  /*  Create a new script  */
  script = g_new (SFScript, 1);

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

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

Manish Singh's avatar
Manish Singh committed
318 319 320 321
  /* Allow scripts with no menus */
  if (strncmp(val, "<None>", 6) != 0)
      menu_path = script->description;

Elliot Lee's avatar
Elliot Lee committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  /*  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;

358 359 360 361 362 363 364 365 366 367 368
  args = g_new (GParamDef, script->num_args + 1);
  args[0].type = PARAM_INT32;
  args[0].name = "run_mode";
  args[0].description = "Interactive, non-interactive";

  script->args_widgets = NULL;
  script->arg_types = g_new (SFArgType, script->num_args);
  script->arg_labels = g_new (char *, script->num_args);
  script->arg_defaults = g_new (SFArgValue, script->num_args);
  script->arg_values = g_new (SFArgValue, script->num_args);

Elliot Lee's avatar
Elliot Lee committed
369 370 371 372 373 374 375
  if (script->num_args > 0)
    {
      for (i = 0; i < script->num_args; i++)
	{
	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_flonum))
376
		return my_err ("script-fu-register: argument types must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
377 378 379 380
	      script->arg_types[i] = get_c_long (car (a));
	      a = cdr (a);
	    }
	  else
381
	    return my_err ("script-fu-register: missing type specifier", NIL);
Elliot Lee's avatar
Elliot Lee committed
382 383 384 385

	  if (a != NIL)
	    {
	      if (!TYPEP (car (a), tc_string))
386
		return my_err ("script-fu-register: argument labels must be strings", NIL);
Elliot Lee's avatar
Elliot Lee committed
387 388 389 390
	      script->arg_labels[i] = g_strdup (get_c_string (car (a)));
	      a = cdr (a);
	    }
	  else
391
	    return my_err ("script-fu-register: missing arguments label", NIL);
Elliot Lee's avatar
Elliot Lee committed
392 393 394 395 396 397 398 399 400 401

	  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))
402
		    return my_err ("script-fu-register: drawable defaults must be integer values", NIL);
Elliot Lee's avatar
Elliot Lee committed
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 431 432
		  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:
		      args[i + 1].type = PARAM_IMAGE;
		      args[i + 1].name = "image";
		      break;
		    case SF_DRAWABLE:
		      args[i + 1].type = PARAM_DRAWABLE;
		      args[i + 1].name = "drawable";
		      break;
		    case SF_LAYER:
		      args[i + 1].type = PARAM_LAYER;
		      args[i + 1].name = "layer";
		      break;
		    case SF_CHANNEL:
		      args[i + 1].type = PARAM_CHANNEL;
		      args[i + 1].name = "channel";
		      break;
		    default:
		      break;
		    }

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

		case SF_COLOR:
		  if (!TYPEP (car (a), tc_cons))
433
		    return my_err ("script-fu-register: color defaults must be a list of 3 integers", NIL);
Elliot Lee's avatar
Elliot Lee committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		  color_list = car (a);
		  color[0] = (gdouble) get_c_long (car (color_list)) / 255.0;
		  color_list = cdr (color_list);
		  color[1] = (gdouble) get_c_long (car (color_list)) / 255.0;
		  color_list = cdr (color_list);
		  color[2] = (gdouble) get_c_long (car (color_list)) / 255.0;
		  memcpy (script->arg_defaults[i].sfa_color.color, color, sizeof (gdouble) * 3);
		  memcpy (script->arg_values[i].sfa_color.color, color, sizeof (gdouble) * 3);
		  script->arg_values[i].sfa_color.preview = NULL;
		  script->arg_values[i].sfa_color.dialog = NULL;

		  args[i + 1].type = PARAM_COLOR;
		  args[i + 1].name = "color";
		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_TOGGLE:
		  if (!TYPEP (car (a), tc_flonum))
452
		    return my_err ("script-fu-register: toggle default must be an integer value", NIL);
Elliot Lee's avatar
Elliot Lee committed
453 454 455 456 457 458 459 460 461 462
		  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;

		  args[i + 1].type = PARAM_INT32;
		  args[i + 1].name = "toggle";
		  args[i + 1].description = script->arg_labels[i];
		  break;

		case SF_VALUE:
		  if (!TYPEP (car (a), tc_string))
463
		    return my_err ("script-fu-register: value defaults must be string values", NIL);
Elliot Lee's avatar
Elliot Lee committed
464 465 466 467 468 469
		  script->arg_defaults[i].sfa_value = g_strdup (get_c_string (car (a)));

		  args[i + 1].type = PARAM_STRING;
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;
470 471 472 473 474 475 476 477 478 479 480

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

		  args[i + 1].type = PARAM_STRING;
		  args[i + 1].name = "string";
		  args[i + 1].description = script->arg_labels[i];
		  break;

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
		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);
		  script->arg_defaults[i].sfa_adjustment.value = get_c_double (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.lower = get_c_double (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.upper = get_c_double (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.step = get_c_double (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.page = get_c_double (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.digits = get_c_long (car (adj_list));
		  adj_list = cdr (adj_list);
		  script->arg_defaults[i].sfa_adjustment.type = get_c_long (car (adj_list));
		  script->arg_values[i].sfa_adjustment.adj = NULL;
		  script->arg_values[i].sfa_adjustment.value = script->arg_defaults[i].sfa_adjustment.value;

		  args[i + 1].type = PARAM_STRING;
		  args[i + 1].name = "value";
		  args[i + 1].description = script->arg_labels[i];
		  break;

Elliot Lee's avatar
Elliot Lee committed
506 507 508 509 510 511 512
		default:
		  break;
		}

	      a = cdr (a);
	    }
	  else
513
	    return my_err ("script-fu-register: missing default argument", NIL);
Elliot Lee's avatar
Elliot Lee committed
514 515 516 517 518 519 520 521 522
	}
    }

  gimp_install_temp_proc (script->script_name,
			  script->description,
			  script->help,
			  script->author,
			  script->copyright,
			  script->date,
Manish Singh's avatar
Manish Singh committed
523
			  menu_path,
Elliot Lee's avatar
Elliot Lee committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			  script->img_types,
			  PROC_TEMPORARY,
			  script->num_args + 1, 0,
			  args, NULL,
			  script_fu_script_proc);

  g_free (args);

  script_list = g_list_append (script_list, script);

  return NIL;
}

void
script_fu_report_cc (gchar *command)
{
  if (last_command && strcmp (last_command, command) == 0)
    {
      char *new_command;

      new_command = g_new (gchar, strlen (command) + 10);
      sprintf (new_command, "%s <%d>", command, ++consec_command_count);
      if (current_command_enabled == TRUE)
547 548 549
	{
	  gtk_entry_set_text (GTK_ENTRY (sf_interface.status), new_command);
	}
Elliot Lee's avatar
Elliot Lee committed
550 551 552 553 554 555 556
      g_free (new_command);
      g_free (last_command);
    }
  else
    {
      consec_command_count = 1;
      if (current_command_enabled == TRUE)
557
	gtk_entry_set_text (GTK_ENTRY (sf_interface.status), command);
Elliot Lee's avatar
Elliot Lee committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
      if (last_command)
	g_free (last_command);
    }
  last_command = g_strdup (command);
  command_count++;

  if (current_command_enabled == TRUE)
    gdk_flush ();
}

static void
script_fu_script_proc (char     *name,
		       int       nparams,
		       GParam   *params,
		       int      *nreturn_vals,
		       GParam  **return_vals)
{
  static GParam values[1];
  GStatusType status = STATUS_SUCCESS;
  GRunModeType run_mode;
  SFScript *script;
579
  int min_args;
Elliot Lee's avatar
Elliot Lee committed
580 581 582 583 584 585 586

  run_mode = params[0].data.d_int32;

  if (! (script = script_fu_find_script (name)))
    status = STATUS_CALLING_ERROR;
  else
    {
587 588 589
      if (script->num_args == 0)
	run_mode = RUN_NONINTERACTIVE;

Elliot Lee's avatar
Elliot Lee committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
      switch (run_mode)
	{
	case RUN_INTERACTIVE:
	case RUN_WITH_LAST_VALS:
	  /*  Determine whether the script is image based (runs on an image)  */
	  if (strncmp (script->description, "<Image>", 7) == 0)
	    {
	      script->arg_values[0].sfa_image = params[1].data.d_image;
	      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  */
605 606 607 608 609 610
	  /*  Skip this part if the script takes no parameters */ 
	  min_args = (script->image_based) ? 2 : 0;
	  if (script->num_args > min_args) {
	    script_fu_interface (script); 
	    break;
	  }
Elliot Lee's avatar
Elliot Lee committed
611 612 613 614 615 616 617 618

	case RUN_NONINTERACTIVE:
	  /*  Make sure all the arguments are there!  */
	  if (nparams != (script->num_args + 1))
	    status = STATUS_CALLING_ERROR;
	  if (status == STATUS_SUCCESS)
	    {
	      gint err_msg;
Manish Singh's avatar
Manish Singh committed
619
	      char *text = NULL;
Elliot Lee's avatar
Elliot Lee committed
620
	      char *command, *c;
621
	      char buffer[MAX_STRING_LENGTH];
Elliot Lee's avatar
Elliot Lee committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	      int length;
	      int i;

	      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;
		  case SF_COLOR:
		    length += 16;  /*  Maximum size of color string: '(XXX XXX XXX)  */
		    break;
		  case SF_TOGGLE:
		    length += 6;   /*  Maximum size of (TRUE, FALSE)  */
		    break;
		  case SF_VALUE:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
645 646 647
		  case SF_STRING:
		    length += strlen (params[i + 1].data.d_string) + 3;
		    break;
648 649 650
		  case SF_ADJUSTMENT:
		    length += strlen (params[i + 1].data.d_string) + 1;
		    break;
Elliot Lee's avatar
Elliot Lee committed
651 652 653 654 655 656
		  default:
		    break;
		  }

	      c = command = g_new (char, length);

657 658
	      if (script->num_args)
	      {
Elliot Lee's avatar
Elliot Lee committed
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
	      sprintf (command, "(%s ", script->script_name);
	      c += strlen (script->script_name) + 2;
	      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:
		      sprintf (buffer, "%d", params[i + 1].data.d_image);
		      text = buffer;
		      break;
		    case SF_COLOR:
		      sprintf (buffer, "'(%d %d %d)",
			       params[i + 1].data.d_color.red,
			       params[i + 1].data.d_color.green,
			       params[i + 1].data.d_color.blue);
		      text = buffer;
		      break;
		    case SF_TOGGLE:
		      sprintf (buffer, "%s", (params[i + 1].data.d_int32) ? "TRUE" : "FALSE");
		      text = buffer;
		      break;
		    case SF_VALUE:
		      text = params[i + 1].data.d_string;
		      break;
686
		    case SF_STRING:
687
		      g_snprintf (buffer, MAX_STRING_LENGTH, "\"%s\"", params[i + 1].data.d_string);
688 689
		      text = buffer;
		      break;
690 691 692
		    case SF_ADJUSTMENT:
		      text = params[i + 1].data.d_string;
		      break;
Elliot Lee's avatar
Elliot Lee committed
693 694 695 696 697 698 699 700 701 702
		    default:
		      break;
		    }

		  if (i == script->num_args - 1)
		    sprintf (c, "%s)", text);
		  else
		    sprintf (c, "%s ", text);
		  c += strlen (text) + 1;
		}
703 704 705
	      }
	      else
		sprintf (command, "(%s)", script->script_name);
Elliot Lee's avatar
Elliot Lee committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

	      /*  run the command through the interpreter  */
	      err_msg = (repl_c_string (command, 0, 0, 1) != 0) ? TRUE : FALSE;

	      g_free (command);
	    }
	  break;

	default:
	  break;
	}
    }

  *nreturn_vals = 1;
  *return_vals = values;

  values[0].type = PARAM_STATUS;
  values[0].data.d_status = status;
}

static SFScript *
script_fu_find_script (gchar *script_name)
{
  GList *list;
  SFScript *script;

  list = script_list;
  while (list)
    {
      script = (SFScript *) list->data;
      if (! strcmp (script->script_name, script_name))
	return script;

      list = list->next;
    }

  return NULL;
}

static void
script_fu_free_script (SFScript *script)
{
  int i;

  /*  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;
	    case SF_VALUE:
	      g_free (script->arg_defaults[i].sfa_value);
	      break;
778 779 780
	    case SF_STRING:
	      g_free (script->arg_defaults[i].sfa_string);
	      break;
781 782
	    case SF_ADJUSTMENT:
	      break;
Elliot Lee's avatar
Elliot Lee committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	    default:
	      break;
	    }
	}

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

      g_free (script);
    }
}

static void
script_fu_enable_cc ()
{
  current_command_enabled = TRUE;
}

static void
script_fu_disable_cc (gint err_msg)
{
  if (err_msg)
806 807 808 809
    g_message ("Script-Fu Error\n%s\n"
              "If this happens while running a logo script,\n"
              "you might not have the font it wants installed on your system",
              siod_err_msg);
Elliot Lee's avatar
Elliot Lee committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

  current_command_enabled = FALSE;

  if (last_command)
    g_free (last_command);
  last_command = NULL;
  command_count = 0;
  consec_command_count = 0;
}

static void
script_fu_interface (SFScript *script)
{
  GtkWidget *dlg;
  GtkWidget *button;
  GtkWidget *label;
  GtkWidget *menu;
  GtkWidget *table;
828 829
  GtkWidget *vbox;
  GtkWidget *hbox;
Elliot Lee's avatar
Elliot Lee committed
830
  guchar *title;
831
  guchar *buf;
Elliot Lee's avatar
Elliot Lee committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  gchar **argv;
  gint argc;
  int start_args;
  int i;
  guchar *color_cube;

  static gint gtk_initted = FALSE;

  if (!gtk_initted)
    {
      argc = 1;
      argv = g_new (gchar *, 1);
      argv[0] = g_strdup ("script-fu");
      
      gtk_init (&argc, &argv);
      gtk_rc_parse (gimp_gtkrc ());
      
      gdk_set_use_xshm(gimp_use_xshm());
  
      gtk_preview_set_gamma(gimp_gamma());
      gtk_preview_set_install_cmap(gimp_install_cmap());
      color_cube = gimp_color_cube();
      gtk_preview_set_color_cube(color_cube[0], color_cube[1], color_cube[2], color_cube[3]);
      
      gtk_widget_set_default_visual(gtk_preview_get_visual());
      gtk_widget_set_default_colormap(gtk_preview_get_cmap());
      
      gtk_initted = TRUE;
    }

  sf_interface.script = script;
863 864 865 866 867 868 869 870 871
  
  title = g_new (guchar, strlen ("Script-Fu: ") + strlen (script->description));   
  buf = strstr (script->description, "Script-Fu/");
  if (buf)
    sprintf ((char *)title, "Script-Fu: %s", (buf + 10));
  else 
    sprintf ((char *)title, "Script-Fu: %s", script->description);

  dlg = gtk_window_new (GTK_WINDOW_TOPLEVEL);
872
  gtk_quit_add_destroy (1, GTK_OBJECT (dlg));
873
  gtk_window_set_title (GTK_WINDOW (dlg), (const gchar *)title);
874 875 876 877
  gtk_window_set_wmclass (GTK_WINDOW (dlg), "script_fu", "Gimp");
  gtk_signal_connect (GTK_OBJECT (dlg), "delete_event",
		      (GtkSignalFunc) script_fu_close_callback,
		      NULL);
Elliot Lee's avatar
Elliot Lee committed
878 879 880 881
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
		      (GtkSignalFunc) script_fu_close_callback,
		      NULL);

882 883 884 885
  /* the vbox holding all widgets */
  vbox = gtk_vbox_new (0,2);
  gtk_container_add (GTK_CONTAINER (dlg), vbox);
  gtk_container_border_width (GTK_CONTAINER (vbox), 2);
Elliot Lee's avatar
Elliot Lee committed
886

887
  /*  The info label  */
Elliot Lee's avatar
Elliot Lee committed
888 889
  label = gtk_label_new ("Script Arguments");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
890
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
Elliot Lee's avatar
Elliot Lee committed
891 892 893 894 895
  gtk_widget_show (label);

  /*  The argument table  */
  table = gtk_table_new (script->num_args, 2, FALSE);
  gtk_container_border_width (GTK_CONTAINER (table), 4);
896
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
Elliot Lee's avatar
Elliot Lee committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

  script->args_widgets = g_new (GtkWidget *, script->num_args);

  start_args = (script->image_based) ? 2 : 0;

  for (i = start_args; i < script->num_args; i++)
    {
      label = gtk_label_new (script->arg_labels[i]);
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_table_attach (GTK_TABLE (table), label,
			0, 1, i, i + 1, GTK_FILL, GTK_FILL, 4, 2);
      gtk_widget_show (label);

      switch (script->arg_types[i])
	{
	case SF_IMAGE:
	case SF_DRAWABLE:
	case SF_LAYER:
	case SF_CHANNEL:
	  script->args_widgets[i] = gtk_option_menu_new ();
	  switch (script->arg_types[i])
	    {
	    case SF_IMAGE:
	      menu = gimp_image_menu_new (NULL, script_fu_menu_callback,
					  &script->arg_values[i].sfa_image,
					  script->arg_defaults[i].sfa_image);
	      break;
	    case SF_DRAWABLE:
	      menu = gimp_drawable_menu_new (NULL, script_fu_menu_callback,
					     &script->arg_values[i].sfa_drawable,
					     script->arg_defaults[i].sfa_drawable);
	      break;
	    case SF_LAYER:
	      menu = gimp_layer_menu_new (NULL, script_fu_menu_callback,
					  &script->arg_values[i].sfa_layer,
					  script->arg_defaults[i].sfa_layer);
	      break;
	    case SF_CHANNEL:
	      menu = gimp_channel_menu_new (NULL, script_fu_menu_callback,
					    &script->arg_values[i].sfa_channel,
					    script->arg_defaults[i].sfa_channel);
	      break;
	    default:
	      menu = NULL;
	      break;
	    }
	  gtk_option_menu_set_menu (GTK_OPTION_MENU (script->args_widgets[i]), menu);
	  break;

	case SF_COLOR:
	  script->args_widgets[i] = gtk_button_new();

	  script->arg_values[i].sfa_color.preview = gtk_preview_new(GTK_PREVIEW_COLOR);
	  gtk_preview_size (GTK_PREVIEW (script->arg_values[i].sfa_color.preview),
			    COLOR_SAMPLE_WIDTH, COLOR_SAMPLE_HEIGHT);
	  gtk_container_add (GTK_CONTAINER (script->args_widgets[i]),
			     script->arg_values[i].sfa_color.preview);
	  gtk_widget_show (script->arg_values[i].sfa_color.preview);

	  script_fu_color_preview (script->arg_values[i].sfa_color.preview,
				   script->arg_values[i].sfa_color.color);

	  gtk_signal_connect (GTK_OBJECT (script->args_widgets[i]), "clicked",
960
			      (GtkSignalFunc) script_fu_color_preview_callback,
Elliot Lee's avatar
Elliot Lee committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
			      &script->arg_values[i].sfa_color);
	  break;

	case SF_TOGGLE:
	  gtk_label_set (GTK_LABEL (label), "Script Toggle");
	  script->args_widgets[i] = gtk_check_button_new_with_label (script->arg_labels[i]);
	  gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (script->args_widgets[i]),
				       script->arg_values[i].sfa_toggle);
	  gtk_signal_connect (GTK_OBJECT (script->args_widgets[i]), "toggled",
			      (GtkSignalFunc) script_fu_toggle_update,
			      &script->arg_values[i].sfa_toggle);
	  break;

	case SF_VALUE:
	  script->args_widgets[i] = gtk_entry_new ();
	  gtk_widget_set_usize (script->args_widgets[i], TEXT_WIDTH, 0);
	  gtk_entry_set_text (GTK_ENTRY (script->args_widgets[i]),
			      script->arg_defaults[i].sfa_value);
	  break;
980 981 982 983 984 985 986

	case SF_STRING:
	  script->args_widgets[i] = gtk_entry_new ();
	  gtk_widget_set_usize (script->args_widgets[i], TEXT_WIDTH, 0);
	  gtk_entry_set_text (GTK_ENTRY (script->args_widgets[i]),
			      script->arg_defaults[i].sfa_string);
	  break;
987 988 989 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 1016 1017 1018 1019 1020

	case SF_ADJUSTMENT:
	  script->arg_values[i].sfa_adjustment.adj = 
	    (GtkAdjustment *) gtk_adjustment_new (script->arg_values[i].sfa_adjustment.value, 
						  script->arg_defaults[i].sfa_adjustment.lower, 
						  script->arg_defaults[i].sfa_adjustment.upper, 
						  script->arg_defaults[i].sfa_adjustment.step, 
						  script->arg_defaults[i].sfa_adjustment.page, 0);
	  switch (script->arg_defaults[i].sfa_adjustment.type)
	    {
	    case SF_SLIDER:
	      script->args_widgets[i] = gtk_hscale_new (script->arg_values[i].sfa_adjustment.adj);
	      gtk_widget_set_usize (GTK_WIDGET (script->args_widgets[i]), 
				    SLIDER_WIDTH, SLIDER_HEIGHT);
	      gtk_scale_set_digits (GTK_SCALE (script->args_widgets[i]), 
				    script->arg_defaults[i].sfa_adjustment.digits);
	      gtk_scale_set_draw_value (GTK_SCALE (script->args_widgets[i]), TRUE);
	      gtk_range_set_update_policy (GTK_RANGE (script->args_widgets[i]), 
					   GTK_UPDATE_DELAYED);
	      break;
	    case SF_SPINNER:
	      script->args_widgets[i] = gtk_spin_button_new (script->arg_values[i].sfa_adjustment.adj, 0, 0);
	      gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (script->args_widgets[i]), TRUE);
	      gtk_widget_set_usize (script->args_widgets[i], SPINNER_WIDTH, 0);
	      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (script->args_widgets[i]),
					  script->arg_defaults[i].sfa_adjustment.digits);
	      gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (script->args_widgets[i]), TRUE);
	      break;
	    default: /* this shouldn't happen */
	      script->args_widgets[i] = NULL;
	      break;
	    }	  
	  break;

Elliot Lee's avatar
Elliot Lee committed
1021 1022 1023
	default:
	  break;
	}
1024 1025
      hbox = gtk_hbox_new (FALSE, 0);
      gtk_box_pack_start (GTK_BOX(hbox), script->args_widgets[i],
1026 1027
                          ((script->arg_types[i] == SF_VALUE) || (script->arg_types[i] == SF_STRING)),
                          ((script->arg_types[i] == SF_VALUE) || (script->arg_types[i] == SF_STRING)), 0);
1028 1029 1030 1031 1032 1033 1034
      gtk_widget_show (hbox);

      gtk_table_attach (GTK_TABLE (table), hbox, /* script->args_widgets[i], */
			1, 2, i, i + 1,
                        GTK_FILL | ((script->arg_types[i] == SF_VALUE) ?
                                                     GTK_EXPAND : 0),
                        GTK_FILL, 4, 2);
Elliot Lee's avatar
Elliot Lee committed
1035 1036 1037 1038
      gtk_widget_show (script->args_widgets[i]);
    }
  gtk_widget_show (table);

1039 1040 1041 1042
  /*  Action area  */
  hbox = gtk_hbox_new (0, 2);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
  gtk_container_border_width (GTK_CONTAINER (hbox), 2);
Elliot Lee's avatar
Elliot Lee committed
1043

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  button = gtk_button_new_with_label ("OK");
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
                      (GtkSignalFunc) script_fu_ok_callback,
                      NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_widget_grab_default (button);
  gtk_widget_show (button);

  button = gtk_button_new_with_label ("Cancel");
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
                      (GtkSignalFunc) script_fu_close_callback,
                      NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_widget_show (button);

  gtk_widget_show (hbox);

  /* The statusbar (well it's a faked statusbar...) */
  sf_interface.status = gtk_entry_new ();
  gtk_entry_set_editable (GTK_ENTRY (sf_interface.status), FALSE);
  gtk_box_pack_end (GTK_BOX (vbox), sf_interface.status, FALSE, TRUE, 0);
  gtk_entry_set_text (GTK_ENTRY (sf_interface.status), title);
  gtk_widget_show (sf_interface.status);

  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
1071 1072 1073
  gtk_widget_show (dlg);

  gtk_main ();
1074
  
Elliot Lee's avatar
Elliot Lee committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 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 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
  g_free (script->args_widgets);
  gdk_flush ();
}

static void
script_fu_color_preview (GtkWidget *preview,
			 gdouble   *color)
{
  gint i;
  guchar buf[3 * COLOR_SAMPLE_WIDTH];

  for (i = 0; i < COLOR_SAMPLE_WIDTH; i++)
    {
      buf[3*i] = (guint) (255.999 * color[0]);
      buf[3*i+1] = (guint) (255.999 * color[1]);
      buf[3*i+2] = (guint) (255.999 * color[2]);
    }
  for (i = 0; i < COLOR_SAMPLE_HEIGHT; i++)
    gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i, COLOR_SAMPLE_WIDTH);

  gtk_widget_draw (preview, NULL);
}

static void
script_fu_cleanup_widgets (SFScript *script)
{
  int i;

  for (i = 0; i < script->num_args; i++)
    switch (script->arg_types[i])
      {
      case SF_COLOR:
	if (script->arg_values[i].sfa_color.dialog != NULL)
	  {
	    gtk_widget_destroy (script->arg_values[i].sfa_color.dialog);
	    script->arg_values[i].sfa_color.dialog = NULL;
	  }
	break;
      default:
	break;
      }
}

static void
script_fu_ok_callback (GtkWidget *widget,
		       gpointer   data)
{
  SFScript *script;
  gint err_msg;
Manish Singh's avatar
Manish Singh committed
1124
  char *text = NULL;
Elliot Lee's avatar
Elliot Lee committed
1125
  char *command, *c;
1126
  char buffer[MAX_STRING_LENGTH];
Elliot Lee's avatar
Elliot Lee committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
  int length;
  int i;

  if ((script = sf_interface.script) == NULL)
    return;

  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;
      case SF_COLOR:
	length += 16;  /*  Maximum size of color string: '(XXX XXX XXX)  */
	break;
      case SF_TOGGLE:
	length += 6;   /*  Maximum size of (TRUE, FALSE)  */
	break;
      case SF_VALUE:
	length += strlen (gtk_entry_get_text (GTK_ENTRY (script->args_widgets[i]))) + 1;
	break;
1153 1154 1155
      case SF_STRING:
	length += strlen (gtk_entry_get_text (GTK_ENTRY (script->args_widgets[i]))) + 3;
	break;
1156 1157 1158
      case SF_ADJUSTMENT:
	length += 24;  /*  Maximum size of float value should not exceed this many characters  */
	break;
Elliot Lee's avatar
Elliot Lee committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
      default:
	break;
      }

  c = command = g_new (char, length);

  sprintf (command, "(%s ", script->script_name);
  c += strlen (script->script_name) + 2;
  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:
	  sprintf (buffer, "%d", script->arg_values[i].sfa_image);
	  text = buffer;
	  break;
	case SF_COLOR:
	  sprintf (buffer, "'(%d %d %d)",
		   (gint32) (script->arg_values[i].sfa_color.color[0] * 255.999),
		   (gint32) (script->arg_values[i].sfa_color.color[1] * 255.999),
		   (gint32) (script->arg_values[i].sfa_color.color[2] * 255.999));
	  text = buffer;
	  break;
	case SF_TOGGLE:
	  sprintf (buffer, "%s", (script->arg_values[i].sfa_toggle) ? "TRUE" : "FALSE");
	  text = buffer;
	  break;
	case SF_VALUE:
	  text = gtk_entry_get_text (GTK_ENTRY (script->args_widgets[i]));
	  break;
1192 1193
	case SF_STRING:
	  text = gtk_entry_get_text (GTK_ENTRY (script->args_widgets[i]));
1194
	  g_snprintf (buffer, MAX_STRING_LENGTH, "\"%s\"", text);
1195 1196
	  text = buffer;
	  break;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	case SF_ADJUSTMENT:
	  switch (script->arg_defaults[i].sfa_adjustment.type)
	    {
	    case SF_SLIDER:
	      g_snprintf (buffer, 24, "%f", script->arg_values[i].sfa_adjustment.adj->value);
	      text = buffer;
	      break;
	    case SF_SPINNER:
	      g_snprintf (buffer, 24, "%f", 
			  gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (script->args_widgets[i])));
	      text = buffer;
	      break;
	    default:
	      break;
	    }
	  break;
Elliot Lee's avatar
Elliot Lee committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	default:
	  break;
	}

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

  /*  enable the current command field  */
  script_fu_enable_cc ();

  /*  run the command through the interpreter  */
  err_msg = (repl_c_string (command, 0, 0, 1) != 0) ? TRUE : FALSE;

  /*  disable the current command field  */
  script_fu_disable_cc (err_msg);

  /* Clean up flying widgets before terminating Gtk */
  script_fu_cleanup_widgets(script);

  gtk_main_quit ();

  g_free (command);
}

static void
script_fu_close_callback (GtkWidget *widget,
			  gpointer   data)
{
  if (sf_interface.script != NULL)
    script_fu_cleanup_widgets(sf_interface.script);

  gtk_main_quit ();
}

static void
script_fu_menu_callback  (gint32     id,
			  gpointer   data)
{
  *((gint32 *) data) = id;
}

static void
script_fu_toggle_update (GtkWidget *widget,
			 gpointer   data)
{
  int *toggle_val;

  toggle_val = (int *) data;

  if (GTK_TOGGLE_BUTTON (widget)->active)
    *toggle_val = TRUE;
  else
    *toggle_val = FALSE;
}

static void
1273 1274
script_fu_color_preview_callback (GtkWidget *widget,
				  gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
1275 1276 1277 1278 1279
{
  GtkColorSelectionDialog *csd;
  SFColor *color;

  color = (SFColor *) data;
1280 1281 1282 1283
  color->old_color[0] = color->color[0];
  color->old_color[1] = color->color[1];
  color->old_color[2] = color->color[2];
      
Elliot Lee's avatar
Elliot Lee committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
  if (!color->dialog)
    {
      color->dialog = gtk_color_selection_dialog_new ("Script-Fu Color Picker");
      csd = GTK_COLOR_SELECTION_DIALOG (color->dialog);

      gtk_widget_destroy (csd->help_button);

      gtk_signal_connect_object (GTK_OBJECT (csd->ok_button), "clicked",
				 (GtkSignalFunc) gtk_widget_hide,
				 GTK_OBJECT (color->dialog));
1294
      gtk_signal_connect (GTK_OBJECT (csd), "delete_event",
1295
			  (GtkSignalFunc) script_fu_color_preview_delete,
1296
			  color);
Elliot Lee's avatar
Elliot Lee committed
1297
      gtk_signal_connect (GTK_OBJECT (csd->cancel_button), "clicked",
1298
			  (GtkSignalFunc) script_fu_color_preview_cancel,
Elliot Lee's avatar
Elliot Lee committed
1299 1300
			  color);
      gtk_signal_connect (GTK_OBJECT (csd->colorsel), "color_changed",
1301
			  (GtkSignalFunc) script_fu_color_preview_changed,
Elliot Lee's avatar
Elliot Lee committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
			  color);

      /* call here so the old color is set */
      gtk_color_selection_set_color (GTK_COLOR_SELECTION (csd->colorsel),
				     color->color);
    }
  else
    csd = GTK_COLOR_SELECTION_DIALOG (color->dialog);

  gtk_color_selection_set_color (GTK_COLOR_SELECTION (csd->colorsel),
				 color->color);

  gtk_window_position (GTK_WINDOW (color->dialog), GTK_WIN_POS_MOUSE);
  gtk_widget_show (color->dialog);
}

static void
1319 1320
script_fu_color_preview_changed (GtkWidget *widget,
				 gpointer data)
Elliot Lee's avatar
Elliot Lee committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
{
  SFColor *color;

  color = (SFColor *) data;
  gtk_color_selection_get_color (GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (color->dialog)->colorsel),
				 color->color);
  script_fu_color_preview (color->preview, color->color);
}

static void
1331 1332
script_fu_color_preview_cancel (GtkWidget *widget,
				gpointer data)
Elliot Lee's avatar
Elliot Lee committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
{
  SFColor *color;

  color = (SFColor *) data;

  gtk_widget_hide(color->dialog);

  color->color[0] = color->old_color[0];
  color->color[1] = color->old_color[1];
  color->color[2] = color->old_color[2];

  script_fu_color_preview (color->preview, color->color);
}
1346 1347

static gint
1348 1349 1350
script_fu_color_preview_delete (GtkWidget *widget,
				GdkEvent *event,
				gpointer data)
1351
{
1352
  script_fu_color_preview_cancel (widget, data);
1353 1354
  return TRUE;
}