Advertisement
gilzad

unremovable text tags

May 8th, 2015
2,603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.44 KB | None | 0 0
  1. //SOLVED, see: http://comments.gmane.org/gmane.comp.gnome.lib.gtk%2B.devel.apps/32646
  2. using Gtk;
  3.  
  4. //compile with: valac --pkg gtk+-3.0 textTagTest.vala
  5. void main(string[] args)
  6. {
  7.     Gtk.init(ref args);
  8.     TextView textView;
  9.     Gtk.TextBuffer buffer;
  10.     Gtk.TextIter startIter, currentIter, lastIter, endIter;
  11.     Gtk.TextTag tag;
  12.     Gtk.TextMark lastMark;
  13.     Window win;
  14.     string tagName = "";
  15.    
  16.     textView = new TextView();
  17.     buffer = textView.buffer;
  18.    
  19.     buffer.get_start_iter(out startIter);//get the iterator at the beginning.
  20.     lastMark = buffer.create_mark("lastMark", startIter, true);//set a mark at the beginning.
  21.    
  22.     int loopCounter = 0;
  23.    
  24.     //The following loop adds three text tags with a unique tag-name to the text buffer.
  25.     //Then it attempts to delete them (buffer.remove_all_tags(...) ).
  26.     //In the next iteration I expect to be able to use the same tag-names as they should have been deleted.
  27.     //But I get errors saying those tag names already exist in the buffer.
  28.     while (loopCounter++ < 2)
  29.     {
  30.         for(int tagCounter = 0; tagCounter < 3; tagCounter++)
  31.         {
  32.             tagName = "tag_" + tagCounter.to_string();//create a unique tagname.
  33.             buffer.set_text(tagName.to_string()+"\n", -1);//write "tag_<num>" into buffer
  34.             buffer.get_iter_at_mark(out lastIter, lastMark);//where was the cursor placed before we wrote text?
  35.             buffer.get_iter_at_offset(out currentIter, buffer.cursor_position);//where is the cursor placed now?
  36.             tag = buffer.create_tag(tagName);//create a tag named "tag_<num>"
  37.             buffer.apply_tag(tag, lastIter, currentIter);//apply the tag from the beginning to the end of the written text.
  38.             buffer.delete_mark(lastMark);
  39.             lastMark = buffer.create_mark("lastMark", currentIter, true);//upcoming text to be placed at the current cursor pos.
  40.             Thread.usleep(1000000);//delay to monitor on stdout.
  41.         }
  42.         //when done, delete everything.
  43.         buffer.get_start_iter(out startIter);//get the very first text iterator
  44.         buffer.get_end_iter(out endIter);//get the very last iterator (redundant, but to be sure).
  45.         buffer.remove_all_tags(startIter, endIter);//remove all created tags.
  46.         buffer.delete(ref startIter, ref endIter);//remove all written text.
  47.         //we should be safe to start over with the tag names "tag_0", "tag_1", "tag_2" again.
  48.     }
  49.  
  50.     win = new Window();
  51.     win.title = "TextTags";
  52.     win.border_width = 5;
  53.     win.window_position = WindowPosition.CENTER;
  54.     win.set_default_size(655, 480);
  55.     win.add(textView);
  56.     win.show_all();
  57.  
  58.     Gtk.main();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement