LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2012, 11:03 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
load file into gtktextveiw


Hey y'all I Have a UI I wrote 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, but then I am having other issues. Maybe there is a different way? A function for just this?

Ideas? Thanks.

Last edited by amboxer21; 05-13-2012 at 11:07 PM.
 
Old 05-14-2012, 07:09 AM   #2
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Manjaro
Posts: 1,791

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
Here's a block of code from one of my programs, basically doing what you're trying to do. I read the info into a buffer (archinfo_msg), then dump it into the textview's buffer (b)
Code:
    archinfo = os.popen(my_cmd,'r')
    line = archinfo.readline()
    while line:
      archinfo_msg = archinfo_msg+line
      line = archinfo.readline()
    b = self.archive_info.get_buffer()
    b.set_text(archinfo_msg)
 
Old 05-14-2012, 12:50 PM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by RockDoctor View Post
Here's a block of code from one of my programs, basically doing what you're trying to do. I read the info into a buffer (archinfo_msg), then dump it into the textview's buffer (b)
Code:
    archinfo = os.popen(my_cmd,'r')
    line = archinfo.readline()
    while line:
      archinfo_msg = archinfo_msg+line
      line = archinfo.readline()
    b = self.archive_info.get_buffer()
    b.set_text(archinfo_msg)
Yeah, I'm working with C programming. I wish it were as easy as python makes it lol I really do! I'm pretty sure I have to keep track of iterators and a whole bunch of other nonsense.

So far I have used C to read a file into a buffer. Then a function to put that array into the GtkTextBuffer *buffer. Like I said, I most likely need to set and keep track of the iterator.
 
Old 05-14-2012, 09:02 PM   #4
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
So I got the textview widget to take in a file, but there are complications. It will only work if the file has characters like this
Code:
/nx/fd/ /x /g
I'm guessing I need to translate the array to UTF-8? Would this be the proper way; To convert the array to utf? Would I need to re-write my whole file to buffer function from C to gtk? How would I go about this?

UPDATE: I think i have the problem "almost" solved. I am using g_convert to convet the ascii to utf-8.

NEW 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", NULL, 0, &err);   
gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
gtk_text_buffer_insert(buffer, &iter, UTF, 0);
//gtk_text_buffer_insert(buffer, &iter, "GtkTextView Widget\n", -1);
gtk_widget_show(view);
I receive this error:
Code:
(warning:6280): Gtk-CRITICAL **: IA__gtk_text_buffer_insert: assertion `text != NULL' failed
I can't figure out how to set the params of bytes_read and bytes_written in the g_convert function.

here are the relevant parts of my code:
OLD CODE:
Code:
FILE *file;
char array[4096];
gchar *text; //Not used at the moment.

gtk_init(&argc, &argv);

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);

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));
gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
gtk_text_buffer_insert(buffer, &iter, (gchar *)array, -1);
//gtk_text_buffer_insert(buffer, &iter, "GtkTextView Widget\n", -1);
gtk_widget_show(view);

Last edited by amboxer21; 05-14-2012 at 10:15 PM.
 
Old 05-15-2012, 12:31 AM   #5
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
UPDATE:
This way was actually wrong. It had a ton of bugs and errors! I have updated wih the actual working code.

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);
	}
BAD CODE: WORKING CODE IS IN PRECEEDING POST.
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);
	}

/*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);

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;
}
Thanks!

Last edited by amboxer21; 05-16-2012 at 02:37 AM. Reason: Program wasn't actually working correctly. Letting all know.
 
Old 05-15-2012, 11:04 PM   #6
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
The hard and persistent work finlly paid off! I actually removed the array and fread function. It was causing the text output in the textview widget to display/act really funky!

After 3 days on this I have finally got the darn program to work properly.

What I did:
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 amboxer21; 05-15-2012 at 11:10 PM.
 
Old 05-16-2012, 01:01 AM   #7
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
crontab -e load a file into casperdaghost Linux - Newbie 1 07-24-2010 01:24 AM
why i can open the BT down load file haheyhahey Linux - Newbie 1 12-19-2007 01:36 PM
cannot load wmv file ringo1 Linux - Desktop 2 11-12-2006 04:44 AM
Only load last portion of a file, instead of the whole really big text file 1veedo Linux - General 2 02-20-2006 06:34 PM
Websites load slow, but once they load, any links inside them load fast smurcoch Mandriva 3 02-06-2006 12:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration