gimp/app/path.c

340 lines
6.8 KiB
C
Raw Normal View History

/* The GIMP -- an image manipulation program
* Copyright (C) 1999 Andy Thomas alt@picnic.demon.co.uk
*
* 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.
* Some of this code is based on the layers_dialog box code.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "core/core-types.h"
#include "tools/tools-types.h"
#include "core/gimpimage.h"
app/Makefile.am app/gui/Makefile.am app/about_dialog.[ch] 2001-04-17 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/gui/Makefile.am * app/about_dialog.[ch] * app/brush_edit.[ch] * app/brush_select.[ch] * app/channels_dialog.[ch] * app/color_area.[ch] * app/color_notebook.[ch] * app/color_select.[ch] * app/colormap_dialog.[ch] * app/commands.[ch] * app/file_new_dialog.[ch] * app/gradient_editor.[ch] * app/gradient_select.[ch] * app/indicator_area.[ch] * app/info_dialog.[ch] * app/info_window.[ch] * app/layer_select.[ch] * app/layers_dialog.[ch] * app/menus.[ch] * app/palette.[ch] * app/palette_import.[ch] * app/palette_select.[ch] * app/paths_dialog.[ch] * app/pattern_select.[ch] * app/preferences_dialog.[ch] * app/session.[ch] * app/test_commands.[ch] * app/tips_dialog.[ch] * app/toolbox.[ch]: moved to gui/ (s/_/-/ and some more useful filenames on the way). * app/app_procs.c * app/context_manager.c * app/convert.c * app/disp_callbacks.c * app/errorconsole.c * app/file-open.c * app/file-save.c * app/file-utils.c * app/gdisplay.c * app/gimage.c * app/gimprc.c * app/image_new.c * app/interface.c * app/nav_window.c * app/path.c * app/plug_in.c * app/gui/dialogs-constructors.c * app/pdb/brush_select_cmds.c * app/pdb/convert_cmds.c * app/pdb/gradient_select_cmds.c * app/pdb/pattern_select_cmds.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpmagnifytool.c * app/tools/gimpmeasuretool.c * app/tools/gimpscaletool.c * app/tools/gimptransformtool.c * app/widgets/gimpcolorpanel.c * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/pattern_select.pdb * po/POTFILES.in: changed accordingly.
2001-04-18 05:43:29 +08:00
#include "tools/gimpbezierselecttool.h"
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include "gdisplay.h"
app/Makefile.am app/gui/Makefile.am app/about_dialog.[ch] 2001-04-17 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/gui/Makefile.am * app/about_dialog.[ch] * app/brush_edit.[ch] * app/brush_select.[ch] * app/channels_dialog.[ch] * app/color_area.[ch] * app/color_notebook.[ch] * app/color_select.[ch] * app/colormap_dialog.[ch] * app/commands.[ch] * app/file_new_dialog.[ch] * app/gradient_editor.[ch] * app/gradient_select.[ch] * app/indicator_area.[ch] * app/info_dialog.[ch] * app/info_window.[ch] * app/layer_select.[ch] * app/layers_dialog.[ch] * app/menus.[ch] * app/palette.[ch] * app/palette_import.[ch] * app/palette_select.[ch] * app/paths_dialog.[ch] * app/pattern_select.[ch] * app/preferences_dialog.[ch] * app/session.[ch] * app/test_commands.[ch] * app/tips_dialog.[ch] * app/toolbox.[ch]: moved to gui/ (s/_/-/ and some more useful filenames on the way). * app/app_procs.c * app/context_manager.c * app/convert.c * app/disp_callbacks.c * app/errorconsole.c * app/file-open.c * app/file-save.c * app/file-utils.c * app/gdisplay.c * app/gimage.c * app/gimprc.c * app/image_new.c * app/interface.c * app/nav_window.c * app/path.c * app/plug_in.c * app/gui/dialogs-constructors.c * app/pdb/brush_select_cmds.c * app/pdb/convert_cmds.c * app/pdb/gradient_select_cmds.c * app/pdb/pattern_select_cmds.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpmagnifytool.c * app/tools/gimpmeasuretool.c * app/tools/gimpscaletool.c * app/tools/gimptransformtool.c * app/widgets/gimpcolorpanel.c * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/pattern_select.pdb * po/POTFILES.in: changed accordingly.
2001-04-18 05:43:29 +08:00
#include "gui/paths-dialog.h"
#include "path.h"
#include "pathP.h"
static gchar * unique_name (GimpImage *gimage,
gchar *cstr);
Path *
path_new (GimpImage *gimage,
PathType ptype,
GSList *path_details,
gint closed,
gint state,
gint locked,
gint tattoo,
gchar *name)
{
gchar *suniq;
Path *path = g_new0 (Path, 1);
suniq = unique_name (gimage, name);
if (suniq)
path->name = suniq;
else
path->name = g_strdup (name);
path->path_details = path_details;
path->closed = closed;
path->state = state;
path->locked = locked;
path->pathtype = ptype;
if(tattoo)
path->tattoo = tattoo;
else
path->tattoo = gimp_image_get_new_tattoo (gimage);
return path;
}
Path *
path_copy (GimpImage *gimage,
Path *path)
{
Path *p_copy = g_new0 (Path, 1);
gchar *name;
name = unique_name (gimage, path->name);
if (name)
p_copy->name = name;
else
p_copy->name = g_strdup (path->name);
p_copy->closed = path->closed;
p_copy->state = path->state;
p_copy->pathtype = path->pathtype;
p_copy->path_details = pathpoints_copy (path->path_details);
if (gimage)
p_copy->tattoo = gimp_image_get_new_tattoo (gimage);
else
p_copy->tattoo = path->tattoo;
return p_copy;
}
void
path_free (Path *path)
{
g_return_if_fail (path != NULL);
g_free (path->name);
pathpoints_free (path->path_details);
g_free (path);
}
PathPoint *
path_point_new (guint type,
gdouble x,
gdouble y)
{
PathPoint *pathpoint = g_new0 (PathPoint,1);
pathpoint->type = type;
pathpoint->x = x;
pathpoint->y = y;
return pathpoint;
}
void
path_point_free (PathPoint *pathpoint)
{
g_free (pathpoint);
}
void
path_stroke (GimpImage *gimage,
PathList *pl,
Path *bzp)
{
GimpBezierSelectTool *bezier_sel;
GDisplay *gdisp;
gdisp = gdisplays_check_valid (pl->gdisp, gimage);
bezier_sel = path_to_beziersel (bzp);
bezier_stroke (bezier_sel, gdisp, SUBDIVIDE, !bzp->closed);
bezier_select_free (bezier_sel);
}
gint
path_distance (Path *bzp,
gdouble dist,
gint *x,
gint *y,
gdouble *grad)
{
gint ret;
GimpBezierSelectTool *bezier_sel;
bezier_sel = path_to_beziersel (bzp);
ret = bezier_distance_along (bezier_sel, !bzp->closed, dist, x, y, grad);
bezier_select_free (bezier_sel);
return ret;
}
Tattoo
path_get_tattoo (Path* p)
{
if (!p)
{
g_warning ("path_get_tattoo: invalid path");
return 0;
}
return p->tattoo;
}
Path*
path_get_path_by_tattoo (GimpImage *gimage,
Tattoo tattoo)
{
GSList *tlist;
PathList *plp;
if (!gimage || !tattoo)
return NULL;
/* Go around the list and check all tattoos. */
/* Get path structure */
plp = (PathList*) gimp_image_get_paths (gimage);
if (!plp)
return (NULL);
for (tlist = plp->bz_paths; tlist; tlist = g_slist_next(tlist))
{
Path* p = (Path*)(tlist->data);
if (p->tattoo == tattoo)
return (p);
}
return (NULL);
}
PathList *
path_list_new (GimpImage *gimage,
gint last_selected_row,
GSList *bz_paths)
{
PathList *pip = g_new0 (PathList, 1);
pip->gimage = gimage;
pip->last_selected_row = last_selected_row;
/* add connector to image delete/destroy */
pip->sig_id = g_signal_connect (G_OBJECT (gimage), "destroy",
G_CALLBACK (paths_dialog_destroy_cb),
pip);
pip->bz_paths = bz_paths;
return (PathList *) pip;
}
void
path_list_free (PathList* iml)
{
g_return_if_fail (iml != NULL);
if (iml->bz_paths)
{
g_slist_foreach (iml->bz_paths, (GFunc) path_free, NULL);
g_slist_free (iml->bz_paths);
}
g_free (iml);
}
/* Always return a copy that must be freed later */
static gchar *
strip_off_cnumber (gchar *str)
{
gchar *hashptr;
gint num;
gchar *copy;
if (!str)
return str;
copy = g_strdup (str);
if ((hashptr = strrchr (copy,'#')) && /* have a hash */
strlen(hashptr) > 0 && /* followed by something */
(num = atoi(hashptr+1)) > 0 && /* which is a number */
((int) log10 (num) + 1) == strlen (hashptr+1)) /* which is at the end */
{
gchar *tstr;
/* Has a #<number> */
*hashptr = '\0';
tstr = g_strdup (copy);
g_free (copy);
copy = tstr;
}
return copy;
}
/* Return NULL if already unique else a unique string */
static gchar *
unique_name (GimpImage *gimage,
gchar *cstr)
{
GSList *tlist;
PathList *plp;
gboolean unique = TRUE;
gchar *copy_cstr;
gchar *copy_test;
gchar *stripped_copy;
gint counter = 1;
/* Get bzpath structure */
if (!gimage || !(plp = (PathList*) gimp_image_get_paths(gimage)))
return NULL;
tlist = plp->bz_paths;
while (tlist)
{
gchar *test_str;
test_str = ((Path *) (tlist->data))->name;
if (strcmp (cstr, test_str) == 0)
{
unique = FALSE;
break;
}
tlist = g_slist_next(tlist);
}
if (unique)
return NULL;
/* OK Clashes with something */
/* restart scan to find unique name */
stripped_copy = strip_off_cnumber (cstr);
copy_cstr = g_strdup_printf ("%s#%d", stripped_copy, counter++);
tlist = plp->bz_paths;
while (tlist)
{
copy_test = ((Path*)(tlist->data))->name;
if (strcmp (copy_cstr,copy_test) == 0)
{
g_free (copy_cstr);
copy_cstr = g_strdup_printf ("%s#%d", stripped_copy, counter++);
tlist = plp->bz_paths;
continue;
}
tlist = g_slist_next(tlist);
}
g_free (stripped_copy);
return copy_cstr;
}