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.
This commit is contained in:
Emmanuele Bassi 2012-02-06 18:23:21 +00:00
parent 3305105a8c
commit 7ba9774572
4 changed files with 26 additions and 9 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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

View File

@ -1966,6 +1966,7 @@ clutter_text_get_cursor_size
<SUBSECTION>
clutter_text_activate
clutter_text_coords_to_position
clutter_text_position_to_coords
clutter_text_set_preedit_string
clutter_text_get_layout_offsets