
Index: widgets/text/e-text.c
===================================================================
RCS file: /cvs/gnome/evolution/widgets/text/e-text.c,v
retrieving revision 1.173
diff -u -8 -p -r1.173 e-text.c
--- widgets/text/e-text.c	21 Jul 2006 05:47:37 -0000	1.173
+++ widgets/text/e-text.c	25 Jul 2006 13:24:01 -0000
@@ -278,59 +278,57 @@ e_text_dispose (GObject *object)
 
 static void
 insert_preedit_text (EText *text)
 {
 	PangoAttrList *attrs = NULL;
 	PangoAttrList *preedit_attrs = NULL;
 	gchar *preedit_string = NULL;
 	GString *tmp_string = g_string_new (NULL);
-	gint length = 0, cpos = 0, preedit_length = 0;
+	gint length = 0, cpos = 0;
 	gboolean new_attrs = FALSE;
 
 	if (text->layout == NULL || !GTK_IS_IM_CONTEXT (text->im_context))
 		return;
 
 	text->text = e_text_model_get_text(text->model);
 	length = strlen (text->text);
 
 	g_string_prepend_len (tmp_string, text->text,length); 
 
-	if (text->preedit_len)
-		gtk_im_context_get_preedit_string (text->im_context,
-					   &preedit_string, &preedit_attrs,
-					   NULL);
+	/* we came into this function only when text->preedit_len was not 0
+	   so we can saftely fetch the preedit string */
+	gtk_im_context_get_preedit_string (text->im_context, &preedit_string, &preedit_attrs, NULL);
 
 	if (preedit_string && g_utf8_validate (preedit_string, -1, NULL))
-		text->preedit_len = preedit_length = strlen (preedit_string);
-	else
-		text->preedit_len  = preedit_length = 0;
-
-	cpos = g_utf8_offset_to_pointer (text->text, text->selection_start) - text->text;	
-
-	if (preedit_length) {
+	{
+		text->preedit_len = strlen (preedit_string);
+		cpos = g_utf8_offset_to_pointer (text->text, text->selection_start) - text->text;	
+		
 		g_string_insert (tmp_string, cpos, preedit_string);
 
 		reset_layout_attrs (text);
 
 		attrs = pango_layout_get_attributes (text->layout);
 		if (!attrs) {
 			attrs = pango_attr_list_new ();
 			new_attrs = TRUE;
 		}
 
 		pango_layout_set_text (text->layout, tmp_string->str, tmp_string->len);
-
-		pango_attr_list_splice (attrs, preedit_attrs, cpos, preedit_length);
+		
+		pango_attr_list_splice (attrs, preedit_attrs, cpos, text->preedit_len);
 
 		if (new_attrs) {
 			pango_layout_set_attributes (text->layout, attrs);
 			pango_attr_list_unref (attrs);
-		}
+		}		
 	}
+	else
+		text->preedit_len = 0;
 
 	if (preedit_string)
 		g_free (preedit_string);
 	if (preedit_attrs)
 		pango_attr_list_unref (preedit_attrs);
 	if (tmp_string)
 		g_string_free (tmp_string, TRUE);
 }
@@ -377,19 +375,22 @@ reset_layout_attrs (EText *text)
 		if (text->strikeout) {
 			PangoAttribute *attr = pango_attr_strikethrough_new (TRUE);
 			attr->start_index = 0;
 			attr->end_index = length;
 
 			pango_attr_list_insert_before (attrs, attr);
 		}
 	}
+
 	pango_layout_set_attributes (text->layout, attrs);
+	
 	if (attrs)
 		pango_attr_list_unref (attrs);
+	
 	calc_height (text);
 }
 
 static void
 create_layout (EText *text)
 {
 	GnomeCanvasItem *item = GNOME_CANVAS_ITEM (text);
 
@@ -1503,17 +1504,24 @@ e_text_draw (GnomeCanvasItem *item, GdkD
 				gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
 						 &area, widget, "button",
 						 thisx + xoff, thisy + yoff, thiswidth - 1, thisheight - 1);
 			}
 		}
 	}
 
 
-	insert_preedit_text (text);	
+	/* Insert preedit text only when im_context signals are connected & text->preedit_len is not zero */
+	if (text->im_context_signals_registered && text->preedit_len)
+		insert_preedit_text (text);
+	
+	/* Need to reset the layout to cleanly clear the preedit buffer
+	   when typing in CJK & using backspace on the preedit */
+	if(!text->preedit_len)
+		reset_layout (text);
 
 	if (!pango_layout_get_text (text->layout))
 		return;
 
 	if (text->stipple)
 		gnome_canvas_set_stipple_origin (item->canvas, main_gc);
 
 	xpos = text->text_cx;
