Inserting text in a text buffer using a callback function



I have the following problem: while trying to learn GTK, I decided to create a button that should insert text in a scrolled window (more exactly: into a text buffer in a scrolled window). I created a callback for a function, and it wouldn't work. I transferred parts of code from the function to main (so that it is inserted during the realisation of the window), and that did work. The function behaves weird: before, the program would just segfault when this button is clicked, now it returns a Gtk-critical message saying that assertion GTK_IS_TEXT_BUFFER (buffer) for the argument I passed to the function failed (I don't understand that change either - I didn't change anything of significance). From what I learned from learning programming, the error is probably something very trivial, but it can take me days to sort out. Here are the significant parts of code:

  GtkWidget *view;
  GtkTextBuffer *buffer;

  button=gtk_button_new_with_label("text");   /*the button*/
g_signal_connect(G_OBJECT (button), "clicked", G_CALLBACK (text), buffer); /*connecting signal to callback*/

  view=gtk_text_view_new();
  buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW (view));   /*the buffer*/
gtk_text_buffer_set_text(buffer, "This is the text in the scrolled window\n", -1); /*inserting text, this works*/

void text(GtkTextBuffer *buffer)    /*the callback function*/
{
  GtkTextIter iter;
  gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
gtk_text_buffer_insert(buffer, &iter, "This is the text in the scrolled window\n", -1); /*this doesn't work*/
}

Of course, I've already run the code through a debugger. The callback works, the function text is called with pointer "buffer" as argument, it is the correct one, it is the correct type inside the function. Yet it doesn't work. I've also tried using exactly the same function that works in main (gtk_text_buffer_set_text()) in the callback function: nothing. I also thought about a conflict between the two variables with the same name "buffer" (which should hardly matter) and tried giving them different names: nothing. I'm quite stuck, I hope someone can help.

Thanks in advance
Horror Vacui




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