added new function gimp_object_name_collate() which compares two object

2003-10-09  Sven Neumann  <sven@gimp.org>

	* app/core/gimpobject.[ch]: added new function
	gimp_object_name_collate() which compares two object names for
	ordering using the linguistically correct rules for the current
	locale and does some caching to speed up subsequent calls.

	* app/core/gimpdatalist.c (gimp_data_list_data_compare_func): use
	gimp_object_name_collate() from here.

	* app/core/gimplist.[ch]: added convenience function
	gimp_list_sort_by_name.

	* app/text/gimpfontlist.c (gimp_font_list_restore): use
	gimp_list_sort_by_name() instead of g_utf8_collate.
This commit is contained in:
Sven Neumann 2003-10-09 13:35:15 +00:00 committed by Sven Neumann
parent 482aa11bfb
commit f0f3ea36b8
7 changed files with 186 additions and 50 deletions

View File

@ -1,3 +1,19 @@
2003-10-09 Sven Neumann <sven@gimp.org>
* app/core/gimpobject.[ch]: added new function
gimp_object_name_collate() which compares two object names for
ordering using the linguistically correct rules for the current
locale and does some caching to speed up subsequent calls.
* app/core/gimpdatalist.c (gimp_data_list_data_compare_func): use
gimp_object_name_collate() from here.
* app/core/gimplist.[ch]: added convenience function
gimp_list_sort_by_name.
* app/text/gimpfontlist.c (gimp_font_list_restore): use
gimp_list_sort_by_name() instead of g_utf8_collate.
2003-10-09 Michael Natterer <mitch@gimp.org>
* app/core/gimpviewable.h: added GIMP_VIEWABLE_MAX_BUTTON_SIZE

View File

