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
28 static void print_window_state(Gtk.Window w)
30 Gdk.WindowState state = w.window.get_state();
31 int istate = (int) state;
32 bool is_icon = (state & Gdk.WindowState.ICONIFIED) != 0;
33 bool is_visible = w.visible;
34 bool is_focus = w.is_focus;
35 bool has_focus = ((Gtk.Widget)w).has_focus;
36 bool is_active = w.is_active;
37 bool is_toplevel = w.is_toplevel();
38 bool is_tfocus = w.has_toplevel_focus;
40 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",
42 is_icon ? "true" : "false",
43 is_visible ? "true" : "false",
44 is_focus ? "true" : "false",
45 has_focus ? "true" : "false",
46 is_active ? "true" : "false",
47 is_toplevel ? "true" : "false",
48 is_tfocus ? "true" : "false"
53 public class RaiseIcon : Gtk.StatusIcon
57 activate += on_activate;
58 zavai.app.visibility_changed += on_visibility_changed;
62 private void on_visibility_changed(bool visible)
67 private void on_activate()
69 zavai.app.toggle_visibility();
73 protected void update_icon()
75 string name = zavai.config.icondir + "/";
76 if (!zavai.app.visibility)
77 name += "zavai_on.png";
79 name += "zavai_off.png";
84 public class CloseOrBack : Gtk.StatusIcon
88 activate += on_activate;
89 zavai.app.visibility_changed += on_visibility_changed;
93 private void on_visibility_changed(bool visible)
98 private void on_activate()
100 if (zavai.app.visibility)
105 Gdk.Window w = zavai.app.get_screen().get_active_window();
106 if (w != zavai.app.window)
113 protected void update_icon()
115 string name = zavai.config.icondir + "/";
116 if (!zavai.app.visibility)
117 name += "quit_on.png";
119 name += "quit_off.png";
124 public class WindowList : Applet
126 Wnck.Tasklist selector;
128 public WindowList(string label)
131 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
132 pack_start(selector, true, true, 0);
136 public class LauncherButton : Gtk.Button
140 public LauncherButton(string name, string exec)
144 clicked += on_clicked;
145 set_size_request(0, zavai.config.min_button_height);
148 public void on_clicked()
150 zavai.log.info("Run program: " + exec);
151 string[] args = exec.split(" ");
152 string[] args1 = new string[args.length + 1];
154 for (int cin = 0; cin < args.length; ++cin)
156 if (args[cin][0] == '%') continue;
157 args1[cout++] = args[cin];
162 Environment.get_home_dir(),
165 SpawnFlags.SEARCH_PATH,
171 public class Launcher: Applet
173 static const string DENTRY_GROUP = "Desktop Entry";
175 public Launcher(string label)
179 var dir = File.new_for_path(zavai.config.homedir);
180 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
183 var icon_theme = Gtk.IconTheme.get_default();
184 Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
185 while ((file_info = enumerator.next_file(null)) != null)
187 if (!file_info.get_name().has_suffix(".desktop")) continue;
189 string pathname = zavai.config.homedir + "/" + file_info.get_name();
190 //stdout.printf("Load %s\n", pathname);
193 var kf = new KeyFile();
195 kf.load_from_file(pathname, KeyFileFlags.NONE);
196 if (! kf.has_group(DENTRY_GROUP)) continue;
197 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
198 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
199 if (kf.has_key(DENTRY_GROUP, "Icon"))
200 icon = kf.get_string(DENTRY_GROUP, "Icon");
202 zavai.log.error("Skipping file " + pathname + ": " + e.message);
205 var name = kf.get_string(DENTRY_GROUP, "Name");
206 var exec = kf.get_string(DENTRY_GROUP, "Exec");
207 var button = new LauncherButton(name, exec);
208 if (icon != null && icon_theme.has_icon(icon))
211 var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
212 button.image = new Gtk.Image.from_pixbuf(pb);
214 zavai.log.error("Skipping icon " + icon + ": " + e.message);
220 // Create the table with the launcher buttons
221 uint ROWMAX = 5; // Maximum number of rows
222 uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
223 uint rows = (buttons.size + 1) / cols; // ceil(size/cols)
224 var table = new Gtk.Table(rows, cols, true);
226 // Attach the buttons in the table
229 foreach (LauncherButton b in buttons)
231 table.attach(b, col, col+1, row, row+1,
232 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
233 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
243 pack_start(table, true, true, 0);
248 stderr.printf ("Error: %s\n", e.message);
252 for (string file in confdir)
259 RaiseIcon raise_icon;
260 CloseOrBack close_or_back;
261 WindowList window_list;
266 raise_icon = new RaiseIcon();
267 raise_icon.set_visible(true);
269 close_or_back = new CloseOrBack();
270 close_or_back.set_visible(true);
272 window_list = new WindowList("Current apps");
273 zavai.registry.register_applet("wm.list", window_list);
274 zavai.registry.getmenu("menu.main").add_applet("wm.list");
277 launcher = new Launcher("Run program");
279 zavai.log.error("Not running launcher: " + e.message);
283 if (launcher != null)
285 zavai.registry.register_applet("wm.launcher", launcher);
286 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");