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.HBox
142 protected Gtk.Button b_decmore;
143 protected Gtk.Button b_dec;
144 protected Gtk.Button b_inc;
145 protected Gtk.Button b_incmore;
146 static const int BH = 50;
147 static const int BW = 50;
149 public signal void tweaked(IncDec id, int amount);
154 b_decmore = new Gtk.Button.with_label("«");
155 b_decmore.set_size_request(BW, BH);
156 b_dec = new Gtk.Button.with_label("<");
157 b_dec.set_size_request(BW, BH);
158 b_inc = new Gtk.Button.with_label(">");
159 b_inc.set_size_request(BW, BH);
160 b_incmore = new Gtk.Button.with_label("»");
161 b_incmore.set_size_request(BW, BH);
162 pack_start(b_decmore, false, true, 0);
163 pack_start(b_dec, false, true, 0);
164 pack_start(b_inc, false, true, 0);
165 pack_start(b_incmore, false, true, 0);
166 b_decmore.clicked += on_clicked;
167 b_dec.clicked += on_clicked;
168 b_inc.clicked += on_clicked;
169 b_incmore.clicked += on_clicked;
172 protected void on_clicked(Gtk.Button b)
180 else if (b == b_incmore)
185 public class LabelEntry : Gtk.Entry
192 public class AddAbsoluteDeadline : Applet
194 protected LabelEntry dl_label;
195 protected Gtk.Label dl_day;
196 protected IncDec dl_day_tweak;
197 protected Gtk.Label dl_hour;
198 protected IncDec dl_hour_tweak;
199 protected Gtk.Label dl_min;
200 protected IncDec dl_min_tweak;
201 protected time_t dl_time;
202 protected Gtk.Button dl_submit;
204 public AddAbsoluteDeadline()
206 _label = "Add absolute deadline";
209 dl_label = new LabelEntry();
210 dl_label.changed += on_label_changed;
211 var hbox = new Gtk.HBox(false, 0);
212 hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
213 hbox.pack_start(dl_label, true, true, 0);
214 pack_start(hbox, false, false, 0);
216 dl_day = new Gtk.Label("");
217 dl_day_tweak = new IncDec();
218 dl_day_tweak.tweaked += on_tweak;
219 hbox = new Gtk.HBox(false, 0);
220 hbox.pack_start(new Gtk.Label("Day: "), false, false, 0);
221 hbox.pack_start(dl_day, true, true, 0);
222 hbox.pack_start(dl_day_tweak, false, false, 0);
223 pack_start(hbox, false, false, 0);
225 dl_hour = new Gtk.Label("");
226 dl_hour_tweak = new IncDec();
227 dl_hour_tweak.tweaked += on_tweak;
228 hbox = new Gtk.HBox(false, 0);
229 hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
230 hbox.pack_start(dl_hour, true, true, 0);
231 hbox.pack_start(dl_hour_tweak, false, false, 0);
232 pack_start(hbox, false, false, 0);
234 dl_min = new Gtk.Label("");
235 dl_min_tweak = new IncDec();
236 dl_min_tweak.tweaked += on_tweak;
237 hbox = new Gtk.HBox(false, 0);
238 hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
239 hbox.pack_start(dl_min, true, true, 0);
240 hbox.pack_start(dl_min_tweak, false, false, 0);
241 pack_start(hbox, false, false, 0);
243 dl_submit = new Gtk.Button.with_label("Activate");
244 dl_submit.set_sensitive(false);
245 dl_submit.clicked += on_submit;
246 button_box.pack_start(dl_submit, true, true, 0);
249 public override void start()
252 dl_label.set_text("");
256 protected void update()
258 Time t = Time.local(dl_time);
259 dl_day.set_text(t.format("%a %d %b"));
260 dl_hour.set_text(t.format("%H"));
261 dl_min.set_text(t.format("%M"));
262 dl_submit.set_sensitive(dl_label.text == "" ? false : true);
265 protected void on_tweak(IncDec id, int amount)
267 time_t old = dl_time;
268 if (id == dl_day_tweak)
269 dl_time += amount * 3600 * 24;
270 else if (id == dl_hour_tweak)
271 dl_time += amount * 3600;
272 else if (id == dl_min_tweak)
273 dl_time += amount * 60;
274 time_t now = time_t();
280 protected void on_submit(Gtk.Button b)
282 zavai.clock.clock.schedule(new zavai.clock.Alarm(dl_time, dl_label.text));
286 protected void on_label_changed(LabelEntry e)
292 public class AddRelativeDeadline : Applet
294 protected LabelEntry dl_label;
295 protected Gtk.Label dl_hour;
296 protected IncDec dl_hour_tweak;
297 protected Gtk.Label dl_min;
298 protected IncDec dl_min_tweak;
299 protected int dl_time;
300 protected Gtk.Button dl_submit;
302 public AddRelativeDeadline()
304 _label = "Add relative deadline";
307 dl_label = new LabelEntry();
308 dl_label.changed += on_label_changed;
309 var hbox = new Gtk.HBox(false, 0);
310 hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
311 hbox.pack_start(dl_label, true, true, 0);
312 pack_start(hbox, false, false, 0);
314 dl_hour = new Gtk.Label("");
315 dl_hour_tweak = new IncDec();
316 dl_hour_tweak.tweaked += on_tweak;
317 hbox = new Gtk.HBox(false, 0);
318 hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
319 hbox.pack_start(dl_hour, true, true, 0);
320 hbox.pack_start(dl_hour_tweak, false, false, 0);
321 pack_start(hbox, false, false, 0);
323 dl_min = new Gtk.Label("");
324 dl_min_tweak = new IncDec();
325 dl_min_tweak.tweaked += on_tweak;
326 hbox = new Gtk.HBox(false, 0);
327 hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
328 hbox.pack_start(dl_min, true, true, 0);
329 hbox.pack_start(dl_min_tweak, false, false, 0);
330 pack_start(hbox, false, false, 0);
332 dl_submit = new Gtk.Button.with_label("Activate");
333 dl_submit.set_sensitive(false);
334 dl_submit.clicked += on_submit;
335 button_box.pack_start(dl_submit, true, true, 0);
338 public override void start()
341 dl_label.set_text("");
345 protected void update()
347 dl_hour.set_text("%2d".printf(dl_time/60));
348 dl_min.set_text("%2d".printf(dl_time%60));
349 dl_submit.set_sensitive(dl_label.text == "" ? false : true);
352 protected void on_tweak(IncDec id, int amount)
355 if (id == dl_hour_tweak)
356 dl_time += amount * 60;
357 else if (id == dl_min_tweak)
364 protected void on_submit(Gtk.Button b)
366 zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + dl_time * 60, dl_label.text));
370 protected void on_label_changed(LabelEntry e)
376 public class AddDailyDeadline : Applet
378 public AddDailyDeadline()
380 _label = "Add daily deadline";
385 AddAbsoluteDeadline aad;
386 AddRelativeDeadline ard;
387 AddDailyDeadline add;
391 status = new Status("Zavai");
392 zavai.registry.register_applet("zavai.status", status);
394 aad = new AddAbsoluteDeadline();
395 zavai.registry.register_applet("clock.addabsolute", aad);
396 ard = new AddRelativeDeadline();
397 zavai.registry.register_applet("clock.addrelative", ard);
398 add = new AddDailyDeadline();
399 zavai.registry.register_applet("clock.adddaily", add);
401 var menu_misc = zavai.registry.getmenu("menu.misc");
402 menu_misc.add_applet("clock.addabsolute");
403 menu_misc.add_applet("clock.addrelative");
404 menu_misc.add_applet("clock.adddaily");