[Nautilus-list] working ellipsizing patch



Hi,

This one is actually tested. ;-)

Havoc

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/eel/ChangeLog,v
retrieving revision 1.243
diff -u -p -u -r1.243 ChangeLog
--- ChangeLog	2002/01/10 15:54:29	1.243
+++ ChangeLog	2002/01/12 00:29:13
@@ -1,3 +1,19 @@
+2002-01-11  Havoc Pennington  <hp redhat com>
+
+	* eel/eel.h: add eel-pango-extensions.h
+
+	* eel/eel-pango-extensions.c
+	(eel_pango_layout_set_text_ellipsized): put ellipsize code back in
+	here and port to Pango
+	
+	* eel/eel-ellipsizing-label.c: re-enable ellipsization in the
+	widget, change bad hack implementation to different bad hack
+	implementation to avoid queueing a resize in size_allocate
+	(real_style_set): remove style_set handler because it results in a
+	queue_resize anyway
+	(real_size_allocate): auto-select ellipsize mode based on label
+	alignment
+
 Thu Jan 10 10:53:52 2002  Owen Taylor  <otaylor redhat com>
 
 	* eel/eel-gnome-extensions.c
Index: eel/eel-ellipsizing-label.c
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-ellipsizing-label.c,v
retrieving revision 1.10
diff -u -p -u -r1.10 eel-ellipsizing-label.c
--- eel/eel-ellipsizing-label.c	2002/01/08 22:37:06	1.10
+++ eel/eel-ellipsizing-label.c	2002/01/12 00:29:13
@@ -26,6 +26,7 @@
 #include "eel-ellipsizing-label.h"
 
 #include "eel-gtk-macros.h"
+#include "eel-pango-extensions.h"
 #include "eel-string.h"
 
 struct EelEllipsizingLabelDetails
@@ -66,29 +67,9 @@ eel_ellipsizing_label_new (const char *s
 	return GTK_WIDGET (label);
 }
 
