diff --git a/ChangeLog b/ChangeLog index bb88cf0693..015fbaad34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,40 @@ -2004-07-27 Michael Natterer +2004-07-27 Sven Neumann + + * libgimpbase/Makefile.am + * libgimpbase/gimpbase.h + * libgimpbase/gimpbase.def + * libgimpbase/gimpmemsize.[ch]: added new files with memsize + related functions (moved here from gimputil.c) and + GIMP_TYPE_MEMSIZE (moved here from app/config/gimpconfig-types.[ch]). + + * libgimpbase/gimputils.[ch]: removed gimp_memsize_to_string() here. + + * libgimpbase/gimpunit.[ch]: added GIMP_TYPE_UNIT (moved here from + app/config/gimpconfig-types.[ch]). + + * libgimpbase/gimpbase-private.c + * libgimp/gimptile.c + * libgimp/gimpunitcache.c + * plug-ins/help/domain.c + * app/xcf/xcf-read.c: need to include glib-object.h. + + * plug-ins/common/uniteditor.c: use GIMP_TYPE_UNIT. + + * app/config/gimpconfig-types.[ch]: removed code that lives in + libgimpbase now. + + * app/config/gimpconfig-deserialize.c: changed accordingly. + + * app/config/gimpbaseconfig.c + * app/config/gimpdisplayconfig.c + * app/core/gimpcontext.c + * app/gui/grid-dialog.c + * app/tools/gimpcolortool.c + * app/widgets/gimpaction.c + * app/widgets/gimpunitstore.c: no need to include gimpconfig-types.h + any longer. + +004-07-27 Michael Natterer * libgimp/Makefile.am * libgimp/gimp.h diff --git a/app/config/gimpbaseconfig.c b/app/config/gimpbaseconfig.c index a6cfb24b53..a6ceacc6f0 100644 --- a/app/config/gimpbaseconfig.c +++ b/app/config/gimpbaseconfig.c @@ -28,7 +28,6 @@ #include "config-types.h" #include "gimpconfig-params.h" -#include "gimpconfig-types.h" #include "gimprc-blurbs.h" #include "gimpbaseconfig.h" diff --git a/app/config/gimpconfig-deserialize.c b/app/config/gimpconfig-deserialize.c index 23f2f9d780..6abc3815ae 100644 --- a/app/config/gimpconfig-deserialize.c +++ b/app/config/gimpconfig-deserialize.c @@ -560,8 +560,9 @@ gimp_config_deserialize_memsize (GValue *value, GParamSpec *prop_spec, GScanner *scanner) { - gchar *orig_cset_first = scanner->config->cset_identifier_first; - gchar *orig_cset_nth = scanner->config->cset_identifier_nth; + gchar *orig_cset_first = scanner->config->cset_identifier_first; + gchar *orig_cset_nth = scanner->config->cset_identifier_nth; + guint64 memsize; scanner->config->cset_identifier_first = G_CSET_DIGITS; scanner->config->cset_identifier_nth = G_CSET_DIGITS "gGmMkKbB"; @@ -574,10 +575,12 @@ gimp_config_deserialize_memsize (GValue *value, scanner->config->cset_identifier_first = orig_cset_first; scanner->config->cset_identifier_nth = orig_cset_nth; - if (gimp_memsize_set_from_string (value, scanner->value.v_identifier)) - return G_TOKEN_RIGHT_PAREN; - else + if (! gimp_memsize_deserialize (scanner->value.v_identifier, &memsize)) return G_TOKEN_NONE; + + g_value_set_uint64 (value, memsize); + + return G_TOKEN_RIGHT_PAREN; } static GTokenType diff --git a/app/config/gimpconfig-types.c b/app/config/gimpconfig-types.c index 2edec147e4..ba3a8f7468 100644 --- a/app/config/gimpconfig-types.c +++ b/app/config/gimpconfig-types.c @@ -21,12 +21,8 @@ #include "config.h" -#include -#include - #include -#include "libgimpbase/gimpbase.h" #include "libgimpmath/gimpmath.h" #include "config-types.h" @@ -34,35 +30,8 @@ #include "gimpconfig-types.h" -static GimpRGB * color_copy (const GimpRGB *color); -static void color_free (GimpRGB *color); +static GimpMatrix2 * matrix2_copy (const GimpMatrix2 *matrix); -static GimpMatrix2 * matrix2_copy (const GimpMatrix2 *matrix); -static void matrix2_free (GimpMatrix2 *matrix); - -static void memsize_to_string (const GValue *src_value, - GValue *dest_value); -static void string_to_memsize (const GValue *src_value, - GValue *dest_value); - -static void unit_to_string (const GValue *src_value, - GValue *dest_value); -static void string_to_unit (const GValue *src_value, - GValue *dest_value); - - -GType -gimp_color_get_type (void) -{ - static GType color_type = 0; - - if (!color_type) - color_type = g_boxed_type_register_static ("GimpColor", - (GBoxedCopyFunc) color_copy, - (GBoxedFreeFunc) color_free); - - return color_type; -} GType gimp_matrix2_get_type (void) @@ -72,85 +41,17 @@ gimp_matrix2_get_type (void) if (!matrix_type) matrix_type = g_boxed_type_register_static ("GimpMatrix2", (GBoxedCopyFunc) matrix2_copy, - (GBoxedFreeFunc) matrix2_free); + (GBoxedFreeFunc) g_free); return matrix_type; } -GType -gimp_memsize_get_type (void) +static GimpMatrix2 * +matrix2_copy (const GimpMatrix2 *matrix) { - static GType memsize_type = 0; - - if (!memsize_type) - { - static const GTypeInfo type_info = { 0, }; - - memsize_type = g_type_register_static (G_TYPE_UINT64, "GimpMemsize", - &type_info, 0); - - g_value_register_transform_func (memsize_type, G_TYPE_STRING, - memsize_to_string); - g_value_register_transform_func (G_TYPE_STRING, memsize_type, - string_to_memsize); - } - - return memsize_type; + return (GimpMatrix2 *) g_memdup (matrix, sizeof (GimpMatrix2)); } -gboolean -gimp_memsize_set_from_string (GValue *value, - const gchar *string) -{ - gchar *end; - guint64 size; - - g_return_val_if_fail (GIMP_VALUE_HOLDS_MEMSIZE (value), FALSE); - g_return_val_if_fail (string != NULL, FALSE); - - size = g_ascii_strtoull (string, &end, 0); - - if (size == G_MAXUINT64 && errno == ERANGE) - return FALSE; - - if (end && *end) - { - guint shift; - - switch (g_ascii_tolower (*end)) - { - case 'b': - shift = 0; - break; - case 'k': - shift = 10; - break; - case 'm': - shift = 20; - break; - case 'g': - shift = 30; - break; - default: - return FALSE; - } - - /* protect against overflow */ - if (shift) - { - guint64 limit = G_MAXUINT64 >> shift; - - if (size != (size & limit)) - return FALSE; - - size <<= shift; - } - } - - g_value_set_uint64 (value, size); - - return TRUE; -} GType gimp_path_get_type (void) @@ -167,126 +68,3 @@ gimp_path_get_type (void) return path_type; } - -GType -gimp_unit_get_type (void) -{ - static GType unit_type = 0; - - if (!unit_type) - { - static const GTypeInfo type_info = { 0, }; - - unit_type = g_type_register_static (G_TYPE_INT, "GimpUnit", - &type_info, 0); - - g_value_register_transform_func (unit_type, G_TYPE_STRING, - unit_to_string); - g_value_register_transform_func (G_TYPE_STRING, unit_type, - string_to_unit); - } - - return unit_type; -} - - -static GimpRGB * -color_copy (const GimpRGB *color) -{ - return (GimpRGB *) g_memdup (color, sizeof (GimpRGB)); -} - -static void -color_free (GimpRGB *color) -{ - g_free (color); -} - - -static void -memsize_to_string (const GValue *src_value, - GValue *dest_value) -{ - guint64 size; - gchar *str; - - size = g_value_get_uint64 (src_value); - - if (size > (1 << 30) && size % (1 << 30) == 0) - str = g_strdup_printf ("%" G_GUINT64_FORMAT "G", size >> 30); - else if (size > (1 << 20) && size % (1 << 20) == 0) - str = g_strdup_printf ("%" G_GUINT64_FORMAT "M", size >> 20); - else if (size > (1 << 10) && size % (1 << 10) == 0) - str = g_strdup_printf ("%" G_GUINT64_FORMAT "k", size >> 10); - else - str = g_strdup_printf ("%" G_GUINT64_FORMAT, size); - - g_value_take_string (dest_value, str); -} - -static void -string_to_memsize (const GValue *src_value, - GValue *dest_value) -{ - const gchar *str = g_value_get_string (src_value); - - if (!str || !gimp_memsize_set_from_string (dest_value, str)) - g_warning ("Can't convert string to GimpMemsize."); -} - - -static GimpMatrix2 * -matrix2_copy (const GimpMatrix2 *matrix) -{ - return (GimpMatrix2 *) g_memdup (matrix, sizeof (GimpMatrix2)); -} - -static void -matrix2_free (GimpMatrix2 *matrix) -{ - g_free (matrix); -} - - -static void -unit_to_string (const GValue *src_value, - GValue *dest_value) -{ - GimpUnit unit = (GimpUnit) g_value_get_int (src_value); - - g_value_set_string (dest_value, gimp_unit_get_identifier (unit)); -} - -static void -string_to_unit (const GValue *src_value, - GValue *dest_value) -{ - const gchar *str; - gint num_units; - gint i; - - str = g_value_get_string (src_value); - - if (!str || !*str) - goto error; - - num_units = gimp_unit_get_number_of_units (); - - for (i = GIMP_UNIT_PIXEL; i < num_units; i++) - if (strcmp (str, gimp_unit_get_identifier (i)) == 0) - break; - - if (i == num_units) - { - if (strcmp (str, gimp_unit_get_identifier (GIMP_UNIT_PERCENT)) == 0) - i = GIMP_UNIT_PERCENT; - else - goto error; - } - - g_value_set_int (dest_value, i); - return; - - error: - g_warning ("Can't convert string to GimpUnit."); -} diff --git a/app/config/gimpconfig-types.h b/app/config/gimpconfig-types.h index b1b5ca0db5..25a33b31c5 100644 --- a/app/config/gimpconfig-types.h +++ b/app/config/gimpconfig-types.h @@ -29,24 +29,10 @@ GType gimp_matrix2_get_type (void) G_GNUC_CONST; -#define GIMP_TYPE_MEMSIZE (gimp_memsize_get_type ()) -#define GIMP_VALUE_HOLDS_MEMSIZE(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_MEMSIZE)) - -GType gimp_memsize_get_type (void) G_GNUC_CONST; -gboolean gimp_memsize_set_from_string (GValue *value, - const gchar *string); - - #define GIMP_TYPE_PATH (gimp_path_get_type ()) #define GIMP_VALUE_HOLDS_PATH(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_PATH)) GType gimp_path_get_type (void) G_GNUC_CONST; -#define GIMP_TYPE_UNIT (gimp_unit_get_type ()) -#define GIMP_VALUE_HOLDS_UNIT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_UNIT)) - -GType gimp_unit_get_type (void) G_GNUC_CONST; - - #endif /* __GIMP_CONFIG_TYPES_H__ */ diff --git a/app/config/gimpdisplayconfig.c b/app/config/gimpdisplayconfig.c index 846ca8d90b..4dd62fcbb1 100644 --- a/app/config/gimpdisplayconfig.c +++ b/app/config/gimpdisplayconfig.c @@ -33,7 +33,6 @@ #include "gimpconfig.h" #include "gimpconfig-params.h" -#include "gimpconfig-types.h" #include "gimpconfig-utils.h" #include "gimprc-blurbs.h" diff --git a/app/core/gimpcontext.c b/app/core/gimpcontext.c index 8c2195ebc6..f3238de7ea 100644 --- a/app/core/gimpcontext.c +++ b/app/core/gimpcontext.c @@ -34,7 +34,6 @@ #include "config/gimpconfig.h" #include "config/gimpconfig-params.h" #include "config/gimpconfig-serialize.h" -#include "config/gimpconfig-types.h" #include "config/gimpconfigwriter.h" #include "config/gimpcoreconfig.h" #include "config/gimpscanner.h" diff --git a/app/dialogs/grid-dialog.c b/app/dialogs/grid-dialog.c index 26db07c22a..d6fb1e9b81 100644 --- a/app/dialogs/grid-dialog.c +++ b/app/dialogs/grid-dialog.c @@ -29,7 +29,6 @@ #include "gui-types.h" #include "config/gimpconfig.h" -#include "config/gimpconfig-types.h" #include "config/gimpconfig-utils.h" #include "config/gimpcoreconfig.h" diff --git a/app/gui/grid-dialog.c b/app/gui/grid-dialog.c index 26db07c22a..d6fb1e9b81 100644 --- a/app/gui/grid-dialog.c +++ b/app/gui/grid-dialog.c @@ -29,7 +29,6 @@ #include "gui-types.h" #include "config/gimpconfig.h" -#include "config/gimpconfig-types.h" #include "config/gimpconfig-utils.h" #include "config/gimpcoreconfig.h" diff --git a/app/tools/gimpcolortool.c b/app/tools/gimpcolortool.c index 77806cd60e..690c4c94d9 100644 --- a/app/tools/gimpcolortool.c +++ b/app/tools/gimpcolortool.c @@ -25,8 +25,6 @@ #include "tools-types.h" -#include "config/gimpconfig-types.h" - #include "core/gimpimage.h" #include "core/gimpimage-pick-color.h" #include "core/gimpitem.h" diff --git a/app/widgets/gimpaction.c b/app/widgets/gimpaction.c index 1bd69b959b..dcdef551ef 100644 --- a/app/widgets/gimpaction.c +++ b/app/widgets/gimpaction.c @@ -31,7 +31,6 @@ #include "widgets-types.h" #include "config/gimpconfig-params.h" -#include "config/gimpconfig-types.h" #include "core/gimpmarshal.h" #include "core/gimpviewable.h" diff --git a/app/widgets/gimpunitstore.c b/app/widgets/gimpunitstore.c index 4cc22794ca..29f5fb37ea 100644 --- a/app/widgets/gimpunitstore.c +++ b/app/widgets/gimpunitstore.c @@ -28,8 +28,6 @@ #include "widgets-types.h" -#include "config/gimpconfig-types.h" - #include "gimpunitstore.h" diff --git a/app/xcf/xcf-read.c b/app/xcf/xcf-read.c index e78d1343d6..4e0dc4a1aa 100644 --- a/app/xcf/xcf-read.c +++ b/app/xcf/xcf-read.c @@ -20,7 +20,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h" diff --git a/devel-docs/ChangeLog b/devel-docs/ChangeLog index 491083f2cd..33f73b52f5 100644 --- a/devel-docs/ChangeLog +++ b/devel-docs/ChangeLog @@ -1,3 +1,12 @@ +2004-07-27 Sven Neumann + + * libgimpbase/libgimpbase-docs.sgml + * libgimpbase/libgimpbase-sections.txt + * libgimpbase/tmpl/gimpmemsize.sgml + * libgimpbase/tmpl/gimpunit.sgml + * libgimpbase/tmpl/gimputils.sgml: added docs for new unit and + memsize functions. + 2004-07-27 Michael Natterer * libgimp/tmpl/gimppalettemenu.sgml diff --git a/devel-docs/libgimp/tmpl/gimppalettemenu.sgml b/devel-docs/libgimp/tmpl/gimppalettemenu.sgml index b8739e4ba2..64fced56a4 100644 --- a/devel-docs/libgimp/tmpl/gimppalettemenu.sgml +++ b/devel-docs/libgimp/tmpl/gimppalettemenu.sgml @@ -9,13 +9,11 @@ gimppalettemenu - - diff --git a/devel-docs/libgimp/tmpl/gimppaletteselect.sgml b/devel-docs/libgimp/tmpl/gimppaletteselect.sgml index 64d1c931fd..9c4e27271d 100644 --- a/devel-docs/libgimp/tmpl/gimppaletteselect.sgml +++ b/devel-docs/libgimp/tmpl/gimppaletteselect.sgml @@ -9,13 +9,11 @@ gimppaletteselect - - diff --git a/devel-docs/libgimpbase/libgimpbase-docs.sgml b/devel-docs/libgimpbase/libgimpbase-docs.sgml index 2ede1d2c71..57ead874bb 100644 --- a/devel-docs/libgimpbase/libgimpbase-docs.sgml +++ b/devel-docs/libgimpbase/libgimpbase-docs.sgml @@ -18,6 +18,7 @@ + diff --git a/devel-docs/libgimpbase/libgimpbase-sections.txt b/devel-docs/libgimpbase/libgimpbase-sections.txt index e42d8d9c29..704b4150cc 100644 --- a/devel-docs/libgimpbase/libgimpbase-sections.txt +++ b/devel-docs/libgimpbase/libgimpbase-sections.txt @@ -36,6 +36,17 @@ GIMP_CHECK_DARK GIMP_CHECK_LIGHT +
+gimpmemsize +gimp_memsize_serialize +gimp_memsize_deserialize +gimp_memsize_to_string +GIMP_VALUE_HOLDS_MEMSIZE + +GIMP_TYPE_MEMSIZE +gimp_memsize_get_type +
+
gimpparasite GimpParasite @@ -119,6 +130,10 @@ gimp_unit_get_symbol gimp_unit_get_abbreviation gimp_unit_get_singular gimp_unit_get_plural +GIMP_VALUE_HOLDS_UNIT + +GIMP_TYPE_UNIT +gimp_unit_get_type
@@ -126,7 +141,6 @@ gimp_unit_get_plural gimp_any_to_utf8 gimp_filename_to_utf8 gimp_utf8_strtrim -gimp_memsize_to_string gimp_escape_uline gimp_strip_uline
diff --git a/devel-docs/libgimpbase/tmpl/gimpmemsize.sgml b/devel-docs/libgimpbase/tmpl/gimpmemsize.sgml new file mode 100644 index 0000000000..6f6859849b --- /dev/null +++ b/devel-docs/libgimpbase/tmpl/gimpmemsize.sgml @@ -0,0 +1,52 @@ + +gimpmemsize + + + + + + + + + + + + + + + + + + + +@memsize: +@Returns: + + + + + + + +@string: +@memsize: +@Returns: + + + + + + + +@memsize: +@Returns: + + + + + + + +@value: + + diff --git a/devel-docs/libgimpbase/tmpl/gimpunit.sgml b/devel-docs/libgimpbase/tmpl/gimpunit.sgml index 84d9d65438..352f59baed 100644 --- a/devel-docs/libgimpbase/tmpl/gimpunit.sgml +++ b/devel-docs/libgimpbase/tmpl/gimpunit.sgml @@ -145,3 +145,11 @@ user-defined units. @Returns: + + + + + +@value: + + diff --git a/devel-docs/libgimpbase/tmpl/gimputils.sgml b/devel-docs/libgimpbase/tmpl/gimputils.sgml index 04300f55fd..86d1a8ee7e 100644 --- a/devel-docs/libgimpbase/tmpl/gimputils.sgml +++ b/devel-docs/libgimpbase/tmpl/gimputils.sgml @@ -45,15 +45,6 @@ Utilities of general interest @Returns: - - - - - -@memsize: -@Returns: - - diff --git a/libgimp/gimptile.c b/libgimp/gimptile.c index 42d1f0e9a5..d5c54a52e7 100644 --- a/libgimp/gimptile.c +++ b/libgimp/gimptile.c @@ -23,7 +23,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h" #include "libgimpbase/gimpprotocol.h" diff --git a/libgimp/gimpunitcache.c b/libgimp/gimpunitcache.c index 81d82d8a4c..af9daedeed 100644 --- a/libgimp/gimpunitcache.c +++ b/libgimp/gimpunitcache.c @@ -22,7 +22,7 @@ #include "config.h" -#include +#include #include "libgimpbase/gimpbasetypes.h" diff --git a/libgimpbase/Makefile.am b/libgimpbase/Makefile.am index 84fe20c3af..5fc401fa84 100644 --- a/libgimpbase/Makefile.am +++ b/libgimpbase/Makefile.am @@ -77,6 +77,8 @@ libgimpbase_2_0_la_SOURCES = \ gimpdatafiles.h \ gimpenv.c \ gimpenv.h \ + gimpmemsize.c \ + gimpmemsize.h \ gimpparasite.c \ gimpparasite.h \ gimpparasiteio.c \ @@ -101,6 +103,7 @@ libgimpbaseinclude_HEADERS = \ \ gimpdatafiles.h \ gimpenv.h \ + gimpmemsize.h \ gimpparasite.h \ gimpparasiteio.h \ gimpsignal.h \ diff --git a/libgimpbase/gimpbase-private.c b/libgimpbase/gimpbase-private.c index 6a32a50e17..c1c27187b6 100644 --- a/libgimpbase/gimpbase-private.c +++ b/libgimpbase/gimpbase-private.c @@ -22,7 +22,7 @@ #include "config.h" -#include +#include #include "libgimpbase/gimpbase.h" diff --git a/libgimpbase/gimpbase.def b/libgimpbase/gimpbase.def index 0b7e4c32ab..567b4a54a4 100644 --- a/libgimpbase/gimpbase.def +++ b/libgimpbase/gimpbase.def @@ -10,6 +10,9 @@ EXPORTS gimp_filename_to_utf8 gimp_gtkrc gimp_locale_directory + gimp_memsize_deserialize + gimp_memsize_get_type + gimp_memsize_serialize gimp_memsize_to_string gimp_parasite_compare gimp_parasite_copy @@ -45,6 +48,7 @@ EXPORTS gimp_unit_get_plural gimp_unit_get_singular gimp_unit_get_symbol + gimp_unit_get_type gimp_unit_new gimp_unit_set_deletion_flag gimp_utf8_strtrim diff --git a/libgimpbase/gimpbase.h b/libgimpbase/gimpbase.h index 9e221a3f75..8011c67ef0 100644 --- a/libgimpbase/gimpbase.h +++ b/libgimpbase/gimpbase.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/libgimpbase/gimpmemsize.c b/libgimpbase/gimpmemsize.c new file mode 100644 index 0000000000..52eded6bfe --- /dev/null +++ b/libgimpbase/gimpmemsize.c @@ -0,0 +1,247 @@ +/* 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 Lesser 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 Lesser 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 "config.h" + +#include + +#include + +#include "gimpmemsize.h" + +#include "libgimp/libgimp-intl.h" + + +static void memsize_to_string (const GValue *src_value, + GValue *dest_value); +static void string_to_memsize (const GValue *src_value, + GValue *dest_value); + + +GType +gimp_memsize_get_type (void) +{ + static GType memsize_type = 0; + + if (!memsize_type) + { + static const GTypeInfo type_info = { 0, }; + + memsize_type = g_type_register_static (G_TYPE_UINT64, "GimpMemsize", + &type_info, 0); + + g_value_register_transform_func (memsize_type, G_TYPE_STRING, + memsize_to_string); + g_value_register_transform_func (G_TYPE_STRING, memsize_type, + string_to_memsize); + } + + return memsize_type; +} + +/** + * gimp_memsize_serialize: + * @memsize: memory size in bytes + * + * Creates a string representation of a given memory size. This string + * can be parsed by gimp_memsize_deserialize() and can thus be used in + * config files. It should not be displayed to the user. If you need a + * nice human-readable string please use gimp_memsize_to_string(). + * + * Return value: A newly allocated string representation of @memsize. + * + * Since: GIMP 2.2 + **/ +gchar * +gimp_memsize_serialize (guint64 memsize) +{ + if (memsize > (1 << 30) && memsize % (1 << 30) == 0) + return g_strdup_printf ("%" G_GUINT64_FORMAT "G", memsize >> 30); + else if (memsize > (1 << 20) && memsize % (1 << 20) == 0) + return g_strdup_printf ("%" G_GUINT64_FORMAT "M", memsize >> 20); + else if (memsize > (1 << 10) && memsize % (1 << 10) == 0) + return g_strdup_printf ("%" G_GUINT64_FORMAT "k", memsize >> 10); + else + return g_strdup_printf ("%" G_GUINT64_FORMAT, memsize); +} + +/** + * gimp_memsize_deserialize: + * @string: a string as returned by gimp_memsize_serialize() + * @memsize: return location for memory size in bytes + * + * Parses a string representation of a memory size as returned by + * gimp_memsize_serialize(). + * + * Return value: %TRUE if the @string was successfully parsed and + * @memsize has been set, %FALSE otherwise. + * + * Since: GIMP 2.2 + **/ +gboolean +gimp_memsize_deserialize (const gchar *string, + guint64 *memsize) +{ + gchar *end; + guint64 size; + + g_return_val_if_fail (string != NULL, FALSE); + g_return_val_if_fail (memsize != NULL, FALSE); + + size = g_ascii_strtoull (string, &end, 0); + + if (size == G_MAXUINT64 && errno == ERANGE) + return FALSE; + + if (end && *end) + { + guint shift; + + switch (g_ascii_tolower (*end)) + { + case 'b': + shift = 0; + break; + case 'k': + shift = 10; + break; + case 'm': + shift = 20; + break; + case 'g': + shift = 30; + break; + default: + return FALSE; + } + + /* protect against overflow */ + if (shift) + { + guint64 limit = G_MAXUINT64 >> shift; + + if (size != (size & limit)) + return FALSE; + + size <<= shift; + } + } + + *memsize = size; + + return TRUE; +} + + +/** + * gimp_memsize_to_string: + * @memsize: A memory size in bytes. + * + * This function returns a human readable, translated representation + * of the passed @memsize. Large values are displayed using a + * reasonable memsize unit, e.g.: "345" becomes "345 Bytes", "4500" + * becomes "4.4 KB" and so on. + * + * Return value: A newly allocated human-readable, translated string. + **/ +gchar * +gimp_memsize_to_string (guint64 memsize) +{ +#if defined _MSC_VER && (_MSC_VER < 1300) +/* sorry, error C2520: conversion from unsigned __int64 to double not + * implemented, use signed __int64 + */ +# define CAST_DOUBLE (gdouble)(gint64) +#else +# define CAST_DOUBLE (gdouble) +#endif + + if (memsize < 1024) + { + return g_strdup_printf (_("%d Bytes"), (gint) memsize); + } + + if (memsize < 1024 * 10) + { + return g_strdup_printf (_("%.2f KB"), CAST_DOUBLE memsize / 1024.0); + } + else if (memsize < 1024 * 100) + { + return g_strdup_printf (_("%.1f KB"), CAST_DOUBLE memsize / 1024.0); + } + else if (memsize < 1024 * 1024) + { + return g_strdup_printf (_("%d KB"), (gint) memsize / 1024); + } + + memsize /= 1024; + + if (memsize < 1024 * 10) + { + return g_strdup_printf (_("%.2f MB"), CAST_DOUBLE memsize / 1024.0); + } + else if (memsize < 1024 * 100) + { + return g_strdup_printf (_("%.1f MB"), CAST_DOUBLE memsize / 1024.0); + } + else if (memsize < 1024 * 1024) + { + return g_strdup_printf (_("%d MB"), (gint) memsize / 1024); + } + + memsize /= 1024; + + if (memsize < 1024 * 10) + { + return g_strdup_printf (_("%.2f GB"), CAST_DOUBLE memsize / 1024.0); + } + else if (memsize < 1024 * 100) + { + return g_strdup_printf (_("%.1f GB"), CAST_DOUBLE memsize / 1024.0); + } + else + { + return g_strdup_printf (_("%d GB"), (gint) memsize / 1024); + } +#undef CAST_DOUBLE +} + + +static void +memsize_to_string (const GValue *src_value, + GValue *dest_value) +{ + g_value_take_string (dest_value, + gimp_memsize_serialize (g_value_get_uint64 (src_value))); +} + +static void +string_to_memsize (const GValue *src_value, + GValue *dest_value) +{ + const gchar *str; + guint64 memsize; + + str = g_value_get_string (src_value); + + if (! str || ! gimp_memsize_deserialize (str, &memsize)) + g_warning ("Can't convert string to GimpMemsize."); + + g_value_set_uint64 (dest_value, memsize); +} diff --git a/libgimpbase/gimpmemsize.h b/libgimpbase/gimpmemsize.h new file mode 100644 index 0000000000..733aeb39d0 --- /dev/null +++ b/libgimpbase/gimpmemsize.h @@ -0,0 +1,36 @@ +/* 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 Lesser 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 Lesser 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_MEMSIZE_H__ +#define __GIMP_MEMSIZE_H__ + + +#define GIMP_TYPE_MEMSIZE (gimp_memsize_get_type ()) +#define GIMP_VALUE_HOLDS_MEMSIZE(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_MEMSIZE)) + +GType gimp_memsize_get_type (void) G_GNUC_CONST; + +gchar * gimp_memsize_serialize (guint64 memsize); +gboolean gimp_memsize_deserialize (const gchar *string, + guint64 *memsize); + +gchar * gimp_memsize_to_string (guint64 memsize); + + +#endif /* __GIMP_MEMSIZE_H__ */ diff --git a/libgimpbase/gimpunit.c b/libgimpbase/gimpunit.c index 8d3127c40a..8aa1db7c46 100644 --- a/libgimpbase/gimpunit.c +++ b/libgimpbase/gimpunit.c @@ -22,7 +22,9 @@ #include "config.h" -#include +#include + +#include #include "gimpbasetypes.h" @@ -30,6 +32,76 @@ #include "gimpunit.h" +static void unit_to_string (const GValue *src_value, + GValue *dest_value); +static void string_to_unit (const GValue *src_value, + GValue *dest_value); + +GType +gimp_unit_get_type (void) +{ + static GType unit_type = 0; + + if (!unit_type) + { + static const GTypeInfo type_info = { 0, }; + + unit_type = g_type_register_static (G_TYPE_INT, "GimpUnit", + &type_info, 0); + + g_value_register_transform_func (unit_type, G_TYPE_STRING, + unit_to_string); + g_value_register_transform_func (G_TYPE_STRING, unit_type, + string_to_unit); + } + + return unit_type; +} + +static void +unit_to_string (const GValue *src_value, + GValue *dest_value) +{ + GimpUnit unit = (GimpUnit) g_value_get_int (src_value); + + g_value_set_string (dest_value, gimp_unit_get_identifier (unit)); +} + +static void +string_to_unit (const GValue *src_value, + GValue *dest_value) +{ + const gchar *str; + gint num_units; + gint i; + + str = g_value_get_string (src_value); + + if (!str || !*str) + goto error; + + num_units = gimp_unit_get_number_of_units (); + + for (i = GIMP_UNIT_PIXEL; i < num_units; i++) + if (strcmp (str, gimp_unit_get_identifier (i)) == 0) + break; + + if (i == num_units) + { + if (strcmp (str, gimp_unit_get_identifier (GIMP_UNIT_PERCENT)) == 0) + i = GIMP_UNIT_PERCENT; + else + goto error; + } + + g_value_set_int (dest_value, i); + return; + + error: + g_warning ("Can't convert string to GimpUnit."); +} + + /** * gimp_unit_get_number_of_units: * diff --git a/libgimpbase/gimpunit.h b/libgimpbase/gimpunit.h index da6824ba66..31218d525d 100644 --- a/libgimpbase/gimpunit.h +++ b/libgimpbase/gimpunit.h @@ -27,6 +27,11 @@ G_BEGIN_DECLS /* For information look into the C source or the html documentation */ +#define GIMP_TYPE_UNIT (gimp_unit_get_type ()) +#define GIMP_VALUE_HOLDS_UNIT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_UNIT)) + +GType gimp_unit_get_type (void) G_GNUC_CONST; + gint gimp_unit_get_number_of_units (void); gint gimp_unit_get_number_of_built_in_units (void) G_GNUC_CONST; diff --git a/libgimpbase/gimputils.c b/libgimpbase/gimputils.c index f8a663ce32..1b1010ff78 100644 --- a/libgimpbase/gimputils.c +++ b/libgimpbase/gimputils.c @@ -216,78 +216,6 @@ gimp_filename_to_utf8 (const gchar *filename) return filename_utf8; } -/** - * gimp_memsize_to_string: - * @memsize: A memory size in bytes. - * - * This function returns a human readable, translated representation - * of the passed @memsize. Large values are displayed using a - * reasonable memsize unit, e.g.: "345" becomes "345 Bytes", "4500" - * becomes "4.4 KB" and so on. - * - * Return value: A newly allocated human-readable, translated string. - **/ -gchar * -gimp_memsize_to_string (guint64 memsize) -{ -#if defined _MSC_VER && (_MSC_VER < 1300) -/* sorry, error C2520: conversion from unsigned __int64 to double not - * implemented, use signed __int64 - */ -# define CAST_DOUBLE (gdouble)(gint64) -#else -# define CAST_DOUBLE (gdouble) -#endif - - if (memsize < 1024) - { - return g_strdup_printf (_("%d Bytes"), (gint) memsize); - } - - if (memsize < 1024 * 10) - { - return g_strdup_printf (_("%.2f KB"), CAST_DOUBLE memsize / 1024.0); - } - else if (memsize < 1024 * 100) - { - return g_strdup_printf (_("%.1f KB"), CAST_DOUBLE memsize / 1024.0); - } - else if (memsize < 1024 * 1024) - { - return g_strdup_printf (_("%d KB"), (gint) memsize / 1024); - } - - memsize /= 1024; - - if (memsize < 1024 * 10) - { - return g_strdup_printf (_("%.2f MB"), CAST_DOUBLE memsize / 1024.0); - } - else if (memsize < 1024 * 100) - { - return g_strdup_printf (_("%.1f MB"), CAST_DOUBLE memsize / 1024.0); - } - else if (memsize < 1024 * 1024) - { - return g_strdup_printf (_("%d MB"), (gint) memsize / 1024); - } - - memsize /= 1024; - - if (memsize < 1024 * 10) - { - return g_strdup_printf (_("%.2f GB"), CAST_DOUBLE memsize / 1024.0); - } - else if (memsize < 1024 * 100) - { - return g_strdup_printf (_("%.1f GB"), CAST_DOUBLE memsize / 1024.0); - } - else - { - return g_strdup_printf (_("%d GB"), (gint) memsize / 1024); - } -#undef CAST_DOUBLE -} /** * gimp_strip_uline: diff --git a/libgimpbase/gimputils.h b/libgimpbase/gimputils.h index b607e2cfc5..346ba66d0c 100644 --- a/libgimpbase/gimputils.h +++ b/libgimpbase/gimputils.h @@ -29,8 +29,6 @@ gchar * gimp_any_to_utf8 (const gchar *str, ...) G_GNUC_PRINTF (3, 4); const gchar * gimp_filename_to_utf8 (const gchar *filename); -gchar * gimp_memsize_to_string (guint64 memsize); - gchar * gimp_strip_uline (const gchar *str); gchar * gimp_escape_uline (const gchar *str); diff --git a/libgimpconfig/gimpconfig-deserialize.c b/libgimpconfig/gimpconfig-deserialize.c index 23f2f9d780..6abc3815ae 100644 --- a/libgimpconfig/gimpconfig-deserialize.c +++ b/libgimpconfig/gimpconfig-deserialize.c @@ -560,8 +560,9 @@ gimp_config_deserialize_memsize (GValue *value, GParamSpec *prop_spec, GScanner *scanner) { - gchar *orig_cset_first = scanner->config->cset_identifier_first; - gchar *orig_cset_nth = scanner->config->cset_identifier_nth; + gchar *orig_cset_first = scanner->config->cset_identifier_first; + gchar *orig_cset_nth = scanner->config->cset_identifier_nth; + guint64 memsize; scanner->config->cset_identifier_first = G_CSET_DIGITS; scanner->config->cset_identifier_nth = G_CSET_DIGITS "gGmMkKbB"; @@ -574,10 +575,12 @@ gimp_config_deserialize_memsize (GValue *value, scanner->config->cset_identifier_first = orig_cset_first; scanner->config->cset_identifier_nth = orig_cset_nth; - if (gimp_memsize_set_from_string (value, scanner->value.v_identifier)) - return G_TOKEN_RIGHT_PAREN; - else + if (! gimp_memsize_deserialize (scanner->value.v_identifier, &memsize)) return G_TOKEN_NONE; + + g_value_set_uint64 (value, memsize); + + return G_TOKEN_RIGHT_PAREN; } static GTokenType diff --git a/plug-ins/common/uniteditor.c b/plug-ins/common/uniteditor.c index 59e51f54ed..99413c926d 100644 --- a/plug-ins/common/uniteditor.c +++ b/plug-ins/common/uniteditor.c @@ -519,18 +519,18 @@ unit_editor_dialog (void) gimp_ui_init ("uniteditor", FALSE); list_store = gtk_list_store_new (NUM_COLUMNS, - G_TYPE_BOOLEAN, - G_TYPE_STRING, - G_TYPE_DOUBLE, - G_TYPE_INT, - G_TYPE_STRING, - G_TYPE_STRING, - G_TYPE_STRING, - G_TYPE_STRING, - G_TYPE_INT, - G_TYPE_BOOLEAN, - GDK_TYPE_COLOR, - GDK_TYPE_COLOR); + G_TYPE_BOOLEAN, /* SAVE */ + G_TYPE_STRING, /* IDENTIFIER */ + G_TYPE_DOUBLE, /* FACTOR */ + G_TYPE_INT, /* DIGITS */ + G_TYPE_STRING, /* SYMBOL */ + G_TYPE_STRING, /* ABBREVIATION */ + G_TYPE_STRING, /* SINGULAR */ + G_TYPE_STRING, /* PLURAL */ + GIMP_TYPE_UNIT, /* UNIT */ + G_TYPE_BOOLEAN, /* USER_UNIT */ + GDK_TYPE_COLOR); /* BG_COLOR */ + tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store)); g_object_unref (list_store); @@ -607,11 +607,12 @@ unit_editor_dialog (void) if (i == SAVE) continue; - col = gtk_tree_view_column_new_with_attributes (gettext (columns[i].title), - gtk_cell_renderer_text_new (), - "text", i, - "cell-background-gdk", BG_COLOR, - NULL); + col = + gtk_tree_view_column_new_with_attributes (gettext (columns[i].title), + gtk_cell_renderer_text_new (), + "text", i, + "cell-background-gdk", BG_COLOR, + NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col); diff --git a/plug-ins/help/domain.c b/plug-ins/help/domain.c index a25d8d7ee5..968da5778f 100644 --- a/plug-ins/help/domain.c +++ b/plug-ins/help/domain.c @@ -25,7 +25,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h" diff --git a/plug-ins/help/gimphelp.c b/plug-ins/help/gimphelp.c index a25d8d7ee5..968da5778f 100644 --- a/plug-ins/help/gimphelp.c +++ b/plug-ins/help/gimphelp.c @@ -25,7 +25,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h" diff --git a/plug-ins/help/gimphelpdomain.c b/plug-ins/help/gimphelpdomain.c index a25d8d7ee5..968da5778f 100644 --- a/plug-ins/help/gimphelpdomain.c +++ b/plug-ins/help/gimphelpdomain.c @@ -25,7 +25,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h" diff --git a/plug-ins/help/gimphelplocale.c b/plug-ins/help/gimphelplocale.c index a25d8d7ee5..968da5778f 100644 --- a/plug-ins/help/gimphelplocale.c +++ b/plug-ins/help/gimphelplocale.c @@ -25,7 +25,7 @@ #include -#include +#include #include "libgimpbase/gimpbase.h"