2 * zapolygen - zavai polygen applet
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 protected class PolygenPage : Object
26 public string name { get; construct; }
27 public Gtk.ListStore model;
28 public Gtk.TreeView list;
29 public Gtk.ScrolledWindow scroll;
30 public signal void selected(string page, string name, string type);
32 public PolygenPage(string name)
35 model = new Gtk.ListStore(3, typeof(string), typeof(string), typeof(string));
36 list = new Gtk.TreeView.with_model(model);
37 list.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText(), "text", 0);
38 list.insert_column_with_attributes (-1, "Title", new Gtk.CellRendererText(), "text", 1);
39 scroll = new Gtk.ScrolledWindow (null, null);
40 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
43 list.row_activated += on_row_activated;
48 // Get currently selected/focused
50 Gtk.TreeViewColumn col;
51 list.get_cursor(out path, out col);
54 list.row_activated(path, col);
57 private void on_row_activated(Gtk.TreeView tv, Gtk.TreePath path, Gtk.TreeViewColumn column)
60 if (!model.get_iter(out iter, path)) return;
61 Value grm_name, grm_type;
62 model.get_value(iter, 0, out grm_name);
63 model.get_value(iter, 2, out grm_type);
64 selected(name, (string)grm_name, (string)grm_type);
69 protected class PolygenRun : Gtk.VBox
71 public Gtk.ScrolledWindow scroll;
72 protected Gtk.TextBuffer text_buffer;
73 protected Gtk.TextView text;
74 protected Regex unhtml;
75 protected Regex seplines;
77 public string grm_name { get; set; }
78 public string grm_type { get; set; }
80 public PolygenRun() throws RegexError
84 unhtml = new Regex("[ ]*<[^>]+>[ ]*", 0, 0);
85 seplines = new Regex("\n");
86 text_buffer = new Gtk.TextBuffer(null);
87 text = new Gtk.TextView.with_buffer(text_buffer);
88 text.wrap_mode = Gtk.WrapMode.WORD;
89 text.cursor_visible = false;
90 text.editable = false;
91 text.add_events(Gdk.EventMask.BUTTON_PRESS_MASK);
92 text.button_press_event += on_button_press;
93 scroll = new Gtk.ScrolledWindow (null, null);
94 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
96 pack_start(scroll, true, true, 0);
99 public bool on_button_press(Gdk.EventButton event)
107 if (grm_name == "") return;
108 string[] args = new string[5];
109 args[0] = "/usr/bin/polygen";
110 args[1] = "/usr/share/polygen/" + grm_name + ".grm";
111 if (grm_type == "line/text" || grm_type == "line/html")
122 Process.spawn_async_with_pipes(homedir, args, null, 0, null, null, null, out pipe_out, null);
123 IOChannel ch = new IOChannel.unix_new(pipe_out);
125 ch.read_to_end(out result, out res_len);
128 if (grm_type == "line/html" || grm_type == "block/html")
130 result = unhtml.replace(result, (long)res_len, 0, " ", 0);
131 } else if (grm_type == "line/text") {
132 result = seplines.replace(result, (long)res_len, 0, "\n\n", 0);
137 result = "Error: " + e.message;
139 text_buffer.text = result;
141 text_buffer.get_iter_at_offset(out iter, 0);
142 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
143 text.scroll_mark_onscreen(mark);
147 public class Polygen : Gtk.Window
149 //protected Gee.ArrayList<PolygenPage> pages;
150 protected Gtk.Notebook notebook;
151 protected PolygenRun result;
152 protected Gtk.ListStore grammars;
153 protected FisheyeListView grammarlist;
155 private void add_grammar(string page, string name, string type, string title)
158 PolygenPage pg = null;
159 foreach (PolygenPage p in pages)
169 pg = new PolygenPage(page);
170 pg.selected += on_selected;
175 pg.model.append (out iter);
176 pg.model.set(iter, 0, name);
177 pg.model.set(iter, 1, title);
178 pg.model.set(iter, 2, type);
181 grammars.append (out iter);
182 grammars.set(iter, 0, "%s/%s - %s".printf(page, name, title));
183 grammars.set(iter, 1, "%s/%s".printf(page, name));
184 grammars.set(iter, 2, type);
188 protected void on_selected(string page, string name, string type)
190 result.grm_name = page + "/" + name;
191 result.grm_type = type;
193 notebook.set_current_page(notebook.get_n_pages()-1);
197 public Polygen(string label, IOChannel data) throws ConvertError, IOChannelError, RegexError
200 destroy += Gtk.main_quit;
202 grammars = new Gtk.ListStore(3, typeof(string), typeof(string), typeof(string));
204 var vbox = new Gtk.VBox(false, 0);
207 //pages = new Gee.ArrayList<PolygenPage>();
212 var res = data.read_line(out line, null, null);
213 if (res != IOStatus.NORMAL) break;
214 string[] vals = line.split(" ", 3);
215 if (vals == null) break;
216 string[] np = vals[0].split("/", 2);
217 if (np == null) break;
218 add_grammar(np[0], np[1], vals[1], vals[2].strip());
221 grammarlist = new FisheyeListView();
222 grammarlist.set_model(grammars);
223 grammarlist.row_activated += on_row_activated;
225 notebook = new Gtk.Notebook();
226 notebook.append_page(grammarlist, new Gtk.Label("Grammars"));
228 foreach (PolygenPage p in pages)
230 notebook.append_page(p.scroll, new Gtk.Label(p.name));
233 result = new PolygenRun();
234 notebook.append_page(result, new Gtk.Label("Result"));
236 vbox.pack_start(notebook, true, true, 0);
238 //Gtk.Button select = new Gtk.Button.with_label("Select");
239 //button_box.pack_start(select, true, true, 0);
240 //select.clicked += on_click;
243 public void on_row_activated(Gtk.TreePath path)
246 grammars.get_iter(out iter, path);
249 grammars.get(iter, 1, out grm, 2, out type, -1);
250 stdout.printf("Clicked on %s %s\n", type, grm);
252 result.grm_name = grm;
253 result.grm_type = type;
255 notebook.set_current_page(notebook.get_n_pages()-1);
259 private void on_click(Gtk.Button b)
261 stderr.printf("ZA\n");
262 if (notebook.page >= pages.size)
263 result.update(); // If we are in the result page, just update
266 PolygenPage page = pages[notebook.page];
275 static int main (string[] args) {
278 homedir = GLib.Environment.get_home_dir() + "/.zavai";
281 var data = new IOChannel.file(homedir + "/polygen-info", "r");
282 polygen = new Polygen("Polygen", data);
283 polygen.set_size_request(300, 500);
285 data.shutdown(false);
287 //zavai.registry.register_applet("ui.polygen", polygen);
288 //zavai.registry.getmenu("menu.misc").add_applet("ui.polygen");
290 stderr.printf("%s", e.message);