-static void
-recompute_ellipsized_text (EelEllipsizingLabel *label, int width)
-{
-#if GNOME2_CONVERSION_COMPLETE
-	char *ellipsized_text;
-
-	if (label->details->full_text == NULL) {
-		ellipsized_text = NULL;
-	} else {
-		ellipsized_text = eel_string_ellipsize (label->details->full_text, 
-							GTK_WIDGET (label)->style->font, 
-							width,
-							EEL_ELLIPSIZE_MIDDLE);
-	}
-
-	gtk_label_set_text (GTK_LABEL (label), ellipsized_text);
-	g_free (ellipsized_text);
-#endif
-}
-
 void
 eel_ellipsizing_label_set_text (EelEllipsizingLabel *label, 
-				     const char *string)
+				const char          *string)
 {
 	g_return_if_fail (EEL_IS_ELLIPSIZING_LABEL (label));
 
@@ -99,7 +80,8 @@ eel_ellipsizing_label_set_text (EelEllip
 	g_free (label->details->full_text);
 	label->details->full_text = g_strdup (string);
 
-	recompute_ellipsized_text (label, GTK_WIDGET (label)->allocation.width);
+	/* Queues a resize as side effect */
+	gtk_label_set_text (GTK_LABEL (label), label->details->full_text);
 }
 
 static void
@@ -114,19 +96,60 @@ real_size_request (GtkWidget *widget, Gt
 static void	
 real_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
 {
-	recompute_ellipsized_text (EEL_ELLIPSIZING_LABEL (widget), allocation->width);
+	EelEllipsizingLabel *label;
+
+	label = EEL_ELLIPSIZING_LABEL (widget);
+	
+	/* This is the bad hack of the century, using private
+	 * GtkLabel layout object. If the layout is NULL
+	 * then it got blown away since size request,
+	 * we just punt in that case, I don't know what to do really.
+	 */
+
+	if (GTK_LABEL (label)->layout != NULL) {
+		if (label->details->full_text == NULL) {
+			pango_layout_set_text (GTK_LABEL (label)->layout, "", -1);
+		} else {
+			EelEllipsizeMode mode;
+
+			if (ABS (GTK_MISC (label)->xalign - 0.5) < 1e-12)
+				mode = EEL_ELLIPSIZE_MIDDLE;
+			else if (GTK_MISC (label)->xalign < 0.5)
+				mode = EEL_ELLIPSIZE_END;
+			else
+				mode = EEL_ELLIPSIZE_START;
+			
+			eel_pango_layout_set_text_ellipsized (GTK_LABEL (label)->layout,
+							      label->details->full_text,
+							      allocation->width,
+							      mode);
+		}
+	}
 	
 	EEL_CALL_PARENT (GTK_WIDGET_CLASS, size_allocate, (widget, allocation));
 }
 
-static void 
-real_style_set (GtkWidget *widget, GtkStyle  *previous_style)
+static gboolean
+real_expose_event (GtkWidget *widget, GdkEventExpose *event)
 {
-	recompute_ellipsized_text (EEL_ELLIPSIZING_LABEL (widget), widget->allocation.width);
+	EelEllipsizingLabel *label;
+	GtkRequisition req;
 	
-	EEL_CALL_PARENT (GTK_WIDGET_CLASS, style_set, (widget, previous_style));
+	label = EEL_ELLIPSIZING_LABEL (widget);
+
+	/* push/pop the actual size so expose draws in the right
+	 * place, yes this is bad hack central. Here we assume the
+	 * ellipsized text has been set on the layout in size_allocate
+	 */
+	EEL_CALL_PARENT (GTK_WIDGET_CLASS, size_request, (widget, &req));
+	widget->requisition.width = req.width;
+	EEL_CALL_PARENT (GTK_WIDGET_CLASS, expose_event, (widget, event));
+	widget->requisition.width = 0;
+
+	return FALSE;
 }
 
+
 static void
 eel_ellipsizing_label_class_init (EelEllipsizingLabelClass *klass)
 {
@@ -138,5 +161,5 @@ eel_ellipsizing_label_class_init (EelEll
 
 	widget_class->size_request = real_size_request;
 	widget_class->size_allocate = real_size_allocate;
-  	widget_class->style_set = real_style_set;
+	widget_class->expose_event = real_expose_event;
 }
Index: eel/eel-lib-self-check-functions.h
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-lib-self-check-functions.h,v
retrieving revision 1.13
diff -u -p -u -r1.13 eel-lib-self-check-functions.h
--- eel/eel-lib-self-check-functions.h	2002/01/08 22:37:06	1.13
+++ eel/eel-lib-self-check-functions.h	2002/01/12 00:29:13
@@ -45,6 +45,7 @@ void eel_run_lib_self_checks (void);
 	macro (eel_self_check_gdk_extensions) \
 	macro (eel_self_check_gdk_pixbuf_extensions) \
 	macro (eel_self_check_glib_extensions) \
+        macro (eel_self_check_pango_extensions) \
 	macro (eel_self_check_preferences) \
 	macro (eel_self_check_string) \
 	macro (eel_self_check_string_list) \
Index: eel/eel-pango-extensions.c
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-pango-extensions.c,v
retrieving revision 1.4
diff -u -p -u -r1.4 eel-pango-extensions.c
--- eel/eel-pango-extensions.c	2002/01/08 22:37:06	1.4
+++ eel/eel-pango-extensions.c	2002/01/12 00:29:13
@@ -27,8 +27,17 @@
 #include <config.h>
 #include "eel-pango-extensions.h"
 
+#include "eel-self-checks.h"
+
+#include <pango/pango-attributes.h>
+#include <pango/pango-break.h>
+#include <pango/pango-context.h>
+#include <gtk/gtkmain.h>
+
 #include <string.h>
 
+void eel_self_check_pango_extensions (void);
+
 PangoAttrList *
 eel_pango_attr_list_copy_or_create (PangoAttrList *attr_list)
 {
@@ -88,3 +97,500 @@ eel_pango_layout_set_font_desc_from_stri
 	pango_font_description_free (desc);
 }
 
+
+
+#define ELLIPSIS "..."
+
+/* Caution: this is an _expensive_ function */
+static int
+measure_string_width (const char   *string,
+		      PangoContext *context,
+		      PangoAttrList *list)
+{
+	PangoLayout *layout;
+	int width;
+	
+	layout = pango_layout_new (context);
+	pango_layout_set_text (layout, string, -1);
+	pango_layout_set_attributes (layout, list);
+	pango_layout_get_pixel_size (layout, &width, NULL);
+	g_object_unref (G_OBJECT (layout));
+
+	return width;
+}
+
+/* this is also plenty slow */
+static void
+compute_character_widths (const char   *string,
+			  PangoContext *context,
+			  PangoAttrList *list,
+			  int          *char_len_return,
+			  int         **widths_return,
+			  int         **cuts_return)
+{
+	int *widths;
+	int *offsets;
+	int *cuts;
+	int char_len;
+	int byte_len;
+	const char *p;
+	int i;
+	PangoLayout *layout;
+	PangoLayoutIter *iter;
+	PangoLogAttr *attrs;
+	
+#define BEGINS_UTF8_CHAR(x) (((x) & 0xc0) != 0x80)
+	
+	char_len = g_utf8_strlen (string, -1);
+	byte_len = strlen (string);
+	
+	widths = g_new (int, char_len);
+	offsets = g_new (int, byte_len);
+
+	/* Create a translation table from byte index to char offset */
+	p = string;
+	i = 0;
+	while (*p) {
+		int byte_index = p - string;
+		
+		if (BEGINS_UTF8_CHAR (*p)) {
+			offsets[byte_index] = i;
+			++i;
+		} else {
+			offsets[byte_index] = G_MAXINT; /* segv if we try to use this */
+		}
+		
+		++p;
+	}
+
+	/* Now fill in the widths array */
+	layout = pango_layout_new (context);
+	pango_layout_set_text (layout, string, -1);
+	pango_layout_set_attributes (layout, list);
+	
+	iter = pango_layout_get_iter (layout);
+
+	do {
+		PangoRectangle extents;
+		int byte_index;
+
+		byte_index = pango_layout_iter_get_index (iter);
+
+		if (byte_index < byte_len) {
+			pango_layout_iter_get_char_extents (iter, &extents);
+			
+			g_assert (BEGINS_UTF8_CHAR (string[byte_index]));
+			g_assert (offsets[byte_index] < char_len);
+			
+			widths[offsets[byte_index]] = PANGO_PIXELS (extents.width);
+		}
+		
+	} while (pango_layout_iter_next_char (iter));
+
+	pango_layout_iter_free (iter);
+	g_object_unref (G_OBJECT (layout));
+
+	g_free (offsets);
+	
+	*widths_return = widths;
+
+	/* Now compute character offsets that are legitimate places to
+	 * chop the string
+	 */
+	attrs = g_new (PangoLogAttr, char_len + 1);
+	
+	pango_get_log_attrs (string, byte_len, -1,
+			     pango_context_get_language (context),
+			     attrs,
+			     char_len + 1);
+
+	cuts = g_new (int, char_len);
+	i = 0;
+	while (i < char_len) {
+		cuts[i] = attrs[i].is_cursor_position;
+
+		++i;
+	}
+
+	g_free (attrs);
+
+	*cuts_return = cuts;
+
+	*char_len_return = char_len;
+}
+
+
+static char *
+eel_string_ellipsize_end (const char *string, PangoLayout *layout, int width)
+{
+	int resulting_width;
+	int *cuts;
+	int *widths;
+	int char_len;
+	const char *p;
+	int truncate_offset;
+	PangoAttrList *list;
+	PangoContext *context;
+
+	context = pango_layout_get_context (layout);
+	list = pango_layout_get_attributes (layout);
+	
+	/* Zero-length string can't get shorter - catch this here to
+	 * avoid expensive calculations
+	 */
+	if (*string == '\0')
+		return g_strdup ("");
+
+	/* I'm not sure if this short-circuit is a net win; it might be better
+	 * to just dump this, and always do the compute_character_widths() etc.
+	 * down below.
+	 */
+	resulting_width = measure_string_width (string, context, list);
+
+	if (resulting_width <= width) {
+		/* String is already short enough. */
+		return g_strdup (string);
+	}
+
+	/* Remove width of an ellipsis */
+	width -= measure_string_width (ELLIPSIS, context, list);
+
+	if (width < 0) {
+		/* No room even for an ellipsis. */
+		return g_strdup ("");
+	}
+
+	/* Our algorithm involves removing enough chars from the string to bring
+	 * the width to the required small size. However, due to ligatures,
+	 * combining characters, etc., it's not guaranteed that the algorithm
+	 * always works 100%. It's sort of a heuristic thing. It should work
+	 * nearly all the time... but I wouldn't put in
+	 * g_assert (width of resulting string < width).
+	 *
+	 * Hmm, another thing that this breaks with is explicit line breaks
+	 * in "string"
+	 */
+
+	compute_character_widths (string, context, list, &char_len, &widths, &cuts);
+
+        for (truncate_offset = 1; truncate_offset < char_len; truncate_offset++) {
+
+        	resulting_width -= widths[truncate_offset];
+
+        	if (resulting_width <= width &&
+		    cuts[truncate_offset]) {
+			break;
+        	}
+        }
+
+	g_free (cuts);
+	g_free (widths);
+	
+	p = g_utf8_offset_to_pointer (string, truncate_offset);
+	
+	return g_strconcat (ELLIPSIS, p, NULL);
+}
+
+static char *
+eel_string_ellipsize_start (const char *string, PangoLayout *layout, int width)
+{
+	int resulting_width;
+	int *cuts;
+	int *widths;
+	int char_len;
+	const char *p;
+	int truncate_offset;
+	char *result;
+	PangoAttrList *list;
+	PangoContext *context;
+
+	context = pango_layout_get_context (layout);
+	list = pango_layout_get_attributes (layout);
+	
+	/* See explanatory comments in ellipsize_start */
+	
+	if (*string == '\0')
+		return g_strdup ("");
+
+	resulting_width = measure_string_width (string, context, list);
+	
+	if (resulting_width <= width) {
+		return g_strdup (string);
+	}
+
+	width -= measure_string_width (ELLIPSIS, context, list);
+
+	if (width < 0) {
+		return g_strdup ("");
+	}
+	
+	compute_character_widths (string, context, list, &char_len, &widths, &cuts);
+	
+        for (truncate_offset = char_len - 1; truncate_offset > 0; truncate_offset--) {
+        	resulting_width -= widths[truncate_offset];
+        	if (resulting_width <= width &&
+		    cuts[truncate_offset]) {
+			break;
+        	}
+        }
+
+	g_free (cuts);
+	g_free (widths);
+
+	p = g_utf8_offset_to_pointer (string, truncate_offset);
+	
+	result = g_malloc ((p - string) + strlen (ELLIPSIS) + 1);
+	memcpy (result, string, (p - string));
+	strcpy (result + (p - string), ELLIPSIS);
+
+	return result;
+}
+
+static char *
+eel_string_ellipsize_middle (const char *string, PangoLayout *layout, int width)
+{
+	int resulting_width;
+	int *cuts;
+	int *widths;
+	int char_len;
+	int starting_fragment_byte_len;
+	int ending_fragment_byte_index;
+	int starting_fragment_length;
+	int ending_fragment_offset;
+	char *result;
+	PangoAttrList *list;
+	PangoContext *context;
+
+	context = pango_layout_get_context (layout);
+	list = pango_layout_get_attributes (layout);
+
+	
+	/* See explanatory comments in ellipsize_start */
+	
+	if (*string == '\0')
+		return g_strdup ("");
+
+	resulting_width = measure_string_width (string, context, list);
+	
+	if (resulting_width <= width) {
+		return g_strdup (string);
+	}
+
+	width -= measure_string_width (ELLIPSIS, context, list);
+
+	if (width < 0) {
+		return g_strdup ("");
+	}
+	
+	compute_character_widths (string, context, list, &char_len, &widths, &cuts);
+	
+	starting_fragment_length = char_len / 2;
+	ending_fragment_offset = starting_fragment_length + 1;
+	
+	/* Shave off a character at a time from the first and the second half
+	 * until we can fit
+	 */
+	resulting_width -= widths[ending_fragment_offset - 1];
+	
+	/* depending on whether the original string length is odd or even, start by
+	 * shaving off the characters from the starting or ending fragment
+	 */
+	switch (char_len % 2) {
+	        while (starting_fragment_length > 0 || ending_fragment_offset < char_len) {
+	case 0:
+			if (resulting_width <= width &&
+			    cuts[ending_fragment_offset] &&
+			    cuts[starting_fragment_length]) {
+				break;
+			}
+
+			if (starting_fragment_length > 0) {
+				resulting_width -= widths[starting_fragment_length];
+				starting_fragment_length--;
+			}
+	case 1:
+			if (resulting_width <= width &&
+			    cuts[ending_fragment_offset] &&
+			    cuts[starting_fragment_length]) {
+				break;
+			}
+
+			if (ending_fragment_offset < char_len) {
+				resulting_width -= widths[ending_fragment_offset];
+				ending_fragment_offset++;
+			}
+	        }
+	}
+
+	g_free (cuts);
+	g_free (widths);	
+	
+	/* patch the two fragments together with an ellipsis */
+	result = g_malloc (strlen (string) + strlen (ELLIPSIS) + 1); /* a bit wasteful, no biggie */
+
+	starting_fragment_byte_len = g_utf8_offset_to_pointer (string, starting_fragment_length) - string;
+	ending_fragment_byte_index = g_utf8_offset_to_pointer (string, ending_fragment_offset) - string;
+	
+	memcpy (result, string, starting_fragment_byte_len);
+	strcpy (result + starting_fragment_byte_len, ELLIPSIS);
+	strcpy (result + starting_fragment_byte_len + strlen (ELLIPSIS), string + ending_fragment_byte_index);
+
+	return result;
+}
+
+
+/**
+ * eel_pango_layout_set_text_ellipsized
+ *
+ * @layout: a pango layout
+ * @string: A a string to be ellipsized.
+ * @width: Desired maximum width in points.
+ * @mode: The desired ellipsizing mode.
+ * 
+ * Truncates a string if required to fit in @width and sets it on the
+ * layout. Truncation involves removing characters from the start, middle or end
+ * respectively and replacing them with "...". Algorithm is a bit
+ * fuzzy, won't work 100%.
+ * 
+ */
+void
+eel_pango_layout_set_text_ellipsized (PangoLayout  *layout,
+				      const char   *string,
+				      int           width,
+				      EelEllipsizeMode mode)
+{
+	char *s;
+
+	g_return_if_fail (PANGO_IS_LAYOUT (layout));
+	g_return_if_fail (string != NULL);
+	g_return_if_fail (width >= 0);
+	
+	switch (mode) {
+	case EEL_ELLIPSIZE_START:
+		s = eel_string_ellipsize_start (string, layout, width);
+		break;
+	case EEL_ELLIPSIZE_MIDDLE:
+		s = eel_string_ellipsize_middle (string, layout, width);
+		break;
+	case EEL_ELLIPSIZE_END:
+		s = eel_string_ellipsize_end (string, layout, width);
+		break;
+	default:
+		s = NULL;
+		g_assert_not_reached ();
+	}
+	
+	pango_layout_set_text (layout, s, -1);
+	
+	g_free (s);
+}
+
+
+static PangoContext*
+eel_create_bogus_test_pango_context (void)
+{
+  PangoContext *context;
+
+  context = gdk_pango_context_get ();
+
+  /* FIXME do we have to set a font on the PangoContext or does Pango
+   * have some sane default?
+   */
+  
+  pango_context_set_language (context, gtk_get_default_language ());
+
+  return context;
+}
+
+/* Testing string truncation is tough because we do not know what font/
+ * font metrics to expect on a given system. To work around this we use
+ * a substring of the original, measure it's length using the given font, 
+ * add the length of the "..." string and use that for truncation.
+ * The result should then be the substring prepended with a "..."
+ */
+static char *
+eel_self_check_ellipsize (const char *string, const char *truncate_to_length_string, EelEllipsizeMode mode)
+{
+	PangoContext *context;
+	int truncation_length;
+	char *result;
+	PangoLayout *layout;
+	
+	context = eel_create_bogus_test_pango_context ();
+	layout = pango_layout_new (context);
+	
+	/* measure the length we want to truncate to */
+	truncation_length = measure_string_width (truncate_to_length_string, context, NULL);
+	truncation_length += measure_string_width (ELLIPSIS, context, NULL);
+
+	eel_pango_layout_set_text_ellipsized (layout, string, truncation_length, mode);
+
+	result = g_strdup (pango_layout_get_text (layout));
+	
+	g_object_unref (G_OBJECT (context));
+	g_object_unref (G_OBJECT (layout));
+
+	return result;
+}
+
+static char *
+eel_self_check_ellipsize_start (const char *string, const char *truncate_to_length_string)
+{
+	return eel_self_check_ellipsize (string, truncate_to_length_string, EEL_ELLIPSIZE_START);
+}
+
+static char *
+eel_self_check_ellipsize_middle (const char *string, const char *truncate_to_length_string)
+{
+	return eel_self_check_ellipsize (string, truncate_to_length_string, EEL_ELLIPSIZE_MIDDLE);
+}
+
+static char *
+eel_self_check_ellipsize_end (const char *string, const char *truncate_to_length_string)
+{
+	return eel_self_check_ellipsize (string, truncate_to_length_string, EEL_ELLIPSIZE_END);
+}
+
+void
+eel_self_check_pango_extensions (void)
+{
+	PangoContext *context;
+
+	/* used to test ellipsize routines */
+	context = eel_create_bogus_test_pango_context ();
+
+	/* eel_string_ellipsize_start */
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "0012345678"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "012345678"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "45678"), "...45678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "5678"), "...5678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "678"), "...678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "78"), "...78");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_start ("012345678", "8"), "...8");
+
+
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "0123456789"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "012345678"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "01278"), "012...78");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "0178"), "01...78");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "018"), "01...8");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "08"), "0...8");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("012345678", "0"), "0...");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "0123456789"), "0123456789");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "012789"), "012...789");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "01289"), "012...89");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "0189"), "01...89");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "019"), "01...9");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "09"), "0...9");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_middle ("0123456789", "0"), "0...");
+
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "0123456789"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "012345678"), "012345678");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "01234"), "01234...");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "0123"), "0123...");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "012"), "012...");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "01"), "01...");
+	EEL_CHECK_STRING_RESULT (eel_self_check_ellipsize_end ("012345678", "0"), "0...");
+
+	g_object_unref (G_OBJECT (context));
+}
Index: eel/eel-pango-extensions.h
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-pango-extensions.h,v
retrieving revision 1.4
diff -u -p -u -r1.4 eel-pango-extensions.h
--- eel/eel-pango-extensions.h	2002/01/08 22:37:06	1.4
+++ eel/eel-pango-extensions.h	2002/01/12 00:29:13
@@ -29,15 +29,26 @@
 
 #include <pango/pango-layout.h>
 
