gnucash master: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Tue May 26 18:13:02 EDT 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/12680ebb (commit)
	 via  https://github.com/Gnucash/gnucash/commit/a873cae9 (commit)
	from  https://github.com/Gnucash/gnucash/commit/fde6be6e (commit)



commit 12680ebb58ee978613d2e25af860079d8b03f54d
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue May 26 12:27:10 2020 -0700

    gnucash-item-edit.c: Small whitespace fixup.

diff --git a/gnucash/register/register-gnome/gnucash-item-edit.c b/gnucash/register/register-gnome/gnucash-item-edit.c
index 7c588117a..a307d1bd7 100644
--- a/gnucash/register/register-gnome/gnucash-item-edit.c
+++ b/gnucash/register/register-gnome/gnucash-item-edit.c
@@ -568,10 +568,13 @@ draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
     cairo_set_source_rgb (cr, fg_color->red, fg_color->green, fg_color->blue);
     cairo_set_line_width (cr, 1.0);
 
-    cairo_move_to (cr, cursor_x + 0.5, gnc_item_edit_get_margin (item_edit, top) +
-                                       gnc_item_edit_get_padding_border (item_edit, top));
-    cairo_rel_line_to (cr, 0, height - gnc_item_edit_get_margin (item_edit, top_bottom) -
-                                       gnc_item_edit_get_padding_border (item_edit, top_bottom));
+    cairo_move_to (cr, cursor_x + 0.5,
+                   gnc_item_edit_get_margin (item_edit, top) +
+                   gnc_item_edit_get_padding_border (item_edit, top));
+    cairo_rel_line_to (cr, 0,
+                       height - gnc_item_edit_get_margin (item_edit, top_bottom)
+                       - gnc_item_edit_get_padding_border (item_edit,
+                                                           top_bottom));
 
     cairo_stroke (cr);
 

commit a873cae90e418e317122a202873302093140a314
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue May 26 12:19:51 2020 -0700

    Place the cursor after the preedit.
    
    Connect to the GtkEntry's preedit-changed signal and stash the preedit
    length.
    
    Uses the PangoLayout's text instead of the GtkEntry's because the former
    includes the current preedit string and the latter does not. Add the
    preedit length to the cursor position so that the cursor is drawn to the
    right (left in RTL languages) of the preedit.

diff --git a/gnucash/register/register-gnome/gnucash-item-edit.c b/gnucash/register/register-gnome/gnucash-item-edit.c
index a51e51110..7c588117a 100644
--- a/gnucash/register/register-gnome/gnucash-item-edit.c
+++ b/gnucash/register/register-gnome/gnucash-item-edit.c
@@ -31,6 +31,7 @@
 #include <config.h>
 
 #include <string.h>
+#include <qof.h>
 
 #include "gnucash-color.h"
 #include "gnucash-cursor.h"
@@ -58,6 +59,7 @@ enum
 
 #define MIN_BUTT_WIDTH 20 // minimum size for a button excluding border
 
+static QofLogModule log_module = G_LOG_DOMAIN;
 static GtkBoxClass *gnc_item_edit_parent_class;
 
 static GtkToggleButtonClass *gnc_item_edit_tb_parent_class;
@@ -311,6 +313,7 @@ gnc_item_edit_init (GncItemEdit *item_edit)
 
     item_edit->sheet = NULL;
     item_edit->editor = NULL;
+    item_edit->preedit_length = 0;
 
     item_edit->is_popup = FALSE;
     item_edit->show_popup = FALSE;
@@ -503,6 +506,19 @@ draw_background_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
     return FALSE;
 }
 
+/* The signal is emitted at the beginning of gtk_entry_preedit_changed_cb which
+ * proceeds to set its private members preedit_length = strlen(preedit) and
+ * preeditc_cursor = g_utf8_strlen(preedit, -1), then calls gtk_entry_recompute
+ * which in turn queues a redraw.
+ */
+static void
+preedit_changed_cb (GtkEntry* entry, gchar *preedit, GncItemEdit* item_edit)
+{
+    int pos, bound;
+    item_edit->preedit_length = g_utf8_strlen (preedit, -1); // Note codepoints not bytes
+    DEBUG ("%s %lu", preedit, item_edit->preedit_length);
+}
+
 
 static gboolean
 draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
