From: Karl Gabel Date: 2007-03-15T11:41:32+09:00 Subject: Re: Gtk::TextBuffer colorful Olivier Renaud schrieb: > Le jeudi 15 mars 2007 01:02, Karl Gabel a �crit : > >> Hello, >> >> is there any way to bring some color into the Gtk::TextView for some lines? >> >> example: >> this line is black >> this line is black >> this line is red >> this line is black >> ... >> >> I use the Gtk::TextBuffer.insert_at_cursor(text) function, because I >> generate a lot of lines dynamically. >> >> @textv_w = @glade.get_widget("textview1") >> @buf = @textv_w.buffer >> >> >> but if I like to change the color ... >> >> def text_color(color) >> #Gdk::flush >> @textv_w.modify_text(Gtk::STATE_NORMAL, Gdk::Color.parse(color)) >> @buf= @textv_w.buffer >> end >> >> >> @buf.insert_at_cursor("this should be black") >> text_color("red") >> @buf.insert_at_cursor("this should be red") >> text_color("black") >> @buf.insert_at_cursor("this should be black again") >> >> ... the whole thing is black. >> >> I even tried to use the TextBuffer.text=(text) function. >> Gdk::flush does not work at all. >> Is there any way to flush the buffer before changing the color like >> $stdout.flush? >> >> >> Thanks! >> >> Karl >> > > Hello, > > For changing the color of a text (as well as the font, size, and so on), I > remember I used a markup system, for GtkLabels. I cannot find a reference to > markups in the doc for TextBuffer, but tags may be what you are looking for : > > TextBuffer#insert(iter, text, tag1, tag2, tag3, ...) > Inserts text into buffer at iter, applying an array of tags to the > newly-inserted text. Equivalent to calling Gtk::TextBuffer#insert(iter, > text), then Gtk::TextBuffer#apply_tag on the inserted text. > > These tags are instances of class Gtk::TextTag, used to set the colors > (foreground and background), size, font, style, ... > > I haven't tried yet, so tell us if this works for you ! > > Hello again, this works really nice @buf.create_tag("tag",{"foreground"=>"red"} ) @buf.insert_at_cursor("this line is still black") start = @buf.get_iter_at_offset(@buf.end_iter.offset) @buf.insert(start, "this line is red", "tag") @buf.apply_tag("tag",start, @buf.end_iter) @buf.insert_at_cursor("this line is still black") Thanks again! Karl