From a1a8193179dec84b29fac146e5cf34d43a66f093 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Mon, 30 Mar 2009 07:54:42 +0100 Subject: [PATCH] [text] Check text length in ::button-press-event Bug 1529 - Selection bound out of sync with an empty Text actor When the user clicks on a Text actor the cursor position and the selection bound are set using bytes_to_offset(); if the Text is empty, this means placing them both to 0. Setting the selection bound to 0 means that when the user inserts a character by typing it into the Text, the selection will be [0,1]; this will select the first character, which will then be overwritten when typing a new character. The Text actor should, instead, check if there are no contents and set the cursor position and the selection bound to -1. The clutter_text_set_selection_bound() method should also validate the value passed, in case it's bigger than the text lenght, or smaller than -1. --- clutter/clutter-text.c | 44 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/clutter/clutter-text.c b/clutter/clutter-text.c index 50fb545cd..982612609 100644 --- a/clutter/clutter-text.c +++ b/clutter/clutter-text.c @@ -893,28 +893,45 @@ clutter_text_button_press (ClutterActor *actor, { ClutterText *self = CLUTTER_TEXT (actor); ClutterTextPrivate *priv = self->priv; + gboolean res = FALSE; ClutterUnit x, y; gint index_; + /* we'll steal keyfocus if we do not have it */ + clutter_actor_grab_key_focus (actor); + + /* if the actor is empty we just reset everything and not + * set up the dragging of the selection since there's nothing + * to select + */ + if (priv->text == NULL || priv->text[0] == '\0') + { + clutter_text_set_cursor_position (self, -1); + clutter_text_set_selection_bound (self, -1); + + return TRUE; + } + x = CLUTTER_UNITS_FROM_INT (event->x); y = CLUTTER_UNITS_FROM_INT (event->y); - clutter_actor_transform_stage_point (actor, x, y, &x, &y); + res = clutter_actor_transform_stage_point (actor, x, y, &x, &y); + if (res) + { + index_ = clutter_text_coords_to_position (self, + CLUTTER_UNITS_TO_INT (x), + CLUTTER_UNITS_TO_INT (y)); - index_ = clutter_text_coords_to_position (self, - CLUTTER_UNITS_TO_INT (x), - CLUTTER_UNITS_TO_INT (y)); - - clutter_text_set_cursor_position (self, bytes_to_offset (priv->text, index_)); - clutter_text_set_selection_bound (self, bytes_to_offset (priv->text, index_)); + clutter_text_set_cursor_position (self, + bytes_to_offset (priv->text, index_)); + clutter_text_set_selection_bound (self, + bytes_to_offset (priv->text, index_)); + } /* grab the pointer */ priv->in_select_drag = TRUE; clutter_grab_pointer (actor); - /* we'll steal keyfocus if we do not have it */ - clutter_actor_grab_key_focus (actor); - return TRUE; } @@ -2636,7 +2653,12 @@ clutter_text_set_selection_bound (ClutterText *self, if (priv->selection_bound != selection_bound) { - priv->selection_bound = selection_bound; + gint len = priv->n_chars; + + if (selection_bound < 0 || selection_bound >= len) + priv->selection_bound = -1; + else + priv->selection_bound = selection_bound; if (CLUTTER_ACTOR_IS_VISIBLE (self)) clutter_actor_queue_redraw (CLUTTER_ACTOR (self));