From 83035c1d4b80880cd49e8f984ba337b5fdab792e Mon Sep 17 00:00:00 2001 From: Marek Kasik Date: Wed, 7 Aug 2024 20:25:31 +0200 Subject: [PATCH] printeroptionwidget: Maintain cursor position in combo-entry Maintain cursor position even if the text in the entry has been filtered. This is achieved by maintaining the same position with respect to the end of the text buffer as we suppose that the text after the cursor hasn't changed. This is measured in characters not bytes. Fixes #6782 --- gtk/print/gtkprinteroptionwidget.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gtk/print/gtkprinteroptionwidget.c b/gtk/print/gtkprinteroptionwidget.c index 51a8a0089f..ecafe3439d 100644 --- a/gtk/print/gtkprinteroptionwidget.c +++ b/gtk/print/gtkprinteroptionwidget.c @@ -812,9 +812,21 @@ handle_combo_entry_change (GtkPrinterOptionWidget *widget) if (changed) { GtkWidget *entry = gtk_widget_get_first_child (priv->combo); + gssize buffer_length, filtered_buffer_length; + gint position; + + position = gtk_editable_get_position (GTK_EDITABLE (entry)); + buffer_length = gtk_entry_buffer_get_length (gtk_entry_get_buffer (GTK_ENTRY (entry))); + g_signal_handler_block (entry, priv->comboentry_changed_handler_id); gtk_editable_set_text (GTK_EDITABLE (entry), filtered_val); g_signal_handler_unblock (entry, priv->comboentry_changed_handler_id); + + filtered_buffer_length = gtk_entry_buffer_get_length (gtk_entry_get_buffer (GTK_ENTRY (entry))); + + /* Maintain position of the cursor with respect to the end of the buffer. */ + if (position > 0 && filtered_buffer_length < buffer_length) + gtk_editable_set_position (GTK_EDITABLE (entry), position - (buffer_length - filtered_buffer_length)); } value = filtered_val; }