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