gimp/app/gimpbrushlist.c

453 lines
10 KiB
C
Raw Normal View History

1997-11-25 06:05:25 +08:00
/* 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.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
1997-11-25 06:05:25 +08:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
1999-04-23 17:39:51 +08:00
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
1997-11-25 06:05:25 +08:00
#include <unistd.h>
#endif
#ifdef HAVE_DIRENT_H
1997-11-25 06:05:25 +08:00
#include <dirent.h>
#endif
#include <math.h>
1997-11-25 06:05:25 +08:00
#include "appenv.h"
#include "gimpbrushgenerated.h"
#include "gimpbrushpipe.h"
1997-11-25 06:05:25 +08:00
#include "brush_header.h"
#include "brush_select.h"
1997-11-25 06:05:25 +08:00
#include "datafiles.h"
#include "gimprc.h"
#include "gimpsignal.h"
#include "gimplist.h"
#include "gimpbrush.h"
#include "gimplistP.h"
#include "gimpbrushlistP.h"
1997-11-25 06:05:25 +08:00
#include "libgimp/gimpenv.h"
#include "libgimp/gimpintl.h"
1997-11-25 06:05:25 +08:00
/* global variables */
GimpBrushList *brush_list = NULL;
1997-11-25 06:05:25 +08:00
1999-10-27 02:27:27 +08:00
/* local function prototypes */
static void brushes_brush_load (gchar *filename);
1997-11-25 06:05:25 +08:00
static gint brush_compare_func (gconstpointer first,
gconstpointer second);
1997-11-25 06:05:25 +08:00
1999-10-27 02:27:27 +08:00
/* class functions */
static GimpObjectClass* parent_class;
static void
1999-10-27 02:27:27 +08:00
gimp_brush_list_add_func (GimpList *list,
gpointer val)
{
list->list = g_slist_insert_sorted (list->list, val, brush_compare_func);
1999-10-27 02:27:27 +08:00
GIMP_BRUSH_LIST (list)->num_brushes++;
}
static void
1999-10-27 02:27:27 +08:00
gimp_brush_list_remove_func (GimpList *list,
gpointer val)
{
list->list = g_slist_remove (list->list, val);
1999-10-27 02:27:27 +08:00
GIMP_BRUSH_LIST (list)->num_brushes--;
}
static void
gimp_brush_list_class_init (GimpBrushListClass *klass)
1997-11-25 06:05:25 +08:00
{
GimpListClass *gimp_list_class;
gimp_list_class = GIMP_LIST_CLASS(klass);
gimp_list_class->add = gimp_brush_list_add_func;
gimp_list_class->remove = gimp_brush_list_remove_func;
parent_class = gtk_type_class (gimp_list_get_type ());
}
1997-11-25 06:05:25 +08:00
void
1999-10-27 02:27:27 +08:00
gimp_brush_list_init (GimpBrushList *list)
{
list->num_brushes = 0;
}
1997-11-25 06:05:25 +08:00
1999-10-27 02:27:27 +08:00
GtkType
gimp_brush_list_get_type (void)
{
1999-10-27 02:27:27 +08:00
static GtkType type = 0;
if (!type)
{
GtkTypeInfo info =
{
"GimpBrushList",
sizeof (GimpBrushList),
sizeof (GimpBrushListClass),
(GtkClassInitFunc) gimp_brush_list_class_init,
(GtkObjectInitFunc) gimp_brush_list_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL
};
type = gtk_type_unique (gimp_list_get_type (), &info);
}
return type;
}
1997-11-25 06:05:25 +08:00
GimpBrushList *
gimp_brush_list_new (void)
{
1999-10-27 02:27:27 +08:00
GimpBrushList *list;
list = GIMP_BRUSH_LIST (gtk_type_new (gimp_brush_list_get_type ()));
GIMP_LIST (list)->type = GIMP_TYPE_BRUSH;
GIMP_LIST (list)->weak = FALSE;
return list;
}
1997-11-25 06:05:25 +08:00
/* function declarations */
void
brushes_init (gint no_data)
{
if (brush_list)
1999-10-27 02:27:27 +08:00
brushes_free ();
else
1999-10-27 02:27:27 +08:00
brush_list = gimp_brush_list_new ();
1999-10-27 02:27:27 +08:00
if (brush_path != NULL && !no_data)
{
brush_select_freeze_all ();
datafiles_read_directories (brush_path, brushes_brush_load, 0);
datafiles_read_directories (brush_vbr_path, brushes_brush_load, 0);
brush_select_thaw_all ();
}
1999-10-27 02:27:27 +08:00
gimp_context_refresh_brushes ();
}
GimpBrush *
brushes_get_standard_brush (void)
{
static GimpBrush *standard_brush = NULL;
1999-10-27 02:27:27 +08:00
if (! standard_brush)
{
standard_brush =
GIMP_BRUSH (gimp_brush_generated_new (5.0, 0.5, 0.0, 1.0));
app/airbrush.c app/apptypes.h app/brushes_cmds.c 1999-11-14 Michael Natterer <mitch@gimp.org> * app/airbrush.c * app/apptypes.h * app/brushes_cmds.c * tools/pdbgen/pdb/brushes.pdb * app/bucket_fill.c * app/clone.c * app/gimpbrushpipe.c * app/paint_core.c * app/patterns.h * app/patterns_cmds.c * tools/pdbgen/pdb/patterns.pdb: removed the GimpBrushP and GPatternP types and use ordinary pointers instead. The following stuff makes the "no_data" behaviour consistent. As a side-effect it should make the gimp work when there are _really_ no brushes/patterns/gradients. * app/brush_select.c * app/pattern_select.c: set the initial brush/pattern name to "No Brushes/Patterns available" instead of "Active". * app/devices.c: set the device contexts' brush/pattern/gradient names if we started with no_data, so we find them on refresh. * app/gimpbrushlist.c: set the name of the standard_brush to "Standard". * app/gimpcontext.c: don't replace the current brush/pattern/gradient's name if the new one to be set is the standard one. Together with the change in devices.c, this ensures that we get what is set in devicerc. Minor fixes. * app/gradient.c: changed gradients_init() to work like the other data init functions. Only insert a default gradient in the gradients list when the editor is opened (this means that the gradients now behave like brushes/patterns when we start with "no_data"). New function gradient_update() avoids tons of useless redraws of all clist gradient previews whenever the gradient editor wants to update it's large preview. * app/gradient_select.c: don't segfault when the user tries to drag from an empty gradient list. * app/patterns.c: set the index of the standard_pattern to -1 to indicate that it's not part of the pattern list.
1999-11-14 18:50:19 +08:00
gimp_brush_set_name (standard_brush, "Standard");
/* set ref_count to 2 --> never swap the standard brush */
1999-10-27 02:27:27 +08:00
gtk_object_ref (GTK_OBJECT (standard_brush));
gtk_object_ref (GTK_OBJECT (standard_brush));
}
return standard_brush;
}
1997-11-25 06:05:25 +08:00
static void
1999-10-27 02:27:27 +08:00
brushes_brush_load (gchar *filename)
{
1999-10-27 02:27:27 +08:00
if (strcmp (&filename[strlen (filename) - 4], ".gbr") == 0)
{
GimpBrush *brush;
1999-10-27 02:27:27 +08:00
brush = gimp_brush_new (filename);
if (brush != NULL)
1999-10-27 02:27:27 +08:00
gimp_brush_list_add (brush_list, brush);
else
g_message (_("Warning: Failed to load brush\n\"%s\""), filename);
}
1999-10-27 02:27:27 +08:00
else if (strcmp (&filename[strlen(filename) - 4], ".vbr") == 0)
{
GimpBrushGenerated *brush;
1999-10-27 02:27:27 +08:00
brush = gimp_brush_generated_load (filename);
if (brush != NULL)
1999-10-27 02:27:27 +08:00
gimp_brush_list_add (brush_list, GIMP_BRUSH (brush));
else
g_message (_("Warning: Failed to load brush\n\"%s\""), filename);
}
1999-10-27 02:27:27 +08:00
else if (strcmp (&filename[strlen (filename) - 4], ".gpb") == 0)
{
GimpBrushPipe *brush;
1999-10-27 02:27:27 +08:00
brush = gimp_brush_pixmap_load (filename);
if (brush != NULL)
1999-10-27 02:27:27 +08:00
gimp_brush_list_add (brush_list, GIMP_BRUSH (brush));
else
g_message (_("Warning: Failed to load pixmap brush\n\"%s\""), filename);
}
1999-10-27 02:27:27 +08:00
else if (strcmp (&filename[strlen (filename) - 4], ".gih") == 0)
{
GimpBrushPipe *brush;
1999-10-27 02:27:27 +08:00
brush = gimp_brush_pipe_load (filename);
if (brush != NULL)
1999-10-27 02:27:27 +08:00
gimp_brush_list_add (brush_list, GIMP_BRUSH (brush));
else
g_message (_("Warning: Failed to load pixmap pipe\n\"%s\""), filename);
}
}
static gint
1999-10-27 02:27:27 +08:00
brush_compare_func (gconstpointer first,
gconstpointer second)
{
1999-10-27 02:27:27 +08:00
return strcmp (((const GimpBrush *) first)->name,
((const GimpBrush *) second)->name);
}
void
brushes_free (void)
{
GList *vbr_path;
gchar *vbr_dir;
if (!brush_list)
return;
vbr_path = gimp_path_parse (brush_vbr_path, 16, TRUE, NULL);
vbr_dir = gimp_path_get_user_writable_dir (vbr_path);
gimp_path_free (vbr_path);
brush_select_freeze_all ();
while (GIMP_LIST (brush_list)->list)
{
GimpBrush *brush = GIMP_BRUSH (GIMP_LIST (brush_list)->list->data);
if (GIMP_IS_BRUSH_GENERATED (brush) && vbr_dir)
{
gchar *filename = g_strdup (brush->filename);
if (!filename)
1999-10-27 02:27:27 +08:00
{
FILE *tmp_fp;
gint unum = 0;
filename = g_strconcat (vbr_dir,
brush->name, ".vbr",
NULL);
/* make sure we don't overwrite an existing brush */
while ((tmp_fp = fopen (filename, "r")))
1999-10-27 02:27:27 +08:00
{
fclose (tmp_fp);
g_free (filename);
filename = g_strdup_printf ("%s%s_%d.vbr",
vbr_dir,
brush->name,
unum);
unum++;
}
}
else
{
if (strcmp (&filename[strlen (filename) - 4], ".vbr"))
1999-10-27 02:27:27 +08:00
{
g_free (filename);
filename = NULL;
1999-10-27 02:27:27 +08:00
}
}
1997-11-25 06:05:25 +08:00
/* okay we are ready to try to save the generated file */
if (filename)
{
gimp_brush_generated_save (GIMP_BRUSH_GENERATED (brush),
filename);
g_free (filename);
}
1999-10-27 02:27:27 +08:00
}
gimp_brush_list_remove (brush_list, brush);
1997-11-25 06:05:25 +08:00
}
brush_select_thaw_all ();
g_free (vbr_dir);
1997-11-25 06:05:25 +08:00
}
#if 0
static GSList *
1999-10-27 02:27:27 +08:00
insert_brush_in_list (GSList *list,
GimpBrush *brush)
1997-11-25 06:05:25 +08:00
{
return g_slist_insert_sorted (list, brush, brush_compare_func);
1997-11-25 06:05:25 +08:00
}
#endif
1997-11-25 06:05:25 +08:00
1999-10-27 02:27:27 +08:00
gint
gimp_brush_list_get_brush_index (GimpBrushList *brush_list,
1999-10-27 02:27:27 +08:00
GimpBrush *brush)
{
/* fix me: make a gimp_list function that does this? */
return g_slist_index (GIMP_LIST(brush_list)->list, brush);
}
GimpBrush *
1999-10-27 02:27:27 +08:00
gimp_brush_list_get_brush_by_index (GimpBrushList *brush_list,
gint index)
1997-11-25 06:05:25 +08:00
{
1999-10-27 02:27:27 +08:00
GimpBrush *brush = NULL;
GSList *list;
1999-10-27 02:27:27 +08:00
/* fix me: make a gimp_list function that does this? */
1999-10-27 02:27:27 +08:00
list = g_slist_nth (GIMP_LIST (brush_list)->list, index);
if (list)
brush = (GimpBrush *) list->data;
1997-11-25 06:05:25 +08:00
return brush;
1997-11-25 06:05:25 +08:00
}
static void
1999-10-27 02:27:27 +08:00
gimp_brush_list_uniquefy_brush_name (GimpBrushList *brush_list,
GimpBrush *brush)
{
GSList *list, *listb;
GimpBrush *brushb;
gint number = 1;
gchar *newname;
gchar *oldname;
gchar *ext;
1999-10-27 02:27:27 +08:00
g_return_if_fail (GIMP_IS_BRUSH_LIST (brush_list));
g_return_if_fail (GIMP_IS_BRUSH (brush));
for (list = GIMP_LIST (brush_list)->list; list; list = g_slist_next (list))
1999-10-27 02:27:27 +08:00
{
brushb = GIMP_BRUSH (list->data);
if (brush != brushb &&
strcmp (gimp_brush_get_name (brush),
gimp_brush_get_name (brushb)) == 0)
{
/* names conflict */
1999-10-27 02:27:27 +08:00
oldname = gimp_brush_get_name (brush);
newname = g_malloc (strlen (oldname) + 10); /* if this aint enough
yer screwed */
strcpy (newname, oldname);
if ((ext = strrchr (newname, '#')))
1999-10-27 02:27:27 +08:00
{
number = atoi (ext+1);
if (&ext[(gint)(log10 (number) + 1)] !=
1999-10-27 02:27:27 +08:00
&newname[strlen (newname) - 1])
{
number = 1;
ext = &newname[strlen (newname)];
}
}
else
{
number = 1;
ext = &newname[strlen (newname)];
}
sprintf (ext, "#%d", number+1);
for (listb = GIMP_LIST (brush_list)->list; listb; listb = listb->next)
1999-10-27 02:27:27 +08:00
{
brushb = GIMP_BRUSH (listb->data);
if (brush != brushb &&
strcmp (newname, gimp_brush_get_name (brushb)) == 0)
{
number++;
sprintf (ext, "#%d", number+1);
listb = GIMP_LIST (brush_list)->list;
}
}
gimp_brush_set_name (brush, newname);
g_free (newname);
if (gimp_list_have (GIMP_LIST (brush_list), brush))
{
/* ought to have a better way than this to resort the brush */
1999-10-27 02:27:27 +08:00
gtk_object_ref (GTK_OBJECT (brush));
gimp_brush_list_remove (brush_list, brush);
gimp_brush_list_add (brush_list, brush);
gtk_object_unref (GTK_OBJECT (brush));
}
return;
}
}
}
1999-10-27 02:27:27 +08:00
static void
brush_renamed (GimpBrush *brush,
GimpBrushList *brush_list)
{
1999-10-27 02:27:27 +08:00
gimp_brush_list_uniquefy_brush_name (brush_list, brush);
}
void
1999-10-27 02:27:27 +08:00
gimp_brush_list_add (GimpBrushList *brush_list,
GimpBrush *brush)
{
1999-10-27 02:27:27 +08:00
gimp_brush_list_uniquefy_brush_name (brush_list, brush);
gimp_list_add (GIMP_LIST (brush_list), brush);
gtk_object_unref (GTK_OBJECT (brush));
1999-10-27 02:27:27 +08:00
gtk_signal_connect (GTK_OBJECT (brush), "rename",
GTK_SIGNAL_FUNC (brush_renamed),
brush_list);
}
1997-11-25 06:05:25 +08:00
void
1999-10-27 02:27:27 +08:00
gimp_brush_list_remove (GimpBrushList *brush_list,
GimpBrush *brush)
{
1999-10-27 02:27:27 +08:00
gtk_signal_disconnect_by_data (GTK_OBJECT (brush), brush_list);
gimp_list_remove (GIMP_LIST (brush_list), brush);
}
1999-10-27 02:27:27 +08:00
gint
gimp_brush_list_length (GimpBrushList *brush_list)
{
1999-10-27 02:27:27 +08:00
g_return_val_if_fail (GIMP_IS_BRUSH_LIST (brush_list), 0);
return brush_list->num_brushes;
}
GimpBrush *
1999-10-27 02:27:27 +08:00
gimp_brush_list_get_brush (GimpBrushList *blist,
gchar *name)
{
GimpBrush *brushp;
GSList *list;
1999-10-27 02:27:27 +08:00
if (blist == NULL)
return NULL;
1999-10-27 02:27:27 +08:00
for (list = GIMP_LIST (brush_list)->list; list; list = g_slist_next (list))
{
1999-10-27 02:27:27 +08:00
brushp = (GimpBrush *) list->data;
if (!strcmp (brushp->name, name))
return brushp;
}
return NULL;
}