@ -84,7 +84,7 @@ static void
gimp_data_list_class_init (GimpDataListClass *klass)
{
GimpContainerClass *container_class;
container_class = GIMP_CONTAINER_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
@ -197,6 +197,6 @@ gimp_data_list_data_compare_func (gconstpointer first,
if (first_data->internal != second_data->internal)
return first_data->internal ? -1 : 1;
return strcmp (((const GimpObject *) first)->name,
((const GimpObject *) second)->name);
return gimp_object_name_collate ((GimpObject *) first,
(GimpObject *) second);
}

View File

@ -285,6 +285,17 @@ gimp_list_get_child_index (const GimpContainer *container,
return g_list_index (list->list, (gpointer) object);
}
/**
* gimp_list_new:
* @children_type: the #GType of objects the list is going to hold
* @policy: the #GimpContainerPolicy for the new list
*
* Creates a new #GimpList object. Since #GimpList is a #GimpContainer
* implementation, it holds GimpObjects. Thus @children_type must be
* GIMP_TYPE_OBJECT or a type derived from it.
*
* Return value: a new #GimpList object
**/
GimpContainer *
gimp_list_new (GType children_type,
GimpContainerPolicy policy)
@ -303,6 +314,12 @@ gimp_list_new (GType children_type,
return GIMP_CONTAINER (list);
}
/**
* gimp_list_reverse:
* @list: a #GimpList
*
* Reverses the order of elements in a #GimpList.
**/
void
gimp_list_reverse (GimpList *list)
{
@ -316,6 +333,14 @@ gimp_list_reverse (GimpList *list)
}
}
/**
* gimp_list_sort:
* @list: a #GimpList
* @compare_func: a #GCompareFunc
*
* Sorts the elements of a #GimpList according to the given @compare_func.
* See g_list_sort() for a detailed description of this function.
**/
void
gimp_list_sort (GimpList *list,
GCompareFunc compare_func)
@ -331,6 +356,20 @@ gimp_list_sort (GimpList *list,
}
}
/**
* gimp_list_sort_by_name:
* @list: a #GimpList
*
* Sorts the #GimpObject elements of a #GimpList by their names.
**/
void
gimp_list_sort_by_name (GimpList *list)
{
g_return_if_fail (GIMP_IS_LIST (list));
gimp_list_sort (list, (GCompareFunc) gimp_object_name_collate);
}
void
gimp_list_uniquefy_name (GimpList *gimp_list,
GimpObject *object,

View File

@ -56,6 +56,7 @@ GimpContainer * gimp_list_new (GType children_type,
void gimp_list_reverse (GimpList *list);
void gimp_list_sort (GimpList *list,
GCompareFunc compare_func);
void gimp_list_sort_by_name (GimpList *list);
void gimp_list_uniquefy_name (GimpList *gimp_list,
GimpObject *object,
gboolean use_set_name);

View File

@ -44,21 +44,22 @@ enum
};
static void gimp_object_class_init (GimpObjectClass *klass);
static void gimp_object_init (GimpObject *object);
static void gimp_object_class_init (GimpObjectClass *klass);
static void gimp_object_init (GimpObject *object);
static void gimp_object_dispose (GObject *object);
static void gimp_object_finalize (GObject *object);
static void gimp_object_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_object_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static gsize gimp_object_real_get_memsize (GimpObject *object,
gsize *gui_size);
static void gimp_object_dispose (GObject *object);
static void gimp_object_finalize (GObject *object);
static void gimp_object_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_object_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static gsize gimp_object_real_get_memsize (GimpObject *object,
gsize *gui_size);
static void gimp_object_name_normalize (GimpObject *object);
static guint object_signals[LAST_SIGNAL] = { 0 };
@ -66,7 +67,7 @@ static guint object_signals[LAST_SIGNAL] = { 0 };
static GObjectClass *parent_class = NULL;
GType
GType
gimp_object_get_type (void)
{
static GType object_type = 0;
@ -87,7 +88,7 @@ gimp_object_get_type (void)
};
object_type = g_type_register_static (G_TYPE_OBJECT,
"GimpObject",
"GimpObject",
&object_info, 0);
}
@ -141,7 +142,8 @@ gimp_object_class_init (GimpObjectClass *klass)
static void
gimp_object_init (GimpObject *object)
{
object->name = NULL;
object->name = NULL;
object->normalized = NULL;
}
static void
@ -170,6 +172,14 @@ gimp_object_finalize (GObject *object)
gimp_object = GIMP_OBJECT (object);
if (gimp_object->normalized)
{
if (gimp_object->normalized != gimp_object->name)
g_free (gimp_object->normalized);
gimp_object->normalized = NULL;
}
if (gimp_object->name)
{
g_free (gimp_object->name);
@ -221,6 +231,14 @@ gimp_object_get_property (GObject *object,
}
}
/**
* gimp_object_set_name:
* @object: a #GimpObject
* @name: the @object's new name
*
* Sets the @object's name. Takes care of freeing the old name and
* emitting the "name_changed" signal if the old and new name differ.
**/
void
gimp_object_set_name (GimpObject *object,
const gchar *name)
@ -238,9 +256,15 @@ gimp_object_set_name (GimpObject *object,
gimp_object_name_changed (object);
}
/* A safe version of gimp_object_set_name() that takes care
* of newlines and overly long names.
*/
/**
* gimp_object_set_name_safe:
* @object: a #GimpObject
* @name: the @object's new name
*
* A safe version of gimp_object_set_name() that takes care of
* handling newlines and overly long names. The actual name set
* may be different to the @name you pass.
**/
void
gimp_object_set_name_safe (GimpObject *object,
const gchar *name)
@ -271,9 +295,64 @@ gimp_object_name_changed (GimpObject *object)
{
g_return_if_fail (GIMP_IS_OBJECT (object));
if (object->normalized)
{
if (object->normalized != object->name)
g_free (object->normalized);
object->normalized = NULL;
}
g_signal_emit (object, object_signals[NAME_CHANGED], 0);
}
/**
* gimp_object_name_collate:
* @object1: a #GimpObject
* @object2: another #GimpObject
*
* Compares two object names for ordering using the linguistically
* correct rules for the current locale. It caches the normalized
* version of the object name to speed up subsequent calls.
*
* Return value: -1 if object1 compares before object2,
* 0 if they compare equal,
* 1 if object1 compares after object2.
**/
gint
gimp_object_name_collate (GimpObject *object1,
GimpObject *object2)
{
if (! object1->normalized)
gimp_object_name_normalize (object1);
if (! object2->normalized)
gimp_object_name_normalize (object2);
return strcoll (object1->normalized, object2->normalized);
}
static void
gimp_object_name_normalize (GimpObject *object)
{
g_return_if_fail (object->normalized == NULL);
if (object->name)
{
gchar *key = g_utf8_collate_key (object->name, -1);
if (strcmp (key, object->name))
{
object->normalized = key;
}
else
{
g_free (key);
object->normalized = object->name;
}
}
}
#define DEBUG_MEMSIZE 1
@ -375,3 +454,4 @@ gimp_object_real_get_memsize (GimpObject *object,
return memsize + gimp_g_object_get_memsize ((GObject *) object);
}

