Gtk::TextBuffer colorful

Discussion in 'Ruby' started by Karl Gabel, Mar 15, 2007.

  1. Karl Gabel

    Karl Gabel Guest

    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
     
    Karl Gabel, Mar 15, 2007
    #1
    1. Advertising

  2. 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
     
    Olivier Renaud, Mar 15, 2007
    #2
    1. Advertising

  3. Karl Gabel

    Karl Gabel Guest

    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
     
    Karl Gabel, Mar 15, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    546
  2. Gergely Kontra
    Replies:
    2
    Views:
    273
    Joao Pedrosa
    Jun 18, 2004
  3. WoodHacker
    Replies:
    0
    Views:
    173
    WoodHacker
    Nov 15, 2006
  4. Alex Shulgin
    Replies:
    1
    Views:
    196
    thefed
    Nov 21, 2007
  5. Vincent R
    Replies:
    2
    Views:
    250
    Marcel Müller
    Jan 12, 2013
Loading...

Share This Page