Thread: loading file int gtktextview widget

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    loading file int gtktextview widget

    This is relevant to both C programming and Linux. I decided to post here.

    I Have that port monitor UI I wrote and that i am building on. Now I wanted to load a file into a gtktextview widget.

    How would I go about doing this?

    I have thought about loading the file into a buffer, with C programming.
    Something like this
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
    FILE *file;
    char buffer[4096];
    
    file = fopen("/home/annonymous/Documents/netstat.txt", "r+");
    	if(file == NULL) {
    	printf("FOPEN(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    file = fopen("/home/annonymous/Documents/netstat.txt", "r+");
    	if(file < 0) {
    	printf("FOPEN(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    fread(buffer, sizeof(buffer), 1, file);
    	if(file < 0) {
    	printf("FREAD(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    printf("%s", buffer);
    
    return 0;
    }
    Then incorporate it into the UI. I ran into some conflicting types though. The buffer needed for the UI is of type struct. While the C buffer is obviously not. I am looking for a different way. More specifically a function made for just this. Thanks.

    FOR ALL THE PEOPLE WHO HELPED ON THE BASH SCRIPT; Ultimately I will want all the opened ports other than 443 and 80 displayed in the UI. Thanks again!

    Ideas? Thanks.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The buffer needed for the UI is of type struct.
    O_o

    So? Programming isn't easy; sometimes you have to do work.

    This is one of those times.

    Create an instance of that structure using one of the provided conversion routines and add text to the view.

    You can buffer the entire file. You can read it line by line.

    This is like a ten line function if you already have the text view up and going.

    Soma

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    GtkTextBuffer

    PS
    Lines 16 to 20 of your post are complete rubbish.
    1. You've already opened the file once already.
    2. You don't compare a pointer with < 0

    Line 23, you don't compare a file with < 0 to test whether fread() was successful or not.
    fread() returns a result, use it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Quote Originally Posted by phantomotap View Post
    O_o

    So? Programming isn't easy; sometimes you have to do work.

    This is one of those times.

    Create an instance of that structure using one of the provided conversion routines and add text to the view.

    You can buffer the entire file. You can read it line by line.

    This is like a ten line function if you already have the text view up and going.

    Soma
    Your right @phantomotap! lol It is work a lot of the time but it is fun. I'll keep at it then. So, I can read the file into a buffer with C, no problem. The GTK way is what's the problem. i have a blank textview up with my UI. I guess I am seeking help to fast and to soon? I need to work the problem out instead of the constant unnecessary postings. Well, then I'm going to get back to google and the documentation to see what I can cook up.

    Sorry @Salem, I was obviously not thinking lol I took out --> file = fopen("/home/annonymous/Documents/netstat.txt", "r+"); I thought I needed to test for less than 0 and NULL with files. Maybe I'm wrong and I guess working on sockets and UI's only has left me rusty on the basics! Thanks

    ALso, I made that mistake with checking the return value of fread because I copy and pasted the previous if statement and forgot to change the conditions. I do that a lot when I spend along time at the computer. I've been at my laptop since 8pm and it is now 2:05am. Maybe a bad habit I should break!

    I CAN'T BELIEVE I MISSED THOSE OTHER MISTAKES<SHAKES HEAD!>
    Too much time in front of the computer maybe??
    Last edited by Annonymous; 05-14-2012 at 12:08 AM.

  5. #5
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Can someone povide an example on how to use bytes_read and bytes_written with g_convert?

    I figured out how to load a file into the textview widget but it only works if characters like /ca /ma occupy the file. So i am using g_convert to convert the ascii text to UTF-8.

    All I am missing is those 2 params. Thanks

    Here is the relevant code:
    Code:
    view = gtk_text_view_new();
    gtk_table_attach_defaults(GTK_TABLE(table), view, 1, 9, 1, 5);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
    ch = g_strdup((gchar *)array);
    UTF = g_convert(ch, strlen(ch), "UTF-8", "us-ascii", ?, ?, &err);   
    gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
    gtk_text_buffer_insert(buffer, &iter, UTF, -1);
    //gtk_text_buffer_insert(buffer, &iter, "GtkTextView Widget\n", -1);
    gtk_widget_show(view);
    I recieve this error:
    Code:
    (warning:6280): Gtk-CRITICAL **: IA__gtk_text_buffer_insert: assertion `text != NULL' failed
    Thanks again.
    Last edited by Annonymous; 05-14-2012 at 09:16 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Character Set Conversion

    Code:
    gchar *             g_convert                           (const gchar *str,
                                                             gssize len,
                                                             const gchar *to_codeset,
                                                             const gchar *from_codeset,
                                                             gsize *bytes_read,
                                                             gsize *bytes_written,
                                                             GError **error);
    Declare two variables, like

    gsize bytes_read, bytes_written;
    UTF = g_convert(ch, strlen(ch), "UTF-8", "us-ascii", &bytes_read, &bytes_written, &err);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Yeah no matter how I code it, returns
    Code:
    Invalid or incomplete multibyte or wide character --> 
    
    (warning:8757): Gtk-CRITICAL **: IA__gtk_text_buffer_insert: assertion `text != NULL' failed
    I think it has something to do with this function
    Code:
    gtk_text_buffer_insert(buffer, &iter, UTF, -1);

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
    ch = g_strdup((gchar *)array);
    UTF = g_convert(ch, strlen(ch), "UTF-8", "us-ascii", ?, ?, &err);
    gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
    gtk_text_buffer_insert(buffer, &iter, UTF, -1);

    So where are you put array / ch / UTF into buffer?

    Character Set Conversion
    Did you see that this can return NULL?
    Perhaps UTF is NULL when you pass it into gtk_text_buffer_insert, and that's where the assert comes from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I re-wrote my file to array function from C specific to GTK specific and it works with no problems now.

    FROM THIS:
    Code:
    file = fopen("/home/amboxer21/Documents/netstat.txt", "r+");
    	if(file == NULL) {
    	printf("FOPEN(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if(file < 0) {
    	printf("FOPEN(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    fread(array, sizeof(array), 1, file);
    	if(fread < 0) {
    	printf("FREAD(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    //printf("%s", array);
    TO THIS:
    Code:
    array = g_array_new(FALSE, FALSE, sizeof(gint));
    
    FILE *file = g_fopen("/home/amboxer/Documents/netstat.txt", "r+");  
    	if(file == NULL) {
    	printf("FOPEN(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    fread(array, sizeof(array), 2, file);
    	if(fread < 0) {
    	printf("FREAD(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    The whole program:
    Code:
    #include <glib/gstdio.h>
    #include <gtk/gtk.h>
    #include <glib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    
    static void destroy_event(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
    }
    
    static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
    gtk_main_quit();
    return FALSE;
    }
    
    static void callback( GtkWidget *widget, gpointer data ) {
    gtk_main_quit();
    }
    
    static void killed( GtkWidget *widget, GdkEvent *event, gpointer data ) {
    execl("/usr/bin/killall", "killall", "watch", (char *)NULL);
    gtk_main_quit();
    }
    
    //To compile append `pkg-config --cflags --libs gtk+-2.0`
    int main(int argc, char *argv[]) {
    
    GtkWidget *window;
    GtkWidget *table;
    GtkWidget *button;
    GtkWidget *button2;
    GtkWidget *frame;
    GtkWidget *view;
    
    GtkTooltips *tooltips;
    
    GtkTextBuffer *buffer;
    GtkTextIter iter;
    
    //FILE *file;
    GArray *array;
    //char array[4096];
    gchar *ch;
    gchar *UTF;
    gsize *length;
    GError *err = NULL;
    gsize *bytes_read = NULL; 
    gsize *bytes_written = NULL;
    
    gtk_init(&argc, &argv);
    
    array = g_array_new(FALSE, FALSE, sizeof(gint));
    
    FILE *file = g_fopen("/home/amboxer21/Documents/netstat.txt", "r+");  
    	if(file == NULL) {
    	printf("FOPEN(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    fread(array, sizeof(array), 2, file);
    	if(fread < 0) {
    	printf("FREAD(-1) error --> %s", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "SECURITY WARNING");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 500, 400);
    gtk_widget_show(window);
    
    table = gtk_table_new(10, 10, TRUE);
    gtk_container_add(GTK_CONTAINER(window), table);
    gtk_widget_show(table);
    
    view = gtk_text_view_new();
    gtk_table_attach_defaults(GTK_TABLE(table), view, 1, 9, 1, 5);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
    //ch = g_strdup((gchar *)array);
    UTF = g_convert((gchar *)array, strlen((gchar *)array), "UTF-8", "us-ascii", bytes_read, bytes_written, &err);
         if (err != NULL) {
         printf("%s --> \n", strerror(errno));
         g_error_free (err);
         } 
    
    gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
    gtk_text_buffer_set_text(buffer, UTF, strlen(UTF));
    //gtk_text_buffer_insert(buffer, &iter, UTF, -1);
    //gtk_text_buffer_insert(buffer, &iter, "GtkTextView Widget\n", -1);
    gtk_widget_show(view);
    
    frame = gtk_frame_new("SUSPICIOUS PORT IS OPEN");
    gtk_table_attach_defaults(GTK_TABLE(table), frame, 1, 9, 1, 9);
    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
    gtk_widget_show(frame);
    
    button = gtk_button_new_with_label("CONFIRM");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 2, 5, 6, 8);
    gtk_widget_show(button);
    
    tooltips = gtk_tooltips_new();
    gtk_tooltips_set_tip (tooltips, button, "Click to confirm that you understand that there are suspicious ports open on your computer.", NULL);
    
    button2 = gtk_button_new_with_label("KILL");
    gtk_table_attach_defaults(GTK_TABLE(table), button2, 5, 8, 6, 8);
    gtk_widget_show(button2);
    
    tooltips = gtk_tooltips_new();
    gtk_tooltips_set_tip(tooltips, button2, "Click to stop this program entirely.", NULL);
    
      g_signal_connect_swapped(G_OBJECT(window), "destroy-event",
          G_CALLBACK(destroy_event), NULL);
    
      g_signal_connect_swapped(G_OBJECT(window), "delete-event",
          G_CALLBACK(delete_event), NULL);
    
      g_signal_connect(button, "clicked",
          G_CALLBACK (callback), (gpointer) "cool button");
    
      g_signal_connect_swapped(button2, "clicked",
          G_CALLBACK(killed), (gpointer) "killed");
    
    gtk_main_iteration();
    gtk_main();
    
    return 0;
    }
    Only problem is Now I need to print all elements of the file not just 2. So..??
    Code:
    fread(array, sizeof(array), 2, file);
    Thanks @Salem!
    Last edited by Annonymous; 05-14-2012 at 11:39 PM.

  10. #10
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    The previos post has code that had a ton or errors and bugs. I have been at it since that last post and fixed everything. I removed the array and fread function. It was causing the text output in the textview widget to display/act funky!

    I Used iterators for point of insertion. g_fopen to open a file descriptor. get file contents to store the wanted/needed file into a string. Created the view widget. Converted the ascii file to utf-8 with g_convert. Got the off-set, then inserted the string into the buffer with the gtk_text_buffer_insert function.
    ----> WORKS FLAWLESSLY!!!! <----

    JUST IN CASE ANYONE ELSE HAS THE SAME PROBLEM:

    Code:
    #include <glib/gstdio.h>
    #include <gtk/gtk.h>
    #include <glib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    
    static void destroy_event(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
    }
    
    static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
    gtk_main_quit();
    return FALSE;
    }
    
    static void callback( GtkWidget *widget, gpointer data ) {
    gtk_main_quit();
    }
    
    static void killed( GtkWidget *widget, GdkEvent *event, gpointer data ) {
    execl("/usr/bin/killall", "killall", "watch", (char *)NULL);
    gtk_main_quit();
    }
    
    //To compile append `pkg-config --cflags --libs gtk+-2.0`
    int main(int argc, char *argv[]) {
    
    GtkWidget *window;
    GtkWidget *table;
    GtkWidget *button;
    GtkWidget *button2;
    GtkWidget *frame;
    GtkWidget *view;
    
    GtkTooltips *tooltips;
    
    GtkTextBuffer *buffer;
    GtkTextIter iter;
    
    gchar *array;
    gchar *utf8;
    GError *err = NULL;
    gsize *bytes_read = NULL; 
    gsize *bytes_written = NULL;
    gsize length;
    int count = 0;
    char ch;
    
    gtk_init(&argc, &argv);
    
    FILE *file = g_fopen("/home/annonymous/Documents/netstat2.txt", "r");  
    	if(file == NULL) {
    	printf("FOPEN(NULL) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if(file < 0) {
    	printf("FOPEN(-1) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	if(file == 0) {
    	printf("FOPEN(0) error --> %s.\n", strerror(errno));
    	exit(EXIT_FAILURE);
    	}
    
    	while((ch != EOF) && (ch != '\n')) {
    	ch = fgetc(file);
    		if(ch == '\n') {
    		count++;
    		}
    
    g_file_get_contents("/home/annonymous/Documents/netstat2.txt", &array, &length, NULL);
    
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "SECURITY WARNING");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 500, 400);
    gtk_widget_show(window);
    
    table = gtk_table_new(10, 10, TRUE);
    gtk_container_add(GTK_CONTAINER(window), table);
    gtk_widget_show(table);
    
    view = gtk_text_view_new ();
    gtk_table_attach_defaults(GTK_TABLE(table), view, 1, 9, 1, 5);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));		
    
    utf8 = g_convert(array, length, "UTF-8", "us-ascii", bytes_read, bytes_written, &err);
         if (err != NULL) {
         printf("G_CONVERT(!NULL) error --> %s \n", strerror(errno));
         g_error_free(err);
         }
    
    gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
    gtk_text_buffer_insert(buffer, &iter, utf8, -1);
    //gtk_text_buffer_set_text(buffer, utf8, -1);
    gtk_widget_show(view);
    
    //gtk_text_buffer_insert(buffer, &iter, "OPEN PORTS\n", -1);
    
    frame = gtk_frame_new("SUSPICIOUS PORT IS OPEN");
    gtk_table_attach_defaults(GTK_TABLE(table), frame, 1, 9, 1, 9);
    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
    gtk_widget_show(frame);
    
    button = gtk_button_new_with_label("CONFIRM");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 2, 5, 6, 8);
    gtk_widget_show(button);
    
    tooltips = gtk_tooltips_new();
    gtk_tooltips_set_tip (tooltips, button, "Click to confirm that you understand that there are suspicious ports open on your computer.", NULL);
    
    button2 = gtk_button_new_with_label("KILL");
    gtk_table_attach_defaults(GTK_TABLE(table), button2, 5, 8, 6, 8);
    gtk_widget_show(button2);
    
    tooltips = gtk_tooltips_new();
    gtk_tooltips_set_tip(tooltips, button2, "Click to stop this program entirely.", NULL);
    
      g_signal_connect_swapped(G_OBJECT(window), "destroy-event",
          G_CALLBACK(destroy_event), NULL);
    
      g_signal_connect_swapped(G_OBJECT(window), "delete-event",
          G_CALLBACK(delete_event), NULL);
    
      g_signal_connect(button, "clicked",
          G_CALLBACK (callback), (gpointer) "cool button");
    
      g_signal_connect_swapped(button2, "clicked",
          G_CALLBACK(killed), (gpointer) "killed");
    
    gtk_main_iteration();
    gtk_main();
    
    return 0;
    }

    Now i need to make the textview area scrollable.
    Thanks to all!!
    Last edited by Annonymous; 05-15-2012 at 10:11 PM.

  11. #11
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I figured it out.

    I detached the textview from the table.(Removed the highlighted in red)
    Code:
    view = gtk_text_view_new ();
    //gtk_table_attach_defaults(GTK_TABLE(table), view, 1, 9, 2, 5);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
    And enabled the scrollable window with:
    Code:
    scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
    gtk_container_add(GTK_CONTAINER(scrolledwindow), view);
    gtk_table_attach_defaults(GTK_TABLE(table), scrolledwindow, 1, 9, 2, 5);
    gtk_widget_show(scrolledwindow);
    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Widget problem [GTK RELATED]
    By darekg11 in forum C Programming
    Replies: 5
    Last Post: 05-09-2011, 12:25 PM
  2. Widget tookits
    By Epy in forum Tech Board
    Replies: 0
    Last Post: 05-04-2011, 11:30 AM
  3. Best widget toolkit?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2010, 01:10 PM
  4. Latest Topics widget
    By Programmer_P in forum Tech Board
    Replies: 1
    Last Post: 07-24-2009, 09:25 AM
  5. case sensitivity in GtkTextView
    By MK27 in forum Linux Programming
    Replies: 2
    Last Post: 08-07-2008, 03:16 PM

Tags for this Thread