Re: Gtk 2.0 signals in GtkEntry



Havoc:

> move-cursor is an action signal; if it were a notification signal it
> would be called "cursor-moved"
> 
> So here you basically need to add a cursor-moved signal and implement
> having it be emitted at the proper times.

Here is a patch that implements the "cursor-moved" signal on GtkEntry
widgets.  Can you add this to CVS?  The patch is based upon CVS head of 
May 29th.  I've tested it and it works well.

Thanks!

Brian
--- ../../src/src-2.0n-240501/gtk+/gtk/gtkentry.c	Thu May 24 07:34:34 2001
+++ gtkentry.c	Wed May 30 14:38:41 2001
@@ -58,6 +58,7 @@
   DELETE_TEXT,
   CHANGED,
   ACTIVATE,
+  CURSOR_MOVED,
   POPULATE_POPUP,
   MOVE_CURSOR,
   INSERT_AT_CURSOR,
@@ -257,6 +258,8 @@
 							GdkEventButton *event);
 static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
 							gboolean        group_cycling);
+static void         gtk_entry_set_current_pos          (GtkEntry       *entry,
+		  				        gint            position);
 
 static GtkWidgetClass *parent_class = NULL;
 
@@ -475,6 +478,14 @@
 		    gtk_marshal_VOID__OBJECT,
 		    GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
   
+  signals[CURSOR_MOVED] =
+    gtk_signal_new ("cursor_moved",
+		    GTK_RUN_LAST,
+		    GTK_CLASS_TYPE (object_class),
+		    GTK_SIGNAL_OFFSET (GtkEntryClass, cursor_moved),
+		    gtk_marshal_VOID__INT,
+		    GTK_TYPE_NONE, 1, GTK_TYPE_INT);
+  
  /* Action signals */
   
   signals[ACTIVATE] =
@@ -1260,6 +1271,17 @@
   return FALSE;
 }
 
+static void gtk_entry_set_current_pos (GtkEntry *entry, gint position)
+{
+   gint *cp = &(entry->current_pos);
+
+   if (*cp != position)
+   {
+      *cp = position;
+      gtk_signal_emit (GTK_OBJECT (entry), signals[CURSOR_MOVED], position);
+   }
+}
+
 static gint
 gtk_entry_button_press (GtkWidget      *widget,
 			GdkEventButton *event)
@@ -1295,7 +1317,7 @@
 	  if (tmp_pos > sel_start && tmp_pos < sel_end)
 	    {
 	      /* Truncate current selection */
-	      entry->current_pos = tmp_pos;
+              gtk_entry_set_current_pos(entry, tmp_pos);
 	    }
 	  else
 	    {
@@ -1306,7 +1328,8 @@
 	      switch (event->type)
 		{
 		case GDK_BUTTON_PRESS:
-		  entry->current_pos = entry->selection_bound = tmp_pos;
+                  gtk_entry_set_current_pos(entry, tmp_pos);
+		  entry->selection_bound = tmp_pos;
 		  break;
 		  
 		case GDK_2BUTTON_PRESS:
@@ -1335,12 +1358,12 @@
 	      if (extend_to_left)
 		{
 		  entry->selection_bound = end;
-		  entry->current_pos = start;
+                  gtk_entry_set_current_pos(entry, start);
 		}
 	      else
 		{
 		  entry->selection_bound = start;
-		  entry->current_pos = end;
+                  gtk_entry_set_current_pos(entry, end);
 		}
 	    }
 	  
@@ -1364,7 +1387,7 @@
 	    {
 	      gtk_entry_reset_im_context (entry);
 	      
-	      entry->current_pos = tmp_pos;
+              gtk_entry_set_current_pos(entry, tmp_pos);
 	      entry->selection_bound = tmp_pos;
 
 	      gtk_entry_recompute (entry);
@@ -1430,7 +1453,7 @@
 
       gtk_entry_reset_im_context (entry);
 	      
-      entry->current_pos = tmp_pos;
+      gtk_entry_set_current_pos(entry, tmp_pos);
       entry->selection_bound = tmp_pos;
 	      
       gtk_entry_recompute (entry);
@@ -1441,7 +1464,7 @@
   entry->button = 0;
   
   gtk_entry_update_primary_selection (entry);
-	      
+
   return TRUE;
 }
 
@@ -1484,7 +1507,7 @@
 
       if (tmp_pos != entry->current_pos)
 	{
-	  entry->current_pos = tmp_pos;
+          gtk_entry_set_current_pos(entry, tmp_pos);
 	  gtk_entry_recompute (entry);
 	}
     }
@@ -1671,7 +1694,8 @@
     {
       gtk_entry_reset_im_context (entry);
 
-      entry->current_pos = entry->selection_bound = position;
+      gtk_entry_set_current_pos(entry, position);
+      entry->selection_bound = position;
       gtk_entry_recompute (entry);
 
       g_object_notify (G_OBJECT (entry), "text_position");
@@ -1699,7 +1723,7 @@
   gtk_entry_reset_im_context (entry);
 
   entry->selection_bound = MIN (start, entry->text_length);
-  entry->current_pos = MIN (end, entry->text_length);
+  gtk_entry_set_current_pos(entry, MIN (end, entry->text_length));
 
   gtk_entry_update_primary_selection (entry);
   
@@ -1793,7 +1817,7 @@
   entry->text[entry->n_bytes] = '\0';
   
   if (entry->current_pos > *position)
-    entry->current_pos += n_chars;
+    gtk_entry_set_current_pos(entry, entry->current_pos + n_chars);
   
   if (entry->selection_bound > *position)
     entry->selection_bound += n_chars;
@@ -1823,7 +1847,13 @@
       entry->n_bytes -= (end_index - start_index);
       
       if (entry->current_pos > start_pos)
-	entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
+      {
+        gint newpos = entry->current_pos;
+ 
+        newpos -= MIN (entry->current_pos, end_pos) - start_pos;
+
+        gtk_entry_set_current_pos(entry, newpos);
+      }
 
       if (entry->selection_bound > start_pos)
 	entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;


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