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)
106 Gdk.Window w = zavai.app.get_screen().get_active_window();
107 if (w != zavai.app.window)
114 protected void update_icon()
116 string name = zavai.config.icondir + "/";
117 if (!zavai.app.visibility)
118 name += "quit_on.png";
120 name += "quit_off.png";
125 public class WindowList : Applet
127 protected Wnck.Tasklist selector;
128 protected AppletPushLink launcher_link;
130 public WindowList(string label)
133 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
134 pack_start(selector, true, true, 0);
136 launcher_link = new AppletPushLink("wm.launcher");
137 button_box.pack_start(launcher_link, true, true, 0);
141 public class LauncherButton : Gtk.Button
145 public LauncherButton(string name, string exec)
149 clicked += on_clicked;
150 set_size_request(0, zavai.config.min_button_height);
153 public void on_clicked()
155 zavai.log.info("Run program: " + exec);
156 string[] args = exec.split(" ");
157 string[] args1 = new string[args.length + 1];
159 for (int cin = 0; cin < args.length; ++cin)
161 if (args[cin][0] == '%') continue;
162 args1[cout++] = args[cin];
168 Environment.get_home_dir(),
171 SpawnFlags.SEARCH_PATH,
174 } catch (SpawnError e) {
175 zavai.log.error("Launching " + exec + ": " + e.message);
180 public class Launcher: Applet
182 static const string DENTRY_GROUP = "Desktop Entry";
184 public Launcher(string label)
188 var dir = File.new_for_path(zavai.config.homedir);
189 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
192 var icon_theme = Gtk.IconTheme.get_default();
193 Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
194 while ((file_info = enumerator.next_file(null)) != null)
196 if (!file_info.get_name().has_suffix(".desktop")) continue;
198 string pathname = zavai.config.homedir + "/" + file_info.get_name();
199 //stdout.printf("Load %s\n", pathname);
202 var kf = new KeyFile();
204 kf.load_from_file(pathname, KeyFileFlags.NONE);
205 if (! kf.has_group(DENTRY_GROUP)) continue;
206 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
207 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
208 if (kf.has_key(DENTRY_GROUP, "Icon"))
209 icon = kf.get_string(DENTRY_GROUP, "Icon");
211 zavai.log.error("Skipping file " + pathname + ": " + e.message);
214 var name = kf.get_string(DENTRY_GROUP, "Name");
215 var exec = kf.get_string(DENTRY_GROUP, "Exec");
216 var button = new LauncherButton(name, exec);
217 if (icon != null && icon_theme.has_icon(icon))
220 var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
221 button.image = new Gtk.Image.from_pixbuf(pb);
223 zavai.log.error("Skipping icon " + icon + ": " + e.message);
226 button.clicked += a => { this.back_to_main(); zavai.app.ensure_hidden(); };
230 // Create the table with the launcher buttons
231 uint ROWMAX = 5; // Maximum number of rows
232 uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
233 uint rows = (buttons.size + 1) / cols; // ceil(size/cols)
234 var table = new Gtk.Table(rows, cols, true);
236 // Attach the buttons in the table
239 foreach (LauncherButton b in buttons)
241 table.attach(b, col, col+1, row, row+1,
242 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
243 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
253 pack_start(table, true, true, 0);
258 stderr.printf ("Error: %s\n", e.message);
262 for (string file in confdir)
269 public class AppShortcut : Object
271 private StatusIcon icon;
275 icon = new StatusIcon();
277 icon.clicked += on_icon_clicked;
278 icon.set_from_file(zavai.config.icondir + "/apps.png");
281 public void on_icon_clicked()
283 zavai.app.push_applet("wm.list");
287 RaiseIcon raise_icon;
288 CloseOrBack close_or_back;
289 WindowList window_list;
291 AppShortcut app_shortcut;
295 raise_icon = new RaiseIcon();
296 raise_icon.set_visible(true);
298 close_or_back = new CloseOrBack();
299 close_or_back.set_visible(true);
301 app_shortcut = new AppShortcut();
303 launcher = new Launcher("Run program");
304 zavai.registry.register_applet("wm.launcher", launcher);
306 window_list = new WindowList("Apps");
307 zavai.registry.register_applet("wm.list", window_list);
308 zavai.registry.getmenu("menu.main").add_applet("wm.list");
310 //zavai.registry.getmenu("menu.main").add_applet("wm.launcher");