View File

@ -35,6 +35,9 @@ struct _GimpObject
GObject parent_instance;
gchar *name;
/*< private >*/
gchar *normalized;
};
struct _GimpObjectClass
@ -60,6 +63,9 @@ void gimp_object_set_name_safe (GimpObject *object,
const gchar *name);
void gimp_object_name_changed (GimpObject *object);
gint gimp_object_name_collate (GimpObject *object1,
GimpObject *object2);
gsize gimp_object_get_memsize (GimpObject *object,
gsize *gui_size);
gsize gimp_g_object_get_memsize (GObject *object);

View File

@ -46,19 +46,16 @@
#endif
static void gimp_font_list_class_init (GimpFontListClass *klass);
static void gimp_font_list_init (GimpFontList *list);
static void gimp_font_list_class_init (GimpFontListClass *klass);
static void gimp_font_list_init (GimpFontList *list);
static gint gimp_font_list_font_compare_func (gconstpointer first,
gconstpointer second);
static void gimp_font_list_add_font (GimpFontList *list,
PangoContext *context,
PangoFontDescription *desc);
static void gimp_font_list_add_font (GimpFontList *list,
PangoContext *context,
PangoFontDescription *desc);
static void gimp_font_list_load_names (GimpFontList *list,
PangoFontMap *fontmap,
PangoContext *context);
static void gimp_font_list_load_names (GimpFontList *list,
PangoFontMap *fontmap,
PangoContext *context);
static GimpListClass *parent_class = NULL;
@ -85,7 +82,7 @@ gimp_font_list_get_type (void)
};
list_type = g_type_register_static (GIMP_TYPE_LIST,
"GimpFontList",
"GimpFontList",
&list_info, 0);
}
@ -131,7 +128,7 @@ gimp_font_list_restore (GimpFontList *list)
g_return_if_fail (GIMP_IS_FONT_LIST (list));
fontmap = pango_ft2_font_map_new ();
fontmap = pango_ft2_font_map_new ();
pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap),
list->xresolution, list->yresolution);
@ -143,19 +140,11 @@ gimp_font_list_restore (GimpFontList *list)
gimp_font_list_load_names (list, fontmap, context);
g_object_unref (context);
gimp_list_sort (GIMP_LIST (list), gimp_font_list_font_compare_func);
gimp_list_sort_by_name (GIMP_LIST (list));
gimp_container_thaw (GIMP_CONTAINER (list));
}
static gint
gimp_font_list_font_compare_func (gconstpointer first,
gconstpointer second)
{
return g_utf8_collate (((const GimpObject *) first)->name,
((const GimpObject *) second)->name);
}
static void
gimp_font_list_add_font (GimpFontList *list,
PangoContext *context,
@ -195,9 +184,13 @@ gimp_font_list_make_alias (GimpFontList *list,
PangoFontDescription *desc = pango_font_description_new ();
pango_font_description_set_family (desc, family);
pango_font_description_set_style (desc, italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
pango_font_description_set_style (desc,
italic ?
PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
pango_font_description_set_variant (desc, PANGO_VARIANT_NORMAL);
pango_font_description_set_weight (desc, bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
pango_font_description_set_weight (desc,
bold ?
PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
pango_font_description_set_stretch (desc, PANGO_STRETCH_NORMAL);
gimp_font_list_add_font (list, context, desc);
@ -234,7 +227,8 @@ gimp_font_list_font_desc_from_pattern (FcPattern *pattern)
desc = pango_font_description_new ();
g_assert (FcPatternGetString (pattern, FC_FAMILY, 0, (FcChar8 **) &s) == FcResultMatch);
g_assert (FcPatternGetString (pattern,
FC_FAMILY, 0, (FcChar8 **) &s) == FcResultMatch);
pango_font_description_set_family (desc, s);
@ -294,7 +288,7 @@ gimp_font_list_load_names (GimpFontList *list,
pat = FcPatternCreate ();
fontset = FcFontList (NULL, pat, os);
FcPatternDestroy (pat);
FcObjectSetDestroy (os);
@ -326,7 +320,7 @@ gimp_font_list_load_names (GimpFontList *list,
for (i = 0; i < n_families; i++)
{
pango_font_family_list_faces (families[i], &faces, &n_faces);
for (j = 0; j < n_faces; j++)
{
PangoFontDescription *desc;