]> ToastFreeware Gitweb - gregoa/zavai.git/blob - src/app_main.vala
Bigger buttons, bigger steps
[gregoa/zavai.git] / src / app_main.vala
1 /*
2  * app_main - zavai main status display
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 main {
26
27 /*
28 public class StatusBar : Gtk.HBox
29 {
30     void StatusBar()
31     {
32     }
33 }
34 */
35
36 public class Clock : Gtk.VBox
37 {
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;
45
46     public Clock()
47     {
48         last_time = time_t();
49         last_time_type = zavai.clock.SourceType.SYSTEM;
50         last_deadline = 0;
51         last_deadline_label = "";
52
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);
63
64         zavai.clock.clock.minute_changed += on_minute_changed;
65         zavai.clock.clock.schedule_changed += on_schedule_changed;
66
67         zavai.clock.clock.request("ui.main.clock");
68     }
69
70     private void on_schedule_changed()
71     {
72         zavai.clock.Alarm a = zavai.clock.clock.next_alarm();
73         if (a == null)
74         {
75             last_deadline = 0;
76             last_deadline_label = "";
77         }
78         else
79         {
80             last_deadline = a.deadline;
81             last_deadline_label = a.label;
82         }
83         on_minute_changed((long)last_time, last_time_type);
84     }
85
86     private void on_minute_changed(long ts, zavai.clock.SourceType type)
87     {
88         last_time = (time_t)ts;
89         last_time_type = type;
90
91         string typetag = "unknown";
92         switch (type)
93         {
94             case zavai.clock.SourceType.GPS:
95                 typetag = "gps";
96                 break;
97             case zavai.clock.SourceType.SYSTEM:
98                 typetag = "sys";
99                 break;
100         }
101                 
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));
105
106         if (last_deadline == 0)
107             l_deadline.set_text("");
108         else
109         {
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");
115             else
116                 l_deadline.set_text("%s\non %02dh %02dm".printf(last_deadline_label, hours, minutes));
117         }
118     }
119 }
120
121 public class Status : Applet
122 {
123     public Gtk.HBox status_icons;
124     public Clock clock;
125     public AppletPushLink menu;
126
127         public Status(string label)
128         {
129                 _label = label;
130         status_icons = new Gtk.HBox(false, 0);
131         clock = new Clock();
132         menu = new AppletPushLink("menu.main");
133
134                 pack_start(status_icons, false, false, 0);
135                 pack_start(clock, false, false, 0);
136                 pack_end(menu, false, false, 0);
137         }
138 }
139
140 public class IncDec : Gtk.HBox
141 {
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 = 40;
147     static const int BW = 40;
148
149     public signal void tweaked(IncDec id, int amount);
150
151     public IncDec()
152     {
153         homogeneous = true;
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_dec.clicked += on_clicked;
167         b_inc.clicked += on_clicked;
168     }
169
170     protected void on_clicked(Gtk.Button b)
171     {
172         if (b == b_decmore)
173             tweaked(this, -10);
174         else if (b == b_dec)
175             tweaked(this, -1);
176         else if (b == b_inc)
177             tweaked(this, 1);
178         else if (b == b_incmore)
179             tweaked(this, 10);
180     }
181 }
182
183 public class LabelEntry : Gtk.Entry
184 {
185     public LabelEntry()
186     {
187     }
188 }
189
190 public class AddAbsoluteDeadline : Applet
191 {
192     protected LabelEntry dl_label;
193     protected Gtk.Label dl_day;
194     protected IncDec dl_day_tweak;
195     protected Gtk.Label dl_hour;
196     protected IncDec dl_hour_tweak;
197     protected Gtk.Label dl_min;
198     protected IncDec dl_min_tweak;
199     protected time_t dl_time;
200     protected Gtk.Button dl_submit;
201
202     public AddAbsoluteDeadline()
203     {
204         _label = "Add absolute deadline";
205         dl_time = 0;
206
207         dl_label = new LabelEntry();
208         dl_label.changed += on_label_changed;
209         var hbox = new Gtk.HBox(false, 0);
210         hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
211         hbox.pack_start(dl_label, true, true, 0);
212         pack_start(hbox, false, false, 0);
213
214         dl_day = new Gtk.Label("");
215         dl_day_tweak = new IncDec();
216         dl_day_tweak.tweaked += on_tweak;
217         hbox = new Gtk.HBox(false, 0);
218         hbox.pack_start(new Gtk.Label("Day: "), false, false, 0);
219         hbox.pack_start(dl_day, true, true, 0);
220         hbox.pack_start(dl_day_tweak, false, false, 0);
221         pack_start(hbox, false, false, 0);
222
223         dl_hour = new Gtk.Label("");
224         dl_hour_tweak = new IncDec();
225         dl_hour_tweak.tweaked += on_tweak;
226         hbox = new Gtk.HBox(false, 0);
227         hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
228         hbox.pack_start(dl_hour, true, true, 0);
229         hbox.pack_start(dl_hour_tweak, false, false, 0);
230         pack_start(hbox, false, false, 0);
231
232         dl_min = new Gtk.Label("");
233         dl_min_tweak = new IncDec();
234         dl_min_tweak.tweaked += on_tweak;
235         hbox = new Gtk.HBox(false, 0);
236         hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
237         hbox.pack_start(dl_min, true, true, 0);
238         hbox.pack_start(dl_min_tweak, false, false, 0);
239         pack_start(hbox, false, false, 0);
240
241         dl_submit = new Gtk.Button.with_label("Activate");
242         dl_submit.set_sensitive(false);
243         dl_submit.clicked += on_submit;
244         button_box.pack_start(dl_submit, true, true, 0);
245     }
246
247         public override void start()
248     {
249         dl_time = time_t();
250         dl_label.set_text("");
251         update();
252     }
253
254     protected void update()
255     {
256         Time t = Time.local(dl_time);
257         dl_day.set_text(t.format("%a %d %b"));
258         dl_hour.set_text(t.format("%H"));
259         dl_min.set_text(t.format("%M"));
260         dl_submit.set_sensitive(dl_label.text == "" ? false : true);
261     }
262
263     protected void on_tweak(IncDec id, int amount)
264     {
265         time_t old = dl_time;
266         if (id == dl_day_tweak)
267             dl_time += amount * 3600 * 24;
268         else if (id == dl_hour_tweak)
269             dl_time += amount * 3600;
270         else if (id == dl_min_tweak)
271             dl_time += amount * 60;
272         time_t now = time_t();
273         if (dl_time < now)
274             dl_time = old;
275         update();
276     }
277
278     protected void on_submit(Gtk.Button b)
279     {
280         zavai.clock.clock.schedule(new zavai.clock.Alarm(dl_time, dl_label.text));
281         back();
282     }
283
284     protected void on_label_changed(LabelEntry e)
285     {
286         update();
287     }
288 }
289
290 public class AddRelativeDeadline : Applet
291 {
292     protected LabelEntry dl_label;
293     protected Gtk.Label dl_hour;
294     protected IncDec dl_hour_tweak;
295     protected Gtk.Label dl_min;
296     protected IncDec dl_min_tweak;
297     protected int dl_time;
298     protected Gtk.Button dl_submit;
299
300     public AddRelativeDeadline()
301     {
302         _label = "Add relative deadline";
303         dl_time = 0;
304
305         dl_label = new LabelEntry();
306         dl_label.changed += on_label_changed;
307         var hbox = new Gtk.HBox(false, 0);
308         hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
309         hbox.pack_start(dl_label, true, true, 0);
310         pack_start(hbox, false, false, 0);
311
312         dl_hour = new Gtk.Label("");
313         dl_hour_tweak = new IncDec();
314         dl_hour_tweak.tweaked += on_tweak;
315         hbox = new Gtk.HBox(false, 0);
316         hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
317         hbox.pack_start(dl_hour, true, true, 0);
318         hbox.pack_start(dl_hour_tweak, false, false, 0);
319         pack_start(hbox, false, false, 0);
320
321         dl_min = new Gtk.Label("");
322         dl_min_tweak = new IncDec();
323         dl_min_tweak.tweaked += on_tweak;
324         hbox = new Gtk.HBox(false, 0);
325         hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
326         hbox.pack_start(dl_min, true, true, 0);
327         hbox.pack_start(dl_min_tweak, false, false, 0);
328         pack_start(hbox, false, false, 0);
329
330         dl_submit = new Gtk.Button.with_label("Activate");
331         dl_submit.set_sensitive(false);
332         dl_submit.clicked += on_submit;
333         button_box.pack_start(dl_submit, true, true, 0);
334     }
335
336         public override void start()
337     {
338         dl_time = 0;
339         dl_label.set_text("");
340         update();
341     }
342
343     protected void update()
344     {
345         dl_hour.set_text("%2d".printf(dl_time/60));
346         dl_min.set_text("%2d".printf(dl_time%60));
347         dl_submit.set_sensitive(dl_label.text == "" ? false : true);
348     }
349
350     protected void on_tweak(IncDec id, int amount)
351     {
352         int old = dl_time;
353         if (id == dl_hour_tweak)
354             dl_time += amount * 60;
355         else if (id == dl_min_tweak)
356             dl_time += amount;
357         if (dl_time < 0)
358             dl_time = old;
359         update();
360     }
361
362     protected void on_submit(Gtk.Button b)
363     {
364         zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + dl_time * 60, dl_label.text));
365         back();
366     }
367
368     protected void on_label_changed(LabelEntry e)
369     {
370         update();
371     }
372 }
373
374 public class AddDailyDeadline : Applet
375 {
376     public AddDailyDeadline()
377     {
378         _label = "Add daily deadline";
379     }
380 }
381
382 Status status;
383 AddAbsoluteDeadline aad;
384 AddRelativeDeadline ard;
385 AddDailyDeadline add;
386
387 public void init()
388 {
389     status = new Status("Zavai");
390     zavai.registry.register_applet("zavai.status", status);
391
392     aad = new AddAbsoluteDeadline();
393     zavai.registry.register_applet("clock.addabsolute", aad);
394     ard = new AddRelativeDeadline();
395     zavai.registry.register_applet("clock.addrelative", ard);
396     add = new AddDailyDeadline();
397     zavai.registry.register_applet("clock.adddaily", add);
398
399     var menu_misc = zavai.registry.getmenu("menu.misc");
400         menu_misc.add_applet("clock.addabsolute");
401         menu_misc.add_applet("clock.addrelative");
402         menu_misc.add_applet("clock.adddaily");
403 }
404
405 }
406 }
407 }