# Gtk::TextBuffer colorful

**URL:** <https://www.ruby-forum.com/t/gtk-textbuffer-colorful/88855>\
**Category:** Ruby\
**Created:** [March 15, 2007, 1:02am UTC](https://www.ruby-forum.com/t/gtk-textbuffer-colorful/88855 "2007-03-15T01:02:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Gabel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@Karl\_Gabel](https://www.ruby-forum.com/u/Karl_Gabel)\
**Post date:** [March 15, 2007, 1:02am UTC](https://www.ruby-forum.com/t/gtk-textbuffer-colorful/88855/1 "2007-03-15T01:02:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karl\_Gabel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@Karl\_Gabel](https://www.ruby-forum.com/u/Karl_Gabel)\
**Post date:** [March 15, 2007, 1:34am UTC](https://www.ruby-forum.com/t/gtk-textbuffer-colorful/88855/2 "2007-03-15T01:34:45Z")

</div>

Le jeudi 15 mars 2007 01:02, Karl Gabel a écrit :

> ```
> #Gdk::flush
> 
> ```
> 
> 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 !

---

<div class="post-metadata">

**Author:** ![Karl\_Gabel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@Karl\_Gabel](https://www.ruby-forum.com/u/Karl_Gabel)\
**Post date:** [March 15, 2007, 3:41am UTC](https://www.ruby-forum.com/t/gtk-textbuffer-colorful/88855/3 "2007-03-15T03:41:59Z")

</div>

Olivier R. schrieb:

> > this line is black
> > 
> > text\_color(“black”)  
> > Thanks!  
> > 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
