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