2 * app_main - zavai main status display
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
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.
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.
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
28 public class StatusBar : Gtk.HBox
36 public class Clock : Gtk.VBox
38 protected Gtk.Label l_date;
39 protected Gtk.Label l_time;
40 protected Gtk.Label l_deadline;
41 protected time_t last_time;
42 protected zavai.clock.SourceType last_time_type;
43 protected time_t last_deadline;
44 protected string last_deadline_label;
49 last_time_type = zavai.clock.SourceType.SYSTEM;
51 last_deadline_label = "";
53 l_date = new Gtk.Label("--");
54 l_date.modify_font(Pango.FontDescription.from_string("Sans 40"));
55 pack_start(l_date, false, false, 0);
56 l_time = new Gtk.Label("--:--");
57 l_time.modify_font(Pango.FontDescription.from_string("Sans 60"));
58 pack_start(l_time, false, false, 0);
59 l_deadline = new Gtk.Label("");
60 l_deadline.modify_font(Pango.FontDescription.from_string("Sans 30"));
61 l_deadline.set_justify(Gtk.Justification.CENTER);
62 pack_start(l_deadline, false, false, 0);
64 zavai.clock.clock.minute_changed += on_minute_changed;
65 zavai.clock.clock.schedule_changed += on_schedule_changed;
67 zavai.clock.clock.request("ui.main.clock");
70 private void on_schedule_changed()
72 zavai.clock.Alarm a = zavai.clock.clock.next_alarm();
76 last_deadline_label = "";
80 last_deadline = a.deadline;
81 last_deadline_label = a.label;
83 on_minute_changed((long)last_time, last_time_type);
86 private void on_minute_changed(long ts, zavai.clock.SourceType type)
88 last_time = (time_t)ts;
89 last_time_type = type;
91 string typetag = "unknown";
94 case zavai.clock.SourceType.GPS:
97 case zavai.clock.SourceType.SYSTEM:
102 var t = Time.local((time_t)ts);
103 l_date.set_text(t.format("%a %d %b"));
104 l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, typetag));
106 if (last_deadline == 0)
107 l_deadline.set_text("");
110 int remaining = (int)(last_deadline - last_time);
111 int hours = remaining / 3600;
112 int minutes = (remaining % 3600) / 60;
113 if (hours == 0 && minutes == 0)
114 l_deadline.set_text(last_deadline_label + "\nanytime now");
116 l_deadline.set_text("%s\non %02dh %02dm".printf(last_deadline_label, hours, minutes));
121 public class Status : Applet
123 public Gtk.HBox status_icons;
125 public AppletPushLink menu;
127 public Status(string label)
130 status_icons = new Gtk.HBox(false, 0);
132 menu = new AppletPushLink("menu.main");
134 pack_start(status_icons, false, false, 0);
135 pack_start(clock, false, false, 0);
136 pack_end(menu, false, false, 0);
140 public class IncDec : Gtk.HButtonBox
142 protected Gtk.Button b_dec;
143 protected Gtk.Button b_inc;
145 public signal void tweaked(IncDec id, int amount);
149 b_dec = new Gtk.Button.with_label("-");
150 b_inc = new Gtk.Button.with_label("+");
151 pack_start(b_dec, false, false, 0);
152 pack_start(b_inc, false, false, 0);
153 b_dec.clicked += on_clicked;
154 b_inc.clicked += on_clicked;
157 protected void on_clicked(Gtk.Button b)
159 tweaked(this, b == b_dec ? -1 : 1);
163 public class LabelEntry : Gtk.Entry
170 public class AddAbsoluteDeadline : Applet
172 protected LabelEntry dl_label;
173 protected Gtk.Label dl_day;
174 protected IncDec dl_day_tweak;
175 protected Gtk.Label dl_hour;
176 protected IncDec dl_hour_tweak;
177 protected Gtk.Label dl_min;
178 protected IncDec dl_min_tweak;
179 protected time_t dl_time;
180 protected Gtk.Button dl_submit;
182 public AddAbsoluteDeadline()
184 _label = "Add absolute deadline";
187 dl_label = new LabelEntry();
188 dl_label.changed += on_label_changed;
189 var hbox = new Gtk.HBox(false, 0);
190 hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
191 hbox.pack_start(dl_label, true, true, 0);
192 pack_start(hbox, false, false, 0);
194 dl_day = new Gtk.Label("");
195 dl_day_tweak = new IncDec();
196 dl_day_tweak.tweaked += on_tweak;
197 hbox = new Gtk.HBox(false, 0);
198 hbox.pack_start(new Gtk.Label("Day: "), false, false, 0);
199 hbox.pack_start(dl_day, true, true, 0);
200 hbox.pack_start(dl_day_tweak, false, false, 0);
201 pack_start(hbox, false, false, 0);
203 dl_hour = new Gtk.Label("");
204 dl_hour_tweak = new IncDec();
205 dl_hour_tweak.tweaked += on_tweak;
206 hbox = new Gtk.HBox(false, 0);
207 hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
208 hbox.pack_start(dl_hour, true, true, 0);
209 hbox.pack_start(dl_hour_tweak, false, false, 0);
210 pack_start(hbox, false, false, 0);
212 dl_min = new Gtk.Label("");
213 dl_min_tweak = new IncDec();
214 dl_min_tweak.tweaked += on_tweak;
215 hbox = new Gtk.HBox(false, 0);
216 hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
217 hbox.pack_start(dl_min, true, true, 0);
218 hbox.pack_start(dl_min_tweak, false, false, 0);
219 pack_start(hbox, false, false, 0);
221 dl_submit = new Gtk.Button.with_label("Activate");
222 dl_submit.set_sensitive(false);
223 dl_submit.clicked += on_submit;
224 button_box.pack_start(dl_submit, true, true, 0);
227 public override void start()
230 dl_label.set_text("");
234 protected void update()
236 Time t = Time.local(dl_time);
237 dl_day.set_text(t.format("%a %d %b"));
238 dl_hour.set_text(t.format("%H"));
239 dl_min.set_text(t.format("%M"));
240 dl_submit.set_sensitive(dl_label.text == "" ? false : true);
243 protected void on_tweak(IncDec id, int amount)
245 time_t old = dl_time;
246 if (id == dl_day_tweak)
247 dl_time += amount * 3600 * 24;
248 else if (id == dl_hour_tweak)
249 dl_time += amount * 3600;
250 else if (id == dl_min_tweak)
251 dl_time += amount * 60;
252 time_t now = time_t();
258 protected void on_submit(Gtk.Button b)
260 zavai.clock.clock.schedule(new zavai.clock.Alarm(dl_time, dl_label.text));
264 protected void on_label_changed(LabelEntry e)
270 public class AddRelativeDeadline : Applet
272 protected LabelEntry dl_label;
273 protected Gtk.Label dl_hour;
274 protected IncDec dl_hour_tweak;
275 protected Gtk.Label dl_min;
276 protected IncDec dl_min_tweak;
277 protected int dl_time;
278 protected Gtk.Button dl_submit;
280 public AddRelativeDeadline()
282 _label = "Add relative deadline";
285 dl_label = new LabelEntry();
286 dl_label.changed += on_label_changed;
287 var hbox = new Gtk.HBox(false, 0);
288 hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
289 hbox.pack_start(dl_label, true, true, 0);
290 pack_start(hbox, false, false, 0);
292 dl_hour = new Gtk.Label("");
293 dl_hour_tweak = new IncDec();
294 dl_hour_tweak.tweaked += on_tweak;
295 hbox = new Gtk.HBox(false, 0);
296 hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
297 hbox.pack_start(dl_hour, true, true, 0);
298 hbox.pack_start(dl_hour_tweak, false, false, 0);
299 pack_start(hbox, false, false, 0);
301 dl_min = new Gtk.Label("");
302 dl_min_tweak = new IncDec();
303 dl_min_tweak.tweaked += on_tweak;
304 hbox = new Gtk.HBox(false, 0);
305 hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
306 hbox.pack_start(dl_min, true, true, 0);
307 hbox.pack_start(dl_min_tweak, false, false, 0);
308 pack_start(hbox, false, false, 0);
310 dl_submit = new Gtk.Button.with_label("Activate");
311 dl_submit.set_sensitive(false);
312 dl_submit.clicked += on_submit;
313 button_box.pack_start(dl_submit, true, true, 0);
316 public override void start()
319 dl_label.set_text("");
323 protected void update()
325 dl_hour.set_text("%2d".printf(dl_time/60));
326 dl_min.set_text("%2d".printf(dl_time%60));
327 dl_submit.set_sensitive(dl_label.text == "" ? false : true);
330 protected void on_tweak(IncDec id, int amount)
333 if (id == dl_hour_tweak)
334 dl_time += amount * 60;
335 else if (id == dl_min_tweak)
342 protected void on_submit(Gtk.Button b)
344 zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + dl_time * 60, dl_label.text));
348 protected void on_label_changed(LabelEntry e)
354 public class AddDailyDeadline : Applet
356 public AddDailyDeadline()
358 _label = "Add daily deadline";
363 AddAbsoluteDeadline aad;
364 AddRelativeDeadline ard;
365 AddDailyDeadline add;
369 status = new Status("Zavai");
370 zavai.registry.register_applet("zavai.status", status);
372 aad = new AddAbsoluteDeadline();
373 zavai.registry.register_applet("clock.addabsolute", aad);
374 ard = new AddRelativeDeadline();
375 zavai.registry.register_applet("clock.addrelative", ard);
376 add = new AddDailyDeadline();
377 zavai.registry.register_applet("clock.adddaily", add);
379 var menu_misc = zavai.registry.getmenu("menu.misc");
380 menu_misc.add_applet("clock.addabsolute");
381 menu_misc.add_applet("clock.addrelative");
382 menu_misc.add_applet("clock.adddaily");