Set zavai to not appear in pagers and taskbars
[gregoa/zavai.git] / src / app.vala
1 /*
2  * app - zavai main window
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24
25 public class Zavai : Gtk.Window, zavai.Resource
26 {
27         public bool visibility = true;
28         public signal void visibility_changed(bool visible);
29
30         zavai.Applet current;
31         string current_name;
32
33         public Zavai()
34         {
35                 title = "Zavai";
36                 current = null;
37                 current_name = null;
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
44         }
45
46         private bool on_visibility(Gdk.Event event)
47         {
48                 visibility = (event.visibility.state == Gdk.VisibilityState.UNOBSCURED);
49                 visibility_changed(visibility);
50                 return true;
51         }
52
53         public void toggle_visibility()
54         {
55                 if (visibility)
56                 {
57                         visible = false;
58                         visibility = false;
59                         visibility_changed(visibility);
60                         //zavai.app.iconify();
61                 } else {
62                         visible = true;
63                         present();
64                 }
65         }
66
67         public void show_applet(string name)
68         {
69                 zavai.Applet applet = zavai.registry.geta(name);
70
71                 // Remove the current applet
72                 if (current != null)
73                 {
74                         current.stop();
75                         remove(current);
76                         current = null;
77                         current_name = null;
78                 }
79
80                 // Add the new applet
81                 current = applet;
82                 current_name = name;
83                 add(current);
84                 current.start();
85                 current.show_all();
86         }
87
88         public void push_applet(string name)
89         {
90 stderr.printf("push applet %s -> %s\n", current_name, name);
91                 zavai.Applet applet = zavai.registry.geta(name);
92
93                 // Remove the current applet
94                 if (current != null)
95                 {
96 stderr.printf("push applet remove %s\n", current_name);
97                         applet.back_link = current_name;
98                         current.stop();
99                         remove(current);
100                         current = null;
101                         current_name = null;
102                 }
103
104 stderr.printf("push applet add %s\n", name);
105                 // Add the new applet
106                 current = applet;
107                 current_name = name;
108                 add(current);
109                 current.start();
110                 current.show_all();
111         }
112
113         public void shutdown()
114         {
115         }
116
117         public void run()
118         {
119                 set_size_request(300, 500);
120                 //fullscreen();
121                 show_all();
122         }
123 }
124 /*
125 class Zavai(gtk.Window, zavai.Resource):
126     def __init__(self, registry, name):
127         super(Zavai, self).__init__()
128         self.registry = registry
129         self.current = None
130         self.activate_resource("menu.main")
131
132     def activate_resource(self, name):
133         widget = self.registry.resource(name)
134         if widget is None:
135             widget = self.registry.resource("menu.main")
136         if isinstance(widget, gtk.Action):
137             widget.activate()
138         else:
139             self.show_widget(name, widget)
140 */
141
142 public abstract class Applet : Gtk.VBox, Resource
143 {
144         // 'label' property: label to show in window title or button names
145         protected string _label;
146         public string label {
147                 get { return this._label; }
148                 set {
149                         if (_label != value)
150                         {
151                                 _label = value;
152                                 label_changed();
153                         }
154                 }
155         }
156         public signal void label_changed();
157
158         // 'back_link' property: link to use to "go back". If null, do not show
159         // a way to go back.
160         protected AppletLink _back_link = null;
161         public string back_link {
162                 get { return _back_link.target; }
163                 set {
164 stderr.printf("Set back link of %s to %s\n", _label, value);
165                         if (value == null && _back_link != null)
166                         {
167                                 _back_link.target = value;
168                                 remove(_back_link);
169                         } else if (value != null) {
170                                 if (_back_link.target == null)
171                                 {
172                                         _back_link.target = value;
173                                         pack_end(_back_link, false, false, 0);
174                                         _back_link.show();
175                                 } else
176                                         _back_link.target = value;
177                         }
178                 }
179         }
180
181         public Applet()
182         {
183                 this.homogeneous = false;
184                 this.spacing = 0;
185                 _back_link = new AppletStraightLink();
186         }
187 /*
188     name = gobject.property(type=str)
189     label = gobject.property(type=str)
190
191     def __init__(self, registry, name, label = None):
192         super(Applet, self).__init__()
193
194         self.zavai_registry = registry
195
196         self.props.name = name
197         if label is None:
198             self.props.label = zavai.default_label(name)
199         else:
200             self.props.label = label
201
202         self.back_link = zavai.LinkButton(registry, zavai.get_parent(name), _("Back"))
203         self.pack_end(self.back_link, False, False)
204
205     def add(self, widget):
206         self.pack_start(widget, True, True)
207 */
208
209         public void shutdown()
210         {
211                 stop();
212         }
213
214         public virtual void start() {}
215         public virtual void stop() {}
216 }
217
218 public class Menu : Applet
219 {
220         public Menu(string label)
221         {
222                 _label = label;
223         }
224
225         public void add_applet(string target)
226         {
227 stderr.printf("menu.add_applet.packpre me %s them %s\n", _label, target);
228                 pack_start(new AppletPushLink(target), false, false, 0);
229 stderr.printf("menu.add_applet.packpost me %s them %s\n", _label, target);
230         }
231
232         public void add_service_toggle(string service_name, string label_start, string label_stop)
233         {
234                 pack_start(new ServiceRequestLink(service_name, label_start, label_stop), false, false, 0);
235         }
236
237         public void add_widget(Gtk.Widget w)
238         {
239                 pack_start(w, false, false, 0);
240         }
241 }
242
243 public class BigButton : Gtk.Button
244 {
245         public BigButton()
246         {
247                 set_size_request(0, zavai.config.min_button_height);
248         }
249 }
250
251 public abstract class AppletLink : BigButton
252 {
253         protected string _target;
254         public string target {
255                 get { return _target; }
256                 set
257                 {
258                         if (_target != null)
259                         {
260                                 Applet a = zavai.registry.geta(_target);
261                                 a.label_changed -= on_label_changed;
262                         }
263                         bool was_shown = _target != null;
264                         _target = value;
265                         if (_target != null)
266                         {
267                                 Applet a = zavai.registry.geta(_target);
268                                 set_label(a.label);
269                                 a.label_changed += on_label_changed;
270                                 if (!was_shown) show();
271                         } else {
272                                 if (was_shown) hide();
273                         }
274                 }
275         }
276
277         private void on_label_changed(Applet a)
278         {
279                 set_label(a.label);
280         }
281
282         private abstract void on_clicked(Gtk.Button src);
283
284         public AppletLink(string? name = null)
285         {
286                 _target = null;
287                 target = name;
288
289                 clicked += on_clicked;
290         }
291 }
292
293 public class AppletStraightLink : AppletLink
294 {
295         private override void on_clicked(Gtk.Button src)
296         {
297 stderr.printf("straight link: %s\n", _target);
298                 if (_target != null)
299                         zavai.app.show_applet(_target);
300         }
301
302         public AppletStraightLink(string? name = null)
303         {
304                 base(name);
305         }
306 }
307
308 public class AppletPushLink : AppletLink
309 {
310         private override void on_clicked(Gtk.Button src)
311         {
312 stderr.printf("push link: %s\n", _target);
313                 if (_target != null)
314                         zavai.app.push_applet(_target);
315         }
316
317         public AppletPushLink(string? name = null)
318         {
319                 base(name);
320         }
321 }
322
323 public class ServiceRequestLink : Gtk.ToggleButton
324 {
325         protected string service_name;
326         protected string label_start;
327         protected string label_stop;
328
329         private void on_toggled(Gtk.Button src)
330         {
331                 Service s = zavai.registry.gets(service_name);
332                 if (get_active())
333                         s.request("servicerequestlink");
334                 else
335                         s.release("servicerequestlink");
336                 set_label(get_active() ? label_stop : label_start);
337         }
338
339         public ServiceRequestLink(string service_name, string label_start, string label_stop)
340         {
341                 this.service_name = service_name;
342                 this.label_start = label_start;
343                 this.label_stop = label_stop;
344                 set_size_request(0, zavai.config.min_button_height);
345                 toggled += on_toggled;
346
347                 set_label(get_active() ? label_stop : label_start);
348         }
349 }
350
351 zavai.Zavai app;
352
353 }