-PangoAttrList *eel_pango_attr_list_copy_or_create         (PangoAttrList  *attr_list);
-PangoAttrList *eel_pango_attr_list_apply_global_attribute (PangoAttrList  *attr_list,
-							   PangoAttribute *attr);
-void           eel_pango_layout_set_weight                (PangoLayout    *layout,
-							   PangoWeight     weight);
-void           eel_pango_layout_set_underline             (PangoLayout    *layout,
-							   PangoUnderline  underline);
-void           eel_pango_layout_set_font_desc_from_string (PangoLayout    *layout,
-							   const char     *str);
+typedef enum {
+	EEL_ELLIPSIZE_START,
+	EEL_ELLIPSIZE_MIDDLE,
+	EEL_ELLIPSIZE_END
+} EelEllipsizeMode;
+
+PangoAttrList *eel_pango_attr_list_copy_or_create         (PangoAttrList    *attr_list);
+PangoAttrList *eel_pango_attr_list_apply_global_attribute (PangoAttrList    *attr_list,
+							   PangoAttribute   *attr);
+void           eel_pango_layout_set_weight                (PangoLayout      *layout,
+							   PangoWeight       weight);
+void           eel_pango_layout_set_underline             (PangoLayout      *layout,
+							   PangoUnderline    underline);
+void           eel_pango_layout_set_font_desc_from_string (PangoLayout      *layout,
+							   const char       *str);
+/* caution: this function is expensive. */
+void           eel_pango_layout_set_text_ellipsized       (PangoLayout      *layout,
+							   const char       *string,
+							   int               width,
+							   EelEllipsizeMode  mode);
 
 
 #endif /* EEL_PANGO_EXTENSIONS_H */
