Skip to content

Instantly share code, notes, and snippets.

@rain-1
Created April 7, 2019 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rain-1/a1d93260456c7dcc0c3d0e411210b6c0 to your computer and use it in GitHub Desktop.
Save rain-1/a1d93260456c7dcc0c3d0e411210b6c0 to your computer and use it in GitHub Desktop.
bold text in vala
using Gtk;
// valac --pkg gtk+-3.0 mytv.vala
public class MarkupTextView : TextView {
public void append_text (string s) {
TextIter end_iter;
buffer.get_end_iter (out end_iter);
buffer.insert_with_tags_by_name (ref end_iter, s, -1, null);
}
public void append_tagged_text (string s, string t) {
TextIter end_iter;
buffer.get_end_iter (out end_iter);
buffer.insert_with_tags_by_name (ref end_iter, s, -1, t, null);
}
}
public static int main (string[] args) {
Gtk.init (ref args);
var window = new Window ();
window.set_default_size (400, 600);
var text_view = new MarkupTextView ();
text_view.editable = false;
//this.text_view.cursor_visible = false;
var mybold_tag = new TextTag ("mybold");
mybold_tag.weight = Pango.Weight.BOLD; //https://valadoc.org/pango/Pango.Weight.html
text_view.buffer.tag_table.add(mybold_tag);
// text_view.buffer.create_tag ("bold", mybold_tag, null);
text_view.append_text("hello\nworld!\nand ");
text_view.append_tagged_text("**bye**", "mybold");
text_view.append_text("; btw\n!");
var scroll = new ScrolledWindow (null, null);
scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
scroll.add (text_view);
window.add (scroll);
window.destroy.connect (Gtk.main_quit);
window.show_all ();
Gtk.main ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment