# 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.

# "Perlized" from C source by Manish Singh <yosh@gimp.org>

sub pdb_misc {
    $author = $copyright = 'Adam D. Moss';
    $date = '1998';
}

sub image_add_guide {
    my ($desc, $help_desc, $type, $max, $pos) = @_;

    $blurb = "Add a $desc guide to an image.";

    $help = <<HELP;
This procedure adds a $desc guide to an image. It takes the input image and the
$type-position of the new guide as parameters. It returns the guide ID of the
new guide.
HELP

    &pdb_misc;

    @inargs = (
	&std_image_arg,
	{ name => "${type}position", type => '0 <= int32',
	  desc => "The guide's ${type}-offset from $pos of image",
	  alias => 'offset' }
    );

    @outargs = (
	{ name => 'guide', type => 'guide', init => 1, alias => 'guide_ID',
	  desc => 'The new guide' }
    );

    my $func = substr($desc, 0, 1);
    %invoke = (
	vars => [ 'GimpGuide *guide' ],
	code => <<CODE
{
  if (offset < gimage->$max)
    {
      guide = gimp_image_add_${func}guide (gimage);
      gimp_image_undo_push_image_guide (gimage, _("${help_desc}"), guide);
      guide->position = offset;
      guide_ID = guide->guide_ID;
    }
  else
    success = FALSE;
}
CODE
    );
}

# The defs

sub image_add_hguide {
    &image_add_guide('horizontal', 'Add Horizontal Guide', 'y', 'height', 'top');
}

sub image_add_vguide {
    &image_add_guide('vertical', 'Add Vertical Guide', 'x', 'width', 'left');
}

sub image_delete_guide {
    $blurb = 'Deletes a guide from an image.';

    $help = <<'HELP';
This procedure takes an image and a guide ID as input and removes the specified
guide from the specified image.
HELP

    &pdb_misc;

    @inargs = (
	&std_image_arg,
	{ name => 'guide', type => 'guide',
	  desc => 'The ID of the guide to be removed' }
    );

    %invoke = (
	vars => [ 'GList *guides' ],
	code => <<'CODE'
{
  success = FALSE;

  for (guides = gimage->guides; guides; guides = g_list_next (guides))
    {
      GimpGuide *g = (GimpGuide *) guides->data;

      if ((g->guide_ID == guide) && (g->position >= 0))
	{
	  gimp_image_undo_push_image_guide (gimage, _("Remove Guide"), g);
          gimp_image_delete_guide (gimage, g);

	  success = TRUE;
	  break;
	}
    }
}
CODE
    );
}

sub image_find_next_guide {
    $blurb = 'Find next guide on an image.';

    $help = <<'HELP';
This procedure takes an image and a guide ID as input and finds the guide ID of
the successor of the given guide ID in the image's guide list. If the supplied
guide ID is 0, the procedure will return the first Guide. The procedure will
return 0 if given the final guide ID as an argument or the image has no guides.
HELP

    &pdb_misc;

    @inargs = (
	&std_image_arg,
	{ name => 'guide', type => 'guide',
	  desc => 'The ID of the current guide (0 if first invocation)' }
    );

    @outargs = (
	{ name => 'next_guide', type => 'guide', init => 1,
	  desc => "The next guide's ID" }
    );

    %invoke = (
	vars => [ 'GList *guides', 'gboolean guide_found' ],
	code => <<'CODE'
{
  if (gimage->guides != NULL)
    {
      success     = FALSE;
      guide_found = FALSE;

      for (guides = gimage->guides; guides; guides = g_list_next (guides))
        {
          GimpGuide *g = (GimpGuide *) guides->data;

          if (g->position < 0)
            continue;

          if (guide == 0)  /* init - Return first guide ID in list */
            {
              next_guide = g->guide_ID;

              guide_found = TRUE;
              break;
            }

          if (! guide_found)
            {
              if (g->guide_ID == guide)
                guide_found = TRUE;
            }
          else
            {
              next_guide = g->guide_ID;
            }
        }

      if (guide_found)
        success = TRUE;
    }
}
CODE
    );
}

sub image_get_guide_orientation {
    $blurb = 'Get orientation of a guide on an image.';

    $help = <<'HELP';
This procedure takes an image and a guide ID as input and returns the
orientations of the guide.
HELP

    &pdb_misc;
    
    @inargs = (
	&std_image_arg,
	{ name => 'guide', type => 'guide',
	  desc => 'The guide' }
    );

    @outargs = (
	{ name => 'orientation', type => &std_orientation_enum, init => 1,
	  desc => "The guide's orientation: { %%desc%% }",
	  libdef => 'GIMP_ORIENTATION_UNKNOWN' }
    );

    %invoke = (
	vars => [ 'GList *guides' ],
	code => <<'CODE'
{
  success = FALSE;

  for (guides = gimage->guides; guides; guides = g_list_next (guides))
    {
      GimpGuide *g = (GimpGuide *) guides->data;

      if ((g->guide_ID == guide) && (g->position >= 0))
	{
	  orientation = g->orientation;

	  success = TRUE;
	  break;
	}
    }
}
CODE
    );
}

sub image_get_guide_position {
    $blurb = 'Get position of a guide on an image.';

    $help = <<'HELP';
This procedure takes an image and a guide ID as input and returns the position
of the guide relative to the top or left of the image.
HELP

    &pdb_misc;

    @inargs = (
	&std_image_arg,
	{ name => 'guide', type => 'guide',
	  desc => 'The guide' }
    );

    @outargs = (
	{ name => 'position', type => 'int32', init => 1, libdef => '-1',
	  desc => "The guide's position relative to top or left of image" }
    );

    %invoke = (
	vars => [ 'GList *guides' ],
	code => <<'CODE'
{
  success = FALSE;

  for (guides = gimage->guides; guides; guides = g_list_next (guides))
    {
      GimpGuide *g = (GimpGuide *) guides->data;

      if ((g->guide_ID == guide) && (g->position >= 0))
	{
	  position = g->position;

	  success = TRUE;
	  break;
	}
    } 
}
CODE
    );
}

@headers = qw("core/gimpimage-guides.h" "core/gimpimage-undo-push.h"
              "libgimp/gimpintl.h");

@procs = qw(image_add_hguide image_add_vguide image_delete_guide
	    image_find_next_guide image_get_guide_orientation
	    image_get_guide_position);
%exports = (app => [@procs], lib => [@procs]);

$desc = 'Guide procedures';

1;
