gimp/app/patterns.c

345 lines
7.7 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>
#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
1997-11-25 06:05:25 +08:00
#include <sys/types.h>
1999-04-23 23:20:10 +08:00
#include <sys/stat.h>
1997-11-25 06:05:25 +08:00
#include "appenv.h"
#include "colormaps.h"
#include "datafiles.h"
#include "devices.h"
1997-11-25 06:05:25 +08:00
#include "patterns.h"
#include "pattern_header.h"
#include "pattern_select.h"
#include "buildmenu.h"
#include "colormaps.h"
#include "errors.h"
#include "general.h"
#include "gimprc.h"
#include "menus.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 */
GPatternP active_pattern = NULL;
GSList * pattern_list = NULL;
1997-11-25 06:05:25 +08:00
int num_patterns = 0;
PatternSelectP pattern_select_dialog = NULL;
/* static variables */
static int have_default_pattern = 0;
/* static function prototypes */
static GSList * insert_pattern_in_list (GSList *, GPatternP);
1997-11-25 06:05:25 +08:00
static void load_pattern (char *filename);
static void free_pattern (GPatternP);
static void pattern_free_one (gpointer, gpointer);
static gint pattern_compare_func (gconstpointer,
gconstpointer);
1997-11-25 06:05:25 +08:00
/* function declarations */
void
patterns_init (int no_data)
1997-11-25 06:05:25 +08:00
{
GSList *list;
1997-11-25 06:05:25 +08:00
if (pattern_list)
patterns_free ();
pattern_list = NULL;
num_patterns = 0;
if (!pattern_path)
return;
if(!no_data)
datafiles_read_directories (pattern_path, load_pattern, 0);
1997-11-25 06:05:25 +08:00
/* assign indexes to the loaded patterns */
list = pattern_list;
while (list) {
1997-11-25 06:05:25 +08:00
/* Set the pattern index */
((GPattern *) list->data)->index = num_patterns++;
list = g_slist_next (list);
}
1997-11-25 06:05:25 +08:00
}
static void
pattern_free_one (gpointer data, gpointer dummy)
1997-11-25 06:05:25 +08:00
{
free_pattern ((GPatternP) data);
}
1997-11-25 06:05:25 +08:00
static gint
pattern_compare_func(gconstpointer first, gconstpointer second)
{
return strcmp (((const GPatternP)first)->name,
((const GPatternP)second)->name);
}
1997-11-25 06:05:25 +08:00
void
patterns_free ()
{
if (pattern_list) {
g_slist_foreach (pattern_list, pattern_free_one, NULL);
g_slist_free (pattern_list);
}
1997-11-25 06:05:25 +08:00
have_default_pattern = 0;
active_pattern = NULL;
num_patterns = 0;
pattern_list = NULL;
}
void
pattern_select_dialog_free ()
{
if (pattern_select_dialog)
{
pattern_select_free (pattern_select_dialog);
pattern_select_dialog = NULL;
}
}
GPatternP
get_active_pattern ()
{
if (have_default_pattern)
{
have_default_pattern = 0;
if (!active_pattern)
fatal_error (_("Specified default pattern not found!"));
1997-11-25 06:05:25 +08:00
}
else if (! active_pattern && pattern_list)
active_pattern = (GPatternP) pattern_list->data;
return active_pattern;
}
static GSList *
insert_pattern_in_list (GSList *list, GPatternP pattern)
1997-11-25 06:05:25 +08:00
{
return g_slist_insert_sorted (list, pattern, pattern_compare_func);
1997-11-25 06:05:25 +08:00
}
static void
load_pattern (char *filename)
{
GPatternP pattern;
FILE * fp;
int bn_size;
unsigned char buf [sz_PatternHeader];
PatternHeader header;
unsigned int * hp;
int i;
pattern = (GPatternP) g_malloc (sizeof (GPattern));
pattern->filename = g_strdup (filename);
pattern->name = NULL;
pattern->mask = NULL;
/* Open the requested file */
if (! (fp = fopen (filename, "rb")))
1997-11-25 06:05:25 +08:00
{
free_pattern (pattern);
return;
}
/* Read in the header size */
if ((fread (buf, 1, sz_PatternHeader, fp)) < sz_PatternHeader)
{
fclose (fp);
free_pattern (pattern);
return;
}
/* rearrange the bytes in each unsigned int */
hp = (unsigned int *) &header;
for (i = 0; i < (sz_PatternHeader / 4); i++)
hp [i] = (buf [i * 4] << 24) + (buf [i * 4 + 1] << 16) +
(buf [i * 4 + 2] << 8) + (buf [i * 4 + 3]);
/* Check for correct file format */
if (header.magic_number != GPATTERN_MAGIC)
{
/* One thing that can save this error is if the pattern is version 1 */
if (header.version != 1)
{
fclose (fp);
free_pattern (pattern);
return;
}
}
/* Check for correct version */
if (header.version != FILE_VERSION)
{
g_message (_("Unknown GIMP version #%d in \"%s\"\n"), header.version,
filename);
1997-11-25 06:05:25 +08:00
fclose (fp);
free_pattern (pattern);
return;
}
/* Get a new pattern mask */
pattern->mask = temp_buf_new (header.width, header.height, header.bytes, 0, 0, NULL);
/* Read in the pattern name */
if ((bn_size = (header.header_size - sz_PatternHeader)))
{
pattern->name = (char *) g_malloc (sizeof (char) * bn_size);
if ((fread (pattern->name, 1, bn_size, fp)) < bn_size)
{
g_message (_("Error in GIMP pattern file...aborting."));
1997-11-25 06:05:25 +08:00
fclose (fp);
free_pattern (pattern);
return;
}
}
else
pattern->name = g_strdup (_("Unnamed"));
1997-11-25 06:05:25 +08:00
/* Read the pattern mask data */
/* Read the image data */
if ((fread (temp_buf_data (pattern->mask), 1,
header.width * header.height * header.bytes, fp)) <
header.width * header.height * header.bytes)
g_message (_("GIMP pattern file appears to be truncated."));
1997-11-25 06:05:25 +08:00
/* Clean up */
fclose (fp);
/*temp_buf_swap (pattern->mask);*/
pattern_list = insert_pattern_in_list(pattern_list, pattern);
/* Check if the current pattern is the default one */
if (strcmp(default_pattern, g_basename(filename)) == 0) {
1997-11-25 06:05:25 +08:00
active_pattern = pattern;
have_default_pattern = 1;
} /* if */
}
GPatternP
pattern_list_get_pattern (GSList *list, char *name)
{
GPatternP patternp;
while (list)
{
patternp = (GPatternP) list->data;
if (!strcmp (patternp->name, name))
{
return patternp;
}
list = g_slist_next (list);
}
return NULL;
}
1997-11-25 06:05:25 +08:00
GPatternP
get_pattern_by_index (int index)
1997-11-25 06:05:25 +08:00
{
GSList *list;
GPatternP pattern = NULL;
1997-11-25 06:05:25 +08:00
list = g_slist_nth (pattern_list, index);
if (list)
pattern = (GPatternP) list->data;
1997-11-25 06:05:25 +08:00
return pattern;
1997-11-25 06:05:25 +08:00
}
void
select_pattern (pattern)
GPatternP pattern;
{
/* Set the active pattern */
active_pattern = pattern;
/* Make sure the active pattern is unswapped... */
/*temp_buf_unswap (pattern->mask);*/
/* Keep up appearances in the pattern dialog */
if (pattern_select_dialog)
pattern_select_select (pattern_select_dialog, pattern->index);
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_pattern_dialog (void)
1997-11-25 06:05:25 +08:00
{
if (!pattern_select_dialog)
{
/* Create the dialog... */
pattern_select_dialog = pattern_select_new (NULL,NULL);
/* register this one only */
dialog_register(pattern_select_dialog->shell);
1997-11-25 06:05:25 +08:00
}
else
{
/* Popup the dialog */
if (!GTK_WIDGET_VISIBLE (pattern_select_dialog->shell))
gtk_widget_show (pattern_select_dialog->shell);
else
gdk_window_raise(pattern_select_dialog->shell->window);
}
}
static void
free_pattern (pattern)
GPatternP pattern;
{
if (pattern->mask)
temp_buf_free (pattern->mask);
if (pattern->filename)
g_free (pattern->filename);
if (pattern->name)
g_free (pattern->name);
g_free (pattern);
}