gimp/app/gui/gradient-select.c
Michael Natterer 8d86ec25e0 Move away from creating all item_factories statically in menus_init() but
2003-01-10  Michael Natterer  <mitch@gimp.org>

	Move away from creating all item_factories statically in
	menus_init() but create a new one for each place where one is
	needed:

	* app/widgets/Makefile.am
	* app/widgets/widgets-types.h
	* app/widgets/gimpmenufactory.[ch]: new factory which creates and
	configures the GimpItemFactories it knows about on-the-fly.

	* app/widgets/gimpitemfactory.[ch]: added
	gimp_item_factory_update() which calls the "update_func". Added
	"gboolean update_on_popup" so item_factories can be configured to
	require manual updates (used for the <Image> factory).

	* app/gui/menus.[ch]: create a "global_menu_factory" and register
	all menus we have with it. Added various setup functions which
	do stuff like adding the "Open Recent" menu or reorder plug-in
	menu entries. Removed the debugging stuff...

	* app/gui/Makefile.am
	* app/gui/debug-commands.[ch]: ...and added it here.

	* app/gui/gui.c: create the <Toolbox>, the popup-<Image> and the
	<Paths> factories here because they are still global.

	* app/gui/plug-in-menus.[ch]: changed the "image_factory"
	parameters to "item_factory" and create/update the entries for the
	passed item_factory only. Makes the whole stuff much more
	straightforward.

	* app/plug-in/plug-ins.c: don't call plug_in_make_menu().

	* app/display/gimpdisplay.[ch]
	* app/display/gimpdisplayshell.[ch]: added "menu_factory" and
	"popup_factory" parameters to gimp_display_new() and
	gimp_display_shell_new(). Create the menubar_factory and the
	qmask_factory dynamically. Pass the shell, not a Gimp to the QMask
	callbacks. Changed gimp_display_shell_set_menu_sensitivity() to
	gimp_display_shell_menu_update() and don't call it directly (it's
	a GimpItemFactory update_func now). Call gimp_item_factory_update()
	on the resp. factories instead.

	* app/gui/qmask-commands.c
	* app/display/gimpdisplayshell-callbacks.c
	* app/tools/gimpimagemaptool.c: changed accordingly.

	* app/widgets/gimpbrusheditor.c
	* app/widgets/gimpbrushfactoryview.[ch]
	* app/widgets/gimpbufferview.[ch]
	* app/widgets/gimpcolormapeditor.[ch]
	* app/widgets/gimpcontainereditor.[ch]
	* app/widgets/gimpdataeditor.[ch]
	* app/widgets/gimpdatafactoryview.[ch]
	* app/widgets/gimpdialogfactory.[ch]
	* app/widgets/gimpdock.c
	* app/widgets/gimpdockbook.[ch]
	* app/widgets/gimpdocumentview.[ch]
	* app/widgets/gimpgradienteditor.[ch]
	* app/widgets/gimpimageview.[ch]
	* app/widgets/gimpitemlistview.[ch]
	* app/widgets/gimppaletteeditor.[ch]: pass around lots of
	GimpMenuFactory pointers and menu_identifiers so all views can
	create their item_factories themselves. Unref the factories when
	they are no longer needed because they belong to the views now.

	* app/gui/dialogs-commands.c
	* app/gui/dialogs-constructors.c
	* app/gui/dialogs.c
	* app/gui/brush-select.c
	* app/gui/gradient-select.c
	* app/gui/palette-select.c
	* app/gui/pattern-select.c: changed accordingly.

	* app/gui/file-dialog-utils.[ch] (file_dialog_new): require
	menu_factory and menu_identifier parameters.

	* app/gui/file-open-dialog.[ch]
	* app/gui/file-save-dialog.[ch]: removed file_*_dialog_menu_init()
	(they went to menus.c as setup_funcs). Added file_*_dialog_set_type()
	and moved the <Load> and <Save> factory callbacks to file-commands.c

	* app/gui/file-commands.[ch]: changed accordingly.

	* app/gui/view-commands.c: changed the statusbar, menubar, rulers
	and guides callbacks to do their job only if the setting has
	actually changed. Don't update whole item factories afterwards.
	Instead, just change the state of the items that actually need
	update.

	Unrelated:

	* app/core/gimpchannel.c (gimp_channel_init): set "bounds_known"
	and friends to FALSE since we don't know that the new channel will
	be empty (fixes QMask and probably other stuff).

	* app/gui/image-commands.c
	* app/gui/vectors-commands.c: cleanup.
2003-01-10 17:55:53 +00:00

291 lines
7.4 KiB
C

