Re: talking about gtk_text widget

>>>>> "X" == Xavier Ingles Rubinat <> writes:

 X> Hi everybody.  I've got a problem using the gtk_text widget. When
 X> you get it, you can load a text file in the screen, you can edit
 X> this text using the "Editable" function and you can use the "wrap
 X> words" function too.... If you want to save the changes done in
 X> the text, you can't do it, or better i can't do it!!!

 X> If somebody has a saving-function, or knows where i can get a
 X> piece of this code i would be very grateful!!!!

Here you are:

static void
save_file_cb (char *filename)
  int i, len;
  char *buf;
  FILE *fp = fopen (filename, "w");

  if (!fp) return;

  gtk_text_freeze (GTK_TEXT (mainbuffer));

  /* get_chars allocs a new buffer each time, so I have to do all this
   * g_free stuff */  
  len = gtk_text_get_length (GTK_TEXT (mainbuffer));
  for (i = 0; i < len - BUF_MAX_SIZE;) {
    buf = gtk_editable_get_chars (GTK_EDITABLE (mainbuffer), i, i + BUF_MAX_SIZE);
    fputs (buf, fp);
    i += BUF_MAX_SIZE;
    g_free (buf);
  /* and the leftovers */
  buf = gtk_editable_get_chars (GTK_EDITABLE (mainbuffer), i, len);
  fputs (buf, fp);
  g_free (buf);

  gtk_text_thaw (GTK_TEXT (mainbuffer));

  fclose (fp);

Here mainbuffer is a global var, pointing to an object of a subclass
of GtkText, but you should get the picture.

It may not be that nice, but it works.


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