File: script-fu-console.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (561 lines) | stat: -rw-r--r-- 14,011 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
174
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"

#include "script-fu-intl.h"

#include "siod-wrapper.h"
#include "script-fu-console.h"

#include <plug-ins/dbbrowser/gimpprocbrowser.h>


#define TEXT_WIDTH  480
#define TEXT_HEIGHT 400

#define BUFSIZE     256


typedef struct
{
  GtkTextBuffer *console;
  GtkWidget     *cc;
  GtkWidget     *text_view;

  gint32         input_id;
} ConsoleInterface;


/*
 *  Local Functions
 */
static void       script_fu_console_interface  (void);
static void       script_fu_response           (GtkWidget    *widget,
                                                gint          response_id,
						gpointer      data);
static void       script_fu_browse_callback    (GtkWidget    *widget,
						gpointer      data);
static gboolean   script_fu_cc_is_empty        (void);
static gboolean   script_fu_cc_key_function    (GtkWidget    *widget,
						GdkEventKey  *event,
						gpointer      data);

static void       script_fu_open_siod_console  (void);
static void       script_fu_close_siod_console (void);


/*
 *  Local variables
 */
static ConsoleInterface cint =
{
  NULL,  /*  console  */
  NULL,  /*  current command  */
  NULL,  /*  text view  */

  -1     /*  input id  */
};

static GList *history     = NULL;
static gint   history_len = 0;
static gint   history_cur = 0;
static gint   history_max = 50;


/*
 *  Function definitions
 */

void
script_fu_console_run (const gchar      *name,
		       gint              nparams,
		       const GimpParam  *params,
		       gint             *nreturn_vals,
		       GimpParam       **return_vals)
{
  static GimpParam  values[1];
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  GimpRunMode       run_mode;

  run_mode = params[0].data.d_int32;

  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
      /*  Enable SIOD output  */
      script_fu_open_siod_console ();

      /*  Run the interface  */
      script_fu_console_interface ();

      /*  Clean up  */
      script_fu_close_siod_console ();
      break;

    case GIMP_RUN_WITH_LAST_VALS:
    case GIMP_RUN_NONINTERACTIVE:
      status = GIMP_PDB_CALLING_ERROR;
      g_message (_("Script-Fu console mode allows only interactive invocation"));
      break;

    default:
      break;
    }

  *nreturn_vals = 1;
  *return_vals = values;

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

