[Question] gtk_text_set_point vs. the cursor




I'm hacking around using the text widget, and in a function I need to
move some text around. Doing that I change the point, but when I
return from the function I want to return to the point I was at when I
called the function.

To do that I first get the current point, move the stuff around, and
then set the point (with gtk_text_set_point ()) to the old point. You
get the idear from the test program below.

This works ok, as long is the text editing is beeing done from my
program. If you run the program below, and press tab a few times, you
see that point is indeed beeing restored.

But! The cursor doesn't change with the point, so if I start editing
my text manually, I find that I am *way* off from where I was supposed
to be.

I can get the behavior I want by using the virtual function (from
GtkEditable) set_position:

 GTK_EDITABLE_CLASS (parent_class)->set_position (GTK_EDITABLE (text), point);

(I'm workin on a subclass of the text widget, so parent_class is a
GtkText*)

This is a little less elegant though, and my program tends to crash
around that call :(

What I would rather do, is to set the cursor to the place of the
point. So in a moment of hubris I simply set the cursor_mark to the
point:

  GTK_TEXT (text)->cursor_mark = GTK_TEXT (text)->point;

It seems to work, but it does make me a little uncomfortable...

So what I would like to know is, what's the _right_way_ of doing this?
Is this safe? will it remain safe? If not, what should I do then?

/mailund


===File ~/src/X/gtk/texttest/test.c=========================
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

static gint
foo (GtkWidget *text, GdkEventKey *event)
{
  guint point;

  g_return_val_if_fail (text != NULL, FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  /* fun stuff */
  if (event->keyval == GDK_Tab) {
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "foo\n", 4);
    point = gtk_text_get_point (GTK_TEXT (text));
    g_print ("point is now %u, we'll try to keep it there...\n", point);
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "bar\n", 4);
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "baz\n", 4);
    gtk_text_set_point (GTK_TEXT (text), point);
    point = gtk_text_get_point (GTK_TEXT (text));
    g_print ("point is now %u.\n", point);  
    
    gtk_signal_emit_stop_by_name (GTK_OBJECT (text), "key_press_event");
    
    return TRUE;
  }
}

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

  /* initialize */
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  text = gtk_text_new(NULL, NULL);
  gtk_widget_show (text);

  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_container_add (GTK_CONTAINER (window), text);

  /* show */
  gtk_widget_show (window);

  gtk_signal_connect (GTK_OBJECT (text), "key_press_event",
		      GTK_SIGNAL_FUNC (foo),
		      NULL);
  
  /* main loop */
  gtk_main ();

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



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