2 * app - zavai main window
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
25 public class Zavai : Gtk.Window, zavai.Resource
27 public bool visibility = true;
28 public signal void visibility_changed(bool visible);
38 destroy += Gtk.main_quit;
39 set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
40 visibility_notify_event += on_visibility;
41 set_skip_pager_hint(true);
42 set_skip_taskbar_hint(true);
43 //set_type_hint(Gdk.WindowTypeHint.DESKTOP);
46 private bool on_visibility(Gdk.Event event)
48 visibility = (event.visibility.state == Gdk.VisibilityState.UNOBSCURED);
49 //visible = visibility;
50 visibility_changed(visibility);
54 public void toggle_visibility()
60 visibility_changed(visibility);
61 //zavai.app.iconify();
65 set_skip_pager_hint(true);
66 set_skip_taskbar_hint(true);
70 public void ensure_visible()
76 set_skip_pager_hint(true);
77 set_skip_taskbar_hint(true);
81 public void ensure_hidden()
87 visibility_changed(visibility);
91 public void show_applet(string name)
93 zavai.Applet applet = zavai.registry.geta(name);
95 // Remove the current applet
104 // Add the new applet
112 public void push_applet(string name)
114 // Make the function idempotent
115 if (current_name == name)
118 //stderr.printf("push applet %s -> %s\n", current_name, name);
119 zavai.Applet applet = zavai.registry.geta(name);
121 // Remove the current applet
124 //stderr.printf("push applet remove %s\n", current_name);
125 applet.back_link = current_name;
132 //stderr.printf("push applet add %s\n", name);
133 // Add the new applet
147 public void back_to_main()
149 show_applet("zavai.status");
152 public void shutdown()
158 set_size_request(300, 500);
163 public void run_script(string command)
165 zavai.log.info("Run program: " + command);
166 string[] args = command.split(" ");
170 Environment.get_home_dir(),
173 SpawnFlags.SEARCH_PATH,
176 } catch (SpawnError e) {
177 zavai.log.error("Running " + command + ": " + e.message);
182 public abstract class Applet : Gtk.VBox, Resource
184 // 'label' property: label to show in window title or button names
185 protected string _label;
186 public string label {
187 get { return this._label; }
196 public signal void label_changed();
198 protected Gtk.HBox button_box;
200 // 'back_link' property: link to use to "go back". If null, do not show
202 protected AppletLink _back_link = null;
203 public string back_link {
204 get { return _back_link.target; }
206 //stderr.printf("Set back link of %s to %s\n", _label, value);
207 if (value == null && _back_link != null)
209 _back_link.target = value;
210 button_box.remove(_back_link);
211 } else if (value != null) {
212 if (_back_link.target == null)
214 _back_link.target = value;
215 button_box.pack_end(_back_link, true, true, 0);
218 _back_link.target = value;
225 button_box = new Gtk.HBox(true, 0);
226 this.homogeneous = false;
228 pack_end(button_box, false, true, 0);
229 _back_link = new AppletStraightLink();
232 public virtual void back()
234 _back_link.activate_applet();
237 public virtual void back_to_main()
239 zavai.app.back_to_main();
242 public void shutdown()
247 public virtual void start() {}
248 public virtual void stop() {}
251 public class Menu : Applet
253 public Menu(string label)
258 public void add_applet(string target)
260 pack_start(new AppletPushLink(target), false, false, 0);
263 public void add_service_toggle(string service_name, string label_start, string label_stop)
265 pack_start(new ServiceRequestLink(service_name, label_start, label_stop), false, false, 0);
268 public void add_widget(Gtk.Widget w)
270 pack_start(w, false, false, 0);
274 public class BigButton : Gtk.Button
278 set_size_request(0, zavai.config.min_button_height);
282 public abstract class AppletLink : BigButton
284 protected string _target;
285 public string target {
286 get { return _target; }
291 Applet a = zavai.registry.geta(_target);
292 a.label_changed -= on_label_changed;
294 bool was_shown = _target != null;
298 Applet a = zavai.registry.geta(_target);
300 a.label_changed += on_label_changed;
301 if (!was_shown) show();
303 if (was_shown) hide();
308 private void on_label_changed(Applet a)
313 private abstract void on_clicked(Gtk.Button src);
315 public AppletLink(string? name = null)
320 clicked += on_clicked;
323 public virtual void activate_applet()
329 public class AppletStraightLink : AppletLink
331 private override void on_clicked(Gtk.Button src)
333 //stderr.printf("straight link: %s\n", _target);
335 zavai.app.show_applet(_target);
338 public AppletStraightLink(string? name = null)
344 public class AppletPushLink : AppletLink
346 private override void on_clicked(Gtk.Button src)
348 //stderr.printf("push link: %s\n", _target);
350 zavai.app.push_applet(_target);
353 public AppletPushLink(string? name = null)
359 public class ServiceRequestLink : Gtk.ToggleButton
361 protected string service_name;
362 protected string label_start;
363 protected string label_stop;
365 private void on_toggled(Gtk.Button src)
367 Service s = zavai.registry.gets(service_name);
369 s.request("servicerequestlink");
371 s.release("servicerequestlink");
372 set_label(get_active() ? label_stop : label_start);
375 public ServiceRequestLink(string service_name, string label_start, string label_stop)
377 this.service_name = service_name;
378 this.label_start = label_start;
379 this.label_stop = label_stop;
380 set_size_request(0, zavai.config.min_button_height);
381 toggled += on_toggled;
383 set_label(get_active() ? label_stop : label_start);
387 public class StatusIcon : Gtk.Button
389 private Gtk.Image image_widget;
393 relief = Gtk.ReliefStyle.NONE;
394 image_widget = new Gtk.Image();
395 image = image_widget;
398 public void set_from_file(string file)
400 image_widget.set_from_file(file);
403 public void install()
405 zavai.ui.main.status.status_icons.pack_start(this, false, false, 0);