/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-2003 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.
 */

/* NOTE: This file is autogenerated by pdbgen.pl. */

#include "config.h"

#include <string.h>

#include <glib-object.h>

#include "pdb-types.h"
#include "procedural_db.h"

#include "base/temp-buf.h"
#include "core/gimp.h"
#include "core/gimpcontainer-filter.h"
#include "core/gimpcontext.h"
#include "core/gimpdatafactory.h"
#include "core/gimplist.h"
#include "core/gimppattern.h"

static ProcRecord patterns_refresh_proc;
static ProcRecord patterns_get_list_proc;
static ProcRecord patterns_get_pattern_proc;
static ProcRecord patterns_get_pattern_data_proc;

void
register_patterns_procs (Gimp *gimp)
{
  procedural_db_register (gimp, &patterns_refresh_proc);
  procedural_db_register (gimp, &patterns_get_list_proc);
  procedural_db_register (gimp, &patterns_get_pattern_proc);
  procedural_db_register (gimp, &patterns_get_pattern_data_proc);
}

static Argument *
patterns_refresh_invoker (Gimp         *gimp,
                          GimpContext  *context,
                          GimpProgress *progress,
                          Argument     *args)
{
  gimp_data_factory_data_save (gimp->pattern_factory);
  gimp_data_factory_data_init (gimp->pattern_factory, FALSE);
  return procedural_db_return_args (&patterns_refresh_proc, TRUE);
}

static ProcRecord patterns_refresh_proc =
{
  "gimp_patterns_refresh",
  "Refresh current patterns. This function always succeeds.",
  "This procedure retrieves all patterns currently in the user's pattern path and updates all pattern dialogs accordingly.",
  "Michael Natterer",
  "Michael Natterer",
  "2002",
  NULL,
  GIMP_INTERNAL,
  0,
  NULL,
  0,
  NULL,
  { { patterns_refresh_invoker } }
};

static Argument *
patterns_get_list_invoker (Gimp         *gimp,
                           GimpContext  *context,
                           GimpProgress *progress,
                           Argument     *args)
{
  gboolean success = TRUE;
  Argument *return_args;
  gchar *filter;
  gint32 num_patterns;
  gchar **pattern_list = NULL;

  filter = (gchar *) args[0].value.pdb_pointer;
  if (filter && !g_utf8_validate (filter, -1, NULL))
    success = FALSE;

  if (success)
    pattern_list = gimp_container_get_filtered_name_array (gimp->pattern_factory->container, filter, &num_patterns);

  return_args = procedural_db_return_args (&patterns_get_list_proc, success);

  if (success)
    {
      return_args[1].value.pdb_int = num_patterns;
      return_args[2].value.pdb_pointer = pattern_list;
    }

  return return_args;
}

static ProcArg patterns_get_list_inargs[] =
{
  {
    GIMP_PDB_STRING,
    "filter",
    "An optional regular expression used to filter the list"
  }
};

static ProcArg patterns_get_list_outargs[] =
{
  {
    GIMP_PDB_INT32,
    "num_patterns",
    "The number of patterns in the pattern list"
  },
  {
    GIMP_PDB_STRINGARRAY,
    "pattern_list",
    "The list of pattern names"
  }
};

static ProcRecord patterns_get_list_proc =
{
  "gimp_patterns_get_list",
  "Retrieve a complete listing of the available patterns.",
  "This procedure returns a complete listing of available GIMP patterns. Each name returned can be used as input to the 'gimp_context_set_pattern'.",
  "Spencer Kimball & Peter Mattis",
  "Spencer Kimball & Peter Mattis",
  "1995-1996",
  NULL,
  GIMP_INTERNAL,
  1,
  patterns_get_list_inargs,
  2,
  patterns_get_list_outargs,
  { { patterns_get_list_invoker } }
};

static Argument *
patterns_get_pattern_invoker (Gimp         *gimp,
                              GimpContext  *context,
                              GimpProgress *progress,
                              Argument     *args)
{
  gboolean success = TRUE;
  Argument *return_args;
  GimpPattern *pattern;

  success = (pattern = gimp_context_get_pattern (context)) != NULL;

  return_args = procedural_db_return_args (&patterns_get_pattern_proc, success);

  if (success)
    {
      return_args[1].value.pdb_pointer = g_strdup (GIMP_OBJECT (pattern)->name);
      return_args[2].value.pdb_int = pattern->mask->width;
      return_args[3].value.pdb_int = pattern->mask->height;
    }

  return return_args;
}

