/* * app_wm - zavai window management functions * * Copyright (C) 2009 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; namespace zavai { namespace ui { namespace polygen { protected class PolygenPage : Object { public string name { get; construct; } public Gtk.ListStore model; public Gtk.TreeView list; public Gtk.ScrolledWindow scroll; public signal void selected(string page, string name); public PolygenPage(string name) { this.name = name; model = new Gtk.ListStore(2, typeof(string), typeof(string)); list = new Gtk.TreeView.with_model(model); list.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText(), "text", 0); list.insert_column_with_attributes (-1, "Title", new Gtk.CellRendererText(), "text", 1); scroll = new Gtk.ScrolledWindow (null, null); scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC); scroll.add(list); list.row_activated += on_row_activated; } private void on_row_activated(Gtk.TreeView tv, Gtk.TreePath path, Gtk.TreeViewColumn column) { Gtk.TreeIter iter; if (!model.get_iter(out iter, path)) return; Value grm_name; model.get_value(iter, 0, out grm_name); selected(name, (string)grm_name); } } protected class PolygenRun : Gtk.VBox { public Gtk.ScrolledWindow scroll; protected Gtk.TextBuffer text_buffer; protected Gtk.TextView text; private string _grammar; public string grammar { get { return _grammar; } set { _grammar = value; update(); } } public PolygenRun() { _grammar = ""; 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 bool on_button_press(Gdk.EventButton event) { update(); return true; } public void update() { 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); } } public class Polygen : Applet { protected Gee.ArrayList pages; protected Gtk.Notebook notebook; protected PolygenRun result; private void add(string page, string name, string title) { PolygenPage pg = null; foreach (PolygenPage p in pages) { if (p.name == page) { pg = p; break; } } if (pg == null) { pg = new PolygenPage(page); pg.selected += on_selected; pages.add(pg); } Gtk.TreeIter iter; pg.model.append (out iter); pg.model.set(iter, 0, name); pg.model.set(iter, 1, title); } protected void on_selected(string page, string name) { result.grammar = page + "/" + name; notebook.set_current_page(notebook.get_n_pages()-1); } public Polygen(string label, IOChannel data) { _label = label; pages = new Gee.ArrayList(); while (true) { string line; var res = data.read_line(out line, null, null); if (res != IOStatus.NORMAL) break; string[] vals = line.split(" ", 2); if (vals == null) break; string[] np = vals[0].split("/", 2); if (np == null) break; add(np[0], np[1], vals[1].strip()); } notebook = new Gtk.Notebook(); foreach (PolygenPage p in pages) { notebook.append_page(p.scroll, new Gtk.Label(p.name)); } result = new PolygenRun(); notebook.append_page(result, new Gtk.Label("Result")); pack_start(notebook, true, true, 0); } } Polygen polygen; public void init() { try { var data = new IOChannel.file(zavai.config.homedir + "/polygen-info", "r"); polygen = new Polygen("Polygen", data); data.shutdown(false); zavai.registry.register_applet("ui.polygen", polygen); zavai.registry.getmenu("menu.main").add_applet("ui.polygen"); } catch (FileError e) { polygen = null; zavai.log.error("Skipping polygen plugin: " + e.message); } /* window_list = new WindowList("Current apps"); zavai.registry.register_applet("wm.list", window_list); zavai.registry.getmenu("menu.main").add_applet("wm.list"); try { launcher = new Launcher("Run program"); } catch (Error e) { zavai.log.error("Not running launcher: " + e.message); launcher = null; } if (launcher != null) { zavai.registry.register_applet("wm.launcher", launcher); zavai.registry.getmenu("menu.main").add_applet("wm.launcher"); } */ } } } }