+
+static gboolean
+correct_scroll_idle_func (gpointer userdata)
+{
+ ModestSignatureEditorDialog *self = (ModestSignatureEditorDialog *) userdata;
+ ModestSignatureEditorDialogPrivate *priv;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+ GdkRectangle rectangle;
+ gint offset_min, offset_max;
+ GtkTextMark *insert;
+ GtkAdjustment *vadj;
+
+ /* It could happen that the window was already closed */
+ if (!GTK_WIDGET_VISIBLE (self))
+ return FALSE;
+
+ priv = SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(self);
+ buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
+
+ insert = gtk_text_buffer_get_insert (buffer);
+ gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
+
+ gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->textview), &iter, &rectangle);
+ offset_min = priv->textview->allocation.y + rectangle.y;
+ offset_max = offset_min + rectangle.height;
+
+ vadj = hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (priv->pannable));
+ offset_min = MAX (offset_min - 48, 0);
+ offset_max = MIN (offset_max + 48, vadj->upper);
+
+ if ((offset_min < vadj->value) || (offset_max > vadj->value + vadj->page_size)) {
+ /* We check if the current center of the visible area is already matching the center
+ of the selection */
+ gint offset_center;
+ gint center_top, center_bottom;
+
+ offset_center = (offset_min + offset_max) / 2;
+ center_top = vadj->value + vadj->page_size / 3;
+ center_bottom = vadj->value + vadj->page_size * 2 / 3;
+
+ if ((offset_center < center_top) ||
+ (offset_center > center_bottom))
+ hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (priv->pannable), -1, offset_center);
+ }
+
+ priv->correct_scroll_idle = 0;
+ return FALSE;
+}
+
+static void correct_scroll (ModestSignatureEditorDialog *self)
+{
+ ModestSignatureEditorDialogPrivate *priv;
+
+ priv = SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(self);
+
+ if (!gtk_widget_is_focus (priv->textview))
+ return;
+
+ if (priv->correct_scroll_idle > 0) {
+ return;
+ }
+
+ priv->correct_scroll_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+ (GSourceFunc) correct_scroll_idle_func,
+ g_object_ref (self),
+ g_object_unref);
+}
+
+static void
+text_buffer_end_user_action (GtkTextBuffer *buffer,
+ ModestSignatureEditorDialog *userdata)
+{
+
+ correct_scroll (userdata);
+}
+