tags:

views:

47

answers:

4

Hi,

When you write a program in c which has a lot of printf for a lot purpose: debuggin, information, etc. I would like to do something like that in gtk, having some widget that would display all the printf messages I usually would do in a normal c program

Thanks,

---UPDATE--- I'm using vte as ptomato advised. Although some problems came up. I was using glade-3 and when the file glade was open the vte widget didn't show. I notice that a lot of Warning and 2 Critical errors would display while opening. One of those critical was

Unable to load module 'vte' from any search paths

I installed libvte-dev throug apt-get and know i see as in the widget palette

A: 

You can use asprintf() as a GNU extension to perform formatted output which it is stored in a new allocated memory and must be freed later.

char* str;
asprintf(&str, "It is a number %d\n", 1);
//...
free(str);

Instead that one, you can use snprintf() which didn't allocate anything for you, it just fill an array of chars for you (as a formatted output).

char str[100];
snprintf(str, 100, "It is number %d\n", 1);

Also you can use g_string_printf() which is a glib function, but it stores the formatted output to a GString variable.

GString str;
g_string_printf(&str, "It is number %d\n", 1);
//...
g_string_free(&str, TRUE);

When you got your string as a formatted output, you can use it as a text for any GTK widget.

PC2st
this doesn't really answer the question, he is asking how to create a widget which could be used to display this logging information to the user.
Evan Teran
A: 

You can either create a text view or use the label.

Add this text view to the parent window say Gtk Main window. You can decide which other widgets to add. (may be a scroll window and then a text view).

Use the api gtk_text_view_set_buffer to display the text on to the window everytime you want to display a log.

Praveen S
+1  A: 

You can use VTE, a terminal emulator widget. I'm not sure, but I think you can redirect your program's stdout to the widget using vte_terminal_set_pty().

ptomato
I can't find vte in glade... help?
gvalero87
I got it. I updated my question with the answer
gvalero87
A: 

You should simply use a GtkTextView and use these functions to redirect your logging into it:

  • g_log_set_default_handler() for g_message()/g_warning()/etc. (everything that goes through g_log())
  • g_set_print_handler() (for g_print())
  • g_set_printerr_handler() (for g_printerr())

Unfortunately, the simple printf() cannot be redirected easily (but g_print() is a good replacement for it).

PS: Sorry, I can only paste one link for you (new user)…

Sven Herzberg
do you have any example?
gvalero87