summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2010-06-03 02:37:25 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2010-06-03 02:37:25 +0200
commitcf74195121a2cfc4537cb64c965fa1945c57ffae (patch)
treed8c5868cd9175160e80e4ae0fce767ec9a2da9d6
downloaddclock-cf74195121a2cfc4537cb64c965fa1945c57ffae.tar
dclock-cf74195121a2cfc4537cb64c965fa1945c57ffae.zip
Initial commit
-rw-r--r--.gitignore2
-rw-r--r--DClock.vala242
-rw-r--r--DDate.vala98
-rw-r--r--Makefile7
4 files changed, 349 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fc5a6a3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*~
+DClock
diff --git a/DClock.vala b/DClock.vala
new file mode 100644
index 0000000..5f85d62
--- /dev/null
+++ b/DClock.vala
@@ -0,0 +1,242 @@
+using GLib;
+using Gtk;
+using Pango;
+using Panel;
+
+public class DClock : Panel.Applet {
+ private Gtk.ToggleButton button = null;
+ private Gtk.Label label = null;
+ private Panel.AppletOrient orient = (Panel.AppletOrient)(-1);
+ private BonoboUI.Verb[] verbs = null;
+ private time_t current_time;
+ private uint timeout = 0;
+
+ private int size;
+ private int fixed_width = -1;
+ private int fixed_height = -1;
+ private Gtk.Allocation old_allocation;
+
+ public DClock() {
+ }
+
+ static construct {
+ Gtk.rc_parse_string("\n" +
+ " style \"dclock-applet-button-style\"\n" +
+ " {\n" +
+ " GtkWidget::focus-line-width=0\n" +
+ " GtkWidget::focus-padding=0\n" +
+ " }\n" +
+ "\n" +
+ " widget \"*.dclock-applet-button\" style \"dclock-applet-button-style\"\n" +
+ "\n");
+ }
+
+ private void create() {
+ set_flags(Panel.AppletFlags.EXPAND_MINOR);
+
+ button = new Gtk.ToggleButton();
+ button.set_relief(Gtk.ReliefStyle.NONE);
+ button.set_name("dclock-applet-button");
+ GLib.Signal.connect(button, "button_press_event", (GLib.Callback)do_not_eat_button_press, null);
+ GLib.Signal.connect(this, "change_orient", (GLib.Callback)applet_change_orient, this);
+ GLib.Signal.connect(button, "size_allocate", (GLib.Callback)change_pixel_size, this);
+ this.add(button);
+
+ label = new Gtk.Label(null);
+ GLib.Signal.connect(label, "size_request", (GLib.Callback)clock_size_request, this);
+ GLib.Signal.connect_swapped(label, "style_set", (GLib.Callback)unfix_size, this);
+ label.set_justify(Gtk.Justification.CENTER);
+ clock_update_text_gravity(label);
+ GLib.Signal.connect(label, "screen-changed", (GLib.Callback)clock_update_text_gravity, null);
+ button.add(label);
+
+ set_border_width(0);
+ size = (int)get_size();
+
+ set_background_widget(this);
+
+ verbs = new BonoboUI.Verb[2];
+
+ verbs[0].cname = "About";
+ verbs[0].cb = on_about_clicked;
+ verbs[0].user_data = this;
+
+ verbs[1].cname = null;
+ verbs[1].cb = null;
+ verbs[1].user_data = null;
+
+
+ string menuxml = "<popup name=\"button3\">" +
+ "<menuitem name=\"About Item\" verb=\"About\" _label=\"About\" pixtype=\"stock\" pixname=\"gnome-stock-about\"/>" +
+ "</popup>";
+ setup_menu(menuxml, verbs, this);
+
+ show_all();
+
+ refresh_clock_timeout();
+ applet_change_orient(this, get_orient(), this);
+ }
+
+ private void unfix_size() {
+ fixed_width = -1;
+ fixed_height = -1;
+ button.queue_resize();
+ }
+
+ private static void clock_size_request (Gtk.Widget clock, Gtk.Requisition req, DClock data) {
+ if(req.width > data.fixed_width)
+ data.fixed_width = req.width;
+ if (req.height > data.fixed_height)
+ data.fixed_height = req.height;
+ req.width = data.fixed_width;
+ req.height = data.fixed_height;
+ }
+
+ private static void clock_update_text_gravity(Gtk.Label label) {
+ Pango.Layout layout;
+ Pango.Context context;
+
+ layout = label.get_layout();
+ context = layout.get_context();
+ context.set_base_gravity(Pango.Gravity.AUTO);
+ }
+ private void update_timeformat() {
+
+ }
+
+ private void update_clock() {
+ current_time = time_t();
+ string date_str = DDate.with_time_t(current_time).to_string();
+ string time_str = Time.local(current_time).format("%H:%M");
+
+ label.set_text(date_str + ", " + time_str);
+
+ update_orient();
+ button.queue_resize();
+ }
+
+ /*private void refresh_clock() {
+ unfix_size();
+ update_clock();
+ }*/
+
+ private void refresh_clock_timeout() {
+ unfix_size();
+
+ update_timeformat();
+
+ if (timeout != 0)
+ GLib.Source.remove (timeout);
+
+ update_clock();
+
+ clock_set_timeout(current_time);
+ }
+
+ /*private void refresh_click_timeout_time_only() {
+ if (timeout != 0)
+ GLib.Source.remove (timeout);
+
+ clock_timeout_callback(this);
+ }*/
+
+ private void clock_set_timeout(time_t now) {
+ uint timeouttime;
+
+ GLib.TimeVal tv = GLib.TimeVal();
+
+ timeouttime = (uint)((1000000 - tv.tv_usec)/1000 + 1 + 1000*(59 - now % 60));
+
+ timeout = GLib.Timeout.add (timeouttime, clock_timeout_callback);
+ }
+
+ private bool clock_timeout_callback() {
+ time_t new_time = time_t();
+
+ if(new_time/60 != current_time/60) {
+ update_clock();
+ }
+
+ clock_set_timeout(new_time);
+
+ return false;
+ }
+
+ private static void on_about_clicked(BonoboUI.Component component, void* user_data, string cname) {
+
+ }
+
+ private static bool do_not_eat_button_press(Gtk.Widget widget, Gdk.EventButton event) {
+ if(event.button != 1)
+ GLib.Signal.stop_emission_by_name(widget, "button_press_event");
+
+ return false;
+ }
+
+ private static void change_pixel_size(Gtk.Widget widget, Gtk.Allocation allocation, DClock clock) {
+ if(clock.old_allocation.width == allocation.width && clock.old_allocation.height == allocation.height)
+ return;
+
+ clock.old_allocation.width = allocation.width;
+ clock.old_allocation.height = allocation.height;
+
+ int new_size;
+
+ if(clock.orient == Panel.AppletOrient.LEFT || clock.orient == Panel.AppletOrient.RIGHT)
+ new_size = allocation.width;
+ else
+ new_size = allocation.height;
+
+ clock.size = new_size;
+
+ clock.unfix_size();
+ clock.update_timeformat();
+ clock.update_clock();
+ }
+
+ void update_orient() {
+ /*string text;
+ int min_width;
+ double new_angle;
+ double angle;*/
+
+ //text = gtk_label_get_text (GTK_LABEL (cd->clockw));
+ //min_width = calculate_minimum_width (cd->panel_button, text);
+
+ /*if(orient == Panel.AppletOrient.LEFT && min_width > button.allocation.width)
+ new_angle = 270;
+ else if(orient == Panel.AppletOrient.RIGHT && min_width > button.allocation.width)
+ new_angle = 90;
+ else
+ new_angle = 0;*/
+
+ /*angle = gtk_label_get_angle(GTK_LABEL (cd->clockw));
+ if (angle != new_angle) {
+ unfix_size (cd);
+ gtk_label_set_angle (GTK_LABEL (cd->clockw), new_angle);
+ gtk_label_set_angle (GTK_LABEL (cd->panel_temperature_label), new_angle);
+ }*/
+ }
+
+ private static void applet_change_orient(Panel.Applet applet, Panel.AppletOrient orient, DClock clock) {
+ if(orient == clock.orient)
+ return;
+
+ clock.orient = orient;
+
+ clock.unfix_size();
+ clock.update_clock();
+ //update_calendar_popup (cd);
+ }
+
+ private static bool factory(DClock applet, string iid, void *data) {
+ applet.create();
+ return true;
+ }
+
+ public static int main(string[] args) {
+ Gnome.Program.init("DClock", "0", Gnome.libgnomeui_module, args);
+ return Panel.Applet.factory_main("OAFIID:GNOME_DClock_Factory", typeof(DClock), (Panel.AppletFactoryCallback)factory);
+ }
+}
+
diff --git a/DDate.vala b/DDate.vala
new file mode 100644
index 0000000..54c1064
--- /dev/null
+++ b/DDate.vala
@@ -0,0 +1,98 @@
+using GLib;
+
+public enum DDateMonth {
+ CHAOS = 0, DISCORD = 1, CONFUSION = 2, BUREAUCRACY = 3, THE_AFTERMATH = 4, ST_TIBS_DAY;
+
+ public string to_short_string() {
+ switch(this) {
+ case CHAOS:
+ return "Chs";
+ case DISCORD:
+ return "Dsc";
+ case CONFUSION:
+ return "Cfn";
+ case BUREAUCRACY:
+ return "Bcy";
+ case THE_AFTERMATH:
+ return "Afm";
+ default:
+ return "St. Tib's Day";
+ }
+ }
+}
+
+public enum DDateWeekday {
+ SWEETMORN = 0, BOOMTIME = 1, PUNGENDAY = 2, PRICKLE_PRICKLE = 3, SETTING_ORANGE = 4, ST_TIBS_DAY;
+
+ public string to_short_string() {
+ switch(this) {
+ case SWEETMORN:
+ return "SM";
+ case BOOMTIME:
+ return "BT";
+ case PUNGENDAY:
+ return "PD";
+ case PRICKLE_PRICKLE:
+ return "PP";
+ case SETTING_ORANGE:
+ return "SO";
+ default:
+ return "St. Tib's Day";
+ }
+ }
+}
+
+public struct DDate {
+ private GLib.Date date;
+
+ private uint get_day_of_year() {
+ if (date.get_year().is_leap_year() && date.get_day_of_year() > 60)
+ return date.get_day_of_year()-1;
+ else
+ return date.get_day_of_year();
+ }
+
+ public DDate(GLib.Date date0) {
+ date = date0;
+ }
+
+ public DDate.with_time_t(time_t time) {
+ date = Date();
+ date.set_time_t(time);
+ }
+
+ public bool is_st_tibs_day() {
+ return (date.get_month() == GLib.DateMonth.FEBRUARY && date.get_day() == 29);
+ }
+
+ public uchar get_day() {
+ return (uchar)(get_day_of_year() % 73);
+ }
+
+ public DDateMonth get_month() {
+ if(is_st_tibs_day())
+ return DDateMonth.ST_TIBS_DAY;
+
+ return (DDateMonth)(get_day_of_year()/73);
+ }
+
+ public DDateWeekday get_weekday() {
+ if(is_st_tibs_day())
+ return DDateWeekday.ST_TIBS_DAY;
+
+ return (DDateWeekday)((get_day_of_year()-1)%5);
+ }
+
+ public string to_string() {
+ string ret = "";
+
+ if(is_st_tibs_day()) {
+ ret = "St. Tib's Day";
+ }
+ else {
+ ret = get_weekday().to_short_string() + ", " + get_day().to_string() + ". " + get_month().to_short_string();
+ }
+
+ return ret;
+ }
+}
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0d4c1bc
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+all : DClock
+
+DClock : DClock.vala DDate.vala
+ valac --pkg=libpanelapplet-2.0 DClock.vala DDate.vala
+
+clean :
+ rm -f DClock