From 23d88f24bdfe6a6d644b1d021abebd2febb66fd9 Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Sat, 5 Dec 1998 03:38:13 +0000 Subject: [PATCH] Added a new widget that allows to specify lengths in pixels, inches and centimeters. It is tested, but not yet used anywhere. As I don't have the time to finish this now, I add this here for later use or for someone else to integrate it into all gimp dialogs... --Sven --- ChangeLog | 9 ++ libgimp/Makefile.am | 2 + libgimp/gimpentry.c | 332 ++++++++++++++++++++++++++++++++++++++++++++ libgimp/gimpentry.h | 86 ++++++++++++ 4 files changed, 429 insertions(+) create mode 100644 libgimp/gimpentry.c create mode 100644 libgimp/gimpentry.h diff --git a/ChangeLog b/ChangeLog index 54da75a69e..23c853527a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Sat Dec 5 04:27:44 MET 1998 Sven Neumann + + * libgimp/Makefile.am + * libgimp/gimpentry.[ch]: New files. Adds a new widget + that allows to specify lengths in pixels, inches and centimeters. + It is tested, but not yet used anywhere. As I don't have the time + to finish this now, I add this here for later use or for someone else + to integrate it into all gimp dialogs... + Fri Dec 4 01:43:59 EST 1998 Nick Fetchak * app/crop.[ch]: Added option to crop the active layer only diff --git a/libgimp/Makefile.am b/libgimp/Makefile.am index cde9d8a032..3373fd2d06 100644 --- a/libgimp/Makefile.am +++ b/libgimp/Makefile.am @@ -54,6 +54,7 @@ libgimp_la_SOURCES = \ parasiteP.h libgimpui_la_SOURCES = \ + gimpentry.c gimpmenu.c \ gimpbrushmenu.c \ gimpgradientmenu.c \ @@ -61,6 +62,7 @@ libgimpui_la_SOURCES = \ gimpinclude_HEADERS = \ gimp.h \ + gimpentry.h \ gimpenums.h \ gimpfeatures.h \ gimpmatrix.h \ diff --git a/libgimp/gimpentry.c b/libgimp/gimpentry.c new file mode 100644 index 0000000000..b8923a724e --- /dev/null +++ b/libgimp/gimpentry.c @@ -0,0 +1,332 @@ +/* LIBGIMP - The GIMP Library + * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#include +#include + +#include "gimpentry.h" + +#define SIZE_MAX_VALUE 500000.0 /* is that enough ?? */ +#define NUM_UNITS 3 + +static float gse_unit_factor[] = +{ + 1.0, + 1.0, + 2.54 +}; + +static gchar* gse_unit_string[] = +{ + "px", + "in", + "cm" +}; + +static void gimp_size_entry_unit_callback (GtkWidget *widget, gpointer data); +static void gimp_size_entry_value_callback (GtkWidget *widget, gpointer data); + + +enum { + GSE_VALUE_CHANGED_SIGNAL, + GSE_UNIT_CHANGED_SIGNAL, + GSE_RESOLUTION_CHANGED_SIGNAL, + LAST_SIGNAL +}; + +static gint gimp_size_entry_signals[LAST_SIGNAL] = { 0 }; + +static void +gimp_size_entry_class_init (GimpSizeEntryClass *class) +{ + GtkObjectClass *object_class; + + object_class = (GtkObjectClass*) class; + + gimp_size_entry_signals[GSE_VALUE_CHANGED_SIGNAL] = + gtk_signal_new ("value_changed", + GTK_RUN_FIRST, + object_class->type, + GTK_SIGNAL_OFFSET (GimpSizeEntryClass, gimp_size_entry), + gtk_signal_default_marshaller, GTK_TYPE_NONE, 0); + gimp_size_entry_signals[GSE_UNIT_CHANGED_SIGNAL] = + gtk_signal_new ("unit_changed", + GTK_RUN_FIRST, + object_class->type, + GTK_SIGNAL_OFFSET (GimpSizeEntryClass, gimp_size_entry), + gtk_signal_default_marshaller, GTK_TYPE_NONE, 0); + gimp_size_entry_signals[GSE_RESOLUTION_CHANGED_SIGNAL] = + gtk_signal_new ("resolution_changed", + GTK_RUN_FIRST, + object_class->type, + GTK_SIGNAL_OFFSET (GimpSizeEntryClass, gimp_size_entry), + gtk_signal_default_marshaller, GTK_TYPE_NONE, 0); + gtk_object_class_add_signals (object_class, gimp_size_entry_signals, + LAST_SIGNAL); + class->gimp_size_entry = NULL; +} + +static void +gimp_size_entry_init (GimpSizeEntry *gse) +{ + + GtkWidget *spinbutton; + GtkWidget *menu; + GtkWidget *menuitem; + GtkWidget *optionmenu; + GtkAdjustment *adj; + guint i; + + adj = (GtkAdjustment *) gtk_adjustment_new (0.0, -SIZE_MAX_VALUE, SIZE_MAX_VALUE, + 1.0, 50.0, 0.0); + spinbutton = gtk_spin_button_new (adj, 1.0, 0); + gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON(spinbutton), GTK_SHADOW_NONE); + gtk_widget_set_usize (spinbutton, 75, 0); + gtk_signal_connect (GTK_OBJECT (spinbutton), "changed", + (GtkSignalFunc) gimp_size_entry_value_callback, gse); + + gtk_box_pack_start (GTK_BOX(gse), spinbutton, FALSE, FALSE, 0); + gtk_widget_show (spinbutton); + + menu = gtk_menu_new(); + for (i=0; ispinbutton = spinbutton; + gse->optionmenu = optionmenu; + gse->resolution = 72; +} + +guint +gimp_size_entry_get_type () +{ + static guint gse_type = 0; + + if (!gse_type) + { + GtkTypeInfo gse_info = + { + "GimpSizeEntry", + sizeof (GimpSizeEntry), + sizeof (GimpSizeEntryClass), + (GtkClassInitFunc) gimp_size_entry_class_init, + (GtkObjectInitFunc) gimp_size_entry_init, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL + }; + + gse_type = gtk_type_unique (gtk_hbox_get_type (), &gse_info); + } + + return gse_type; +} + + +GtkWidget* +gimp_size_entry_new (gfloat value, + GSizeUnit unit, + gfloat resolution, + guint positive_only) +{ + GimpSizeEntry *gse; + float max; + GtkAdjustment *adj; + + if (resolution <= 0.0) + return (NULL); + + gse = gtk_type_new (gimp_size_entry_get_type ()); + + gse->resolution = resolution; + gse->positive_only = positive_only; + + gtk_option_menu_set_history (GTK_OPTION_MENU (gse->optionmenu), unit); + + if ((positive_only == TRUE) && (value <= 0.0)) + value = 0.0; + + if (unit != PIXELS) + { + max = SIZE_MAX_VALUE / gse->resolution * gse_unit_factor[unit]; + adj = (GtkAdjustment *) gtk_adjustment_new (0.0, + ((positive_only ==TRUE) ? 0.0 : -max), + max, 0.1, 5.0, 0.0); + gtk_spin_button_configure (GTK_SPIN_BUTTON (gse->spinbutton), adj, 0.01, 2); + } + else + { + if (positive_only == TRUE) + { + adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0, SIZE_MAX_VALUE, + 1.0, 50.0, 0.0); + gtk_spin_button_configure (GTK_SPIN_BUTTON (gse->spinbutton), adj, 1.0, 0); + } + } + + gtk_spin_button_set_value (GTK_SPIN_BUTTON (gse->spinbutton), value); + + return GTK_WIDGET (gse); +} + +void +gimp_size_entry_set_value (GimpSizeEntry *gse, + gfloat value) +{ + gtk_spin_button_set_value (GTK_SPIN_BUTTON (gse->spinbutton), value); +} + + +void +gimp_size_entry_set_value_as_pixels (GimpSizeEntry *gse, + gint pixels) +{ + gfloat value; + + if (gse->unit == PIXELS) + value = (float)pixels; + else + value = (float)pixels / gse->resolution * gse_unit_factor[gse->unit]; + + gtk_spin_button_set_value (GTK_SPIN_BUTTON (gse->spinbutton), value); +} + + +gfloat +gimp_size_entry_get_value (GimpSizeEntry *gse) +{ + return (gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (gse->spinbutton))); +} + +gint +gimp_size_entry_get_value_as_pixels (GimpSizeEntry *gse) +{ + gfloat value; + + value = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (gse->spinbutton)); + if (gse->unit != PIXELS) + value = value * gse->resolution / gse_unit_factor[gse->unit]; + + if (value - floor (value) < ceil (value) - value) + return floor (value); + else + return ceil (value); +} + +void +gimp_size_entry_set_unit (GimpSizeEntry *gse, + GSizeUnit new_unit) +{ + gfloat value; + gfloat max; + GtkAdjustment *adj; + + if (new_unit >= NUM_UNITS) + new_unit = PIXELS; + + if (new_unit == gse->unit) return; + + value = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (gse->spinbutton)); + + gtk_signal_handler_block_by_data (GTK_OBJECT(gse->spinbutton), gse); + + if (new_unit == PIXELS) + { + max = SIZE_MAX_VALUE; + adj = (GtkAdjustment *)gtk_adjustment_new (0.0, + ((gse->positive_only == TRUE) ? 0.0 : -max), + max, 0.1, 5.0, 0.0); + gtk_spin_button_configure (GTK_SPIN_BUTTON (gse->spinbutton), adj, 1.0, 0); + value = value * gse->resolution / gse_unit_factor[gse->unit]; + } + else + { + max = SIZE_MAX_VALUE / gse->resolution * gse_unit_factor[new_unit]; + adj = (GtkAdjustment *)gtk_adjustment_new (0.0, + ((gse->positive_only == TRUE) ? 0.0 : -max), + max, 0.1, 5.0, 0.0); + gtk_spin_button_configure (GTK_SPIN_BUTTON (gse->spinbutton), adj, 0.01, 2); + value = value * gse_unit_factor[new_unit] / gse_unit_factor[gse->unit]; + if (gse->unit == PIXELS) + value = value / gse->resolution; + } + gtk_spin_button_set_value (GTK_SPIN_BUTTON (gse->spinbutton), value); + gtk_signal_handler_unblock_by_data (GTK_OBJECT(gse->spinbutton), gse); + + gtk_option_menu_set_history (GTK_OPTION_MENU (gse->optionmenu), new_unit); + gse->unit = new_unit; +} + +GSizeUnit +gimp_size_entry_get_unit (GimpSizeEntry *gse) +{ + return (gse->unit); +} + +void +gimp_size_entry_set_resolution (GimpSizeEntry *gse, + gfloat resolution) +{ + if (resolution <= 0.0) + return; + + /* This function does NOT change the value of the size_entry + for you! You have to take care of that yourself. */ + gse->resolution = resolution; + gtk_signal_emit (GTK_OBJECT (gse), gimp_size_entry_signals[GSE_RESOLUTION_CHANGED_SIGNAL]); +} + + +static void +gimp_size_entry_unit_callback (GtkWidget *widget, + gpointer data) +{ + GimpSizeEntry *gse; + guint new_unit; + + gse = data; + new_unit = (guint) gtk_object_get_data (GTK_OBJECT (widget), "gimp_size_entry_unit"); + + if (gse->unit == new_unit) + return; + + gimp_size_entry_set_unit (GIMP_SIZE_ENTRY(gse), new_unit); + gtk_signal_emit (GTK_OBJECT (gse), gimp_size_entry_signals[GSE_UNIT_CHANGED_SIGNAL]); +} + +static void +gimp_size_entry_value_callback (GtkWidget *widget, + gpointer data) +{ + GimpSizeEntry *gse; + + gse = data; + gtk_signal_emit (GTK_OBJECT (gse), gimp_size_entry_signals[GSE_VALUE_CHANGED_SIGNAL]); +} diff --git a/libgimp/gimpentry.h b/libgimp/gimpentry.h new file mode 100644 index 0000000000..5c7ae88bc6 --- /dev/null +++ b/libgimp/gimpentry.h @@ -0,0 +1,86 @@ +/* LIBGIMP - The GIMP Library + * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#ifndef __GIMP_ENTRY_H__ +#define __GIMP_ENTRY_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define GIMP_TYPE_SIZE_ENTRY (gimp_size_entry_get_type ()) +#define GIMP_SIZE_ENTRY(obj) (GTK_CHECK_CAST ((obj), GIMP_TYPE_SIZE_ENTRY, GimpSizeEntry)) +#define GIMP_SIZE_ENTRY_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GIMP_TYPE_SIZE_ENTRY, GimpSizeEntryClass)) +#define GIMP_IS_SIZE_ENTRY(obj) (GTK_CHECK_TYPE (obj, GIMP_TYPE_SIZE_ENTRY) +#define GIMP_IS_ENTRY_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_ENTRY)) + +typedef enum +{ + PIXELS = 0, + INCHES = 1, + CM = 2 +} GSizeUnit; + +typedef struct _GimpSizeEntry GimpSizeEntry; +typedef struct _GimpSizeEntryClass GimpSizeEntryClass; + +struct _GimpSizeEntry +{ + GtkHBox hbox; + + GtkWidget *spinbutton; + GtkWidget *optionmenu; + GSizeUnit unit; + gfloat resolution; + guint positive_only; +}; + +struct _GimpSizeEntryClass +{ + GtkHBoxClass parent_class; + + void (* gimp_size_entry) (GimpSizeEntry *gse); +}; + +guint gimp_size_entry_get_type (void); +GtkWidget* gimp_size_entry_new (gfloat value, + GSizeUnit unit, + gfloat resolution, + guint positive_only); +void gimp_size_entry_set_value (GimpSizeEntry *gse, + gfloat value); +void gimp_size_entry_set_value_as_pixels (GimpSizeEntry *gse, + gint pixels); +gfloat gimp_size_entry_get_value (GimpSizeEntry *gse); +gint gimp_size_entry_get_value_as_pixels (GimpSizeEntry *gse); +void gimp_size_entry_set_unit (GimpSizeEntry *gse, + GSizeUnit unit); +GSizeUnit gimp_size_entry_get_unit (GimpSizeEntry *gse); +void gimp_size_entry_set_resolution (GimpSizeEntry *gse, + gfloat resolution); +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* __GIMP_ENTRY_H__ */ + + +