Gtk::TextBuffer colorful

K

Karl Gabel

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
 
O

Olivier Renaud

Le jeudi 15 mars 2007 01:02, Karl Gabel a =E9crit=A0:
Hello,

is there any way to bring some color into the Gtk::TextView for some line= s?

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 =3D @glade.get_widget("textview1")
@buf =3D @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=3D @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=3D(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,

=46or changing the color of a text (as well as the font, size, and so on), =
I=20
remember I used a markup system, for GtkLabels. I cannot find a reference t=
o=20
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=20
newly-inserted text. Equivalent to calling Gtk::TextBuffer#insert(iter,=20
text), then Gtk::TextBuffer#apply_tag on the inserted text.

These tags are instances of class Gtk::TextTag, used to set the colors=20
(foreground and background), size, font, style, ...

I haven't tried yet, so tell us if this works for you !

=2D-=20
Olivier Renaud
 
K

Karl Gabel

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top