@@ -512,7 +528,8 @@ draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
     GtkStyleContext *stylectxt = gtk_widget_get_style_context (GTK_WIDGET(widget));
     GtkStateFlags flags = gtk_widget_get_state_flags (GTK_WIDGET(widget));
     gint height = gtk_widget_get_allocated_height (widget);
-    const gchar *text;
+    PangoLayout *layout = gtk_entry_get_layout (GTK_ENTRY(widget));
+    const char *pango_text = pango_layout_get_text(layout);
     GdkRGBA *fg_color;
     GdkRGBA color;
     gint x_offset;
@@ -526,26 +543,27 @@ draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
     gtk_style_context_get_color (stylectxt, flags, &color);
     fg_color = &color;
 
-    text = gtk_entry_get_text (GTK_ENTRY (widget));
 
-    if ((text != NULL) && (*text != '\0'))
+    if (pango_text && *pango_text)
     {
-        PangoLayout *layout;
         PangoRectangle strong_pos;
-        gint start_pos, end_pos, cursor_pos, cursor_byte_pos;
-
-        cursor_pos = gtk_editable_get_position (editable);
-        cursor_byte_pos = g_utf8_offset_to_pointer (text, cursor_pos) - text;
-
-        gtk_editable_get_selection_bounds (editable, &start_pos, &end_pos);
-
-        layout = gtk_entry_get_layout (GTK_ENTRY(widget));
-        pango_layout_get_cursor_pos (layout, cursor_byte_pos, &strong_pos, NULL);
+        glong text_len = g_utf8_strlen (pango_text, -1);
+        gint cursor_pos =
+            gtk_editable_get_position (editable) + item_edit->preedit_length;
+        gint cursor_byte_pos = cursor_pos < text_len ?
+            g_utf8_offset_to_pointer (pango_text, cursor_pos) - pango_text :
+            strlen (pango_text);
+        DEBUG ("Cursor: %d, byte offset %d, text byte len %ld", cursor_pos,
+               cursor_byte_pos, strlen (pango_text));
+        pango_layout_get_cursor_pos (layout, cursor_byte_pos,
+                                     &strong_pos, NULL);
         cursor_x = x_offset + PANGO_PIXELS (strong_pos.x);
     }
     else
+    {
+        DEBUG ("No text, cursor at %d.", x_offset);
         cursor_x = x_offset;
-
+    }
     // Now draw a vertical line
     cairo_set_source_rgb (cr, fg_color->red, fg_color->green, fg_color->blue);
     cairo_set_line_width (cr, 1.0);
@@ -881,6 +899,9 @@ gnc_item_edit_new (GnucashSheet *sheet)
     g_signal_connect_after (item_edit->editor, "draw",
                             G_CALLBACK (draw_text_cursor_cb), item_edit);
 
+    g_signal_connect (item_edit->editor, "preedit-changed",
+                      G_CALLBACK (preedit_changed_cb), item_edit);
+
     // Fill in the background so the underlying sheet cell can not be seen
     g_signal_connect (item_edit, "draw",
                             G_CALLBACK (draw_background_cb), item_edit);
diff --git a/gnucash/register/register-gnome/gnucash-item-edit.h b/gnucash/register/register-gnome/gnucash-item-edit.h
index 926f2d6a2..185f3e325 100644
--- a/gnucash/register/register-gnome/gnucash-item-edit.h
+++ b/gnucash/register/register-gnome/gnucash-item-edit.h
@@ -77,6 +77,7 @@ typedef struct
 
     /* The editor whose status we reflect on the sheet */
     GtkWidget *editor;
+    gulong preedit_length;
 
     gboolean is_popup;
     gboolean show_popup;



Summary of changes:
 .../register/register-gnome/gnucash-item-edit.c    | 60 +++++++++++++++-------
 .../register/register-gnome/gnucash-item-edit.h    |  1 +
 2 files changed, 43 insertions(+), 18 deletions(-)



More information about the gnucash-changes mailing list