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;
49 private void on_row_activated(Gtk.TreeView tv, Gtk.TreePath path, Gtk.TreeViewColumn column)
52 if (!model.get_iter(out iter, path)) return;
53 Value grm_name, grm_type;
54 model.get_value(iter, 0, out grm_name);
55 model.get_value(iter, 2, out grm_type);
56 selected(name, (string)grm_name, (string)grm_type);
60 protected class PolygenRun : Gtk.VBox
62 public Gtk.ScrolledWindow scroll;
63 protected Gtk.TextBuffer text_buffer;
64 protected Gtk.TextView text;
65 protected Regex unhtml;
66 protected Regex seplines;
68 public string grm_name { get; set; }
69 public string grm_type { get; set; }
75 unhtml = new Regex("[ ]*<[^>]+>[ ]*", 0, 0);
76 seplines = new Regex("\n");
77 text_buffer = new Gtk.TextBuffer(null);
78 text = new Gtk.TextView.with_buffer(text_buffer);
79 text.wrap_mode = Gtk.WrapMode.WORD;
80 text.cursor_visible = false;
81 text.editable = false;
82 text.add_events(Gdk.EventMask.BUTTON_PRESS_MASK);
83 text.button_press_event += on_button_press;
84 scroll = new Gtk.ScrolledWindow (null, null);
85 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
87 pack_start(scroll, true, true, 0);
90 public bool on_button_press(Gdk.EventButton event)
98 if (grm_name == "") return;
99 string[] args = new string[5];
100 args[0] = "/usr/bin/polygen";
101 args[1] = "/usr/share/polygen/" + grm_name + ".grm";
102 if (grm_type == "line/text" || grm_type == "line/html")
111 Process.spawn_async_with_pipes(config.homedir, args, null, 0, null, null, null, out pipe_out, null);
112 IOChannel ch = new IOChannel.unix_new(pipe_out);
115 ch.read_to_end(out result, out res_len);
118 if (grm_type == "line/html" || grm_type == "block/html")
120 result = unhtml.replace(result, (long)res_len, 0, " ", 0);
121 } else if (grm_type == "line/text") {
122 result = seplines.replace(result, (long)res_len, 0, "\n\n", 0);
126 text_buffer.text = result;
128 text_buffer.get_iter_at_offset(out iter, 0);
129 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
130 text.scroll_mark_onscreen(mark);
134 public class Polygen : Applet
136 protected Gee.ArrayList<PolygenPage> pages;
137 protected Gtk.Notebook notebook;
138 protected PolygenRun result;
140 private void add_grammar(string page, string name, string type, string title)
142 PolygenPage pg = null;
143 foreach (PolygenPage p in pages)
153 pg = new PolygenPage(page);
154 pg.selected += on_selected;
159 pg.model.append (out iter);
160 pg.model.set(iter, 0, name);
161 pg.model.set(iter, 1, title);
162 pg.model.set(iter, 2, type);
165 protected void on_selected(string page, string name, string type)
167 result.grm_name = page + "/" + name;
168 result.grm_type = type;
170 notebook.set_current_page(notebook.get_n_pages()-1);
173 public Polygen(string label, IOChannel data)
176 pages = new Gee.ArrayList<PolygenPage>();
181 var res = data.read_line(out line, null, null);
182 if (res != IOStatus.NORMAL) break;
183 string[] vals = line.split(" ", 3);
184 if (vals == null) break;
185 string[] np = vals[0].split("/", 2);
186 if (np == null) break;
187 add_grammar(np[0], np[1], vals[1], vals[2].strip());
190 notebook = new Gtk.Notebook();
191 foreach (PolygenPage p in pages)
193 notebook.append_page(p.scroll, new Gtk.Label(p.name));
195 result = new PolygenRun();
196 notebook.append_page(result, new Gtk.Label("Result"));
198 pack_start(notebook, true, true, 0);
207 var data = new IOChannel.file(zavai.config.homedir + "/polygen-info", "r");
208 polygen = new Polygen("Polygen", data);
209 data.shutdown(false);
211 zavai.registry.register_applet("ui.polygen", polygen);
212 zavai.registry.getmenu("menu.main").add_applet("ui.polygen");
213 } catch (FileError e) {
215 zavai.log.error("Skipping polygen plugin: " + e.message);
219 window_list = new WindowList("Current apps");
220 zavai.registry.register_applet("wm.list", window_list);
221 zavai.registry.getmenu("menu.main").add_applet("wm.list");
224 launcher = new Launcher("Run program");
226 zavai.log.error("Not running launcher: " + e.message);
230 if (launcher != null)
232 zavai.registry.register_applet("wm.launcher", launcher);
233 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");