[ease/text] [text] Clicking selects the proper index.



commit 88844ca97d67f3c920a345350d5c581b85d3d586
Author: Nate Stedman <natesm gmail com>
Date:   Mon Nov 29 09:31:02 2010 -0500

    [text] Clicking selects the proper index.
    
    Pango.SCALE strikes again

 ease-core/ease-actor.vala      |   19 ++++++++++++++++
 ease-core/ease-text-actor.vala |   47 ++++++++++++++++++++++++++++++++-------
 ease/ease-editor-embed.vala    |   20 ++++++++++++++--
 vapi/pango.vapi                |    2 +-
 4 files changed, 75 insertions(+), 13 deletions(-)
---
diff --git a/ease-core/ease-actor.vala b/ease-core/ease-actor.vala
index 534029b..1493caa 100644
--- a/ease-core/ease-actor.vala
+++ b/ease-core/ease-actor.vala
@@ -236,5 +236,24 @@ public abstract class Ease.Actor : Clutter.Group
 	{
 		return false;
 	}
+	
+	/**
+	 * Called when a mouse button is pressed while { link editing} is true. If
+	 * the event is handled, true should be returned, otherwise, false should
+	 * be returned.
+	 *
+	 * By default, the function simply returns false.
+	 *
+	 * @param sender The actor that initiated the event.
+	 * @param event The event itself.
+	 * @param mouse_x The mouse x position of the click, relative to the actor.
+	 * @param mouse_y The mouse y position of the click, relative to the actor.
+	 */
+	public virtual bool clicked_event(Clutter.Actor sender,
+	                                  Clutter.ButtonEvent event,
+	                                  float mouse_x, float mouse_y)
+	{
+		return false;
+	}
 }
 
diff --git a/ease-core/ease-text-actor.vala b/ease-core/ease-text-actor.vala
index 5164219..197ebe9 100644
--- a/ease-core/ease-text-actor.vala
+++ b/ease-core/ease-text-actor.vala
@@ -93,15 +93,17 @@ public class Ease.TextActor : Actor
 	
 	public override void edit(Gtk.Widget sender, float mouse_x, float mouse_y)
 	{
-		int trailing = -1;
-		if (!(element as TextElement).text.layout.xy_to_index((int)mouse_x,
-		                                                      (int)mouse_y,
-		                                                      ref cursor_index,
-		                                                      ref trailing))
+		int trailing = 0;
+		var layout = (element as TextElement).text.layout;
+		if (!layout.xy_to_index((int)mouse_x * Pango.SCALE,
+		                        (int)mouse_y * Pango.SCALE,
+		                        out cursor_index, out trailing))
 		{
+			debug("Click was not inside element (%f, %f)", mouse_x, mouse_y);
 			cursor_index =
 				(int)(element as TextElement).text.layout.get_text().length;
 		}
+		cursor_index += trailing;
 		
 		debug("Editing text, cursor index is %i", cursor_index);
 		
@@ -118,6 +120,13 @@ public class Ease.TextActor : Actor
 		render_text();
 	}
 	
+	private override void end_edit(Gtk.Widget sender)
+	{
+		// remove the cursor and stop its animation
+		remove_actor(cursor);
+		render_text();
+	}
+	
 	public override bool key_event(Gtk.Widget sender, Gdk.EventKey event)
 	{
 		if (event.type == Gdk.EventType.KEY_PRESS)
@@ -171,11 +180,31 @@ public class Ease.TextActor : Actor
 		return true;
 	}
 	
-	private override void end_edit(Gtk.Widget sender)
+	private override bool clicked_event(Clutter.Actor self,
+	                                    Clutter.ButtonEvent event,
+	                                    float mouse_x, float mouse_y)
 	{
-		// remove the cursor and stop its animation
-		remove_actor(cursor);
-		render_text();
+		int trailing = 0;
+		var layout = (element as TextElement).text.layout;
+		if (!layout.xy_to_index((int)mouse_x * Pango.SCALE,
+		                        (int)mouse_y * Pango.SCALE,
+		                        out cursor_index, out trailing))
+		{
+			debug("Edit click not inside element (%f, %f)", mouse_x, mouse_y);
+			return true;
+		}
+		cursor_index += trailing;
+		position_cursor();
+		cursor.opacity = 255;
+		cursor_timeline.rewind();
+		
+		return true;
+	}
+	
+	private bool on_button_release_event(Clutter.Actor self,
+	                                     Clutter.ButtonEvent event)
+	{
+		return true;
 	}
 	
 	/**
diff --git a/ease/ease-editor-embed.vala b/ease/ease-editor-embed.vala
index 66eea68..289b768 100644
--- a/ease/ease-editor-embed.vala
+++ b/ease/ease-editor-embed.vala
@@ -467,14 +467,28 @@ internal class Ease.EditorEmbed : ScrolledEmbedWindow, UndoSource
 	 */
 	private bool actor_clicked(Clutter.Actor sender, Clutter.ButtonEvent event)
 	{
+		// if an actor is already being edited and is clicked, give it the event
+		if (is_editing && sender == selected)
+		{
+			float act_x, act_y;
+			sender.transform_stage_point(event.x, event.y,
+			                             out act_x, out act_y);
+			if (selected.clicked_event(sender, event, act_x, act_y))
+			{
+				return true;
+			}
+		}
+		
 		// if this is a double click, edit the actor
 		if (event.click_count == 2)
 		{
+			float act_x, act_y;
+			sender.transform_stage_point(event.x, event.y,
+			                             out act_x, out act_y);
+			
 			disconnect_keys();
 			(sender as Actor).editing = true;
-			(sender as Actor).edit(this,
-			                       event.x - sender.x,
-			                       event.y - sender.y);
+			(sender as Actor).edit(this, act_x, act_y);
 			is_editing = true;
 			return true;
 		}
diff --git a/vapi/pango.vapi b/vapi/pango.vapi
index 56da307..d255b6d 100644
--- a/vapi/pango.vapi
+++ b/vapi/pango.vapi
@@ -372,7 +372,7 @@ namespace Pango {
 		public void set_text (string text, int length);
 		public void set_width (int width);
 		public void set_wrap (Pango.WrapMode wrap);
-		public bool xy_to_index (int x, int y, ref int index_, ref int trailing);
+		public bool xy_to_index (int x, int y, out int index_, out int trailing);
 	}
 	[Compact]
 	[CCode (cheader_filename = "pango/pango.h")]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]