GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Aug 30, 2016 1:08 pm

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Inserting text on a GtkTextView, with colors and styles
PostPosted: Tue Mar 25, 2008 10:19 pm 
Familiar Face

Joined: Tue Mar 25, 2008 9:41 pm
Posts: 7
#include <gtk/gtk.h>

int main (int argc, char **argv)
GtkWidget *window, *text;
GtkTextBuffer *buffer;
GtkTextIter iter;
GdkColor color;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);

text = gtk_text_view_new ();
gtk_container_add (GTK_CONTAINER (window), text);
gtk_text_view_set_editable (GTK_TEXT_VIEW (text), FALSE);
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text), FALSE);
gtk_text_view_set_justification (GTK_TEXT_VIEW (text), GTK_JUSTIFY_LEFT);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text), GTK_WRAP_WORD);
gtk_widget_show (text);

gdk_color_parse ("yellow", &color);
gtk_widget_modify_base (text, GTK_STATE_NORMAL, &color);
gtk_widget_modify_base (text, GTK_STATE_ACTIVE, &color);
gtk_widget_modify_base (text, GTK_STATE_PRELIGHT, &color);
gtk_widget_modify_base (text, GTK_STATE_SELECTED, &color);
gtk_widget_modify_base (text, GTK_STATE_INSENSITIVE, &color);

buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));

gdk_color_parse ("blue", &color);
gtk_text_buffer_create_tag (buffer, "color_blue", "foreground-gdk", &color, NULL);
gdk_color_parse ("red", &color);
gtk_text_buffer_create_tag (buffer, "color_red", "foreground-gdk", &color, NULL);

gtk_text_buffer_create_tag (buffer, "style_italic", "style", PANGO_STYLE_ITALIC, NULL);
gtk_text_buffer_create_tag (buffer, "weight_bold", "weight", PANGO_WEIGHT_BOLD, NULL);
gtk_text_buffer_create_tag (buffer, "size_medium", "scale", PANGO_SCALE_MEDIUM, NULL);
gtk_text_buffer_create_tag (buffer, "size_large", "scale", PANGO_SCALE_LARGE, NULL);

gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Hello", -1,
"size_medium", "style_italic", "color_blue", NULL);
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "World!", -1,
"size_large", "weight_bold", "color_red", NULL);

gtk_widget_show (window);

gtk_main ();
return 0;

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group