static void
script_fu_console_interface (void)
{
  GtkWidget  *dialog;
  GtkWidget  *main_vbox;
  GtkWidget  *vbox;
  GtkWidget  *button;
  GtkWidget  *label;
  GtkWidget  *scrolled_window;
  GtkWidget  *hbox;

  gimp_ui_init ("script-fu", FALSE);

  dialog = gimp_dialog_new (_("Script-Fu Console"), "script-fu-console",
                            NULL, 0,
			    gimp_standard_help_func,
                            "plug-in-script-fu-console",

			    GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,

			    NULL);

  g_signal_connect (dialog, "response",
		    G_CALLBACK (script_fu_response),
		    NULL);
  g_signal_connect (dialog, "destroy",
		    G_CALLBACK (gtk_widget_destroyed),
		    &dialog);

  /*  The main vbox  */
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), main_vbox,
		      TRUE, TRUE, 0);
  gtk_widget_show (main_vbox);

  vbox = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (main_vbox), vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);

  label = gtk_label_new (_("SIOD Output"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /*  The output text widget  */
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_ALWAYS);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_window);

  cint.console = gtk_text_buffer_new (NULL);
  cint.text_view = gtk_text_view_new_with_buffer (cint.console);
  g_object_unref (cint.console);

  gtk_text_view_set_editable (GTK_TEXT_VIEW (cint.text_view), FALSE);
  gtk_text_view_set_left_margin (GTK_TEXT_VIEW (cint.text_view), 12);
  gtk_text_view_set_right_margin (GTK_TEXT_VIEW (cint.text_view), 12);
  gtk_widget_set_size_request (cint.text_view, TEXT_WIDTH, TEXT_HEIGHT);
  gtk_container_add (GTK_CONTAINER (scrolled_window), cint.text_view);
  gtk_widget_show (cint.text_view);

  gtk_text_buffer_create_tag (cint.console, "strong",
			      "weight", PANGO_WEIGHT_BOLD,
			      "size",   12 * PANGO_SCALE,
			      NULL);
  gtk_text_buffer_create_tag (cint.console, "emphasis",
			      "style",  PANGO_STYLE_OBLIQUE,
			      "size",   10 * PANGO_SCALE,
			      NULL);
  gtk_text_buffer_create_tag (cint.console, "weak",
			      "size",   10 * PANGO_SCALE,
			      NULL);

  {
    const gchar *greeting_texts[] =
    {
      "weak",     "\n",
      "strong",   "Welcome to SIOD, Scheme In One Defun\n",
      "weak",     "(C) Copyright 1988-1994 Paradigm Associates Inc.\n\n",
      "strong",   "Script-Fu Console - ",
      "emphasis", "Interactive Scheme Development\n",
      NULL
    };

    GtkTextIter cursor;
    gint        i;

    gtk_text_buffer_get_end_iter (cint.console, &cursor);

    for (i = 0; greeting_texts[i]; i += 2)
      {
	gtk_text_buffer_insert_with_tags_by_name (cint.console, &cursor,
						  greeting_texts[i + 1], -1,
						  greeting_texts[i],
						  NULL);
      }
  }

  /*  The current command  */
  vbox = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);

  label = gtk_label_new (_("Current Command"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  cint.cc = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (hbox), cint.cc, TRUE, TRUE, 0);
  gtk_widget_grab_focus (cint.cc);
  gtk_widget_show (cint.cc);

  g_signal_connect (cint.cc, "key_press_event",
		    G_CALLBACK (script_fu_cc_key_function),
		    NULL);

  button = gtk_button_new_with_mnemonic (_("_Browse..."));
  gtk_misc_set_padding (GTK_MISC (GTK_BIN (button)->child), 2, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
  gtk_widget_show (button);

  g_signal_connect (button, "clicked",
		    G_CALLBACK (script_fu_browse_callback),
		    NULL);

  /*  Initialize the history  */
  history     = g_list_append (history, NULL);
  history_len = 1;

  gtk_widget_show (dialog);

  gtk_main ();

  g_source_remove (cint.input_id);

  if (dialog)
    gtk_widget_destroy (dialog);
}

static void
script_fu_response (GtkWidget *widget,
                    gint       response_id,
                    gpointer   data)
{
  gtk_main_quit ();
}

static void
apply_callback (const gchar        *proc_name,
		const gchar        *proc_blurb,
		const gchar        *proc_help,
		const gchar        *proc_author,
		const gchar        *proc_copyright,
		const gchar        *proc_date,
		GimpPDBProcType     proc_type,
		gint                n_params,
		gint                n_return_vals,
		const GimpParamDef *params,
		const GimpParamDef *return_vals,
		gpointer            user_data)
{
  gint     i;
  GString *text;

  if (proc_name == NULL)
    return;

  text = g_string_new ("(");
  text = g_string_append (text, proc_name);

  for (i = 0; i < n_params; i++)
    {
      text = g_string_append_c (text, ' ');
      text = g_string_append (text, params[i].name);
    }

  text = g_string_append_c (text, ')');

  gtk_entry_set_text (GTK_ENTRY (cint.cc), text->str);
  g_string_free (text, TRUE);
}

static void
script_fu_browse_callback (GtkWidget *widget,
			   gpointer   data)
{
  gtk_quit_add_destroy (1, (GtkObject *)
                        gimp_proc_browser_dialog_new (TRUE, apply_callback, NULL));
}

static gboolean
script_fu_console_idle_scroll_end (gpointer data)
{
  GtkAdjustment *adj = GTK_ADJUSTMENT (data);

  gtk_adjustment_set_value (adj, adj->upper - adj->page_size);

  return FALSE;
}

static void
script_fu_console_scroll_end (void)
{
  /*  the text view idle updates so we need to idle scroll too
   */
  g_idle_add (script_fu_console_idle_scroll_end,
              GTK_TEXT_VIEW (cint.text_view)->vadjustment);
}

void
script_fu_output_to_console (gchar *text)
{
  GtkTextIter cursor;

  gtk_text_buffer_get_end_iter (cint.console, &cursor);
  gtk_text_buffer_insert_with_tags_by_name (cint.console, &cursor,
                                            text, -1,
                                            "weak",
                                            NULL);

  script_fu_console_scroll_end ();
}

static gboolean
script_fu_cc_is_empty (void)
{
  const gchar *str;

  if ((str = gtk_entry_get_text (GTK_ENTRY (cint.cc))) == NULL)
    return TRUE;

  while (*str)
    {
      if (*str != ' ' && *str != '\t' && *str != '\n')
	return FALSE;

      str ++;
    }

  return TRUE;
}

static gboolean
script_fu_cc_key_function (GtkWidget   *widget,
			   GdkEventKey *event,
			   gpointer     data)
{
  GList       *list;
  gint         direction = 0;
  GtkTextIter  cursor;

  switch (event->keyval)
    {
    case GDK_Return:
      if (script_fu_cc_is_empty ())
	return TRUE;

      list = g_list_nth (history, (g_list_length (history) - 1));
      if (list->data)
	g_free (list->data);
      list->data = g_strdup (gtk_entry_get_text (GTK_ENTRY (cint.cc)));

      gtk_text_buffer_get_end_iter (cint.console, &cursor);

      gtk_text_buffer_insert_with_tags_by_name (cint.console, &cursor,
						"\n=> ", -1,
						"strong",
						NULL);

      gtk_text_buffer_insert_with_tags_by_name (cint.console, &cursor,
                                                gtk_entry_get_text (GTK_ENTRY (cint.cc)), -1,
                                                "weak",
                                                NULL);

      gtk_text_buffer_insert_with_tags_by_name (cint.console, &cursor,
						"\n", -1,
						"weak",
						NULL);

      script_fu_console_scroll_end ();

      gtk_entry_set_text (GTK_ENTRY (cint.cc), "");

      siod_interpret_string ((char *) list->data);
      gimp_displays_flush ();

      history = g_list_append (history, NULL);
      if (history_len == history_max)
	{
	  history = g_list_remove (history, history->data);
	  if (history->data)
	    g_free (history->data);
	}
      else
	history_len++;
      history_cur = g_list_length (history) - 1;

      return TRUE;
      break;

    case GDK_KP_Up:
    case GDK_Up:
      direction = -1;
      break;

    case GDK_KP_Down:
    case GDK_Down:
      direction = 1;
      break;

    case GDK_P:
    case GDK_p:
      if (event->state & GDK_CONTROL_MASK)
        direction = -1;
      break;

    case GDK_N:
    case GDK_n:
      if (event->state & GDK_CONTROL_MASK)
        direction = 1;
      break;

    default:
      break;
    }

  if (direction)
    {
      /*  Make sure we keep track of the current one  */
      if (history_cur == g_list_length (history) - 1)
	{
	  list = g_list_nth (history, history_cur);
	  if (list->data)
	    g_free (list->data);
	  list->data = g_strdup (gtk_entry_get_text (GTK_ENTRY (cint.cc)));
	}

      history_cur += direction;
      if (history_cur < 0)
	history_cur = 0;
      if (history_cur >= history_len)
	history_cur = history_len - 1;

      gtk_entry_set_text (GTK_ENTRY (cint.cc),
			  (gchar *) (g_list_nth (history, history_cur))->data);

      gtk_editable_set_position (GTK_EDITABLE (cint.cc), -1);

      return TRUE;
    }

  return FALSE;
}

static void
script_fu_open_siod_console (void)
{
  siod_set_console_mode (1);
  siod_set_verbose_level (2);
}

static void
script_fu_close_siod_console (void)
{
  FILE *siod_output;

  siod_output = siod_get_output_file ();

  if (siod_output != stdout)
    fclose (siod_output);

  siod_set_console_mode (0);
}

void
script_fu_eval_run (const gchar      *name,
		    gint              nparams,
		    const GimpParam  *params,
		    gint             *nreturn_vals,
		    GimpParam       **return_vals)
{
  static GimpParam  values[1];
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  GimpRunMode   run_mode;

  run_mode = params[0].data.d_int32;

  switch (run_mode)
    {
    case GIMP_RUN_NONINTERACTIVE:
      if (siod_interpret_string (params[1].data.d_string) != 0)
	status = GIMP_PDB_EXECUTION_ERROR;
      break;

    case GIMP_RUN_INTERACTIVE:
    case GIMP_RUN_WITH_LAST_VALS:
      status = GIMP_PDB_CALLING_ERROR;
      g_message (_("Script-Fu evaluate mode allows only "
                   "noninteractive invocation"));
      break;

    default:
      break;
    }

  *nreturn_vals = 1;
  *return_vals = values;

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