[gnumeric] item edit fixes.



commit f6ee9c72ec79f5faa38b32a37d4046daa44fc7fc
Author: Jean Brefort <jean brefort normalesup org>
Date:   Sat Nov 28 17:37:33 2009 +0100

    item edit fixes.

 ChangeLog       |    6 ++++++
 NEWS            |    2 ++
 src/item-edit.c |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 51 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6e01b3d..6eec2ed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-11-28  Jean Brefort  <jean brefort normalesup org>
+
+	* src/item-edit.c (item_edit_draw), (item_edit_button_pressed),
+	(item_edit_realize), (item_edit_unrealize): invert selected text, and allow
+	mouse selection past the last character. [#122176 and #388342]
+
 2009-11-27  Morten Welinder  <terra gnome org>
 
 	* src/workbook.c (workbook_finalize): Plug leak.
diff --git a/NEWS b/NEWS
index 25e7a0a..a68a026 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,8 @@ Andreas:
 Jean:
 	* Fix cursor and cell edition on dark backgrounds. [#600656]
 	* Fix multiple error message boxes in graph guru. [#152517]
+	* Invert selected text in a cell when editing. [#122176]
+	* Allow selection when the click occur after the last character. [#388342]
 
 Jody:
 	* turnkey win32 build on 32bit host.
diff --git a/src/item-edit.c b/src/item-edit.c
index bf6729d..4f7e460 100644
--- a/src/item-edit.c
+++ b/src/item-edit.c
@@ -58,6 +58,7 @@ struct _ItemEdit {
 
 	GnmFont   *gfont;
 	GnmStyle  *style;
+	GdkGC    *gc;
 };
 
 typedef GocItemClass ItemEditClass;
@@ -95,6 +96,9 @@ item_edit_draw (GocItem const *item, cairo_t *cr)
 	int top, left;
 	GOColor color;
 	int x0, y0, x1, y1; /* in widget coordinates */
+	int start, end;
+	PangoRectangle pos;
+	char const *text = gtk_entry_get_text (ie->entry);
 
 	get_top_left (ie, &top, &left);
 	if (goc_canvas_get_direction (item->canvas) == GOC_DIRECTION_RTL) {
@@ -113,10 +117,9 @@ item_edit_draw (GocItem const *item, cairo_t *cr)
 	color = GO_COLOR_FROM_GDK (gtk_widget_get_style (GTK_WIDGET (item->canvas))->black);
 	cairo_set_source_rgba (cr, GO_COLOR_TO_CAIRO (color));
 	cairo_move_to (cr, left, top);
+	gtk_editable_get_selection_bounds (GTK_EDITABLE (ie->entry), &start, &end);
 	pango_cairo_show_layout (cr, ie->layout);
 	if (ie->cursor_visible) {
-		PangoRectangle pos;
-		char const *text = gtk_entry_get_text (ie->entry);
 		int cursor_pos = gtk_editable_get_position (GTK_EDITABLE (ie->entry));
 		pango_layout_index_to_pos (ie->layout,
 			g_utf8_offset_to_pointer (text, cursor_pos) - text, &pos);
@@ -129,6 +132,23 @@ item_edit_draw (GocItem const *item, cairo_t *cr)
 		cairo_line_to (cr, left + PANGO_PIXELS (pos.x) + .5, top + PANGO_PIXELS (pos.y + pos.height) - 1);
 		cairo_stroke (cr);
 	}
+	if (start != end) {
+		/* invert selected region */
+		int x, y, w, h;
+		GdkEventExpose *expose = (GdkEventExpose *) goc_canvas_get_cur_event (item->canvas);
+		GdkDrawable *drawable = GDK_DRAWABLE (expose->window);
+		pango_layout_index_to_pos (ie->layout, start, &pos);
+		x = PANGO_PIXELS (pos.x);
+		y = PANGO_PIXELS (pos.y);
+		h = PANGO_PIXELS (pos.height);
+		pango_layout_index_to_pos (ie->layout, end, &pos);
+		w = PANGO_PIXELS (pos.x) - x;
+		if (w < 0) {
+			x += w;
+			w = -w;
+		}
+		gdk_draw_rectangle (drawable, ie->gc, TRUE, left + x, top + y, w, h);
+	}
 }
 
 static double
@@ -158,6 +178,7 @@ item_edit_button_pressed (GocItem *item, int button, double x, double y)
 		GtkEditable *ed = GTK_EDITABLE (ie->entry);
 		int target_index, trailing;
 		int top, left;
+		char const *text = pango_layout_get_text (ie->layout);
 
 		get_top_left (ie, &top, &left);
 		y -= top;
@@ -167,7 +188,6 @@ item_edit_button_pressed (GocItem *item, int button, double x, double y)
 					      x * PANGO_SCALE, y * PANGO_SCALE,
 					      &target_index, &trailing)) {
 			int preedit = GNM_PANE (item->canvas)->preedit_length;
-			char const *text = pango_layout_get_text (ie->layout);
 			gint cur_index = gtk_editable_get_position (ed);
 			cur_index = g_utf8_offset_to_pointer (text, cur_index) - text;
 
@@ -178,13 +198,18 @@ item_edit_button_pressed (GocItem *item, int button, double x, double y)
 				} else
 					target_index -= preedit;
 			}
-			gtk_editable_set_position (GTK_EDITABLE (ie->entry),
-				g_utf8_pointer_to_offset (text, text + target_index)
-				+ trailing);
-
-			return TRUE;
+		} else {
+			/* the click occured after text end (#388342) */
+			target_index = strlen (text);
+			trailing = 0;
 		}
+		gtk_editable_set_position (GTK_EDITABLE (ie->entry),
+			g_utf8_pointer_to_offset (text, text + target_index)
+			+ trailing);
+
+		return TRUE;
 	}
+
 	return FALSE;
 }
 
@@ -360,6 +385,7 @@ item_edit_realize (GocItem *item)
 	Sheet const *sheet;
 	GnmPane	*pane;
 	double scale;
+	GdkGCValues values;
 
 	parent_class->realize (item);
 
@@ -417,6 +443,12 @@ item_edit_realize (GocItem *item)
 				    : PANGO_ALIGN_LEFT);
 
 	item_edit_cursor_blink_start (ie);
+
+	ie->gc = gdk_gc_new (GTK_WIDGET (item->canvas)->window);
+	gdk_gc_set_rgb_fg_color (ie->gc, &gs_white);
+	gdk_gc_set_rgb_bg_color (ie->gc, &gs_white);
+	values.function = GDK_XOR;
+	gdk_gc_set_values (ie->gc, &values, GDK_GC_FUNCTION);
 }
 
 static void
@@ -424,6 +456,9 @@ item_edit_unrealize (GocItem *item)
 {
 	ItemEdit *ie = ITEM_EDIT (item);
 
+	g_object_unref (G_OBJECT (ie->gc));
+	ie->gc = NULL;
+
 	item_edit_cursor_blink_stop (ie);
 
 	/* to destroy the feedback ranges */



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