Index: eel/eel.h
===================================================================
RCS file: /cvs/gnome/eel/eel/eel.h,v
retrieving revision 1.11
diff -u -p -u -r1.11 eel.h
--- eel/eel.h	2002/01/08 22:37:06	1.11
+++ eel/eel.h	2002/01/12 00:29:13
@@ -54,6 +54,7 @@
 #include <eel/eel-labeled-image.h>
 #include <eel/eel-list-column-title.h>
 #include <eel/eel-list.h>
+#include <eel/eel-pango-extensions.h>
 #include <eel/eel-password-dialog.h>
 #include <eel/eel-preferences-box.h>
 #include <eel/eel-preferences-group.h>
Index: test/Makefile.am
===================================================================
RCS file: /cvs/gnome/eel/test/Makefile.am,v
retrieving revision 1.17
diff -u -p -u -r1.17 Makefile.am
--- test/Makefile.am	2002/01/04 18:33:42	1.17
+++ test/Makefile.am	2002/01/12 00:29:13
@@ -14,6 +14,7 @@ LDADD =\
 
 noinst_PROGRAMS =\
 	test-eel-background \
+	test-eel-ellipsizing	\
 	test-eel-font-picker \
 	test-eel-gtk-style \
 	test-eel-image-background \
@@ -29,6 +30,7 @@ noinst_PROGRAMS =\
 	$(NULL)
 
 test_eel_background_SOURCES = test-eel-background.c
