I'm writing on Ubuntu 12.04 in Anjuta with C and GTK a program. It's a graphical interface for the nbc (Lego NXT Compiler). I have a GTKTextView. Now I want to save the content of the textview to a file, which could be chosen by a GTKFileChooser. Now I don't know how to get the text from the TextView and write it to the file. How do i do this?
Asked
Active
Viewed 5,575 times
2 Answers
6
First, get the GtkTextBuffer
from the GtkTextView
using gtk_text_view_get_buffer()
. Then get the start and end GtkTextIters from the buffer to use to get the text of the buffer. Finally, write that text to the file using the API of your choice, however, I would recommend Gio
. Here's a snippet from my old tutorial:
gtk_widget_set_sensitive (text_view, FALSE);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->text_view));
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
gtk_text_buffer_set_modified (buffer, FALSE);
gtk_widget_set_sensitive (editor->text_view, TRUE);
/* set the contents of the file to the text from the buffer */
if (filename != NULL)
result = g_file_set_contents (filename, text, -1, &err);
else
result = g_file_set_contents (editor->filename, text, -1, &err);
if (result == FALSE)
{
/* error saving file, show message to user */
error_message (err->message);
g_error_free (err);
}
g_free (text);
Check out the following API documentation:

gpoo
- 8,408
- 3
- 38
- 53

Micah Carrick
- 9,967
- 3
- 31
- 43
-
By the way, `result` is a gboolean, `buffer` is a GtkTextBuffer, `err` is a GError, and `start`/`end` are GtkTextIter. It's taken from this code: http://www.micahcarrick.com/files/gtk-glade-tutorial/C/main.c – Micah Carrick Jun 18 '12 at 18:33
-
I implemented the code in my program, but it crashes with Error code 11 (segmentation fault). What's wrong? This is my function: – user1464420 Jun 19 '12 at 13:05
-
Can you paste in the function you actually used? – Micah Carrick Jun 19 '12 at 13:07
-
Does this solution also take the TAGS into account? What if I use tags and I want to store my rich text to a file? – cfa45ca55111016ee9269f0a52e771 Feb 20 '13 at 09:40
2
void on_toolbutton3_clicked(GtkToolButton *toolbutton, gpointer data)
{
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Abspeichern...",
NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
char *filename;
char *text;
GtkTextIter *start;
GtkTextIter *end;
gboolean result;
GError *err;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_widget_set_sensitive (data, FALSE);
savebuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data));
gtk_text_buffer_get_start_iter (savebuffer, &start);
gtk_text_buffer_get_end_iter (savebuffer, &end);
text = gtk_text_buffer_get_text (savebuffer, &start, &end, FALSE);
gtk_text_buffer_set_modified (savebuffer, FALSE);
gtk_widget_set_sensitive (data, TRUE);
/* set the contents of the file to the text from the buffer */
if (filename != NULL)
result = g_file_set_contents (filename, text, -1, &err);
else
result = g_file_set_contents (filename, text, -1, &err);
if (result == FALSE)
{
/* error saving file, show message to user */
}
g_free (text);
}
gtk_widget_destroy (dialog);
}
data points on textview1.

user1464420
- 89
- 1
- 4