[gimp] plug-ins: port file-aa to GimpProcedureConfig



commit 93bfd6f1547428571573ac53bc11b799f0384370
Author: Michael Natterer <mitch gimp org>
Date:   Tue Oct 8 01:04:59 2019 +0200

    plug-ins: port file-aa to GimpProcedureConfig
    
    Now takes an integer index into the aa_formats array instead of the
    format's name. "4" is about as magic as "weird aallib format name #4"
    so no harm done.

 plug-ins/common/file-aa.c | 201 ++++++++++++++++++++--------------------------
 1 file changed, 86 insertions(+), 115 deletions(-)
---
diff --git a/plug-ins/common/file-aa.c b/plug-ins/common/file-aa.c
index 606cfe58e0..679b6d8af9 100644
--- a/plug-ins/common/file-aa.c
+++ b/plug-ins/common/file-aa.c
@@ -1,13 +1,3 @@
-/**
- * aa.c version 1.0
- * A plugin that uses libaa (ftp://ftp.ta.jcu.cz/pub/aa) to save images as
- * ASCII.
- * NOTE: This plugin *requires* aalib 1.2 or later. Earlier versions will
- * not work.
- * Code copied from all over the GIMP source.
- * Tim Newsome <nuisance cmu edu>
- */
-
 /* GIMP - The GNU Image Manipulation Program
  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  *
@@ -25,6 +15,16 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+/**
+ * aa.c version 1.0
+ * A plugin that uses libaa (ftp://ftp.ta.jcu.cz/pub/aa) to save images as
+ * ASCII.
+ * NOTE: This plugin *requires* aalib 1.2 or later. Earlier versions will
+ * not work.
+ * Code copied from all over the GIMP source.
+ * Tim Newsome <nuisance cmu edu>
+ */
+
 #include "config.h"
 
 #include <string.h>
@@ -39,7 +39,6 @@
 
 #define SAVE_PROC      "file-aa-save"
 #define PLUG_IN_BINARY "file-aa"
-#define PLUG_IN_ROLE   "gimp-file-aa"
 
 
 typedef struct _Ascii      Ascii;
@@ -63,23 +62,25 @@ GType                   ascii_get_type         (void) G_GNUC_CONST;
 
 static GList          * ascii_query_procedures (GimpPlugIn           *plug_in);
 static GimpProcedure  * ascii_create_procedure (GimpPlugIn           *plug_in,
-                                               const gchar          *name);
+                                                const gchar          *name);
 
 static GimpValueArray * ascii_save             (GimpProcedure        *procedure,
-                                               GimpRunMode           run_mode,
-                                               GimpImage            *image,
-                                               GimpDrawable         *drawable,
-                                               GFile                *file,
-                                               const GimpValueArray *args,
-                                               gpointer              run_data);
+                                                GimpRunMode           run_mode,
+                                                GimpImage            *image,
+                                                GimpDrawable         *drawable,
+                                                GFile                *file,
+                                                const GimpValueArray *args,
+                                                gpointer              run_data);
 
-static gboolean         save_aa               (GimpDrawable         *drawable,
-                                               gchar                *filename,
-                                               gint                  output_type);
-static void             gimp2aa               (GimpDrawable         *drawable,
-                                               aa_context           *context);
+static gboolean         save_aa                (GFile                *file,
+                                                GimpDrawable         *drawable,
+                                                GObject              *config,
+                                                GError              **error);
+static void             gimp2aa                (GimpDrawable         *drawable,
+                                                aa_context           *context);
 
-static gint             aa_dialog             (gint                  selected);
+static gboolean         save_dialog            (GimpProcedure        *procedure,
+                                                GObject              *config);
 
 
 G_DEFINE_TYPE (Ascii, ascii, GIMP_TYPE_PLUG_IN)
@@ -115,6 +116,8 @@ ascii_create_procedure (GimpPlugIn  *plug_in,
 
   if (! strcmp (name, SAVE_PROC))
     {
+      gint i;
+
       procedure = gimp_save_procedure_new (plug_in, name,
                                            GIMP_PDB_PROC_TYPE_PLUGIN,
                                            ascii_save, NULL, NULL);
@@ -140,37 +143,16 @@ ascii_create_procedure (GimpPlugIn  *plug_in,
       gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
                                           "txt,ansi,text");
 
-      GIMP_PROC_ARG_STRING (procedure, "file-type",
-                            "File type",
-                            "File type to use",
-                            NULL,
-                            G_PARAM_READWRITE);
-    }
-
-  return procedure;
-}
-
-/**
- * Searches aa_formats defined by aalib to find the index of the type
- * specified by string.
- * -1 means it wasn't found.
- */
-static gint
-get_type_from_string (const gchar *string)
-{
-  gint type = 0;
-  aa_format **p = (aa_format **) aa_formats;
+      for (i = 0; aa_formats[i]; i++);
 
-  while (*p && strcmp ((*p)->formatname, string))
-    {
-      p++;
-      type++;
+      GIMP_PROC_ARG_INT (procedure, "file-type",
+                         "File type",
+                         "File type to use",
+                         0, i, 0,
+                         G_PARAM_READWRITE);
     }
 
-  if (*p == NULL)
-    return -1;
-
-  return type;
+  return procedure;
 }
 
 static GimpValueArray *
