Show polygen results in a clickable text area
authorEnrico Zini <enrico@enricozini.org>
Thu, 13 Aug 2009 15:33:48 +0000 (16:33 +0100)
committerEnrico Zini <enrico@enricozini.org>
Thu, 13 Aug 2009 15:33:48 +0000 (16:33 +0100)
src/app_polygen.vala

index af01b8fd4061d58f3c6af5d56d3eae78414f0e2d..7ee4ea86cae8691e5a999eaa998995240ef86d31 100644 (file)
@@ -58,8 +58,9 @@ protected class PolygenPage : Object
 
 protected class PolygenRun : Gtk.VBox
 {
-    protected Gtk.Label text;
-    protected Gtk.Button text_button;
+    public Gtk.ScrolledWindow scroll;
+    protected Gtk.TextBuffer text_buffer;
+    protected Gtk.TextView text;
 
     private string _grammar;
     public string grammar {
@@ -73,20 +74,43 @@ protected class PolygenRun : Gtk.VBox
     public PolygenRun()
     {
         _grammar = "";
-        text = new Gtk.Label("");
-        text.use_markup = true;
-        text.wrap = true;
-        text_button = new Gtk.Button();
-        text_button.set_image(text);
-               pack_start(text_button, true, true, 0);
-        text_button.clicked += on_clicked;
+        text_buffer = new Gtk.TextBuffer(null);
+        text = new Gtk.TextView.with_buffer(text_buffer);
+        text.wrap_mode = Gtk.WrapMode.WORD;
+        text.cursor_visible = false;
+        text.editable = false;
+        text.add_events(Gdk.EventMask.BUTTON_PRESS_MASK);
+        text.button_press_event += on_button_press;
+        scroll = new Gtk.ScrolledWindow (null, null);
+        scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+        scroll.add(text);
+               pack_start(scroll, true, true, 0);
     }
 
-    public void on_clicked(Gtk.Button button) { update(); }
+    public bool on_button_press(Gdk.EventButton event)
+    {
+        update();
+        return true;
+    }
 
     public void update()
     {
-        text.label = "ciao " + _grammar;
+        string[] args = new string[3];
+        args[0] = "/usr/bin/polygen";
+        args[1] = "/usr/share/polygen/" + _grammar + ".grm";
+        args[2] = null;
+        int pipe_out;
+        Process.spawn_async_with_pipes(config.homedir, args, null, 0, null, null, null, out pipe_out, null);
+        IOChannel ch = new IOChannel.unix_new(pipe_out);
+        string result;
+        ch.read_to_end(out result, null);
+        ch.shutdown(false);
+
+        text_buffer.text = result;
+        Gtk.TextIter iter;
+        text_buffer.get_iter_at_offset(out iter, 0);
+        Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
+        text.scroll_mark_onscreen(mark);
     }
 }