gimp/app/posterize.c
Michael Natterer 002aa905db app/Makefile.am app/gimphelp.[ch] new files
1999-09-27  Michael Natterer  <mitch@gimp.org>

	* app/Makefile.am
	* app/gimphelp.[ch]
	* app/gimpui.[ch]: new files

	* app/interface.[ch]
	* app/preferences_dialog.[ch]

	The GIMP Help System part 1: Press "F1" in any dialog to pop up
	the help page for this dialog.

	Moved the widget constructors from preferences_dialog.[ch] and the
	query boxes from interface.[ch] to gimpui.[ch].

	The dialog constructors take a help_func and a help_data
	parameter and install the "F1" accelerator which emits the new
	"help" signal.

	The "help" signal callback calls help_func(help_data) which finally
	has to call gimp_help() which in turn invokes the help browser.

	Still have to find a proper way to (1) prevent "F1" being assigned
	to some menu item and (2) to catch "F1" while browsing the menu
	trees in order to pop up the help for the selected item.

	* app/menus.c: a <Toolbox>/File/Help... menu item.
	* app/commands.[ch]: a command callback for the "Help..." menu item.

	* app/gimprc.[ch]: new boolean gimprc variable "use_help".

	* app/info_dialog.[ch]: pass a help function and data to the info
	dialog constructor.

	* app/tools.[ch]: store the tools help page names in the tool info
	structure. Export a special tools_help_func() which shows the help
	page for the active tool.

	* app/[all files calling a dialog constructor]: pass the dialog's
	help page to the constructor.

	Most dialogs are now created by gimp_dialog_new() which also sets
	up the action_area and the WM delete event callback, so I removed
	the resp. code from these files.

	Fixed some minor bugs and did some other stuff but didn't change
	any logic except dialog creation.

	* plug-ins/helpbrowser/helpbrowser.c: don't try to call a running
	help browser and don't install any menu path (all done in
	app/gimphelp.[ch] now).
1999-09-27 17:58:10 +00:00

359 lines
8.5 KiB
C

/* 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.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "appenv.h"
#include "drawable.h"
#include "gdisplay.h"
#include "image_map.h"
#include "interface.h"
#include "posterize.h"
#include "gimplut.h"
#include "gimpui.h"
#include "lut_funcs.h"
#include "libgimp/gimpintl.h"
#define TEXT_WIDTH 55
/* the posterize structures */
typedef struct _Posterize Posterize;
struct _Posterize
{
int x, y; /* coords for last mouse click */
};
typedef struct _PosterizeDialog PosterizeDialog;
struct _PosterizeDialog
{
GtkWidget *shell;
GtkWidget *levels_text;
GimpDrawable *drawable;
ImageMap image_map;
int levels;
gint preview;
GimpLut *lut;
};
/* the posterize tool options */
static ToolOptions *posterize_options = NULL;
/* the posterize tool dialog */
static PosterizeDialog *posterize_dialog = NULL;
/* posterize action functions */
static void posterize_control (Tool *, ToolAction, gpointer);
static PosterizeDialog * posterize_new_dialog (void);
static void posterize_preview (PosterizeDialog *);
static void posterize_ok_callback (GtkWidget *, gpointer);
static void posterize_cancel_callback (GtkWidget *, gpointer);
static void posterize_preview_update (GtkWidget *, gpointer);
static void posterize_levels_text_update (GtkWidget *, gpointer);
/* posterize select action functions */
static void
posterize_control (Tool *tool,
ToolAction action,
gpointer gdisp_ptr)
{
switch (action)
{
case PAUSE:
break;
case RESUME:
break;
case HALT:
if (posterize_dialog)
posterize_cancel_callback (NULL, (gpointer) posterize_dialog);
break;
default:
break;
}
}
Tool *
tools_new_posterize ()
{
Tool * tool;
Posterize * private;
/* The tool options */
if (! posterize_options)
{
posterize_options = tool_options_new (("Posterize Options"));
tools_register (POSTERIZE, posterize_options);
}
/* The posterize dialog */
if (!posterize_dialog)
posterize_dialog = posterize_new_dialog ();
else
if (!GTK_WIDGET_VISIBLE (posterize_dialog->shell))
gtk_widget_show (posterize_dialog->shell);
tool = tools_new_tool (POSTERIZE);
private = g_new (Posterize, 1);
tool->scroll_lock = TRUE; /* Disallow scrolling */
tool->preserve = FALSE; /* Don't preserve on drawable change */
tool->private = (void *) private;
tool->control_func = posterize_control;
return tool;
}
void
tools_free_posterize (Tool *tool)
{
Posterize * post;
post = (Posterize *) tool->private;
/* Close the posterize dialog */
if (posterize_dialog)
posterize_cancel_callback (NULL, (gpointer) posterize_dialog);
g_free (post);
}
void
posterize_initialize (GDisplay *gdisp)
{
if (drawable_indexed (gimage_active_drawable (gdisp->gimage)))
{
g_message (_("Posterize does not operate on indexed drawables."));
return;
}
/* The posterize dialog */
if (!posterize_dialog)
posterize_dialog = posterize_new_dialog ();
else
if (!GTK_WIDGET_VISIBLE (posterize_dialog->shell))
gtk_widget_show (posterize_dialog->shell);
posterize_dialog->drawable = gimage_active_drawable (gdisp->gimage);
posterize_dialog->image_map =
image_map_create (gdisp, posterize_dialog->drawable);
if (posterize_dialog->preview)
posterize_preview (posterize_dialog);
}
/**********************/
/* Posterize dialog */
/**********************/
static PosterizeDialog *
posterize_new_dialog ()
{
PosterizeDialog *pd;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *toggle;
pd = g_new (PosterizeDialog, 1);
pd->preview = TRUE;
pd->levels = 3;
pd->lut = gimp_lut_new ();
/* The shell and main vbox */
pd->shell =
gimp_dialog_new (_("Posterize"), "posterize",
tools_help_func, NULL,
GTK_WIN_POS_NONE,
FALSE, TRUE, FALSE,
_("OK"), posterize_ok_callback,
pd, NULL, TRUE, FALSE,
_("Cancel"), posterize_cancel_callback,
pd, NULL, FALSE, TRUE,
NULL);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (pd->shell)->vbox), vbox);
/* Horizontal box for levels text widget */
hbox = gtk_hbox_new (TRUE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
label = gtk_label_new (_("Posterize Levels: "));
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, FALSE, 0);
gtk_widget_show (label);
/* levels text */
pd->levels_text = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY (pd->levels_text), "3");
gtk_widget_set_usize (pd->levels_text, TEXT_WIDTH, 25);
gtk_box_pack_start (GTK_BOX (hbox), pd->levels_text, TRUE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (pd->levels_text), "changed",
(GtkSignalFunc) posterize_levels_text_update,
pd);
gtk_widget_show (pd->levels_text);
gtk_widget_show (hbox);
/* Horizontal box for preview */
hbox = gtk_hbox_new (TRUE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
/* The preview toggle */
toggle = gtk_check_button_new_with_label (_("Preview"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), pd->preview);
gtk_box_pack_start (GTK_BOX (hbox), toggle, TRUE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
(GtkSignalFunc) posterize_preview_update,
pd);
gtk_widget_show (label);
gtk_widget_show (toggle);
gtk_widget_show (hbox);
gtk_widget_show (vbox);
gtk_widget_show (pd->shell);
return pd;
}
static void
posterize_preview (PosterizeDialog *pd)
{
if (!pd->image_map)
g_warning ("posterize_preview(): No image map");
active_tool->preserve = TRUE;
posterize_lut_setup(pd->lut, pd->levels, gimp_drawable_bytes(pd->drawable));
image_map_apply (pd->image_map, (ImageMapApplyFunc)gimp_lut_process_2,
(void *) pd->lut);
active_tool->preserve = FALSE;
}
static void
posterize_ok_callback (GtkWidget *widget,
gpointer client_data)
{
PosterizeDialog *pd;
pd = (PosterizeDialog *) client_data;
if (GTK_WIDGET_VISIBLE (pd->shell))
gtk_widget_hide (pd->shell);
active_tool->preserve = TRUE;
if (!pd->preview)
{
posterize_lut_setup(pd->lut, pd->levels,
gimp_drawable_bytes(pd->drawable));
image_map_apply (pd->image_map, (ImageMapApplyFunc)gimp_lut_process_2,
(void *) pd->lut);
}
if (pd->image_map)
image_map_commit (pd->image_map);
active_tool->preserve = FALSE;
pd->image_map = NULL;
active_tool->gdisp_ptr = NULL;
active_tool->drawable = NULL;
}
static void
posterize_cancel_callback (GtkWidget *widget,
gpointer client_data)
{
PosterizeDialog *pd;
pd = (PosterizeDialog *) client_data;
if (GTK_WIDGET_VISIBLE (pd->shell))
gtk_widget_hide (pd->shell);
if (pd->image_map)
{
active_tool->preserve = TRUE;
image_map_abort (pd->image_map);
active_tool->preserve = FALSE;
pd->image_map = NULL;
gdisplays_flush ();
}
active_tool->gdisp_ptr = NULL;
active_tool->drawable = NULL;
}
static void
posterize_preview_update (GtkWidget *w,
gpointer data)
{
PosterizeDialog *pd;
pd = (PosterizeDialog *) data;
if (GTK_TOGGLE_BUTTON (w)->active)
{
pd->preview = TRUE;
posterize_preview (pd);
}
else
pd->preview = FALSE;
}
static void
posterize_levels_text_update (GtkWidget *w,
gpointer data)
{
PosterizeDialog *pd;
char *str;
int value;
pd = (PosterizeDialog *) data;
str = gtk_entry_get_text (GTK_ENTRY (w));
value = BOUNDS (((int) atof (str)), 2, 256);
if (value != pd->levels)
{
pd->levels = value;
if (pd->preview)
posterize_preview (pd);
}
}