Reputation: 365
I am trying to insert multiple lines of data into my TextView (About 5-10 lines) using the following method :
Glib::RefPtr<Gtk::TextBuffer> buffer = txtView.get_buffer();
buffer->set_text("");
Gtk::TextBuffer::iterator iter;
iter = buffer->get_iter_at_offset(0);
iter = buffer->insert(iter, myString);
Where "myString" is a well formatted string (with end lines set), that is passed from another function. A sample of "myString" would be as follow :
- This is Line One
This is Line Two
Blablabla
This is Line Three
Blablabla
The contents of the string is different on every function call. My problem here is that I am trying to apply different buffers to different lines of the string.
For example, I want to apply a background colour of blue to Line 2, and green to Line 3, and red to Line 5. How could that be done since I'm passing my lines of data into the function as an entire string instead of passing it in line by line. (I could not pass all of them in line by line as my program is multi-threaded and that would not be good).
I have done something like this, but this only change the entire buffer of the TextView, instead of a specific line.
buffer->property_background() = "red";
txtView->set_buffer(buffer);
Upvotes: 1
Views: 2281
Reputation: 518
As andlabs said, you need to use Gtk::TextBuffer::Tag. Please find some snippets for the start. Reference here is https://developer.gnome.org/gtkmm-tutorial/stable/sec-textview-buffer.html.sl
Create the necessary TagTable and Tag, assign properties to the Tag and add the Tag to the TagTable.
m_reftagtable = Gtk::TextBuffer::TagTable::create();
reftagmatch = Gtk::TextBuffer::Tag::create();
reftagmatch->property_background() = "orange";
m_reftagtable->add(reftagmatch);
Create a Textbuffer with the TagTable
m_textbuffer = Gtk::TextBuffer::create(m_reftagtable);
m_textview->set_buffer(m_textbuffer);
Let us assume you have some condition and depending on the boolean state of plaintextcondition you can either add plain text or text with orange background.
iterend = m_textbuffer->get_iter_at_offset(m_textbuffer->get_char_count());
if (plaintextcondition){
m_textbuffer->insert(iterend, "Plain text");
} else {
m_textbuffer->insert_with_tag(iterend, "Orange Text", refTagMatch);
}
Of course this is only a hint. Please refer to the reference for more information.
Upvotes: 1