gimp/app/gimpbrushlist.c

439 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"
1997-11-25 06:05:25 +08:00
#include "brush_header.h"
#include "brush_select.h"
#include "buildmenu.h"
#include "colormaps.h"
#include "datafiles.h"
#include "devices.h"
1997-11-25 06:05:25 +08:00
#include "errors.h"
#include "general.h"
#include "gimprc.h"
#include "gimpsignal.h"
1997-11-25 06:05:25 +08:00
#include "menus.h"
#include "paint_core.h"
First version of per-tool paint options. No PDB interface yet. The tool 1999-04-22 Michael Natterer <mitschel@cs.tu-berlin.de> First version of per-tool paint options. No PDB interface yet. The tool options dialog got rather big when in per-tool mode, so it will probably have to become a notebook. It's not yet 100% consistent. If switched off, everything should behave exactly like before. * app/Makefile.am * app/paint_options.h: new file * app/tool_options.c: PaintOptions gui. Maintain a list of all paint tools' ToolOptions to enable switching between global and per-tool paint options. * app/brush_select.[ch]: changed packing boxes, tables, ... The paint options in the brush selection can be hidden now. Moved create_paint_mode_menu() to paint_options.h and tool_options.c and renamed it to paint_mode_menu_new(). * app/gimage_mask.c * app/gimpbrush.[ch] * app/gimpbrushlist.[ch] * app/paint_core.c: moved gimp_brush_[set|get]_spacing() from gimpbrushlist.[ch] to gimpbrush.[ch]. Moved gimp_brush_[get|set]_[opacity|paint_mode]() to paint_options.h and tool_options.c and renamed them to paint_options_*_*(). They are "global paint options" now. * app/airbrush.c * app/blend.c * app/bucket_fill.c * app/clone.c * app/convolve.c * app/eraser.c * app/ink.c * app/paintbrush.c * app/pencil.c: all paint tools' options are derived from "PaintOptions" now. Opacity and paint mode are obtained through macros which take into account the current paint options mode. * app/buildmenu.h: #include <gtk/gtk.h> * app/color_picker.c * app/text_tool.c: changed spacings. * app/gimprc.[ch]: new gimprc option "global-paint-options" * app/preferences_dialog.c: Added a "Tool Options" page. Code cleanup. Some work on the convenience constructors test site. * app/tools.c: fixed "unused variable" warning.
1999-04-22 22:34:00 +08:00
#include "paint_options.h"
#include "gimplist.h"
#include "gimpbrush.h"
#include "gimplistP.h"
#include "gimpbrushlistP.h"
#include "dialog_handler.h"
1997-11-25 06:05:25 +08:00
#include "libgimp/gimpintl.h"
1997-11-25 06:05:25 +08:00
/* global variables */
GimpBrush *active_brush = NULL;
GimpBrushList *brush_list = NULL;
1997-11-25 06:05:25 +08:00
First version of per-tool paint options. No PDB interface yet. The tool 1999-04-22 Michael Natterer <mitschel@cs.tu-berlin.de> First version of per-tool paint options. No PDB interface yet. The tool options dialog got rather big when in per-tool mode, so it will probably have to become a notebook. It's not yet 100% consistent. If switched off, everything should behave exactly like before. * app/Makefile.am * app/paint_options.h: new file * app/tool_options.c: PaintOptions gui. Maintain a list of all paint tools' ToolOptions to enable switching between global and per-tool paint options. * app/brush_select.[ch]: changed packing boxes, tables, ... The paint options in the brush selection can be hidden now. Moved create_paint_mode_menu() to paint_options.h and tool_options.c and renamed it to paint_mode_menu_new(). * app/gimage_mask.c * app/gimpbrush.[ch] * app/gimpbrushlist.[ch] * app/paint_core.c: moved gimp_brush_[set|get]_spacing() from gimpbrushlist.[ch] to gimpbrush.[ch]. Moved gimp_brush_[get|set]_[opacity|paint_mode]() to paint_options.h and tool_options.c and renamed them to paint_options_*_*(). They are "global paint options" now. * app/airbrush.c * app/blend.c * app/bucket_fill.c * app/clone.c * app/convolve.c * app/eraser.c * app/ink.c * app/paintbrush.c * app/pencil.c: all paint tools' options are derived from "PaintOptions" now. Opacity and paint mode are obtained through macros which take into account the current paint options mode. * app/buildmenu.h: #include <gtk/gtk.h> * app/color_picker.c * app/text_tool.c: changed spacings. * app/gimprc.[ch]: new gimprc option "global-paint-options" * app/preferences_dialog.c: Added a "Tool Options" page. Code cleanup. Some work on the convenience constructors test site. * app/tools.c: fixed "unused variable" warning.
1999-04-22 22:34:00 +08:00
BrushSelectP brush_select_dialog = NULL;
1997-11-25 06:05:25 +08:00
/* static variables */
static int have_default_brush = 0;
/* static function prototypes */
static void create_default_brush (void);
static gint brush_compare_func (gconstpointer, gconstpointer);
1997-11-25 06:05:25 +08:00
static void brush_load (char *filename);
/* class functions */
static GimpObjectClass* parent_class;
static void
gimp_brush_list_add_func(GimpList* list, gpointer val)
{
list->list=g_slist_insert_sorted (list->list, val, brush_compare_func);
GIMP_BRUSH_LIST(list)->num_brushes++;
}
static void
gimp_brush_list_remove_func(GimpList* list, gpointer val)
{
list->list=g_slist_remove(list->list, val);
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
gimp_brush_list_init(GimpBrushList *list)
{
list->num_brushes = 0;
}
1997-11-25 06:05:25 +08:00
GtkType gimp_brush_list_get_type(void)
{
static GtkType type;
if(!type)
{
GtkTypeInfo info={
"GimpBrushList",
sizeof(GimpBrushList),
sizeof(GimpBrushListClass),
(GtkClassInitFunc)gimp_brush_list_class_init,
(GtkObjectInitFunc)gimp_brush_list_init,
NULL,
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 ()
{
GimpBrushList *list=GIMP_BRUSH_LIST(gtk_type_new(gimp_brush_list_get_type()));
GIMP_LIST(list)->type = GIMP_TYPE_BRUSH;
GIMP_LIST(list)->weak = 0;
return list;
}
1997-11-25 06:05:25 +08:00
/* function declarations */
void
brushes_init (int no_data)
{
if (brush_list)
brushes_free();
else
brush_list = gimp_brush_list_new();
if (brush_path == NULL || (no_data))
create_default_brush ();
else
datafiles_read_directories (brush_path,(datafile_loader_t)brush_load, 0);
}
1997-11-25 06:05:25 +08:00
static void
brush_load(char *filename)
{
if (strcmp(&filename[strlen(filename) - 4], ".gbr") == 0)
{
GimpBrush *brush;
brush = gimp_brush_new(filename);
if (brush != NULL)
gimp_brush_list_add(brush_list, brush);
else
g_message("Warning: failed to load brush \"%s\"", filename);
}
else if (strcmp(&filename[strlen(filename) - 4], ".vbr") == 0)
{
GimpBrushGenerated *brush;
brush = gimp_brush_generated_load(filename);
if (brush != NULL)
gimp_brush_list_add(brush_list, GIMP_BRUSH(brush));
else
g_message("Warning: failed to load brush \"%s\"", filename);
}
}
static gint
brush_compare_func (gconstpointer first, gconstpointer second)
{
return strcmp (((const GimpBrush *)first)->name,
((const GimpBrush *)second)->name);
}
1997-11-25 06:05:25 +08:00
void
brushes_free ()
{
if (brush_list)
{
while (GIMP_LIST(brush_list)->list)
gimp_brush_list_remove(brush_list, GIMP_LIST(brush_list)->list->data);
}
1997-11-25 06:05:25 +08:00
have_default_brush = 0;
}
void
brush_select_dialog_free ()
{
if (brush_select_dialog)
{
brush_select_free (brush_select_dialog);
brush_select_dialog = NULL;
}
}
GimpBrush *
1997-11-25 06:05:25 +08:00
get_active_brush ()
{
if (have_default_brush)
{
have_default_brush = 0;
if (!active_brush)
fatal_error (_("Specified default brush not found!"));
1997-11-25 06:05:25 +08:00
}
else if (! active_brush && brush_list)
/* need a gimp_list_get_first() type function */
select_brush((GimpBrush *) GIMP_LIST(brush_list)->list->data);
1997-11-25 06:05:25 +08:00
return active_brush;
}
#if 0
static GSList *
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
static void
create_default_brush ()
{
GimpBrushGenerated *brush;
brush = gimp_brush_generated_new(5.0, .5, 0.0, 1.0);
1997-11-25 06:05:25 +08:00
/* Swap the brush to disk (if we're being stingy with memory) */
if (stingy_memory_use)
temp_buf_swap (GIMP_BRUSH(brush)->mask);
1997-11-25 06:05:25 +08:00
/* Make this the default, active brush */
select_brush(GIMP_BRUSH(brush));
1997-11-25 06:05:25 +08:00
have_default_brush = 1;
}
int
gimp_brush_list_get_brush_index (GimpBrushList *brush_list,
GimpBrush *brush)
{
/* fix me: make a gimp_list function that does this? */
return g_slist_index (GIMP_LIST(brush_list)->list, brush);
}
GimpBrush *
gimp_brush_list_get_brush_by_index (GimpBrushList *brush_list, int index)
1997-11-25 06:05:25 +08:00
{
GSList *list;
GimpBrush * brush = NULL;
/* fix me: make a gimp_list function that does this? */
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
gimp_brush_list_uniquefy_brush_name(GimpBrushList *brush_list,
GimpBrush *brush)
{
GSList *list, *listb;
GimpBrush *brushb;
int number = 1;
char *newname;
char *oldname;
char *ext;
g_return_if_fail(GIMP_IS_BRUSH_LIST(brush_list));
g_return_if_fail(GIMP_IS_BRUSH(brush));
list = GIMP_LIST(brush_list)->list;
while (list)
{
brushb = GIMP_BRUSH(list->data);
if (brush != brushb &&
strcmp(gimp_brush_get_name(brush), gimp_brush_get_name(brushb)) == 0)
{ /* names conflict */
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, '#')))
{
number = atoi(ext+1);
if (&ext[(int)(log10(number) + 1)] != &newname[strlen(newname) - 1])
{
number = 1;
ext = &newname[strlen(newname)];
}
}
else
{
number = 1;
ext = &newname[strlen(newname)];
}
sprintf(ext, "#%d", number+1);
listb = GIMP_LIST(brush_list)->list;
while (listb) /* make sure the new name is unique */
{
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;
}
listb = listb->next;
}
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 */
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;
}
list = list->next;
}
}
static void brush_renamed(GimpBrush *brush, GimpBrushList *brush_list)
{
gimp_brush_list_uniquefy_brush_name(brush_list, brush);
}
void
gimp_brush_list_add (GimpBrushList *brush_list, GimpBrush * brush)
{
gimp_brush_list_uniquefy_brush_name(brush_list, brush);
gimp_list_add(GIMP_LIST(brush_list), brush);
gtk_object_sink(GTK_OBJECT(brush));
gtk_signal_connect(GTK_OBJECT(brush), "rename",
(GtkSignalFunc)brush_renamed, brush_list);
}
1997-11-25 06:05:25 +08:00
void
gimp_brush_list_remove (GimpBrushList *brush_list, GimpBrush * brush)
{
gtk_signal_disconnect_by_data(GTK_OBJECT(brush), brush_list);
gimp_list_remove(GIMP_LIST(brush_list), brush);
}
int
gimp_brush_list_length (GimpBrushList *brush_list)
{
g_return_val_if_fail(GIMP_IS_BRUSH_LIST(brush_list), 0);
return (brush_list->num_brushes);
}
GimpBrush *
gimp_brush_list_get_brush(GimpBrushList *blist, char *name)
{
GimpBrush *brushp;
GSList *list;
if (blist == NULL)
return NULL;
list = GIMP_LIST(brush_list)->list;
while (list)
{
brushp = (GimpBrush *) list->data;
if (!strcmp (brushp->name, name))
{
return brushp;
}
list = g_slist_next (list);
}
return NULL;
}
void
select_brush (GimpBrush * brush)
1997-11-25 06:05:25 +08:00
{
/* Make sure the active brush is swapped before we get a new one... */
if (stingy_memory_use && active_brush && active_brush->mask)
1997-11-25 06:05:25 +08:00
temp_buf_swap (active_brush->mask);
if (active_brush)
gtk_object_unref(GTK_OBJECT(active_brush));
1997-11-25 06:05:25 +08:00
/* Set the active brush */
active_brush = brush;
gtk_object_ref(GTK_OBJECT(active_brush));
1997-11-25 06:05:25 +08:00
/* Make sure the active brush is unswapped... */
if (stingy_memory_use)
temp_buf_unswap (brush->mask);
/* Keep up appearances in the brush dialog */
if (brush_select_dialog)
brush_select_select (brush_select_dialog,
gimp_brush_list_get_brush_index(brush_list, brush));
device_status_update (current_device);
1997-11-25 06:05:25 +08:00
}
void
gave parasite undo a MISC_UNDO class for now so it compiles * app/gimpdrawable.c: gave parasite undo a MISC_UNDO class for now so it compiles * app/tools_cmds.c: fix crop invoker to give correct args to crop_image * app/color_cmds.c: s/GRAY/GRAY_LUT/g; * app/brush_select.[ch]: removed PDB procs, export brush_active_dialogs, brush_select_dialog, s/active_dialogs/brush_active_dialogs/ * app/gimage_cmds.[ch] * app/channel_ops.[ch]: removed channel ops PDB procs, moved duplicate function from gimage_cmds to channel_ops, export offset and duplicate * app/gimpbrushlist.[ch]: removed PDB procs * app/gradient.[ch]: removed PDB procs, * app/gradient_header.h: exported G_SAMPLE, GradSelect, num_gradients, grad_active_dialogs, gradient_select_dialog * app/gradient_select.c: removed PDB procs, s/active_dialogs/grad_active_dialogs/ * app/patterns.[ch]: removed PDB procs * app/pattern_select.[ch]: removed PDB procs, s/active_dialogs/pattern_active_dialogs/ * app/procedural_db.c: removed PDB procs and supporting functions * app/procedrual_db.h: fiddled with enums * app/channel_cmds.[ch] * app/drawable_cmds.[ch] * app/parasite_cmds.[ch]: pdbgenned now, removed header files * app/gimpparasite.c: minor cleanup * app/internal_procs.c: use pdbgen stuff * app/tools_cmds.c * app/text_tool_cmds.c: updated from pdbgen * app/brushes_cmds.c * app/brush_select_cmds.c * app/gradient_cmds.c * app/gradient_select_cmds.c * app/patterns_cmds.c * app/pattern_select_cmds.c * app/procedural_db_cmds.c: new pdbgen files * app/Makefile.am: file shuffle (see above) -Yosh
1999-04-24 04:54:02 +08:00
create_brush_dialog (void)
1997-11-25 06:05:25 +08:00
{
if (!brush_select_dialog)
{
/* Create the dialog... */
brush_select_dialog = brush_select_new (NULL,NULL,0.0,0,0);
/* register this one only */
dialog_register(brush_select_dialog->shell);
1997-11-25 06:05:25 +08:00
}
else
{
/* Popup the dialog */
if (!GTK_WIDGET_VISIBLE (brush_select_dialog->shell))
gtk_widget_show (brush_select_dialog->shell);
else
gdk_window_raise(brush_select_dialog->shell->window);
}
}