Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SOLVED, see: http://comments.gmane.org/gmane.comp.gnome.lib.gtk%2B.devel.apps/32646
- using Gtk;
- //compile with: valac --pkg gtk+-3.0 textTagTest.vala
- void main(string[] args)
- {
- Gtk.init(ref args);
- TextView textView;
- Gtk.TextBuffer buffer;
- Gtk.TextIter startIter, currentIter, lastIter, endIter;
- Gtk.TextTag tag;
- Gtk.TextMark lastMark;
- Window win;
- string tagName = "";
- textView = new TextView();
- buffer = textView.buffer;
- buffer.get_start_iter(out startIter);//get the iterator at the beginning.
- lastMark = buffer.create_mark("lastMark", startIter, true);//set a mark at the beginning.
- int loopCounter = 0;
- //The following loop adds three text tags with a unique tag-name to the text buffer.
- //Then it attempts to delete them (buffer.remove_all_tags(...) ).
- //In the next iteration I expect to be able to use the same tag-names as they should have been deleted.
- //But I get errors saying those tag names already exist in the buffer.
- while (loopCounter++ < 2)
- {
- for(int tagCounter = 0; tagCounter < 3; tagCounter++)
- {
- tagName = "tag_" + tagCounter.to_string();//create a unique tagname.
- buffer.set_text(tagName.to_string()+"\n", -1);//write "tag_<num>" into buffer
- buffer.get_iter_at_mark(out lastIter, lastMark);//where was the cursor placed before we wrote text?
- buffer.get_iter_at_offset(out currentIter, buffer.cursor_position);//where is the cursor placed now?
- tag = buffer.create_tag(tagName);//create a tag named "tag_<num>"
- buffer.apply_tag(tag, lastIter, currentIter);//apply the tag from the beginning to the end of the written text.
- buffer.delete_mark(lastMark);
- lastMark = buffer.create_mark("lastMark", currentIter, true);//upcoming text to be placed at the current cursor pos.
- Thread.usleep(1000000);//delay to monitor on stdout.
- }
- //when done, delete everything.
- buffer.get_start_iter(out startIter);//get the very first text iterator
- buffer.get_end_iter(out endIter);//get the very last iterator (redundant, but to be sure).
- buffer.remove_all_tags(startIter, endIter);//remove all created tags.
- buffer.delete(ref startIter, ref endIter);//remove all written text.
- //we should be safe to start over with the tag names "tag_0", "tag_1", "tag_2" again.
- }
- win = new Window();
- win.title = "TextTags";
- win.border_width = 5;
- win.window_position = WindowPosition.CENTER;
- win.set_default_size(655, 480);
- win.add(textView);
- win.show_all();
- Gtk.main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement