Added app_wm
[gregoa/zavai.git] / src / app_wm.vala
1 /*
2  * app_wm - zavai window management functions
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 namespace ui {
25 namespace wm {
26
27 /*
28 class GPSOn(gtk.ToggleAction):
29     states = [_("GPS always on"), _("GPS on when needed")]
30
31     def __init__(self, registry, **kw):
32         self.state = 0
33         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
34
35         self.registry = registry
36         self.set_active(False)
37
38         self.connect("toggled", self.on_toggle)
39
40     def on_toggle(self, *args):
41         self.state = (self.state + 1) % len(self.states)
42         self.set_property("label", self.states[self.state])
43         if self.get_active():
44             self.start()
45         else:
46             self.stop()
47
48     def start(self):
49         self.registry.resource("gps").connect("gps", self)
50
51     def stop(self):
52         self.registry.resource("gps").disconnect("gps", self)
53
54 class GPXTracer(gtk.ToggleAction):
55     states = [_("Start GPX trace"), _("Stop GPX trace")]
56
57     def __init__(self, registry, **kw):
58         self.state = 0
59         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
60
61         self.registry = registry
62         self.set_active(False)
63
64         self.connect("toggled", self.on_toggle)
65
66     def on_toggle(self, *args):
67         self.state = (self.state + 1) % len(self.states)
68         self.set_property("label", self.states[self.state])
69         if self.get_active():
70             self.start()
71         else:
72             self.stop()
73
74     def start(self):
75         zavai.info("GPX trace started")
76         self.registry.resource("gpx").connect("gpx", self)
77
78     def stop(self):
79         zavai.info("GPX trace ended")
80         self.registry.resource("gpx").disconnect("gpx", self)
81 */
82
83 /*
84 class GPXAudioTracer(gtk.ToggleAction):
85     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
86
87     def __init__(self, registry, **kw):
88         self.state = 0
89         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
90
91         self.registry = registry
92         self.recorder = zavai.Recorder(registry)
93         self.set_active(False)
94
95         self.connect("toggled", self.on_toggle)
96
97     def shutdown(self):
98         self.recorder.stop()
99         super(GPXAudioTracer, self).shutdown()
100
101     def on_toggle(self, *args):
102         self.state = (self.state + 1) % len(self.states)
103         self.set_property("label", self.states[self.state])
104         if self.get_active():
105             self.start()
106         else:
107             self.stop()
108
109     def start(self):
110         zavai.info("GPX trace started")
111         gpx = self.registry.resource("gpx")
112         gpx.connect("gpx", self)
113         gpx.add_activity_monitor(self.on_gpx_activity_changed)
114
115     def stop(self):
116         zavai.info("GPX trace ended")
117         gpx = self.registry.resource("gpx")
118         gpx.disconnect("gpx", self)
119         self.recorder.stop()
120         gpx.del_activity_monitor(self.on_gpx_activity_changed)
121
122     def on_gpx_activity_changed(self, gpx, state):
123         if state:
124             self.recorder.start(gpx.basename + ".wav")
125         else:
126             self.recorder.stop()
127 */
128
129 static void print_window_state(Gtk.Window w)
130 {
131         Gdk.WindowState state = w.window.get_state();
132         int istate = (int) state;
133         bool is_icon = (state & Gdk.WindowState.ICONIFIED) != 0;
134         bool is_visible = w.visible;
135         bool is_focus = w.is_focus;
136         bool has_focus = ((Gtk.Widget)w).has_focus;
137         bool is_active = w.is_active;
138         bool is_toplevel = w.is_toplevel();
139         bool is_tfocus = w.has_toplevel_focus;
140
141         stderr.printf("istate: %d; is_icon: %s; is_visible: %s; is_focus: %s; has_focus: %s, is_active: %s; is_toplevel: %s; is_tfocus: %s\n",
142                         istate,
143                         is_icon ? "true" : "false",
144                         is_visible ? "true" : "false",
145                         is_focus ? "true" : "false",
146                         has_focus ? "true" : "false",
147                         is_active ? "true" : "false",
148                         is_toplevel ? "true" : "false",
149                         is_tfocus ? "true" : "false"
150                      );
151 }
152
153 public class RaiseIcon : Gtk.StatusIcon
154 {
155         /*
156         public bool on_focus_in (Gdk.EventFocus event)
157         {
158                 stderr.printf("FOCUS IN %c %d\n", event.send_event, event.in);
159                 //update_icon();
160                 return true;
161         }
162         public bool on_focus_out (Gdk.EventFocus event)
163         {
164                 stderr.printf("FOCUS OUT %c %d\n", event.send_event, event.in);
165                 //update_icon();
166                 return true;
167         }
168         */
169
170         public RaiseIcon()
171         {
172                 activate += on_activate;
173                 zavai.app.visibility_changed += on_visibility_changed;
174                 /*
175                 zavai.app.focus_in_event += on_focus_in;
176                 zavai.app.focus_out_event += on_focus_out;
177                 */
178
179                 update_icon();
180         }
181
182         private void on_visibility_changed(bool visible)
183         {
184                 update_icon();
185         }
186
187         private void on_activate()
188         {
189                 zavai.app.toggle_visibility();
190                 update_icon();
191
192         }
193
194         protected void update_icon()
195         {
196                 string name = zavai.config.icondir + "/";
197                 if (!zavai.app.visibility)
198                         name += "kbd_on.png";
199                 else
200                         name += "kbd_off.png";
201                 set_from_file(name);
202         }
203 }
204
205 public class Test : BigButton
206 {
207     public Test()
208     {
209             set_label("Window state");
210             clicked += on_clicked;
211     }
212
213     protected void on_clicked()
214     {
215             print_window_state(zavai.app);
216     }
217 }
218
219
220 /*
221 public class Waypoint : BigButton
222 {
223     public Waypoint()
224     {
225         set_label("Take waypoint");
226         zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
227         clicked += on_clicked;
228         set_sensitive(zavai.gps.gpx.tracking);
229     }
230
231     protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
232     {
233 stderr.printf("Toggled %d\n", (int)new_state);
234         set_sensitive(new_state);
235     }
236
237     protected void on_clicked()
238     {
239 stderr.printf("Activate\n");
240         zavai.gps.gpx.waypoint();
241     }
242 }
243 */
244
245 /*
246 public class GPSRequestLink : Gtk.ToggleButton
247 {
248         public GPSRequestLink()
249         {
250         // GPS status icon
251         status_icon = new Gtk.StatusIcon();
252         status_icon.set_visible(true);
253         status_icon.activate += on_status_activate;
254         update_icon();
255         }
256
257     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
258     {
259         this.fix_status = fix_status;
260         update_icon();
261     }
262
263         private void on_toggled(Gtk.Button src)
264         {
265                 Service s = zavai.registry.gets(service_name);
266                 if (get_active())
267                         s.request("servicerequestlink");
268                 else
269                         s.release("servicerequestlink");
270                 set_label(get_active() ? label_stop : label_start);
271         update_icon();
272         }
273
274     private void on_status_activate()
275     {
276         set_active(!get_active());
277     }
278 }
279 */
280
281 RaiseIcon raise_icon;
282
283 public void init()
284 {
285         raise_icon = new RaiseIcon();
286         raise_icon.set_visible(true);
287
288         var menu_test = new Test();
289         zavai.registry.getmenu("menu.main").add_widget(menu_test);
290 }
291
292 }
293 }
294 }