+test_eel_ellipsizing_SOURCES = test-eel-ellipsizing.c
 test_eel_font_picker_SOURCES = test-eel-font-picker.c test.c
 test_eel_gtk_style_SOURCES = test-eel-gtk-style.c test.c dumb-box.c dumb-box.h
 test_eel_image_background_SOURCES = test-eel-image-background.c test.c
Index: test/test-eel-ellipsizing.c
===================================================================
RCS file: test-eel-ellipsizing.c
diff -N test-eel-ellipsizing.c
--- /dev/null	Tue May  5 16:32:27 1998
+++ test-eel-ellipsizing.c	Fri Jan 11 19:29:13 2002
@@ -0,0 +1,55 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+
+#include <config.h>
+
+#include <gtk/gtk.h>
+
+#include <eel/eel-ellipsizing-label.h>
+
+
+static void
+quit (GtkWidget *widget, gpointer data)
+{
+	gtk_main_quit ();
+}
+
+int 
+main (int argc, char* argv[])
+{
+	GtkWidget *window;
+	GtkWidget *label;
+	GtkWidget *vbox;
+	
+	gtk_init (&argc, &argv);
+
+	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+	g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (quit), NULL);
+
+	vbox = gtk_vbox_new (FALSE, 0);
+
+	gtk_container_add (GTK_CONTAINER (window), vbox);
+	
+	label = eel_ellipsizing_label_new ("Centered Label");
+
+	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 4);
+
+	label = eel_ellipsizing_label_new ("Left aligned label");
+
+	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+	
+	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 4);
+
+	label = eel_ellipsizing_label_new ("Right aligned label");
+
+	gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+	
+	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 4);
+	
+	gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
+	
+	gtk_widget_show_all (window);	
+	
+	gtk_main ();
+
+	return 0;
+}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]