@@ -182,14 +164,17 @@ ascii_save (GimpProcedure        *procedure,
             const GimpValueArray *args,
             gpointer              run_data)
 {
-  GimpPDBStatusType  status      = GIMP_PDB_SUCCESS;
-  GimpExportReturn   export      = GIMP_EXPORT_CANCEL;
-  gint               output_type = 0;
-  GError            *error       = NULL;
+  GimpProcedureConfig *config;
+  GimpPDBStatusType    status = GIMP_PDB_SUCCESS;
+  GimpExportReturn     export = GIMP_EXPORT_CANCEL;
+  GError              *error  = NULL;
 
   INIT_I18N ();
   gegl_init (NULL, NULL);
 
+  config = gimp_procedure_create_config (procedure);
+  gimp_procedure_config_begin_run (config, image, run_mode, args);
+
   switch (run_mode)
     {
     case GIMP_RUN_INTERACTIVE:
@@ -212,72 +197,59 @@ ascii_save (GimpProcedure        *procedure,
       break;
     }
 
-  switch (run_mode)
+  if (run_mode == GIMP_RUN_INTERACTIVE)
     {
-    case GIMP_RUN_INTERACTIVE:
-      gimp_get_data (SAVE_PROC, &output_type);
-      output_type = aa_dialog (output_type);
-      if (output_type < 0)
+      if (! save_dialog (procedure, G_OBJECT (config)))
         status = GIMP_PDB_CANCEL;
-      break;
-
-    case GIMP_RUN_NONINTERACTIVE:
-      output_type = get_type_from_string (GIMP_VALUES_GET_STRING (args, 0));
-      if (output_type < 0)
-        status = GIMP_PDB_CALLING_ERROR;
-      break;
-
-    case GIMP_RUN_WITH_LAST_VALS:
-      gimp_get_data (SAVE_PROC, &output_type);
-      break;
-
-    default:
-      break;
     }
 
   if (status == GIMP_PDB_SUCCESS)
     {
-      if (save_aa (drawable, g_file_get_path (file), output_type))
-        {
-          gimp_set_data (SAVE_PROC, &output_type, sizeof (output_type));
-        }
-      else
+      if (! save_aa (file, drawable, G_OBJECT (config), &error))
         {
           status = GIMP_PDB_EXECUTION_ERROR;
         }
     }
 
+  gimp_procedure_config_end_run (config, status);
+  g_object_unref (config);
+
   if (export == GIMP_EXPORT_EXPORT)
     gimp_image_delete (image);
 
   return gimp_procedure_new_return_values (procedure, status, error);
 }
 
-/**
- * The actual save function. What it's all about.
- * The image type has to be GRAY.
- */
 static gboolean
-save_aa (GimpDrawable *drawable,
-         gchar        *filename,
-         gint          output_type)
+save_aa (GFile         *file,
+         GimpDrawable  *drawable,
+         GObject       *config,
+         GError       **error)
 {
   aa_savedata  savedata;
   aa_context  *context;
-  aa_format    format = *aa_formats[output_type];
+  aa_format    format;
+  gint         output_type;
+
+  g_object_get (config,
+                "file-type", &output_type,
+                NULL);
+
+  memcpy (&format, aa_formats[output_type], sizeof (aa_format));
 
   format.width  = gimp_drawable_width  (drawable) / 2;
   format.height = gimp_drawable_height (drawable) / 2;
 
   /* Get a libaa context which will save its output to filename. */
-  savedata.name   = filename;
+  savedata.name   = g_file_get_path (file);
   savedata.format = &format;
 
   context = aa_init (&save_d, &aa_defparams, &savedata);
-  if (!context)
+  if (! context)
     return FALSE;
 
   gimp2aa (drawable, context);
+
   aa_flush (context);
   aa_close (context);
 
@@ -380,17 +352,21 @@ gimp2aa (GimpDrawable *drawable,
              aa_scrwidth (context), aa_scrheight (context));
 }
 
-static gint
-aa_dialog (gint selected)
+static gboolean
+save_dialog (GimpProcedure *procedure,
+             GObject       *config)
 {
-  GtkWidget *dialog;
-  GtkWidget *hbox;
-  GtkWidget *label;
-  GtkWidget *combo;
-  gint       i;
-
-  /* Create the actual window. */
-  dialog = gimp_export_dialog_new (_("Text"), PLUG_IN_BINARY, SAVE_PROC);
+  GtkWidget    *dialog;
+  GtkWidget    *hbox;
+  GtkWidget    *label;
+  GtkListStore *store;
+  GtkWidget    *combo;
+  gint          i;
+  gboolean      run;
+
+  dialog = gimp_procedure_dialog_new (procedure,
+                                      GIMP_PROCEDURE_CONFIG (config),
+                                      _("Export Image as Text"));
 
   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
   gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
@@ -402,28 +378,23 @@ aa_dialog (gint selected)
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   gtk_widget_show (label);
 
-  combo = g_object_new (GIMP_TYPE_INT_COMBO_BOX, NULL);
-  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
-  gtk_widget_show (combo);
-
-  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
+  store = g_object_new (GIMP_TYPE_INT_STORE, NULL);
 
   for (i = 0; aa_formats[i]; i++)
-    gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (combo),
-                               GIMP_INT_STORE_VALUE, i,
-                               GIMP_INT_STORE_LABEL, aa_formats[i]->formatname,
-                               -1);
+    gtk_list_store_insert_with_values (store, NULL, -1,
+                                       GIMP_INT_STORE_VALUE, i,
+                                       GIMP_INT_STORE_LABEL, aa_formats[i]->formatname,
+                                       -1);
 
-  gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), selected,
-                              G_CALLBACK (gimp_int_combo_box_get_active),
-                              &selected, NULL);
+  combo = gimp_prop_int_combo_box_new (config, "file-type",
+                                       GIMP_INT_STORE (store));
+  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
 
   gtk_widget_show (dialog);
 
-  if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK)
-    selected = -1;
+  run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
 
   gtk_widget_destroy (dialog);
 
-  return selected;
+  return run;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]