Forum: Ruby Gtk::TextBuffer colorful

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
342751daf814ff3b8faa885ec94d3d2f?d=identicon&s=25 Karl Gabel (Guest)
on 2007-03-15 01:02
(Received via mailing list)
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
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-15 01:34
(Received via mailing list)
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 !
342751daf814ff3b8faa885ec94d3d2f?d=identicon&s=25 Karl Gabel (Guest)
on 2007-03-15 03:41
(Received via mailing list)
Olivier Renaud 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
This topic is locked and can not be replied to.