Re: [gtk-list] setting insertion point in GtkText



>>>>> "M" == Michael J Hammel <mjhammel@graphics-muse.org> writes:

 M> If you click in a GtkText window, the cursor is drawn in the
 M> correct spot, but the insertion point remains at the last spot
 M> text was inserted.  The only way I can find to update the
 M> insertion point is to type something.  How can I get the insertion
 M> point to match where the cursor has been placed so I can use
 M> gtk_text_insert() without having to type anything?

 M> Looking at gtktext.c I'd say that the code for handling
 M> button_press_events should do some of the work that goes on with
 M> key_press_event, namely the computation of where in the text
 M> you're at.  The only difference should probably be that with a
 M> button press event, you don't add any text, just update the point
 M> values.

This is the usual point/position trouble.  To update the insertion
point you can get the position (from the editable superclass) and then
set the (gtktext) point:

  gtk_text_set_point (GTK_TEXT (text),
		      gtk_editable_get_position (GTK_EDITABLE  (text)));

If you run the first attached program, the text widget will start to fill
with 'foo'.  If you click somewhere in the text, the cursor will of
course jump to that point, but the next insertion point depends on the
arguments to the program.  If you run the program without arguments,
the insertion point will not be updated when you click in the text
widget, if you call the program with the '--setpoint' option, it it
will.

If you want the insertion point to follow the cursor at all time, you
should probably cach the button press event, and then move the
insertion point.  Catch it _after_ the widget has had the change to
update the cursor position though.  See program 2.

        /mailund


===File ~/tmp/foo.c=========================================
#include <gtk/gtk.h>

static gboolean set_point = FALSE;

static gint
insert_foo (GtkWidget *text)
{
  /* jump to cursor mark */
  if (set_point) {
    gtk_text_set_point (GTK_TEXT (text),
			gtk_editable_get_position (GTK_EDITABLE  (text)));
  }

  gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "foo", strlen ("foo"));
  return TRUE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window, *text;

  gtk_init (&argc, &argv);

  if (argc == 2) {
    if (strcmp (argv[1],"--setpoint") == 0) {
      set_point = TRUE;
    }
  }

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  text = gtk_text_new (NULL, NULL);
  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_container_add (GTK_CONTAINER (window), text);

  gtk_timeout_add (600, insert_foo, text);

  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}
============================================================

===File ~/tmp/bar.c=========================================
#include <gtk/gtk.h>

static void
insert_bar (GtkWidget *text)
{
  /* jump to cursor mark */
  gtk_text_set_point (GTK_TEXT (text),
		      gtk_editable_get_position (GTK_EDITABLE  (text)));

  gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "bar", strlen ("bar"));
}

int
main (int argc, char *argv[])
{
  GtkWidget *window, *text;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  text = gtk_text_new (NULL, NULL);
  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_container_add (GTK_CONTAINER (window), text);

  gtk_signal_connect_after (GTK_OBJECT (text), "button_press_event",
			    GTK_SIGNAL_FUNC (insert_bar),
			    NULL);

  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}
============================================================



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