2 * app_wm - zavai window management functions
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
27 protected class PolygenPage : Object
29 public string name { get; construct; }
30 public Gtk.ListStore model;
31 public Gtk.TreeView list;
32 public Gtk.ScrolledWindow scroll;
33 public signal void selected(string page, string name, string type);
35 public PolygenPage(string name)
38 model = new Gtk.ListStore(3, typeof(string), typeof(string), typeof(string));
39 list = new Gtk.TreeView.with_model(model);
40 list.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText(), "text", 0);
41 list.insert_column_with_attributes (-1, "Title", new Gtk.CellRendererText(), "text", 1);
42 scroll = new Gtk.ScrolledWindow (null, null);
43 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
46 list.row_activated += on_row_activated;
51 // Get currently selected/focused
53 Gtk.TreeViewColumn col;
54 list.get_cursor(out path, out col);
57 list.row_activated(path, col);
60 private void on_row_activated(Gtk.TreeView tv, Gtk.TreePath path, Gtk.TreeViewColumn column)
63 if (!model.get_iter(out iter, path)) return;
64 Value grm_name, grm_type;
65 model.get_value(iter, 0, out grm_name);
66 model.get_value(iter, 2, out grm_type);
67 selected(name, (string)grm_name, (string)grm_type);
71 protected class PolygenRun : Gtk.VBox
73 public Gtk.ScrolledWindow scroll;
74 protected Gtk.TextBuffer text_buffer;
75 protected Gtk.TextView text;
76 protected Regex unhtml;
77 protected Regex seplines;
79 public string grm_name { get; set; }
80 public string grm_type { get; set; }
86 unhtml = new Regex("[ ]*<[^>]+>[ ]*", 0, 0);
87 seplines = new Regex("\n");
88 text_buffer = new Gtk.TextBuffer(null);
89 text = new Gtk.TextView.with_buffer(text_buffer);
90 text.wrap_mode = Gtk.WrapMode.WORD;
91 text.cursor_visible = false;
92 text.editable = false;
93 text.add_events(Gdk.EventMask.BUTTON_PRESS_MASK);
94 text.button_press_event += on_button_press;
95 scroll = new Gtk.ScrolledWindow (null, null);
96 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
98 pack_start(scroll, true, true, 0);
101 public bool on_button_press(Gdk.EventButton event)
109 if (grm_name == "") return;
110 string[] args = new string[5];
111 args[0] = "/usr/bin/polygen";
112 args[1] = "/usr/share/polygen/" + grm_name + ".grm";
113 if (grm_type == "line/text" || grm_type == "line/html")
122 Process.spawn_async_with_pipes(config.homedir, args, null, 0, null, null, null, out pipe_out, null);
123 IOChannel ch = new IOChannel.unix_new(pipe_out);
126 ch.read_to_end(out result, out res_len);
129 if (grm_type == "line/html" || grm_type == "block/html")
131 result = unhtml.replace(result, (long)res_len, 0, " ", 0);
132 } else if (grm_type == "line/text") {
133 result = seplines.replace(result, (long)res_len, 0, "\n\n", 0);
137 text_buffer.text = result;
139 text_buffer.get_iter_at_offset(out iter, 0);
140 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
141 text.scroll_mark_onscreen(mark);
145 public class Polygen : Applet
147 protected Gee.ArrayList<PolygenPage> pages;
148 protected Gtk.Notebook notebook;
149 protected PolygenRun result;
151 private void add_grammar(string page, string name, string type, string title)
153 PolygenPage pg = null;
154 foreach (PolygenPage p in pages)
164 pg = new PolygenPage(page);
165 pg.selected += on_selected;
170 pg.model.append (out iter);
171 pg.model.set(iter, 0, name);
172 pg.model.set(iter, 1, title);
173 pg.model.set(iter, 2, type);
176 protected void on_selected(string page, string name, string type)
178 result.grm_name = page + "/" + name;
179 result.grm_type = type;
181 notebook.set_current_page(notebook.get_n_pages()-1);
184 public Polygen(string label, IOChannel data)
187 pages = new Gee.ArrayList<PolygenPage>();
192 var res = data.read_line(out line, null, null);
193 if (res != IOStatus.NORMAL) break;
194 string[] vals = line.split(" ", 3);
195 if (vals == null) break;
196 string[] np = vals[0].split("/", 2);
197 if (np == null) break;
198 add_grammar(np[0], np[1], vals[1], vals[2].strip());
201 notebook = new Gtk.Notebook();
202 foreach (PolygenPage p in pages)
204 notebook.append_page(p.scroll, new Gtk.Label(p.name));
206 result = new PolygenRun();
207 notebook.append_page(result, new Gtk.Label("Result"));
209 pack_start(notebook, true, true, 0);
211 Gtk.Button select = new Gtk.Button.with_label("Select");
212 button_box.pack_start(select, true, true, 0);
213 select.clicked += on_click;
216 private void on_click(Gtk.Button b)
218 stderr.printf("ZA\n");
219 if (notebook.page >= pages.size)
220 result.update(); // If we are in the result page, just update
223 PolygenPage page = pages[notebook.page];
233 var menu_misc = new zavai.Menu("Misc");
234 zavai.registry.register_menu("menu.misc", menu_misc);
235 zavai.registry.getmenu("menu.main").add_applet("menu.misc");
238 var data = new IOChannel.file(zavai.config.homedir + "/polygen-info", "r");
239 polygen = new Polygen("Polygen", data);
240 data.shutdown(false);
242 zavai.registry.register_applet("ui.polygen", polygen);
243 zavai.registry.getmenu("menu.misc").add_applet("ui.polygen");
244 } catch (FileError e) {
246 zavai.log.error("Skipping polygen plugin: " + e.message);