Hi,
I’m using Gtk 3.2 on Ubuntu 18.04.1 LTS with C language. I have GtkMenuItem ‘Help’, with submenu ‘contents’, this calls ‘callback_contents()’ as shown below. The included comments describe the problems in reading a textfile into a textview buffer to create help documentation. The textfile was created in another part of the program using other menuitems ‘file/new’ ‘file/save’ as per modified versions of Andrew Krauses Gtk 2 book. It compiles with no warnings (other than a couple of unused variables) and when the submenuitem ‘help/contents’ is clicked the contents of the .txt file are displayed in the transient window as desired, but the runtime message:
(myexec:3474): Gtk-CRITICAL **: 19:26:32.147: gtk_text_buffer_emit_insert: assertion 'g_utf8_validate (text, len, NULL)' failed
is displayed at the terminal, once for each character in the file. How do I stop this? It seems to me that by writing and reading from the same system would mean that it would naturally be UTF8 coded. I have had previous version built with ui manager that included <?xml version="1.0" encoding="UTF-8"?> command at the start of the ui file, but the current version is fully coded.
Also included below are the help_file.txt contents as well as other failed and commented attempts in the callback_contents() function.
void callback_contents(GtkMenuItem *contents, struct main_struct *stuff)
{
printf("Hello from callback_contents()\n");
int i = 0;
int count = 0;
GtkWidget *window_help = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_help), "Help");
gtk_container_set_border_width(GTK_CONTAINER(window_help), 5);
gtk_window_set_default_size(GTK_WINDOW(window_help), 600, 250);
gtk_window_set_position(GTK_WINDOW(window_help), GTK_WIN_POS_CENTER);
gtk_window_set_resizable(GTK_WINDOW(window_help), TRUE);
GtkTextBuffer *buffer_textview_help;
GtkWidget *textview_help;
GtkWidget *entry_help;
textview_help = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(textview_help), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview_help), TRUE);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview_help), GTK_WRAP_WORD);
gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview_help), 5);
gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview_help), 5);
gtk_text_view_set_pixels_inside_wrap(GTK_TEXT_VIEW(textview_help), 5);
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview_help), 10);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textview_help), 10);
entry_help = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry_help), "Search for ...");
buffer_textview_help = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_help));
// This works but I want to read it from file:
/*gtk_text_buffer_set_text(buffer_textview_help,
"Hello,\nhow\n are\n you? textview_help\n***********************\n\n\n\n\n\n\n** **\n\n\n\n\n\n\n** **\n\n\n\n\n\n\n** **\n\n\n\n\n\n\n** **\n\n\nroger1\n\n\n\n** **\n\n\nroger2\n\n\n\n** **\n\n\n\nroger3\n\n\n** **\n\n\n\nroger4\n\n\n** **\n\n\nroger5\n\n\n\n** **\n\n\n\nroger6\n\n\n** ****\n\n\n\nroger7\n\n\n** ***\n\n\n\nroger8\n\n\n** **\n\n\nroger9\n\n\n\n** **\n\n\n\nroger10\n\n\n** **\n\n\nroger11\n\n\n\n** **\nend\n", -1);*/
char line[82];
char string[303];
// Compiles but get segmentation fault when run
/*char help_contents;
FILE* infile_help_contents = fopen("help_file.txt", "r");
fscanf(infile_help_contents, "%s\n", &help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) &help_contents, -1);
fclose(infile_help_contents);*/
FILE* infile_help_contents = fopen("help_file.txt", "r");
// This works but only shows the first line (81 characters)
/*fgets(string, 166, infile_help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) string, -1);*/
// This works but also only shows the first line (81 characters)
/*fscanf(infile_help_contents, "%s\n", (char *) &string);
gtk_text_buffer_set_text(buffer_textview_help, (char *) string, -1);*/
/*******************************************************************************************/
// Here is the relevant bit:
// This shows all in the file, but get this run-time message:
// (myexec:3474): Gtk-CRITICAL **: 19:26:32.147: gtk_text_buffer_emit_insert:
// assertion 'g_utf8_validate (text, len, NULL)' failed
for (i=0; i<303; i++)
{
string[i] = fgetc(infile_help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) string, -1);
}
/*******************************************************************************************/
// No messages, but shows last line only
/*for (i=0; i<303; i++)
{
fgets(string, 303*sizeof(char *), infile_help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) string, -1);
}*/
// Only shows the last line
/*while (fgets(line, 82, infile_help_contents) != NULL)
{
fgets(line, 82, infile_help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) line, -1);
printf("%s\n", line);
count = count +1;
printf("count = %d\n", count);
i++;
}*/
// Only shows part of last line
/*while ((i = fgetc(infile_help_contents)) != EOF)
{
fgets(line, 82, infile_help_contents);
gtk_text_buffer_set_text(buffer_textview_help, (char *) line, -1);
printf("%s\n", line);
count = count +1;
printf("count = %d\n", count);
i++;
}*/
fclose(infile_help_contents);
GtkWidget *grid_help = gtk_grid_new();
GtkWidget *swin_textview_help = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_size_request (swin_textview_help, 300, 200);
gtk_widget_set_hexpand(swin_textview_help, TRUE);
gtk_widget_set_vexpand(swin_textview_help, TRUE);
gtk_container_add (GTK_CONTAINER (swin_textview_help), textview_help);
gtk_grid_attach(GTK_GRID(grid_help), entry_help, 0, 0, 10, 1);
gtk_grid_attach(GTK_GRID(grid_help), swin_textview_help, 0, 2, 10, 1);
gtk_container_add (GTK_CONTAINER (window_help), grid_help);
gtk_widget_show_all(window_help);
}
The contents of file ‘help_file.txt’:
help_file.txt line1 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn81
help_file.txt line2 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn81
help_file.txt line3 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn81
line4
line5
line6
line7
line8
line9
line10
last line (11)