/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
* Copyright (C) 1998 Andy Thomas.
*
* 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 PURIGHTE. 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.
*/
#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include "libgimpwidgets/gimpwidgets.h"
#include "gui-types.h"
#include "core/gimp.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdatafactory.h"
#include "core/gimpgradient.h"
#include "pdb/procedural_db.h"
#include "widgets/gimpdatafactoryview.h"
#include "dialogs-constructors.h"
#include "gradient-select.h"
#include "menus.h"
#include "libgimp/gimpintl.h"
/* local function prototypes */
static void gradient_select_change_callbacks (GradientSelect *gsp,
gboolean closing);
static void gradient_select_gradient_changed (GimpContext *context,
GimpGradient *gradient,
GradientSelect *gsp);
static void gradient_select_close_callback (GtkWidget *widget,
GradientSelect *gsp);
/* list of active dialogs */
static GSList *gradient_active_dialogs = NULL;
/* public functions */
GradientSelect *
gradient_select_new (Gimp *gimp,
const gchar *title,
const gchar *initial_gradient,
const gchar *callback_name,
gint sample_size)
{
GradientSelect *gsp;
GimpGradient *active = NULL;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (title != NULL, NULL);
if (gimp->no_data)
{
static gboolean first_call = TRUE;
if (first_call)
gimp_data_factory_data_init (gimp->gradient_factory, FALSE);
first_call = FALSE;
}
if (initial_gradient && strlen (initial_gradient))
{
active = (GimpGradient *)
gimp_container_get_child_by_name (gimp->gradient_factory->container,
initial_gradient);
}
if (! active)
active = gimp_context_get_gradient (gimp_get_current_context (gimp));
if (! active)
return NULL;
gsp = g_new0 (GradientSelect, 1);
/* Add to active gradient dialogs list */
gradient_active_dialogs = g_slist_append (gradient_active_dialogs, gsp);
gsp->context = gimp_context_new (gimp, title, NULL);
gsp->callback_name = g_strdup (callback_name);
gsp->sample_size = sample_size;
gimp_context_set_gradient (gsp->context, active);
g_signal_connect (gsp->context, "gradient_changed",
G_CALLBACK (gradient_select_gradient_changed),
gsp);
/* the shell */
gsp->shell = gimp_dialog_new (title, "gradient_selection",
gimp_standard_help_func,
"dialogs/gradient_selection.html",
GTK_WIN_POS_MOUSE,
FALSE, TRUE, FALSE,
GTK_STOCK_CLOSE, gradient_select_close_callback,
gsp, NULL, NULL, TRUE, TRUE,
NULL);
/* the gradient list */
gsp->view = gimp_data_factory_view_new (GIMP_VIEW_TYPE_LIST,
gsp->context->gimp->gradient_factory,
dialogs_edit_gradient_func,
gsp->context,
GIMP_PREVIEW_SIZE_EXTRA_SMALL,
6, 6,
global_menu_factory, "<Gradients>");
gtk_container_set_border_width (GTK_CONTAINER (gsp->view), 4);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (gsp->shell)->vbox), gsp->view);
gtk_widget_show (gsp->view);
gtk_widget_show (gsp->shell);
return gsp;
}
void
gradient_select_free (GradientSelect *gsp)
{
g_return_if_fail (gsp != NULL);
gtk_widget_destroy (gsp->shell);
/* remove from active list */
gradient_active_dialogs = g_slist_remove (gradient_active_dialogs, gsp);
if (gsp->callback_name)
g_free (gsp->callback_name);
if (gsp->context)
g_object_unref (gsp->context);
g_free (gsp);
}
GradientSelect *
gradient_select_get_by_callback (const gchar *callback_name)
{
GSList *list;
GradientSelect *gsp;
for (list = gradient_active_dialogs; list; list = g_slist_next (list))
{
gsp = (GradientSelect *) list->data;
if (gsp->callback_name && ! strcmp (callback_name, gsp->callback_name))
return gsp;
}
return NULL;
}
void
gradient_select_dialogs_check (void)
{
GradientSelect *gsp;
GSList *list;
list = gradient_active_dialogs;
while (list)
{
gsp = (GradientSelect *) list->data;
list = g_slist_next (list);
if (gsp->callback_name)
{
if (! procedural_db_lookup (gsp->context->gimp, gsp->callback_name))
gradient_select_close_callback (NULL, gsp);
}
}
}
/* private functions */
static void
gradient_select_change_callbacks (GradientSelect *gsp,
gboolean closing)
{
ProcRecord *proc = NULL;
GimpGradient *gradient;
Argument *return_vals;
gint nreturn_vals;
static gboolean busy = FALSE;
if (! (gsp && gsp->callback_name) || busy)
return;
busy = TRUE;
gradient = gimp_context_get_gradient (gsp->context);
/* If its still registered run it */
proc = procedural_db_lookup (gsp->context->gimp, gsp->callback_name);
if (proc && gradient)
{
gdouble *values, *pv;
double pos, delta;
GimpRGB color;
gint i;
i = gsp->sample_size;
pos = 0.0;
delta = 1.0 / (i - 1);
values = g_new (gdouble, 4 * i);
pv = values;
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, &color);
*pv++ = color.r;
*pv++ = color.g;
*pv++ = color.b;
*pv++ = color.a;
pos += delta;
}
return_vals =
procedural_db_run_proc (gsp->context->gimp,
gsp->callback_name,
&nreturn_vals,
GIMP_PDB_STRING, GIMP_OBJECT (gradient)->name,
GIMP_PDB_INT32, gsp->sample_size * 4,
GIMP_PDB_FLOATARRAY, values,
GIMP_PDB_INT32, (gint) closing,
GIMP_PDB_END);
if (!return_vals || return_vals[0].value.pdb_int != GIMP_PDB_SUCCESS)
g_message (_("Unable to run gradient callback.\n"
"The corresponding plug-in may have crashed."));
if (return_vals)
procedural_db_destroy_args (return_vals, nreturn_vals);
}
busy = FALSE;
}
static void
gradient_select_gradient_changed (GimpContext *context,
GimpGradient *gradient,
GradientSelect *gsp)
{
if (gradient)
gradient_select_change_callbacks (gsp, FALSE);
}
static void
gradient_select_close_callback (GtkWidget *widget,
GradientSelect *gsp)
{
gradient_select_change_callbacks (gsp, TRUE);
gradient_select_free (gsp);
}