Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtkhtml/src/ChangeLog,v
retrieving revision 1.2023.2.12
diff -u -p -r1.2023.2.12 ChangeLog
--- ChangeLog	16 Sep 2004 22:09:17 -0000	1.2023.2.12
+++ ChangeLog	17 Sep 2004 20:52:47 -0000
@@ -1,3 +1,23 @@
+Fri Sep 17 13:50:19 2004  Owen Taylor  <otaylor@redhat.com>
+
+	* gtkhtml.c (gtk_html_im_retrieve_surrounding_cb): Fix chars/bytes
+	confusion for cursor_index parameter of
+	gtk_im_context_set_surrounding().
+
+	* gtkhtml.c (get_surrounding_text): Fix problem where
+	we'd call gtk_im_context_set_surrounding() with an uninitialized 
+	value for offset.
+
+	* gtkhtml.c (unrealize): Set IM context client window to NULL,
+	not to widget->window.
+
+	* gtkhtml.c (gtk_html_im_delete_surrounding_cb): Handle the
+	case where offset < 0 and n_chars < -offset.
+
+	* gtkhtml.c (gtk_html_im_preedit_changed_cb): Fix chars/bytes
+	confusion for returned cursor position for 
+	gtk_im_context_get_preedit_string()
+
 Thu Sep 16 18:05:52 2004  Owen Taylor  <otaylor@redhat.com>
 
 	* gtkhtml.c (gtk_html_im_commit_cb): Commit text before
Index: gtkhtml.c
===================================================================
RCS file: /cvs/gnome/gtkhtml/src/gtkhtml.c,v
retrieving revision 1.571.2.6
diff -u -p -r1.571.2.6 gtkhtml.c
--- gtkhtml.c	16 Sep 2004 22:22:22 -0000	1.571.2.6
+++ gtkhtml.c	17 Sep 2004 20:52:47 -0000
@@ -1009,7 +1009,7 @@ unrealize (GtkWidget *widget)
 
 	html_engine_unrealize (html->engine);
 
-	gtk_im_context_set_client_window (html->priv->im_context, widget->window);
+	gtk_im_context_set_client_window (html->priv->im_context, NULL);
 
 	html_image_factory_stop_animations (html->engine->image_factory);
 
@@ -3044,7 +3046,7 @@ gtk_html_im_preedit_changed_cb (GtkIMCon
 {
 	PangoAttrList *attrs;
 	gchar *preedit_string;
-	gint cursor_pos, initial_position;
+	gint cursor_pos, cursor_offset, initial_position;
 	gboolean state = html->priv->im_block_reset;
 	gboolean pop_selection = FALSE;
 	gint deleted = 0;
@@ -3079,15 +3081,16 @@ gtk_html_im_preedit_changed_cb (GtkIMCon
 		html->priv->im_orig_style = html_engine_get_font_style (html->engine);
 
 	gtk_im_context_get_preedit_string (html->priv->im_context, &preedit_string, &attrs, &cursor_pos);
+	cursor_pos = CLAMP (cursor_pos, 0, strlen (preedit_string)); /* Paranoia */
+	cursor_offset = g_utf8_pointer_to_offset (preedit_string, preedit_string + cursor_pos);
 
 	D_IM (printf ("IM preedit changed to %s\n", preedit_string);)
 	html->priv->im_pre_len = g_utf8_strlen (preedit_string, -1);
 
 	if (html->priv->im_pre_len > 0) {
-		cursor_pos = CLAMP (cursor_pos, 0, html->priv->im_pre_len);
 		html->priv->im_pre_pos = html->engine->cursor->position;
 		html_engine_paste_text_with_extra_attributes (html->engine, preedit_string, html->priv->im_pre_len, attrs);
-		html_cursor_jump_to_position_no_spell (html->engine->cursor, html->engine, html->priv->im_pre_pos + cursor_pos);
+		html_cursor_jump_to_position_no_spell (html->engine->cursor, html->engine, html->priv->im_pre_pos + cursor_offset);
 	} else
 		html_engine_set_font_style (html->engine, 0, html->priv->im_orig_style);
 	g_free (preedit_string);
@@ -3127,6 +3130,7 @@ get_surrounding_text (HTMLEngine *e, gin
 	gchar *text = NULL;
 
 	if (!html_object_is_text (o)) {
+		*offset = 0;
 		if (e->cursor->offset == 0) {
 			prev = html_object_prev_not_slave (o);
 			if (html_object_is_text (prev)) {
@@ -3142,7 +3146,6 @@ get_surrounding_text (HTMLEngine *e, gin
 			} else
 				return NULL;
 		}
-		*offset = 0;
 	} else
 		*offset = e->cursor->offset;
 
@@ -3163,10 +3166,14 @@ get_surrounding_text (HTMLEngine *e, gin
 static gboolean
 gtk_html_im_retrieve_surrounding_cb (GtkIMContext *context, GtkHTML *html)
 {
-	gint offset;
+	gint cursor_offset, cursor_index;
+	gchar *str;
+
+	D_IM (printf ("IM gtk_html_im_retrieve_surrounding_cb\n"));
 
-	D_IM (printf ("IM gtk_html_im_retrieve_surrounding_cb\n");)
-	gtk_im_context_set_surrounding (context, get_surrounding_text (html->engine, &offset), -1, offset);
+	str = get_surrounding_text (html->engine, &cursor_offset);
+	cursor_index = g_utf8_offset_to_pointer (str, cursor_offset) - str;
+	gtk_im_context_set_surrounding (context, str, -1, cursor_index);
 
 	return TRUE;
 }
@@ -3182,8 +3189,9 @@ gtk_html_im_delete_surrounding_cb (GtkIM
 		html_engine_set_mark (html->engine);
 		html_cursor_jump_to_position_no_spell (html->engine->cursor, html->engine, orig_position + offset + n_chars);
 		html_engine_delete (html->engine);
-		if (offset >= 0)
-			html_cursor_jump_to_position_no_spell (html->engine->cursor, html->engine, orig_position);
+		if (offset < 0)
+			orig_position -= MIN (n_chars, - offset);
+		html_cursor_jump_to_position_no_spell (html->engine->cursor, html->engine, orig_position);
 	}
 	return TRUE;
 }
