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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-13-2012, 11:03 PM
#1
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Rep:
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 .
05-14-2012, 07:09 AM
#2
Senior Member
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Manjaro
Posts: 1,791
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)
05-14-2012, 12:50 PM
#3
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Original Poster
Rep:
Quote:
Originally Posted by
RockDoctor
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.
05-14-2012, 09:02 PM
#4
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Original Poster
Rep:
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
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 .
05-15-2012, 12:31 AM
#5
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Original Poster
Rep:
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.
05-15-2012, 11:04 PM
#6
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Original Poster
Rep:
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 .
05-16-2012, 01:01 AM
#7
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Original Poster
Rep:
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.
All times are GMT -5. The time now is 11:55 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News