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 visibility_changed(visibility);
53 public void toggle_visibility()
59 visibility_changed(visibility);
60 //zavai.app.iconify();
64 set_skip_pager_hint(true);
65 set_skip_taskbar_hint(true);
70 public void hide_window()
74 visibility_changed(visibility);
78 public void ensure_visible()
84 set_skip_pager_hint(true);
85 set_skip_taskbar_hint(true);
89 public void show_applet(string name)
91 zavai.Applet applet = zavai.registry.geta(name);
93 // Remove the current applet
102 // Add the new applet
110 public void push_applet(string name)
112 // Make the function idempotent
113 if (current_name == name)
116 //stderr.printf("push applet %s -> %s\n", current_name, name);
117 zavai.Applet applet = zavai.registry.geta(name);
119 // Remove the current applet
122 //stderr.printf("push applet remove %s\n", current_name);
123 applet.back_link = current_name;
130 //stderr.printf("push applet add %s\n", name);
131 // Add the new applet
145 public void back_to_main()
147 show_applet("zavai.status");
150 public void shutdown()
156 set_size_request(300, 500);
161 public void run_script(string command)
163 zavai.log.info("Run program: " + command);
164 string[] args = command.split(" ");
168 Environment.get_home_dir(),
171 SpawnFlags.SEARCH_PATH,
174 } catch (SpawnError e) {
175 zavai.log.error("Running " + command + ": " + e.message);
180 public abstract class Applet : Gtk.VBox, Resource
182 // 'label' property: label to show in window title or button names
183 protected string _label;
184 public string label {
185 get { return this._label; }
194 public signal void label_changed();
196 protected Gtk.HBox button_box;
198 // 'back_link' property: link to use to "go back". If null, do not show
200 protected AppletLink _back_link = null;
201 public string back_link {
202 get { return _back_link.target; }
204 //stderr.printf("Set back link of %s to %s\n", _label, value);
205 if (value == null && _back_link != null)
207 _back_link.target = value;
208 button_box.remove(_back_link);
209 } else if (value != null) {
210 if (_back_link.target == null)
212 _back_link.target = value;
213 button_box.pack_end(_back_link, true, true, 0);
216 _back_link.target = value;
223 button_box = new Gtk.HBox(true, 0);
224 this.homogeneous = false;
226 pack_end(button_box, false, true, 0);
227 _back_link = new AppletStraightLink();
230 public virtual void back()
232 _back_link.activate_applet();
235 public virtual void back_to_main()
237 zavai.app.back_to_main();
240 public void shutdown()
245 public virtual void start() {}
246 public virtual void stop() {}
249 public class Menu : Applet
251 public Menu(string label)
256 public void add_applet(string target)
258 pack_start(new AppletPushLink(target), false, false, 0);
261 public void add_service_toggle(string service_name, string label_start, string label_stop)
263 pack_start(new ServiceRequestLink(service_name, label_start, label_stop), false, false, 0);
266 public void add_widget(Gtk.Widget w)
268 pack_start(w, false, false, 0);
272 public class BigButton : Gtk.Button
276 set_size_request(0, zavai.config.min_button_height);
280 public abstract class AppletLink : BigButton
282 protected string _target;
283 public string target {
284 get { return _target; }
289 Applet a = zavai.registry.geta(_target);
290 a.label_changed -= on_label_changed;
292 bool was_shown = _target != null;
296 Applet a = zavai.registry.geta(_target);
298 a.label_changed += on_label_changed;
299 if (!was_shown) show();
301 if (was_shown) hide();
306 private void on_label_changed(Applet a)
311 private abstract void on_clicked(Gtk.Button src);
313 public AppletLink(string? name = null)
318 clicked += on_clicked;
321 public virtual void activate_applet()
327 public class AppletStraightLink : AppletLink
329 private override void on_clicked(Gtk.Button src)
331 //stderr.printf("straight link: %s\n", _target);
333 zavai.app.show_applet(_target);
336 public AppletStraightLink(string? name = null)
342 public class AppletPushLink : AppletLink
344 private override void on_clicked(Gtk.Button src)
346 //stderr.printf("push link: %s\n", _target);
348 zavai.app.push_applet(_target);
351 public AppletPushLink(string? name = null)
357 public class ServiceRequestLink : Gtk.ToggleButton
359 protected string service_name;
360 protected string label_start;
361 protected string label_stop;
363 private void on_toggled(Gtk.Button src)
365 Service s = zavai.registry.gets(service_name);
367 s.request("servicerequestlink");
369 s.release("servicerequestlink");
370 set_label(get_active() ? label_stop : label_start);
373 public ServiceRequestLink(string service_name, string label_start, string label_stop)
375 this.service_name = service_name;
376 this.label_start = label_start;
377 this.label_stop = label_stop;
378 set_size_request(0, zavai.config.min_button_height);
379 toggled += on_toggled;
381 set_label(get_active() ? label_stop : label_start);
385 public class StatusIcon : Gtk.Button
387 private Gtk.Image image_widget;
391 relief = Gtk.ReliefStyle.NONE;
392 image_widget = new Gtk.Image();
393 image = image_widget;
396 public void set_from_file(string file)
398 image_widget.set_from_file(file);
401 public void install()
403 zavai.ui.main.status.status_icons.pack_start(this, false, false, 0);