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