From 7ba9774572e44ad0049ecc8acb5dd093c9b0bbd1 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Mon, 6 Feb 2012 18:23:21 +0000 Subject: [PATCH] text: Add the coords_to_position() method The reverse of position_to_coords(). While providing documentation on how to implement it using the PangoLayout API, I realized that the verbosity of it all, plus the usage of the Pango API, was not worth it, and decided to expose the method we are using internally. --- clutter/clutter-text.c | 30 +++++++++++++++------- clutter/clutter-text.h | 3 +++ clutter/clutter.symbols | 1 + doc/reference/clutter/clutter-sections.txt | 1 + 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/clutter/clutter-text.c b/clutter/clutter-text.c index 9c0177ddb..a34e87a29 100644 --- a/clutter/clutter-text.c +++ b/clutter/clutter-text.c @@ -818,24 +818,36 @@ clutter_text_create_layout (ClutterText *text, return oldest_cache->layout; } -static gint -clutter_text_coords_to_position (ClutterText *text, +/** + * clutter_text_coords_to_position: + * @self: a #ClutterText + * @x: the X coordinate, relative to the actor + * @y: the Y coordinate, relative to the actor + * + * Retrieves the position of the character at the given coordinates. + * + * Return: the position of the character + * + * Since: 1.10 + */ +gint +clutter_text_coords_to_position (ClutterText *self, gfloat x, gfloat y) { - ClutterTextPrivate *priv = text->priv; gint index_; gint px, py; gint trailing; - /* Take any offset due to scrolling into account */ - if (priv->single_line_mode) - x += priv->text_x * -1; + g_return_val_if_fail (CLUTTER_IS_TEXT (self), 0); - px = x * PANGO_SCALE; - py = y * PANGO_SCALE; + /* Take any offset due to scrolling into account, and normalize + * the coordinates to PangoScale units + */ + px = (x - self->priv->text_x) * PANGO_SCALE; + py = (y - self->priv->text_y) * PANGO_SCALE; - pango_layout_xy_to_index (clutter_text_get_layout (text), + pango_layout_xy_to_index (clutter_text_get_layout (self), px, py, &index_, &trailing); diff --git a/clutter/clutter-text.h b/clutter/clutter-text.h index 9129978ec..761136920 100644 --- a/clutter/clutter-text.h +++ b/clutter/clutter-text.h @@ -210,6 +210,9 @@ void clutter_text_get_selected_text_color (ClutterText ClutterColor *color); gboolean clutter_text_activate (ClutterText *self); +gint clutter_text_coords_to_position (ClutterText *self, + gfloat x, + gfloat y); gboolean clutter_text_position_to_coords (ClutterText *self, gint position, gfloat *x, diff --git a/clutter/clutter.symbols b/clutter/clutter.symbols index 98510fd9a..ff048606a 100644 --- a/clutter/clutter.symbols +++ b/clutter/clutter.symbols @@ -1149,6 +1149,7 @@ clutter_text_buffer_new clutter_text_buffer_set_max_length clutter_text_buffer_set_text clutter_text_activate +clutter_text_coords_to_position clutter_text_delete_chars clutter_text_delete_selection clutter_text_delete_text diff --git a/doc/reference/clutter/clutter-sections.txt b/doc/reference/clutter/clutter-sections.txt index 11ca667dc..26a2df4e2 100644 --- a/doc/reference/clutter/clutter-sections.txt +++ b/doc/reference/clutter/clutter-sections.txt @@ -1966,6 +1966,7 @@ clutter_text_get_cursor_size clutter_text_activate +clutter_text_coords_to_position clutter_text_position_to_coords clutter_text_set_preedit_string clutter_text_get_layout_offsets