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);
69 public void ensure_visible()
75 set_skip_pager_hint(true);
76 set_skip_taskbar_hint(true);
80 public void show_applet(string name)
82 zavai.Applet applet = zavai.registry.geta(name);
84 // Remove the current applet
101 public void push_applet(string name)
103 // Make the function idempotent
104 if (current_name == name)
107 //stderr.printf("push applet %s -> %s\n", current_name, name);
108 zavai.Applet applet = zavai.registry.geta(name);
110 // Remove the current applet
113 //stderr.printf("push applet remove %s\n", current_name);
114 applet.back_link = current_name;
121 //stderr.printf("push applet add %s\n", name);
122 // Add the new applet
130 public void shutdown()
136 set_size_request(300, 500);
142 class Zavai(gtk.Window, zavai.Resource):
143 def __init__(self, registry, name):
144 super(Zavai, self).__init__()
145 self.registry = registry
147 self.activate_resource("menu.main")
149 def activate_resource(self, name):
150 widget = self.registry.resource(name)
152 widget = self.registry.resource("menu.main")
153 if isinstance(widget, gtk.Action):
156 self.show_widget(name, widget)
159 public abstract class Applet : Gtk.VBox, Resource
161 // 'label' property: label to show in window title or button names
162 protected string _label;
163 public string label {
164 get { return this._label; }
173 public signal void label_changed();
175 protected Gtk.HBox button_box;
177 // 'back_link' property: link to use to "go back". If null, do not show
179 protected AppletLink _back_link = null;
180 public string back_link {
181 get { return _back_link.target; }
183 //stderr.printf("Set back link of %s to %s\n", _label, value);
184 if (value == null && _back_link != null)
186 _back_link.target = value;
187 button_box.remove(_back_link);
188 } else if (value != null) {
189 if (_back_link.target == null)
191 _back_link.target = value;
192 button_box.pack_end(_back_link, true, true, 0);
195 _back_link.target = value;
202 button_box = new Gtk.HBox(true, 0);
203 this.homogeneous = false;
205 pack_end(button_box, false, true, 0);
206 _back_link = new AppletStraightLink();
209 name = gobject.property(type=str)
210 label = gobject.property(type=str)
212 def __init__(self, registry, name, label = None):
213 super(Applet, self).__init__()
215 self.zavai_registry = registry
217 self.props.name = name
219 self.props.label = zavai.default_label(name)
221 self.props.label = label
223 self.back_link = zavai.LinkButton(registry, zavai.get_parent(name), _("Back"))
224 self.pack_end(self.back_link, False, False)
226 def add(self, widget):
227 self.pack_start(widget, True, True)
230 public void shutdown()
235 public virtual void start() {}
236 public virtual void stop() {}
239 public class Menu : Applet
241 public Menu(string label)
246 public void add_applet(string target)
248 pack_start(new AppletPushLink(target), false, false, 0);
251 public void add_service_toggle(string service_name, string label_start, string label_stop)
253 pack_start(new ServiceRequestLink(service_name, label_start, label_stop), false, false, 0);
256 public void add_widget(Gtk.Widget w)
258 pack_start(w, false, false, 0);
262 public class BigButton : Gtk.Button
266 set_size_request(0, zavai.config.min_button_height);
270 public abstract class AppletLink : BigButton
272 protected string _target;
273 public string target {
274 get { return _target; }
279 Applet a = zavai.registry.geta(_target);
280 a.label_changed -= on_label_changed;
282 bool was_shown = _target != null;
286 Applet a = zavai.registry.geta(_target);
288 a.label_changed += on_label_changed;
289 if (!was_shown) show();
291 if (was_shown) hide();
296 private void on_label_changed(Applet a)
301 private abstract void on_clicked(Gtk.Button src);
303 public AppletLink(string? name = null)
308 clicked += on_clicked;
312 public class AppletStraightLink : AppletLink
314 private override void on_clicked(Gtk.Button src)
316 //stderr.printf("straight link: %s\n", _target);
318 zavai.app.show_applet(_target);
321 public AppletStraightLink(string? name = null)
327 public class AppletPushLink : AppletLink
329 private override void on_clicked(Gtk.Button src)
331 //stderr.printf("push link: %s\n", _target);
333 zavai.app.push_applet(_target);
336 public AppletPushLink(string? name = null)
342 public class ServiceRequestLink : Gtk.ToggleButton
344 protected string service_name;
345 protected string label_start;
346 protected string label_stop;
348 private void on_toggled(Gtk.Button src)
350 Service s = zavai.registry.gets(service_name);
352 s.request("servicerequestlink");
354 s.release("servicerequestlink");
355 set_label(get_active() ? label_stop : label_start);
358 public ServiceRequestLink(string service_name, string label_start, string label_stop)
360 this.service_name = service_name;
361 this.label_start = label_start;
362 this.label_stop = label_stop;
363 set_size_request(0, zavai.config.min_button_height);
364 toggled += on_toggled;
366 set_label(get_active() ? label_stop : label_start);
370 public class CommandButton : Gtk.Button
372 private string command;
374 public CommandButton(string name, string command)
377 this.command = command;
378 clicked += on_clicked;
379 set_size_request(0, zavai.config.min_button_height);
382 public void on_clicked()
384 zavai.log.info("Run program: " + command);
385 string[] args = command.split(" ");
388 Environment.get_home_dir(),
391 SpawnFlags.SEARCH_PATH,