/* * 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 wm { /* static void print_window_state(Gtk.Window w) { Gdk.WindowState state = w.window.get_state(); int istate = (int) state; bool is_icon = (state & Gdk.WindowState.ICONIFIED) != 0; bool is_visible = w.visible; bool is_focus = w.is_focus; bool has_focus = ((Gtk.Widget)w).has_focus; bool is_active = w.is_active; bool is_toplevel = w.is_toplevel(); bool is_tfocus = w.has_toplevel_focus; stderr.printf("istate: %d; is_icon: %s; is_visible: %s; is_focus: %s; has_focus: %s, is_active: %s; is_toplevel: %s; is_tfocus: %s\n", istate, is_icon ? "true" : "false", is_visible ? "true" : "false", is_focus ? "true" : "false", has_focus ? "true" : "false", is_active ? "true" : "false", is_toplevel ? "true" : "false", is_tfocus ? "true" : "false" ); } */ public class RaiseIcon : Gtk.StatusIcon { public RaiseIcon() { activate += on_activate; zavai.app.visibility_changed += on_visibility_changed; update_icon(); } private void on_visibility_changed(bool visible) { update_icon(); } private void on_activate() { zavai.app.toggle_visibility(); update_icon(); } protected void update_icon() { string name = zavai.config.icondir + "/"; if (!zavai.app.visibility) name += "zavai_on.png"; else name += "zavai_off.png"; set_from_file(name); } } public class CloseOrBack : Gtk.StatusIcon { public CloseOrBack() { activate += on_activate; zavai.app.visibility_changed += on_visibility_changed; update_icon(); } private void on_visibility_changed(bool visible) { update_icon(); } private void on_activate() { if (zavai.app.visibility) { // Back zavai.app.back(); } else { // Close current app Gdk.Window w = zavai.app.get_screen().get_active_window(); if (w != zavai.app.window) { w.destroy(); } } } protected void update_icon() { string name = zavai.config.icondir + "/"; if (!zavai.app.visibility) name += "quit_on.png"; else name += "quit_off.png"; set_from_file(name); } } public class WindowList : Applet { protected Wnck.Tasklist selector; protected AppletPushLink launcher_link; public WindowList(string label) { _label = label; selector = new Wnck.Tasklist(Wnck.Screen.get_default()); pack_start(selector, true, true, 0); launcher_link = new AppletPushLink("wm.launcher"); button_box.pack_start(launcher_link, true, true, 0); } } public class LauncherButton : Gtk.Button { private string exec; public LauncherButton(string name, string exec) { label = name; this.exec = exec; clicked += on_clicked; set_size_request(0, zavai.config.min_button_height); } public void on_clicked() { zavai.log.info("Run program: " + exec); string[] args = exec.split(" "); string[] args1 = new string[args.length + 1]; int cout = 0; for (int cin = 0; cin < args.length; ++cin) { if (args[cin][0] == '%') continue; args1[cout++] = args[cin]; } args1[cout] = null; Pid pid; try { Process.spawn_async( Environment.get_home_dir(), args1, null, SpawnFlags.SEARCH_PATH, null, out pid); } catch (SpawnError e) { zavai.log.error("Launching " + exec + ": " + e.message); } } } public class Launcher: Applet { static const string DENTRY_GROUP = "Desktop Entry"; public Launcher(string label) { _label = label; var dir = File.new_for_path(zavai.config.homedir); var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null); FileInfo file_info; var icon_theme = Gtk.IconTheme.get_default(); Gee.ArrayList buttons = new Gee.ArrayList(); while ((file_info = enumerator.next_file(null)) != null) { if (!file_info.get_name().has_suffix(".desktop")) continue; string pathname = zavai.config.homedir + "/" + file_info.get_name(); //stdout.printf("Load %s\n", pathname); string icon = null; var kf = new KeyFile(); try { kf.load_from_file(pathname, KeyFileFlags.NONE); if (! kf.has_group(DENTRY_GROUP)) continue; if (! kf.has_key(DENTRY_GROUP, "Name")) continue; if (! kf.has_key(DENTRY_GROUP, "Exec")) continue; if (kf.has_key(DENTRY_GROUP, "Icon")) icon = kf.get_string(DENTRY_GROUP, "Icon"); } catch (Error e) { zavai.log.error("Skipping file " + pathname + ": " + e.message); continue; } var name = kf.get_string(DENTRY_GROUP, "Name"); var exec = kf.get_string(DENTRY_GROUP, "Exec"); var button = new LauncherButton(name, exec); if (icon != null && icon_theme.has_icon(icon)) { try { var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0); button.image = new Gtk.Image.from_pixbuf(pb); } catch (Error e) { zavai.log.error("Skipping icon " + icon + ": " + e.message); } } buttons.add(button); } // Create the table with the launcher buttons uint ROWMAX = 5; // Maximum number of rows uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX) uint rows = (buttons.size + 1) / cols; // ceil(size/cols) var table = new Gtk.Table(rows, cols, true); // Attach the buttons in the table uint row = 0; uint col = 0; foreach (LauncherButton b in buttons) { table.attach(b, col, col+1, row, row+1, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0); ++col; if (col == cols) { col = 0; ++row; } } pack_start(table, true, true, 0); /* try { } catch (Error e) { stderr.printf ("Error: %s\n", e.message); return 1; } for (string file in confdir) { } */ } } RaiseIcon raise_icon; CloseOrBack close_or_back; WindowList window_list; Launcher launcher; public void init() { raise_icon = new RaiseIcon(); raise_icon.set_visible(true); close_or_back = new CloseOrBack(); close_or_back.set_visible(true); launcher = new Launcher("Run program"); zavai.registry.register_applet("wm.launcher", launcher); window_list = new WindowList("Current apps"); zavai.registry.register_applet("wm.list", window_list); zavai.registry.getmenu("menu.main").add_applet("wm.list"); //zavai.registry.getmenu("menu.main").add_applet("wm.launcher"); } } } }