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