[gspell/wip/entry-context-menu: 3/3] EntryUtils: add function to get char position at GdkEventButton
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gspell/wip/entry-context-menu: 3/3] EntryUtils: add function to get char position at GdkEventButton
- Date: Fri, 25 Nov 2016 19:59:00 +0000 (UTC)
commit d703d6fc2526d4e698086b5e3ad2e73a8169a2d7
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Nov 25 18:45:05 2016 +0100
EntryUtils: add function to get char position at GdkEventButton
gspell/gspell-entry-utils.c | 61 +++++++++++++++++++++++++++++++++++++++++++
gspell/gspell-entry-utils.h | 11 +++++--
2 files changed, 69 insertions(+), 3 deletions(-)
---
diff --git a/gspell/gspell-entry-utils.c b/gspell/gspell-entry-utils.c
index 3a081b4..718198c 100644
--- a/gspell/gspell-entry-utils.c
+++ b/gspell/gspell-entry-utils.c
@@ -148,4 +148,65 @@ _gspell_entry_utils_get_words (GtkEntry *entry)
return g_slist_reverse (list);
}
+static gint
+get_cursor_byte_position (GtkEntry *entry)
+{
+ return 0;
+}
+
+/* The return value is in characters, not bytes. And a position suitable for the
+ * text in the GtkEntryBuffer, i.e. without the preedit text.
+ */
+gint
+_gspell_entry_utils_get_char_position_at_event (GtkEntry *entry,
+ GdkEventButton *event,
+ gint preedit_byte_length)
+{
+ gint scroll_offset; /* in pixels */
+ gint x; /* in pixels */
+ PangoLayout *layout;
+ PangoLayoutLine *line;
+ gint byte_index;
+ gint trailing_chars;
+ gint cursor_byte_pos;
+ const gchar *text;
+ gint char_pos;
+
+ g_object_get (entry,
+ "scroll-offset", &scroll_offset,
+ NULL);
+
+ x = event->x + scroll_offset;
+
+ layout = gtk_entry_get_layout (entry);
+ line = pango_layout_get_line_readonly (layout, 0);
+
+ pango_layout_line_x_to_index (line,
+ x * PANGO_SCALE,
+ &byte_index,
+ &trailing_chars);
+
+ cursor_byte_pos = get_cursor_byte_position (entry);
+
+ if (preedit_byte_length > 0 &&
+ cursor_byte_pos <= byte_index)
+ {
+ if (cursor_byte_pos + preedit_byte_length <= byte_index)
+ {
+ byte_index -= preedit_byte_length;
+ }
+ else
+ {
+ byte_index = cursor_byte_pos;
+ trailing_chars = 0;
+ }
+ }
+
+ text = gtk_entry_get_text (entry);
+ char_pos = g_utf8_pointer_to_offset (text, text + byte_index);
+ char_pos += trailing;
+
+ return char_pos;
+}
+
/* ex:set ts=8 noet: */
diff --git a/gspell/gspell-entry-utils.h b/gspell/gspell-entry-utils.h
index 813b5b2..8a9d483 100644
--- a/gspell/gspell-entry-utils.h
+++ b/gspell/gspell-entry-utils.h
@@ -43,13 +43,18 @@ struct _GspellEntryWord
};
G_GNUC_INTERNAL
-GspellEntryWord *_gspell_entry_word_new (void);
+GspellEntryWord *_gspell_entry_word_new (void);
G_GNUC_INTERNAL
-void _gspell_entry_word_free (gpointer data);
+void _gspell_entry_word_free (gpointer data);
G_GNUC_INTERNAL
-GSList * _gspell_entry_utils_get_words (GtkEntry *entry);
+GSList * _gspell_entry_utils_get_words (GtkEntry *entry);
+
+G_GNUC_INTERNAL
+gint _gspell_entry_utils_get_char_position_at_event (GtkEntry *entry,
+ GdkEventButton *event,
+ gint preedit_byte_length);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]