More work on calendar
[gregoa/zavai.git] / src / app_calendar.vala
1 /*
2  * app_calendar - zavai calendar applet
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 namespace zavai {
22 namespace ui {
23 namespace calendar {
24
25 public class Calendar : Applet
26 {
27         protected Gtk.Calendar calendar;
28         protected Gtk.ScrolledWindow scroll;
29         protected Gtk.TextBuffer text_buffer;
30         protected Gtk.TextView text;
31
32         public Calendar()
33         {
34                 _label = "Calendar";
35                 calendar = new Gtk.Calendar();
36                 calendar.day_selected += on_day_selected;
37                 calendar.month_changed += on_month_changed;
38                 pack_start(calendar, false, false, 0);
39
40                 text_buffer = new Gtk.TextBuffer(null);
41                 text = new Gtk.TextView.with_buffer(text_buffer);
42                 text.wrap_mode = Gtk.WrapMode.WORD;
43                 text.cursor_visible = false;
44                 text.editable = false;
45                 scroll = new Gtk.ScrolledWindow (null, null);
46                 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
47                 scroll.add(text);
48                 pack_start(scroll, true, true, 0);
49
50                 on_month_changed();
51         }
52
53         private void on_month_changed()
54         {
55                 calendar.clear_marks();
56                 calendar.mark_day(1);
57         }
58
59         private void on_day_selected()
60         {
61                 string day = "%02d/%02d/%04d".printf(calendar.day, calendar.month, calendar.year);
62                 text_buffer.text = day;
63                 Gtk.TextIter iter;
64                 text_buffer.get_iter_at_offset(out iter, 0);
65                 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
66                 text.scroll_mark_onscreen(mark);
67         }
68
69         public override void start()
70         {
71                 // Go to current date
72                 var now = Time.local(time_t());
73                 calendar.year = now.year + 1900;
74                 calendar.month = now.month;
75                 calendar.day = now.day;
76         }
77         public override void stop()
78         {
79         }
80 }
81
82 Calendar calendar;
83
84 public void init()
85 {
86         calendar = new Calendar();
87         zavai.registry.register_applet("ui.calendar", calendar);
88         zavai.registry.getmenu("menu.misc").add_applet("ui.calendar");
89 }
90
91 }
92 }
93 }