static ProcArg patterns_get_pattern_outargs[] =
{
  {
    GIMP_PDB_STRING,
    "name",
    "The pattern name"
  },
  {
    GIMP_PDB_INT32,
    "width",
    "The pattern width"
  },
  {
    GIMP_PDB_INT32,
    "height",
    "The pattern height"
  }
};

static ProcRecord patterns_get_pattern_proc =
{
  "gimp_patterns_get_pattern",
  "This procedure is deprecated! Use 'gimp_context_get_pattern' instead.",
  "This procedure is deprecated! Use 'gimp_context_get_pattern' instead.",
  "",
  "",
  "",
  "gimp_context_get_pattern",
  GIMP_INTERNAL,
  0,
  NULL,
  3,
  patterns_get_pattern_outargs,
  { { patterns_get_pattern_invoker } }
};

static Argument *
patterns_get_pattern_data_invoker (Gimp         *gimp,
                                   GimpContext  *context,
                                   GimpProgress *progress,
                                   Argument     *args)
{
  gboolean success = TRUE;
  Argument *return_args;
  gchar *name;
  gint32 length = 0;
  guint8 *mask_data = NULL;
  GimpPattern *pattern = NULL;

  name = (gchar *) args[0].value.pdb_pointer;
  if (name && !g_utf8_validate (name, -1, NULL))
    success = FALSE;

  if (success)
    {
      if (name && strlen (name))
        {
          pattern = (GimpPattern *)
            gimp_container_get_child_by_name (gimp->pattern_factory->container,
                                              name);
        }
      else
        {
          pattern = gimp_context_get_pattern (context);
        }

      if (pattern)
        {
          length = pattern->mask->height * pattern->mask->width *
                   pattern->mask->bytes;
          mask_data = g_memdup (temp_buf_data (pattern->mask), length);
        }
      else
        success = FALSE;
    }

  return_args = procedural_db_return_args (&patterns_get_pattern_data_proc, success);

  if (success)
    {
      return_args[1].value.pdb_pointer = g_strdup (GIMP_OBJECT (pattern)->name);
      return_args[2].value.pdb_int = pattern->mask->width;
      return_args[3].value.pdb_int = pattern->mask->height;
      return_args[4].value.pdb_int = pattern->mask->bytes;
      return_args[5].value.pdb_int = length;
      return_args[6].value.pdb_pointer = mask_data;
    }

  return return_args;
}

static ProcArg patterns_get_pattern_data_inargs[] =
{
  {
    GIMP_PDB_STRING,
    "name",
    "The pattern name (\"\" means currently active pattern)"
  }
};

static ProcArg patterns_get_pattern_data_outargs[] =
{
  {
    GIMP_PDB_STRING,
    "name",
    "The pattern name"
  },
  {
    GIMP_PDB_INT32,
    "width",
    "The pattern width"
  },
  {
    GIMP_PDB_INT32,
    "height",
    "The pattern height"
  },
  {
    GIMP_PDB_INT32,
    "mask_bpp",
    "Pattern bytes per pixel"
  },
  {
    GIMP_PDB_INT32,
    "length",
    "Length of pattern mask data"
  },
  {
    GIMP_PDB_INT8ARRAY,
    "mask_data",
    "The pattern mask data"
  }
};

static ProcRecord patterns_get_pattern_data_proc =
{
  "gimp_patterns_get_pattern_data",
  "This procedure is deprecated! Use 'gimp_pattern_get_pixels' instead.",
  "This procedure is deprecated! Use 'gimp_pattern_get_pixels' instead.",
  "",
  "",
  "",
  "gimp_pattern_get_pixels",
  GIMP_INTERNAL,
  1,
  patterns_get_pattern_data_inargs,
  6,
  patterns_get_pattern_data_outargs,
  { { patterns_get_pattern_data_invoker } }
};
