Lance Dillon wrote:
> - Original Message
> From: Carlos Pereira <[EMAIL PROTECTED]>
> To: gtk-app-devel-list@gnome.org
> Sent: Monday, March 17, 2008 8:46:02 AM
> Subject: GtkTextView: inserting text with different styles
>
> Hi,
> Let's say I have a GtkTextView, with two color tags, red and blue:
>
> text_view = gtk_text_view_new ();
> buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
> gtk_text_buffer_create_tag (buffer, "my_red", "foreground", "#ff",
> NULL);
> gtk_text_buffer_create_tag (buffer, "my_blue", "foreground", "#00ff00",
> NULL);
>
> Now I want too insert "Hello World":
> gtk_text_buffer_insert_at_cursor (buffer, "Hello ", -1);
> gtk_text_buffer_insert_at_cursor (buffer, "World", -1);
>
> but I want Hello to be red and World to be blue.
>
> What is the usual procedure to achieve this?
> (perhaps get the iterators for the last insertion? it should be quite
> easy but apparently I could not find information about this...)
>
> Thanks,
> Carlos
> ---
> I would do this:
>
> GtkTextIter iter;
> GtkTextTag *myred,*myblue;
> GtkTextTagTable *tb;
>
> /* blah blah blah */
> /* start at top */
> gtk_text_buffer_get_iter_at_offset(buffer,&iter,0);
> tb=gtk_text_buffer_get_tag_table(buffer);
> myred=gtk_text_tag_new("my_red");
> g_object_set(G_OBJECT(myred),"foreground","#ff",NULL);
> myblue=gtk_text_tag_new("my_blue");
> g_object_set(G_OBJECT(myblue),"foreground","#00ff00",NULL);
> gtk_text_tag_table_add(GTK_TEXT_TAG_TABLE(tb),myred);
> gtk_text_tag_table_add(GTK_TEXT_TAG_TABLE(tb),myblue);
>
> gtk_text_buffer_insert(buffer,&iter,"random text...",-1);
> gtk_text_buffer_insert_with_tags(buffer,&iter,"Hello ",-1,myred,NULL);
> gtk_text_buffer_insert_with_tags(buffer,&iter,"World",-1,myblue,NULL);
>
>
>
>
> There may be a little more efficient way to do this (add multiple tags to a
> tag table at the same time, perhaps?), but this is just off the top off my
> head...
>
>
>
>
>
>
>
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search.
> http://tools.search.yahoo.com/newsearch/category.php?category=shopping
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
>
Thanks a lot, that's what I was looking for.
I include below a simplified (43 lines) working
example, of a GtkTextView with a base color,
and two words in different foreground colors.
Carlos
--color text view
#include
int main (int argc, char **argv)
{
GtkWidget *window, *vbox, *text;
GtkTextBuffer *buffer;
GtkTextTag *red, *blue;
GtkTextIter iter;
GdkColor color;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add (GTK_CONTAINER (window), vbox);
text = gtk_text_view_new ();
gtk_box_pack_start (GTK_BOX (vbox), text, 1, 1, 0);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (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);
gdk_color_parse ("red", &color);
red = gtk_text_buffer_create_tag (buffer, "red", "foreground-gdk",
&color, NULL);
gdk_color_parse ("blue", &color);
blue = gtk_text_buffer_create_tag (buffer, "blue", "foreground-gdk",
&color, NULL);
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
gtk_text_buffer_insert_with_tags (buffer, &iter, "Hello ", -1, red, NULL);
gtk_text_buffer_insert_with_tags (buffer, &iter, "World!", -1, blue, NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list