How to save GTKTextBuffer contents to a file

I am writing on Ubuntu 12.04 in Anjuta with a C and GTK program. This is the GUI for nbc (Lego NXT compiler). I have a GTKTextView. Now I want to save the contents of the text view to a file that GTKFileChooser can select. Now I do not know how to get text from a TextView and write it to a file. How to do it?

+5
source share
3 answers

First enter GtkTextBuffer from GtkTextView using gtk_text_view_get_buffer() . Then get the start and end of GtkTextIters from the buffer to use to get the text of the buffer. Finally, write this text to a file using the API of your choice, however I would recommend Gio . Here is a snippet of 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:

+5
source
 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 in a text box 1.

+1
source

I'm really struggling to save what is in the buffer to a text file. There is literally nothing comprehensive in how to deal with the two together. Much about opening files, but not about saving them! This is the closest I've seen, it explains. You mention a tutorial. Can you post a link to it?

Hurrah

0
source

All Articles