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